uidriver/mobile: Refactoring

This commit is contained in:
Hajime Hoshi 2019-05-31 01:44:01 +09:00
parent ffb9871839
commit ca907e2846
3 changed files with 18 additions and 10 deletions

View File

@ -73,7 +73,10 @@ type contextImpl struct {
worker mgl.Worker worker mgl.Worker
} }
func (c *context) doWork(chDone <-chan struct{}) error { // doWork consumes the queued GL tasks.
//
// doWork is called only on gomobile-bind.
func (c *context) doWork(done <-chan struct{}) error {
if c.worker == nil { if c.worker == nil {
panic("opengl: worker must be initialized but not") panic("opengl: worker must be initialized but not")
} }
@ -84,7 +87,7 @@ loop:
select { select {
case <-workAvailable: case <-workAvailable:
c.worker.DoWork() c.worker.DoWork()
case <-chDone: case <-done:
break loop break loop
} }
} }

View File

@ -20,8 +20,8 @@ import (
"golang.org/x/mobile/gl" "golang.org/x/mobile/gl"
) )
func (d *Driver) DoWork(chDone <-chan struct{}) error { func (d *Driver) DoWork(done <-chan struct{}) error {
return d.context.doWork(chDone) return d.context.doWork(done)
} }
func (d *Driver) Init() { func (d *Driver) Init() {

View File

@ -37,9 +37,14 @@ import (
var ( var (
glContextCh = make(chan gl.Context) glContextCh = make(chan gl.Context)
renderCh = make(chan struct{})
renderChEnd = make(chan struct{}) // renderCh recieves when updating starts.
theUI = &UserInterface{} renderCh = make(chan struct{})
// renderEndCh receives when updating finishes.
renderEndCh = make(chan struct{})
theUI = &UserInterface{}
) )
func init() { func init() {
@ -62,7 +67,7 @@ func (u *UserInterface) Render(chError <-chan error) error {
case err := <-chError: case err := <-chError:
return err return err
case renderCh <- struct{}{}: case renderCh <- struct{}{}:
return opengl.Get().DoWork(renderChEnd) return opengl.Get().DoWork(renderEndCh)
case <-time.After(500 * time.Millisecond): case <-time.After(500 * time.Millisecond):
// This function must not be blocked. We need to break for timeout. // This function must not be blocked. We need to break for timeout.
return nil return nil
@ -127,7 +132,7 @@ func (u *UserInterface) appMain(a app.App) {
continue continue
} }
renderCh <- struct{}{} renderCh <- struct{}{}
<-renderChEnd <-renderEndCh
a.Publish() a.Publish()
a.Send(paint.Event{}) a.Send(paint.Event{})
case touch.Event: case touch.Event:
@ -253,7 +258,7 @@ render:
context.ResumeAudio() context.ResumeAudio()
defer func() { defer func() {
renderChEnd <- struct{}{} renderEndCh <- struct{}{}
}() }()
if err := context.Update(func() { if err := context.Update(func() {