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.
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
package atlas
|
2018-03-03 10:51:52 +01:00
|
|
|
|
|
|
|
import (
|
2019-02-07 09:19:24 +01:00
|
|
|
"fmt"
|
2021-07-29 09:09:25 +02:00
|
|
|
"image"
|
2022-04-01 21:59:30 +02:00
|
|
|
"math"
|
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
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/affine"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/packing"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
|
2018-03-03 10:51:52 +01:00
|
|
|
)
|
|
|
|
|
2019-06-23 04:32:43 +02:00
|
|
|
var (
|
|
|
|
minSize = 0
|
|
|
|
maxSize = 0
|
|
|
|
)
|
|
|
|
|
2022-03-21 07:37:21 +01:00
|
|
|
type temporaryBytes struct {
|
2021-06-26 16:29:45 +02:00
|
|
|
pixels []byte
|
|
|
|
pos int
|
|
|
|
notFullyUsedTime int
|
2021-06-25 19:33:14 +02:00
|
|
|
}
|
|
|
|
|
2022-03-21 07:37:21 +01:00
|
|
|
var theTemporaryBytes temporaryBytes
|
2021-06-25 19:33:14 +02:00
|
|
|
|
2022-03-21 07:37:21 +01:00
|
|
|
func temporaryBytesSize(size int) int {
|
2021-06-26 16:29:45 +02:00
|
|
|
l := 16
|
|
|
|
for l < size {
|
|
|
|
l *= 2
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2021-08-25 20:35:18 +02:00
|
|
|
// alloc allocates the pixels and reutrns it.
|
|
|
|
// Be careful that the returned pixels might not be zero-cleared.
|
2022-03-21 07:37:21 +01:00
|
|
|
func (t *temporaryBytes) alloc(size int) []byte {
|
2021-06-25 19:33:14 +02:00
|
|
|
if len(t.pixels) < t.pos+size {
|
2022-03-21 07:37:21 +01:00
|
|
|
t.pixels = make([]byte, max(len(t.pixels)*2, temporaryBytesSize(size)))
|
2021-06-25 19:33:14 +02:00
|
|
|
t.pos = 0
|
|
|
|
}
|
|
|
|
pix := t.pixels[t.pos : t.pos+size]
|
|
|
|
t.pos += size
|
|
|
|
return pix
|
|
|
|
}
|
|
|
|
|
2022-03-21 07:37:21 +01:00
|
|
|
func (t *temporaryBytes) resetAtFrameEnd() {
|
2021-06-26 16:29:45 +02:00
|
|
|
const maxNotFullyUsedTime = 60
|
2021-06-25 19:33:14 +02:00
|
|
|
|
2022-03-21 07:37:21 +01:00
|
|
|
if temporaryBytesSize(t.pos) < len(t.pixels) {
|
2021-06-26 16:29:45 +02:00
|
|
|
if t.notFullyUsedTime < maxNotFullyUsedTime {
|
|
|
|
t.notFullyUsedTime++
|
2021-06-25 19:33:14 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-06-26 16:29:45 +02:00
|
|
|
t.notFullyUsedTime = 0
|
2021-06-25 19:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Let the pixels GCed if this is not used for a while.
|
2021-06-26 16:29:45 +02:00
|
|
|
if t.notFullyUsedTime == maxNotFullyUsedTime && len(t.pixels) > 0 {
|
2021-06-25 19:33:14 +02:00
|
|
|
t.pixels = nil
|
2021-10-31 09:18:07 +01:00
|
|
|
t.notFullyUsedTime = 0
|
2021-06-25 19:33:14 +02:00
|
|
|
}
|
2021-08-25 20:35:18 +02:00
|
|
|
|
|
|
|
// Reset the position and reuse the allocated bytes.
|
|
|
|
// t.pixels should already be sent to GPU, then this can be reused.
|
|
|
|
t.pos = 0
|
2021-06-25 19:33:14 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-19 11:52:05 +02:00
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
// baseCountToPutOnAtlas represents the base time duration when the image can be put onto an atlas.
|
2021-03-09 18:29:56 +01:00
|
|
|
// Actual time duration is increased in an exponential way for each usages as a rendering target.
|
2021-03-11 15:13:24 +01:00
|
|
|
const baseCountToPutOnAtlas = 10
|
2021-03-09 18:29:56 +01:00
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func putImagesOnAtlas(graphicsDriver graphicsdriver.Graphics) error {
|
2021-03-11 15:13:24 +01:00
|
|
|
for i := range imagesToPutOnAtlas {
|
2021-01-17 10:22:45 +01:00
|
|
|
i.usedAsSourceCount++
|
2021-06-19 11:52:05 +02:00
|
|
|
if i.usedAsSourceCount >= baseCountToPutOnAtlas*(1<<uint(min(i.isolatedCount, 31))) {
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := i.putOnAtlas(graphicsDriver); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
return err
|
|
|
|
}
|
2021-01-17 10:22:45 +01:00
|
|
|
i.usedAsSourceCount = 0
|
2021-03-11 15:13:24 +01:00
|
|
|
delete(imagesToPutOnAtlas, i)
|
2019-05-11 16:16:05 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-17 10:22:45 +01:00
|
|
|
|
|
|
|
// Reset the images. The images will be registered again when it is used as a rendering source.
|
2021-03-11 15:13:24 +01:00
|
|
|
for k := range imagesToPutOnAtlas {
|
|
|
|
delete(imagesToPutOnAtlas, k)
|
2021-01-25 16:43:45 +01:00
|
|
|
}
|
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 {
|
2021-03-11 15:13:24 +01:00
|
|
|
// restorable is an atlas on which there might be multiple images.
|
2018-03-03 10:51:52 +01:00
|
|
|
restorable *restorable.Image
|
2018-03-15 17:21:33 +01:00
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
// page is an atlas map. Each part is called a node.
|
|
|
|
// If page is nil, the backend's image is isolated and not on an atlas.
|
2018-03-15 17:21:33 +01:00
|
|
|
page *packing.Page
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01: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 {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: 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
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
// theBackends is a set of atlases.
|
2018-03-10 16:02:23 +01:00
|
|
|
theBackends = []*backend{}
|
2019-05-11 16:16:05 +02:00
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
imagesToPutOnAtlas = 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
|
|
|
}
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
type ImageType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ImageTypeRegular ImageType = iota
|
|
|
|
ImageTypeScreen
|
|
|
|
ImageTypeVolatile
|
2022-06-09 19:11:08 +02:00
|
|
|
ImageTypeUnmanaged
|
2022-06-07 16:57:56 +02:00
|
|
|
)
|
|
|
|
|
2022-01-08 15:33:59 +01:00
|
|
|
// Image is a rectangle pixel set that might be on an atlas.
|
2018-03-10 16:05:06 +01:00
|
|
|
type Image struct {
|
2022-06-07 16:57:56 +02:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
imageType ImageType
|
|
|
|
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
|
|
|
|
2019-05-11 16:16:05 +02:00
|
|
|
node *packing.Node
|
|
|
|
|
2021-01-17 10:22:45 +01:00
|
|
|
// usedAsSourceCount represents how long the image is used as a rendering source and kept not modified with
|
|
|
|
// DrawTriangles.
|
2019-05-11 16:16:05 +02:00
|
|
|
// In the current implementation, if an image is being modified by DrawTriangles, the image is separated from
|
2021-03-11 15:13:24 +01:00
|
|
|
// a restorable image on an atlas by ensureIsolated.
|
2019-05-11 16:16:05 +02:00
|
|
|
//
|
2021-01-17 10:22:45 +01:00
|
|
|
// usedAsSourceCount is increased if the image is used as a rendering source, or set to 0 if the image is
|
2019-05-11 16:16:05 +02:00
|
|
|
// modified.
|
|
|
|
//
|
2021-03-11 15:13:24 +01:00
|
|
|
// ReplacePixels doesn't affect this value since ReplacePixels can be done on images on an atlas.
|
2021-01-17 10:22:45 +01:00
|
|
|
usedAsSourceCount int
|
2021-03-09 18:29:56 +01:00
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
// isolatedCount represents how many times the image on a texture atlas is changed into an isolated image.
|
|
|
|
// isolatedCount affects the calculation when to put the image onto a texture atlas again.
|
|
|
|
isolatedCount int
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 16:26:38 +01:00
|
|
|
// moveTo moves its content to the given image dst.
|
|
|
|
// After moveTo is called, the image i is no longer available.
|
|
|
|
//
|
|
|
|
// moveTo is smilar to C++'s move semantics.
|
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)
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
func (i *Image) isOnAtlas() bool {
|
2018-06-25 18:59:12 +02:00
|
|
|
return i.node != nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 10:22:45 +01:00
|
|
|
func (i *Image) resetUsedAsSourceCount() {
|
|
|
|
i.usedAsSourceCount = 0
|
2021-03-11 15:13:24 +01:00
|
|
|
delete(imagesToPutOnAtlas, i)
|
2020-11-05 18:10:03 +01:00
|
|
|
}
|
|
|
|
|
2022-06-09 17:14:04 +02:00
|
|
|
func (i *Image) paddingSize() int {
|
2022-06-09 17:32:55 +02:00
|
|
|
if i.imageType == ImageTypeRegular {
|
|
|
|
return 1
|
2022-06-09 17:14:04 +02:00
|
|
|
}
|
2022-06-09 17:32:55 +02:00
|
|
|
return 0
|
2022-06-09 17:14:04 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
func (i *Image) ensureIsolated() {
|
2021-01-17 10:22:45 +01:00
|
|
|
i.resetUsedAsSourceCount()
|
2020-11-05 17:45:04 +01:00
|
|
|
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend == nil {
|
2018-04-29 11:51:48 +02:00
|
|
|
i.allocate(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
if !i.isOnAtlas() {
|
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)
|
2022-04-01 17:32:16 +02:00
|
|
|
sw, sh := i.backend.restorable.InternalSize()
|
|
|
|
sx0 /= float32(sw)
|
|
|
|
sy0 /= float32(sh)
|
|
|
|
sx1 /= float32(sw)
|
|
|
|
sy1 /= float32(sh)
|
2022-06-06 02:29:30 +02:00
|
|
|
typ := restorable.ImageTypeRegular
|
2022-06-07 16:57:56 +02:00
|
|
|
if i.imageType == ImageTypeVolatile {
|
2022-06-06 02:29:30 +02:00
|
|
|
typ = restorable.ImageTypeVolatile
|
|
|
|
}
|
|
|
|
newImg := restorable.NewImage(w, h, typ)
|
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-18 12:56:22 +02:00
|
|
|
srcs := [graphics.ShaderImageNum]*restorable.Image{i.backend.restorable}
|
|
|
|
var offsets [graphics.ShaderImageNum - 1][2]float32
|
2022-02-06 12:41:32 +01:00
|
|
|
dstRegion := graphicsdriver.Region{
|
2022-06-09 17:14:04 +02:00
|
|
|
X: float32(i.paddingSize()),
|
|
|
|
Y: float32(i.paddingSize()),
|
|
|
|
Width: float32(w - 2*i.paddingSize()),
|
|
|
|
Height: float32(h - 2*i.paddingSize()),
|
2020-11-07 11:14:06 +01:00
|
|
|
}
|
2022-02-06 12:41:32 +01:00
|
|
|
newImg.DrawTriangles(srcs, offsets, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dstRegion, graphicsdriver.Region{}, nil, nil, false)
|
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,
|
|
|
|
}
|
2021-03-09 18:29:56 +01:00
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
i.isolatedCount++
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func (i *Image) putOnAtlas(graphicsDriver graphicsdriver.Graphics) 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
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
if i.isOnAtlas() {
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
if !i.canBePutOnAtlas() {
|
|
|
|
panic("atlas: putOnAtlas cannot be called on a image that cannot be on an atlas")
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2022-06-09 19:38:30 +02:00
|
|
|
if i.imageType != ImageTypeRegular {
|
|
|
|
panic(fmt.Sprintf("atlas: the image type must be ImageTypeRegular but %d", i.imageType))
|
|
|
|
}
|
|
|
|
|
|
|
|
newI := NewImage(i.width, i.height, ImageTypeRegular)
|
2021-02-26 04:56:22 +01:00
|
|
|
|
2022-06-09 19:58:01 +02:00
|
|
|
w, h := float32(i.width), float32(i.height)
|
|
|
|
vs := graphics.QuadVertices(0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
|
|
|
|
is := graphics.QuadIndices()
|
|
|
|
dr := graphicsdriver.Region{
|
|
|
|
X: 0,
|
|
|
|
Y: 0,
|
|
|
|
Width: w,
|
|
|
|
Height: h,
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
2022-06-09 19:58:01 +02:00
|
|
|
newI.drawTriangles([graphics.ShaderImageNum]*Image{i}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, [graphics.ShaderImageNum - 1][2]float32{}, nil, nil, false, true)
|
2021-02-26 04:56:22 +01:00
|
|
|
|
2018-07-21 23:27:57 +02:00
|
|
|
newI.moveTo(i)
|
2021-01-17 10:22:45 +01:00
|
|
|
i.usedAsSourceCount = 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 {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: backend must not be nil: not allocated yet?")
|
2018-04-29 11:51:48 +02:00
|
|
|
}
|
2021-03-11 15:13:24 +01:00
|
|
|
if !i.isOnAtlas() {
|
2022-06-09 17:14:04 +02:00
|
|
|
return 0, 0, i.width + 2*i.paddingSize(), i.height + 2*i.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) {
|
2020-07-17 18:09:58 +02:00
|
|
|
if src == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-06-29 06:24:49 +02:00
|
|
|
if src.disposed {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: the drawing source image must not be disposed (DrawTriangles)")
|
2020-06-29 06:24:49 +02:00
|
|
|
}
|
|
|
|
if src.backend == nil {
|
|
|
|
src.allocate(true)
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
// Compare i and source images after ensuring i is not on an atlas, or
|
|
|
|
// i and a source image might share the same atlas even though i != src.
|
2020-06-29 06:24:49 +02:00
|
|
|
if i.backend.restorable == src.backend.restorable {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: Image.DrawTriangles: source must be different from the receiver")
|
2020-06-29 06:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2022-04-03 19:15:33 +02:00
|
|
|
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms [][]float32, evenOdd bool) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
2021-02-26 04:56:22 +01:00
|
|
|
defer backendsM.Unlock()
|
2021-07-02 12:26:09 +02:00
|
|
|
i.drawTriangles(srcs, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, subimageOffsets, shader, uniforms, evenOdd, false)
|
2021-02-26 04:56:22 +01:00
|
|
|
}
|
2018-04-29 11:51:48 +02:00
|
|
|
|
2022-04-03 19:15:33 +02:00
|
|
|
func (i *Image) drawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms [][]float32, evenOdd bool, keepOnAtlas bool) {
|
2018-04-29 20:34:35 +02:00
|
|
|
if i.disposed {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: the drawing target image must not be disposed (DrawTriangles)")
|
2018-04-29 20:34:35 +02:00
|
|
|
}
|
2021-03-11 15:13:24 +01:00
|
|
|
if keepOnAtlas {
|
2021-02-26 04:56:22 +01:00
|
|
|
if i.backend == nil {
|
|
|
|
i.allocate(true)
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-11 15:13:24 +01:00
|
|
|
i.ensureIsolated()
|
2021-02-26 04:56:22 +01:00
|
|
|
}
|
2018-03-10 15:48:10 +01:00
|
|
|
|
2020-07-17 18:09:58 +02:00
|
|
|
for _, src := range srcs {
|
2020-07-05 18:55:46 +02:00
|
|
|
i.processSrc(src)
|
2018-03-10 15:48:10 +01:00
|
|
|
}
|
|
|
|
|
2022-06-09 17:14:04 +02:00
|
|
|
x, y, _, _ := i.regionWithPadding()
|
|
|
|
dx := float32(x + i.paddingSize())
|
|
|
|
dy := float32(y + i.paddingSize())
|
|
|
|
// TODO: Check if dstRegion does not to violate the region.
|
|
|
|
|
2020-11-07 11:14:06 +01:00
|
|
|
dstRegion.X += dx
|
2021-02-26 04:56:22 +01:00
|
|
|
dstRegion.Y += dy
|
2020-11-07 11:14:06 +01:00
|
|
|
|
2020-06-03 18:35:35 +02:00
|
|
|
var oxf, oyf float32
|
2020-07-17 18:09:58 +02:00
|
|
|
if srcs[0] != nil {
|
|
|
|
ox, oy, _, _ := srcs[0].regionWithPadding()
|
2022-06-09 17:14:04 +02:00
|
|
|
ox += srcs[0].paddingSize()
|
|
|
|
oy += srcs[0].paddingSize()
|
2020-06-03 18:35:35 +02:00
|
|
|
oxf, oyf = float32(ox), float32(oy)
|
2022-04-01 17:32:16 +02:00
|
|
|
sw, sh := srcs[0].backend.restorable.InternalSize()
|
|
|
|
swf, shf := float32(sw), float32(sh)
|
2021-09-22 17:19:23 +02:00
|
|
|
n := len(vertices)
|
|
|
|
for i := 0; i < n; i += graphics.VertexFloatNum {
|
2022-04-01 21:59:30 +02:00
|
|
|
vertices[i] = adjustDestinationPixel(vertices[i] + dx)
|
|
|
|
vertices[i+1] = adjustDestinationPixel(vertices[i+1] + dy)
|
2022-04-01 17:32:16 +02:00
|
|
|
vertices[i+2] = (vertices[i+2] + oxf) / swf
|
|
|
|
vertices[i+3] = (vertices[i+3] + oyf) / shf
|
2020-06-03 18:35:35 +02:00
|
|
|
}
|
2020-11-07 11:14:06 +01:00
|
|
|
// srcRegion can be delibarately empty when this is not needed in order to avoid unexpected
|
2020-08-11 19:12:32 +02:00
|
|
|
// performance issue (#1293).
|
2020-11-07 11:14:06 +01:00
|
|
|
if srcRegion.Width != 0 && srcRegion.Height != 0 {
|
|
|
|
srcRegion.X += oxf
|
|
|
|
srcRegion.Y += oyf
|
2020-08-11 19:12:32 +02:00
|
|
|
}
|
2020-08-26 19:00:20 +02:00
|
|
|
} else {
|
2021-09-22 17:19:23 +02:00
|
|
|
n := len(vertices)
|
|
|
|
for i := 0; i < n; i += graphics.VertexFloatNum {
|
2022-04-01 21:59:30 +02:00
|
|
|
vertices[i] = adjustDestinationPixel(vertices[i] + dx)
|
|
|
|
vertices[i+1] = adjustDestinationPixel(vertices[i+1] + dy)
|
2020-08-26 19:00:20 +02:00
|
|
|
}
|
2019-09-20 17:41:53 +02:00
|
|
|
}
|
|
|
|
|
2020-07-18 12:56:22 +02:00
|
|
|
var offsets [graphics.ShaderImageNum - 1][2]float32
|
2020-05-26 15:26:55 +02:00
|
|
|
var s *restorable.Shader
|
2020-07-17 18:09:58 +02:00
|
|
|
var imgs [graphics.ShaderImageNum]*restorable.Image
|
2020-09-20 22:36:47 +02:00
|
|
|
if shader == nil {
|
|
|
|
// Fast path for rendering without a shader (#1355).
|
|
|
|
imgs[0] = srcs[0].backend.restorable
|
|
|
|
} else {
|
|
|
|
for i, subimageOffset := range subimageOffsets {
|
|
|
|
src := srcs[i+1]
|
|
|
|
if src == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ox, oy, _, _ := src.regionWithPadding()
|
2022-06-09 17:14:04 +02:00
|
|
|
offsets[i][0] = float32(ox+src.paddingSize()) - oxf + subimageOffset[0]
|
|
|
|
offsets[i][1] = float32(oy+src.paddingSize()) - oyf + subimageOffset[1]
|
2020-09-20 22:36:47 +02:00
|
|
|
}
|
|
|
|
s = shader.shader
|
|
|
|
for i, src := range srcs {
|
|
|
|
if src == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
imgs[i] = src.backend.restorable
|
2020-07-17 18:09:58 +02:00
|
|
|
}
|
2020-05-26 15:26:55 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
i.backend.restorable.DrawTriangles(imgs, offsets, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, s, uniforms, evenOdd)
|
2018-07-11 18:40:24 +02:00
|
|
|
|
2020-07-17 18:09:58 +02:00
|
|
|
for _, src := range srcs {
|
|
|
|
if src == nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-11 15:13:24 +01:00
|
|
|
if !src.isOnAtlas() && src.canBePutOnAtlas() {
|
2020-11-05 16:48:32 +01:00
|
|
|
// src might already registered, but assiging it again is not harmful.
|
2021-03-11 15:13:24 +01:00
|
|
|
imagesToPutOnAtlas[src] = struct{}{}
|
2020-07-17 18:09:58 +02:00
|
|
|
}
|
2019-05-11 16:16:05 +02:00
|
|
|
}
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 10:07:29 +01:00
|
|
|
// ReplacePixels replaces the pixels on the image.
|
|
|
|
// ReplacePixels cannot take a region due to the current implementation.
|
|
|
|
// internal/restorable.Image has to record the areas of replaced pixels, and the areas must not be overlapped so far.
|
2022-03-20 19:56:04 +01:00
|
|
|
func (i *Image) ReplacePixels(pix []byte, mask []byte) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
2022-03-20 19:56:04 +01:00
|
|
|
i.replacePixels(pix, mask)
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
2018-03-10 16:28:30 +01:00
|
|
|
|
2022-03-20 19:56:04 +01:00
|
|
|
func (i *Image) replacePixels(pix []byte, mask []byte) {
|
2018-04-29 20:34:35 +02:00
|
|
|
if i.disposed {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: the image must not be disposed at replacePixels")
|
2018-04-29 20:34:35 +02:00
|
|
|
}
|
2020-11-05 18:10:03 +01:00
|
|
|
|
2021-01-17 10:22:45 +01:00
|
|
|
i.resetUsedAsSourceCount()
|
2020-11-05 18:10:03 +01: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)
|
|
|
|
}
|
|
|
|
|
2022-03-19 19:51:57 +01:00
|
|
|
px, py, pw, ph := i.regionWithPadding()
|
2020-06-14 09:03:26 +02:00
|
|
|
if pix == nil {
|
2022-03-21 07:11:38 +01:00
|
|
|
if mask != nil {
|
|
|
|
panic("atlas: mask must be nil when pix is nil")
|
|
|
|
}
|
2022-03-20 18:08:37 +01:00
|
|
|
i.backend.restorable.ReplacePixels(nil, nil, px, py, pw, ph)
|
2020-06-14 09:03:26 +02:00
|
|
|
return
|
2018-03-10 15:36:07 +01:00
|
|
|
}
|
2020-06-14 09:03:26 +02:00
|
|
|
|
2022-06-09 17:14:04 +02:00
|
|
|
ow, oh := pw-2*i.paddingSize(), ph-2*i.paddingSize()
|
2020-06-14 09:03:26 +02:00
|
|
|
if l := 4 * ow * oh; len(pix) != l {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic(fmt.Sprintf("atlas: len(p) must be %d but %d", l, len(pix)))
|
2020-06-14 09:03:26 +02:00
|
|
|
}
|
|
|
|
|
2022-06-09 17:32:55 +02:00
|
|
|
if i.paddingSize() == 0 {
|
|
|
|
pix2 := make([]byte, len(pix))
|
|
|
|
copy(pix2, pix)
|
|
|
|
var mask2 []byte
|
|
|
|
if mask != nil {
|
|
|
|
mask2 = make([]byte, len(mask))
|
|
|
|
copy(mask2, mask)
|
|
|
|
}
|
|
|
|
i.backend.restorable.ReplacePixels(pix2, mask2, px, py, pw, ph)
|
|
|
|
return
|
|
|
|
}
|
2022-06-09 17:14:04 +02:00
|
|
|
|
2022-03-21 07:37:21 +01:00
|
|
|
pixb := theTemporaryBytes.alloc(4 * pw * ph)
|
2021-08-25 13:01:30 +02:00
|
|
|
|
2021-08-25 20:35:18 +02:00
|
|
|
// Clear the edges. pixb might not be zero-cleared.
|
2022-03-21 07:37:21 +01:00
|
|
|
// TODO: These loops assume that paddingSize is 1.
|
2022-06-09 17:14:04 +02:00
|
|
|
const paddingSize = 1
|
|
|
|
if paddingSize != i.paddingSize() {
|
|
|
|
panic(fmt.Sprintf("atlas: replacePixels assumes the padding is always 1 but the actual padding was %d", i.paddingSize()))
|
|
|
|
}
|
2022-03-19 19:51:57 +01:00
|
|
|
rowPixels := 4 * pw
|
2021-08-25 20:35:18 +02:00
|
|
|
for i := 0; i < rowPixels; i++ {
|
|
|
|
pixb[i] = 0
|
2022-03-21 07:37:21 +01:00
|
|
|
pixb[rowPixels*(ph-1)+i] = 0
|
2021-08-25 20:35:18 +02:00
|
|
|
}
|
2022-03-19 19:51:57 +01:00
|
|
|
for j := 1; j < ph-1; j++ {
|
2021-08-25 20:35:18 +02:00
|
|
|
pixb[rowPixels*j] = 0
|
|
|
|
pixb[rowPixels*j+1] = 0
|
|
|
|
pixb[rowPixels*j+2] = 0
|
|
|
|
pixb[rowPixels*j+3] = 0
|
|
|
|
pixb[rowPixels*(j+1)-4] = 0
|
|
|
|
pixb[rowPixels*(j+1)-3] = 0
|
|
|
|
pixb[rowPixels*(j+1)-2] = 0
|
|
|
|
pixb[rowPixels*(j+1)-1] = 0
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:01:30 +02:00
|
|
|
// Copy the content.
|
2020-06-14 09:03:26 +02:00
|
|
|
for j := 0; j < oh; j++ {
|
2022-03-19 19:51:57 +01:00
|
|
|
copy(pixb[4*((j+paddingSize)*pw+paddingSize):], pix[4*j*ow:4*(j+1)*ow])
|
2020-06-14 09:03:26 +02:00
|
|
|
}
|
|
|
|
|
2022-03-21 07:37:21 +01:00
|
|
|
// Add the paddings to the mask if needed.
|
|
|
|
if mask != nil {
|
|
|
|
origMask := mask
|
|
|
|
mask = theTemporaryBytes.alloc((pw*ph-1)/8 + 1)
|
2022-05-08 16:05:05 +02:00
|
|
|
// Clear the allocated region explicitly (#2089).
|
|
|
|
for i := range mask {
|
|
|
|
mask[i] = 0
|
|
|
|
}
|
2022-03-21 07:37:21 +01:00
|
|
|
for i := 0; i < pw; i++ {
|
|
|
|
// Top edge
|
|
|
|
idx := i
|
2022-05-08 16:05:05 +02:00
|
|
|
mask[idx/8] |= 1 << (idx % 8)
|
2022-03-21 07:37:21 +01:00
|
|
|
// Bottom edge
|
|
|
|
idx = (ph-1)*pw + i
|
2022-05-08 16:05:05 +02:00
|
|
|
mask[idx/8] |= 1 << (idx % 8)
|
2022-03-21 07:37:21 +01:00
|
|
|
}
|
|
|
|
for j := 1; j < ph-1; j++ {
|
|
|
|
// Left edge
|
|
|
|
idx := j * pw
|
2022-05-08 16:05:05 +02:00
|
|
|
mask[idx/8] |= 1 << (idx % 8)
|
2022-03-21 07:37:21 +01:00
|
|
|
// Right edge
|
|
|
|
idx = j*pw + pw - 1
|
2022-05-08 16:05:05 +02:00
|
|
|
mask[idx/8] |= 1 << (idx % 8)
|
2022-03-21 07:37:21 +01:00
|
|
|
|
|
|
|
// Content
|
|
|
|
for i := 1; i < pw-1; i++ {
|
|
|
|
idx := j*pw + i
|
|
|
|
origIdx := (j-paddingSize)*(pw-paddingSize*2) + i - paddingSize
|
|
|
|
origValue := (origMask[origIdx/8] >> (origIdx % 8)) & 1
|
|
|
|
mask[idx/8] |= origValue << (idx % 8)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i.backend.restorable.ReplacePixels(pixb, mask, px, py, pw, ph)
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 10:16:57 +01:00
|
|
|
func (img *Image) Pixels(graphicsDriver graphicsdriver.Graphics) ([]byte, error) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
2020-04-18 13:01:40 +02:00
|
|
|
defer backendsM.Unlock()
|
|
|
|
|
2022-06-09 17:14:04 +02:00
|
|
|
x := img.paddingSize()
|
|
|
|
y := img.paddingSize()
|
2020-06-14 09:03:26 +02:00
|
|
|
|
2022-03-20 10:16:57 +01:00
|
|
|
bs := make([]byte, 4*img.width*img.height)
|
2020-04-18 13:01:40 +02:00
|
|
|
idx := 0
|
2022-03-20 10:16:57 +01:00
|
|
|
for j := y; j < y+img.height; j++ {
|
|
|
|
for i := x; i < x+img.width; i++ {
|
2022-03-19 15:55:14 +01:00
|
|
|
r, g, b, a, err := img.at(graphicsDriver, i, j)
|
2020-04-18 13:01:40 +02:00
|
|
|
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
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func (i *Image) at(graphicsDriver graphicsdriver.Graphics, 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
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
return i.backend.restorable.At(graphicsDriver, 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() {
|
2022-06-08 04:18:08 +02:00
|
|
|
// As MarkDisposed can be invoked from finalizers, backendsM should not be used.
|
2019-09-21 12:54:39 +02:00
|
|
|
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
|
|
|
}()
|
|
|
|
|
2021-01-17 10:22:45 +01:00
|
|
|
i.resetUsedAsSourceCount()
|
2020-11-12 15:37:49 +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
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
if !i.isOnAtlas() {
|
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 {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: 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
|
|
|
}
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
func NewImage(width, height int, imageType ImageType) *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{
|
2022-06-07 16:57:56 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
imageType: imageType,
|
2022-06-06 01:25:43 +02:00
|
|
|
}
|
2020-08-18 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
func (i *Image) canBePutOnAtlas() bool {
|
2019-08-23 18:06:14 +02:00
|
|
|
if minSize == 0 || maxSize == 0 {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: minSize or maxSize must be initialized")
|
2019-06-22 13:17:52 +02:00
|
|
|
}
|
2022-06-07 16:57:56 +02:00
|
|
|
if i.imageType != ImageTypeRegular {
|
2018-07-24 15:25:15 +02:00
|
|
|
return false
|
|
|
|
}
|
2022-06-09 17:14:04 +02:00
|
|
|
return i.width+2*i.paddingSize() <= maxSize && i.height+2*i.paddingSize() <= maxSize
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
func (i *Image) allocate(putOnAtlas bool) {
|
2018-04-29 20:26:58 +02:00
|
|
|
if i.backend != nil {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: 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
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
if i.imageType == ImageTypeScreen {
|
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{
|
2022-06-07 16:45:35 +02:00
|
|
|
restorable: restorable.NewImage(i.width, i.height, restorable.ImageTypeScreen),
|
2019-08-24 17:43:26 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
if !putOnAtlas || !i.canBePutOnAtlas() {
|
2022-06-06 02:29:30 +02:00
|
|
|
typ := restorable.ImageTypeRegular
|
2022-06-07 16:57:56 +02:00
|
|
|
if i.imageType == ImageTypeVolatile {
|
2022-06-06 02:29:30 +02:00
|
|
|
typ = restorable.ImageTypeVolatile
|
|
|
|
}
|
2018-04-29 11:51:48 +02:00
|
|
|
i.backend = &backend{
|
2022-06-09 17:14:04 +02:00
|
|
|
restorable: restorable.NewImage(i.width+2*i.paddingSize(), i.height+2*i.paddingSize(), typ),
|
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 {
|
2022-06-09 17:14:04 +02:00
|
|
|
if n, ok := b.tryAlloc(i.width+2*i.paddingSize(), i.height+2*i.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
|
2022-06-09 17:14:04 +02:00
|
|
|
for i.width+2*i.paddingSize() > size || i.height+2*i.paddingSize() > size {
|
2018-03-25 15:41:15 +02:00
|
|
|
if size == maxSize {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic(fmt.Sprintf("atlas: the image being put on an atlas is too big: width: %d, height: %d", i.width, i.height))
|
2018-03-25 15:41:15 +02:00
|
|
|
}
|
|
|
|
size *= 2
|
|
|
|
}
|
|
|
|
|
2022-06-06 02:29:30 +02:00
|
|
|
typ := restorable.ImageTypeRegular
|
2022-06-07 16:57:56 +02:00
|
|
|
if i.imageType == ImageTypeVolatile {
|
2022-06-06 02:29:30 +02:00
|
|
|
typ = restorable.ImageTypeVolatile
|
|
|
|
}
|
2018-03-23 20:27:10 +01:00
|
|
|
b := &backend{
|
2022-06-06 02:29:30 +02:00
|
|
|
restorable: restorable.NewImage(size, size, typ),
|
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
|
|
|
|
2022-06-09 17:14:04 +02:00
|
|
|
n := b.page.Alloc(i.width+2*i.paddingSize(), i.height+2*i.paddingSize())
|
2018-03-03 10:51:52 +01:00
|
|
|
if n == nil {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: 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
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool) error {
|
2019-07-19 18:42:19 +02:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
|
2022-06-09 17:14:04 +02:00
|
|
|
return i.backend.restorable.Dump(graphicsDriver, path, blackbg, image.Rect(i.paddingSize(), i.paddingSize(), i.width+i.paddingSize(), i.height+i.paddingSize()))
|
2019-07-19 18:42:19 +02:00
|
|
|
}
|
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func EndFrame(graphicsDriver graphicsdriver.Graphics) error {
|
2018-03-25 16:37:32 +02:00
|
|
|
backendsM.Lock()
|
2019-08-25 17:32:30 +02:00
|
|
|
|
2022-03-21 07:37:21 +01:00
|
|
|
theTemporaryBytes.resetAtFrameEnd()
|
2021-06-25 19:33:14 +02:00
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
return restorable.ResolveStaleImages(graphicsDriver)
|
2018-03-25 16:37:32 +02:00
|
|
|
}
|
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func BeginFrame(graphicsDriver graphicsdriver.Graphics) 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() {
|
2022-03-19 15:55:14 +01:00
|
|
|
err = restorable.InitializeGraphicsDriverState(graphicsDriver)
|
2019-08-25 10:32:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(theBackends) != 0 {
|
2021-03-11 15:13:24 +01:00
|
|
|
panic("atlas: all the images must be not on an atlas before the game starts")
|
2019-08-25 10:32:32 +02:00
|
|
|
}
|
2021-06-24 14:31:31 +02:00
|
|
|
minSize = 1024
|
2022-03-19 15:55:14 +01:00
|
|
|
maxSize = restorable.MaxImageSize(graphicsDriver)
|
2019-08-25 10:28:59 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-17 19:19:44 +02:00
|
|
|
// Restore images first before other image manipulations (#2075).
|
|
|
|
if err := restorable.RestoreIfNeeded(graphicsDriver); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-13 19:45:56 +01:00
|
|
|
resolveDeferred()
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := putImagesOnAtlas(graphicsDriver); err != nil {
|
2022-02-13 19:45:56 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-17 19:19:44 +02:00
|
|
|
return nil
|
2018-03-25 16:37:32 +02:00
|
|
|
}
|
2018-04-05 18:12:08 +02:00
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) error {
|
2018-04-28 14:49:00 +02:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
2022-03-19 15:55:14 +01:00
|
|
|
return restorable.DumpImages(graphicsDriver, dir)
|
2018-04-28 14:49:00 +02:00
|
|
|
}
|
2022-04-01 21:59:30 +02:00
|
|
|
|
|
|
|
func adjustDestinationPixel(x float32) float32 {
|
|
|
|
// Avoid the center of the pixel, which is problematic (#929, #1171).
|
|
|
|
// Instead, align the vertices with about 1/3 pixels.
|
|
|
|
ix := float32(math.Floor(float64(x)))
|
|
|
|
frac := x - ix
|
|
|
|
switch {
|
|
|
|
case frac < 3.0/16.0:
|
|
|
|
return ix
|
|
|
|
case frac < 8.0/16.0:
|
|
|
|
return ix + 5.0/16.0
|
|
|
|
case frac < 13.0/16.0:
|
|
|
|
return ix + 11.0/16.0
|
|
|
|
default:
|
|
|
|
return ix + 16.0/16.0
|
|
|
|
}
|
|
|
|
}
|