1- use std:: ffi:: c_void;
2- use std:: ptr:: NonNull ;
3- use std:: sync:: OnceLock ;
4-
1+ use crate :: context_attributes:: ContextAttributes ;
52use icrate:: objc2:: rc:: Id ;
63use icrate:: objc2:: { class, msg_send, msg_send_id} ;
74use objc2_foundation:: NSObject ;
85use 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 ;
119
1210pub static IS_GL_SYMBOLS_LOADED : OnceLock < bool > = OnceLock :: new ( ) ;
1311
@@ -178,15 +176,16 @@ impl NSOpenGLContext {
178176pub struct NSOpenGLPixelFormat ( Id < NSObject > ) ;
179177
180178impl NSOpenGLPixelFormat {
181- pub fn init_with_attributes ( attribs : & [ u32 ] ) -> Self {
179+ pub fn init_with_attributes ( attribs : & [ u32 ] ) -> Option < Self > {
182180 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 {
185183 msg_send_id ! [
186- instance ,
184+ alloc ,
187185 initWithAttributes: attribs. as_ptr( )
188186 ]
189- } )
187+ } ;
188+ instance. map ( |id| NSOpenGLPixelFormat ( id) )
190189 }
191190}
192191
@@ -241,16 +240,18 @@ impl NSOpenGLView {
241240#[ derive( Debug , Default ) ]
242241pub struct GLContext ( GLContextInner ) ;
243242
244- fn parse_context_attributes ( context_attrs : & ContextAttributes ) -> NSOpenGLPixelFormat {
243+ fn parse_context_attributes ( context_attrs : & ContextAttributes , accelerated : bool ) -> Option < NSOpenGLPixelFormat > {
245244 let mut attributes = vec ! [
246- NSOpenGLPixelFormatAttribute :: NSOpenGLPFAAccelerated as u32 ,
247- NSOpenGLPixelFormatAttribute :: NSOpenGLPFADoubleBuffer as u32 ,
248245 NSOpenGLPixelFormatAttribute :: NSOpenGLPFAOpenGLProfile as u32 ,
249246 NSOpenGLPFAOpenGLProfiles :: NSOpenGLProfileVersion3_2Core as u32 ,
250247 NSOpenGLPixelFormatAttribute :: NSOpenGLPFAColorSize as u32 ,
251248 24 ,
252249 ] ;
253250
251+ if accelerated {
252+ attributes. push ( NSOpenGLPixelFormatAttribute :: NSOpenGLPFAAccelerated as u32 ) ;
253+ }
254+
254255 if context_attrs. get_alpha ( ) {
255256 attributes. push ( NSOpenGLPixelFormatAttribute :: NSOpenGLPFAAlphaSize as u32 ) ;
256257 attributes. push ( 8 ) ;
@@ -274,6 +275,9 @@ fn parse_context_attributes(context_attrs: &ContextAttributes) -> NSOpenGLPixelF
274275 attributes. push ( 4 ) ;
275276 }
276277
278+ attributes. push ( NSOpenGLPixelFormatAttribute :: NSOpenGLPFADoubleBuffer as u32 ) ;
279+ attributes. push ( 0 ) ;
280+
277281 NSOpenGLPixelFormat :: init_with_attributes ( attributes. as_slice ( ) )
278282}
279283
@@ -363,14 +367,26 @@ impl GLContext {
363367 NSOpenGLPixelFormatAttribute :: NSOpenGLPFAOpenGLProfile as u32 ,
364368 NSOpenGLPFAOpenGLProfiles :: NSOpenGLProfileVersion3_2Core as u32 ,
365369 0 ,
366- ] ) ;
370+ ] ) ? ;
367371 NSOpenGLContext :: new ( format, None )
368372 }
369373 } ;
370374
371- let format = parse_context_attributes ( context_attrs) ;
375+ let mut format = parse_context_attributes ( context_attrs, true ) ;
372376
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 ( ) ) ;
374390
375391
376392 if context. is_none ( ) {
@@ -393,9 +409,24 @@ impl GLContext {
393409 width : i32 ,
394410 height : i32 ,
395411 ) -> 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+
397427 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+
399430
400431 GLContext :: create_window_context_with_gl_view ( context_attrs, view, None )
401432 }
0 commit comments