driver: Add Graphics.NeedsRestoring

This commit is contained in:
Hajime Hoshi 2019-05-26 19:08:46 +09:00
parent 1d51435bbb
commit fcb5554aa1
13 changed files with 45 additions and 74 deletions

View File

@ -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 Draw(indexLen int, indexOffset int, mode graphics.CompositeMode, colorM *affine.ColorM, filter graphics.Filter, address graphics.Address) error
SetVsyncEnabled(enabled bool) SetVsyncEnabled(enabled bool)
VDirection() VDirection VDirection() VDirection
NeedsRestoring() bool
IsGL() bool IsGL() bool
} }

View File

@ -28,6 +28,10 @@ func SetGraphicsDriver(driver driver.Graphics) {
theGraphicsDriver = driver theGraphicsDriver = driver
} }
func NeedsRestoring() bool {
return theGraphicsDriver.NeedsRestoring()
}
// command represents a drawing command. // command represents a drawing command.
// //
// A command for drawing that is created when Image functions are called like DrawTriangles, // A command for drawing that is created when Image functions are called like DrawTriangles,

View File

@ -707,6 +707,10 @@ func (d *Driver) VDirection() driver.VDirection {
return driver.VUpward return driver.VUpward
} }
func (d *Driver) NeedsRestoring() bool {
return false
}
func (d *Driver) IsGL() bool { func (d *Driver) IsGL() bool {
return false return false
} }

View File

@ -500,3 +500,7 @@ func (c *context) flush() {
return nil return nil
}) })
} }
func (c *context) needsRestoring() bool {
return false
}

View File

@ -22,6 +22,7 @@ import (
"syscall/js" "syscall/js"
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/web"
) )
type ( type (
@ -459,3 +460,7 @@ func (c *context) flush() {
gl := c.gl gl := c.gl
gl.Call("flush") gl.Call("flush")
} }
func (c *context) needsRestoring() bool {
return !web.IsMobileBrowser()
}

View File

@ -378,3 +378,7 @@ func (c *context) flush() {
gl := c.gl gl := c.gl
gl.Flush() gl.Flush()
} }
func (c *context) needsRestoring() bool {
return true
}

View File

@ -126,6 +126,10 @@ func (d *Driver) VDirection() driver.VDirection {
return driver.VDownward return driver.VDownward
} }
func (d *Driver) NeedsRestoring() bool {
return d.context.needsRestoring()
}
func (d *Driver) IsGL() bool { func (d *Driver) IsGL() bool {
return true return true
} }

View File

@ -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
}

View File

@ -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
}
}

View File

@ -301,7 +301,7 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
} }
i.image.ReplacePixels(pixels, x, y, width, height) i.image.ReplacePixels(pixels, x, y, width, height)
if !IsRestoringEnabled() { if !NeedsRestoring() {
i.makeStale() i.makeStale()
return return
} }
@ -362,7 +362,7 @@ func (i *Image) DrawTriangles(img *Image, vertices []float32, indices []uint16,
} }
theImages.makeStaleIfDependingOn(i) 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() i.makeStale()
} else { } else {
i.appendDrawTrianglesHistory(img, vertices, indices, colorm, mode, filter, address) i.appendDrawTrianglesHistory(img, vertices, indices, colorm, mode, filter, address)
@ -447,7 +447,7 @@ func (i *Image) readPixelsFromGPU() {
// resolveStale resolves the image's 'stale' state. // resolveStale resolves the image's 'stale' state.
func (i *Image) resolveStale() { func (i *Image) resolveStale() {
if !IsRestoringEnabled() { if !NeedsRestoring() {
return return
} }
@ -557,7 +557,7 @@ func (i *Image) Dispose() {
func (i *Image) IsInvalidated() (bool, error) { func (i *Image) IsInvalidated() (bool, error) {
// FlushCommands is required because c.offscreen.impl might not have an actual texture. // FlushCommands is required because c.offscreen.impl might not have an actual texture.
graphicscommand.FlushCommands() graphicscommand.FlushCommands()
if !IsRestoringEnabled() { if !NeedsRestoring() {
return false, nil return false, nil
} }

View File

@ -20,22 +20,20 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphicscommand" "github.com/hajimehoshi/ebiten/internal/graphicscommand"
) )
// restoringEnabled indicates if restoring happens or not. // forceRestoring reports whether restoring forcely happens or not.
// var forceRestoring = true
// This value is overridden at enabled_*.go.
var restoringEnabled = true
// IsRestoringEnabled returns a boolean value indicating whether // NeedsRestoring reports whether restoring process works or not.
// restoring process works or not. func NeedsRestoring() bool {
func IsRestoringEnabled() bool { if forceRestoring {
// This value is updated only at init or EnableRestoringForTesting. return true
// No need to lock here. }
return restoringEnabled return graphicscommand.NeedsRestoring()
} }
// EnableRestoringForTesting forces to enable restoring for testing. // EnableRestoringForTesting forces to enable restoring for testing.
func EnableRestoringForTesting() { func EnableRestoringForTesting() {
restoringEnabled = true forceRestoring = true
} }
// images is a set of Image objects. // 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. // ResolveStaleImages is intended to be called at the end of a frame.
func ResolveStaleImages() { func ResolveStaleImages() {
graphicscommand.FlushCommands() graphicscommand.FlushCommands()
if !restoringEnabled { if !NeedsRestoring() {
return return
} }
theImages.resolveStaleImages() 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. // Restoring means to make all *graphicscommand.Image objects have their textures and framebuffers.
func (i *images) restore() error { func (i *images) restore() error {
if !IsRestoringEnabled() { if !NeedsRestoring() {
panic("restorable: restore cannot be called when restoring is disabled") panic("restorable: restore cannot be called when restoring is disabled")
} }

View File

@ -508,9 +508,9 @@ func ResolveStaleImages() {
restorable.ResolveStaleImages() restorable.ResolveStaleImages()
} }
func IsRestoringEnabled() bool { func NeedsRestoring() bool {
// As IsRestoringEnabled is an immutable state, no need to lock here. // As NeedsRestoring is an immutable state, no need to lock here.
return restorable.IsRestoringEnabled() return restorable.NeedsRestoring()
} }
func Restore() error { func Restore() error {

View File

@ -151,7 +151,7 @@ func (c *uiContext) needsRestoring() (bool, error) {
} }
func (c *uiContext) restoreIfNeeded() error { func (c *uiContext) restoreIfNeeded() error {
if !shareable.IsRestoringEnabled() { if !shareable.NeedsRestoring() {
return nil return nil
} }
r, err := c.needsRestoring() r, err := c.needsRestoring()