2
0
mirror of synced 2025-02-22 14:28:14 +00:00
mobile/gl/work.go
Nigel Tao 42f0d17876 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-26 07:43:17 +00:00

122 lines
2.7 KiB
Go

// 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.
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"
struct fnargs cargs[10];
uintptr_t ret;
void process(int count) {
int i;
for (i = 0; i < count; i++) {
processFn(&cargs[i]);
}
}
*/
import "C"
// work is a queue of calls to execute.
var work = make(chan call, 10)
// 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.
var retvalue = make(chan C.uintptr_t)
type call struct {
args C.struct_fnargs
blocking bool
}
func enqueue(c call) C.uintptr_t {
work <- c
select {
case workAvailable <- struct{}{}:
default:
}
if c.blocking {
return <-retvalue
}
return 0
}
var (
workAvailable = make(chan struct{}, 1)
// WorkAvailable communicates when DoWork should be called.
//
// This is an internal implementation detail and should only be used by the
// golang.org/x/mobile/app package.
WorkAvailable <-chan struct{} = workAvailable
)
// DoWork performs any pending OpenGL calls.
//
// This is an internal implementation detail and should only be used by the
// golang.org/x/mobile/app package.
func DoWork() {
queue := make([]call, 0, len(work))
for {
// Wait until at least one piece of work is ready.
// Accumulate work until a piece is marked as blocking.
select {
case w := <-work:
queue = append(queue, w)
default:
return
}
blocking := queue[len(queue)-1].blocking
enqueue:
for len(queue) < cap(queue) && !blocking {
select {
case w := <-work:
queue = append(queue, w)
blocking = queue[len(queue)-1].blocking
default:
break enqueue
}
}
// Process the queued GL functions.
for i, q := range queue {
C.cargs[i] = q.args
}
C.process(C.int(len(queue)))
// Cleanup and signal.
queue = queue[:0]
if blocking {
retvalue <- C.ret
}
}
}
func glBoolean(b bool) C.uintptr_t {
if b {
return TRUE
}
return FALSE
}