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 (
|
2018-04-28 14:49:00 +02:00
|
|
|
"image"
|
|
|
|
|
2018-10-28 12:10:05 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphicscommand"
|
2017-05-03 16:11:43 +02:00
|
|
|
)
|
|
|
|
|
2017-07-02 08:58:00 +02:00
|
|
|
// restoringEnabled indicates if restoring happens or not.
|
2017-09-14 17:24:18 +02:00
|
|
|
//
|
|
|
|
// This value is overridden at enabled_*.go.
|
|
|
|
var restoringEnabled = true
|
2017-07-02 08:58:00 +02:00
|
|
|
|
2017-09-11 20:12:17 +02:00
|
|
|
// IsRestoringEnabled returns a boolean value indicating whether
|
|
|
|
// restoring process works or not.
|
2017-07-02 08:58:00 +02:00
|
|
|
func IsRestoringEnabled() bool {
|
|
|
|
// This value is updated only at init or EnableRestoringForTesting.
|
|
|
|
// No need to lock here.
|
|
|
|
return restoringEnabled
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
restoringEnabled = true
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-09-14 17:24:18 +02:00
|
|
|
images map[*Image]struct{}
|
|
|
|
lastTarget *Image
|
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{
|
|
|
|
images: map[*Image]struct{}{},
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// ResolveStaleImages flushes the queued draw commands and resolves
|
2017-09-11 20:12:17 +02:00
|
|
|
// all stale images.
|
2017-09-14 17:24:18 +02:00
|
|
|
//
|
|
|
|
// ResolveStaleImages is intended to be called at the end of a frame.
|
2018-07-11 19:11:18 +02:00
|
|
|
func ResolveStaleImages() {
|
2018-10-28 12:10:05 +01:00
|
|
|
graphicscommand.FlushCommands()
|
2018-02-22 19:19:20 +01:00
|
|
|
if !restoringEnabled {
|
2018-07-11 19:11:18 +02:00
|
|
|
return
|
2018-02-22 19:19:20 +01:00
|
|
|
}
|
2018-07-11 19:11:18 +02:00
|
|
|
theImages.resolveStaleImages()
|
2017-05-03 17:08:07 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2017-05-30 19:09:27 +02:00
|
|
|
func Restore() error {
|
2018-10-31 19:02:08 +01:00
|
|
|
if err := graphicscommand.ResetGraphicsDriverState(); err != nil {
|
2017-08-06 16:00:24 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-05-30 19:09:27 +02:00
|
|
|
return theImages.restore()
|
2017-05-03 17:08:07 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 19:11:18 +02:00
|
|
|
func Images() []image.Image {
|
2018-04-28 14:49:00 +02:00
|
|
|
var imgs []image.Image
|
|
|
|
for img := range theImages.images {
|
|
|
|
if img.volatile {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if img.screen {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
w, h := img.Size()
|
|
|
|
pix := make([]byte, 4*w*h)
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
2018-07-11 19:11:18 +02:00
|
|
|
c := img.At(i, j)
|
2018-04-28 14:49:00 +02:00
|
|
|
pix[4*(i+j*w)] = byte(c.R)
|
|
|
|
pix[4*(i+j*w)+1] = byte(c.G)
|
|
|
|
pix[4*(i+j*w)+2] = byte(c.B)
|
|
|
|
pix[4*(i+j*w)+3] = byte(c.A)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
imgs = append(imgs, &image.RGBA{
|
|
|
|
Pix: pix,
|
|
|
|
Stride: 4 * w,
|
|
|
|
Rect: image.Rect(0, 0, w, h),
|
|
|
|
})
|
|
|
|
}
|
2018-07-11 19:11:18 +02:00
|
|
|
return imgs
|
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{}{}
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-03-25 11:21:43 +02:00
|
|
|
i.makeStaleIfDependingOnImpl(img)
|
2017-05-03 16:11:43 +02:00
|
|
|
delete(i.images, img)
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// resolveStaleImages resolves stale images.
|
2018-07-11 19:11:18 +02:00
|
|
|
func (i *images) resolveStaleImages() {
|
2017-09-14 17:24:18 +02:00
|
|
|
i.lastTarget = nil
|
2017-05-03 16:11:43 +02:00
|
|
|
for img := range i.images {
|
2018-07-11 19:11:18 +02:00
|
|
|
img.resolveStale()
|
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.
|
|
|
|
//
|
|
|
|
// When target is changed, all images depending on target can't be restored with target.
|
|
|
|
// makeStaleIfDependingOn is called in such situation.
|
|
|
|
func (i *images) makeStaleIfDependingOn(target *Image) {
|
2018-03-25 11:21:43 +02:00
|
|
|
// Avoid defer for performance
|
|
|
|
i.makeStaleIfDependingOnImpl(target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *images) makeStaleIfDependingOnImpl(target *Image) {
|
2017-05-03 16:11:43 +02:00
|
|
|
if target == nil {
|
2018-03-24 13:57:06 +01:00
|
|
|
panic("not reached")
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2017-05-30 19:09:27 +02:00
|
|
|
func (i *images) restore() error {
|
2017-07-02 08:58:00 +02:00
|
|
|
if !IsRestoringEnabled() {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
2017-09-14 17:24:18 +02:00
|
|
|
|
2018-03-24 15:27:08 +01:00
|
|
|
// Dispose image explicitly
|
|
|
|
for img := range i.images {
|
|
|
|
img.image.Dispose()
|
|
|
|
// img.image can't be set nil here, or Size() panics when restoring.
|
|
|
|
}
|
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 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-10-31 19:02:08 +01:00
|
|
|
// InitializeGraphicsDriverState initializes the graphics driver state.
|
|
|
|
func InitializeGraphicsDriverState() error {
|
|
|
|
return graphicscommand.ResetGraphicsDriverState()
|
2017-08-06 14:24:10 +02:00
|
|
|
}
|
2018-07-11 19:11:18 +02:00
|
|
|
|
|
|
|
func Error() error {
|
2018-10-28 12:10:05 +01:00
|
|
|
return graphicscommand.Error()
|
2018-07-11 19:11:18 +02:00
|
|
|
}
|