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 (
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
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{}
|
|
|
|
m sync.Mutex
|
|
|
|
lastChecked *Image
|
|
|
|
}
|
|
|
|
|
|
|
|
var theImages = &images{
|
|
|
|
images: map[*Image]struct{}{},
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:08:07 +02:00
|
|
|
func ResolveStalePixels(context *opengl.Context) error {
|
|
|
|
return theImages.resolveStalePixels(context)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Restore(context *opengl.Context) error {
|
|
|
|
return theImages.restore(context)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03 17:08:07 +02:00
|
|
|
func (i *images) resolveStalePixels(context *opengl.Context) error {
|
2017-05-03 16:11:43 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
|
|
|
i.lastChecked = nil
|
|
|
|
for img := range i.images {
|
|
|
|
if err := img.resolveStalePixels(context); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *images) resetPixelsIfDependingOn(target *Image) {
|
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
|
|
|
if target == nil {
|
|
|
|
// disposed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if i.lastChecked == target {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i.lastChecked = target
|
|
|
|
for img := range i.images {
|
2017-05-03 16:18:35 +02:00
|
|
|
img.makeStaleIfDependingOn(target)
|
2017-05-03 16:11:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:08:07 +02:00
|
|
|
func (i *images) restore(context *opengl.Context) 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.
|
|
|
|
imagesWithoutDependency := []*Image{}
|
|
|
|
imagesWithDependency := []*Image{}
|
|
|
|
for img := range i.images {
|
|
|
|
if img.hasDependency() {
|
|
|
|
imagesWithDependency = append(imagesWithDependency, img)
|
|
|
|
} else {
|
|
|
|
imagesWithoutDependency = append(imagesWithoutDependency, img)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Images depending on other images should be processed first.
|
|
|
|
for _, img := range imagesWithoutDependency {
|
2017-05-03 16:18:35 +02:00
|
|
|
if err := img.restore(context); err != nil {
|
2017-05-03 16:11:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, img := range imagesWithDependency {
|
2017-05-03 16:18:35 +02:00
|
|
|
if err := img.restore(context); 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
|
|
|
}
|
|
|
|
}
|