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 (
|
2019-02-07 09:19:24 +01:00
|
|
|
"fmt"
|
2019-08-24 19:39:59 +02: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"
|
2019-06-22 13:17:52 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
2018-10-28 12:25:52 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
2019-05-11 16:16:05 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/hooks"
|
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"
|
|
|
|
)
|
|
|
|
|
2020-06-14 09:03:26 +02:00
|
|
|
const (
|
2020-06-16 05:48:20 +02:00
|
|
|
// paddingSize represents the size of padding around an image.
|
|
|
|
// Every image or node except for a screen image has its padding.
|
|
|
|
paddingSize = 1
|
2020-06-14 09:03:26 +02:00
|
|
|
)
|
|
|
|
|
2019-06-22 13:17:52 +02:00
|
|
|
var graphicsDriver driver.Graphics
|
|
|
|
|
|
|
|
func SetGraphicsDriver(graphics driver.Graphics) {
|
|
|
|
graphicsDriver = graphics
|
|
|
|
}
|
|
|
|
|
2019-06-23 04:32:43 +02:00
|
|
|
var (
|
|
|
|
minSize = 0
|
|
|
|
maxSize = 0
|
|
|
|
)
|
|
|
|
|
2020-06-27 21:14:52 +02:00
|
|
|
func max(a, b int) int {
|
|
|
|
if a > b {
|
2019-07-03 18:55:55 +02:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2019-05-11 16:16:05 +02:00
|
|
|
func init() {
|
|
|
|
hooks.AppendHookOnBeforeUpdate(func() error {
|
2019-06-22 13:17:52 +02:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
2019-08-12 12:37:20 +02:00
|
|
|
|
|
|
|
resolveDeferred()
|
2020-01-18 17:18:56 +01:00
|
|
|
return makeImagesShared()
|
2019-05-11 16:16:05 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-12 12:37:20 +02:00
|
|
|
func resolveDeferred() {
|
2019-09-21 12:54:39 +02:00
|
|
|
deferredM.Lock()
|
|
|
|
fs := deferred
|
|
|
|
deferred = nil
|
|
|
|
deferredM.Unlock()
|
|
|
|
|
|
|
|
for _, f := range fs {
|
2019-08-12 12:37:20 +02:00
|
|
|
f()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 16:16:05 +02:00
|
|
|
// MaxCountForShare represents the time duration when the image can become shared.
|
|
|
|
//
|
2019-08-15 05:59:50 +02:00
|
|
|
// This value is exported for testing.
|
2019-05-11 16:16:05 +02:00
|
|
|
const MaxCountForShare = 10
|
|
|
|
|
2020-01-18 17:18:56 +01:00
|
|
|
func makeImagesShared() error {
|
2019-05-11 16:16:05 +02:00
|
|
|
for i := range imagesToMakeShared {
|
|
|
|
i.nonUpdatedCount++
|
|
|
|
if i.nonUpdatedCount >= MaxCountForShare {
|
2020-01-18 17:18:56 +01:00
|
|
|
if err := i.makeShared(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-11 16:16:05 +02:00
|
|
|
}
|
|
|
|
delete(imagesToMakeShared, i)
|
|
|
|
}
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2019-05-11 16:16:05 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2019-11-17 15:21:50 +01:00
|
|
|
// If the region is allocated without any extension, that's fine.
|
2018-03-25 15:41:15 +02:00
|
|
|
if n := b.page.Alloc(width, height); n != nil {
|
|
|
|
return n, true
|
|
|
|
}
|
|
|
|
|
2019-11-17 16:23:10 +01:00
|
|
|
nExtended := 1
|
2019-12-19 17:13:23 +01:00
|
|
|
var n *packing.Node
|
2018-03-25 15:41:15 +02:00
|
|
|
for {
|
2019-11-17 16:23:10 +01:00
|
|
|
if !b.page.Extend(nExtended) {
|
2018-03-25 15:41:15 +02:00
|
|
|
// The page can't be extended any more. Return as failure.
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
nExtended++
|
2019-12-19 17:13:23 +01:00
|
|
|
n = b.page.Alloc(width, height)
|
|
|
|
if n != nil {
|
2019-11-17 16:23:10 +01:00
|
|
|
b.page.CommitExtension()
|
2018-03-25 15:41:15 +02:00
|
|
|
break
|
|
|
|
}
|
2019-11-17 16:23:10 +01:00
|
|
|
b.page.RollbackExtension()
|
2018-03-25 15:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s := b.page.Size()
|
2019-07-16 20:32:08 +02:00
|
|
|
b.restorable = b.restorable.Extend(s, s)
|
2018-03-25 15:41:15 +02:00
|
|
|
|
|
|
|
if n == nil {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic("shareable: Alloc result must not be nil at TryAlloc")
|
2018-03-25 15:41:15 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2019-08-23 18:06:14 +02:00
|
|
|
initOnce sync.Once
|
2019-08-12 17:18:49 +02:00
|
|
|
|
2018-03-10 16:02:23 +01:00
|
|
|
// theBackends is a set of actually shared images.
|
|
|
|
theBackends = []*backend{}
|
2019-05-11 16:16:05 +02:00
|
|
|
|
|
|
|
imagesToMakeShared = map[*Image]struct{}{}
|
2019-08-12 12:37:20 +02:00
|
|
|
|
2019-08-12 13:48:26 +02:00
|
|
|
deferred []func()
|
2019-09-21 12:54:39 +02:00
|
|
|
|
|
|
|
// deferredM is a mutext for the slice operations. This must not be used for other usages.
|
|
|
|
deferredM sync.Mutex
|
2018-03-03 10:51:52 +01:00
|
|
|
)
|
|
|
|
|
2019-08-23 18:06:14 +02:00
|
|
|
func init() {
|
2019-08-25 17:32:30 +02:00
|
|
|
// Lock the mutex before a frame begins.
|
|
|
|
//
|
|
|
|
// In each frame, restoring images and resolving images happen respectively:
|
|
|
|
//
|
|
|
|
// [Restore -> Resolve] -> [Restore -> Resolve] -> ...
|
|
|
|
//
|
|
|
|
// Between each frame, any image operations are not permitted, or stale images would remain when restoring
|
|
|
|
// (#913).
|
2019-08-23 18:06:14 +02:00
|
|
|
backendsM.Lock()
|
2019-06-22 13:17:52 +02: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
|
2019-09-21 07:53:52 +02:00
|
|
|
volatile bool
|
2019-08-24 17:43:26 +02:00
|
|
|
screen 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
|
|
|
|
2019-05-11 16:16:05 +02:00
|
|
|
node *packing.Node
|
|
|
|
|
|
|
|
// nonUpdatedCount represents how long the image is kept not modified with DrawTriangles.
|
|
|
|
// In the current implementation, if an image is being modified by DrawTriangles, the image is separated from
|
|
|
|
// a shared (restorable) image by ensureNotShared.
|
|
|
|
//
|
2019-05-12 10:53:18 +02:00
|
|
|
// nonUpdatedCount is increased every frame if the image is not modified, or set to 0 if the image is
|
2019-05-11 16:16:05 +02:00
|
|
|
// modified.
|
|
|
|
//
|
|
|
|
// ReplacePixels doesn't affect this value since ReplacePixels can be done on shared images.
|
|
|
|
nonUpdatedCount int
|
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-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
|
|
|
|
}
|
|
|
|
|
2020-06-16 05:48:20 +02:00
|
|
|
ox, oy, w, h := i.regionWithPadding()
|
2019-09-20 17:41:53 +02:00
|
|
|
dx0 := float32(0)
|
|
|
|
dy0 := float32(0)
|
|
|
|
dx1 := float32(w)
|
|
|
|
dy1 := float32(h)
|
|
|
|
sx0 := float32(ox)
|
|
|
|
sy0 := float32(oy)
|
|
|
|
sx1 := float32(ox + w)
|
|
|
|
sy1 := float32(oy + h)
|
2019-09-21 07:53:52 +02:00
|
|
|
newImg := restorable.NewImage(w, h, i.volatile)
|
2019-09-20 17:41:53 +02:00
|
|
|
vs := []float32{
|
2020-07-02 16:06:58 +02:00
|
|
|
dx0, dy0, sx0, sy0, 1, 1, 1, 1,
|
|
|
|
dx1, dy0, sx1, sy0, 1, 1, 1, 1,
|
|
|
|
dx0, dy1, sx0, sy1, 1, 1, 1, 1,
|
|
|
|
dx1, dy1, sx1, sy1, 1, 1, 1, 1,
|
2019-09-20 17:41:53 +02:00
|
|
|
}
|
2018-10-28 15:03:06 +01:00
|
|
|
is := graphics.QuadIndices()
|
2020-07-05 18:55:46 +02:00
|
|
|
newImg.DrawTriangles(i.backend.restorable, vs, is, nil, driver.CompositeModeCopy, driver.FilterNearest, driver.AddressUnsafe, driver.Region{}, nil, nil, nil)
|
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
|
|
|
}
|
|
|
|
|
2020-01-18 17:18:56 +01:00
|
|
|
func (i *Image) makeShared() error {
|
2018-07-11 18:40:24 +02:00
|
|
|
if i.backend == nil {
|
2018-07-29 16:49:53 +02:00
|
|
|
i.allocate(true)
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if i.isShared() {
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !i.shareable() {
|
2019-05-11 16:16:05 +02:00
|
|
|
panic("shareable: makeShared cannot be called on a non-shareable image")
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 07:53:52 +02:00
|
|
|
newI := NewImage(i.width, i.height, i.volatile)
|
2018-07-11 18:40:24 +02:00
|
|
|
pixels := make([]byte, 4*i.width*i.height)
|
|
|
|
for y := 0; y < i.height; y++ {
|
|
|
|
for x := 0; x < i.width; x++ {
|
2020-06-16 05:48:20 +02:00
|
|
|
r, g, b, a, err := i.at(x+paddingSize, y+paddingSize)
|
2020-01-18 17:18:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-14 08:29:04 +02:00
|
|
|
pixels[4*(i.width*y+x)] = r
|
|
|
|
pixels[4*(i.width*y+x)+1] = g
|
|
|
|
pixels[4*(i.width*y+x)+2] = b
|
|
|
|
pixels[4*(i.width*y+x)+3] = a
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
newI.replacePixels(pixels)
|
2018-07-21 23:27:57 +02:00
|
|
|
newI.moveTo(i)
|
2019-05-11 16:16:05 +02:00
|
|
|
i.nonUpdatedCount = 0
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 05:48:20 +02:00
|
|
|
func (i *Image) regionWithPadding() (x, y, width, height int) {
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend == nil {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic("shareable: backend must not be nil: not allocated yet?")
|
2018-04-29 11:51:48 +02:00
|
|
|
}
|
2018-06-25 18:59:12 +02:00
|
|
|
if !i.isShared() {
|
2020-06-16 05:48:20 +02:00
|
|
|
return 0, 0, i.width + 2*paddingSize, i.height + 2*paddingSize
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
2018-04-08 19:18:46 +02:00
|
|
|
return i.node.Region()
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
2020-06-29 06:24:49 +02:00
|
|
|
func (i *Image) processSrc(src *Image) {
|
|
|
|
if src.disposed {
|
|
|
|
panic("shareable: the drawing source image must not be disposed (DrawTriangles)")
|
|
|
|
}
|
|
|
|
if src.backend == nil {
|
|
|
|
src.allocate(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare i and source images after ensuring i is not shared, or
|
|
|
|
// i and a source image might share the same texture even though i != src.
|
|
|
|
if i.backend.restorable == src.backend.restorable {
|
|
|
|
panic("shareable: Image.DrawTriangles: source must be different from the receiver")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeSharedIfNeeded(src *Image) {
|
|
|
|
if !src.isShared() && src.shareable() {
|
|
|
|
imagesToMakeShared[src] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 19:35:18 +02:00
|
|
|
// DrawTriangles draws triangles with the given image.
|
|
|
|
//
|
|
|
|
// The vertex floats are:
|
|
|
|
//
|
2020-07-02 16:06:58 +02:00
|
|
|
// 0: Destination X in pixels
|
|
|
|
// 1: Destination Y in pixels
|
|
|
|
// 2: Source X in pixels (the upper-left is (0, 0))
|
|
|
|
// 3: Source Y in pixels
|
|
|
|
// 4: Color R [0.0-1.0]
|
|
|
|
// 5: Color G
|
|
|
|
// 6: Color B
|
|
|
|
// 7: Color Y
|
2020-07-14 20:33:13 +02:00
|
|
|
func (i *Image) DrawTriangles(img *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode driver.CompositeMode, filter driver.Filter, address driver.Address, sourceRegion driver.Region, shader *Shader, uniforms []interface{}, images []*Image) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
2019-09-28 20:50:43 +02:00
|
|
|
// Do not use defer for performance.
|
2018-04-29 11:51:48 +02:00
|
|
|
|
2018-04-29 20:34:35 +02:00
|
|
|
if i.disposed {
|
2019-04-22 16:12:03 +02:00
|
|
|
panic("shareable: the drawing target image must not be disposed (DrawTriangles)")
|
2018-04-29 20:34:35 +02:00
|
|
|
}
|
2018-04-08 19:18:46 +02:00
|
|
|
i.ensureNotShared()
|
2018-03-10 15:48:10 +01:00
|
|
|
|
2020-06-29 06:24:49 +02:00
|
|
|
if img != nil {
|
|
|
|
i.processSrc(img)
|
|
|
|
}
|
2020-06-29 17:50:32 +02:00
|
|
|
firstImg := img
|
2020-07-14 20:33:13 +02:00
|
|
|
for _, src := range images {
|
2020-07-05 18:55:46 +02:00
|
|
|
if firstImg == nil {
|
|
|
|
firstImg = src
|
2020-06-03 18:35:35 +02:00
|
|
|
}
|
2020-07-05 18:55:46 +02:00
|
|
|
i.processSrc(src)
|
2018-03-10 15:48:10 +01:00
|
|
|
}
|
|
|
|
|
2020-06-14 09:03:26 +02:00
|
|
|
var dx, dy float32
|
2020-06-16 05:48:20 +02:00
|
|
|
// A screen image doesn't have its padding.
|
2020-06-14 09:03:26 +02:00
|
|
|
if !i.screen {
|
2020-06-16 05:48:20 +02:00
|
|
|
dx = paddingSize
|
|
|
|
dy = paddingSize
|
2020-06-14 09:03:26 +02:00
|
|
|
}
|
2020-06-29 17:50:32 +02:00
|
|
|
|
2020-06-03 18:35:35 +02:00
|
|
|
var oxf, oyf float32
|
2020-06-29 17:50:32 +02:00
|
|
|
if firstImg != nil {
|
|
|
|
ox, oy, _, _ := firstImg.regionWithPadding()
|
2020-06-16 05:48:20 +02:00
|
|
|
ox += paddingSize
|
|
|
|
oy += paddingSize
|
2020-06-03 18:35:35 +02:00
|
|
|
oxf, oyf = float32(ox), float32(oy)
|
|
|
|
n := len(vertices) / graphics.VertexFloatNum
|
|
|
|
for i := 0; i < n; i++ {
|
2020-06-14 09:03:26 +02:00
|
|
|
vertices[i*graphics.VertexFloatNum+0] += dx
|
|
|
|
vertices[i*graphics.VertexFloatNum+1] += dy
|
2020-06-03 18:35:35 +02:00
|
|
|
vertices[i*graphics.VertexFloatNum+2] += oxf
|
|
|
|
vertices[i*graphics.VertexFloatNum+3] += oyf
|
|
|
|
}
|
2020-06-29 17:02:33 +02:00
|
|
|
if address != driver.AddressUnsafe {
|
|
|
|
sourceRegion.X += oxf
|
|
|
|
sourceRegion.Y += oyf
|
|
|
|
}
|
2019-09-20 17:41:53 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 15:26:55 +02:00
|
|
|
var s *restorable.Shader
|
|
|
|
if shader != nil {
|
|
|
|
s = shader.shader
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:33:13 +02:00
|
|
|
var imgs []*restorable.Image
|
|
|
|
for _, img := range images {
|
|
|
|
imgs = append(imgs, img.backend.restorable)
|
2020-05-26 15:26:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 18:35:35 +02:00
|
|
|
var r *restorable.Image
|
|
|
|
if img != nil {
|
|
|
|
r = img.backend.restorable
|
|
|
|
}
|
2020-07-14 20:33:13 +02:00
|
|
|
i.backend.restorable.DrawTriangles(r, vertices, indices, colorm, mode, filter, address, sourceRegion, s, uniforms, imgs)
|
2018-07-11 18:40:24 +02:00
|
|
|
|
2019-05-11 16:16:05 +02:00
|
|
|
i.nonUpdatedCount = 0
|
|
|
|
delete(imagesToMakeShared, i)
|
2018-08-08 11:40:50 +02:00
|
|
|
|
2020-06-29 06:24:49 +02:00
|
|
|
if img != nil {
|
|
|
|
makeSharedIfNeeded(img)
|
|
|
|
}
|
2020-07-14 20:33:13 +02:00
|
|
|
for _, src := range images {
|
2020-07-14 20:37:35 +02:00
|
|
|
makeSharedIfNeeded(src)
|
2019-05-11 16:16:05 +02:00
|
|
|
}
|
2019-09-28 20:50:43 +02:00
|
|
|
|
|
|
|
backendsM.Unlock()
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2019-09-21 14:16:39 +02:00
|
|
|
func (i *Image) Fill(clr color.RGBA) {
|
2019-08-24 19:39:59 +02:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
|
|
|
|
if i.disposed {
|
|
|
|
panic("shareable: the drawing target image must not be disposed (Fill)")
|
|
|
|
}
|
|
|
|
if i.backend == nil {
|
|
|
|
if _, _, _, a := clr.RGBA(); a == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i.ensureNotShared()
|
|
|
|
|
2019-08-27 00:05:08 +02:00
|
|
|
// As *restorable.Image is an independent image, it is fine to fill the entire image.
|
|
|
|
i.backend.restorable.Fill(clr)
|
2019-08-24 19:39:59 +02:00
|
|
|
|
|
|
|
i.nonUpdatedCount = 0
|
|
|
|
delete(imagesToMakeShared, i)
|
|
|
|
}
|
|
|
|
|
2020-06-14 08:29:04 +02:00
|
|
|
func (i *Image) ReplacePixels(pix []byte) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
2020-06-14 08:29:04 +02:00
|
|
|
i.replacePixels(pix)
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
2018-03-10 16:28:30 +01:00
|
|
|
|
2020-06-14 08:29:04 +02:00
|
|
|
func (i *Image) replacePixels(pix []byte) {
|
2018-04-29 20:34:35 +02:00
|
|
|
if i.disposed {
|
2019-02-09 11:47:45 +01:00
|
|
|
panic("shareable: the image must not be disposed at replacePixels")
|
2018-04-29 20:34:35 +02:00
|
|
|
}
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend == nil {
|
2020-06-14 08:29:04 +02:00
|
|
|
if pix == nil {
|
2018-08-08 18:16:46 +02:00
|
|
|
return
|
|
|
|
}
|
2018-04-29 11:51:48 +02:00
|
|
|
i.allocate(true)
|
|
|
|
}
|
|
|
|
|
2020-06-16 05:48:20 +02:00
|
|
|
x, y, w, h := i.regionWithPadding()
|
2020-06-14 09:03:26 +02:00
|
|
|
if pix == nil {
|
|
|
|
i.backend.restorable.ReplacePixels(nil, x, y, w, h)
|
|
|
|
return
|
2018-03-10 15:36:07 +01:00
|
|
|
}
|
2020-06-14 09:03:26 +02:00
|
|
|
|
2020-06-16 05:48:20 +02:00
|
|
|
ow, oh := w-2*paddingSize, h-2*paddingSize
|
2020-06-14 09:03:26 +02:00
|
|
|
if l := 4 * ow * oh; len(pix) != l {
|
|
|
|
panic(fmt.Sprintf("shareable: len(p) must be %d but %d", l, len(pix)))
|
|
|
|
}
|
|
|
|
|
2020-06-16 05:48:20 +02:00
|
|
|
// Add a padding around the image.
|
2020-06-14 09:03:26 +02:00
|
|
|
pixb := make([]byte, 4*w*h)
|
|
|
|
for j := 0; j < oh; j++ {
|
2020-06-16 05:48:20 +02:00
|
|
|
copy(pixb[4*((j+paddingSize)*w+paddingSize):], pix[4*j*ow:4*(j+1)*ow])
|
2020-06-14 09:03:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
i.backend.restorable.ReplacePixels(pixb, x, y, w, h)
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2020-04-18 13:01:40 +02:00
|
|
|
func (img *Image) Pixels(x, y, width, height int) ([]byte, error) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
2020-04-18 13:01:40 +02:00
|
|
|
defer backendsM.Unlock()
|
|
|
|
|
2020-06-16 05:48:20 +02:00
|
|
|
x += paddingSize
|
|
|
|
y += paddingSize
|
2020-06-14 09:03:26 +02:00
|
|
|
|
2020-04-18 13:01:40 +02:00
|
|
|
bs := make([]byte, 4*width*height)
|
|
|
|
idx := 0
|
|
|
|
for j := y; j < y+height; j++ {
|
|
|
|
for i := x; i < x+width; i++ {
|
|
|
|
r, g, b, a, err := img.at(i, j)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bs[4*idx] = r
|
|
|
|
bs[4*idx+1] = g
|
|
|
|
bs[4*idx+2] = b
|
|
|
|
bs[4*idx+3] = a
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bs, nil
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
2018-03-10 16:28:30 +01:00
|
|
|
|
2020-01-18 17:18:56 +01:00
|
|
|
func (i *Image) at(x, y int) (byte, byte, byte, byte, error) {
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend == nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
return 0, 0, 0, 0, nil
|
2018-04-29 11:51:48 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 05:48:20 +02:00
|
|
|
ox, oy, w, h := i.regionWithPadding()
|
2018-03-10 15:36:07 +01:00
|
|
|
if x < 0 || y < 0 || x >= w || y >= h {
|
2020-01-18 17:18:56 +01:00
|
|
|
return 0, 0, 0, 0, nil
|
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
|
|
|
}
|
|
|
|
|
2019-09-21 12:54:39 +02:00
|
|
|
// MarkDisposed marks the image as disposed. The actual operation is deferred.
|
|
|
|
// MarkDisposed can be called from finalizers.
|
2019-08-12 13:09:17 +02:00
|
|
|
//
|
|
|
|
// A function from finalizer must not be blocked, but disposing operation can be blocked.
|
|
|
|
// Defer this operation until it becomes safe. (#913)
|
2019-09-21 12:54:39 +02:00
|
|
|
func (i *Image) MarkDisposed() {
|
|
|
|
deferredM.Lock()
|
2019-08-12 12:37:20 +02:00
|
|
|
deferred = append(deferred, func() {
|
|
|
|
i.dispose(true)
|
|
|
|
})
|
2019-09-21 12:54:39 +02:00
|
|
|
deferredM.Unlock()
|
2019-08-12 13:09:17 +02: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.
|
2020-06-16 05:48:20 +02:00
|
|
|
i.backend.restorable.ClearPixels(i.regionWithPadding())
|
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 {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic("shareable: backend not found at an image being disposed")
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
2018-03-10 16:02:23 +01:00
|
|
|
theBackends = append(theBackends[:index], theBackends[index+1:]...)
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
|
|
|
|
2019-09-21 07:53:52 +02:00
|
|
|
func NewImage(width, height int, volatile bool) *Image {
|
2019-08-23 18:06:14 +02:00
|
|
|
// Actual allocation is done lazily, and the lock is not needed.
|
2018-04-29 11:51:48 +02:00
|
|
|
return &Image{
|
2019-09-21 07:53:52 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
volatile: volatile,
|
2018-04-29 11:51:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:40:24 +02:00
|
|
|
func (i *Image) shareable() bool {
|
2019-08-23 18:06:14 +02:00
|
|
|
if minSize == 0 || maxSize == 0 {
|
|
|
|
panic("shareable: minSize or maxSize must be initialized")
|
2019-06-22 13:17:52 +02:00
|
|
|
}
|
2019-09-21 07:53:52 +02:00
|
|
|
if i.volatile {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if i.screen {
|
2018-07-24 15:25:15 +02:00
|
|
|
return false
|
|
|
|
}
|
2020-06-27 10:09:23 +02:00
|
|
|
return i.width+2*paddingSize <= maxSize && i.height+2*paddingSize <= maxSize
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic("shareable: the image is already allocated")
|
2018-04-29 12:10:36 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 12:54:39 +02:00
|
|
|
runtime.SetFinalizer(i, (*Image).MarkDisposed)
|
2019-09-21 12:15:49 +02:00
|
|
|
|
2019-08-24 17:43:26 +02:00
|
|
|
if i.screen {
|
2020-06-16 05:48:20 +02:00
|
|
|
// A screen image doesn't have a padding.
|
2019-08-24 17:43:26 +02:00
|
|
|
i.backend = &backend{
|
|
|
|
restorable: restorable.NewScreenFramebufferImage(i.width, i.height),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:40:24 +02:00
|
|
|
if !shareable || !i.shareable() {
|
2018-04-29 11:51:48 +02:00
|
|
|
i.backend = &backend{
|
2020-06-16 05:48:20 +02:00
|
|
|
restorable: restorable.NewImage(i.width+2*paddingSize, i.height+2*paddingSize, i.volatile),
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
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 {
|
2020-06-16 05:48:20 +02:00
|
|
|
if n, ok := b.TryAlloc(i.width+2*paddingSize, i.height+2*paddingSize); ok {
|
2018-04-29 11:51:48 +02:00
|
|
|
i.backend = b
|
|
|
|
i.node = n
|
|
|
|
return
|
2018-03-10 17:30:24 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-22 13:17:52 +02:00
|
|
|
size := minSize
|
2020-06-27 10:09:23 +02:00
|
|
|
for i.width+2*paddingSize > size || i.height+2*paddingSize > size {
|
2018-03-25 15:41:15 +02:00
|
|
|
if size == maxSize {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic(fmt.Sprintf("shareable: the image being shared is too big: width: %d, height: %d", i.width, i.height))
|
2018-03-25 15:41:15 +02:00
|
|
|
}
|
|
|
|
size *= 2
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:27:10 +01:00
|
|
|
b := &backend{
|
2019-09-21 07:53:52 +02:00
|
|
|
restorable: restorable.NewImage(size, size, i.volatile),
|
2018-03-25 15:41:15 +02:00
|
|
|
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
|
|
|
|
2020-06-16 05:48:20 +02:00
|
|
|
n := b.page.Alloc(i.width+2*paddingSize, i.height+2*paddingSize)
|
2018-03-03 10:51:52 +01:00
|
|
|
if n == nil {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic("shareable: Alloc result must not be nil at allocate")
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
2018-04-29 11:51:48 +02:00
|
|
|
i.backend = b
|
|
|
|
i.node = n
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
2018-03-10 15:48:10 +01:00
|
|
|
|
2020-02-05 19:03:38 +01:00
|
|
|
func (i *Image) Dump(path string, blackbg bool) error {
|
2019-07-19 18:42:19 +02:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
|
2020-02-05 19:03:38 +01:00
|
|
|
return i.backend.restorable.Dump(path, blackbg)
|
2019-07-19 18:42:19 +02:00
|
|
|
}
|
|
|
|
|
2018-03-10 16:05:06 +01:00
|
|
|
func NewScreenFramebufferImage(width, height int) *Image {
|
2019-08-24 17:43:26 +02:00
|
|
|
// Actual allocation is done lazily.
|
2018-03-10 16:07:32 +01:00
|
|
|
i := &Image{
|
2019-09-21 07:53:52 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
screen: true,
|
2018-03-10 15:48:10 +01:00
|
|
|
}
|
2018-03-10 16:07:32 +01:00
|
|
|
return i
|
2018-03-10 15:48:10 +01:00
|
|
|
}
|
2018-03-25 16:37:32 +02:00
|
|
|
|
2019-08-12 17:18:49 +02:00
|
|
|
func EndFrame() error {
|
2018-03-25 16:37:32 +02:00
|
|
|
backendsM.Lock()
|
2019-08-25 17:32:30 +02:00
|
|
|
|
2020-01-18 17:18:56 +01:00
|
|
|
return restorable.ResolveStaleImages()
|
2018-03-25 16:37:32 +02:00
|
|
|
}
|
|
|
|
|
2019-08-12 17:18:49 +02:00
|
|
|
func BeginFrame() error {
|
2019-08-25 17:32:30 +02:00
|
|
|
defer backendsM.Unlock()
|
2019-08-25 10:28:59 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
initOnce.Do(func() {
|
|
|
|
err = restorable.InitializeGraphicsDriverState()
|
2019-08-25 10:32:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(theBackends) != 0 {
|
|
|
|
panic("shareable: all the images must be not-shared before the game starts")
|
|
|
|
}
|
2020-06-27 21:14:52 +02:00
|
|
|
minSize = 1024
|
|
|
|
maxSize = max(minSize, graphicsDriver.MaxImageSize())
|
2019-08-25 10:28:59 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-26 14:10:25 +02:00
|
|
|
return restorable.RestoreIfNeeded()
|
2018-03-25 16:37:32 +02:00
|
|
|
}
|
2018-04-05 18:12:08 +02:00
|
|
|
|
2019-07-19 16:29:19 +02:00
|
|
|
func DumpImages(dir string) error {
|
2018-04-28 14:49:00 +02:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
2019-07-19 16:29:19 +02:00
|
|
|
return restorable.DumpImages(dir)
|
2018-04-28 14:49:00 +02:00
|
|
|
}
|