aboutsummaryrefslogtreecommitdiff
path: root/vendor/gioui.org/app/gl_macos.m
blob: fe8374312c85d08a4d522008f464f772c986b89f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: Unlicense OR MIT

// +build darwin,!ios,nometal

@import AppKit;

#include <CoreFoundation/CoreFoundation.h>
#include <OpenGL/OpenGL.h>
#include "_cgo_export.h"

CALayer *gio_layerFactory(void) {
	@autoreleasepool {
		return [CALayer layer];
	}
}

CFTypeRef gio_createGLContext(void) {
	@autoreleasepool {
		NSOpenGLPixelFormatAttribute attr[] = {
			NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
			NSOpenGLPFAColorSize,     24,
			NSOpenGLPFAAccelerated,
			// Opt-in to automatic GPU switching. CGL-only property.
			kCGLPFASupportsAutomaticGraphicsSwitching,
			NSOpenGLPFAAllowOfflineRenderers,
			0
		};
		NSOpenGLPixelFormat *pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];

		NSOpenGLContext *ctx = [[NSOpenGLContext alloc] initWithFormat:pixFormat shareContext: nil];
		return CFBridgingRetain(ctx);
	}
}

void gio_setContextView(CFTypeRef ctxRef, CFTypeRef viewRef) {
	NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
	NSView *view = (__bridge NSView *)viewRef;
	[view setWantsBestResolutionOpenGLSurface:YES];
	[ctx setView:view];
}

void gio_clearCurrentContext(void) {
	@autoreleasepool {
		[NSOpenGLContext clearCurrentContext];
	}
}

void gio_updateContext(CFTypeRef ctxRef) {
	@autoreleasepool {
		NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
		[ctx update];
	}
}

void gio_makeCurrentContext(CFTypeRef ctxRef) {
	@autoreleasepool {
		NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
		[ctx makeCurrentContext];
	}
}

void gio_lockContext(CFTypeRef ctxRef) {
	@autoreleasepool {
		NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
		CGLLockContext([ctx CGLContextObj]);
	}
}

void gio_unlockContext(CFTypeRef ctxRef) {
	@autoreleasepool {
		NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
		CGLUnlockContext([ctx CGLContextObj]);
	}
}