From fcb5554aa1c427a81bd51c7b380632653c76da43 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 26 May 2019 19:08:46 +0900 Subject: [PATCH] driver: Add Graphics.NeedsRestoring --- internal/driver/graphics.go | 1 + internal/graphicscommand/command.go | 4 +++ internal/graphicsdriver/metal/driver.go | 4 +++ .../graphicsdriver/opengl/context_desktop.go | 4 +++ internal/graphicsdriver/opengl/context_js.go | 5 ++++ .../graphicsdriver/opengl/context_mobile.go | 4 +++ internal/graphicsdriver/opengl/driver.go | 4 +++ internal/restorable/enabled_desktop.go | 26 ------------------ internal/restorable/enabled_js.go | 27 ------------------- internal/restorable/image.go | 8 +++--- internal/restorable/images.go | 24 ++++++++--------- internal/shareable/shareable.go | 6 ++--- uicontext.go | 2 +- 13 files changed, 45 insertions(+), 74 deletions(-) delete mode 100644 internal/restorable/enabled_desktop.go delete mode 100644 internal/restorable/enabled_js.go diff --git a/internal/driver/graphics.go b/internal/driver/graphics.go index adac4afbc..849017163 100644 --- a/internal/driver/graphics.go +++ b/internal/driver/graphics.go @@ -31,6 +31,7 @@ type Graphics interface { Draw(indexLen int, indexOffset int, mode graphics.CompositeMode, colorM *affine.ColorM, filter graphics.Filter, address graphics.Address) error SetVsyncEnabled(enabled bool) VDirection() VDirection + NeedsRestoring() bool IsGL() bool } diff --git a/internal/graphicscommand/command.go b/internal/graphicscommand/command.go index 93e89b988..085faf227 100644 --- a/internal/graphicscommand/command.go +++ b/internal/graphicscommand/command.go @@ -28,6 +28,10 @@ func SetGraphicsDriver(driver driver.Graphics) { theGraphicsDriver = driver } +func NeedsRestoring() bool { + return theGraphicsDriver.NeedsRestoring() +} + // command represents a drawing command. // // A command for drawing that is created when Image functions are called like DrawTriangles, diff --git a/internal/graphicsdriver/metal/driver.go b/internal/graphicsdriver/metal/driver.go index 09783274c..9c5807eea 100644 --- a/internal/graphicsdriver/metal/driver.go +++ b/internal/graphicsdriver/metal/driver.go @@ -707,6 +707,10 @@ func (d *Driver) VDirection() driver.VDirection { return driver.VUpward } +func (d *Driver) NeedsRestoring() bool { + return false +} + func (d *Driver) IsGL() bool { return false } diff --git a/internal/graphicsdriver/opengl/context_desktop.go b/internal/graphicsdriver/opengl/context_desktop.go index 37638a99e..e9349e7a3 100644 --- a/internal/graphicsdriver/opengl/context_desktop.go +++ b/internal/graphicsdriver/opengl/context_desktop.go @@ -500,3 +500,7 @@ func (c *context) flush() { return nil }) } + +func (c *context) needsRestoring() bool { + return false +} diff --git a/internal/graphicsdriver/opengl/context_js.go b/internal/graphicsdriver/opengl/context_js.go index 9382edf1b..c290d9915 100644 --- a/internal/graphicsdriver/opengl/context_js.go +++ b/internal/graphicsdriver/opengl/context_js.go @@ -22,6 +22,7 @@ import ( "syscall/js" "github.com/hajimehoshi/ebiten/internal/graphics" + "github.com/hajimehoshi/ebiten/internal/web" ) type ( @@ -459,3 +460,7 @@ func (c *context) flush() { gl := c.gl gl.Call("flush") } + +func (c *context) needsRestoring() bool { + return !web.IsMobileBrowser() +} diff --git a/internal/graphicsdriver/opengl/context_mobile.go b/internal/graphicsdriver/opengl/context_mobile.go index 87a67305b..7ea7e7a9d 100644 --- a/internal/graphicsdriver/opengl/context_mobile.go +++ b/internal/graphicsdriver/opengl/context_mobile.go @@ -378,3 +378,7 @@ func (c *context) flush() { gl := c.gl gl.Flush() } + +func (c *context) needsRestoring() bool { + return true +} diff --git a/internal/graphicsdriver/opengl/driver.go b/internal/graphicsdriver/opengl/driver.go index 4b7665a6b..8250b16b7 100644 --- a/internal/graphicsdriver/opengl/driver.go +++ b/internal/graphicsdriver/opengl/driver.go @@ -126,6 +126,10 @@ func (d *Driver) VDirection() driver.VDirection { return driver.VDownward } +func (d *Driver) NeedsRestoring() bool { + return d.context.needsRestoring() +} + func (d *Driver) IsGL() bool { return true } diff --git a/internal/restorable/enabled_desktop.go b/internal/restorable/enabled_desktop.go deleted file mode 100644 index 158f365a0..000000000 --- a/internal/restorable/enabled_desktop.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2017 The Ebiten Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// +build darwin freebsd linux windows -// +build !js -// +build !android -// +build !ios - -package restorable - -func init() { - // OpenGL (not ES) never causes context lost, - // so restorable feature is not needed. - restoringEnabled = false -} diff --git a/internal/restorable/enabled_js.go b/internal/restorable/enabled_js.go deleted file mode 100644 index 2013c900d..000000000 --- a/internal/restorable/enabled_js.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2017 The Ebiten Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// +build js - -package restorable - -import ( - "github.com/hajimehoshi/ebiten/internal/web" -) - -func init() { - if web.IsMobileBrowser() { - restoringEnabled = false - } -} diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 6d4290dad..ee2ce2705 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -301,7 +301,7 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) { } i.image.ReplacePixels(pixels, x, y, width, height) - if !IsRestoringEnabled() { + if !NeedsRestoring() { i.makeStale() return } @@ -362,7 +362,7 @@ func (i *Image) DrawTriangles(img *Image, vertices []float32, indices []uint16, } theImages.makeStaleIfDependingOn(i) - if img.stale || img.volatile || i.screen || !IsRestoringEnabled() || i.volatile { + if img.stale || img.volatile || i.screen || !NeedsRestoring() || i.volatile { i.makeStale() } else { i.appendDrawTrianglesHistory(img, vertices, indices, colorm, mode, filter, address) @@ -447,7 +447,7 @@ func (i *Image) readPixelsFromGPU() { // resolveStale resolves the image's 'stale' state. func (i *Image) resolveStale() { - if !IsRestoringEnabled() { + if !NeedsRestoring() { return } @@ -557,7 +557,7 @@ func (i *Image) Dispose() { func (i *Image) IsInvalidated() (bool, error) { // FlushCommands is required because c.offscreen.impl might not have an actual texture. graphicscommand.FlushCommands() - if !IsRestoringEnabled() { + if !NeedsRestoring() { return false, nil } diff --git a/internal/restorable/images.go b/internal/restorable/images.go index 42e2348d6..44f04f83a 100644 --- a/internal/restorable/images.go +++ b/internal/restorable/images.go @@ -20,22 +20,20 @@ import ( "github.com/hajimehoshi/ebiten/internal/graphicscommand" ) -// restoringEnabled indicates if restoring happens or not. -// -// This value is overridden at enabled_*.go. -var restoringEnabled = true +// forceRestoring reports whether restoring forcely happens or not. +var forceRestoring = true -// IsRestoringEnabled returns a boolean value indicating whether -// restoring process works or not. -func IsRestoringEnabled() bool { - // This value is updated only at init or EnableRestoringForTesting. - // No need to lock here. - return restoringEnabled +// NeedsRestoring reports whether restoring process works or not. +func NeedsRestoring() bool { + if forceRestoring { + return true + } + return graphicscommand.NeedsRestoring() } // EnableRestoringForTesting forces to enable restoring for testing. func EnableRestoringForTesting() { - restoringEnabled = true + forceRestoring = true } // images is a set of Image objects. @@ -55,7 +53,7 @@ var theImages = &images{ // ResolveStaleImages is intended to be called at the end of a frame. func ResolveStaleImages() { graphicscommand.FlushCommands() - if !restoringEnabled { + if !NeedsRestoring() { return } theImages.resolveStaleImages() @@ -146,7 +144,7 @@ func (i *images) makeStaleIfDependingOnImpl(target *Image) { // // Restoring means to make all *graphicscommand.Image objects have their textures and framebuffers. func (i *images) restore() error { - if !IsRestoringEnabled() { + if !NeedsRestoring() { panic("restorable: restore cannot be called when restoring is disabled") } diff --git a/internal/shareable/shareable.go b/internal/shareable/shareable.go index 4f383989a..5a9b01040 100644 --- a/internal/shareable/shareable.go +++ b/internal/shareable/shareable.go @@ -508,9 +508,9 @@ func ResolveStaleImages() { restorable.ResolveStaleImages() } -func IsRestoringEnabled() bool { - // As IsRestoringEnabled is an immutable state, no need to lock here. - return restorable.IsRestoringEnabled() +func NeedsRestoring() bool { + // As NeedsRestoring is an immutable state, no need to lock here. + return restorable.NeedsRestoring() } func Restore() error { diff --git a/uicontext.go b/uicontext.go index f7232534c..e5e37a943 100644 --- a/uicontext.go +++ b/uicontext.go @@ -151,7 +151,7 @@ func (c *uiContext) needsRestoring() (bool, error) { } func (c *uiContext) restoreIfNeeded() error { - if !shareable.IsRestoringEnabled() { + if !shareable.NeedsRestoring() { return nil } r, err := c.needsRestoring()