| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package gl |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 54 |
work chan call |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 93 |
|
| 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 |
|
| 116 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 157 |
|
| 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 |
|
| 164 |
|
| 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 |
} |