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-08-08 10:18:15 -04:00
|
|
|
// +build darwin linux cgo
|
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-08-08 10:18:15 -04:00
|
|
|
// TODO: return uintptr_t instead of taking ret.
|
|
|
|
void process(struct fnargs* cargs, int count, uintptr_t* ret) {
|
2015-05-28 06:08:52 -04:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < count; i++) {
|
2015-08-08 10:18:15 -04:00
|
|
|
*ret = processFn(&cargs[i]);
|
2015-05-28 06:08:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
2015-08-08 10:18:15 -04:00
|
|
|
const workbufLen = 10
|
|
|
|
|
|
|
|
type context struct {
|
|
|
|
cptr uintptr
|
|
|
|
|
|
|
|
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
|
|
|
|
ret C.uintptr_t
|
|
|
|
}
|
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
|
|
|
|
|
|
|
type call struct {
|
|
|
|
args C.struct_fnargs
|
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
|
|
|
blocking bool
|
2015-05-28 06:08:52 -04:00
|
|
|
}
|
|
|
|
|
2015-08-08 10:18:15 -04:00
|
|
|
func (ctx *context) enqueue(c call) C.uintptr_t {
|
|
|
|
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-08-08 10:18:15 -04:00
|
|
|
return <-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() {
|
|
|
|
queue := make([]call, 0, len(ctx.work))
|
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-08-08 10:18:15 -04:00
|
|
|
ctx.cargs[i] = q.args
|
2015-05-28 06:08:52 -04:00
|
|
|
}
|
2015-08-08 10:18:15 -04:00
|
|
|
C.process(&ctx.cargs[0], C.int(len(queue)), &ctx.ret)
|
2015-05-28 06:08:52 -04:00
|
|
|
|
|
|
|
// Cleanup and signal.
|
|
|
|
queue = queue[:0]
|
|
|
|
if blocking {
|
2015-08-08 10:18:15 -04:00
|
|
|
ctx.retvalue <- ctx.ret
|
2015-05-28 06:08:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func glBoolean(b bool) C.uintptr_t {
|
|
|
|
if b {
|
|
|
|
return TRUE
|
|
|
|
}
|
|
|
|
return FALSE
|
|
|
|
}
|