ebiten/internal/restorable/image.go

678 lines
20 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 (
"fmt"
"image"
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"
"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 {
pixelsRecords *pixelsRecords
2019-02-12 15:31:52 +01:00
}
// Apply applies the Pixels state to the given image especially for restoring.
func (p *Pixels) Apply(img *graphicscommand.Image) {
// Pixels doesn't clear the image. This is a caller's responsibility.
if p.pixelsRecords == nil {
return
}
p.pixelsRecords.apply(img)
2019-02-12 15:31:52 +01:00
}
func (p *Pixels) AddOrReplace(pix []byte, x, y, width, height int) {
if p.pixelsRecords == nil {
p.pixelsRecords = &pixelsRecords{}
2019-02-12 15:31:52 +01:00
}
p.pixelsRecords.addOrReplace(pix, x, y, width, height)
}
func (p *Pixels) Clear(x, y, width, height int) {
// 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.
if p.pixelsRecords == nil {
return
}
p.pixelsRecords.clear(x, y, width, height)
}
func (p *Pixels) ReadPixels(pixels []byte, x, y, width, height, imageWidth, imageHeight int) {
if p.pixelsRecords == nil {
for i := range pixels {
pixels[i] = 0
}
return
2019-02-12 15:31:52 +01:00
}
p.pixelsRecords.readPixels(pixels, x, y, width, height, imageWidth, imageHeight)
2019-02-12 15:31:52 +01:00
}
func (p *Pixels) Region() image.Rectangle {
if p.pixelsRecords == nil {
return image.Rectangle{}
}
return p.pixelsRecords.region()
}
// drawTrianglesHistoryItem is an item for history of draw-image commands.
type drawTrianglesHistoryItem struct {
images [graphics.ShaderImageCount]*Image
offsets [graphics.ShaderImageCount - 1][2]float32
vertices []float32
indices []uint16
blend graphicsdriver.Blend
dstRegion graphicsdriver.Region
srcRegion graphicsdriver.Region
shader *Shader
uniforms [][]uint32
evenOdd bool
2016-07-13 19:35:20 +02:00
}
type ImageType int
const (
// ImageTypeRegular indicates the image is a regular image.
ImageTypeRegular ImageType = iota
// ImageTypeScreen indicates the image is used as an actual screen.
ImageTypeScreen
// ImageTypeVolatile indicates the image is cleared whenever a frame starts.
//
// 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
// reading pixels from GPU are expensive operations. Volatile images can skip such oprations, but the image content
// is cleared every frame instead.
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
width int
height int
basePixels Pixels
2017-09-19 18:35:56 +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).
drawTrianglesHistory []*drawTrianglesHistoryItem
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
// staleRegion indicates the region to restore.
// staleRegion is valid only when stale is true.
staleRegion image.Rectangle
// pixelsForRestore is a cached byte slice for pixels.
// pixelsForRestore is just a cache to avoid allocations, and the data might not be reliable.
//
// pixelsForRestore might be shared by the records of basePixels.
// pixelsForRestore should not be modified until basePixels is invalidated.
//
// pixelsForRestore is an entire pixels of the image or nil.
//
// pixelsForRestore is for an optimization to reduce slice allocations (#2375).
pixelsForRestore []byte
imageType ImageType
// priority indicates whether the image is restored in high priority when context-lost happens.
priority bool
2016-07-13 19:35:20 +02:00
}
2022-10-21 08:23:09 +02:00
var whiteImage *Image
2022-10-21 08:23:09 +02:00
func ensureWhiteImage() *Image {
if whiteImage != nil {
return whiteImage
}
2022-10-21 08:23:09 +02:00
// Initialize the white image lazily. Some functions like needsRestoring might not work at the initial phase.
2022-10-21 08:23:09 +02:00
// w and h are the white image's size. They indicate the 1x1 image with 1px padding around.
const w, h = 3, 3
2022-10-21 08:23:09 +02:00
whiteImage = &Image{
image: graphicscommand.NewImage(w, h, false),
width: w,
height: h,
priority: true,
}
pix := make([]byte, 4*w*h)
for i := range pix {
pix[i] = 0xff
}
2019-07-15 19:16:00 +02:00
2022-10-21 08:23:09 +02:00
// As whiteImage is the source at clearImage, initialize this with WritePixels, not clearImage.
// This operation is also important when restoring whiteImage.
whiteImage.WritePixels(pix, 0, 0, w, h)
theImages.add(whiteImage)
return whiteImage
}
2022-10-21 08:23:09 +02:00
// NewImage creates an white image with the given size.
//
// The returned image is cleared.
//
// Note that Dispose is not called automatically.
func NewImage(width, height int, imageType ImageType) *Image {
if !graphicsDriverInitialized {
panic("restorable: graphics driver must be ready at NewImage but not")
}
2017-05-03 16:15:18 +02:00
i := &Image{
image: graphicscommand.NewImage(width, height, imageType == ImageTypeScreen),
width: width,
height: height,
imageType: imageType,
}
clearImage(i.image)
2017-05-03 16:15:18 +02:00
theImages.add(i)
return i
}
// 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 {
if i.width >= width && i.height >= height {
return i
}
newImg := NewImage(width, height, i.imageType)
// Use DrawTriangles instead of WritePixels because the image i might be stale and not have its pixels
// information.
srcs := [graphics.ShaderImageCount]*Image{i}
var offsets [graphics.ShaderImageCount - 1][2]float32
sw, sh := i.image.InternalSize()
vs := quadVertices(i, 0, 0, float32(sw), float32(sh), 0, 0, float32(sw), float32(sh), 1, 1, 1, 1)
is := graphics.QuadIndices()
dr := graphicsdriver.Region{
X: 0,
Y: 0,
Width: float32(sw),
Height: float32(sh),
}
newImg.DrawTriangles(srcs, offsets, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, NearestFilterShader, nil, false)
2022-09-13 19:20:11 +02:00
// Overwrite the history as if the image newImg is created only by WritePixels.
newImg.clearDrawTrianglesHistory()
newImg.basePixels = i.basePixels
newImg.stale = i.stale
newImg.staleRegion = i.staleRegion
i.Dispose()
return newImg
}
// quadVertices returns vertices to render a quad. These values are passed to graphicscommand.Image.
func quadVertices(src *Image, dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1, cr, cg, cb, ca float32) []float32 {
sw, sh := src.InternalSize()
swf, shf := float32(sw), float32(sh)
return []float32{
dx0, dy0, sx0 / swf, sy0 / shf, cr, cg, cb, ca,
dx1, dy0, sx1 / swf, sy0 / shf, cr, cg, cb, ca,
dx0, dy1, sx0 / swf, sy1 / shf, cr, cg, cb, ca,
dx1, dy1, sx1 / swf, sy1 / shf, cr, cg, cb, ca,
}
}
func clearImage(i *graphicscommand.Image) {
2022-10-21 08:23:09 +02:00
whiteImage := ensureWhiteImage()
2022-10-21 08:23:09 +02:00
if i == whiteImage.image {
panic("restorable: fillImage cannot be called on whiteImage")
}
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.
dw, dh := i.InternalSize()
2022-10-21 08:23:09 +02:00
sw, sh := whiteImage.width, whiteImage.height
vs := quadVertices(whiteImage, 0, 0, float32(dw), float32(dh), 1, 1, float32(sw-1), float32(sh-1), 0, 0, 0, 0)
is := graphics.QuadIndices()
2022-10-21 08:23:09 +02:00
srcs := [graphics.ShaderImageCount]*graphicscommand.Image{whiteImage.image}
var offsets [graphics.ShaderImageCount - 1][2]float32
dstRegion := graphicsdriver.Region{
X: 0,
Y: 0,
Width: float32(dw),
Height: float32(dh),
}
i.DrawTriangles(srcs, offsets, vs, is, graphicsdriver.BlendClear, dstRegion, graphicsdriver.Region{}, NearestFilterShader.shader, nil, false)
}
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 {
return &i.basePixels
2017-05-31 18:27:56 +02:00
}
2017-09-14 17:24:18 +02:00
// makeStale makes the image stale.
func (i *Image) makeStale(rect image.Rectangle) {
i.stale = true
i.staleRegion = i.staleRegion.Union(i.basePixels.Region()).Union(rect)
i.basePixels = Pixels{}
i.clearDrawTrianglesHistory()
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
}
// ClearPixels clears the specified region by WritePixels.
func (i *Image) ClearPixels(x, y, width, height int) {
i.WritePixels(nil, x, y, width, height)
}
func (i *Image) needsRestoring() bool {
return i.imageType == ImageTypeRegular
}
// WritePixels replaces the image pixels with the given pixels slice.
//
// The specified region must not be overlapped with other regions by WritePixels.
func (i *Image) WritePixels(pixels []byte, x, y, width, height int) {
if width <= 0 || height <= 0 {
panic("restorable: width/height must be positive")
}
2019-09-20 22:40:05 +02:00
w, h := i.width, i.height
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)
// 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
if pixels != nil {
i.image.WritePixels(pixels, x, y, width, height)
} else {
// TODO: When pixels == nil, we don't have to care the pixel state there. In such cases, the image
// accepts only WritePixels and not Fill or DrawTriangles.
// TODO: Separate Image struct into two: images for WritePixels-only, and the others.
i.image.WritePixels(make([]byte, 4*width*height), x, y, width, height)
}
// Even if the image is already stale, call makeStale to extend the stale region.
if !needsRestoring() || !i.needsRestoring() || i.stale {
i.makeStale(image.Rect(x, y, x+width, y+height))
return
}
if x == 0 && y == 0 && width == w && height == h {
if pixels != nil {
// pixels can point to a shared region.
// This function is responsible to copy this.
copiedPixels := make([]byte, len(pixels))
copy(copiedPixels, pixels)
i.basePixels.AddOrReplace(copiedPixels, 0, 0, w, h)
} else {
i.basePixels.Clear(0, 0, w, h)
}
i.clearDrawTrianglesHistory()
i.stale = false
i.staleRegion = image.Rectangle{}
return
}
2018-12-26 19:06:44 +01:00
2022-09-13 19:20:11 +02:00
// Records for DrawTriangles cannot come after records for WritePixels.
if len(i.drawTrianglesHistory) > 0 {
i.makeStale(image.Rect(0, 0, i.width, i.height))
return
}
2018-12-26 19:06:44 +01:00
if pixels != nil {
// pixels can point to a shared region.
// This function is responsible to copy this.
copiedPixels := make([]byte, len(pixels))
copy(copiedPixels, pixels)
i.basePixels.AddOrReplace(copiedPixels, x, y, width, height)
} else {
i.basePixels.Clear(x, y, width, height)
2019-07-15 20:08:26 +02:00
}
2016-07-26 03:51:48 +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
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) {
2019-01-20 18:17:09 +01:00
if i.priority {
panic("restorable: DrawTriangles cannot be called on a priority image")
2019-01-20 18:17:09 +01:00
}
if len(vertices) == 0 {
return
}
theImages.makeStaleIfDependingOn(i)
// TODO: Add tests to confirm this logic.
var srcstale bool
for _, src := range srcs {
if src == nil {
continue
}
if src.stale || src.imageType == ImageTypeVolatile {
srcstale = true
break
}
}
if srcstale || !needsRestoring() || !i.needsRestoring() {
i.makeStale(image.Rect(0, 0, i.width, i.height))
2016-09-03 16:42:44 +02:00
} else {
i.appendDrawTrianglesHistory(srcs, offsets, vertices, indices, blend, dstRegion, srcRegion, shader, uniforms, evenOdd)
2020-05-24 17:46:23 +02:00
}
var imgs [graphics.ShaderImageCount]*graphicscommand.Image
for i, src := range srcs {
if src == nil {
continue
}
imgs[i] = src.image
}
i.image.DrawTriangles(imgs, offsets, vertices, indices, blend, dstRegion, srcRegion, shader.shader, uniforms, evenOdd)
2016-09-03 16:42:44 +02:00
}
// appendDrawTrianglesHistory appends a draw-image history item to the image.
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) {
if i.stale || !i.needsRestoring() {
2016-07-26 19:16:31 +02:00
return
}
// TODO: Would it be possible to merge draw image history items?
const maxDrawTrianglesHistoryCount = 1024
if len(i.drawTrianglesHistory)+1 > maxDrawTrianglesHistoryCount {
i.makeStale(image.Rect(0, 0, i.width, i.height))
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.
vs := make([]float32, len(vertices))
copy(vs, vertices)
is := make([]uint16, len(indices))
copy(is, indices)
2020-05-24 17:46:23 +02:00
item := &drawTrianglesHistoryItem{
images: srcs,
offsets: offsets,
vertices: vs,
indices: is,
blend: blend,
dstRegion: dstRegion,
srcRegion: srcRegion,
shader: shader,
uniforms: uniforms,
evenOdd: evenOdd,
2016-07-24 19:28:59 +02:00
}
i.drawTrianglesHistory = append(i.drawTrianglesHistory, item)
2016-07-13 19:35:20 +02:00
}
func (i *Image) readPixelsFromGPUIfNeeded(graphicsDriver graphicsdriver.Graphics) error {
if len(i.drawTrianglesHistory) > 0 || i.stale {
if err := i.readPixelsFromGPU(graphicsDriver); err != nil {
return err
}
2018-11-12 16:00:10 +01:00
}
return nil
2018-11-12 16:00:10 +01:00
}
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, x, y, width, height int) error {
if err := i.readPixelsFromGPUIfNeeded(graphicsDriver); err != nil {
return err
}
if got, want := len(pixels), 4*width*height; got != want {
return fmt.Errorf("restorable: len(pixels) must be %d but %d at ReadPixels", want, got)
}
i.basePixels.ReadPixels(pixels, x, y, width, height, i.width, i.height)
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 {
return
2016-07-26 19:16:31 +02:00
}
2017-09-14 17:57:52 +02:00
if i.dependsOn(target) {
i.makeStale(image.Rect(0, 0, i.width, i.height))
2016-07-13 19:35:20 +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) {
i.makeStale(image.Rect(0, 0, i.width, i.height))
}
}
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(graphicsDriver graphicsdriver.Graphics) error {
i.basePixels = Pixels{}
r := i.staleRegion
if len(i.drawTrianglesHistory) > 0 {
r = image.Rect(0, 0, i.width, i.height)
}
if !r.Empty() {
var pix []byte
if needsRestoring() && i.needsRestoring() {
// pixelsForRestore can be reused as basePixels was invalidated.
l := 4 * r.Dx() * r.Dy()
if len(i.pixelsForRestore) < l {
i.pixelsForRestore = make([]byte, l)
}
pix = i.pixelsForRestore[:l]
} else {
pix = make([]byte, 4*r.Dx()*r.Dy())
}
if err := i.image.ReadPixels(graphicsDriver, pix, r.Min.X, r.Min.Y, r.Dx(), r.Dy()); err != nil {
return err
}
i.basePixels.AddOrReplace(pix, r.Min.X, r.Min.Y, r.Dx(), r.Dy())
}
i.clearDrawTrianglesHistory()
2017-09-14 17:57:52 +02:00
i.stale = false
i.staleRegion = image.Rectangle{}
return nil
}
2017-09-14 17:24:18 +02:00
// resolveStale resolves the image's 'stale' state.
func (i *Image) resolveStale(graphicsDriver graphicsdriver.Graphics) error {
if !needsRestoring() {
return nil
}
if !i.needsRestoring() {
return nil
}
2017-09-14 17:57:52 +02:00
if !i.stale {
return nil
2016-07-26 19:20:42 +02:00
}
return i.readPixelsFromGPU(graphicsDriver)
2016-07-26 19:20:42 +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 {
for _, c := range i.drawTrianglesHistory {
for _, img := range c.images {
if img == nil {
continue
}
if img == target {
2020-05-24 17:46:23 +02:00
return true
}
}
}
return false
}
// 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
}
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{}{}
for _, c := range i.drawTrianglesHistory {
for _, img := range c.images {
if img == nil {
continue
}
r[img] = struct{}{}
2020-05-24 17:46:23 +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
}
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.
func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
2019-09-20 22:40:05 +02:00
w, h := i.width, i.height
// Do not dispose the image here. The image should be already disposed.
switch i.imageType {
case ImageTypeScreen:
// The screen image should also be recreated because framebuffer might
// be changed.
i.image = graphicscommand.NewImage(w, h, true)
i.basePixels = Pixels{}
i.clearDrawTrianglesHistory()
2017-09-14 17:57:52 +02:00
i.stale = false
i.staleRegion = image.Rectangle{}
return nil
case ImageTypeVolatile:
i.image = graphicscommand.NewImage(w, h, false)
clearImage(i.image)
return nil
}
2017-09-14 17:57:52 +02:00
if i.stale {
panic("restorable: pixels must not be stale when restoring")
}
gimg := graphicscommand.NewImage(w, h, false)
// Clear the image explicitly.
2022-10-21 08:23:09 +02:00
if i != ensureWhiteImage() {
// As clearImage uses whiteImage, clearImage cannot be called on whiteImage.
// It is OK to skip this since whiteImage has its entire pixel information.
clearImage(gimg)
}
i.basePixels.Apply(gimg)
for _, c := range i.drawTrianglesHistory {
var imgs [graphics.ShaderImageCount]*graphicscommand.Image
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
}
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
}
if len(i.drawTrianglesHistory) > 0 {
i.basePixels = Pixels{}
// As basePixels was invalidated, pixelsForRestore can be reused.
l := 4 * w * h
if len(i.pixelsForRestore) < l {
i.pixelsForRestore = make([]byte, l)
}
pix := i.pixelsForRestore[:l]
if err := gimg.ReadPixels(graphicsDriver, pix, 0, 0, w, h); err != nil {
return err
}
i.basePixels.AddOrReplace(pix, 0, 0, w, h)
2019-02-12 15:31:52 +01:00
}
i.image = gimg
i.clearDrawTrianglesHistory()
2017-09-14 17:57:52 +02:00
i.stale = false
i.staleRegion = image.Rectangle{}
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 = Pixels{}
i.pixelsForRestore = nil
i.clearDrawTrianglesHistory()
2017-09-14 17:57:52 +02:00
i.stale = false
i.staleRegion = image.Rectangle{}
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.
func (i *Image) isInvalidated(graphicsDriver graphicsdriver.Graphics) (bool, error) {
// 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
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) (string, error) {
return i.image.Dump(graphicsDriver, path, blackbg, rect)
2019-07-19 18:42:19 +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]
}
func (i *Image) InternalSize() (int, int) {
return i.image.InternalSize()
}