ebiten/internal/shareable/shareable.go

614 lines
14 KiB
Go
Raw Normal View History

// 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
import (
2019-02-07 09:19:24 +01:00
"fmt"
"image/color"
2018-03-10 16:07:32 +01:00
"runtime"
"sync"
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/hooks"
2018-03-05 16:38:56 +01:00
"github.com/hajimehoshi/ebiten/internal/packing"
"github.com/hajimehoshi/ebiten/internal/restorable"
)
var graphicsDriver driver.Graphics
func SetGraphicsDriver(graphics driver.Graphics) {
graphicsDriver = graphics
}
2019-06-23 04:32:43 +02:00
var (
minSize = 0
maxSize = 0
)
func min(a, b int) int {
if a < b {
return a
}
return b
}
func init() {
hooks.AppendHookOnBeforeUpdate(func() error {
backendsM.Lock()
defer backendsM.Unlock()
resolveDeferred()
makeImagesShared()
return nil
})
}
func resolveDeferred() {
for _, f := range deferred {
f()
}
deferred = nil
}
// 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.
const MaxCountForShare = 10
func makeImagesShared() {
for i := range imagesToMakeShared {
i.nonUpdatedCount++
if i.nonUpdatedCount >= MaxCountForShare {
i.makeShared()
}
delete(imagesToMakeShared, i)
}
}
func MakeImagesSharedForTesting() {
makeImagesShared()
}
2018-03-10 16:02:23 +01:00
type backend struct {
restorable *restorable.Image
2018-03-15 17:21:33 +01:00
// If page is nil, the backend is not shared.
page *packing.Page
}
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.
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 {
// The page is just for emulation, so we don't have to free it.
break
}
}
for i := 0; i < nExtended; i++ {
b.page.Extend()
}
s := b.page.Size()
b.restorable = b.restorable.Extend(s, s)
n := b.page.Alloc(width, height)
if n == nil {
2019-02-07 09:19:24 +01:00
panic("shareable: Alloc result must not be nil at TryAlloc")
}
return n, true
}
var (
// backendsM is a mutex for critical sections of the backend and packing.Node objects.
backendsM sync.Mutex
initOnce sync.Once
2018-03-10 16:02:23 +01:00
// theBackends is a set of actually shared images.
theBackends = []*backend{}
imagesToMakeShared = map[*Image]struct{}{}
deferred []func()
)
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).
backendsM.Lock()
}
2018-03-10 16:05:06 +01:00
type Image struct {
width int
height int
disposed bool
screen bool
2018-03-10 16:02:23 +01:00
backend *backend
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
// modified.
//
// ReplacePixels doesn't affect this value since ReplacePixels can be done on shared images.
nonUpdatedCount int
neverShared bool
}
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
}
func (i *Image) IsSharedForTesting() bool {
backendsM.Lock()
defer backendsM.Unlock()
return i.isShared()
}
type vertexPutterWithoutLock struct {
*Image
}
func (i vertexPutterWithoutLock) PutVertex(dst []float32, dx, dy, sx, sy float32, bx0, by0, bx1, by1 float32, cr, cg, cb, ca float32) {
i.putVertex(dst, dx, dy, sx, sy, bx0, by0, bx1, by1, cr, cg, cb, ca)
}
2018-04-08 19:18:46 +02:00
func (i *Image) ensureNotShared() {
if i.backend == nil {
i.allocate(false)
return
}
2018-06-25 18:59:12 +02:00
if !i.isShared() {
return
}
_, _, w, h := i.region()
2019-02-12 07:05:05 +01:00
newImg := restorable.NewImage(w, h)
vs := make([]float32, 4*graphics.VertexFloatNum)
graphics.PutQuadVertices(vs, vertexPutterWithoutLock{i}, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
2018-10-28 15:03:06 +01:00
is := graphics.QuadIndices()
newImg.DrawTriangles(i.backend.restorable, vs, is, nil, driver.CompositeModeCopy, driver.FilterNearest, driver.AddressClampToZero)
i.dispose(false)
2018-04-08 19:18:46 +02:00
i.backend = &backend{
restorable: newImg,
}
}
func (i *Image) makeShared() {
if i.backend == nil {
i.allocate(true)
return
}
if i.isShared() {
return
}
if !i.shareable() {
panic("shareable: makeShared cannot be called on a non-shareable image")
}
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++ {
r, g, b, a := i.at(x, y)
pixels[4*(x+i.width*y)] = r
pixels[4*(x+i.width*y)+1] = g
pixels[4*(x+i.width*y)+2] = b
pixels[4*(x+i.width*y)+3] = a
}
}
newI.replacePixels(pixels)
2018-07-21 23:27:57 +02:00
newI.moveTo(i)
i.nonUpdatedCount = 0
}
2018-04-08 19:18:46 +02:00
func (i *Image) region() (x, y, width, height int) {
if i.backend == nil {
2019-02-07 09:19:24 +01:00
panic("shareable: backend must not be nil: not allocated yet?")
}
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()
return 0, 0, w, h
}
2018-04-08 19:18:46 +02:00
return i.node.Region()
}
2018-04-08 19:18:46 +02:00
func (i *Image) Size() (width, height int) {
return i.width, i.height
}
// PutVertices puts the given dst with vertices that can be passed to DrawTriangles.
func (i *Image) PutVertex(dst []float32, dx, dy, sx, sy float32, bx0, by0, bx1, by1 float32, cr, cg, cb, ca float32) {
backendsM.Lock()
defer backendsM.Unlock()
i.putVertex(dst, dx, dy, sx, sy, bx0, by0, bx1, by1, cr, cg, cb, ca)
}
func (i *Image) putVertex(dst []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)
}
2018-06-12 03:33:09 +02:00
ox, oy, _, _ := i.region()
oxf, oyf := float32(ox), float32(oy)
i.backend.restorable.PutVertex(dst, dx, dy, sx+oxf, sy+oyf, bx0+oxf, by0+oyf, bx1+oxf, by1+oyf, cr, cg, cb, ca)
2018-06-12 03:33:09 +02:00
}
func (i *Image) DrawTriangles(img *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode driver.CompositeMode, filter driver.Filter, address driver.Address) {
backendsM.Lock()
defer backendsM.Unlock()
if img.disposed {
panic("shareable: the drawing source image must not be disposed (DrawTriangles)")
}
if i.disposed {
panic("shareable: the drawing target image must not be disposed (DrawTriangles)")
}
if img.backend == nil {
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 {
panic("shareable: Image.DrawTriangles: img must be different from the receiver")
2018-03-10 15:48:10 +01:00
}
i.backend.restorable.DrawTriangles(img.backend.restorable, vertices, indices, colorm, mode, filter, address)
i.nonUpdatedCount = 0
delete(imagesToMakeShared, i)
if !img.isShared() && img.shareable() {
imagesToMakeShared[img] = struct{}{}
}
}
func (i *Image) Fill(clr color.Color) {
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()
// As *restorable.Image is an independent image, it is fine to fill the entire image.
i.backend.restorable.Fill(clr)
i.nonUpdatedCount = 0
delete(imagesToMakeShared, i)
}
// ClearFramebuffer clears the image with a color. This affects not only the (0, 0)-(width, height) region but also
// the whole framebuffer region.
func (i *Image) ClearFramebuffer() {
backendsM.Lock()
defer backendsM.Unlock()
if i.disposed {
2019-02-09 11:47:45 +01:00
panic("shareable: the drawing target image must not be disposed (Fill)")
}
i.ensureNotShared()
2019-07-19 21:53:18 +02:00
i.backend.restorable.Clear()
}
2018-04-08 19:18:46 +02:00
func (i *Image) ReplacePixels(p []byte) {
backendsM.Lock()
defer backendsM.Unlock()
i.replacePixels(p)
}
func (i *Image) replacePixels(p []byte) {
if i.disposed {
2019-02-09 11:47:45 +01:00
panic("shareable: the image must not be disposed at replacePixels")
}
if i.backend == nil {
if p == nil {
return
}
i.allocate(true)
}
2018-04-08 19:18:46 +02:00
x, y, w, h := i.region()
if p != nil {
if l := 4 * w * h; len(p) != l {
2019-02-07 09:19:24 +01:00
panic(fmt.Sprintf("shareable: len(p) must be %d but %d", l, len(p)))
}
}
2018-04-08 19:18:46 +02:00
i.backend.restorable.ReplacePixels(p, x, y, w, h)
}
func (i *Image) At(x, y int) (byte, byte, byte, byte) {
backendsM.Lock()
2018-04-05 05:02:08 +02:00
defer backendsM.Unlock()
return i.at(x, y)
}
func (i *Image) at(x, y int) (byte, byte, byte, byte) {
if i.backend == nil {
return 0, 0, 0, 0
}
2018-04-08 19:18:46 +02:00
ox, oy, w, h := i.region()
if x < 0 || y < 0 || x >= w || y >= h {
return 0, 0, 0, 0
}
return i.backend.restorable.At(x+ox, y+oy)
}
// disposeFromFinalizer disposes images, but the actual operation is deferred.
// disposeFromFinalizer is called from finalizers.
//
// A function from finalizer must not be blocked, but disposing operation can be blocked.
// Defer this operation until it becomes safe. (#913)
func (i *Image) disposeFromFinalizer() {
// deferred doesn't have to be, and should not be protected by a mutex.
deferred = append(deferred, func() {
i.dispose(true)
})
}
func (i *Image) Dispose() {
backendsM.Lock()
defer backendsM.Unlock()
i.dispose(true)
}
func (i *Image) dispose(markDisposed bool) {
defer func() {
if markDisposed {
i.disposed = true
}
2018-04-08 19:18:46 +02:00
i.backend = nil
i.node = nil
if markDisposed {
runtime.SetFinalizer(i, nil)
}
}()
if i.disposed {
return
}
if i.backend == nil {
// Not allocated yet.
return
}
2018-06-25 18:59:12 +02:00
if !i.isShared() {
2018-04-08 19:18:46 +02:00
i.backend.restorable.Dispose()
return
}
2018-04-08 19:18:46 +02:00
i.backend.page.Free(i.node)
if !i.backend.page.IsEmpty() {
// As this part can be reused, this should be cleared explicitly.
i.backend.restorable.ClearPixels(i.region())
return
}
2018-04-08 19:18:46 +02:00
i.backend.restorable.Dispose()
index := -1
2018-04-08 19:18:46 +02:00
for idx, sh := range theBackends {
if sh == i.backend {
index = idx
break
}
}
if index == -1 {
2019-02-07 09:19:24 +01:00
panic("shareable: backend not found at an image being disposed")
}
2018-03-10 16:02:23 +01:00
theBackends = append(theBackends[:index], theBackends[index+1:]...)
}
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-03-10 16:05:06 +01:00
func NewImage(width, height int) *Image {
// Actual allocation is done lazily, and the lock is not needed.
return &Image{
width: width,
height: height,
}
}
func (i *Image) shareable() bool {
if minSize == 0 || maxSize == 0 {
panic("shareable: minSize or maxSize must be initialized")
}
if i.neverShared {
return false
}
return i.width <= maxSize && i.height <= maxSize
}
func (i *Image) allocate(shareable bool) {
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
}
if i.screen {
i.backend = &backend{
restorable: restorable.NewScreenFramebufferImage(i.width, i.height),
}
runtime.SetFinalizer(i, (*Image).disposeFromFinalizer)
return
}
if !shareable || !i.shareable() {
i.backend = &backend{
2019-02-12 07:05:05 +01:00
restorable: restorable.NewImage(i.width, i.height),
}
runtime.SetFinalizer(i, (*Image).disposeFromFinalizer)
return
}
2018-03-23 20:27:10 +01:00
for _, b := range theBackends {
if n, ok := b.TryAlloc(i.width, i.height); ok {
i.backend = b
i.node = n
runtime.SetFinalizer(i, (*Image).disposeFromFinalizer)
return
}
}
size := minSize
for i.width > size || i.height > size {
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))
}
size *= 2
}
2018-03-23 20:27:10 +01:00
b := &backend{
2019-02-12 07:05:05 +01:00
restorable: restorable.NewImage(size, size),
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)
n := b.page.Alloc(i.width, i.height)
if n == nil {
2019-02-07 09:19:24 +01:00
panic("shareable: Alloc result must not be nil at allocate")
}
i.backend = b
i.node = n
runtime.SetFinalizer(i, (*Image).disposeFromFinalizer)
}
2018-03-10 15:48:10 +01:00
2019-02-12 07:12:36 +01:00
func (i *Image) MakeVolatile() {
backendsM.Lock()
defer backendsM.Unlock()
2019-02-12 07:12:36 +01:00
i.ensureNotShared()
i.backend.restorable.MakeVolatile()
i.neverShared = true
2018-03-10 15:48:10 +01:00
}
2019-07-19 18:42:19 +02:00
func (i *Image) Dump(path string) error {
backendsM.Lock()
defer backendsM.Unlock()
return i.backend.restorable.Dump(path)
}
2018-03-10 16:05:06 +01:00
func NewScreenFramebufferImage(width, height int) *Image {
// Actual allocation is done lazily.
2018-03-10 16:07:32 +01:00
i := &Image{
width: width,
height: height,
screen: true,
neverShared: 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
}
func EndFrame() error {
backendsM.Lock()
2019-08-25 17:32:30 +02:00
restorable.ResolveStaleImages()
return restorable.Error()
}
func BeginFrame() error {
2019-08-25 17:32:30 +02:00
defer backendsM.Unlock()
var err error
initOnce.Do(func() {
err = restorable.InitializeGraphicsDriverState()
if err != nil {
return
}
if len(theBackends) != 0 {
panic("shareable: all the images must be not-shared before the game starts")
}
if graphicsDriver.HasHighPrecisionFloat() {
minSize = 1024
// Use 4096 as a maximum size whatever size the graphics driver accepts. There are
// not enough evidences that bigger textures works correctly.
maxSize = min(4096, graphicsDriver.MaxImageSize())
} else {
minSize = 512
maxSize = 512
}
})
if err != nil {
return err
}
2019-05-26 14:10:25 +02:00
return restorable.RestoreIfNeeded()
}
func DumpImages(dir string) error {
backendsM.Lock()
defer backendsM.Unlock()
return restorable.DumpImages(dir)
}