ebiten/internal/restorable/images.go

116 lines
2.6 KiB
Go
Raw Normal View History

// 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 (
"runtime"
"sync"
"github.com/hajimehoshi/ebiten/internal/opengl"
)
type images struct {
images map[*Image]struct{}
m sync.Mutex
lastChecked *Image
}
var theImages = &images{
images: map[*Image]struct{}{},
}
func Images() *images {
return theImages
}
func (i *images) Add(img *Image) {
i.m.Lock()
defer i.m.Unlock()
i.images[img] = struct{}{}
runtime.SetFinalizer(img, theImages.Remove)
}
func (i *images) Remove(img *Image) {
img.dispose()
i.m.Lock()
defer i.m.Unlock()
delete(i.images, img)
runtime.SetFinalizer(img, nil)
}
func (i *images) ResolveStalePixels(context *opengl.Context) error {
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 {
img.MakeStaleIfDependingOn(target)
}
}
func (i *images) Restore(context *opengl.Context) error {
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 {
if err := img.Restore(context); err != nil {
return err
}
}
for _, img := range imagesWithDependency {
if err := img.Restore(context); err != nil {
return err
}
}
return nil
}
func (i *images) ClearVolatileImages() {
i.m.Lock()
defer i.m.Unlock()
for img := range i.images {
img.ClearIfVolatile()
}
}