driver: Rename GraphicsContext -> UIContext

This commit is contained in:
Hajime Hoshi 2019-04-08 23:21:37 -04:00
parent aecfd6b13d
commit f6367308fc
6 changed files with 49 additions and 49 deletions

View File

@ -19,7 +19,7 @@ import (
"image" "image"
) )
type GraphicsContext interface { type UIContext interface {
SetSize(width, height int, scale float64) SetSize(width, height int, scale float64)
Update(afterFrameUpdate func()) error Update(afterFrameUpdate func()) error
SuspendAudio() SuspendAudio()
@ -40,7 +40,7 @@ type UI interface {
IsWindowDecorated() bool IsWindowDecorated() bool
IsWindowResizable() bool IsWindowResizable() bool
Loop(ch <-chan error) error Loop(ch <-chan error) error
Run(width, height int, scale float64, title string, g GraphicsContext, mainloop bool, graphics Graphics) error Run(width, height int, scale float64, title string, context UIContext, mainloop bool, graphics Graphics) error
ScreenPadding() (x0, y0, x1, y1 float64) ScreenPadding() (x0, y0, x1, y1 float64)
ScreenScale() float64 ScreenScale() float64
ScreenSizeInFullscreen() (int, int) ScreenSizeInFullscreen() (int, int)

View File

@ -555,7 +555,7 @@ func (u *UserInterface) DeviceScaleFactor() float64 {
return f return f
} }
func (u *UserInterface) Run(width, height int, scale float64, title string, g driver.GraphicsContext, mainloop bool, graphics driver.Graphics) error { func (u *UserInterface) Run(width, height int, scale float64, title string, context driver.UIContext, mainloop bool, graphics driver.Graphics) error {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
u.graphics = graphics u.graphics = graphics
@ -662,7 +662,7 @@ func (u *UserInterface) Run(width, height int, scale float64, title string, g dr
return nil return nil
}) })
graphics.SetWindow(w) graphics.SetWindow(w)
return u.loop(g) return u.loop(context)
} }
// getSize must be called from the main thread. // getSize must be called from the main thread.
@ -699,7 +699,7 @@ func (u *UserInterface) actualScreenScale() float64 {
return u.getScale() * devicescale.GetAt(u.currentMonitor().GetPos()) return u.getScale() * devicescale.GetAt(u.currentMonitor().GetPos())
} }
func (u *UserInterface) updateGraphicsContext(g driver.GraphicsContext) { func (u *UserInterface) updateGraphics(context driver.UIContext) {
actualScale := 0.0 actualScale := 0.0
sizeChanged := false sizeChanged := false
// TODO: Is it possible to reduce 'runOnMainThread' calls? // TODO: Is it possible to reduce 'runOnMainThread' calls?
@ -719,11 +719,11 @@ func (u *UserInterface) updateGraphicsContext(g driver.GraphicsContext) {
return nil return nil
}) })
if sizeChanged { if sizeChanged {
g.SetSize(u.width, u.height, actualScale) context.SetSize(u.width, u.height, actualScale)
} }
} }
func (u *UserInterface) update(g driver.GraphicsContext) error { func (u *UserInterface) update(context driver.UIContext) error {
shouldClose := false shouldClose := false
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
shouldClose = u.window.ShouldClose() shouldClose = u.window.ShouldClose()
@ -742,17 +742,17 @@ func (u *UserInterface) update(g driver.GraphicsContext) error {
}) })
// This call is needed for initialization. // This call is needed for initialization.
u.updateGraphicsContext(g) u.updateGraphics(context)
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
glfw.PollEvents() glfw.PollEvents()
u.input.update(u.window, u.getScale()*u.glfwScale()) u.input.update(u.window, u.getScale()*u.glfwScale())
defer g.ResumeAudio() defer context.ResumeAudio()
for !u.isRunnableInBackground() && u.window.GetAttrib(glfw.Focused) == 0 { for !u.isRunnableInBackground() && u.window.GetAttrib(glfw.Focused) == 0 {
g.SuspendAudio() context.SuspendAudio()
// Wait for an arbitrary period to avoid busy loop. // Wait for an arbitrary period to avoid busy loop.
time.Sleep(time.Second / 60) time.Sleep(time.Second / 60)
glfw.PollEvents() glfw.PollEvents()
@ -762,9 +762,9 @@ func (u *UserInterface) update(g driver.GraphicsContext) error {
} }
return nil return nil
}) })
if err := g.Update(func() { if err := context.Update(func() {
// The offscreens must be updated every frame (#490). // The offscreens must be updated every frame (#490).
u.updateGraphicsContext(g) u.updateGraphics(context)
}); err != nil { }); err != nil {
return err return err
} }
@ -782,7 +782,7 @@ func (u *UserInterface) update(g driver.GraphicsContext) error {
return nil return nil
} }
func (u *UserInterface) loop(g driver.GraphicsContext) error { func (u *UserInterface) loop(context driver.UIContext) error {
defer func() { defer func() {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
glfw.Terminate() glfw.Terminate()
@ -790,7 +790,7 @@ func (u *UserInterface) loop(g driver.GraphicsContext) error {
}) })
}() }()
for { for {
if err := u.update(g); err != nil { if err := u.update(context); err != nil {
return err return err
} }

View File

@ -45,7 +45,7 @@ type UserInterface struct {
lastActualScale float64 lastActualScale float64
context driver.GraphicsContext context driver.UIContext
input Input input Input
} }
@ -188,7 +188,7 @@ func (u *UserInterface) actualScreenScale() float64 {
return u.getScale() * devicescale.GetAt(0, 0) return u.getScale() * devicescale.GetAt(0, 0)
} }
func (u *UserInterface) updateGraphicsContext() { func (u *UserInterface) updateGraphics() {
a := u.actualScreenScale() a := u.actualScreenScale()
if u.lastActualScale != a { if u.lastActualScale != a {
u.updateScreenSize() u.updateScreenSize()
@ -213,17 +213,17 @@ func (u *UserInterface) update() error {
u.context.ResumeAudio() u.context.ResumeAudio()
u.input.UpdateGamepads() u.input.UpdateGamepads()
u.updateGraphicsContext() u.updateGraphics()
if err := u.context.Update(func() { if err := u.context.Update(func() {
u.updateGraphicsContext() u.updateGraphics()
}); err != nil { }); err != nil {
return err return err
} }
return nil return nil
} }
func (u *UserInterface) loop(g driver.GraphicsContext) <-chan error { func (u *UserInterface) loop(context driver.UIContext) <-chan error {
u.context = g u.context = context
ch := make(chan error) ch := make(chan error)
var cf js.Callback var cf js.Callback
@ -388,11 +388,11 @@ func (u *UserInterface) Loop(ch <-chan error) error {
return <-ch return <-ch
} }
func (u *UserInterface) Run(width, height int, scale float64, title string, g driver.GraphicsContext, mainloop bool, graphics driver.Graphics) error { func (u *UserInterface) Run(width, height int, scale float64, title string, context driver.UIContext, mainloop bool, graphics driver.Graphics) error {
document.Set("title", title) document.Set("title", title)
u.setScreenSize(width, height, scale, u.fullscreen) u.setScreenSize(width, height, scale, u.fullscreen)
canvas.Call("focus") canvas.Call("focus")
ch := u.loop(g) ch := u.loop(context)
if runtime.GOARCH == "wasm" { if runtime.GOARCH == "wasm" {
return <-ch return <-ch
} }

View File

@ -153,7 +153,7 @@ func (u *UserInterface) appMain(a app.App) {
} }
} }
func (u *UserInterface) Run(width, height int, scale float64, title string, g driver.GraphicsContext, mainloop bool, graphics driver.Graphics) error { func (u *UserInterface) Run(width, height int, scale float64, title string, context driver.UIContext, mainloop bool, graphics driver.Graphics) error {
if graphics != opengl.Get() { if graphics != opengl.Get() {
panic("ui: graphics driver must be OpenGL") panic("ui: graphics driver must be OpenGL")
} }
@ -174,9 +174,9 @@ func (u *UserInterface) Run(width, height int, scale float64, title string, g dr
} }
// Force to set the screen size // Force to set the screen size
u.updateGraphicsContext(g) u.updateGraphics(context)
for { for {
if err := u.update(g); err != nil { if err := u.update(context); err != nil {
return err return err
} }
} }
@ -193,7 +193,7 @@ func (u *UserInterface) Loop(ch <-chan error) error {
return nil return nil
} }
func (u *UserInterface) updateGraphicsContext(g driver.GraphicsContext) { func (u *UserInterface) updateGraphics(context driver.UIContext) {
width, height := 0, 0 width, height := 0, 0
actualScale := 0.0 actualScale := 0.0
@ -209,7 +209,7 @@ func (u *UserInterface) updateGraphicsContext(g driver.GraphicsContext) {
if sizeChanged { if sizeChanged {
// Sizing also calls GL functions // Sizing also calls GL functions
g.SetSize(width, height, actualScale) context.SetSize(width, height, actualScale)
} }
} }
@ -228,25 +228,25 @@ func (u *UserInterface) scaleImpl() float64 {
return scale return scale
} }
func (u *UserInterface) update(g driver.GraphicsContext) error { func (u *UserInterface) update(context driver.UIContext) error {
render: render:
for { for {
select { select {
case <-renderCh: case <-renderCh:
break render break render
case <-time.After(500 * time.Millisecond): case <-time.After(500 * time.Millisecond):
g.SuspendAudio() context.SuspendAudio()
continue continue
} }
} }
g.ResumeAudio() context.ResumeAudio()
defer func() { defer func() {
renderChEnd <- struct{}{} renderChEnd <- struct{}{}
}() }()
if err := g.Update(func() { if err := context.Update(func() {
u.updateGraphicsContext(g) u.updateGraphics(context)
}); err != nil { }); err != nil {
return err return err
} }

14
run.go
View File

@ -88,15 +88,15 @@ func IsRunningSlowly() bool {
return IsDrawingSkipped() return IsDrawingSkipped()
} }
var theGraphicsContext atomic.Value var theUIContext atomic.Value
func run(width, height int, scale float64, title string, g *graphicsContext, mainloop bool) error { func run(width, height int, scale float64, title string, context *uiContext, mainloop bool) error {
atomic.StoreInt32(&isRunning, 1) atomic.StoreInt32(&isRunning, 1)
// On GopherJS, run returns immediately. // On GopherJS, run returns immediately.
if !web.IsGopherJS() { if !web.IsGopherJS() {
defer atomic.StoreInt32(&isRunning, 0) defer atomic.StoreInt32(&isRunning, 0)
} }
if err := uiDriver().Run(width, height, scale, title, g, mainloop, graphicsDriver()); err != nil { if err := uiDriver().Run(width, height, scale, title, context, mainloop, graphicsDriver()); err != nil {
if err == driver.RegularTermination { if err == driver.RegularTermination {
return nil return nil
} }
@ -147,8 +147,8 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
go func() { go func() {
defer close(ch) defer close(ch)
g := newGraphicsContext(f) g := newUIContext(f)
theGraphicsContext.Store(g) theUIContext.Store(g)
if err := run(width, height, scale, title, g, true); err != nil { if err := run(width, height, scale, title, g, true); err != nil {
ch <- err ch <- err
return return
@ -173,8 +173,8 @@ func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64,
go func() { go func() {
defer close(ch) defer close(ch)
g := newGraphicsContext(f) g := newUIContext(f)
theGraphicsContext.Store(g) theUIContext.Store(g)
if err := run(width, height, scale, title, g, false); err != nil { if err := run(width, height, scale, title, g, false); err != nil {
ch <- err ch <- err
return return

View File

@ -29,13 +29,13 @@ func init() {
graphicscommand.SetGraphicsDriver(graphicsDriver()) graphicscommand.SetGraphicsDriver(graphicsDriver())
} }
func newGraphicsContext(f func(*Image) error) *graphicsContext { func newUIContext(f func(*Image) error) *uiContext {
return &graphicsContext{ return &uiContext{
f: f, f: f,
} }
} }
type graphicsContext struct { type uiContext struct {
f func(*Image) error f func(*Image) error
offscreen *Image offscreen *Image
screen *Image screen *Image
@ -47,7 +47,7 @@ type graphicsContext struct {
offsetY float64 offsetY float64
} }
func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale float64) { func (c *uiContext) SetSize(screenWidth, screenHeight int, screenScale float64) {
c.screenScale = screenScale c.screenScale = screenScale
if c.screen != nil { if c.screen != nil {
@ -71,7 +71,7 @@ func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale flo
c.offsetY = py0 c.offsetY = py0
} }
func (c *graphicsContext) initializeIfNeeded() error { func (c *uiContext) initializeIfNeeded() error {
if !c.initialized { if !c.initialized {
if err := shareable.InitializeGraphicsDriverState(); err != nil { if err := shareable.InitializeGraphicsDriverState(); err != nil {
return err return err
@ -84,7 +84,7 @@ func (c *graphicsContext) initializeIfNeeded() error {
return nil return nil
} }
func (c *graphicsContext) Update(afterFrameUpdate func()) error { func (c *uiContext) Update(afterFrameUpdate func()) error {
tps := int(MaxTPS()) tps := int(MaxTPS())
updateCount := clock.Update(tps) updateCount := clock.Update(tps)
@ -151,11 +151,11 @@ func (c *graphicsContext) Update(afterFrameUpdate func()) error {
return nil return nil
} }
func (c *graphicsContext) needsRestoring() (bool, error) { func (c *uiContext) needsRestoring() (bool, error) {
return c.offscreen.mipmap.original().IsInvalidated() return c.offscreen.mipmap.original().IsInvalidated()
} }
func (c *graphicsContext) restoreIfNeeded() error { func (c *uiContext) restoreIfNeeded() error {
if !shareable.IsRestoringEnabled() { if !shareable.IsRestoringEnabled() {
return nil return nil
} }
@ -172,10 +172,10 @@ func (c *graphicsContext) restoreIfNeeded() error {
return nil return nil
} }
func (c *graphicsContext) SuspendAudio() { func (c *uiContext) SuspendAudio() {
hooks.SuspendAudio() hooks.SuspendAudio()
} }
func (c *graphicsContext) ResumeAudio() { func (c *uiContext) ResumeAudio() {
hooks.ResumeAudio() hooks.ResumeAudio()
} }