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 (
|
2017-05-26 19:37:01 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/sync"
|
2017-05-03 16:11:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type images struct {
|
|
|
|
images map[*Image]struct{}
|
|
|
|
lastChecked *Image
|
2017-05-31 18:27:56 +02:00
|
|
|
m sync.Mutex
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var theImages = &images{
|
|
|
|
images: map[*Image]struct{}{},
|
|
|
|
}
|
|
|
|
|
2017-05-30 19:09:27 +02:00
|
|
|
func ResolveStalePixels() error {
|
|
|
|
return theImages.resolveStalePixels()
|
2017-05-03 17:08:07 +02:00
|
|
|
}
|
|
|
|
|
2017-05-30 19:09:27 +02:00
|
|
|
func Restore() error {
|
|
|
|
return theImages.restore()
|
2017-05-03 17:08:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClearVolatileImages() {
|
|
|
|
theImages.clearVolatileImages()
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 16:15:18 +02:00
|
|
|
func (i *images) add(img *Image) {
|
2017-05-03 16:11:43 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
|
|
|
i.images[img] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2017-05-03 16:24:00 +02:00
|
|
|
func (i *images) remove(img *Image) {
|
2017-05-03 16:11:43 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
|
|
|
delete(i.images, img)
|
|
|
|
}
|
|
|
|
|
2017-05-30 19:09:27 +02:00
|
|
|
func (i *images) resolveStalePixels() error {
|
2017-05-03 16:11:43 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
|
|
|
i.lastChecked = nil
|
|
|
|
for img := range i.images {
|
2017-05-30 19:09:27 +02:00
|
|
|
if err := img.resolveStalePixels(); err != nil {
|
2017-05-03 16:11:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *images) resetPixelsIfDependingOn(target *Image) {
|
2017-05-27 14:35:38 +02:00
|
|
|
// Avoid defer for performance
|
2017-05-03 16:11:43 +02:00
|
|
|
i.m.Lock()
|
|
|
|
if target == nil {
|
|
|
|
// disposed
|
2017-05-27 14:35:38 +02:00
|
|
|
i.m.Unlock()
|
2017-05-03 16:11:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if i.lastChecked == target {
|
2017-05-27 14:35:38 +02:00
|
|
|
i.m.Unlock()
|
2017-05-03 16:11:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
i.lastChecked = target
|
|
|
|
for img := range i.images {
|
2017-05-29 20:31:29 +02:00
|
|
|
// TODO: This seems not enough: What if img becomes stale but what about
|
|
|
|
// other images depend on img? (#357)
|
2017-05-03 16:18:35 +02:00
|
|
|
img.makeStaleIfDependingOn(target)
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
2017-05-27 14:35:38 +02:00
|
|
|
i.m.Unlock()
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
|
|
|
|
2017-05-30 19:09:27 +02:00
|
|
|
func (i *images) restore() error {
|
2017-05-03 16:11:43 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
|
|
|
// Framebuffers/textures cannot be disposed since framebuffers/textures that
|
|
|
|
// don't belong to the current context.
|
2017-06-01 03:44:28 +02:00
|
|
|
|
|
|
|
// Let's do topological sort based on dependencies of drawing history.
|
|
|
|
// There should not be a loop 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 {
|
|
|
|
images[i] = struct{}{}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2017-06-02 19:41:37 +02:00
|
|
|
sorted := []*Image{}
|
|
|
|
for len(images) > 0 {
|
|
|
|
// current repesents images that have no incoming edges.
|
|
|
|
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
|
|
|
}
|
2017-06-01 03:44:28 +02:00
|
|
|
for _, img := range sorted {
|
2017-05-30 19:09:27 +02:00
|
|
|
if err := img.restore(); err != nil {
|
2017-05-03 16:11:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:08:07 +02:00
|
|
|
func (i *images) clearVolatileImages() {
|
2017-05-03 16:11:43 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
|
|
|
for img := range i.images {
|
2017-05-03 16:18:35 +02:00
|
|
|
img.clearIfVolatile()
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
|
|
|
}
|