2015-05-28 06:08:52 -04:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-12-09 12:52:53 -05:00
|
|
|
// +build darwin linux
|
2015-07-29 15:42:17 -04:00
|
|
|
|
2015-05-28 06:08:52 -04:00
|
|
|
package gl
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo darwin,amd64 LDFLAGS: -framework OpenGL
|
|
|
|
#cgo darwin,arm LDFLAGS: -framework OpenGLES
|
|
|
|
#cgo darwin,arm64 LDFLAGS: -framework OpenGLES
|
|
|
|
#cgo linux LDFLAGS: -lGLESv2
|
|
|
|
|
|
|
|
#cgo darwin,amd64 CFLAGS: -Dos_osx
|
|
|
|
#cgo darwin,arm CFLAGS: -Dos_ios
|
|
|
|
#cgo darwin,arm64 CFLAGS: -Dos_ios
|
|
|
|
#cgo linux CFLAGS: -Dos_linux
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "work.h"
|
|
|
|
|
2015-09-28 11:29:43 -04:00
|
|
|
uintptr_t process(struct fnargs* cargs, char* parg0, char* parg1, char* parg2, int count) {
|
|
|
|
uintptr_t ret;
|
|
|
|
|
|
|
|
ret = processFn(&cargs[0], parg0);
|
|
|
|
if (count > 1) {
|
|
|
|
ret = processFn(&cargs[1], parg1);
|
|
|
|
}
|
|
|
|
if (count > 2) {
|
|
|
|
ret = processFn(&cargs[2], parg2);
|
2015-05-28 06:08:52 -04:00
|
|
|
}
|
2015-09-28 11:29:43 -04:00
|
|
|
|
|
|
|
return ret;
|
2015-05-28 06:08:52 -04:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
2015-09-28 11:29:43 -04:00
|
|
|
import "unsafe"
|
|
|
|
|
|
|
|
const workbufLen = 3
|
2015-08-08 10:18:15 -04:00
|
|
|
|
|
|
|
type context struct {
|
2015-09-30 08:29:39 -04:00
|
|
|
cptr uintptr
|
|
|
|
debug int32
|
2015-08-08 10:18:15 -04:00
|
|
|
|
|
|
|
workAvailable chan struct{}
|
|
|
|
|
|
|
|
// work is a queue of calls to execute.
|
|
|
|
work chan call
|
|
|
|
|
|
|
|
// retvalue is sent a return value when blocking calls complete.
|
|
|
|
// It is safe to use a global unbuffered channel here as calls
|
|
|
|
// cannot currently be made concurrently.
|
|
|
|
//
|
|
|
|
// TODO: the comment above about concurrent calls isn't actually true: package
|
|
|
|
// app calls package gl, but it has to do so in a separate goroutine, which
|
|
|
|
// means that its gl calls (which may be blocking) can race with other gl calls
|
|
|
|
// in the main program. We should make it safe to issue blocking gl calls
|
|
|
|
// concurrently, or get the gl calls out of package app, or both.
|
|
|
|
retvalue chan C.uintptr_t
|
|
|
|
|
|
|
|
cargs [workbufLen]C.struct_fnargs
|
2015-09-28 11:29:43 -04:00
|
|
|
parg [workbufLen]*C.char
|
2015-08-08 10:18:15 -04:00
|
|
|
}
|
2015-05-28 06:08:52 -04:00
|
|
|
|
2015-08-08 10:18:15 -04:00
|
|
|
func (ctx *context) WorkAvailable() <-chan struct{} { return ctx.workAvailable }
|
|
|
|
|
|
|
|
// NewContext creates a cgo OpenGL context.
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
|
|
|
//
|
2015-08-08 10:18:15 -04:00
|
|
|
// See the Worker interface for more details on how it is used.
|
|
|
|
func NewContext() (Context, Worker) {
|
|
|
|
glctx := &context{
|
|
|
|
workAvailable: make(chan struct{}, 1),
|
|
|
|
work: make(chan call, workbufLen),
|
|
|
|
retvalue: make(chan C.uintptr_t),
|
|
|
|
}
|
|
|
|
return glctx, glctx
|
|
|
|
}
|
2015-05-28 06:08:52 -04:00
|
|
|
|
2015-12-09 12:52:53 -05:00
|
|
|
func (ctx *context) enqueue(c call) uintptr {
|
2015-08-08 10:18:15 -04:00
|
|
|
ctx.work <- c
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
|
|
|
|
|
|
|
select {
|
2015-08-08 10:18:15 -04:00
|
|
|
case ctx.workAvailable <- struct{}{}:
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
|
|
|
default:
|
2015-05-28 06:08:52 -04:00
|
|
|
}
|
|
|
|
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
|
|
|
if c.blocking {
|
2015-12-09 12:52:53 -05:00
|
|
|
return uintptr(<-ctx.retvalue)
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
|
|
|
}
|
|
|
|
return 0
|
2015-05-28 06:08:52 -04:00
|
|
|
}
|
|
|
|
|
2015-08-08 10:18:15 -04:00
|
|
|
func (ctx *context) DoWork() {
|
2015-10-05 13:05:35 -04:00
|
|
|
queue := make([]call, 0, workbufLen)
|
2015-05-28 06:08:52 -04:00
|
|
|
for {
|
|
|
|
// Wait until at least one piece of work is ready.
|
|
|
|
// Accumulate work until a piece is marked as blocking.
|
|
|
|
select {
|
2015-08-08 10:18:15 -04:00
|
|
|
case w := <-ctx.work:
|
2015-05-28 06:08:52 -04:00
|
|
|
queue = append(queue, w)
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
|
|
|
default:
|
|
|
|
return
|
2015-05-28 06:08:52 -04:00
|
|
|
}
|
|
|
|
blocking := queue[len(queue)-1].blocking
|
|
|
|
enqueue:
|
|
|
|
for len(queue) < cap(queue) && !blocking {
|
|
|
|
select {
|
2015-08-08 10:18:15 -04:00
|
|
|
case w := <-ctx.work:
|
2015-05-28 06:08:52 -04:00
|
|
|
queue = append(queue, w)
|
|
|
|
blocking = queue[len(queue)-1].blocking
|
|
|
|
default:
|
|
|
|
break enqueue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the queued GL functions.
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
|
|
|
for i, q := range queue {
|
2015-12-09 12:52:53 -05:00
|
|
|
ctx.cargs[i] = *(*C.struct_fnargs)(unsafe.Pointer(&q.args))
|
2015-09-28 11:29:43 -04:00
|
|
|
ctx.parg[i] = (*C.char)(q.parg)
|
2015-05-28 06:08:52 -04:00
|
|
|
}
|
2015-09-28 11:29:43 -04:00
|
|
|
ret := C.process(&ctx.cargs[0], ctx.parg[0], ctx.parg[1], ctx.parg[2], C.int(len(queue)))
|
2015-05-28 06:08:52 -04:00
|
|
|
|
|
|
|
// Cleanup and signal.
|
|
|
|
queue = queue[:0]
|
|
|
|
if blocking {
|
2015-09-28 11:29:43 -04:00
|
|
|
ctx.retvalue <- ret
|
2015-05-28 06:08:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 12:52:53 -05:00
|
|
|
func init() {
|
|
|
|
if unsafe.Sizeof(C.GLint(0)) != unsafe.Sizeof(int32(0)) {
|
|
|
|
panic("GLint is not an int32")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cString creates C string off the Go heap.
|
|
|
|
// ret is a *char.
|
2015-12-14 07:03:56 -08:00
|
|
|
func (ctx *context) cString(str string) (uintptr, func()) {
|
2015-12-09 12:52:53 -05:00
|
|
|
ptr := unsafe.Pointer(C.CString(str))
|
|
|
|
return uintptr(ptr), func() { C.free(ptr) }
|
|
|
|
}
|
|
|
|
|
|
|
|
// cString creates a pointer to a C string off the Go heap.
|
|
|
|
// ret is a **char.
|
2015-12-14 07:03:56 -08:00
|
|
|
func (ctx *context) cStringPtr(str string) (uintptr, func()) {
|
2015-12-09 12:52:53 -05:00
|
|
|
s, free := cString(str)
|
|
|
|
ptr := C.malloc(C.size_t(unsafe.Sizeof((*int)(nil))))
|
|
|
|
*(*uintptr)(ptr) = s
|
|
|
|
return uintptr(ptr), func() {
|
|
|
|
free()
|
|
|
|
C.free(ptr)
|
2015-05-28 06:08:52 -04:00
|
|
|
}
|
|
|
|
}
|