github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/mobile/gl/glutil/context_darwin.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build darwin 6 7 package glutil 8 9 // TODO(crawshaw): Only used in glutil tests for now (cgo is not support in _test.go files). 10 // TODO(crawshaw): Export some kind of Context. Work out what we can offer, where. Maybe just for tests. 11 // TODO(crawshaw): Support android and windows. 12 13 /* 14 #cgo LDFLAGS: -framework OpenGL 15 #import <OpenGL/OpenGL.h> 16 #import <OpenGL/gl3.h> 17 18 void CGCreate(CGLContextObj* ctx) { 19 CGLPixelFormatAttribute attributes[] = { 20 kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core, 21 kCGLPFAColorSize, (CGLPixelFormatAttribute)24, 22 kCGLPFAAlphaSize, (CGLPixelFormatAttribute)8, 23 kCGLPFADepthSize, (CGLPixelFormatAttribute)16, 24 kCGLPFAAccelerated, 25 kCGLPFADoubleBuffer, 26 (CGLPixelFormatAttribute) 0 27 }; 28 CGLPixelFormatObj pix; 29 GLint num; 30 CGLChoosePixelFormat(attributes, &pix, &num); 31 CGLCreateContext(pix, 0, ctx); 32 CGLDestroyPixelFormat(pix); 33 CGLSetCurrentContext(*ctx); 34 CGLLockContext(*ctx); 35 } 36 */ 37 import "C" 38 39 import "runtime" 40 41 // contextGL holds a copy of the OpenGL Context from thread-local storage. 42 // 43 // Do not move a contextGL between goroutines or OS threads. 44 type contextGL struct { 45 ctx C.CGLContextObj 46 } 47 48 // createContext creates an OpenGL context, binds it as the current context 49 // stored in thread-local storage, and locks the current goroutine to an os 50 // thread. 51 func createContext() *contextGL { 52 // The OpenGL active context is stored in TLS. 53 runtime.LockOSThread() 54 55 c := new(contextGL) 56 C.CGCreate(&c.ctx) 57 58 // Using attribute arrays in OpenGL 3.3 requires the use of a VBA. 59 // But VBAs don't exist in ES 2. So we bind a default one. 60 var id C.GLuint 61 C.glGenVertexArrays(1, &id) 62 C.glBindVertexArray(id) 63 64 return c 65 } 66 67 // destroy destroys an OpenGL context and unlocks the current goroutine from 68 // its os thread. 69 func (c *contextGL) destroy() { 70 C.CGLUnlockContext(c.ctx) 71 C.CGLSetCurrentContext(nil) 72 C.CGLDestroyContext(c.ctx) 73 c.ctx = nil 74 runtime.UnlockOSThread() 75 }