ebiten/internal/restorable/image.go

424 lines
11 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"
"fmt"
2016-07-13 19:35:20 +02:00
"image/color"
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/graphics"
2018-10-28 12:10:05 +01:00
"github.com/hajimehoshi/ebiten/internal/graphicscommand"
2016-07-13 19:35:20 +02:00
)
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
indices []uint16
colorm *affine.ColorM
2018-10-28 12:42:57 +01:00
mode graphics.CompositeMode
filter graphics.Filter
address graphics.Address
2016-07-13 19:35:20 +02:00
}
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 {
2018-10-28 12:10:05 +01:00
image *graphicscommand.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
w2 int
h2 int
2016-07-13 19:35:20 +02:00
}
var dummyImage *Image
func init() {
dummyImage = &Image{
image: graphicscommand.NewImage(16, 16),
volatile: false,
}
}
// NewImage creates an empty image with the given size.
//
// The returned image is cleared.
//
// Note that Dispose is not called automatically.
func NewImage(width, height int, volatile bool) *Image {
2017-05-03 16:15:18 +02:00
i := &Image{
2018-10-28 12:10:05 +01:00
image: graphicscommand.NewImage(width, height),
volatile: volatile,
}
i.ReplacePixels(nil, 0, 0, width, height)
2017-05-03 16:15:18 +02:00
theImages.add(i)
return i
}
2017-09-14 17:24:18 +02:00
// NewScreenFramebufferImage creates a special image that framebuffer is one for the screen.
//
// The returned image is cleared.
//
// Note that Dispose is not called automatically.
func NewScreenFramebufferImage(width, height int) *Image {
2017-05-03 16:15:18 +02:00
i := &Image{
2018-10-28 12:10:05 +01:00
image: graphicscommand.NewScreenFramebufferImage(width, height),
volatile: false,
screen: true,
}
2017-05-03 16:15:18 +02:00
theImages.add(i)
return i
2016-09-03 15:24:37 +02:00
}
func (i *Image) IsVolatile() bool {
return i.volatile
}
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
}
// Pixels returns the image's pixel bytes.
//
// Pixels tries to read pixels from GPU if needed.
// It is assured that GPU is not accessed if only the image's ReplacePixels is called.
2018-11-12 16:00:10 +01:00
func (i *Image) Pixels() []byte {
i.readPixelsFromGPUIfNeeded()
return i.basePixels
}
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
// SizePowerOf2 returns the next power of 2 values for the size.
func (i *Image) SizePowerOf2() (int, int) {
if i.w2 == 0 || i.h2 == 0 {
w, h := i.image.Size()
2018-11-17 12:20:46 +01:00
i.w2 = graphics.NextPowerOf2Int(w)
i.h2 = graphics.NextPowerOf2Int(h)
}
return i.w2, i.h2
}
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
2018-03-25 12:42:44 +02:00
// Don't have to call makeStale recursively here.
// Restoring is done after topological sorting is done.
// If an image depends on another stale image, this means that
// the former image can be restored from the latest state of the latter image.
2016-07-26 19:16:31 +02:00
}
2017-09-14 17:24:18 +02:00
// ReplacePixels replaces the image pixels with the given pixels slice.
//
// If pixels is nil, ReplacePixels clears the specified reagion.
func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
w, h := i.image.Size()
if width <= 0 || height <= 0 {
panic("restorable: width/height must be positive")
}
if x < 0 || y < 0 || w <= x || h <= y || x+width <= 0 || y+height <= 0 || w < x+width || h < y+height {
panic(fmt.Sprintf("restorable: out of range x: %d, y: %d, width: %d, height: %d", x, y, width, height))
}
2018-02-28 18:49:45 +01:00
// TODO: Avoid making other images stale if possible. (#514)
2018-02-28 18:49:45 +01:00
// For this purpuse, images should remember which part of that is used for DrawImage.
2017-09-14 17:57:52 +02:00
theImages.makeStaleIfDependingOn(i)
2018-02-28 18:49:45 +01:00
if pixels != nil {
i.image.ReplacePixels(pixels, x, y, width, height)
} else {
// There are not 'drawImageHistoryItem's for this image and dummyImage.
// This means dummyImage might not be restored yet when this image is restored.
// However, that's ok since this image will be stale or have its updated pixel data soon,
// and this image can be restored without dummyImage.
//
// dummyImage should be restored later anyway.
dw, dh := dummyImage.Size()
w2 := graphics.NextPowerOf2Int(w)
h2 := graphics.NextPowerOf2Int(h)
vs := graphics.QuadVertices(w2, h2, 0, 0, dw, dh,
float32(width)/float32(dw), 0, 0, float32(height)/float32(dh),
float32(x), float32(y),
1, 1, 1, 1)
2018-10-28 15:03:06 +01:00
is := graphics.QuadIndices()
i.image.DrawImage(dummyImage.image, vs, is, nil, graphics.CompositeModeClear, graphics.FilterNearest, graphics.AddressClampToZero)
}
if x == 0 && y == 0 && width == w && height == h {
if pixels != nil {
if i.basePixels == nil {
i.basePixels = make([]byte, 4*w*h)
}
copy(i.basePixels, pixels)
} else {
// If basePixels is nil, the restored pixels are cleared.
// See restore() implementation.
i.basePixels = nil
}
i.drawImageHistory = nil
i.stale = false
return
}
if len(i.drawImageHistory) > 0 {
i.makeStale()
return
}
if i.stale {
return
}
if i.basePixels == nil {
i.basePixels = make([]byte, 4*w*h)
}
idx := 4 * (y*w + x)
if pixels != nil {
for j := 0; j < height; j++ {
copy(i.basePixels[idx:idx+4*width], pixels[4*j*width:4*(j+1)*width])
idx += 4 * w
}
} else {
zeros := make([]byte, 4*width)
for j := 0; j < height; j++ {
copy(i.basePixels[idx:idx+4*width], zeros)
idx += 4 * w
}
}
2017-09-14 17:57:52 +02:00
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, vertices []float32, indices []uint16, colorm *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter, address graphics.Address) {
if len(vertices) == 0 {
return
}
theImages.makeStaleIfDependingOn(i)
if img.stale || img.volatile || i.screen || !IsRestoringEnabled() {
2017-09-14 17:57:52 +02:00
i.makeStale()
2016-09-03 16:42:44 +02:00
} else {
i.appendDrawImageHistory(img, vertices, indices, colorm, mode, filter, address)
2016-09-03 16:42:44 +02:00
}
i.image.DrawImage(img.image, vertices, indices, colorm, mode, filter, address)
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, indices []uint16, colorm *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter, address graphics.Address) {
if i.stale || i.volatile || i.screen {
2016-07-26 19:16:31 +02:00
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,
indices: indices,
colorm: colorm,
2016-07-24 19:28:59 +02:00
mode: mode,
filter: filter,
address: address,
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
}
2018-11-12 16:00:10 +01:00
func (i *Image) readPixelsFromGPUIfNeeded() {
if i.basePixels == nil || i.drawImageHistory != nil || i.stale {
graphicscommand.FlushCommands()
i.readPixelsFromGPU()
i.drawImageHistory = nil
i.stale = false
}
}
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.
func (i *Image) At(x, y int) color.RGBA {
2017-09-14 17:57:52 +02:00
w, h := i.image.Size()
if x < 0 || y < 0 || w <= x || h <= y {
return color.RGBA{}
}
2018-11-12 16:00:10 +01:00
i.readPixelsFromGPUIfNeeded()
// Even after readPixelsFromGPU, basePixels might be nil when OpenGL error happens.
if i.basePixels == nil {
return color.RGBA{}
}
idx := 4*x + 4*y*w
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]
return color.RGBA{r, g, b, a}
2016-07-13 19:35:20 +02:00
}
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.
func (i *Image) readPixelsFromGPU() {
i.basePixels = i.image.Pixels()
2017-09-14 17:57:52 +02:00
i.drawImageHistory = nil
i.stale = false
}
2017-09-14 17:24:18 +02:00
// resolveStale resolves the image's 'stale' state.
func (i *Image) resolveStale() {
if !IsRestoringEnabled() {
return
}
2017-09-14 17:57:52 +02:00
if i.volatile {
return
}
if i.screen {
return
}
2017-09-14 17:57:52 +02:00
if !i.stale {
return
2016-07-26 19:20:42 +02:00
}
i.readPixelsFromGPU()
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
}
2018-10-28 12:10:05 +01:00
// Restore restores *graphicscommand.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.
2018-10-28 12:10:05 +01:00
i.image = graphicscommand.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 {
2018-10-28 12:10:05 +01:00
i.image = graphicscommand.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")
}
2018-10-28 12:10:05 +01:00
gimg := graphicscommand.NewImage(w, h)
2017-09-14 17:57:52 +02:00
if i.basePixels != nil {
gimg.ReplacePixels(i.basePixels, 0, 0, w, h)
} else {
// Clear the image explicitly.
pix := make([]uint8, w*h*4)
gimg.ReplacePixels(pix, 0, 0, w, h)
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.indices, c.colorm, c.mode, c.filter, c.address)
2016-07-13 19:35:20 +02:00
}
2017-09-14 17:57:52 +02:00
i.image = gimg
i.basePixels = gimg.Pixels()
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.remove(i)
2017-09-14 17:57:52 +02:00
i.image.Dispose()
i.image = nil
i.basePixels = nil
i.drawImageHistory = nil
i.stale = false
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.
2018-10-28 12:10:05 +01:00
graphicscommand.FlushCommands()
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
}