graphicsdriver/opengl: Use context.Context when possible

This commit is contained in:
Hajime Hoshi 2019-06-08 01:11:55 +09:00
parent 9c283d45b4
commit 1e93d9c699
3 changed files with 15 additions and 6 deletions

View File

@ -17,6 +17,7 @@
package opengl
import (
stdcontext "context"
"errors"
"fmt"
@ -76,7 +77,7 @@ type contextImpl struct {
// doWork consumes the queued GL tasks.
//
// doWork is called only on gomobile-bind.
func (c *context) doWork(done <-chan struct{}) error {
func (c *context) doWork(context stdcontext.Context) {
if c.worker == nil {
panic("opengl: worker must be initialized but not")
}
@ -87,11 +88,10 @@ loop:
select {
case <-workAvailable:
c.worker.DoWork()
case <-done:
case <-context.Done():
break loop
}
}
return nil
}
func (c *context) reset() error {

View File

@ -17,11 +17,13 @@
package opengl
import (
stdcontext "context"
"golang.org/x/mobile/gl"
)
func (d *Driver) DoWork(done <-chan struct{}) error {
return d.context.doWork(done)
func (d *Driver) DoWork(context stdcontext.Context) {
d.context.doWork(context)
}
func (d *Driver) Init() {

View File

@ -17,6 +17,7 @@
package mobile
import (
"context"
"errors"
"image"
"runtime"
@ -71,7 +72,13 @@ func (u *UserInterface) Render(chError <-chan error) error {
}
renderCh <- struct{}{}
return opengl.Get().DoWork(renderEndCh)
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-renderEndCh
cancel()
}()
opengl.Get().DoWork(ctx)
return nil
}
type UserInterface struct {