gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/app/gl_macos.m (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  // +build darwin,!ios,nometal
     4  
     5  #import <AppKit/AppKit.h>
     6  #include <CoreFoundation/CoreFoundation.h>
     7  #include <OpenGL/OpenGL.h>
     8  #include "_cgo_export.h"
     9  
    10  CALayer *gio_layerFactory(void) {
    11  	@autoreleasepool {
    12  		return [CALayer layer];
    13  	}
    14  }
    15  
    16  CFTypeRef gio_createGLContext(void) {
    17  	@autoreleasepool {
    18  		NSOpenGLPixelFormatAttribute attr[] = {
    19  			NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
    20  			NSOpenGLPFAColorSize,     24,
    21  			NSOpenGLPFAAccelerated,
    22  			// Opt-in to automatic GPU switching. CGL-only property.
    23  			kCGLPFASupportsAutomaticGraphicsSwitching,
    24  			NSOpenGLPFAAllowOfflineRenderers,
    25  			0
    26  		};
    27  		NSOpenGLPixelFormat *pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
    28  
    29  		NSOpenGLContext *ctx = [[NSOpenGLContext alloc] initWithFormat:pixFormat shareContext: nil];
    30  		return CFBridgingRetain(ctx);
    31  	}
    32  }
    33  
    34  void gio_setContextView(CFTypeRef ctxRef, CFTypeRef viewRef) {
    35  	NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
    36  	NSView *view = (__bridge NSView *)viewRef;
    37  	[view setWantsBestResolutionOpenGLSurface:YES];
    38  	[ctx setView:view];
    39  }
    40  
    41  void gio_clearCurrentContext(void) {
    42  	@autoreleasepool {
    43  		[NSOpenGLContext clearCurrentContext];
    44  	}
    45  }
    46  
    47  void gio_updateContext(CFTypeRef ctxRef) {
    48  	@autoreleasepool {
    49  		NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
    50  		[ctx update];
    51  	}
    52  }
    53  
    54  void gio_makeCurrentContext(CFTypeRef ctxRef) {
    55  	@autoreleasepool {
    56  		NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
    57  		[ctx makeCurrentContext];
    58  	}
    59  }
    60  
    61  void gio_lockContext(CFTypeRef ctxRef) {
    62  	@autoreleasepool {
    63  		NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
    64  		CGLLockContext([ctx CGLContextObj]);
    65  	}
    66  }
    67  
    68  void gio_unlockContext(CFTypeRef ctxRef) {
    69  	@autoreleasepool {
    70  		NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
    71  		CGLUnlockContext([ctx CGLContextObj]);
    72  	}
    73  }