ui/mobile: Move 'DoWork' logic to ui/mobile package

This simplifies driver.Graphics interface, and will make it easy
to use another graphics driver than OpenGL.
This commit is contained in:
Hajime Hoshi 2019-06-27 00:07:39 +09:00
parent e499535728
commit 480c5527a3
3 changed files with 34 additions and 44 deletions

View File

@ -17,7 +17,6 @@
package opengl
import (
stdcontext "context"
"errors"
"fmt"
@ -70,28 +69,7 @@ const (
)
type contextImpl struct {
gl mgl.Context
worker mgl.Worker
}
// doWork consumes the queued GL tasks.
//
// doWork is called only on gomobile-bind.
func (c *context) doWork(context stdcontext.Context) {
if c.worker == nil {
panic("opengl: worker must be initialized but not")
}
// TODO: Check this is called on the rendering thread
workAvailable := c.worker.WorkAvailable()
loop:
for {
select {
case <-workAvailable:
c.worker.DoWork()
case <-context.Done():
break loop
}
}
gl mgl.Context
}
func (c *context) reset() error {

View File

@ -17,19 +17,9 @@
package opengl
import (
stdcontext "context"
"golang.org/x/mobile/gl"
)
func (d *Driver) DoWork(context stdcontext.Context) {
d.context.doWork(context)
}
func (d *Driver) Init() {
d.context.gl, d.context.worker = gl.NewContext()
}
func (d *Driver) InitWithContext(context gl.Context) {
func (d *Driver) SetMobileGLContext(context gl.Context) {
d.context.gl = context
}

View File

@ -61,7 +61,25 @@ func (u *UserInterface) Render() {
<-renderEndCh
cancel()
}()
opengl.Get().DoWork(ctx)
if u.graphics.IsGL() {
if u.glWorker == nil {
panic("mobile: glWorker must be initialized but not")
}
workAvailable := u.glWorker.WorkAvailable()
loop:
for {
select {
case <-workAvailable:
u.glWorker.DoWork()
case <-ctx.Done():
break loop
}
}
return
}
// TODO: Create and run the thread loop like the GLFW driver does.
}
type UserInterface struct {
@ -75,8 +93,12 @@ type UserInterface struct {
fullscreenWidthPx int
fullscreenHeightPx int
graphics driver.Graphics
input Input
glWorker gl.Worker
m sync.RWMutex
}
@ -171,23 +193,23 @@ func (u *UserInterface) RunWithoutMainLoop(width, height int, scale float64, tit
}
func (u *UserInterface) run(width, height int, scale float64, title string, context driver.UIContext, graphics driver.Graphics, mainloop bool) error {
if graphics != opengl.Get() {
panic("ui: graphics driver must be OpenGL")
}
u.m.Lock()
u.width = width
u.height = height
u.scale = scale
u.sizeChanged = true
u.graphics = graphics
u.m.Unlock()
// title is ignored?
if mainloop {
ctx := <-glContextCh
opengl.Get().InitWithContext(ctx)
} else {
opengl.Get().Init()
if graphics.IsGL() {
var ctx gl.Context
if mainloop {
ctx = <-glContextCh
} else {
ctx, u.glWorker = gl.NewContext()
}
graphics.(*opengl.Driver).SetMobileGLContext(ctx)
}
// Force to set the screen size