2017-05-03 16:11:43 +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 (
|
2022-03-22 18:45:57 +01:00
|
|
|
"runtime"
|
2019-07-19 18:42:19 +02:00
|
|
|
|
2021-08-04 18:15:41 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/debug"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2017-05-03 16:11:43 +02:00
|
|
|
)
|
|
|
|
|
2019-05-26 12:08:46 +02:00
|
|
|
// forceRestoring reports whether restoring forcely happens or not.
|
2019-05-26 14:40:10 +02:00
|
|
|
var forceRestoring = false
|
2019-05-26 12:08:46 +02:00
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
var needsRestoringByGraphicsDriver bool
|
|
|
|
|
2022-06-09 19:58:01 +02:00
|
|
|
// needsRestoring reports whether restoring process works or not.
|
|
|
|
func needsRestoring() bool {
|
2022-03-19 15:55:14 +01:00
|
|
|
return forceRestoring || needsRestoringByGraphicsDriver
|
2017-07-02 08:58:00 +02:00
|
|
|
}
|
|
|
|
|
2023-02-25 17:31:11 +01:00
|
|
|
// AlwaysReadPixelsFromGPU reports whether ReadPixels always reads pixels from GPU or not.
|
|
|
|
func AlwaysReadPixelsFromGPU() bool {
|
|
|
|
return !needsRestoring()
|
2023-02-25 15:11:39 +01:00
|
|
|
}
|
|
|
|
|
2017-09-11 20:12:17 +02:00
|
|
|
// EnableRestoringForTesting forces to enable restoring for testing.
|
2017-07-02 08:58:00 +02:00
|
|
|
func EnableRestoringForTesting() {
|
2019-05-26 12:08:46 +02:00
|
|
|
forceRestoring = true
|
2017-07-02 08:58:00 +02:00
|
|
|
}
|
|
|
|
|
2017-09-11 20:12:17 +02:00
|
|
|
// images is a set of Image objects.
|
2017-05-03 16:11:43 +02:00
|
|
|
type images struct {
|
2020-05-30 12:43:59 +02:00
|
|
|
images map[*Image]struct{}
|
|
|
|
shaders map[*Shader]struct{}
|
|
|
|
lastTarget *Image
|
|
|
|
contextLost bool
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// theImages represents the images for the current process.
|
2017-05-03 16:11:43 +02:00
|
|
|
var theImages = &images{
|
2020-05-24 17:46:23 +02:00
|
|
|
images: map[*Image]struct{}{},
|
|
|
|
shaders: map[*Shader]struct{}{},
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 17:18:26 +02:00
|
|
|
func SwapBuffers(graphicsDriver graphicsdriver.Graphics, swapBuffersForGL func()) error {
|
2021-08-04 18:15:41 +02:00
|
|
|
if debug.IsDebug {
|
|
|
|
debug.Logf("Internal image sizes:\n")
|
|
|
|
imgs := make([]*graphicscommand.Image, 0, len(theImages.images))
|
|
|
|
for i := range theImages.images {
|
|
|
|
imgs = append(imgs, i.image)
|
|
|
|
}
|
|
|
|
graphicscommand.LogImagesInfo(imgs)
|
|
|
|
}
|
2023-08-31 16:31:42 +02:00
|
|
|
return resolveStaleImages(graphicsDriver, true, swapBuffersForGL)
|
2023-05-07 18:06:14 +02:00
|
|
|
}
|
2021-08-04 18:15:41 +02:00
|
|
|
|
2023-05-07 18:06:14 +02:00
|
|
|
// 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.
|
2023-07-29 19:25:10 +02:00
|
|
|
func resolveStaleImages(graphicsDriver graphicsdriver.Graphics, endFrame bool, swapBuffersForGL func()) error {
|
|
|
|
if err := graphicscommand.FlushCommands(graphicsDriver, endFrame, swapBuffersForGL); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-06-09 19:58:01 +02:00
|
|
|
if !needsRestoring() {
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2018-02-22 19:19:20 +01:00
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
return theImages.resolveStaleImages(graphicsDriver)
|
2017-05-03 17:08:07 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 14:10:25 +02:00
|
|
|
// RestoreIfNeeded restores the images.
|
2017-09-14 17:24:18 +02:00
|
|
|
//
|
2018-10-28 12:10:05 +01:00
|
|
|
// Restoring means to make all *graphicscommand.Image objects have their textures and framebuffers.
|
2022-03-19 15:55:14 +01:00
|
|
|
func RestoreIfNeeded(graphicsDriver graphicsdriver.Graphics) error {
|
2022-06-09 19:58:01 +02:00
|
|
|
if !needsRestoring() {
|
2019-05-26 14:10:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !forceRestoring {
|
2020-05-30 12:43:59 +02:00
|
|
|
var r bool
|
|
|
|
|
|
|
|
if canDetectContextLostExplicitly {
|
|
|
|
r = theImages.contextLost
|
|
|
|
} else {
|
|
|
|
// As isInvalidated() is expensive, call this only for one image.
|
|
|
|
// This assumes that if there is one image that is invalidated, all images are invalidated.
|
|
|
|
for img := range theImages.images {
|
|
|
|
// The screen image might not have a texture. Skip this.
|
2022-06-07 16:45:35 +02:00
|
|
|
if img.imageType == ImageTypeScreen {
|
2020-05-30 12:43:59 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
var err error
|
2022-03-19 15:55:14 +01:00
|
|
|
r, err = img.isInvalidated(graphicsDriver)
|
2020-05-30 12:43:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
2020-01-18 17:18:56 +01:00
|
|
|
}
|
2019-05-26 14:10:25 +02:00
|
|
|
}
|
2020-05-30 12:43:59 +02:00
|
|
|
|
2019-05-26 14:10:25 +02:00
|
|
|
if !r {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 04:43:10 +01:00
|
|
|
if err := graphicscommand.ResetGraphicsDriverState(graphicsDriver); err != nil {
|
2017-08-06 16:00:24 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
return theImages.restore(graphicsDriver)
|
2017-05-03 17:08:07 +02:00
|
|
|
}
|
|
|
|
|
2019-07-19 16:29:19 +02:00
|
|
|
// DumpImages dumps all the current images to the specified directory.
|
2019-07-14 21:14:57 +02:00
|
|
|
//
|
|
|
|
// This is for testing usage.
|
2022-08-31 06:10:41 +02:00
|
|
|
func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) (string, error) {
|
2022-08-30 20:10:10 +02:00
|
|
|
images := make([]*graphicscommand.Image, 0, len(theImages.images))
|
2019-02-19 02:15:23 +01:00
|
|
|
for img := range theImages.images {
|
2022-08-30 20:10:10 +02:00
|
|
|
images = append(images, img.image)
|
2018-04-28 14:49:00 +02:00
|
|
|
}
|
2022-08-30 20:10:10 +02:00
|
|
|
|
|
|
|
return graphicscommand.DumpImages(images, graphicsDriver, dir)
|
2018-04-28 14:49:00 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// add adds img to the images.
|
2017-05-03 16:15:18 +02:00
|
|
|
func (i *images) add(img *Image) {
|
2017-05-03 16:11:43 +02:00
|
|
|
i.images[img] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:46:23 +02:00
|
|
|
func (i *images) addShader(shader *Shader) {
|
|
|
|
i.shaders[shader] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// remove removes img from the images.
|
2017-05-03 16:24:00 +02:00
|
|
|
func (i *images) remove(img *Image) {
|
2020-05-25 17:20:08 +02:00
|
|
|
i.makeStaleIfDependingOn(img)
|
2017-05-03 16:11:43 +02:00
|
|
|
delete(i.images, img)
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:46:23 +02:00
|
|
|
func (i *images) removeShader(shader *Shader) {
|
2020-05-25 17:20:08 +02:00
|
|
|
i.makeStaleIfDependingOnShader(shader)
|
2020-05-24 17:46:23 +02:00
|
|
|
delete(i.shaders, shader)
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// resolveStaleImages resolves stale images.
|
2022-03-19 15:55:14 +01:00
|
|
|
func (i *images) resolveStaleImages(graphicsDriver graphicsdriver.Graphics) error {
|
2017-09-14 17:24:18 +02:00
|
|
|
i.lastTarget = nil
|
2017-05-03 16:11:43 +02:00
|
|
|
for img := range i.images {
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := img.resolveStale(graphicsDriver); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
return err
|
|
|
|
}
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// makeStaleIfDependingOn makes all the images stale that depend on target.
|
|
|
|
//
|
2020-05-25 15:49:41 +02:00
|
|
|
// When target is modified, all images depending on target can't be restored with target.
|
2017-09-14 17:24:18 +02:00
|
|
|
// makeStaleIfDependingOn is called in such situation.
|
|
|
|
func (i *images) makeStaleIfDependingOn(target *Image) {
|
2017-05-03 16:11:43 +02:00
|
|
|
if target == nil {
|
2020-05-25 17:20:08 +02:00
|
|
|
panic("restorable: target must not be nil at makeStaleIfDependingOn")
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
2017-09-14 17:24:18 +02:00
|
|
|
if i.lastTarget == target {
|
2017-05-03 16:11:43 +02:00
|
|
|
return
|
|
|
|
}
|
2017-09-14 17:24:18 +02:00
|
|
|
i.lastTarget = target
|
2017-05-03 16:11:43 +02:00
|
|
|
for img := range i.images {
|
2017-05-03 16:18:35 +02:00
|
|
|
img.makeStaleIfDependingOn(target)
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 17:20:08 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// restore restores the images.
|
|
|
|
//
|
2018-10-28 12:10:05 +01:00
|
|
|
// Restoring means to make all *graphicscommand.Image objects have their textures and framebuffers.
|
2022-03-19 15:55:14 +01:00
|
|
|
func (i *images) restore(graphicsDriver graphicsdriver.Graphics) error {
|
2022-06-09 19:58:01 +02:00
|
|
|
if !needsRestoring() {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic("restorable: restore cannot be called when restoring is disabled")
|
2017-07-02 08:58:00 +02:00
|
|
|
}
|
2017-09-14 17:24:18 +02:00
|
|
|
|
2020-05-24 17:46:23 +02:00
|
|
|
// Dispose all the shaders ahead of restoring. A current shader ID and a new shader ID can be duplicated.
|
|
|
|
for s := range i.shaders {
|
2021-10-23 16:46:26 +02:00
|
|
|
s.shader.Dispose()
|
2020-05-24 17:46:23 +02:00
|
|
|
s.shader = nil
|
|
|
|
}
|
|
|
|
for s := range i.shaders {
|
|
|
|
s.restore()
|
|
|
|
}
|
|
|
|
|
2019-08-15 17:36:54 +02:00
|
|
|
// Dispose all the images ahead of restoring. A current texture ID and a new texture ID can be duplicated.
|
|
|
|
// TODO: Write a test to confirm that ID duplication never happens.
|
|
|
|
for i := range i.images {
|
2021-10-23 16:46:26 +02:00
|
|
|
i.image.Dispose()
|
2019-08-15 17:36:54 +02:00
|
|
|
i.image = nil
|
|
|
|
}
|
|
|
|
|
2017-06-01 03:44:28 +02:00
|
|
|
// Let's do topological sort based on dependencies of drawing history.
|
2017-09-14 17:24:18 +02:00
|
|
|
// It is assured that there are not loops since cyclic drawing makes images stale.
|
2017-06-02 19:41:37 +02:00
|
|
|
type edge struct {
|
|
|
|
source *Image
|
|
|
|
target *Image
|
|
|
|
}
|
|
|
|
images := map[*Image]struct{}{}
|
|
|
|
for i := range i.images {
|
2023-03-01 07:38:52 +01:00
|
|
|
images[i] = struct{}{}
|
2017-06-02 19:41:37 +02:00
|
|
|
}
|
|
|
|
edges := map[edge]struct{}{}
|
|
|
|
for t := range images {
|
|
|
|
for s := range t.dependingImages() {
|
|
|
|
edges[edge{source: s, target: t}] = struct{}{}
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-20 16:36:37 +01:00
|
|
|
|
2023-03-01 07:38:52 +01:00
|
|
|
var sorted []*Image
|
2017-06-02 19:41:37 +02:00
|
|
|
for len(images) > 0 {
|
2023-01-28 11:06:38 +01:00
|
|
|
// current represents images that have no incoming edges.
|
2017-06-02 19:41:37 +02:00
|
|
|
current := map[*Image]struct{}{}
|
|
|
|
for i := range images {
|
|
|
|
current[i] = struct{}{}
|
|
|
|
}
|
|
|
|
for e := range edges {
|
|
|
|
if _, ok := current[e.target]; ok {
|
|
|
|
delete(current, e.target)
|
2017-06-01 03:44:28 +02:00
|
|
|
}
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
2017-06-02 19:41:37 +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)
|
2017-06-01 03:44:28 +02:00
|
|
|
}
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
2019-01-20 16:36:37 +01:00
|
|
|
|
2017-06-01 03:44:28 +02:00
|
|
|
for _, img := range sorted {
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := img.restore(graphicsDriver); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
return err
|
|
|
|
}
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
2020-05-30 12:43:59 +02:00
|
|
|
|
|
|
|
i.contextLost = false
|
|
|
|
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 20:58:58 +02:00
|
|
|
var graphicsDriverInitialized bool
|
|
|
|
|
2018-10-31 19:02:08 +01:00
|
|
|
// InitializeGraphicsDriverState initializes the graphics driver state.
|
2022-03-19 15:55:14 +01:00
|
|
|
func InitializeGraphicsDriverState(graphicsDriver graphicsdriver.Graphics) error {
|
2021-09-08 20:58:58 +02:00
|
|
|
graphicsDriverInitialized = true
|
2022-03-19 15:55:14 +01:00
|
|
|
needsRestoringByGraphicsDriver = graphicsDriver.NeedsRestoring()
|
|
|
|
return graphicscommand.InitializeGraphicsDriverState(graphicsDriver)
|
2017-08-06 14:24:10 +02:00
|
|
|
}
|
2020-05-30 12:43:59 +02:00
|
|
|
|
2020-10-12 18:36:40 +02:00
|
|
|
// MaxImageSize returns the maximum size of an image.
|
2022-03-19 15:55:14 +01:00
|
|
|
func MaxImageSize(graphicsDriver graphicsdriver.Graphics) int {
|
|
|
|
return graphicscommand.MaxImageSize(graphicsDriver)
|
2020-10-12 18:36:40 +02:00
|
|
|
}
|
|
|
|
|
2020-05-30 12:43:59 +02:00
|
|
|
// OnContextLost is called when the context lost is detected in an explicit way.
|
|
|
|
func OnContextLost() {
|
2022-03-22 18:45:57 +01:00
|
|
|
canDetectContextLostExplicitly = true
|
2020-05-30 12:43:59 +02:00
|
|
|
theImages.contextLost = true
|
|
|
|
}
|
2022-03-22 18:45:57 +01:00
|
|
|
|
2022-08-29 04:16:39 +02:00
|
|
|
// canDetectContextLostExplicitly reports whether Ebitengine can detect a context lost in an explicit way.
|
2022-03-22 18:45:57 +01:00
|
|
|
// On Android, a context lost can be detected via GLSurfaceView.Renderer.onSurfaceCreated.
|
|
|
|
// On iOS w/ OpenGL ES, this can be detected only when gomobile-build is used.
|
|
|
|
var canDetectContextLostExplicitly = runtime.GOOS == "android"
|