Debian Codegraph @

gl/work.go

raw symbols SCIP
1 // Copyright 2015 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 //go:build darwin || linux || openbsd
6
7 package gl
8
9 /*
10 #cgo ios LDFLAGS: -framework OpenGLES
11 #cgo darwin,!ios LDFLAGS: -framework OpenGL
12 #cgo linux LDFLAGS: -lGLESv2
13 #cgo openbsd LDFLAGS: -L/usr/X11R6/lib/ -lGLESv2
14
15 #cgo android CFLAGS: -Dos_android
16 #cgo ios CFLAGS: -Dos_ios
17 #cgo darwin,!ios CFLAGS: -Dos_macos
18 #cgo darwin CFLAGS: -DGL_SILENCE_DEPRECATION -DGLES_SILENCE_DEPRECATION
19 #cgo linux CFLAGS: -Dos_linux
20 #cgo openbsd CFLAGS: -Dos_openbsd
21
22 #cgo openbsd CFLAGS: -I/usr/X11R6/include/
23
24 #include <stdint.h>
25 #include "work.h"
26
27 uintptr_t process(struct fnargs* cargs, char* parg0, char* parg1, char* parg2, int count) {
28 uintptr_t ret;
29
30 ret = processFn(&cargs[0], parg0);
31 if (count > 1) {
32 ret = processFn(&cargs[1], parg1);
33 }
34 if (count > 2) {
35 ret = processFn(&cargs[2], parg2);
36 }
37
38 return ret;
39 }
40 */
41 import "C"
42
43 import "unsafe"
44
45 const workbufLen = 3
46
47 type context struct {
48 cptr uintptr
49 debug int32
50
51 workAvailable chan struct{}
52
53 // work is a queue of calls to execute.
54 work chan call
55
56 // retvalue is sent a return value when blocking calls complete.
57 // It is safe to use a global unbuffered channel here as calls
58 // cannot currently be made concurrently.
59 //
60 // TODO: the comment above about concurrent calls isn't actually true: package
61 // app calls package gl, but it has to do so in a separate goroutine, which
62 // means that its gl calls (which may be blocking) can race with other gl calls
63 // in the main program. We should make it safe to issue blocking gl calls
64 // concurrently, or get the gl calls out of package app, or both.
65 retvalue chan C.uintptr_t
66
67 cargs [workbufLen]C.struct_fnargs
68 parg [workbufLen]*C.char
69 }
70
71 func (ctx *context) WorkAvailable() <-chan struct{} { return ctx.workAvailable }
72
73 type context3 struct {
74 *context
75 }
76
77 // NewContext creates a cgo OpenGL context.
78 //
79 // See the Worker interface for more details on how it is used.
80 func NewContext() (Context, Worker) {
81 glctx := &context{
82 workAvailable: make(chan struct{}, 1),
83 work: make(chan call, workbufLen),
84 retvalue: make(chan C.uintptr_t),
85 }
86 if C.GLES_VERSION == "GL_ES_2_0" {
87 return glctx, glctx
88 }
89 return context3{glctx}, glctx
90 }
91
92 // Version returns a GL ES version string, either "GL_ES_2_0" or "GL_ES_3_0".
93 // Future versions of the gl package may return "GL_ES_3_1".
94 func Version() string {
95 return C.GLES_VERSION
96 }
97
98 func (ctx *context) enqueue(c call) uintptr {
99 ctx.work <- c
100
101 select {
102 case ctx.workAvailable <- struct{}{}:
103 default:
104 }
105
106 if c.blocking {
107 return uintptr(<-ctx.retvalue)
108 }
109 return 0
110 }
111
112 func (ctx *context) DoWork() {
113 queue := make([]call, 0, workbufLen)
114 for {
115 // Wait until at least one piece of work is ready.
116 // Accumulate work until a piece is marked as blocking.
117 select {
118 case w := <-ctx.work:
119 queue = append(queue, w)
120 default:
121 return
122 }
123 blocking := queue[len(queue)-1].blocking
124 enqueue:
125 for len(queue) < cap(queue) && !blocking {
126 select {
127 case w := <-ctx.work:
128 queue = append(queue, w)
129 blocking = queue[len(queue)-1].blocking
130 default:
131 break enqueue
132 }
133 }
134
135 // Process the queued GL functions.
136 for i, q := range queue {
137 ctx.cargs[i] = *(*C.struct_fnargs)(unsafe.Pointer(&q.args))
138 ctx.parg[i] = (*C.char)(q.parg)
139 }
140 ret := C.process(&ctx.cargs[0], ctx.parg[0], ctx.parg[1], ctx.parg[2], C.int(len(queue)))
141
142 // Cleanup and signal.
143 queue = queue[:0]
144 if blocking {
145 ctx.retvalue <- ret
146 }
147 }
148 }
149
150 func init() {
151 if unsafe.Sizeof(C.GLint(0)) != unsafe.Sizeof(int32(0)) {
152 panic("GLint is not an int32")
153 }
154 }
155
156 // cString creates C string off the Go heap.
157 // ret is a *char.
158 func (ctx *context) cString(str string) (uintptr, func()) {
159 ptr := unsafe.Pointer(C.CString(str))
160 return uintptr(ptr), func() { C.free(ptr) }
161 }
162
163 // cStringPtr creates a pointer to a C string off the Go heap.
164 // ret is a **char.
165 func (ctx *context) cStringPtr(str string) (uintptr, func()) {
166 s, free := ctx.cString(str)
167 ptr := C.malloc(C.size_t(unsafe.Sizeof((*int)(nil))))
168 *(*uintptr)(ptr) = s
169 return uintptr(ptr), func() {
170 free()
171 C.free(ptr)
172 }
173 }