ebiten/internal/restorable/image.go

377 lines
9.8 KiB
Go
Raw Normal View History

2016-07-16 21:41:28 +02:00
// Copyright 2016 The Ebiten Authors
2016-07-13 19:35:20 +02:00
//
// 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.
2016-09-03 15:05:05 +02:00
package restorable
2016-07-13 19:35:20 +02:00
import (
"errors"
2016-07-13 19:35:20 +02:00
"image/color"
"math"
2017-05-03 16:24:00 +02:00
"runtime"
2016-07-13 19:35:20 +02:00
"github.com/hajimehoshi/ebiten/internal/affine"
2016-07-13 19:35:20 +02:00
"github.com/hajimehoshi/ebiten/internal/graphics"
emath "github.com/hajimehoshi/ebiten/internal/math"
"github.com/hajimehoshi/ebiten/internal/opengl"
2016-07-13 19:35:20 +02:00
)
2017-09-14 17:24:18 +02:00
// MaxImageSize represents the maximum width/height of an image.
const MaxImageSize = graphics.MaxImageSize
2017-09-14 17:24:18 +02:00
// drawImageHistoryItem is an item for history of draw-image commands.
2016-07-13 19:35:20 +02:00
type drawImageHistoryItem struct {
image *Image
vertices []float32
colorm *affine.ColorM
2016-07-13 19:35:20 +02:00
mode opengl.CompositeMode
filter graphics.Filter
2016-07-13 19:35:20 +02:00
}
2017-09-14 17:24:18 +02:00
// canMerge returns a boolean value indicating whether the drawImageHistoryItem d
// can be merged with the given conditions.
func (d *drawImageHistoryItem) canMerge(image *Image, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) bool {
if d.image != image {
return false
}
if !d.colorm.Equals(colorm) {
return false
}
if d.mode != mode {
return false
}
if d.filter != filter {
return false
}
return true
}
2016-09-09 18:36:56 +02:00
// Image represents an image that can be restored when GL context is lost.
2016-09-03 15:05:05 +02:00
type Image struct {
image *graphics.Image
2018-01-28 14:40:36 +01:00
basePixels []byte
2017-09-19 18:35:56 +02:00
// drawImageHistory is a set of draw-image commands.
2017-09-21 16:33:27 +02:00
// TODO: This should be merged with the similar command queue in package graphics (#433).
2016-07-13 19:35:20 +02:00
drawImageHistory []*drawImageHistoryItem
2017-05-29 20:31:29 +02:00
// stale indicates whether the image needs to be synced with GPU as soon as possible.
stale bool
// volatile indicates whether the image is cleared whenever a frame starts.
volatile bool
2017-05-29 19:06:45 +02:00
// screen indicates whether the image is used as an actual screen.
screen bool
paddingX0 float64
paddingY0 float64
paddingX1 float64
paddingY1 float64
2016-07-13 19:35:20 +02:00
}
// NewImage creates an empty image with the given size.
func NewImage(width, height int, volatile bool) *Image {
2017-05-03 16:15:18 +02:00
i := &Image{
image: graphics.NewImage(width, height),
volatile: volatile,
}
2017-05-03 16:15:18 +02:00
theImages.add(i)
2017-05-03 16:24:00 +02:00
runtime.SetFinalizer(i, (*Image).Dispose)
2017-05-03 16:15:18 +02:00
return i
}
2017-09-14 17:24:18 +02:00
// NewScreenFramebufferImage creates a special image that framebuffer is one for the screen.
func NewScreenFramebufferImage(width, height int, paddingX0, paddingY0, paddingX1, paddingY1 float64) *Image {
2017-05-03 16:15:18 +02:00
i := &Image{
image: graphics.NewScreenFramebufferImage(width, height),
volatile: true,
screen: true,
paddingX0: paddingX0,
paddingY0: paddingY0,
paddingX1: paddingX1,
paddingY1: paddingY1,
}
2017-05-03 16:15:18 +02:00
theImages.add(i)
2017-05-03 16:24:00 +02:00
runtime.SetFinalizer(i, (*Image).Dispose)
2017-05-03 16:15:18 +02:00
return i
2016-09-03 15:24:37 +02:00
}
2017-09-14 17:24:18 +02:00
// BasePixelsForTesting returns the image's basePixels for testing.
2018-01-28 14:40:36 +01:00
func (i *Image) BasePixelsForTesting() []byte {
2017-09-14 17:57:52 +02:00
return i.basePixels
2017-05-31 18:27:56 +02:00
}
2017-09-14 17:24:18 +02:00
// Size returns the image's size.
2017-09-14 17:57:52 +02:00
func (i *Image) Size() (int, int) {
return i.image.Size()
}
2016-09-03 15:24:37 +02:00
2017-09-14 17:24:18 +02:00
// makeStale makes the image stale.
2017-09-14 17:57:52 +02:00
func (i *Image) makeStale() {
i.basePixels = nil
i.drawImageHistory = nil
i.stale = true
2016-07-26 19:16:31 +02:00
}
var (
dummyImage = graphics.NewImage(16, 16)
clearColorM *affine.ColorM
)
func init() {
clearColorM = clearColorM.Scale(0, 0, 0, 0)
}
2017-09-14 17:24:18 +02:00
// clearIfVolatile clears the image if the image is volatile.
2017-09-14 17:57:52 +02:00
func (i *Image) clearIfVolatile() {
if !i.volatile {
return
}
2017-09-14 17:57:52 +02:00
i.basePixels = nil
i.drawImageHistory = nil
i.stale = false
if i.image == nil {
2017-05-31 18:27:56 +02:00
panic("not reached")
2016-09-03 16:42:44 +02:00
}
w, h := i.image.Size()
x0 := float32(0)
y0 := float32(0)
x1 := float32(w + int(math.Ceil(i.paddingX0+i.paddingX1)))
y1 := float32(h + int(math.Ceil(i.paddingY0+i.paddingY1)))
// For the rule of values, see vertices.go.
clearVertices := []float32{
x0, y0, 0, 0, 1, 1,
x1, y0, 1, 0, 0, 1,
x0, y1, 0, 1, 1, 0,
x1, y1, 1, 1, 0, 0,
}
i.image.DrawImage(dummyImage, clearVertices, clearColorM, opengl.CompositeModeCopy, graphics.FilterNearest)
2016-07-13 19:35:20 +02:00
}
2017-09-14 17:24:18 +02:00
// ReplacePixels replaces the image pixels with the given pixels slice.
2018-01-28 14:40:36 +01:00
func (i *Image) ReplacePixels(pixels []byte) {
2017-09-14 17:57:52 +02:00
theImages.makeStaleIfDependingOn(i)
i.image.ReplacePixels(pixels)
i.basePixels = pixels
i.drawImageHistory = nil
i.stale = false
2016-07-26 03:51:48 +02:00
}
2017-09-14 17:24:18 +02:00
// DrawImage draws a given image img to the image.
func (i *Image) DrawImage(img *Image, sx0, sy0, sx1, sy1 int, geom *affine.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
vs := img.vertices(sx0, sy0, sx1, sy1, geom)
if vs == nil {
return
}
theImages.makeStaleIfDependingOn(i)
if img.stale || img.volatile || !IsRestoringEnabled() {
2017-09-14 17:57:52 +02:00
i.makeStale()
2016-09-03 16:42:44 +02:00
} else {
i.appendDrawImageHistory(img, vs, colorm, mode, filter)
2016-09-03 16:42:44 +02:00
}
i.image.DrawImage(img.image, vs, colorm, mode, filter)
2016-09-03 16:42:44 +02:00
}
2017-09-14 17:24:18 +02:00
// appendDrawImageHistory appends a draw-image history item to the image.
func (i *Image) appendDrawImageHistory(image *Image, vertices []float32, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
2017-09-14 17:57:52 +02:00
if i.stale || i.volatile {
2016-07-26 19:16:31 +02:00
return
}
2017-09-14 17:57:52 +02:00
if len(i.drawImageHistory) > 0 {
last := i.drawImageHistory[len(i.drawImageHistory)-1]
if last.canMerge(image, colorm, mode, filter) {
last.vertices = append(last.vertices, vertices...)
return
}
}
const maxDrawImageHistoryNum = 100
2017-09-14 17:57:52 +02:00
if len(i.drawImageHistory)+1 > maxDrawImageHistoryNum {
i.makeStale()
return
}
2016-08-22 17:05:23 +02:00
// All images must be resolved and not stale each after frame.
// So we don't have to care if image is stale or not here.
2016-07-24 19:28:59 +02:00
item := &drawImageHistoryItem{
image: image,
vertices: vertices,
colorm: colorm,
2016-07-24 19:28:59 +02:00
mode: mode,
filter: filter,
2016-07-24 19:28:59 +02:00
}
2017-09-14 17:57:52 +02:00
i.drawImageHistory = append(i.drawImageHistory, item)
2016-07-13 19:35:20 +02:00
}
2016-12-26 19:18:56 +01:00
// At returns a color value at (x, y).
2016-07-25 03:32:16 +02:00
//
// Note that this must not be called until context is available.
2017-09-14 17:57:52 +02:00
func (i *Image) At(x, y int) (color.RGBA, error) {
w, h := i.image.Size()
w2, h2 := emath.NextPowerOf2Int(w), emath.NextPowerOf2Int(h)
if x < 0 || y < 0 || w2 <= x || h2 <= y {
return color.RGBA{}, nil
}
2017-09-14 17:57:52 +02:00
if i.basePixels == nil || i.drawImageHistory != nil || i.stale {
if err := i.readPixelsFromGPU(i.image); err != nil {
return color.RGBA{}, err
2016-07-13 19:35:20 +02:00
}
}
idx := 4*x + 4*y*w2
2017-09-14 17:57:52 +02:00
r, g, b, a := i.basePixels[idx], i.basePixels[idx+1], i.basePixels[idx+2], i.basePixels[idx+3]
2016-07-13 19:35:20 +02:00
return color.RGBA{r, g, b, a}, nil
}
2017-09-14 17:24:18 +02:00
// makeStaleIfDependingOn makes the image stale if the image depends on target.
2017-09-14 17:57:52 +02:00
func (i *Image) makeStaleIfDependingOn(target *Image) {
if i.stale {
return
2016-07-26 19:16:31 +02:00
}
2017-09-14 17:57:52 +02:00
if i.dependsOn(target) {
i.makeStale()
2016-07-13 19:35:20 +02:00
}
}
2017-09-14 17:24:18 +02:00
// readPixelsFromGPU reads the pixels from GPU and resolves the image's 'stale' state.
2017-09-14 17:57:52 +02:00
func (i *Image) readPixelsFromGPU(image *graphics.Image) error {
var err error
2017-09-14 17:57:52 +02:00
i.basePixels, err = image.Pixels()
if err != nil {
return err
}
2017-09-14 17:57:52 +02:00
i.drawImageHistory = nil
i.stale = false
return nil
}
2017-09-14 17:24:18 +02:00
// resolveStale resolves the image's 'stale' state.
2017-09-14 17:57:52 +02:00
func (i *Image) resolveStale() error {
if !IsRestoringEnabled() {
return nil
}
2017-09-14 17:57:52 +02:00
if i.volatile {
return nil
}
2017-09-14 17:57:52 +02:00
if !i.stale {
2016-07-26 19:20:42 +02:00
return nil
}
2017-09-14 17:57:52 +02:00
return i.readPixelsFromGPU(i.image)
2016-07-26 19:20:42 +02:00
}
2017-09-14 17:24:18 +02:00
// dependsOn returns a boolean value indicating whether the image depends on target.
2017-09-14 17:57:52 +02:00
func (i *Image) dependsOn(target *Image) bool {
for _, c := range i.drawImageHistory {
if c.image == target {
return true
}
}
return false
}
2017-09-14 17:24:18 +02:00
// dependingImages returns all images that is depended by the image.
2017-09-14 17:57:52 +02:00
func (i *Image) dependingImages() map[*Image]struct{} {
r := map[*Image]struct{}{}
2017-09-14 17:57:52 +02:00
for _, c := range i.drawImageHistory {
r[c.image] = struct{}{}
}
return r
}
2017-09-14 17:24:18 +02:00
// hasDependency returns a boolean value indicating whether the image depends on another image.
2017-09-14 17:57:52 +02:00
func (i *Image) hasDependency() bool {
if i.stale {
2016-07-26 19:16:31 +02:00
return false
}
2017-09-14 17:57:52 +02:00
return len(i.drawImageHistory) > 0
2016-07-13 19:35:20 +02:00
}
2017-05-02 17:10:23 +02:00
// Restore restores *graphics.Image from the pixels using its state.
2017-09-14 17:57:52 +02:00
func (i *Image) restore() error {
w, h := i.image.Size()
if i.screen {
// The screen image should also be recreated because framebuffer might
// be changed.
i.image = graphics.NewScreenFramebufferImage(w, h)
2017-09-14 17:57:52 +02:00
i.basePixels = nil
i.drawImageHistory = nil
i.stale = false
return nil
}
2017-09-14 17:57:52 +02:00
if i.volatile {
i.image = graphics.NewImage(w, h)
2017-09-14 17:57:52 +02:00
i.basePixels = nil
i.drawImageHistory = nil
i.stale = false
return nil
}
2017-09-14 17:57:52 +02:00
if i.stale {
// TODO: panic here?
return errors.New("restorable: pixels must not be stale when restoring")
}
gimg := graphics.NewImage(w, h)
2017-09-14 17:57:52 +02:00
if i.basePixels != nil {
gimg.ReplacePixels(i.basePixels)
} else {
// Clear the image explicitly.
pix := make([]uint8, w*h*4)
gimg.ReplacePixels(pix)
2016-07-13 19:35:20 +02:00
}
2017-09-14 17:57:52 +02:00
for _, c := range i.drawImageHistory {
// All dependencies must be already resolved.
if c.image.hasDependency() {
2017-05-31 18:27:56 +02:00
panic("not reached")
}
gimg.DrawImage(c.image.image, c.vertices, c.colorm, c.mode, c.filter)
2016-07-13 19:35:20 +02:00
}
2017-09-14 17:57:52 +02:00
i.image = gimg
var err error
2017-09-14 17:57:52 +02:00
i.basePixels, err = gimg.Pixels()
2016-07-13 19:35:20 +02:00
if err != nil {
return err
2016-07-13 19:35:20 +02:00
}
2017-09-14 17:57:52 +02:00
i.drawImageHistory = nil
i.stale = false
return nil
}
2017-09-14 17:24:18 +02:00
// Dispose disposes the image.
//
2017-09-14 20:13:36 +02:00
// After disposing, calling the function of the image causes unexpected results.
2017-09-14 17:57:52 +02:00
func (i *Image) Dispose() {
theImages.makeStaleIfDependingOn(i)
i.image.Dispose()
i.image = nil
i.basePixels = nil
i.drawImageHistory = nil
i.stale = false
theImages.remove(i)
runtime.SetFinalizer(i, nil)
2016-07-13 19:35:20 +02:00
}
2016-09-03 17:07:06 +02:00
2017-09-14 17:24:18 +02:00
// IsInvalidated returns a boolean value indicating whether the image is invalidated.
//
// If an image is invalidated, GL context is lost and all the images should be restored asap.
2017-09-14 17:57:52 +02:00
func (i *Image) IsInvalidated() (bool, error) {
2017-08-06 16:00:24 +02:00
// FlushCommands is required because c.offscreen.impl might not have an actual texture.
if err := graphics.FlushCommands(); err != nil {
return false, err
}
if !IsRestoringEnabled() {
2017-08-06 16:00:24 +02:00
return false, nil
}
2017-09-14 17:57:52 +02:00
return i.image.IsInvalidated(), nil
2016-09-03 17:07:06 +02:00
}