headless_darwin.go raw
1 // SPDX-License-Identifier: Unlicense OR MIT
2
3 package headless
4
5 import (
6 "github.com/p9c/p9/pkg/gel/gio/gpu"
7 _ "github.com/p9c/p9/pkg/gel/gio/internal/cocoainit"
8 )
9
10 /*
11 #cgo CFLAGS: -DGL_SILENCE_DEPRECATION -Werror -Wno-deprecated-declarations -fmodules -fobjc-arc -x objective-c
12
13 #include <CoreFoundation/CoreFoundation.h>
14
15 __attribute__ ((visibility ("hidden"))) CFTypeRef gio_headless_newContext(void);
16 __attribute__ ((visibility ("hidden"))) void gio_headless_releaseContext(CFTypeRef ctxRef);
17 __attribute__ ((visibility ("hidden"))) void gio_headless_clearCurrentContext(CFTypeRef ctxRef);
18 __attribute__ ((visibility ("hidden"))) void gio_headless_makeCurrentContext(CFTypeRef ctxRef);
19 __attribute__ ((visibility ("hidden"))) void gio_headless_prepareContext(CFTypeRef ctxRef);
20 */
21 import "C"
22
23 type nsContext struct {
24 ctx C.CFTypeRef
25 prepared bool
26 }
27
28 func newGLContext() (context, error) {
29 ctx := C.gio_headless_newContext()
30 return &nsContext{ctx: ctx}, nil
31 }
32
33 func (c *nsContext) API() gpu.API {
34 return gpu.OpenGL{}
35 }
36
37 func (c *nsContext) MakeCurrent() error {
38 C.gio_headless_makeCurrentContext(c.ctx)
39 if !c.prepared {
40 C.gio_headless_prepareContext(c.ctx)
41 c.prepared = true
42 }
43 return nil
44 }
45
46 func (c *nsContext) ReleaseCurrent() {
47 C.gio_headless_clearCurrentContext(c.ctx)
48 }
49
50 func (d *nsContext) Release() {
51 if d.ctx != 0 {
52 C.gio_headless_releaseContext(d.ctx)
53 d.ctx = 0
54 }
55 }
56