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
}
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 {
panic("opengl: worker must be initialized but not")
}
@ -84,7 +87,7 @@ loop:
select {
case <-workAvailable:
c.worker.DoWork()
case <-chDone:
case <-done:
break loop
}
}

View File

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

View File

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