github.com/cybriq/giocore@v0.0.7-0.20210703034601-cfb9cb5f3900/gpu/headless/headless_darwin.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package headless
     4  
     5  import (
     6  	"github.com/cybriq/giocore/gpu"
     7  	_ "github.com/cybriq/giocore/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  */
    20  import "C"
    21  
    22  type nsContext struct {
    23  	ctx C.CFTypeRef
    24  }
    25  
    26  func newGLContext() (context, error) {
    27  	ctx := C.gio_headless_newContext()
    28  	return &nsContext{ctx: ctx}, nil
    29  }
    30  
    31  func (c *nsContext) API() gpu.API {
    32  	return gpu.OpenGL{}
    33  }
    34  
    35  func (c *nsContext) MakeCurrent() error {
    36  	C.gio_headless_makeCurrentContext(c.ctx)
    37  	return nil
    38  }
    39  
    40  func (c *nsContext) ReleaseCurrent() {
    41  	C.gio_headless_clearCurrentContext(c.ctx)
    42  }
    43  
    44  func (d *nsContext) Release() {
    45  	if d.ctx != 0 {
    46  		C.gio_headless_releaseContext(d.ctx)
    47  		d.ctx = 0
    48  	}
    49  }