headless_macos.m raw
1 // SPDX-License-Identifier: Unlicense OR MIT
2
3 // +build darwin,!ios
4
5 @import AppKit;
6 @import OpenGL;
7 @import OpenGL.GL;
8 @import OpenGL.GL3;
9
10 #include <CoreFoundation/CoreFoundation.h>
11 #include "_cgo_export.h"
12
13 void gio_headless_releaseContext(CFTypeRef ctxRef) {
14 CFBridgingRelease(ctxRef);
15 }
16
17 CFTypeRef gio_headless_newContext(void) {
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 if (pixFormat == nil) {
29 return NULL;
30 }
31 NSOpenGLContext *ctx = [[NSOpenGLContext alloc] initWithFormat:pixFormat shareContext:nil];
32 return CFBridgingRetain(ctx);
33 }
34
35 void gio_headless_clearCurrentContext(CFTypeRef ctxRef) {
36 NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
37 CGLUnlockContext([ctx CGLContextObj]);
38 [NSOpenGLContext clearCurrentContext];
39 }
40
41 void gio_headless_makeCurrentContext(CFTypeRef ctxRef) {
42 NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
43 [ctx makeCurrentContext];
44 CGLLockContext([ctx CGLContextObj]);
45 }
46
47 void gio_headless_prepareContext(CFTypeRef ctxRef) {
48 // Bind a default VBA to emulate OpenGL ES 2.
49 GLuint defVBA;
50 glGenVertexArrays(1, &defVBA);
51 glBindVertexArray(defVBA);
52 glEnable(GL_FRAMEBUFFER_SRGB);
53 }
54