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

View File

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

View File

@ -17,6 +17,7 @@
package mobile package mobile
import ( import (
"context"
"errors" "errors"
"image" "image"
"runtime" "runtime"
@ -71,7 +72,13 @@ func (u *UserInterface) Render(chError <-chan error) error {
} }
renderCh <- struct{}{} 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 { type UserInterface struct {