github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/gio/app/internal/window/gl_macos.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  // +build darwin,!ios
     4  
     5  package window
     6  
     7  import (
     8  	"github.com/gop9/olt/gio/app/internal/gl"
     9  )
    10  
    11  /*
    12  #include <CoreFoundation/CoreFoundation.h>
    13  #include <CoreGraphics/CoreGraphics.h>
    14  #include <AppKit/AppKit.h>
    15  #include <OpenGL/gl3.h>
    16  #include "gl_macos.h"
    17  */
    18  import "C"
    19  
    20  type context struct {
    21  	c    *gl.Functions
    22  	ctx  C.CFTypeRef
    23  	view C.CFTypeRef
    24  }
    25  
    26  func init() {
    27  	viewFactory = func() C.CFTypeRef {
    28  		return C.gio_createGLView()
    29  	}
    30  }
    31  
    32  func newContext(w *window) (*context, error) {
    33  	view := w.contextView()
    34  	ctx := C.gio_contextForView(view)
    35  	c := &context{
    36  		ctx:  ctx,
    37  		c:    new(gl.Functions),
    38  		view: view,
    39  	}
    40  	return c, nil
    41  }
    42  
    43  func (c *context) Functions() *gl.Functions {
    44  	return c.c
    45  }
    46  
    47  func (c *context) Release() {
    48  	c.Lock()
    49  	defer c.Unlock()
    50  	C.gio_clearCurrentContext()
    51  }
    52  
    53  func (c *context) Present() error {
    54  	// Assume the caller already locked the context.
    55  	C.glFlush()
    56  	return nil
    57  }
    58  
    59  func (c *context) Lock() {
    60  	C.gio_lockContext(c.ctx)
    61  }
    62  
    63  func (c *context) Unlock() {
    64  	C.gio_unlockContext(c.ctx)
    65  }
    66  
    67  func (c *context) MakeCurrent() error {
    68  	c.Lock()
    69  	defer c.Unlock()
    70  	C.gio_makeCurrentContext(c.ctx)
    71  	return nil
    72  }
    73  
    74  func (w *window) NewContext() (Context, error) {
    75  	return newContext(w)
    76  }