2018-03-03 10:51:52 +01:00
|
|
|
// Copyright 2018 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-03-10 15:48:10 +01:00
|
|
|
package shareable
|
2018-03-03 10:51:52 +01:00
|
|
|
|
|
|
|
import (
|
2018-04-28 14:49:00 +02:00
|
|
|
"image"
|
2018-03-10 15:27:16 +01:00
|
|
|
"image/color"
|
2018-03-10 16:07:32 +01:00
|
|
|
"runtime"
|
2018-05-09 16:41:06 +02:00
|
|
|
"sync"
|
2018-03-10 15:27:16 +01:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/affine"
|
2018-10-28 12:25:52 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
2018-03-05 16:38:56 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/packing"
|
2018-03-03 10:51:52 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/restorable"
|
|
|
|
)
|
|
|
|
|
2018-11-12 16:00:10 +01:00
|
|
|
const (
|
|
|
|
initSize = 1024
|
|
|
|
maxSize = 4096
|
|
|
|
)
|
|
|
|
|
2018-03-10 16:02:23 +01:00
|
|
|
type backend struct {
|
2018-03-03 10:51:52 +01:00
|
|
|
restorable *restorable.Image
|
2018-03-15 17:21:33 +01:00
|
|
|
|
|
|
|
// If page is nil, the backend is not shared.
|
|
|
|
page *packing.Page
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
2018-03-25 15:41:15 +02:00
|
|
|
func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) {
|
2018-12-03 18:23:25 +01:00
|
|
|
// If the region is allocated without any extension, it's fine.
|
2018-03-25 15:41:15 +02:00
|
|
|
if n := b.page.Alloc(width, height); n != nil {
|
|
|
|
return n, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simulate the extending the page and calculate the appropriate page size.
|
|
|
|
page := b.page.Clone()
|
|
|
|
nExtended := 0
|
|
|
|
for {
|
|
|
|
if !page.Extend() {
|
|
|
|
// The page can't be extended any more. Return as failure.
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
nExtended++
|
|
|
|
if n := page.Alloc(width, height); n != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < nExtended; i++ {
|
|
|
|
b.page.Extend()
|
|
|
|
}
|
|
|
|
s := b.page.Size()
|
|
|
|
newImg := restorable.NewImage(s, s, false)
|
|
|
|
oldImg := b.restorable
|
|
|
|
w, h := oldImg.Size()
|
2018-12-20 15:24:26 +01:00
|
|
|
// Do not use DrawImage 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 DrawImage (#593, 758).
|
|
|
|
// TODO: Add validations to ensure that an image cannot accept DrawImage after ReplacePixels on a part of
|
|
|
|
// it.
|
|
|
|
newImg.ReplacePixels(oldImg.Pixels(), 0, 0, w, h)
|
2018-03-25 15:41:15 +02:00
|
|
|
oldImg.Dispose()
|
|
|
|
b.restorable = newImg
|
|
|
|
|
|
|
|
n := b.page.Alloc(width, height)
|
|
|
|
if n == nil {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
return n, true
|
|
|
|
}
|
|
|
|
|
2018-03-03 10:51:52 +01:00
|
|
|
var (
|
2018-03-10 16:28:30 +01:00
|
|
|
// backendsM is a mutex for critical sections of the backend and packing.Node objects.
|
|
|
|
backendsM sync.Mutex
|
|
|
|
|
2018-03-10 16:02:23 +01:00
|
|
|
// theBackends is a set of actually shared images.
|
|
|
|
theBackends = []*backend{}
|
2018-03-03 10:51:52 +01:00
|
|
|
)
|
|
|
|
|
2018-03-10 16:05:06 +01:00
|
|
|
type Image struct {
|
2018-04-29 20:26:58 +02:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
disposed bool
|
2018-04-29 11:51:48 +02:00
|
|
|
|
2018-03-10 16:02:23 +01:00
|
|
|
backend *backend
|
2018-03-10 15:14:59 +01:00
|
|
|
|
2018-07-21 21:32:36 +02:00
|
|
|
node *packing.Node
|
|
|
|
countForShare int
|
2018-07-24 15:25:15 +02:00
|
|
|
|
|
|
|
neverShared bool
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
|
|
|
|
2018-07-21 23:27:57 +02:00
|
|
|
func (i *Image) moveTo(dst *Image) {
|
|
|
|
dst.dispose(false)
|
|
|
|
*dst = *i
|
|
|
|
|
|
|
|
// i is no longer available but Dispose must not be called
|
|
|
|
// since i and dst have the same values like node.
|
|
|
|
runtime.SetFinalizer(i, nil)
|
|
|
|
}
|
|
|
|
|
2018-06-25 18:59:12 +02:00
|
|
|
func (i *Image) isShared() bool {
|
|
|
|
return i.node != nil
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:40:24 +02:00
|
|
|
func (i *Image) IsSharedForTesting() bool {
|
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
return i.isShared()
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +02:00
|
|
|
func (i *Image) ensureNotShared() {
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend == nil {
|
2018-04-29 11:51:48 +02:00
|
|
|
i.allocate(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-25 18:59:12 +02:00
|
|
|
if !i.isShared() {
|
2018-03-10 15:14:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +02:00
|
|
|
x, y, w, h := i.region()
|
2018-03-10 15:14:59 +01:00
|
|
|
newImg := restorable.NewImage(w, h, false)
|
2018-06-10 15:59:00 +02:00
|
|
|
vw, vh := i.backend.restorable.Size()
|
2018-10-28 15:03:06 +01:00
|
|
|
vs := graphics.QuadVertices(vw, vh, x, y, x+w, y+h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
|
|
|
|
is := graphics.QuadIndices()
|
2018-12-23 19:00:00 +01:00
|
|
|
newImg.DrawImage(i.backend.restorable, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
2018-03-10 15:14:59 +01:00
|
|
|
|
2018-05-03 04:49:55 +02:00
|
|
|
i.dispose(false)
|
2018-04-08 19:18:46 +02:00
|
|
|
i.backend = &backend{
|
2018-03-10 15:14:59 +01:00
|
|
|
restorable: newImg,
|
|
|
|
}
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
2018-07-11 18:40:24 +02:00
|
|
|
func (i *Image) forceShared() {
|
|
|
|
if i.backend == nil {
|
2018-07-29 16:49:53 +02:00
|
|
|
i.allocate(true)
|
2018-07-11 18:40:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.isShared() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !i.shareable() {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
|
|
|
|
newI := NewImage(i.width, i.height)
|
|
|
|
pixels := make([]byte, 4*i.width*i.height)
|
|
|
|
for y := 0; y < i.height; y++ {
|
|
|
|
for x := 0; x < i.width; x++ {
|
2018-07-29 14:59:18 +02:00
|
|
|
c := i.at(x, y)
|
2018-07-11 18:40:24 +02:00
|
|
|
pixels[4*(x+i.width*y)] = c.R
|
|
|
|
pixels[4*(x+i.width*y)+1] = c.G
|
|
|
|
pixels[4*(x+i.width*y)+2] = c.B
|
|
|
|
pixels[4*(x+i.width*y)+3] = c.A
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newI.replacePixels(pixels)
|
2018-07-21 23:27:57 +02:00
|
|
|
newI.moveTo(i)
|
|
|
|
i.countForShare = 0
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +02:00
|
|
|
func (i *Image) region() (x, y, width, height int) {
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend == nil {
|
2018-04-29 11:51:48 +02:00
|
|
|
panic("not reached")
|
|
|
|
}
|
2018-06-25 18:59:12 +02:00
|
|
|
if !i.isShared() {
|
2018-04-08 19:18:46 +02:00
|
|
|
w, h := i.backend.restorable.Size()
|
2018-03-10 15:14:59 +01:00
|
|
|
return 0, 0, w, h
|
|
|
|
}
|
2018-04-08 19:18:46 +02:00
|
|
|
return i.node.Region()
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +02:00
|
|
|
func (i *Image) Size() (width, height int) {
|
2018-04-29 11:51:48 +02:00
|
|
|
return i.width, i.height
|
2018-03-10 15:36:07 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 20:33:28 +02:00
|
|
|
func (i *Image) QuadVertices(sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32) []float32 {
|
2018-07-20 20:30:27 +02:00
|
|
|
if i.backend == nil {
|
|
|
|
i.allocate(true)
|
|
|
|
}
|
2018-08-02 19:50:11 +02:00
|
|
|
ox, oy, _, _ := i.region()
|
2018-07-20 20:30:27 +02:00
|
|
|
w, h := i.backend.restorable.SizePowerOf2()
|
2018-10-28 15:03:06 +01:00
|
|
|
return graphics.QuadVertices(w, h, sx0+ox, sy0+oy, sx1+ox, sy1+oy, a, b, c, d, tx, ty, cr, cg, cb, ca)
|
2018-07-20 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 18:35:50 +01:00
|
|
|
func (i *Image) PutVertex(dest []float32, dx, dy, sx, sy float32, bx0, by0, bx1, by1 float32,
|
|
|
|
cr, cg, cb, ca float32) {
|
2018-06-12 03:33:09 +02:00
|
|
|
if i.backend == nil {
|
|
|
|
i.allocate(true)
|
|
|
|
}
|
|
|
|
ox, oy, _, _ := i.region()
|
2018-12-22 18:35:50 +01:00
|
|
|
oxf, oyf := float32(ox), float32(oy)
|
2018-06-12 03:33:09 +02:00
|
|
|
w, h := i.backend.restorable.SizePowerOf2()
|
2018-12-22 18:35:50 +01:00
|
|
|
|
|
|
|
su := (sx + oxf) / float32(w)
|
|
|
|
sv := (sy + oyf) / float32(h)
|
|
|
|
u0 := (bx0 + oxf) / float32(w)
|
|
|
|
v0 := (by0 + oyf) / float32(h)
|
|
|
|
u1 := (bx1 + oxf) / float32(w)
|
|
|
|
v1 := (by1 + oyf) / float32(h)
|
|
|
|
|
|
|
|
graphics.PutVertex(dest, w, h, dx, dy, su, sv, u0, v0, u1, v1, cr, cg, cb, ca)
|
2018-06-12 03:33:09 +02:00
|
|
|
}
|
|
|
|
|
2018-07-21 21:32:36 +02:00
|
|
|
const MaxCountForShare = 10
|
2018-07-11 18:40:24 +02:00
|
|
|
|
2018-12-23 19:00:00 +01:00
|
|
|
func (i *Image) DrawImage(img *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter, address graphics.Address) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
2018-04-29 11:51:48 +02:00
|
|
|
|
2018-04-29 20:34:35 +02:00
|
|
|
if img.disposed {
|
|
|
|
panic("shareable: the drawing source image must not be disposed")
|
|
|
|
}
|
|
|
|
if i.disposed {
|
|
|
|
panic("shareable: the drawing target image must not be disposed")
|
|
|
|
}
|
2018-04-29 20:26:58 +02:00
|
|
|
if img.backend == nil {
|
2018-04-29 11:51:48 +02:00
|
|
|
img.allocate(true)
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +02:00
|
|
|
i.ensureNotShared()
|
2018-03-10 15:48:10 +01:00
|
|
|
|
|
|
|
// Compare i and img after ensuring i is not shared, or
|
|
|
|
// i and img might share the same texture even though i != img.
|
2018-04-08 19:18:46 +02:00
|
|
|
if i.backend.restorable == img.backend.restorable {
|
2018-03-10 15:48:10 +01:00
|
|
|
panic("shareable: Image.DrawImage: img must be different from the receiver")
|
|
|
|
}
|
|
|
|
|
2018-12-23 19:00:00 +01:00
|
|
|
i.backend.restorable.DrawImage(img.backend.restorable, vertices, indices, colorm, mode, filter, address)
|
2018-07-11 18:40:24 +02:00
|
|
|
|
2018-07-21 21:32:36 +02:00
|
|
|
i.countForShare = 0
|
2018-08-08 11:40:50 +02:00
|
|
|
|
|
|
|
// TODO: Reusing shared images is temporarily suspended for performance. See #661.
|
|
|
|
//
|
|
|
|
// if !img.isShared() && img.shareable() {
|
|
|
|
// img.countForShare++
|
|
|
|
// if img.countForShare >= MaxCountForShare {
|
|
|
|
// img.forceShared()
|
|
|
|
// img.countForShare = 0
|
|
|
|
// }
|
|
|
|
// }
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +02:00
|
|
|
func (i *Image) ReplacePixels(p []byte) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
2018-07-11 18:40:24 +02:00
|
|
|
i.replacePixels(p)
|
|
|
|
}
|
2018-03-10 16:28:30 +01:00
|
|
|
|
2018-07-11 18:40:24 +02:00
|
|
|
func (i *Image) replacePixels(p []byte) {
|
2018-04-29 20:34:35 +02:00
|
|
|
if i.disposed {
|
|
|
|
panic("shareable: the image must not be disposed")
|
|
|
|
}
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend == nil {
|
2018-08-08 18:16:46 +02:00
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-04-29 11:51:48 +02:00
|
|
|
i.allocate(true)
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +02:00
|
|
|
x, y, w, h := i.region()
|
2018-08-08 18:16:46 +02:00
|
|
|
if p != nil {
|
|
|
|
if l := 4 * w * h; len(p) != l {
|
2018-11-28 22:24:04 +01:00
|
|
|
panic("not reached")
|
2018-08-08 18:16:46 +02:00
|
|
|
}
|
2018-03-10 15:36:07 +01:00
|
|
|
}
|
2018-04-08 19:18:46 +02:00
|
|
|
i.backend.restorable.ReplacePixels(p, x, y, w, h)
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 14:59:18 +02:00
|
|
|
func (i *Image) At(x, y int) color.RGBA {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
2018-04-05 05:02:08 +02:00
|
|
|
defer backendsM.Unlock()
|
2018-07-11 18:40:24 +02:00
|
|
|
return i.at(x, y)
|
|
|
|
}
|
2018-03-10 16:28:30 +01:00
|
|
|
|
2018-07-29 14:59:18 +02:00
|
|
|
func (i *Image) at(x, y int) color.RGBA {
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend == nil {
|
2018-07-11 19:11:18 +02:00
|
|
|
return color.RGBA{}
|
2018-04-29 11:51:48 +02:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +02:00
|
|
|
ox, oy, w, h := i.region()
|
2018-03-10 15:36:07 +01:00
|
|
|
if x < 0 || y < 0 || x >= w || y >= h {
|
2018-07-11 19:11:18 +02:00
|
|
|
return color.RGBA{}
|
2018-03-10 15:36:07 +01:00
|
|
|
}
|
2018-03-10 16:28:30 +01:00
|
|
|
|
2018-07-11 19:11:18 +02:00
|
|
|
return i.backend.restorable.At(x+ox, y+oy)
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +02:00
|
|
|
func (i *Image) Dispose() {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
2018-05-03 04:49:55 +02:00
|
|
|
i.dispose(true)
|
2018-03-10 16:28:30 +01:00
|
|
|
}
|
|
|
|
|
2018-05-03 04:49:55 +02:00
|
|
|
func (i *Image) dispose(markDisposed bool) {
|
2018-03-10 15:14:59 +01:00
|
|
|
defer func() {
|
2018-05-03 04:49:55 +02:00
|
|
|
if markDisposed {
|
|
|
|
i.disposed = true
|
|
|
|
}
|
2018-04-08 19:18:46 +02:00
|
|
|
i.backend = nil
|
|
|
|
i.node = nil
|
2018-07-21 22:40:04 +02:00
|
|
|
if markDisposed {
|
|
|
|
runtime.SetFinalizer(i, nil)
|
|
|
|
}
|
2018-03-10 15:14:59 +01:00
|
|
|
}()
|
|
|
|
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.disposed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.backend == nil {
|
|
|
|
// Not allocated yet.
|
2018-04-25 18:51:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-25 18:59:12 +02:00
|
|
|
if !i.isShared() {
|
2018-04-08 19:18:46 +02:00
|
|
|
i.backend.restorable.Dispose()
|
2018-03-10 15:14:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +02:00
|
|
|
i.backend.page.Free(i.node)
|
|
|
|
if !i.backend.page.IsEmpty() {
|
2018-04-05 17:36:15 +02:00
|
|
|
// As this part can be reused, this should be cleared explicitly.
|
2018-12-01 11:31:32 +01:00
|
|
|
x, y, w, h := i.region()
|
2018-11-30 00:59:31 +01:00
|
|
|
// TODO: Now nil cannot be used here (see the test result). Fix this.
|
|
|
|
i.backend.restorable.ReplacePixels(make([]byte, 4*w*h), x, y, w, h)
|
2018-03-10 15:14:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +02:00
|
|
|
i.backend.restorable.Dispose()
|
2018-03-10 15:14:59 +01:00
|
|
|
index := -1
|
2018-04-08 19:18:46 +02:00
|
|
|
for idx, sh := range theBackends {
|
|
|
|
if sh == i.backend {
|
|
|
|
index = idx
|
2018-03-10 15:14:59 +01:00
|
|
|
break
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-10 15:14:59 +01:00
|
|
|
if index == -1 {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
2018-03-10 16:02:23 +01:00
|
|
|
theBackends = append(theBackends[:index], theBackends[index+1:]...)
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 16:54:46 +02:00
|
|
|
func (i *Image) IsVolatile() bool {
|
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
if i.backend == nil {
|
|
|
|
// Not allocated yet. Only non-volatile images can do lazy allocation so far.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return i.backend.restorable.IsVolatile()
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:18:46 +02:00
|
|
|
func (i *Image) IsInvalidated() (bool, error) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
2018-04-05 05:02:08 +02:00
|
|
|
defer backendsM.Unlock()
|
2018-04-08 19:18:46 +02:00
|
|
|
v, err := i.backend.restorable.IsInvalidated()
|
2018-03-10 16:28:30 +01:00
|
|
|
return v, err
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 16:05:06 +01:00
|
|
|
func NewImage(width, height int) *Image {
|
2018-04-29 11:51:48 +02:00
|
|
|
// Actual allocation is done lazily.
|
|
|
|
return &Image{
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:40:24 +02:00
|
|
|
func (i *Image) shareable() bool {
|
2018-07-24 15:25:15 +02:00
|
|
|
if i.neverShared {
|
|
|
|
return false
|
|
|
|
}
|
2018-07-11 18:40:24 +02:00
|
|
|
return i.width <= maxSize && i.height <= maxSize
|
|
|
|
}
|
|
|
|
|
2018-04-29 11:51:48 +02:00
|
|
|
func (i *Image) allocate(shareable bool) {
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend != nil {
|
2018-04-29 12:10:36 +02:00
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:40:24 +02:00
|
|
|
if !shareable || !i.shareable() {
|
2018-04-29 11:51:48 +02:00
|
|
|
i.backend = &backend{
|
|
|
|
restorable: restorable.NewImage(i.width, i.height, false),
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
2018-06-28 15:23:01 +02:00
|
|
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
2018-04-29 11:51:48 +02:00
|
|
|
return
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
2018-03-10 15:14:59 +01:00
|
|
|
|
2018-03-23 20:27:10 +01:00
|
|
|
for _, b := range theBackends {
|
2018-04-29 11:51:48 +02:00
|
|
|
if n, ok := b.TryAlloc(i.width, i.height); ok {
|
|
|
|
i.backend = b
|
|
|
|
i.node = n
|
2018-06-28 15:23:01 +02:00
|
|
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
2018-04-29 11:51:48 +02:00
|
|
|
return
|
2018-03-10 17:30:24 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-25 15:41:15 +02:00
|
|
|
size := initSize
|
2018-04-29 11:51:48 +02:00
|
|
|
for i.width > size || i.height > size {
|
2018-03-25 15:41:15 +02:00
|
|
|
if size == maxSize {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
size *= 2
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:27:10 +01:00
|
|
|
b := &backend{
|
2018-03-25 15:41:15 +02:00
|
|
|
restorable: restorable.NewImage(size, size, false),
|
|
|
|
page: packing.NewPage(size, maxSize),
|
2018-03-06 18:18:08 +01:00
|
|
|
}
|
2018-03-23 20:27:10 +01:00
|
|
|
theBackends = append(theBackends, b)
|
2018-03-09 08:02:57 +01:00
|
|
|
|
2018-04-29 11:51:48 +02:00
|
|
|
n := b.page.Alloc(i.width, i.height)
|
2018-03-03 10:51:52 +01:00
|
|
|
if n == nil {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
2018-04-29 11:51:48 +02:00
|
|
|
i.backend = b
|
|
|
|
i.node = n
|
2018-03-10 16:07:32 +01:00
|
|
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
2018-04-29 11:51:48 +02:00
|
|
|
return
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
2018-03-10 15:48:10 +01:00
|
|
|
|
2018-03-10 16:05:06 +01:00
|
|
|
func NewVolatileImage(width, height int) *Image {
|
2018-03-25 16:37:32 +02:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
|
2018-03-10 15:48:10 +01:00
|
|
|
r := restorable.NewImage(width, height, true)
|
2018-03-10 16:07:32 +01:00
|
|
|
i := &Image{
|
2018-04-29 20:26:58 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
2018-03-10 16:02:23 +01:00
|
|
|
backend: &backend{
|
2018-03-10 15:48:10 +01:00
|
|
|
restorable: r,
|
|
|
|
},
|
2018-07-24 15:25:15 +02:00
|
|
|
neverShared: true,
|
2018-03-10 15:48:10 +01:00
|
|
|
}
|
2018-03-10 16:07:32 +01:00
|
|
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
|
|
|
return i
|
2018-03-10 15:48:10 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 16:05:06 +01:00
|
|
|
func NewScreenFramebufferImage(width, height int) *Image {
|
2018-03-25 16:37:32 +02:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
|
2018-03-10 15:48:10 +01:00
|
|
|
r := restorable.NewScreenFramebufferImage(width, height)
|
2018-03-10 16:07:32 +01:00
|
|
|
i := &Image{
|
2018-04-29 20:26:58 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
2018-03-10 16:02:23 +01:00
|
|
|
backend: &backend{
|
2018-03-10 15:48:10 +01:00
|
|
|
restorable: r,
|
|
|
|
},
|
2018-07-24 15:25:15 +02:00
|
|
|
neverShared: true,
|
2018-03-10 15:48:10 +01:00
|
|
|
}
|
2018-03-10 16:07:32 +01:00
|
|
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
|
|
|
return i
|
2018-03-10 15:48:10 +01:00
|
|
|
}
|
2018-03-25 16:37:32 +02:00
|
|
|
|
2018-10-31 19:02:08 +01:00
|
|
|
func InitializeGraphicsDriverState() error {
|
2018-03-25 16:37:32 +02:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
2018-10-31 19:02:08 +01:00
|
|
|
return restorable.InitializeGraphicsDriverState()
|
2018-03-25 16:37:32 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 19:11:18 +02:00
|
|
|
func ResolveStaleImages() {
|
2018-03-25 16:37:32 +02:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
2018-07-11 19:11:18 +02:00
|
|
|
restorable.ResolveStaleImages()
|
2018-03-25 16:37:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsRestoringEnabled() bool {
|
2018-03-25 17:13:26 +02:00
|
|
|
// As IsRestoringEnabled is an immutable state, no need to lock here.
|
2018-03-25 16:37:32 +02:00
|
|
|
return restorable.IsRestoringEnabled()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Restore() error {
|
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
return restorable.Restore()
|
|
|
|
}
|
2018-04-05 18:12:08 +02:00
|
|
|
|
2018-07-11 19:11:18 +02:00
|
|
|
func Images() []image.Image {
|
2018-04-28 14:49:00 +02:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
return restorable.Images()
|
|
|
|
}
|
2018-07-11 19:11:18 +02:00
|
|
|
|
|
|
|
func Error() error {
|
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
return restorable.Error()
|
|
|
|
}
|