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 (
|
2018-02-28 16:27:55 +01:00
|
|
|
"fmt"
|
2021-07-29 09:09:25 +02:00
|
|
|
"image"
|
2023-02-25 05:50:02 +01:00
|
|
|
"math"
|
2016-07-13 19:35:20 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2016-07-13 19:35:20 +02:00
|
|
|
)
|
|
|
|
|
2019-02-12 15:31:52 +01:00
|
|
|
type Pixels struct {
|
2022-03-20 15:00:00 +01:00
|
|
|
pixelsRecords *pixelsRecords
|
2019-02-12 15:31:52 +01:00
|
|
|
}
|
|
|
|
|
2019-07-21 03:52:29 +02:00
|
|
|
// Apply applies the Pixels state to the given image especially for restoring.
|
2019-07-17 16:03:14 +02:00
|
|
|
func (p *Pixels) Apply(img *graphicscommand.Image) {
|
2019-08-27 00:05:08 +02:00
|
|
|
// Pixels doesn't clear the image. This is a caller's responsibility.
|
|
|
|
|
2022-03-20 15:00:00 +01:00
|
|
|
if p.pixelsRecords == nil {
|
2019-07-17 16:03:14 +02:00
|
|
|
return
|
2019-07-16 20:15:01 +02:00
|
|
|
}
|
2022-03-20 15:00:00 +01:00
|
|
|
p.pixelsRecords.apply(img)
|
2019-02-12 15:31:52 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
func (p *Pixels) AddOrReplace(pix []byte, region image.Rectangle) {
|
2022-03-20 15:00:00 +01:00
|
|
|
if p.pixelsRecords == nil {
|
|
|
|
p.pixelsRecords = &pixelsRecords{}
|
2019-02-12 15:31:52 +01:00
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
p.pixelsRecords.addOrReplace(pix, region)
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
func (p *Pixels) Clear(region image.Rectangle) {
|
2019-07-17 16:03:14 +02:00
|
|
|
// Note that we don't care whether the region is actually removed or not here. There is an actual case that
|
|
|
|
// the region is allocated but nothing is rendered. See TestDisposeImmediately at shareable package.
|
2022-03-20 15:00:00 +01:00
|
|
|
if p.pixelsRecords == nil {
|
2019-07-17 16:03:14 +02:00
|
|
|
return
|
2019-07-17 04:13:57 +02:00
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
p.pixelsRecords.clear(region)
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
func (p *Pixels) ReadPixels(pixels []byte, region image.Rectangle, imageWidth, imageHeight int) {
|
2022-08-05 14:40:38 +02:00
|
|
|
if p.pixelsRecords == nil {
|
|
|
|
for i := range pixels {
|
|
|
|
pixels[i] = 0
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
2022-08-05 14:40:38 +02:00
|
|
|
return
|
2019-02-12 15:31:52 +01:00
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
p.pixelsRecords.readPixels(pixels, region, imageWidth, imageHeight)
|
2019-02-12 15:31:52 +01:00
|
|
|
}
|
|
|
|
|
2023-02-24 13:49:33 +01:00
|
|
|
func (p *Pixels) AppendRegion(regions []image.Rectangle) []image.Rectangle {
|
2022-08-27 14:22:31 +02:00
|
|
|
if p.pixelsRecords == nil {
|
2023-02-24 13:49:33 +01:00
|
|
|
return regions
|
2022-08-27 14:22:31 +02:00
|
|
|
}
|
2023-02-24 13:49:33 +01:00
|
|
|
return p.pixelsRecords.appendRegions(regions)
|
2022-08-27 14:22:31 +02:00
|
|
|
}
|
|
|
|
|
2019-04-22 16:12:03 +02:00
|
|
|
// drawTrianglesHistoryItem is an item for history of draw-image commands.
|
|
|
|
type drawTrianglesHistoryItem struct {
|
2022-07-12 18:46:02 +02:00
|
|
|
images [graphics.ShaderImageCount]*Image
|
|
|
|
offsets [graphics.ShaderImageCount - 1][2]float32
|
2020-11-07 11:14:06 +01:00
|
|
|
vertices []float32
|
|
|
|
indices []uint16
|
2022-10-15 11:58:56 +02:00
|
|
|
blend graphicsdriver.Blend
|
2022-02-06 12:41:32 +01:00
|
|
|
dstRegion graphicsdriver.Region
|
|
|
|
srcRegion graphicsdriver.Region
|
2020-11-07 11:14:06 +01:00
|
|
|
shader *Shader
|
2022-12-03 08:11:26 +01:00
|
|
|
uniforms []uint32
|
2021-07-02 12:26:09 +02:00
|
|
|
evenOdd bool
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
|
2022-06-06 02:12:57 +02:00
|
|
|
type ImageType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ImageTypeRegular indicates the image is a regular image.
|
|
|
|
ImageTypeRegular ImageType = iota
|
|
|
|
|
2022-06-07 16:45:35 +02:00
|
|
|
// ImageTypeScreen indicates the image is used as an actual screen.
|
|
|
|
ImageTypeScreen
|
2022-06-06 02:12:57 +02:00
|
|
|
|
|
|
|
// ImageTypeVolatile indicates the image is cleared whenever a frame starts.
|
2022-06-07 19:20:09 +02:00
|
|
|
//
|
|
|
|
// Regular non-volatile images need to record drawing history or read its pixels from GPU if necessary so that all
|
|
|
|
// the images can be restored automatically from the context lost. However, such recording the drawing history or
|
2023-01-28 11:06:38 +01:00
|
|
|
// reading pixels from GPU are expensive operations. Volatile images can skip such operations, but the image content
|
2022-06-07 19:20:09 +02:00
|
|
|
// is cleared every frame instead.
|
2022-06-06 02:12:57 +02:00
|
|
|
ImageTypeVolatile
|
|
|
|
)
|
|
|
|
|
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-08-15 19:28:59 +02:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
|
2019-07-21 03:52:29 +02: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
|
|
|
|
|
2023-02-24 13:49:33 +01:00
|
|
|
// staleRegions indicates the regions to restore.
|
|
|
|
// staleRegions is valid only when stale is true.
|
2023-02-25 17:31:11 +01:00
|
|
|
// staleRegions is not used when AlwaysReadPixelsFromGPU() returns true.
|
2023-02-24 13:49:33 +01:00
|
|
|
staleRegions []image.Rectangle
|
2022-08-27 14:22:31 +02:00
|
|
|
|
2023-03-08 08:27:36 +01:00
|
|
|
// pixelsCache is cached byte slices for pixels.
|
|
|
|
// pixelsCache is just a cache to avoid allocations (#2375).
|
2023-03-08 06:50:13 +01:00
|
|
|
//
|
|
|
|
// A key is the region and a value is a byte slice for the region.
|
2023-03-08 08:40:47 +01:00
|
|
|
//
|
|
|
|
// It is fine to reuse the same byte slice for the same region for basePixels,
|
|
|
|
// as old pixels for the same region will be invalidated at basePixel.AddOrReplace.
|
2023-03-08 08:27:36 +01:00
|
|
|
pixelsCache map[image.Rectangle][]byte
|
2023-03-08 06:50:13 +01:00
|
|
|
|
2023-03-08 08:40:47 +01:00
|
|
|
// regionsCache is cached regions.
|
|
|
|
// regionsCache is just a cache to avoid allocations (#2375).
|
|
|
|
regionsCache []image.Rectangle
|
2023-03-08 08:07:33 +01:00
|
|
|
|
2022-06-06 02:12:57 +02:00
|
|
|
imageType ImageType
|
2018-12-16 14:24:19 +01:00
|
|
|
}
|
2018-04-05 04:15:24 +02:00
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
// NewImage creates a white image with the given size.
|
2018-12-01 20:28:38 +01:00
|
|
|
//
|
|
|
|
// The returned image is cleared.
|
2018-04-27 05:08:59 +02:00
|
|
|
//
|
|
|
|
// Note that Dispose is not called automatically.
|
2022-06-06 02:29:30 +02:00
|
|
|
func NewImage(width, height int, imageType ImageType) *Image {
|
2021-09-08 20:58:58 +02:00
|
|
|
if !graphicsDriverInitialized {
|
|
|
|
panic("restorable: graphics driver must be ready at NewImage but not")
|
|
|
|
}
|
|
|
|
|
2017-05-03 16:15:18 +02:00
|
|
|
i := &Image{
|
2022-06-07 16:45:35 +02:00
|
|
|
image: graphicscommand.NewImage(width, height, imageType == ImageTypeScreen),
|
2022-06-06 02:29:30 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
imageType: imageType,
|
2017-03-03 15:51:25 +01:00
|
|
|
}
|
2020-11-07 18:26:51 +01:00
|
|
|
clearImage(i.image)
|
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-16 20:32:08 +02:00
|
|
|
// Extend extends the image by the given size.
|
|
|
|
// Extend creates a new image with the given size and copies the pixels of the given source image.
|
|
|
|
// Extend disposes itself after its call.
|
|
|
|
func (i *Image) Extend(width, height int) *Image {
|
2022-11-11 13:15:28 +01:00
|
|
|
if i.width >= width && i.height >= height {
|
|
|
|
return i
|
2019-07-16 20:32:08 +02:00
|
|
|
}
|
|
|
|
|
2022-06-06 02:29:30 +02:00
|
|
|
newImg := NewImage(width, height, i.imageType)
|
2019-07-14 19:00:26 +02:00
|
|
|
|
2022-08-07 20:16:23 +02:00
|
|
|
// Use DrawTriangles instead of WritePixels because the image i might be stale and not have its pixels
|
2020-08-15 08:11:56 +02:00
|
|
|
// information.
|
2022-07-12 18:46:02 +02:00
|
|
|
srcs := [graphics.ShaderImageCount]*Image{i}
|
|
|
|
var offsets [graphics.ShaderImageCount - 1][2]float32
|
2020-08-15 08:11:56 +02:00
|
|
|
sw, sh := i.image.InternalSize()
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(0, 0, float32(sw), float32(sh), 0, 0, float32(sw), float32(sh), 1, 1, 1, 1)
|
2020-08-15 08:11:56 +02:00
|
|
|
is := graphics.QuadIndices()
|
2022-02-06 12:41:32 +01:00
|
|
|
dr := graphicsdriver.Region{
|
2020-08-15 08:11:56 +02:00
|
|
|
X: 0,
|
|
|
|
Y: 0,
|
|
|
|
Width: float32(sw),
|
|
|
|
Height: float32(sh),
|
|
|
|
}
|
2022-10-15 11:58:56 +02:00
|
|
|
newImg.DrawTriangles(srcs, offsets, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, NearestFilterShader, nil, false)
|
2019-07-16 20:32:08 +02:00
|
|
|
i.Dispose()
|
|
|
|
|
|
|
|
return newImg
|
2019-07-14 19:00:26 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 18:55:18 +02:00
|
|
|
// quadVertices returns vertices to render a quad. These values are passed to graphicscommand.Image.
|
2023-04-16 11:56:14 +02:00
|
|
|
func quadVertices(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1, cr, cg, cb, ca float32) []float32 {
|
2019-09-19 18:55:18 +02:00
|
|
|
return []float32{
|
2023-04-16 11:56:14 +02:00
|
|
|
dx0, dy0, sx0, sy0, cr, cg, cb, ca,
|
|
|
|
dx1, dy0, sx1, sy0, cr, cg, cb, ca,
|
|
|
|
dx0, dy1, sx0, sy1, cr, cg, cb, ca,
|
|
|
|
dx1, dy1, sx1, sy1, cr, cg, cb, ca,
|
2019-09-19 18:55:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 18:26:51 +01:00
|
|
|
func clearImage(i *graphicscommand.Image) {
|
2020-05-30 09:06:22 +02:00
|
|
|
// This needs to use 'InternalSize' to render the whole region, or edges are unexpectedly cleared on some
|
|
|
|
// devices.
|
2019-08-27 00:05:08 +02:00
|
|
|
dw, dh := i.InternalSize()
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(0, 0, float32(dw), float32(dh), 0, 0, 0, 0, 0, 0, 0, 0)
|
2019-08-24 19:39:59 +02:00
|
|
|
is := graphics.QuadIndices()
|
2022-07-12 18:46:02 +02:00
|
|
|
var offsets [graphics.ShaderImageCount - 1][2]float32
|
2022-02-06 12:41:32 +01:00
|
|
|
dstRegion := graphicsdriver.Region{
|
2020-11-07 11:14:06 +01:00
|
|
|
X: 0,
|
|
|
|
Y: 0,
|
|
|
|
Width: float32(dw),
|
|
|
|
Height: float32(dh),
|
|
|
|
}
|
2023-03-01 07:38:52 +01:00
|
|
|
i.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{}, offsets, vs, is, graphicsdriver.BlendClear, dstRegion, graphicsdriver.Region{}, clearShader.shader, nil, false)
|
2019-08-24 19:39:59 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-07-21 03:52:29 +02:00
|
|
|
return &i.basePixels
|
2017-05-31 18:27:56 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// makeStale makes the image stale.
|
2022-08-27 14:22:31 +02:00
|
|
|
func (i *Image) makeStale(rect image.Rectangle) {
|
|
|
|
i.stale = true
|
2023-02-25 05:50:02 +01:00
|
|
|
|
2023-02-26 04:26:22 +01:00
|
|
|
// If ReadPixels always reads pixels from GPU, staleRegions are never used.
|
2023-02-25 17:31:11 +01:00
|
|
|
if AlwaysReadPixelsFromGPU() {
|
2023-02-25 15:24:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-08 19:31:22 +02:00
|
|
|
var addedRegions []image.Rectangle
|
|
|
|
i.appendRegionsForDrawTriangles(&addedRegions)
|
2023-02-24 13:49:33 +01:00
|
|
|
if !rect.Empty() {
|
2023-04-08 19:31:22 +02:00
|
|
|
appendRegionRemovingDuplicates(&addedRegions, rect)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rect := range addedRegions {
|
|
|
|
appendRegionRemovingDuplicates(&i.staleRegions, rect)
|
2023-02-25 05:50:02 +01:00
|
|
|
}
|
2022-08-27 14:22:31 +02:00
|
|
|
|
2021-09-09 05:57:47 +02:00
|
|
|
i.clearDrawTrianglesHistory()
|
2018-03-25 12:42:44 +02:00
|
|
|
|
2023-02-25 15:57:34 +01:00
|
|
|
// Clear pixels to save memory.
|
2023-04-08 19:31:22 +02:00
|
|
|
for _, r := range addedRegions {
|
2023-04-27 17:12:42 +02:00
|
|
|
i.basePixels.Clear(r)
|
2023-02-25 15:57:34 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-07 20:16:23 +02:00
|
|
|
// ClearPixels clears the specified region by WritePixels.
|
2023-04-27 17:12:42 +02:00
|
|
|
func (i *Image) ClearPixels(region image.Rectangle) {
|
|
|
|
i.WritePixels(nil, region)
|
2019-07-09 19:36:59 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 19:24:10 +02:00
|
|
|
func (i *Image) needsRestoring() bool {
|
|
|
|
return i.imageType == ImageTypeRegular
|
|
|
|
}
|
|
|
|
|
2022-08-07 20:16:23 +02:00
|
|
|
// WritePixels replaces the image pixels with the given pixels slice.
|
2019-07-16 20:15:01 +02:00
|
|
|
//
|
2022-08-07 20:16:23 +02:00
|
|
|
// The specified region must not be overlapped with other regions by WritePixels.
|
2023-04-27 17:12:42 +02:00
|
|
|
func (i *Image) WritePixels(pixels []byte, region image.Rectangle) {
|
|
|
|
if region.Dx() <= 0 || region.Dy() <= 0 {
|
2018-02-28 16:27:55 +01:00
|
|
|
panic("restorable: width/height must be positive")
|
|
|
|
}
|
2019-09-20 22:40:05 +02:00
|
|
|
w, h := i.width, i.height
|
2023-04-27 17:12:42 +02:00
|
|
|
if !region.In(image.Rect(0, 0, w, h)) {
|
|
|
|
panic(fmt.Sprintf("restorable: out of range %v", region))
|
2018-02-28 16:27:55 +01:00
|
|
|
}
|
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)
|
2023-01-28 11:06:38 +01:00
|
|
|
// For this purpose, 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-07-17 16:03:14 +02:00
|
|
|
if pixels != nil {
|
2023-04-27 17:00:51 +02:00
|
|
|
i.image.WritePixels(pixels, region)
|
2019-07-17 16:03:14 +02:00
|
|
|
} else {
|
|
|
|
// TODO: When pixels == nil, we don't have to care the pixel state there. In such cases, the image
|
2022-08-07 20:16:23 +02:00
|
|
|
// accepts only WritePixels and not Fill or DrawTriangles.
|
|
|
|
// TODO: Separate Image struct into two: images for WritePixels-only, and the others.
|
2023-04-27 17:00:51 +02:00
|
|
|
i.image.WritePixels(make([]byte, 4*region.Dx()*region.Dy()), region)
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
2018-02-28 16:27:55 +01:00
|
|
|
|
2022-09-13 15:53:30 +02:00
|
|
|
// Even if the image is already stale, call makeStale to extend the stale region.
|
|
|
|
if !needsRestoring() || !i.needsRestoring() || i.stale {
|
2023-04-27 17:12:42 +02:00
|
|
|
i.makeStale(region)
|
2020-08-15 08:11:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
if region.Eq(image.Rect(0, 0, w, h)) {
|
2019-07-17 16:03:14 +02:00
|
|
|
if pixels != nil {
|
2022-10-06 07:11:09 +02:00
|
|
|
// pixels can point to a shared region.
|
|
|
|
// This function is responsible to copy this.
|
|
|
|
copiedPixels := make([]byte, len(pixels))
|
|
|
|
copy(copiedPixels, pixels)
|
2023-04-27 17:12:42 +02:00
|
|
|
i.basePixels.AddOrReplace(copiedPixels, image.Rect(0, 0, w, h))
|
2019-07-17 16:03:14 +02:00
|
|
|
} else {
|
2023-04-27 17:12:42 +02:00
|
|
|
i.basePixels.Clear(image.Rect(0, 0, w, h))
|
2018-03-19 16:37:02 +01:00
|
|
|
}
|
2021-09-09 05:57:47 +02:00
|
|
|
i.clearDrawTrianglesHistory()
|
2019-11-15 16:43:22 +01:00
|
|
|
i.stale = false
|
2023-02-24 13:49:33 +01:00
|
|
|
i.staleRegions = i.staleRegions[:0]
|
2018-03-25 13:39:06 +02:00
|
|
|
return
|
|
|
|
}
|
2018-12-26 19:06:44 +01:00
|
|
|
|
2023-02-24 10:25:17 +01:00
|
|
|
// Records for DrawTriangles cannot come before records for WritePixels.
|
2019-04-22 16:12:03 +02:00
|
|
|
if len(i.drawTrianglesHistory) > 0 {
|
2023-04-27 17:12:42 +02:00
|
|
|
i.makeStale(region)
|
2022-09-13 15:09:29 +02:00
|
|
|
return
|
2018-03-25 13:39:06 +02:00
|
|
|
}
|
2018-12-26 19:06:44 +01:00
|
|
|
|
2019-07-17 16:03:14 +02:00
|
|
|
if pixels != nil {
|
2022-10-06 07:11:09 +02:00
|
|
|
// pixels can point to a shared region.
|
|
|
|
// This function is responsible to copy this.
|
|
|
|
copiedPixels := make([]byte, len(pixels))
|
|
|
|
copy(copiedPixels, pixels)
|
2023-04-27 17:12:42 +02:00
|
|
|
i.basePixels.AddOrReplace(copiedPixels, region)
|
2019-07-17 16:03:14 +02:00
|
|
|
} else {
|
2023-04-27 17:12:42 +02:00
|
|
|
i.basePixels.Clear(region)
|
2019-07-15 20:08:26 +02:00
|
|
|
}
|
2016-07-26 03:51:48 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 18:55:18 +02:00
|
|
|
// DrawTriangles draws triangles with the given image.
|
|
|
|
//
|
|
|
|
// The vertex floats are:
|
|
|
|
//
|
2022-08-03 13:48:02 +02:00
|
|
|
// 0: Destination X in pixels
|
|
|
|
// 1: Destination Y in pixels
|
|
|
|
// 2: Source X in texels
|
|
|
|
// 3: Source Y in texels
|
|
|
|
// 4: Color R [0.0-1.0]
|
|
|
|
// 5: Color G
|
|
|
|
// 6: Color B
|
|
|
|
// 7: Color Y
|
2022-12-03 08:11:26 +01:00
|
|
|
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, offsets [graphics.ShaderImageCount - 1][2]float32, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion, srcRegion graphicsdriver.Region, shader *Shader, uniforms []uint32, evenOdd bool) {
|
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
|
|
|
|
2020-06-03 18:35:35 +02:00
|
|
|
// TODO: Add tests to confirm this logic.
|
|
|
|
var srcstale bool
|
2020-07-17 18:09:58 +02:00
|
|
|
for _, src := range srcs {
|
|
|
|
if src == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-06 02:12:57 +02:00
|
|
|
if src.stale || src.imageType == ImageTypeVolatile {
|
2020-07-17 18:09:58 +02:00
|
|
|
srcstale = true
|
|
|
|
break
|
2020-06-03 18:35:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-25 05:50:02 +01:00
|
|
|
// Even if the image is already stale, call makeStale to extend the stale region.
|
|
|
|
if srcstale || !needsRestoring() || !i.needsRestoring() || i.stale {
|
|
|
|
i.makeStale(regionToRectangle(dstRegion))
|
2016-09-03 16:42:44 +02:00
|
|
|
} else {
|
2022-10-15 11:58:56 +02:00
|
|
|
i.appendDrawTrianglesHistory(srcs, offsets, vertices, indices, blend, dstRegion, srcRegion, shader, uniforms, evenOdd)
|
2020-05-24 17:46:23 +02:00
|
|
|
}
|
2020-07-05 18:55:46 +02:00
|
|
|
|
2022-07-12 18:46:02 +02:00
|
|
|
var imgs [graphics.ShaderImageCount]*graphicscommand.Image
|
2022-10-02 16:49:07 +02:00
|
|
|
for i, src := range srcs {
|
|
|
|
if src == nil {
|
|
|
|
continue
|
2020-07-17 18:09:58 +02:00
|
|
|
}
|
2022-10-02 16:49:07 +02:00
|
|
|
imgs[i] = src.image
|
2020-07-05 18:55:46 +02:00
|
|
|
}
|
2022-10-15 11:58:56 +02:00
|
|
|
i.image.DrawTriangles(imgs, offsets, vertices, indices, blend, dstRegion, srcRegion, shader.shader, uniforms, evenOdd)
|
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.
|
2022-12-03 08:11:26 +01:00
|
|
|
func (i *Image) appendDrawTrianglesHistory(srcs [graphics.ShaderImageCount]*Image, offsets [graphics.ShaderImageCount - 1][2]float32, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion, srcRegion graphicsdriver.Region, shader *Shader, uniforms []uint32, evenOdd bool) {
|
2022-06-07 19:24:10 +02:00
|
|
|
if i.stale || !i.needsRestoring() {
|
2023-02-25 05:50:02 +01:00
|
|
|
panic("restorable: an image must not be stale or need restoring at appendDrawTrianglesHistory")
|
2016-07-26 19:16:31 +02:00
|
|
|
}
|
2023-02-25 17:31:11 +01:00
|
|
|
if AlwaysReadPixelsFromGPU() {
|
|
|
|
panic("restorable: appendDrawTrianglesHistory must not be called when AlwaysReadPixelsFromGPU() returns true")
|
2023-02-25 15:24:09 +01:00
|
|
|
}
|
2022-06-07 19:24:10 +02:00
|
|
|
|
2019-02-13 02:45:05 +01:00
|
|
|
// TODO: Would it be possible to merge draw image history items?
|
2022-07-12 19:11:12 +02:00
|
|
|
const maxDrawTrianglesHistoryCount = 1024
|
|
|
|
if len(i.drawTrianglesHistory)+1 > maxDrawTrianglesHistoryCount {
|
2023-02-25 05:50:02 +01:00
|
|
|
i.makeStale(regionToRectangle(dstRegion))
|
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-09-28 18:30:26 +02:00
|
|
|
|
2021-06-26 19:21:51 +02:00
|
|
|
vs := make([]float32, len(vertices))
|
|
|
|
copy(vs, vertices)
|
2021-03-20 08:18:50 +01:00
|
|
|
|
2019-09-28 18:30:26 +02:00
|
|
|
is := make([]uint16, len(indices))
|
|
|
|
copy(is, indices)
|
2020-05-24 17:46:23 +02:00
|
|
|
|
2022-12-03 13:47:10 +01:00
|
|
|
us := make([]uint32, len(uniforms))
|
|
|
|
copy(us, uniforms)
|
|
|
|
|
2019-04-22 16:12:03 +02:00
|
|
|
item := &drawTrianglesHistoryItem{
|
2020-11-07 11:14:06 +01:00
|
|
|
images: srcs,
|
|
|
|
offsets: offsets,
|
2021-06-26 19:21:51 +02:00
|
|
|
vertices: vs,
|
2020-11-07 11:14:06 +01:00
|
|
|
indices: is,
|
2022-10-15 11:58:56 +02:00
|
|
|
blend: blend,
|
2020-11-07 11:14:06 +01:00
|
|
|
dstRegion: dstRegion,
|
|
|
|
srcRegion: srcRegion,
|
|
|
|
shader: shader,
|
2022-12-03 13:47:10 +01:00
|
|
|
uniforms: us,
|
2021-07-02 12:26:09 +02:00
|
|
|
evenOdd: evenOdd,
|
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
|
|
|
}
|
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func (i *Image) readPixelsFromGPUIfNeeded(graphicsDriver graphicsdriver.Graphics) error {
|
2019-07-21 03:52:29 +02:00
|
|
|
if len(i.drawTrianglesHistory) > 0 || i.stale {
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := i.readPixelsFromGPU(graphicsDriver); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
return err
|
|
|
|
}
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) error {
|
2023-02-25 17:31:11 +01:00
|
|
|
if AlwaysReadPixelsFromGPU() {
|
2023-04-27 17:00:51 +02:00
|
|
|
if err := i.image.ReadPixels(graphicsDriver, pixels, region); err != nil {
|
2023-02-25 15:11:39 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := i.readPixelsFromGPUIfNeeded(graphicsDriver); err != nil {
|
2022-08-05 14:40:38 +02:00
|
|
|
return err
|
2020-01-18 17:18:56 +01:00
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
if got, want := len(pixels), 4*region.Dx()*region.Dy(); got != want {
|
2022-08-05 14:40:38 +02:00
|
|
|
return fmt.Errorf("restorable: len(pixels) must be %d but %d at ReadPixels", want, got)
|
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
i.basePixels.ReadPixels(pixels, region, i.width, i.height)
|
2022-08-05 14:40:38 +02:00
|
|
|
return nil
|
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) {
|
2023-02-25 05:50:02 +01:00
|
|
|
// There is no new region to make stale.
|
|
|
|
i.makeStale(image.Rectangle{})
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 17:20:08 +02:00
|
|
|
// makeStaleIfDependingOnShader makes the image stale if the image depends on shader.
|
|
|
|
func (i *Image) makeStaleIfDependingOnShader(shader *Shader) {
|
|
|
|
if i.stale {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if i.dependsOnShader(shader) {
|
2023-02-25 05:50:02 +01:00
|
|
|
// There is no new region to make stale.
|
|
|
|
i.makeStale(image.Rectangle{})
|
2020-05-25 17:20:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// readPixelsFromGPU reads the pixels from GPU and resolves the image's 'stale' state.
|
2022-03-19 15:55:14 +01:00
|
|
|
func (i *Image) readPixelsFromGPU(graphicsDriver graphicsdriver.Graphics) error {
|
2023-02-24 13:49:33 +01:00
|
|
|
var rs []image.Rectangle
|
2023-02-25 05:50:02 +01:00
|
|
|
if i.stale {
|
2023-03-08 08:40:47 +01:00
|
|
|
rs = i.staleRegions
|
2023-02-25 05:50:02 +01:00
|
|
|
} else {
|
2023-04-08 19:31:22 +02:00
|
|
|
i.appendRegionsForDrawTriangles(&i.regionsCache)
|
2023-03-08 08:40:47 +01:00
|
|
|
defer func() {
|
|
|
|
i.regionsCache = i.regionsCache[:0]
|
|
|
|
}()
|
2023-04-08 19:31:22 +02:00
|
|
|
rs = i.regionsCache
|
2022-09-13 19:17:12 +02:00
|
|
|
}
|
2023-02-24 13:49:33 +01:00
|
|
|
|
|
|
|
for _, r := range rs {
|
|
|
|
if r.Empty() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-08 08:27:36 +01:00
|
|
|
if i.pixelsCache == nil {
|
|
|
|
i.pixelsCache = map[image.Rectangle][]byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
pix, ok := i.pixelsCache[r]
|
|
|
|
if !ok {
|
|
|
|
pix = make([]byte, 4*r.Dx()*r.Dy())
|
|
|
|
i.pixelsCache[r] = pix
|
|
|
|
}
|
2023-04-27 17:00:51 +02:00
|
|
|
if err := i.image.ReadPixels(graphicsDriver, pix, r); err != nil {
|
2022-08-27 14:22:31 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
i.basePixels.AddOrReplace(pix, r)
|
2022-08-27 14:22:31 +02:00
|
|
|
}
|
2023-03-08 08:40:47 +01:00
|
|
|
|
2021-09-09 05:57:47 +02:00
|
|
|
i.clearDrawTrianglesHistory()
|
2017-09-14 17:57:52 +02:00
|
|
|
i.stale = false
|
2023-02-24 13:49:33 +01:00
|
|
|
i.staleRegions = i.staleRegions[:0]
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2016-07-23 18:28:39 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// resolveStale resolves the image's 'stale' state.
|
2022-03-19 15:55:14 +01:00
|
|
|
func (i *Image) resolveStale(graphicsDriver graphicsdriver.Graphics) error {
|
2022-06-09 19:58:01 +02:00
|
|
|
if !needsRestoring() {
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2017-07-02 08:58:00 +02:00
|
|
|
}
|
2022-06-07 19:24:10 +02:00
|
|
|
if !i.needsRestoring() {
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2018-02-28 14:58:23 +01:00
|
|
|
}
|
2017-09-14 17:57:52 +02:00
|
|
|
if !i.stale {
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2016-07-26 19:20:42 +02:00
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
return i.readPixelsFromGPU(graphicsDriver)
|
2016-07-26 19:20:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 15:49:41 +02:00
|
|
|
// dependsOn reports 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 {
|
2020-07-14 20:33:13 +02:00
|
|
|
for _, img := range c.images {
|
2020-07-17 18:09:58 +02:00
|
|
|
if img == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-14 20:33:13 +02:00
|
|
|
if img == target {
|
2020-05-24 17:46:23 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2017-06-01 03:44:28 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-25 17:20:08 +02:00
|
|
|
// dependsOnShader reports whether the image depends on shader.
|
|
|
|
func (i *Image) dependsOnShader(shader *Shader) bool {
|
|
|
|
for _, c := range i.drawTrianglesHistory {
|
|
|
|
if c.shader == shader {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
// dependingImages returns all images that is depended on 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 {
|
2020-07-14 20:33:13 +02:00
|
|
|
for _, img := range c.images {
|
2020-07-17 18:09:58 +02:00
|
|
|
if img == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-14 20:33:13 +02:00
|
|
|
r[img] = struct{}{}
|
2020-05-24 17:46:23 +02:00
|
|
|
}
|
2017-06-02 19:41:37 +02:00
|
|
|
}
|
|
|
|
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.
|
2022-03-19 15:55:14 +01:00
|
|
|
func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
|
2019-09-20 22:40:05 +02:00
|
|
|
w, h := i.width, i.height
|
2019-08-15 17:36:54 +02:00
|
|
|
// Do not dispose the image here. The image should be already disposed.
|
2019-07-21 09:38:16 +02:00
|
|
|
|
2022-06-06 02:12:57 +02:00
|
|
|
switch i.imageType {
|
2022-06-07 16:45:35 +02:00
|
|
|
case ImageTypeScreen:
|
2016-09-03 19:31:50 +02:00
|
|
|
// The screen image should also be recreated because framebuffer might
|
|
|
|
// be changed.
|
2022-06-06 02:21:11 +02:00
|
|
|
i.image = graphicscommand.NewImage(w, h, true)
|
2019-07-21 03:52:29 +02:00
|
|
|
i.basePixels = Pixels{}
|
2021-09-09 05:57:47 +02:00
|
|
|
i.clearDrawTrianglesHistory()
|
2017-09-14 17:57:52 +02:00
|
|
|
i.stale = false
|
2023-02-24 13:49:33 +01:00
|
|
|
i.staleRegions = i.staleRegions[:0]
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2022-06-06 02:12:57 +02:00
|
|
|
case ImageTypeVolatile:
|
2022-06-06 02:21:11 +02:00
|
|
|
i.image = graphicscommand.NewImage(w, h, false)
|
2020-11-07 18:26:51 +01:00
|
|
|
clearImage(i.image)
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2016-09-03 19:06:54 +02:00
|
|
|
}
|
2022-06-06 02:12:57 +02:00
|
|
|
|
2017-09-14 17:57:52 +02:00
|
|
|
if i.stale {
|
2019-08-12 10:45:43 +02:00
|
|
|
panic("restorable: pixels must not be stale when restoring")
|
2016-07-26 19:25:08 +02:00
|
|
|
}
|
2019-01-13 14:29:00 +01:00
|
|
|
|
2022-06-06 02:21:11 +02:00
|
|
|
gimg := graphicscommand.NewImage(w, h, false)
|
2019-07-21 07:03:14 +02:00
|
|
|
// Clear the image explicitly.
|
2023-03-01 07:38:52 +01:00
|
|
|
clearImage(gimg)
|
|
|
|
|
2019-07-21 03:52:29 +02:00
|
|
|
i.basePixels.Apply(gimg)
|
2019-07-17 16:03:14 +02:00
|
|
|
|
2019-04-22 16:12:03 +02:00
|
|
|
for _, c := range i.drawTrianglesHistory {
|
2022-07-12 18:46:02 +02:00
|
|
|
var imgs [graphics.ShaderImageCount]*graphicscommand.Image
|
2020-07-17 18:09:58 +02:00
|
|
|
for i, img := range c.images {
|
|
|
|
if img == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if img.hasDependency() {
|
|
|
|
panic("restorable: all dependencies must be already resolved but not")
|
|
|
|
}
|
|
|
|
imgs[i] = img.image
|
2020-07-05 18:55:46 +02:00
|
|
|
}
|
2022-10-15 11:58:56 +02:00
|
|
|
gimg.DrawTriangles(imgs, c.offsets, c.vertices, c.indices, c.blend, c.dstRegion, c.srcRegion, c.shader.shader, c.uniforms, c.evenOdd)
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
2016-09-03 16:08:51 +02:00
|
|
|
|
2023-02-25 06:50:24 +01:00
|
|
|
// In order to clear the draw-triangles history, read pixels from GPU.
|
2019-07-15 19:48:49 +02:00
|
|
|
if len(i.drawTrianglesHistory) > 0 {
|
2023-04-08 19:31:22 +02:00
|
|
|
i.appendRegionsForDrawTriangles(&i.regionsCache)
|
2023-03-08 08:40:47 +01:00
|
|
|
defer func() {
|
|
|
|
i.regionsCache = i.regionsCache[:0]
|
|
|
|
}()
|
2023-03-08 06:50:13 +01:00
|
|
|
|
2023-04-08 19:31:22 +02:00
|
|
|
for _, r := range i.regionsCache {
|
2023-02-25 06:50:24 +01:00
|
|
|
if r.Empty() {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-08 06:50:13 +01:00
|
|
|
|
2023-03-08 08:27:36 +01:00
|
|
|
if i.pixelsCache == nil {
|
|
|
|
i.pixelsCache = map[image.Rectangle][]byte{}
|
2023-03-08 06:50:13 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 08:27:36 +01:00
|
|
|
pix, ok := i.pixelsCache[r]
|
2023-03-08 06:50:13 +01:00
|
|
|
if !ok {
|
|
|
|
pix = make([]byte, 4*r.Dx()*r.Dy())
|
2023-03-08 08:27:36 +01:00
|
|
|
i.pixelsCache[r] = pix
|
2023-03-08 06:50:13 +01:00
|
|
|
}
|
2023-04-27 17:00:51 +02:00
|
|
|
if err := gimg.ReadPixels(graphicsDriver, pix, r); err != nil {
|
2023-02-25 06:50:24 +01:00
|
|
|
return err
|
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
i.basePixels.AddOrReplace(pix, r)
|
2020-01-18 17:18:56 +01:00
|
|
|
}
|
2019-02-12 15:31:52 +01:00
|
|
|
}
|
2019-07-15 19:48:49 +02:00
|
|
|
|
|
|
|
i.image = gimg
|
2021-09-09 05:57:47 +02:00
|
|
|
i.clearDrawTrianglesHistory()
|
2017-09-14 17:57:52 +02:00
|
|
|
i.stale = false
|
2023-02-24 13:49:33 +01:00
|
|
|
i.staleRegions = i.staleRegions[:0]
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2016-09-03 16:08:51 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2017-09-14 17:57:52 +02:00
|
|
|
i.image.Dispose()
|
|
|
|
i.image = nil
|
2019-07-21 03:52:29 +02:00
|
|
|
i.basePixels = Pixels{}
|
2023-03-08 08:27:36 +01:00
|
|
|
i.pixelsCache = nil
|
2021-09-09 05:57:47 +02:00
|
|
|
i.clearDrawTrianglesHistory()
|
2017-09-14 17:57:52 +02:00
|
|
|
i.stale = false
|
2023-02-24 13:49:33 +01:00
|
|
|
i.staleRegions = i.staleRegions[:0]
|
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.
|
2022-03-19 15:55:14 +01:00
|
|
|
func (i *Image) isInvalidated(graphicsDriver graphicsdriver.Graphics) (bool, error) {
|
2022-09-13 05:15:14 +02:00
|
|
|
// IsInvalidated flushes the commands internally.
|
|
|
|
return i.image.IsInvalidated(graphicsDriver)
|
2016-09-03 17:07:06 +02:00
|
|
|
}
|
2019-07-19 18:42:19 +02:00
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) (string, error) {
|
2022-03-19 15:55:14 +01:00
|
|
|
return i.image.Dump(graphicsDriver, path, blackbg, rect)
|
2019-07-19 18:42:19 +02:00
|
|
|
}
|
2021-09-09 05:57:47 +02:00
|
|
|
|
|
|
|
func (i *Image) clearDrawTrianglesHistory() {
|
|
|
|
// Clear the items explicitly, or the references might remain (#1803).
|
|
|
|
for idx := range i.drawTrianglesHistory {
|
|
|
|
i.drawTrianglesHistory[idx] = nil
|
|
|
|
}
|
|
|
|
i.drawTrianglesHistory = i.drawTrianglesHistory[:0]
|
|
|
|
}
|
2022-04-01 17:32:16 +02:00
|
|
|
|
|
|
|
func (i *Image) InternalSize() (int, int) {
|
|
|
|
return i.image.InternalSize()
|
|
|
|
}
|
2023-02-25 05:50:02 +01:00
|
|
|
|
2023-04-08 19:31:22 +02:00
|
|
|
func (i *Image) appendRegionsForDrawTriangles(regions *[]image.Rectangle) {
|
2023-02-24 13:49:33 +01:00
|
|
|
for _, d := range i.drawTrianglesHistory {
|
|
|
|
r := regionToRectangle(d.dstRegion)
|
|
|
|
if r.Empty() {
|
|
|
|
continue
|
|
|
|
}
|
2023-04-08 19:31:22 +02:00
|
|
|
appendRegionRemovingDuplicates(regions, r)
|
2023-02-24 13:49:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-25 05:50:02 +01:00
|
|
|
func regionToRectangle(region graphicsdriver.Region) image.Rectangle {
|
|
|
|
return image.Rect(
|
|
|
|
int(math.Floor(float64(region.X))),
|
|
|
|
int(math.Floor(float64(region.Y))),
|
|
|
|
int(math.Ceil(float64(region.X+region.Width))),
|
|
|
|
int(math.Ceil(float64(region.Y+region.Height))))
|
|
|
|
}
|
2023-03-09 09:13:38 +01:00
|
|
|
|
2023-04-08 19:31:22 +02:00
|
|
|
// appendRegionRemovingDuplicates adds a region to a given list of regions,
|
|
|
|
// but removes any duplicate between the newly added region and any existing regions.
|
|
|
|
//
|
|
|
|
// In case the newly added region is fully contained in any pre-existing region, this function does nothing.
|
|
|
|
// Otherwise, any pre-existing regions that are fully contained in the newly added region are removed.
|
|
|
|
//
|
|
|
|
// This is done to avoid unnecessary reading pixels from GPU.
|
|
|
|
func appendRegionRemovingDuplicates(regions *[]image.Rectangle, region image.Rectangle) {
|
|
|
|
for _, r := range *regions {
|
|
|
|
if region.In(r) {
|
|
|
|
// The newly added rectangle is fully contained in one of the input regions.
|
|
|
|
// Nothing to add.
|
|
|
|
return
|
2023-03-09 09:13:38 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-08 19:31:22 +02:00
|
|
|
// Separate loop, as regions must not get mutated before above return.
|
2023-03-09 09:13:38 +01:00
|
|
|
n := 0
|
2023-04-08 19:31:22 +02:00
|
|
|
for _, r := range *regions {
|
|
|
|
if r.In(region) {
|
2023-03-09 09:13:38 +01:00
|
|
|
continue
|
|
|
|
}
|
2023-04-08 19:31:22 +02:00
|
|
|
(*regions)[n] = r
|
2023-03-09 09:13:38 +01:00
|
|
|
n++
|
|
|
|
}
|
2023-04-08 19:31:22 +02:00
|
|
|
*regions = append((*regions)[:n], region)
|
2023-03-09 09:13:38 +01:00
|
|
|
}
|