github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/exp/gl/glutil/context_darwin_amd64.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 CGLError CGCreate(CGLContextObj* ctx) { 19 CGLError err; 20 CGLPixelFormatAttribute attributes[] = { 21 kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core, 22 kCGLPFAColorSize, (CGLPixelFormatAttribute)24, 23 kCGLPFAAlphaSize, (CGLPixelFormatAttribute)8, 24 kCGLPFADepthSize, (CGLPixelFormatAttribute)16, 25 kCGLPFAAccelerated, 26 kCGLPFADoubleBuffer, 27 (CGLPixelFormatAttribute) 0 28 }; 29 CGLPixelFormatObj pix; 30 GLint num; 31 32 if ((err = CGLChoosePixelFormat(attributes, &pix, &num)) != kCGLNoError) { 33 return err; 34 } 35 if ((err = CGLCreateContext(pix, 0, ctx)) != kCGLNoError) { 36 return err; 37 } 38 if ((err = CGLDestroyPixelFormat(pix)) != kCGLNoError) { 39 return err; 40 } 41 if ((err = CGLSetCurrentContext(*ctx)) != kCGLNoError) { 42 return err; 43 } 44 if ((err = CGLLockContext(*ctx)) != kCGLNoError) { 45 return err; 46 } 47 return kCGLNoError; 48 } 49 */ 50 import "C" 51 52 import ( 53 "fmt" 54 "runtime" 55 ) 56 57 // contextGL holds a copy of the OpenGL Context from thread-local storage. 58 // 59 // Do not move a contextGL between goroutines or OS threads. 60 type contextGL struct { 61 ctx C.CGLContextObj 62 } 63 64 // createContext creates an OpenGL context, binds it as the current context 65 // stored in thread-local storage, and locks the current goroutine to an os 66 // thread. 67 func createContext() (*contextGL, error) { 68 // The OpenGL active context is stored in TLS. 69 runtime.LockOSThread() 70 71 c := new(contextGL) 72 if cglErr := C.CGCreate(&c.ctx); cglErr != C.kCGLNoError { 73 return nil, fmt.Errorf("CGL: %v", C.GoString(C.CGLErrorString(cglErr))) 74 } 75 76 // Using attribute arrays in OpenGL 3.3 requires the use of a VBA. 77 // But VBAs don't exist in ES 2. So we bind a default one. 78 var id C.GLuint 79 C.glGenVertexArrays(1, &id) 80 C.glBindVertexArray(id) 81 82 return c, nil 83 } 84 85 // destroy destroys an OpenGL context and unlocks the current goroutine from 86 // its os thread. 87 func (c *contextGL) destroy() { 88 C.CGLUnlockContext(c.ctx) 89 C.CGLSetCurrentContext(nil) 90 C.CGLDestroyContext(c.ctx) 91 c.ctx = nil 92 runtime.UnlockOSThread() 93 }