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"
|
2016-07-13 19:35:20 +02:00
|
|
|
"image"
|
|
|
|
"image/color"
|
2017-05-03 16:24:00 +02:00
|
|
|
"runtime"
|
2016-07-13 19:35:20 +02:00
|
|
|
|
2016-10-31 16:20:27 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/affine"
|
2016-07-13 19:35:20 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
2017-08-06 13:05:14 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/math"
|
2016-11-03 15:31:25 +01:00
|
|
|
"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.
|
2017-08-06 14:24:10 +02:00
|
|
|
const MaxImageSize = graphics.MaxImageSize
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// QuadVertexSizeInBytes returns the byte size of vertices for a quadrilateral.
|
2017-08-06 14:24:10 +02:00
|
|
|
func QuadVertexSizeInBytes() int {
|
|
|
|
return graphics.QuadVertexSizeInBytes()
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-01-20 20:24:39 +01:00
|
|
|
image *Image
|
2016-11-01 18:06:37 +01:00
|
|
|
vertices []float32
|
2016-10-31 16:20:27 +01:00
|
|
|
colorm affine.ColorM
|
2016-07-13 19:35:20 +02:00
|
|
|
mode opengl.CompositeMode
|
|
|
|
}
|
|
|
|
|
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.
|
2017-07-01 19:21:23 +02:00
|
|
|
func (d *drawImageHistoryItem) canMerge(image *Image, colorm *affine.ColorM, mode opengl.CompositeMode) bool {
|
|
|
|
if d.image != image {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !d.colorm.Equals(colorm) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if d.mode != mode {
|
|
|
|
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 {
|
2016-09-03 17:57:23 +02:00
|
|
|
image *graphics.Image
|
|
|
|
filter opengl.Filter
|
2016-09-03 16:08:51 +02:00
|
|
|
|
2016-09-03 15:05:05 +02:00
|
|
|
// baseImage and baseColor are exclusive.
|
2016-07-13 20:29:19 +02:00
|
|
|
basePixels []uint8
|
2016-08-30 16:31:59 +02:00
|
|
|
baseColor color.RGBA
|
2016-07-13 19:35:20 +02:00
|
|
|
drawImageHistory []*drawImageHistoryItem
|
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
|
2017-06-30 21:12:09 +02:00
|
|
|
|
|
|
|
offsetX float64
|
|
|
|
offsetY float64
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// NewImage creates an empty image with the given size and filter.
|
2017-03-03 15:51:25 +01:00
|
|
|
func NewImage(width, height int, filter opengl.Filter, volatile bool) *Image {
|
2017-05-03 16:15:18 +02:00
|
|
|
i := &Image{
|
2017-03-03 15:51:25 +01:00
|
|
|
image: graphics.NewImage(width, height, filter),
|
2016-09-03 19:06:54 +02:00
|
|
|
filter: filter,
|
|
|
|
volatile: volatile,
|
2017-03-03 15:51:25 +01:00
|
|
|
}
|
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 16:08:51 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// NewImageFromImage creates an image with source image.
|
2017-09-14 17:48:44 +02:00
|
|
|
func NewImageFromImage(source image.Image, filter opengl.Filter) *Image {
|
|
|
|
size := source.Bounds().Size()
|
|
|
|
width, height := size.X, size.Y
|
|
|
|
rgbaImg := CopyImage(source)
|
2017-08-06 13:05:14 +02:00
|
|
|
w2, h2 := math.NextPowerOf2Int(width), math.NextPowerOf2Int(height)
|
2017-02-04 20:16:09 +01:00
|
|
|
p := make([]uint8, 4*w2*h2)
|
2017-02-04 18:19:30 +01:00
|
|
|
for j := 0; j < height; j++ {
|
2017-09-14 17:48:44 +02:00
|
|
|
copy(p[j*w2*4:(j+1)*w2*4], rgbaImg.Pix[j*rgbaImg.Stride:])
|
2017-02-04 18:19:30 +01:00
|
|
|
}
|
2017-05-03 16:15:18 +02:00
|
|
|
i := &Image{
|
2017-09-14 17:48:44 +02:00
|
|
|
image: graphics.NewImageFromImage(rgbaImg, width, height, filter),
|
2017-02-04 18:19:30 +01:00
|
|
|
basePixels: p,
|
|
|
|
filter: filter,
|
2017-03-03 15:51:25 +01:00
|
|
|
}
|
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 16:08:51 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// NewScreenFramebufferImage creates a special image that framebuffer is one for the screen.
|
2017-06-30 21:12:09 +02:00
|
|
|
func NewScreenFramebufferImage(width, height int, offsetX, offsetY float64) *Image {
|
2017-05-03 16:15:18 +02:00
|
|
|
i := &Image{
|
2017-06-30 21:12:09 +02:00
|
|
|
image: graphics.NewScreenFramebufferImage(width, height, offsetX, offsetY),
|
2016-09-03 19:06:54 +02:00
|
|
|
volatile: true,
|
2016-09-03 19:31:50 +02:00
|
|
|
screen: true,
|
2017-06-30 21:12:09 +02:00
|
|
|
offsetX: offsetX,
|
|
|
|
offsetY: offsetY,
|
2017-03-03 15:51:25 +01:00
|
|
|
}
|
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
|
|
|
}
|
2016-09-03 18:25:02 +02:00
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// BasePixelsForTesting returns the image's basePixels for testing.
|
2017-05-31 18:27:56 +02:00
|
|
|
func (p *Image) BasePixelsForTesting() []uint8 {
|
|
|
|
return p.basePixels
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// Size returns the image's size.
|
2016-09-03 18:25:02 +02:00
|
|
|
func (p *Image) Size() (int, int) {
|
2016-12-27 02:58:46 +01:00
|
|
|
return p.image.Size()
|
2016-09-03 18:25:02 +02:00
|
|
|
}
|
2016-09-03 15:24:37 +02:00
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// makeStale makes the image stale.
|
2016-09-03 16:54:52 +02:00
|
|
|
func (p *Image) makeStale() {
|
2016-07-26 19:16:31 +02:00
|
|
|
p.basePixels = nil
|
2016-08-30 16:31:59 +02:00
|
|
|
p.baseColor = color.RGBA{}
|
2016-07-26 19:16:31 +02:00
|
|
|
p.drawImageHistory = nil
|
|
|
|
p.stale = true
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// clearIfVolatile clears the image if the image is volatile.
|
2017-05-03 16:18:35 +02:00
|
|
|
func (p *Image) clearIfVolatile() {
|
2016-09-03 19:06:54 +02:00
|
|
|
if !p.volatile {
|
2017-03-03 15:51:25 +01:00
|
|
|
return
|
2016-09-03 19:06:54 +02:00
|
|
|
}
|
2016-07-13 20:29:19 +02:00
|
|
|
p.basePixels = nil
|
2016-08-30 16:31:59 +02:00
|
|
|
p.baseColor = color.RGBA{}
|
2016-07-13 19:52:24 +02:00
|
|
|
p.drawImageHistory = nil
|
2016-07-26 19:16:31 +02:00
|
|
|
p.stale = false
|
2016-09-03 16:42:44 +02:00
|
|
|
if p.image == nil {
|
2017-05-31 18:27:56 +02:00
|
|
|
panic("not reached")
|
2016-09-03 16:42:44 +02:00
|
|
|
}
|
2017-07-02 14:06:36 +02:00
|
|
|
p.image.Fill(0, 0, 0, 0)
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// Fill fills the image with the given color.
|
2017-07-02 14:06:36 +02:00
|
|
|
func (p *Image) Fill(r, g, b, a uint8) {
|
2017-09-14 17:24:18 +02:00
|
|
|
theImages.makeStaleIfDependingOn(p)
|
2016-07-13 20:29:19 +02:00
|
|
|
p.basePixels = nil
|
2017-07-02 14:06:36 +02:00
|
|
|
p.baseColor = color.RGBA{r, g, b, a}
|
2016-07-13 19:52:24 +02:00
|
|
|
p.drawImageHistory = nil
|
2016-07-26 19:16:31 +02:00
|
|
|
p.stale = false
|
2017-07-02 14:06:36 +02:00
|
|
|
p.image.Fill(r, g, b, a)
|
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.
|
2017-03-03 15:51:25 +01:00
|
|
|
func (p *Image) ReplacePixels(pixels []uint8) {
|
2017-09-14 17:24:18 +02:00
|
|
|
theImages.makeStaleIfDependingOn(p)
|
2017-03-03 15:51:25 +01:00
|
|
|
p.image.ReplacePixels(pixels)
|
2017-02-04 20:16:09 +01:00
|
|
|
p.basePixels = pixels
|
2016-08-30 16:31:59 +02:00
|
|
|
p.baseColor = color.RGBA{}
|
2016-07-26 03:51:48 +02:00
|
|
|
p.drawImageHistory = nil
|
2016-07-26 19:16:31 +02:00
|
|
|
p.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.
|
2017-05-27 09:14:48 +02:00
|
|
|
func (p *Image) DrawImage(img *Image, vertices []float32, colorm *affine.ColorM, mode opengl.CompositeMode) {
|
2017-09-14 17:24:18 +02:00
|
|
|
theImages.makeStaleIfDependingOn(p)
|
2017-07-02 08:58:00 +02:00
|
|
|
if img.stale || img.volatile || !IsRestoringEnabled() {
|
2016-09-03 16:54:52 +02:00
|
|
|
p.makeStale()
|
2016-09-03 16:42:44 +02:00
|
|
|
} else {
|
2017-01-20 20:24:39 +01:00
|
|
|
p.appendDrawImageHistory(img, vertices, colorm, mode)
|
2016-09-03 16:42:44 +02:00
|
|
|
}
|
2017-03-03 15:51:25 +01:00
|
|
|
p.image.DrawImage(img.image, vertices, colorm, mode)
|
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.
|
2017-05-27 09:14:48 +02:00
|
|
|
func (p *Image) appendDrawImageHistory(image *Image, vertices []float32, colorm *affine.ColorM, mode opengl.CompositeMode) {
|
2017-07-01 19:21:23 +02:00
|
|
|
if p.stale || p.volatile {
|
2016-07-26 19:16:31 +02:00
|
|
|
return
|
|
|
|
}
|
2017-07-01 19:21:23 +02:00
|
|
|
if len(p.drawImageHistory) > 0 {
|
|
|
|
last := p.drawImageHistory[len(p.drawImageHistory)-1]
|
|
|
|
if last.canMerge(image, colorm, mode) {
|
|
|
|
last.vertices = append(last.vertices, vertices...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-07-01 18:41:07 +02:00
|
|
|
const maxDrawImageHistoryNum = 100
|
|
|
|
if len(p.drawImageHistory)+1 > maxDrawImageHistoryNum {
|
2017-05-03 15:15:59 +02:00
|
|
|
p.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,
|
2017-05-27 09:14:48 +02:00
|
|
|
colorm: *colorm,
|
2016-07-24 19:28:59 +02:00
|
|
|
mode: mode,
|
|
|
|
}
|
2016-07-13 19:52:24 +02:00
|
|
|
p.drawImageHistory = append(p.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-05-30 19:09:27 +02:00
|
|
|
func (p *Image) At(x, y int) (color.RGBA, error) {
|
2017-02-04 20:24:07 +01:00
|
|
|
w, h := p.image.Size()
|
2017-08-06 13:05:14 +02:00
|
|
|
w2, h2 := math.NextPowerOf2Int(w), math.NextPowerOf2Int(h)
|
2017-02-04 20:24:07 +01:00
|
|
|
if x < 0 || y < 0 || w2 <= x || h2 <= y {
|
|
|
|
return color.RGBA{}, nil
|
|
|
|
}
|
2016-07-26 19:16:31 +02:00
|
|
|
if p.basePixels == nil || p.drawImageHistory != nil || p.stale {
|
2017-05-30 19:09:27 +02:00
|
|
|
if err := p.readPixelsFromGPU(p.image); err != nil {
|
2016-08-30 16:31:59 +02:00
|
|
|
return color.RGBA{}, err
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
}
|
2017-02-04 20:24:07 +01:00
|
|
|
idx := 4*x + 4*y*w2
|
2016-07-13 20:29:19 +02:00
|
|
|
r, g, b, a := p.basePixels[idx], p.basePixels[idx+1], p.basePixels[idx+2], p.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-05-03 16:18:35 +02:00
|
|
|
func (p *Image) makeStaleIfDependingOn(target *Image) {
|
2016-07-26 19:16:31 +02:00
|
|
|
if p.stale {
|
2016-09-03 16:54:52 +02:00
|
|
|
return
|
2016-07-26 19:16:31 +02:00
|
|
|
}
|
2017-06-01 03:44:28 +02:00
|
|
|
if p.dependsOn(target) {
|
|
|
|
p.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-05-30 19:09:27 +02:00
|
|
|
func (p *Image) readPixelsFromGPU(image *graphics.Image) error {
|
2016-07-23 18:28:39 +02:00
|
|
|
var err error
|
2017-05-30 19:09:27 +02:00
|
|
|
p.basePixels, err = image.Pixels()
|
2016-07-23 18:28:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-30 16:31:59 +02:00
|
|
|
p.baseColor = color.RGBA{}
|
2016-07-23 18:28:39 +02:00
|
|
|
p.drawImageHistory = nil
|
2016-07-26 19:16:31 +02:00
|
|
|
p.stale = false
|
2016-07-23 18:28:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// resolveStale resolves the image's 'stale' state.
|
|
|
|
func (p *Image) resolveStale() error {
|
2017-07-02 08:58:00 +02:00
|
|
|
if !IsRestoringEnabled() {
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-03 19:06:54 +02:00
|
|
|
if p.volatile {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-26 19:20:42 +02:00
|
|
|
if !p.stale {
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-30 19:09:27 +02:00
|
|
|
return p.readPixelsFromGPU(p.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-06-01 03:44:28 +02:00
|
|
|
func (p *Image) dependsOn(target *Image) bool {
|
|
|
|
for _, c := range p.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-06-02 19:41:37 +02:00
|
|
|
func (p *Image) dependingImages() map[*Image]struct{} {
|
|
|
|
r := map[*Image]struct{}{}
|
|
|
|
for _, c := range p.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-05-03 16:11:43 +02:00
|
|
|
func (p *Image) hasDependency() bool {
|
2016-07-26 19:16:31 +02:00
|
|
|
if p.stale {
|
|
|
|
return false
|
|
|
|
}
|
2017-05-03 15:15:59 +02:00
|
|
|
return len(p.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-05-30 19:09:27 +02:00
|
|
|
func (p *Image) restore() error {
|
2016-12-27 02:58:46 +01:00
|
|
|
w, h := p.image.Size()
|
2016-09-03 19:31:50 +02:00
|
|
|
if p.screen {
|
|
|
|
// The screen image should also be recreated because framebuffer might
|
|
|
|
// be changed.
|
2017-06-30 21:12:09 +02:00
|
|
|
p.image = graphics.NewScreenFramebufferImage(w, h, p.offsetX, p.offsetY)
|
2016-09-03 19:31:50 +02:00
|
|
|
p.basePixels = nil
|
|
|
|
p.baseColor = color.RGBA{}
|
|
|
|
p.drawImageHistory = nil
|
|
|
|
p.stale = false
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-03 19:06:54 +02:00
|
|
|
if p.volatile {
|
2017-03-03 15:51:25 +01:00
|
|
|
p.image = graphics.NewImage(w, h, p.filter)
|
2016-09-03 19:11:02 +02:00
|
|
|
p.basePixels = nil
|
|
|
|
p.baseColor = color.RGBA{}
|
|
|
|
p.drawImageHistory = nil
|
|
|
|
p.stale = false
|
2016-09-03 19:06:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-07-26 19:25:08 +02:00
|
|
|
if p.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
|
|
|
}
|
2017-08-06 13:05:14 +02:00
|
|
|
w2, h2 := math.NextPowerOf2Int(w), math.NextPowerOf2Int(h)
|
2017-02-04 20:16:09 +01:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, w2, h2))
|
2016-07-13 20:29:19 +02:00
|
|
|
if p.basePixels != nil {
|
2016-12-27 02:58:46 +01:00
|
|
|
for j := 0; j < h; j++ {
|
2017-02-04 20:16:09 +01:00
|
|
|
copy(img.Pix[j*img.Stride:], p.basePixels[j*w2*4:(j+1)*w2*4])
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-03 15:51:25 +01:00
|
|
|
gimg := graphics.NewImageFromImage(img, w, h, p.filter)
|
2016-08-30 16:31:59 +02:00
|
|
|
if p.baseColor != (color.RGBA{}) {
|
2016-07-15 17:11:37 +02:00
|
|
|
if p.basePixels != nil {
|
2017-05-31 18:27:56 +02:00
|
|
|
panic("not reached")
|
2016-07-15 17:11:37 +02:00
|
|
|
}
|
2017-07-02 14:06:36 +02:00
|
|
|
gimg.Fill(p.baseColor.R, p.baseColor.G, p.baseColor.B, p.baseColor.A)
|
2016-07-15 17:11:37 +02:00
|
|
|
}
|
2016-07-13 19:52:24 +02:00
|
|
|
for _, c := range p.drawImageHistory {
|
2017-06-02 19:41:37 +02:00
|
|
|
// All dependencies must be already resolved.
|
2017-05-03 16:11:43 +02:00
|
|
|
if c.image.hasDependency() {
|
2017-05-31 18:27:56 +02:00
|
|
|
panic("not reached")
|
2017-01-20 20:24:39 +01:00
|
|
|
}
|
2017-05-27 09:14:48 +02:00
|
|
|
gimg.DrawImage(c.image.image, c.vertices, &c.colorm, c.mode)
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
2016-09-03 16:08:51 +02:00
|
|
|
p.image = gimg
|
|
|
|
|
2017-03-03 15:51:25 +01:00
|
|
|
var err error
|
2017-05-30 19:09:27 +02:00
|
|
|
p.basePixels, err = gimg.Pixels()
|
2016-07-13 19:35:20 +02:00
|
|
|
if err != nil {
|
2016-09-03 16:08:51 +02:00
|
|
|
return err
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
2016-08-30 16:31:59 +02:00
|
|
|
p.baseColor = color.RGBA{}
|
2016-07-13 19:52:24 +02:00
|
|
|
p.drawImageHistory = nil
|
2016-07-26 19:16:31 +02:00
|
|
|
p.stale = false
|
2016-09-03 16:08:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// Dispose disposes the image.
|
|
|
|
//
|
|
|
|
// After disposing, calling the funciton of the image causes unexpected results.
|
2017-05-03 16:24:00 +02:00
|
|
|
func (p *Image) Dispose() {
|
2017-09-14 17:24:18 +02:00
|
|
|
theImages.makeStaleIfDependingOn(p)
|
2017-03-03 15:51:25 +01:00
|
|
|
p.image.Dispose()
|
2016-09-03 16:08:51 +02:00
|
|
|
p.image = nil
|
2016-09-03 16:42:28 +02:00
|
|
|
p.basePixels = nil
|
|
|
|
p.baseColor = color.RGBA{}
|
|
|
|
p.drawImageHistory = nil
|
|
|
|
p.stale = false
|
2017-05-03 16:24:00 +02:00
|
|
|
theImages.remove(p)
|
|
|
|
runtime.SetFinalizer(p, 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-08-06 16:00:24 +02:00
|
|
|
func (p *Image) IsInvalidated() (bool, error) {
|
|
|
|
// FlushCommands is required because c.offscreen.impl might not have an actual texture.
|
|
|
|
if err := graphics.FlushCommands(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2017-07-02 08:58:00 +02:00
|
|
|
if !IsRestoringEnabled() {
|
2017-08-06 16:00:24 +02:00
|
|
|
return false, nil
|
2017-07-02 08:58:00 +02:00
|
|
|
}
|
2017-08-06 16:00:24 +02:00
|
|
|
return p.image.IsInvalidated(), nil
|
2016-09-03 17:07:06 +02:00
|
|
|
}
|