1
- use std:: ffi:: c_void;
2
- use std:: ptr:: NonNull ;
3
- use std:: sync:: OnceLock ;
4
-
1
+ use crate :: context_attributes:: ContextAttributes ;
5
2
use icrate:: objc2:: rc:: Id ;
6
3
use icrate:: objc2:: { class, msg_send, msg_send_id} ;
7
4
use objc2_foundation:: NSObject ;
8
5
use raw_window_handle:: RawWindowHandle ;
9
-
10
- use crate :: context_attributes:: ContextAttributes ;
6
+ use std:: ffi:: c_void;
7
+ use std:: ptr:: NonNull ;
8
+ use std:: sync:: OnceLock ;
11
9
12
10
pub static IS_GL_SYMBOLS_LOADED : OnceLock < bool > = OnceLock :: new ( ) ;
13
11
@@ -178,15 +176,16 @@ impl NSOpenGLContext {
178
176
pub struct NSOpenGLPixelFormat ( Id < NSObject > ) ;
179
177
180
178
impl NSOpenGLPixelFormat {
181
- pub fn init_with_attributes ( attribs : & [ u32 ] ) -> Self {
179
+ pub fn init_with_attributes ( attribs : & [ u32 ] ) -> Option < Self > {
182
180
let cls = class ! ( NSOpenGLPixelFormat ) ;
183
- let instance = unsafe { msg_send_id ! [ cls, alloc] } ;
184
- NSOpenGLPixelFormat ( unsafe {
181
+ let alloc = unsafe { msg_send_id ! [ cls, alloc] } ;
182
+ let instance : Option < Id < NSObject > > = unsafe {
185
183
msg_send_id ! [
186
- instance ,
184
+ alloc ,
187
185
initWithAttributes: attribs. as_ptr( )
188
186
]
189
- } )
187
+ } ;
188
+ instance. map ( |id| NSOpenGLPixelFormat ( id) )
190
189
}
191
190
}
192
191
@@ -241,16 +240,18 @@ impl NSOpenGLView {
241
240
#[ derive( Debug , Default ) ]
242
241
pub struct GLContext ( GLContextInner ) ;
243
242
244
- fn parse_context_attributes ( context_attrs : & ContextAttributes ) -> NSOpenGLPixelFormat {
243
+ fn parse_context_attributes ( context_attrs : & ContextAttributes , accelerated : bool ) -> Option < NSOpenGLPixelFormat > {
245
244
let mut attributes = vec ! [
246
- NSOpenGLPixelFormatAttribute :: NSOpenGLPFAAccelerated as u32 ,
247
- NSOpenGLPixelFormatAttribute :: NSOpenGLPFADoubleBuffer as u32 ,
248
245
NSOpenGLPixelFormatAttribute :: NSOpenGLPFAOpenGLProfile as u32 ,
249
246
NSOpenGLPFAOpenGLProfiles :: NSOpenGLProfileVersion3_2Core as u32 ,
250
247
NSOpenGLPixelFormatAttribute :: NSOpenGLPFAColorSize as u32 ,
251
248
24 ,
252
249
] ;
253
250
251
+ if accelerated {
252
+ attributes. push ( NSOpenGLPixelFormatAttribute :: NSOpenGLPFAAccelerated as u32 ) ;
253
+ }
254
+
254
255
if context_attrs. get_alpha ( ) {
255
256
attributes. push ( NSOpenGLPixelFormatAttribute :: NSOpenGLPFAAlphaSize as u32 ) ;
256
257
attributes. push ( 8 ) ;
@@ -274,6 +275,9 @@ fn parse_context_attributes(context_attrs: &ContextAttributes) -> NSOpenGLPixelF
274
275
attributes. push ( 4 ) ;
275
276
}
276
277
278
+ attributes. push ( NSOpenGLPixelFormatAttribute :: NSOpenGLPFADoubleBuffer as u32 ) ;
279
+ attributes. push ( 0 ) ;
280
+
277
281
NSOpenGLPixelFormat :: init_with_attributes ( attributes. as_slice ( ) )
278
282
}
279
283
@@ -363,14 +367,26 @@ impl GLContext {
363
367
NSOpenGLPixelFormatAttribute :: NSOpenGLPFAOpenGLProfile as u32 ,
364
368
NSOpenGLPFAOpenGLProfiles :: NSOpenGLProfileVersion3_2Core as u32 ,
365
369
0 ,
366
- ] ) ;
370
+ ] ) ? ;
367
371
NSOpenGLContext :: new ( format, None )
368
372
}
369
373
} ;
370
374
371
- let format = parse_context_attributes ( context_attrs) ;
375
+ let mut format = parse_context_attributes ( context_attrs, true ) ;
372
376
373
- let context = NSOpenGLContext :: new ( format, share_group. clone ( ) ) ;
377
+ if format. is_none ( ) {
378
+ if context_attrs. get_antialias ( ) {
379
+ context_attrs. set_antialias ( false ) ;
380
+ }
381
+
382
+ if context_attrs. get_preserve_drawing_buffer ( ) {
383
+ context_attrs. set_preserve_drawing_buffer ( false ) ;
384
+ }
385
+
386
+ format = parse_context_attributes ( context_attrs, false ) ;
387
+ }
388
+
389
+ let context = NSOpenGLContext :: new ( format?, share_group. clone ( ) ) ;
374
390
375
391
376
392
if context. is_none ( ) {
@@ -393,9 +409,24 @@ impl GLContext {
393
409
width : i32 ,
394
410
height : i32 ,
395
411
) -> Option < Self > {
396
- let format = parse_context_attributes ( context_attrs) ;
412
+ let mut format = parse_context_attributes ( context_attrs, true ) ;
413
+
414
+ if format. is_none ( ) {
415
+ if context_attrs. get_antialias ( ) {
416
+ context_attrs. set_antialias ( false ) ;
417
+ }
418
+
419
+ if context_attrs. get_preserve_drawing_buffer ( ) {
420
+ context_attrs. set_preserve_drawing_buffer ( false ) ;
421
+ }
422
+
423
+ format = parse_context_attributes ( context_attrs, false ) ;
424
+ }
425
+
426
+
397
427
let view =
398
- NSOpenGLView :: new_with_frame_pixel_format ( 0. , 0. , width as f32 , height as f32 , format) ;
428
+ NSOpenGLView :: new_with_frame_pixel_format ( 0. , 0. , width as f32 , height as f32 , format?) ;
429
+
399
430
400
431
GLContext :: create_window_context_with_gl_view ( context_attrs, view, None )
401
432
}
0 commit comments