From 1e93d9c69971134e6f720b56dc24ddd69ed2702d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 8 Jun 2019 01:11:55 +0900 Subject: [PATCH] graphicsdriver/opengl: Use context.Context when possible --- internal/graphicsdriver/opengl/context_mobile.go | 6 +++--- internal/graphicsdriver/opengl/driver_mobile.go | 6 ++++-- internal/uidriver/mobile/ui.go | 9 ++++++++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/internal/graphicsdriver/opengl/context_mobile.go b/internal/graphicsdriver/opengl/context_mobile.go index cb3519776..6e5c457d3 100644 --- a/internal/graphicsdriver/opengl/context_mobile.go +++ b/internal/graphicsdriver/opengl/context_mobile.go @@ -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 { diff --git a/internal/graphicsdriver/opengl/driver_mobile.go b/internal/graphicsdriver/opengl/driver_mobile.go index ff6c03dac..19c337609 100644 --- a/internal/graphicsdriver/opengl/driver_mobile.go +++ b/internal/graphicsdriver/opengl/driver_mobile.go @@ -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() { diff --git a/internal/uidriver/mobile/ui.go b/internal/uidriver/mobile/ui.go index 89286e501..ea59d25eb 100644 --- a/internal/uidriver/mobile/ui.go +++ b/internal/uidriver/mobile/ui.go @@ -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 {