2024-09-06 07:29:37 +02:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package restorable
|
|
|
|
|
|
|
|
import (
|
2024-09-06 09:30:20 +02:00
|
|
|
"image"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2024-09-06 07:33:48 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/debug"
|
2024-09-06 07:29:37 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
|
|
|
)
|
|
|
|
|
2024-09-07 12:07:13 +02:00
|
|
|
// forceRestoration reports whether restoration forcibly happens or not.
|
2024-09-06 09:30:20 +02:00
|
|
|
// This is used only for testing.
|
2024-09-07 12:07:13 +02:00
|
|
|
var forceRestoration = false
|
2024-09-06 08:50:08 +02:00
|
|
|
|
2024-09-07 12:07:13 +02:00
|
|
|
// disabled indicates that restoration is disabled or not.
|
|
|
|
// Restoration is enabled by default for some platforms like Android for safety.
|
|
|
|
// Before SetGame, it is not possible to determine whether restoration is needed or not.
|
2024-09-06 09:30:20 +02:00
|
|
|
var disabled atomic.Bool
|
|
|
|
|
|
|
|
var disabledOnce sync.Once
|
|
|
|
|
2024-09-07 12:07:13 +02:00
|
|
|
// Disable disables restoration.
|
2024-09-06 09:30:20 +02:00
|
|
|
func Disable() {
|
|
|
|
disabled.Store(true)
|
|
|
|
}
|
|
|
|
|
2024-09-07 12:07:13 +02:00
|
|
|
// needsRestoration reports whether restoration process works or not.
|
|
|
|
func needsRestoration() bool {
|
|
|
|
if forceRestoration {
|
2024-09-06 09:30:20 +02:00
|
|
|
return true
|
|
|
|
}
|
2024-09-07 12:07:13 +02:00
|
|
|
// TODO: If Vulkan is introduced, restoration might not be needed.
|
2024-09-06 09:30:20 +02:00
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
return !disabled.Load()
|
|
|
|
}
|
|
|
|
return false
|
2024-09-06 08:50:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AlwaysReadPixelsFromGPU reports whether ReadPixels always reads pixels from GPU or not.
|
|
|
|
func AlwaysReadPixelsFromGPU() bool {
|
2024-09-07 12:07:13 +02:00
|
|
|
return !needsRestoration()
|
2024-09-06 08:50:08 +02:00
|
|
|
}
|
|
|
|
|
2024-09-06 07:33:48 +02:00
|
|
|
// images is a set of Image objects.
|
|
|
|
type images struct {
|
2024-09-06 09:30:20 +02:00
|
|
|
images map[*Image]struct{}
|
|
|
|
shaders map[*Shader]struct{}
|
|
|
|
contextLost atomic.Bool
|
2024-09-06 07:33:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// theImages represents the images for the current process.
|
|
|
|
var theImages = &images{
|
2024-09-06 08:27:50 +02:00
|
|
|
images: map[*Image]struct{}{},
|
|
|
|
shaders: map[*Shader]struct{}{},
|
2024-09-06 07:33:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func SwapBuffers(graphicsDriver graphicsdriver.Graphics) error {
|
|
|
|
if debug.IsDebug {
|
|
|
|
debug.FrameLogf("Internal image sizes:\n")
|
|
|
|
imgs := make([]*graphicscommand.Image, 0, len(theImages.images))
|
|
|
|
for i := range theImages.images {
|
2024-09-06 07:39:33 +02:00
|
|
|
imgs = append(imgs, i.image)
|
2024-09-06 07:33:48 +02:00
|
|
|
}
|
|
|
|
graphicscommand.LogImagesInfo(imgs)
|
|
|
|
}
|
2024-09-06 08:50:08 +02:00
|
|
|
return resolveStaleImages(graphicsDriver, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolveStaleImages flushes the queued draw commands and resolves all stale images.
|
|
|
|
// If endFrame is true, the current screen might be used to present when flushing the commands.
|
|
|
|
func resolveStaleImages(graphicsDriver graphicsdriver.Graphics, endFrame bool) error {
|
2024-09-06 09:30:20 +02:00
|
|
|
// When Disable is called, all the images data should be evicted once.
|
|
|
|
if disabled.Load() {
|
|
|
|
disabledOnce.Do(func() {
|
|
|
|
for img := range theImages.images {
|
2024-09-08 09:11:57 +02:00
|
|
|
img.makeStale(image.Rect(0, 0, img.width, img.height))
|
2024-09-06 09:30:20 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-06 08:50:08 +02:00
|
|
|
if err := graphicscommand.FlushCommands(graphicsDriver, endFrame); err != nil {
|
2024-09-06 07:33:48 +02:00
|
|
|
return err
|
|
|
|
}
|
2024-09-07 12:07:13 +02:00
|
|
|
if !needsRestoration() {
|
2024-09-06 08:50:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return theImages.resolveStaleImages(graphicsDriver)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreIfNeeded restores the images.
|
|
|
|
//
|
2024-09-07 12:07:13 +02:00
|
|
|
// Restoration means to make all *graphicscommand.Image objects have their textures and framebuffers.
|
2024-09-06 08:50:08 +02:00
|
|
|
func RestoreIfNeeded(graphicsDriver graphicsdriver.Graphics) error {
|
2024-09-07 12:07:13 +02:00
|
|
|
if !needsRestoration() {
|
2024-09-06 08:50:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-07 12:07:13 +02:00
|
|
|
if !forceRestoration && !theImages.contextLost.Load() {
|
2024-09-06 09:30:20 +02:00
|
|
|
return nil
|
2024-09-06 08:50:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := graphicscommand.ResetGraphicsDriverState(graphicsDriver); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return theImages.restore(graphicsDriver)
|
2024-09-06 07:33:48 +02:00
|
|
|
}
|
|
|
|
|
2024-09-06 07:39:33 +02:00
|
|
|
// DumpImages dumps all the current images to the specified directory.
|
|
|
|
//
|
|
|
|
// This is for testing usage.
|
|
|
|
func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) (string, error) {
|
|
|
|
images := make([]*graphicscommand.Image, 0, len(theImages.images))
|
|
|
|
for img := range theImages.images {
|
|
|
|
images = append(images, img.image)
|
|
|
|
}
|
|
|
|
|
|
|
|
return graphicscommand.DumpImages(images, graphicsDriver, dir)
|
|
|
|
}
|
|
|
|
|
2024-09-06 07:33:48 +02:00
|
|
|
// add adds img to the images.
|
|
|
|
func (i *images) add(img *Image) {
|
|
|
|
i.images[img] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2024-09-06 08:27:50 +02:00
|
|
|
func (i *images) addShader(shader *Shader) {
|
|
|
|
i.shaders[shader] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2024-09-06 07:33:48 +02:00
|
|
|
// remove removes img from the images.
|
|
|
|
func (i *images) remove(img *Image) {
|
2024-09-06 08:28:19 +02:00
|
|
|
i.makeStaleIfDependingOn(img)
|
2024-09-06 07:33:48 +02:00
|
|
|
delete(i.images, img)
|
|
|
|
}
|
|
|
|
|
2024-09-06 08:27:50 +02:00
|
|
|
func (i *images) removeShader(shader *Shader) {
|
2024-09-06 08:28:19 +02:00
|
|
|
i.makeStaleIfDependingOnShader(shader)
|
2024-09-06 08:27:50 +02:00
|
|
|
delete(i.shaders, shader)
|
|
|
|
}
|
|
|
|
|
2024-09-06 08:50:08 +02:00
|
|
|
// resolveStaleImages resolves stale images.
|
|
|
|
func (i *images) resolveStaleImages(graphicsDriver graphicsdriver.Graphics) error {
|
|
|
|
for img := range i.images {
|
|
|
|
if err := img.resolveStale(graphicsDriver); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-08 10:03:18 +02:00
|
|
|
// makeStaleIfDependingOn makes all the images stale that depend on src.
|
2024-09-06 08:28:19 +02:00
|
|
|
//
|
2024-09-08 10:03:18 +02:00
|
|
|
// When src is modified, all images depending on src can't be restored with src.
|
2024-09-08 10:06:25 +02:00
|
|
|
// makeStaleIfDependingOn is called in such situation.
|
2024-09-08 10:03:18 +02:00
|
|
|
func (i *images) makeStaleIfDependingOn(src *Image) {
|
|
|
|
if src == nil {
|
|
|
|
panic("restorable: src must not be nil at makeStaleIfDependingOn")
|
2024-09-06 08:28:19 +02:00
|
|
|
}
|
|
|
|
for img := range i.images {
|
2024-09-08 10:03:18 +02:00
|
|
|
img.makeStaleIfDependingOn(src)
|
2024-09-06 08:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 18:04:51 +02:00
|
|
|
// makeStaleIfDependingOnAtRegion makes all the images stale that depend on src at srcRegion.
|
|
|
|
//
|
|
|
|
// When src is modified, all images depending on src can't be restored with src at srcRegion.
|
|
|
|
// makeStaleIfDependingOnAtRegion is called in such situation.
|
|
|
|
func (i *images) makeStaleIfDependingOnAtRegion(src *Image, srcRegion image.Rectangle) {
|
|
|
|
if src == nil {
|
|
|
|
panic("restorable: src must not be nil at makeStaleIfDependingOnAtRegion")
|
|
|
|
}
|
|
|
|
for img := range i.images {
|
|
|
|
img.makeStaleIfDependingOnAtRegion(src, srcRegion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 08:28:19 +02:00
|
|
|
// makeStaleIfDependingOn makes all the images stale that depend on shader.
|
|
|
|
func (i *images) makeStaleIfDependingOnShader(shader *Shader) {
|
|
|
|
if shader == nil {
|
|
|
|
panic("restorable: shader must not be nil at makeStaleIfDependingOnShader")
|
|
|
|
}
|
|
|
|
for img := range i.images {
|
|
|
|
img.makeStaleIfDependingOnShader(shader)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 08:50:08 +02:00
|
|
|
// restore restores the images.
|
|
|
|
//
|
2024-09-07 12:07:13 +02:00
|
|
|
// Restoration means to make all *graphicscommand.Image objects have their textures and framebuffers.
|
2024-09-06 08:50:08 +02:00
|
|
|
func (i *images) restore(graphicsDriver graphicsdriver.Graphics) error {
|
2024-09-07 12:07:13 +02:00
|
|
|
if !needsRestoration() {
|
|
|
|
panic("restorable: restore cannot be called when restoration is disabled")
|
2024-09-06 08:50:08 +02:00
|
|
|
}
|
|
|
|
|
2024-09-07 12:07:13 +02:00
|
|
|
// Dispose all the shaders ahead of restoration. A current shader ID and a new shader ID can be duplicated.
|
2024-09-06 08:50:08 +02:00
|
|
|
for s := range i.shaders {
|
|
|
|
s.shader.Dispose()
|
|
|
|
s.shader = nil
|
|
|
|
}
|
|
|
|
for s := range i.shaders {
|
|
|
|
s.restore()
|
|
|
|
}
|
|
|
|
|
2024-09-07 12:07:13 +02:00
|
|
|
// Dispose all the images ahead of restoration. A current texture ID and a new texture ID can be duplicated.
|
2024-09-06 08:50:08 +02:00
|
|
|
// TODO: Write a test to confirm that ID duplication never happens.
|
|
|
|
for i := range i.images {
|
|
|
|
i.image.Dispose()
|
|
|
|
i.image = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's do topological sort based on dependencies of drawing history.
|
|
|
|
// It is assured that there are not loops since cyclic drawing makes images stale.
|
|
|
|
type edge struct {
|
|
|
|
source *Image
|
|
|
|
target *Image
|
|
|
|
}
|
|
|
|
images := map[*Image]struct{}{}
|
|
|
|
for i := range i.images {
|
|
|
|
images[i] = struct{}{}
|
|
|
|
}
|
|
|
|
edges := map[edge]struct{}{}
|
|
|
|
for t := range images {
|
|
|
|
for s := range t.dependingImages() {
|
|
|
|
edges[edge{source: s, target: t}] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var sorted []*Image
|
|
|
|
for len(images) > 0 {
|
|
|
|
// current represents images that have no incoming edges.
|
|
|
|
current := map[*Image]struct{}{}
|
|
|
|
for i := range images {
|
|
|
|
current[i] = struct{}{}
|
|
|
|
}
|
|
|
|
for e := range edges {
|
2024-09-08 09:03:56 +02:00
|
|
|
delete(current, e.target)
|
2024-09-06 08:50:08 +02:00
|
|
|
}
|
|
|
|
for i := range current {
|
|
|
|
delete(images, i)
|
|
|
|
sorted = append(sorted, i)
|
|
|
|
}
|
|
|
|
removed := []edge{}
|
|
|
|
for e := range edges {
|
|
|
|
if _, ok := current[e.source]; ok {
|
|
|
|
removed = append(removed, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, e := range removed {
|
|
|
|
delete(edges, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, img := range sorted {
|
|
|
|
if err := img.restore(graphicsDriver); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 09:30:20 +02:00
|
|
|
i.contextLost.Store(false)
|
|
|
|
|
2024-09-06 08:50:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-06 07:29:37 +02:00
|
|
|
var graphicsDriverInitialized bool
|
|
|
|
|
|
|
|
// InitializeGraphicsDriverState initializes the graphics driver state.
|
|
|
|
func InitializeGraphicsDriverState(graphicsDriver graphicsdriver.Graphics) error {
|
|
|
|
graphicsDriverInitialized = true
|
|
|
|
return graphicscommand.InitializeGraphicsDriverState(graphicsDriver)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxImageSize returns the maximum size of an image.
|
|
|
|
func MaxImageSize(graphicsDriver graphicsdriver.Graphics) int {
|
|
|
|
return graphicscommand.MaxImageSize(graphicsDriver)
|
|
|
|
}
|
2024-09-06 09:30:20 +02:00
|
|
|
|
|
|
|
// OnContextLost is called when the context lost is detected in an explicit way.
|
|
|
|
func OnContextLost() {
|
|
|
|
theImages.contextLost.Store(true)
|
|
|
|
}
|