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 (
|
2016-07-26 19:25:08 +02:00
|
|
|
"errors"
|
2018-02-28 16:27:55 +01:00
|
|
|
"fmt"
|
2019-02-12 15:31:52 +01:00
|
|
|
"image/color"
|
2016-07-13 19:35:20 +02:00
|
|
|
|
2016-10-31 16:20:27 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/affine"
|
2019-06-25 17:43:09 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
2018-10-28 12:25:52 +01:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2019-02-12 15:31:52 +01:00
|
|
|
type Pixels struct {
|
|
|
|
pixels []byte
|
|
|
|
|
|
|
|
length int
|
|
|
|
|
|
|
|
// color is used only when pixels == nil
|
|
|
|
color color.RGBA
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pixels) ensurePixels() {
|
|
|
|
if p.pixels != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.pixels = make([]byte, p.length)
|
|
|
|
if p.color.A == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := 0; i < p.length/4; i++ {
|
|
|
|
p.pixels[4*i] = p.color.R
|
|
|
|
p.pixels[4*i+1] = p.color.G
|
|
|
|
p.pixels[4*i+2] = p.color.B
|
|
|
|
p.pixels[4*i+3] = p.color.A
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pixels) CopyFrom(pix []byte, from int) {
|
|
|
|
p.ensurePixels()
|
|
|
|
copy(p.pixels[from:from+len(pix)], pix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pixels) At(i int) byte {
|
|
|
|
if i < 0 || p.length <= i {
|
|
|
|
panic(fmt.Sprintf("restorable: index out of range: %d for length: %d", i, p.length))
|
|
|
|
}
|
|
|
|
if p.pixels == nil {
|
|
|
|
switch i % 4 {
|
|
|
|
case 0:
|
|
|
|
return p.color.R
|
|
|
|
case 1:
|
|
|
|
return p.color.G
|
|
|
|
case 2:
|
|
|
|
return p.color.B
|
|
|
|
case 3:
|
|
|
|
return p.color.A
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p.pixels[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pixels) Slice() []byte {
|
|
|
|
p.ensurePixels()
|
|
|
|
return p.pixels
|
|
|
|
}
|
|
|
|
|
2019-04-22 16:12:03 +02:00
|
|
|
// drawTrianglesHistoryItem is an item for history of draw-image commands.
|
|
|
|
type drawTrianglesHistoryItem struct {
|
2017-01-20 20:24:39 +01:00
|
|
|
image *Image
|
2018-06-10 13:06:17 +02:00
|
|
|
vertices []float32
|
2018-06-10 13:17:48 +02:00
|
|
|
indices []uint16
|
2018-08-05 14:30:06 +02:00
|
|
|
colorm *affine.ColorM
|
2019-06-25 17:43:09 +02:00
|
|
|
mode driver.CompositeMode
|
|
|
|
filter driver.Filter
|
|
|
|
address driver.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
|
2016-09-03 16:08:51 +02:00
|
|
|
|
2019-02-12 15:31:52 +01:00
|
|
|
basePixels *Pixels
|
2017-09-19 18:35:56 +02:00
|
|
|
|
2019-04-22 16:12:03 +02:00
|
|
|
// drawTrianglesHistory 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).
|
2019-04-22 16:12:03 +02:00
|
|
|
drawTrianglesHistory []*drawTrianglesHistoryItem
|
2016-09-03 19:06:54 +02:00
|
|
|
|
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.
|
2016-09-03 19:06:54 +02:00
|
|
|
volatile bool
|
2017-05-29 19:06:45 +02:00
|
|
|
|
|
|
|
// screen indicates whether the image is used as an actual screen.
|
|
|
|
screen bool
|
2018-06-17 16:05:10 +02:00
|
|
|
|
2019-01-20 16:36:37 +01:00
|
|
|
// priority indicates whether the image is restored in high priority when context-lost happens.
|
|
|
|
priority bool
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
|
2019-02-12 14:50:35 +01:00
|
|
|
var emptyImage *Image
|
2018-12-16 14:24:19 +01:00
|
|
|
|
|
|
|
func init() {
|
2019-02-12 14:50:35 +01:00
|
|
|
const w, h = 16, 16
|
|
|
|
emptyImage = &Image{
|
|
|
|
image: graphicscommand.NewImage(w, h),
|
2019-01-20 16:36:37 +01:00
|
|
|
priority: true,
|
2018-12-16 14:24:19 +01:00
|
|
|
}
|
2019-02-12 14:50:35 +01:00
|
|
|
pix := make([]byte, 4*w*h)
|
|
|
|
for i := range pix {
|
|
|
|
pix[i] = 0xff
|
|
|
|
}
|
|
|
|
emptyImage.ReplacePixels(pix, 0, 0, w, h)
|
|
|
|
theImages.add(emptyImage)
|
2018-12-16 14:24:19 +01:00
|
|
|
}
|
2018-04-05 04:15:24 +02:00
|
|
|
|
2018-12-01 20:28:38 +01:00
|
|
|
// NewImage creates an empty image with the given size.
|
|
|
|
//
|
|
|
|
// The returned image is cleared.
|
2018-04-27 05:08:59 +02:00
|
|
|
//
|
|
|
|
// Note that Dispose is not called automatically.
|
2019-02-12 07:05:05 +01:00
|
|
|
func NewImage(width, height int) *Image {
|
2017-05-03 16:15:18 +02:00
|
|
|
i := &Image{
|
2019-02-12 07:05:05 +01:00
|
|
|
image: graphicscommand.NewImage(width, height),
|
2017-03-03 15:51:25 +01:00
|
|
|
}
|
2019-05-26 16:13:54 +02:00
|
|
|
i.clearForInitialization()
|
2017-05-03 16:15:18 +02:00
|
|
|
theImages.add(i)
|
2018-04-05 17:36:15 +02:00
|
|
|
return i
|
|
|
|
}
|
2018-04-05 04:15:24 +02:00
|
|
|
|
2019-07-14 20:24:18 +02:00
|
|
|
// NewImageFromImage creates a new image and copies the pixels of the given source image.
|
2019-07-14 19:00:26 +02:00
|
|
|
//
|
|
|
|
// The given size (width and height) doesn't have to match with the source image's size.
|
|
|
|
// The image is copied at the left-upper corner of the new image.
|
|
|
|
func NewImageFromImage(width, height int, src *Image) *Image {
|
|
|
|
i := NewImage(width, height)
|
|
|
|
|
|
|
|
// Do not use DrawTriangles here. ReplacePixels will be called on a part of newImg later, and it looked like
|
|
|
|
// ReplacePixels on a part of image deletes other region that are rendered by DrawTriangles (#593, #758).
|
|
|
|
i.image.CopyPixels(src.image)
|
|
|
|
|
|
|
|
// As pixels should not be obtained here, making the image stale is inevitable.
|
|
|
|
// TODO: Copy pixel data from the source instead of making this stale (#897).
|
|
|
|
i.makeStale()
|
|
|
|
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2019-02-12 07:05:05 +01:00
|
|
|
func (i *Image) MakeVolatile() {
|
|
|
|
i.volatile = true
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// NewScreenFramebufferImage creates a special image that framebuffer is one for the screen.
|
2018-04-05 17:36:15 +02:00
|
|
|
//
|
|
|
|
// The returned image is cleared.
|
2018-04-27 05:08:59 +02:00
|
|
|
//
|
|
|
|
// Note that Dispose is not called automatically.
|
2018-02-28 14:46:57 +01:00
|
|
|
func NewScreenFramebufferImage(width, height int) *Image {
|
2017-05-03 16:15:18 +02:00
|
|
|
i := &Image{
|
2018-12-26 19:11:56 +01:00
|
|
|
image: graphicscommand.NewScreenFramebufferImage(width, height),
|
|
|
|
screen: true,
|
2017-03-03 15:51:25 +01:00
|
|
|
}
|
2019-05-26 16:13:54 +02:00
|
|
|
i.clearForInitialization()
|
2017-05-03 16:15:18 +02:00
|
|
|
theImages.add(i)
|
|
|
|
return i
|
2016-09-03 15:24:37 +02:00
|
|
|
}
|
2016-09-03 18:25:02 +02:00
|
|
|
|
2019-07-14 21:14:57 +02:00
|
|
|
func (i *Image) Fill(r, g, b, a byte) {
|
2019-02-12 08:13:33 +01:00
|
|
|
theImages.makeStaleIfDependingOn(i)
|
2019-02-12 14:50:35 +01:00
|
|
|
i.fill(r, g, b, a)
|
2019-02-12 08:13:33 +01:00
|
|
|
}
|
|
|
|
|
2019-05-26 16:13:54 +02:00
|
|
|
// clearForInitialization clears the underlying image for initialization.
|
|
|
|
func (i *Image) clearForInitialization() {
|
|
|
|
// As this is for initialization, drawing history doesn't have to be adjusted.
|
2019-02-12 14:50:35 +01:00
|
|
|
i.fill(0, 0, 0, 0)
|
|
|
|
}
|
|
|
|
|
2019-07-14 21:14:57 +02:00
|
|
|
func (i *Image) fill(r, g, b, a byte) {
|
2019-01-20 18:17:09 +01:00
|
|
|
if i.priority {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic("restorable: clear cannot be called on a priority image")
|
2019-01-20 18:17:09 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 14:50:35 +01:00
|
|
|
rf := float32(0)
|
|
|
|
gf := float32(0)
|
|
|
|
bf := float32(0)
|
|
|
|
af := float32(0)
|
|
|
|
if a > 0 {
|
|
|
|
rf = float32(r) / float32(a)
|
|
|
|
gf = float32(g) / float32(a)
|
|
|
|
bf = float32(b) / float32(a)
|
|
|
|
af = float32(a) / 0xff
|
|
|
|
}
|
|
|
|
|
2019-04-22 16:12:03 +02:00
|
|
|
// There are not 'drawTrianglesHistoryItem's for this image and emptyImage.
|
2019-02-12 14:50:35 +01:00
|
|
|
// As emptyImage is a priority image, this is restored before other regular images are restored.
|
2019-06-25 16:41:36 +02:00
|
|
|
|
|
|
|
// The rendering target size needs to be its 'internal' size instead of the exposed size to avoid glitches on
|
|
|
|
// mobile platforms (See the change 1e1f309a).
|
2019-02-14 15:44:02 +01:00
|
|
|
dw, dh := i.internalSize()
|
2019-02-12 14:50:35 +01:00
|
|
|
sw, sh := emptyImage.Size()
|
2019-06-21 17:00:37 +02:00
|
|
|
vs := make([]float32, 4*graphics.VertexFloatNum)
|
2019-07-14 20:09:00 +02:00
|
|
|
graphics.PutQuadVertices(vs, emptyImage, 0, 0, sw, sh,
|
2019-02-12 14:50:35 +01:00
|
|
|
float32(dw)/float32(sw), 0, 0, float32(dh)/float32(sh), 0, 0,
|
|
|
|
rf, gf, bf, af)
|
2019-01-13 14:29:00 +01:00
|
|
|
is := graphics.QuadIndices()
|
2019-06-25 17:43:09 +02:00
|
|
|
c := driver.CompositeModeCopy
|
2019-02-12 14:50:35 +01:00
|
|
|
if a == 0 {
|
2019-06-25 17:43:09 +02:00
|
|
|
c = driver.CompositeModeClear
|
2019-02-12 14:50:35 +01:00
|
|
|
}
|
2019-06-25 17:43:09 +02:00
|
|
|
i.image.DrawTriangles(emptyImage.image, vs, is, nil, c, driver.FilterNearest, driver.AddressClampToZero)
|
2019-01-13 14:29:00 +01:00
|
|
|
|
2019-02-14 14:39:19 +01:00
|
|
|
w, h := i.Size()
|
2019-02-12 15:31:52 +01:00
|
|
|
i.basePixels = &Pixels{
|
|
|
|
color: color.RGBA{r, g, b, a},
|
|
|
|
length: 4 * w * h,
|
2019-02-12 14:50:35 +01:00
|
|
|
}
|
2019-04-22 16:12:03 +02:00
|
|
|
i.drawTrianglesHistory = nil
|
2019-01-13 14:29:00 +01:00
|
|
|
i.stale = false
|
|
|
|
}
|
|
|
|
|
2018-07-29 16:54:46 +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.
|
2019-02-12 15:31:52 +01:00
|
|
|
func (i *Image) BasePixelsForTesting() *Pixels {
|
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 18:25:02 +02:00
|
|
|
}
|
2016-09-03 15:24:37 +02:00
|
|
|
|
2019-02-14 15:44:02 +01:00
|
|
|
// internalSize returns the size of the internal texture.
|
|
|
|
func (i *Image) internalSize() (int, int) {
|
2019-06-25 16:18:00 +02:00
|
|
|
return i.image.InternalSize()
|
2018-06-17 16:05:10 +02:00
|
|
|
}
|
|
|
|
|
2019-02-14 16:22:32 +01:00
|
|
|
func (i *Image) PutVertex(vs []float32, dx, dy, sx, sy float32, bx0, by0, bx1, by1 float32, cr, cg, cb, ca float32) {
|
2019-06-21 04:09:48 +02:00
|
|
|
// Specifying a range explicitly here is redundant but this helps optimization
|
|
|
|
// to eliminate boundary checks.
|
|
|
|
//
|
|
|
|
// VertexFloatNum is better than 12 in terms of code maintenanceability, but in GopherJS, optimization
|
|
|
|
// might not work.
|
|
|
|
vs = vs[0:12]
|
|
|
|
|
2019-02-14 15:44:02 +01:00
|
|
|
w, h := i.internalSize()
|
2019-02-14 16:22:32 +01:00
|
|
|
vs[0] = dx
|
|
|
|
vs[1] = dy
|
|
|
|
vs[2] = sx / float32(w)
|
|
|
|
vs[3] = sy / float32(h)
|
|
|
|
vs[4] = bx0 / float32(w)
|
|
|
|
vs[5] = by0 / float32(h)
|
2019-06-21 20:50:56 +02:00
|
|
|
vs[6] = bx1 / float32(w)
|
|
|
|
vs[7] = by1 / float32(h)
|
2019-02-14 16:22:32 +01:00
|
|
|
vs[8] = cr
|
|
|
|
vs[9] = cg
|
|
|
|
vs[10] = cb
|
|
|
|
vs[11] = ca
|
2019-02-14 15:44:02 +01: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
|
2019-04-22 16:12:03 +02:00
|
|
|
i.drawTrianglesHistory = nil
|
2017-09-14 17:57:52 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-09 19:36:59 +02:00
|
|
|
// ClearPixels clears the specified region by ReplacePixels.
|
|
|
|
func (i *Image) ClearPixels(x, y, width, height int) {
|
|
|
|
// TODO: Allocating bytes for all pixels are wasteful. Allocate memory only for required regions (#897).
|
|
|
|
i.ReplacePixels(make([]byte, 4*width*height), x, y, width, height)
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// ReplacePixels replaces the image pixels with the given pixels slice.
|
2018-04-28 15:50:00 +02:00
|
|
|
//
|
|
|
|
// If pixels is nil, ReplacePixels clears the specified reagion.
|
2018-02-28 16:27:55 +01:00
|
|
|
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
|
|
|
|
2018-03-03 10:51:52 +01:00
|
|
|
// TODO: Avoid making other images stale if possible. (#514)
|
2019-04-22 16:12:03 +02:00
|
|
|
// For this purpuse, images should remember which part of that is used for DrawTriangles.
|
2017-09-14 17:57:52 +02:00
|
|
|
theImages.makeStaleIfDependingOn(i)
|
2018-02-28 18:49:45 +01:00
|
|
|
|
2019-01-10 15:22:59 +01:00
|
|
|
if pixels == nil {
|
2019-07-09 19:36:59 +02:00
|
|
|
// TODO: Allocating bytes for all pixels are wasteful. Allocate memory only for required regions
|
|
|
|
// (#897).
|
2019-01-10 15:22:59 +01:00
|
|
|
pixels = make([]byte, 4*width*height)
|
2018-04-28 15:50:00 +02:00
|
|
|
}
|
2019-01-10 15:22:59 +01:00
|
|
|
i.image.ReplacePixels(pixels, x, y, width, height)
|
2018-02-28 16:27:55 +01:00
|
|
|
|
2019-05-26 14:10:25 +02:00
|
|
|
if !needsRestoring() {
|
2019-02-02 18:39:36 +01:00
|
|
|
i.makeStale()
|
|
|
|
return
|
|
|
|
}
|
2019-02-02 11:00:15 +01:00
|
|
|
|
2018-03-25 13:39:06 +02:00
|
|
|
if x == 0 && y == 0 && width == w && height == h {
|
2018-10-30 03:08:38 +01:00
|
|
|
if pixels != nil {
|
|
|
|
if i.basePixels == nil {
|
2019-02-12 15:31:52 +01:00
|
|
|
i.basePixels = &Pixels{
|
|
|
|
length: 4 * w * h,
|
|
|
|
}
|
2018-10-30 03:08:38 +01:00
|
|
|
}
|
2019-02-12 15:31:52 +01:00
|
|
|
i.basePixels.CopyFrom(pixels, 0)
|
2018-10-30 03:08:38 +01:00
|
|
|
} else {
|
|
|
|
// If basePixels is nil, the restored pixels are cleared.
|
|
|
|
// See restore() implementation.
|
|
|
|
i.basePixels = nil
|
2018-03-19 16:37:02 +01:00
|
|
|
}
|
2019-04-22 16:12:03 +02:00
|
|
|
i.drawTrianglesHistory = nil
|
2018-03-25 13:39:06 +02:00
|
|
|
i.stale = false
|
|
|
|
return
|
|
|
|
}
|
2018-12-26 19:06:44 +01:00
|
|
|
|
2019-04-22 16:12:03 +02:00
|
|
|
if len(i.drawTrianglesHistory) > 0 {
|
|
|
|
panic("restorable: ReplacePixels for a part after DrawTriangles is forbidden")
|
2018-03-25 13:39:06 +02:00
|
|
|
}
|
2018-12-26 19:06:44 +01:00
|
|
|
|
2018-12-26 12:10:12 +01:00
|
|
|
if i.stale {
|
2018-03-25 13:39:06 +02:00
|
|
|
return
|
2018-02-28 16:27:55 +01:00
|
|
|
}
|
2018-12-26 19:06:44 +01:00
|
|
|
|
2018-02-28 16:27:55 +01:00
|
|
|
idx := 4 * (y*w + x)
|
2018-04-28 15:50:00 +02:00
|
|
|
if pixels != nil {
|
2018-12-26 19:06:44 +01:00
|
|
|
if i.basePixels == nil {
|
2019-02-12 15:31:52 +01:00
|
|
|
i.basePixels = &Pixels{
|
|
|
|
length: 4 * w * h,
|
|
|
|
}
|
2018-12-26 19:06:44 +01:00
|
|
|
}
|
2018-04-28 15:50:00 +02:00
|
|
|
for j := 0; j < height; j++ {
|
2019-02-12 15:31:52 +01:00
|
|
|
i.basePixels.CopyFrom(pixels[4*j*width:4*(j+1)*width], idx)
|
2018-04-28 15:50:00 +02:00
|
|
|
idx += 4 * w
|
|
|
|
}
|
2018-12-26 19:06:44 +01:00
|
|
|
} else if i.basePixels != nil {
|
2018-04-28 15:50:00 +02:00
|
|
|
zeros := make([]byte, 4*width)
|
|
|
|
for j := 0; j < height; j++ {
|
2019-02-12 15:31:52 +01:00
|
|
|
i.basePixels.CopyFrom(zeros, idx)
|
2018-04-28 15:50:00 +02:00
|
|
|
idx += 4 * w
|
|
|
|
}
|
2018-02-28 16:27:55 +01:00
|
|
|
}
|
2016-07-26 03:51:48 +02:00
|
|
|
}
|
|
|
|
|
2019-04-22 16:12:03 +02:00
|
|
|
// DrawTriangles draws a given image img to the image.
|
2019-06-25 17:43:09 +02:00
|
|
|
func (i *Image) DrawTriangles(img *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode driver.CompositeMode, filter driver.Filter, address driver.Address) {
|
2019-01-20 18:17:09 +01:00
|
|
|
if i.priority {
|
2019-04-22 16:12:03 +02:00
|
|
|
panic("restorable: DrawTriangles cannot be called on a priority image")
|
2019-01-20 18:17:09 +01:00
|
|
|
}
|
2018-06-10 15:59:00 +02:00
|
|
|
if len(vertices) == 0 {
|
2018-02-26 16:05:11 +01:00
|
|
|
return
|
|
|
|
}
|
2018-02-28 07:56:51 +01:00
|
|
|
theImages.makeStaleIfDependingOn(i)
|
2018-03-25 15:31:51 +02:00
|
|
|
|
2019-05-26 14:10:25 +02:00
|
|
|
if img.stale || img.volatile || i.screen || !needsRestoring() || i.volatile {
|
2017-09-14 17:57:52 +02:00
|
|
|
i.makeStale()
|
2016-09-03 16:42:44 +02:00
|
|
|
} else {
|
2019-04-22 16:12:03 +02:00
|
|
|
i.appendDrawTrianglesHistory(img, vertices, indices, colorm, mode, filter, address)
|
2016-09-03 16:42:44 +02:00
|
|
|
}
|
2019-04-22 16:12:03 +02:00
|
|
|
i.image.DrawTriangles(img.image, vertices, indices, colorm, mode, filter, address)
|
2016-09-03 16:42:44 +02:00
|
|
|
}
|
|
|
|
|
2019-04-22 16:12:03 +02:00
|
|
|
// appendDrawTrianglesHistory appends a draw-image history item to the image.
|
2019-06-25 17:43:09 +02:00
|
|
|
func (i *Image) appendDrawTrianglesHistory(image *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode driver.CompositeMode, filter driver.Filter, address driver.Address) {
|
2018-02-28 14:58:23 +01:00
|
|
|
if i.stale || i.volatile || i.screen {
|
2016-07-26 19:16:31 +02:00
|
|
|
return
|
|
|
|
}
|
2019-02-13 02:45:05 +01:00
|
|
|
// TODO: Would it be possible to merge draw image history items?
|
2019-04-22 16:12:03 +02:00
|
|
|
const maxDrawTrianglesHistoryNum = 1024
|
|
|
|
if len(i.drawTrianglesHistory)+1 > maxDrawTrianglesHistoryNum {
|
2017-09-14 17:57:52 +02:00
|
|
|
i.makeStale()
|
2017-05-03 15:15:59 +02:00
|
|
|
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.
|
2019-04-22 16:12:03 +02:00
|
|
|
item := &drawTrianglesHistoryItem{
|
2016-07-24 19:28:59 +02:00
|
|
|
image: image,
|
2018-06-10 13:06:17 +02:00
|
|
|
vertices: vertices,
|
2018-06-10 13:17:48 +02:00
|
|
|
indices: indices,
|
2018-08-05 14:30:06 +02:00
|
|
|
colorm: colorm,
|
2016-07-24 19:28:59 +02:00
|
|
|
mode: mode,
|
2018-02-13 18:02:48 +01:00
|
|
|
filter: filter,
|
2018-12-23 19:00:00 +01:00
|
|
|
address: address,
|
2016-07-24 19:28:59 +02:00
|
|
|
}
|
2019-04-22 16:12:03 +02:00
|
|
|
i.drawTrianglesHistory = append(i.drawTrianglesHistory, item)
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
|
2018-11-12 16:00:10 +01:00
|
|
|
func (i *Image) readPixelsFromGPUIfNeeded() {
|
2019-04-22 16:12:03 +02:00
|
|
|
if i.basePixels == nil || len(i.drawTrianglesHistory) > 0 || i.stale {
|
2018-11-12 16:00:10 +01:00
|
|
|
graphicscommand.FlushCommands()
|
|
|
|
i.readPixelsFromGPU()
|
2019-04-22 16:12:03 +02:00
|
|
|
i.drawTrianglesHistory = nil
|
2018-11-12 16:00:10 +01:00
|
|
|
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.
|
2019-01-13 20:25:39 +01:00
|
|
|
func (i *Image) At(x, y int) (byte, byte, byte, byte) {
|
2017-09-14 17:57:52 +02:00
|
|
|
w, h := i.image.Size()
|
2018-02-28 16:45:37 +01:00
|
|
|
if x < 0 || y < 0 || w <= x || h <= y {
|
2019-01-13 20:25:39 +01:00
|
|
|
return 0, 0, 0, 0
|
2017-02-04 20:24:07 +01:00
|
|
|
}
|
2018-03-25 15:31:51 +02:00
|
|
|
|
2018-11-12 16:00:10 +01:00
|
|
|
i.readPixelsFromGPUIfNeeded()
|
2018-07-11 19:11:18 +02:00
|
|
|
|
|
|
|
// Even after readPixelsFromGPU, basePixels might be nil when OpenGL error happens.
|
|
|
|
if i.basePixels == nil {
|
2019-01-13 20:25:39 +01:00
|
|
|
return 0, 0, 0, 0
|
2018-07-11 19:11:18 +02:00
|
|
|
}
|
|
|
|
|
2018-02-28 16:45:37 +01:00
|
|
|
idx := 4*x + 4*y*w
|
2019-02-12 15:31:52 +01:00
|
|
|
return i.basePixels.At(idx), i.basePixels.At(idx + 1), i.basePixels.At(idx + 2), i.basePixels.At(idx + 3)
|
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 {
|
2016-09-03 16:54:52 +02:00
|
|
|
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.
|
2018-07-11 19:11:18 +02:00
|
|
|
func (i *Image) readPixelsFromGPU() {
|
2019-02-13 16:54:02 +01:00
|
|
|
pix := i.image.Pixels()
|
2019-02-12 15:31:52 +01:00
|
|
|
i.basePixels = &Pixels{
|
2019-02-13 16:54:02 +01:00
|
|
|
pixels: pix,
|
|
|
|
length: len(pix),
|
2019-02-12 15:31:52 +01:00
|
|
|
}
|
2019-04-22 16:12:03 +02:00
|
|
|
i.drawTrianglesHistory = nil
|
2017-09-14 17:57:52 +02:00
|
|
|
i.stale = false
|
2016-07-23 18:28:39 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// resolveStale resolves the image's 'stale' state.
|
2018-07-11 19:11:18 +02:00
|
|
|
func (i *Image) resolveStale() {
|
2019-05-26 14:10:25 +02:00
|
|
|
if !needsRestoring() {
|
2018-07-11 19:11:18 +02:00
|
|
|
return
|
2017-07-02 08:58:00 +02:00
|
|
|
}
|
2018-03-25 15:31:51 +02:00
|
|
|
|
2017-09-14 17:57:52 +02:00
|
|
|
if i.volatile {
|
2018-07-11 19:11:18 +02:00
|
|
|
return
|
2016-09-03 19:06:54 +02:00
|
|
|
}
|
2018-02-28 14:58:23 +01:00
|
|
|
if i.screen {
|
2018-07-11 19:11:18 +02:00
|
|
|
return
|
2018-02-28 14:58:23 +01:00
|
|
|
}
|
2017-09-14 17:57:52 +02:00
|
|
|
if !i.stale {
|
2018-07-11 19:11:18 +02:00
|
|
|
return
|
2016-07-26 19:20:42 +02:00
|
|
|
}
|
2018-07-11 19:11:18 +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 {
|
2019-04-22 16:12:03 +02:00
|
|
|
for _, c := range i.drawTrianglesHistory {
|
2017-06-01 03:44:28 +02:00
|
|
|
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{} {
|
2017-06-02 19:41:37 +02:00
|
|
|
r := map[*Image]struct{}{}
|
2019-04-22 16:12:03 +02:00
|
|
|
for _, c := range i.drawTrianglesHistory {
|
2017-06-02 19:41:37 +02:00
|
|
|
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
|
|
|
|
}
|
2019-04-22 16:12:03 +02:00
|
|
|
return len(i.drawTrianglesHistory) > 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 {
|
2016-09-03 19:31:50 +02:00
|
|
|
// 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
|
2019-04-22 16:12:03 +02:00
|
|
|
i.drawTrianglesHistory = nil
|
2017-09-14 17:57:52 +02:00
|
|
|
i.stale = false
|
2016-09-03 19:31:50 +02:00
|
|
|
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)
|
2019-05-26 16:13:54 +02:00
|
|
|
i.clearForInitialization()
|
2016-09-03 19:06:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-09-14 17:57:52 +02:00
|
|
|
if i.stale {
|
2017-03-03 15:51:25 +01:00
|
|
|
// TODO: panic here?
|
2016-09-03 16:08:51 +02:00
|
|
|
return errors.New("restorable: pixels must not be stale when restoring")
|
2016-07-26 19:25:08 +02:00
|
|
|
}
|
2019-01-13 14:29:00 +01:00
|
|
|
|
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 {
|
2019-02-12 15:31:52 +01:00
|
|
|
gimg.ReplacePixels(i.basePixels.Slice(), 0, 0, w, h)
|
2018-02-25 15:13:06 +01:00
|
|
|
} else {
|
|
|
|
// Clear the image explicitly.
|
2019-07-14 21:14:57 +02:00
|
|
|
pix := make([]byte, w*h*4)
|
2018-02-28 16:27:55 +01:00
|
|
|
gimg.ReplacePixels(pix, 0, 0, w, h)
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
2019-04-22 16:12:03 +02:00
|
|
|
for _, c := range i.drawTrianglesHistory {
|
2017-05-03 16:11:43 +02:00
|
|
|
if c.image.hasDependency() {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic("restorable: all dependencies must be already resolved but not")
|
2017-01-20 20:24:39 +01:00
|
|
|
}
|
2019-04-22 16:12:03 +02:00
|
|
|
gimg.DrawTriangles(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
|
2016-09-03 16:08:51 +02:00
|
|
|
|
2019-02-13 17:00:11 +01:00
|
|
|
pix := gimg.Pixels()
|
2019-02-12 15:31:52 +01:00
|
|
|
i.basePixels = &Pixels{
|
2019-02-13 17:00:11 +01:00
|
|
|
pixels: pix,
|
|
|
|
length: len(pix),
|
2019-02-12 15:31:52 +01:00
|
|
|
}
|
2019-04-22 16:12:03 +02:00
|
|
|
i.drawTrianglesHistory = nil
|
2017-09-14 17:57:52 +02:00
|
|
|
i.stale = false
|
2016-09-03 16:08:51 +02:00
|
|
|
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() {
|
2018-03-25 11:21:43 +02:00
|
|
|
theImages.remove(i)
|
2018-03-25 15:31:51 +02:00
|
|
|
|
2017-09-14 17:57:52 +02:00
|
|
|
i.image.Dispose()
|
|
|
|
i.image = nil
|
|
|
|
i.basePixels = nil
|
2019-04-22 16:12:03 +02:00
|
|
|
i.drawTrianglesHistory = nil
|
2017-09-14 17:57:52 +02:00
|
|
|
i.stale = false
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
2016-09-03 17:07:06 +02:00
|
|
|
|
2019-05-26 14:10:25 +02:00
|
|
|
// isInvalidated returns a boolean value indicating whether the image is invalidated.
|
2017-09-14 17:24:18 +02:00
|
|
|
//
|
|
|
|
// If an image is invalidated, GL context is lost and all the images should be restored asap.
|
2019-05-26 14:10:25 +02:00
|
|
|
func (i *Image) isInvalidated() bool {
|
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()
|
2019-05-26 14:10:25 +02:00
|
|
|
return i.image.IsInvalidated()
|
2016-09-03 17:07:06 +02:00
|
|
|
}
|