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-03-10 15:36:07 +01:00
|
|
|
"fmt"
|
2018-03-10 15:27:16 +01:00
|
|
|
"image/color"
|
2018-03-10 16:07:32 +01:00
|
|
|
"runtime"
|
2018-03-10 15:27:16 +01:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/affine"
|
2018-03-10 15:14:59 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
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-03-03 12:50:26 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/sync"
|
2018-03-03 10:51:52 +01: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) {
|
|
|
|
// If the region is allocated without any extention, 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 {
|
|
|
|
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()
|
|
|
|
newImg.DrawImage(oldImg, 0, 0, w, h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
|
|
|
|
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-03-10 16:02:23 +01:00
|
|
|
backend *backend
|
2018-03-10 15:14:59 +01:00
|
|
|
|
|
|
|
// If node is nil, the image is not shared.
|
|
|
|
node *packing.Node
|
|
|
|
}
|
|
|
|
|
2018-03-10 16:05:06 +01:00
|
|
|
func (s *Image) ensureNotShared() {
|
2018-03-10 15:14:59 +01:00
|
|
|
if s.node == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
x, y, w, h := s.region()
|
|
|
|
newImg := restorable.NewImage(w, h, false)
|
2018-03-10 16:02:23 +01:00
|
|
|
newImg.DrawImage(s.backend.restorable, x, y, w, h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
|
2018-03-10 15:14:59 +01:00
|
|
|
|
2018-03-10 16:28:30 +01:00
|
|
|
s.dispose()
|
2018-03-10 16:02:23 +01:00
|
|
|
s.backend = &backend{
|
2018-03-10 15:14:59 +01:00
|
|
|
restorable: newImg,
|
|
|
|
}
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 16:05:06 +01:00
|
|
|
func (s *Image) region() (x, y, width, height int) {
|
2018-03-10 15:14:59 +01:00
|
|
|
if s.node == nil {
|
2018-03-10 16:02:23 +01:00
|
|
|
w, h := s.backend.restorable.Size()
|
2018-03-10 15:14:59 +01:00
|
|
|
return 0, 0, w, h
|
|
|
|
}
|
2018-03-03 10:51:52 +01:00
|
|
|
return s.node.Region()
|
|
|
|
}
|
|
|
|
|
2018-03-10 16:05:06 +01:00
|
|
|
func (s *Image) Size() (width, height int) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
2018-03-10 15:36:07 +01:00
|
|
|
_, _, w, h := s.region()
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Unlock()
|
2018-03-10 15:36:07 +01:00
|
|
|
return w, h
|
|
|
|
}
|
|
|
|
|
2018-03-10 16:05:06 +01:00
|
|
|
func (s *Image) DrawImage(img *Image, sx0, sy0, sx1, sy1 int, geom *affine.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
s.drawImage(img, sx0, sy0, sx1, sy1, geom, colorm, mode, filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Image) drawImage(img *Image, sx0, sy0, sx1, sy1 int, geom *affine.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
|
2018-03-10 15:48:10 +01:00
|
|
|
s.ensureNotShared()
|
|
|
|
|
|
|
|
// Compare i and img after ensuring i is not shared, or
|
|
|
|
// i and img might share the same texture even though i != img.
|
2018-03-10 16:02:23 +01:00
|
|
|
if s.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-03-10 15:36:07 +01:00
|
|
|
dx, dy, _, _ := img.region()
|
|
|
|
sx0 += dx
|
|
|
|
sy0 += dy
|
|
|
|
sx1 += dx
|
|
|
|
sy1 += dy
|
2018-03-10 16:02:23 +01:00
|
|
|
s.backend.restorable.DrawImage(img.backend.restorable, sx0, sy0, sx1, sy1, geom, colorm, mode, filter)
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 16:05:06 +01:00
|
|
|
func (s *Image) ReplacePixels(p []byte) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
|
2018-03-10 15:36:07 +01:00
|
|
|
x, y, w, h := s.region()
|
|
|
|
if l := 4 * w * h; len(p) != l {
|
2018-03-10 15:48:10 +01:00
|
|
|
panic(fmt.Sprintf("shareable: len(p) was %d but must be %d", len(p), l))
|
2018-03-10 15:36:07 +01:00
|
|
|
}
|
2018-03-10 16:02:23 +01:00
|
|
|
s.backend.restorable.ReplacePixels(p, x, y, w, h)
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 16:05:06 +01:00
|
|
|
func (s *Image) At(x, y int) (color.Color, error) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
|
|
|
|
2018-03-10 15:36:07 +01:00
|
|
|
ox, oy, w, h := s.region()
|
|
|
|
if x < 0 || y < 0 || x >= w || y >= h {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Unlock()
|
2018-03-10 15:36:07 +01:00
|
|
|
return color.RGBA{}, nil
|
|
|
|
}
|
2018-03-10 16:28:30 +01:00
|
|
|
|
|
|
|
clr, err := s.backend.restorable.At(x+ox, y+oy)
|
|
|
|
backendsM.Unlock()
|
|
|
|
return clr, err
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 16:05:06 +01:00
|
|
|
func (s *Image) isDisposed() bool {
|
2018-03-10 16:02:23 +01:00
|
|
|
return s.backend == nil
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 16:05:06 +01:00
|
|
|
func (s *Image) Dispose() {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
|
|
|
s.dispose()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Image) dispose() {
|
2018-03-10 15:14:59 +01:00
|
|
|
if s.isDisposed() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2018-03-10 16:02:23 +01:00
|
|
|
s.backend = nil
|
2018-03-10 15:14:59 +01:00
|
|
|
s.node = nil
|
2018-03-10 16:07:32 +01:00
|
|
|
runtime.SetFinalizer(s, nil)
|
2018-03-10 15:14:59 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
if s.node == nil {
|
2018-03-10 16:02:23 +01:00
|
|
|
s.backend.restorable.Dispose()
|
2018-03-10 15:14:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-10 16:02:23 +01:00
|
|
|
s.backend.page.Free(s.node)
|
|
|
|
if !s.backend.page.IsEmpty() {
|
2018-03-10 15:14:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
index := -1
|
2018-03-10 16:02:23 +01:00
|
|
|
for i, sh := range theBackends {
|
|
|
|
if sh == s.backend {
|
2018-03-10 15:14:59 +01:00
|
|
|
index = i
|
|
|
|
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-03-10 16:05:06 +01:00
|
|
|
func (s *Image) IsInvalidated() (bool, error) {
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
|
|
|
v, err := s.backend.restorable.IsInvalidated()
|
|
|
|
backendsM.Unlock()
|
|
|
|
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-03-25 15:41:15 +02:00
|
|
|
const (
|
|
|
|
initSize = 1024
|
|
|
|
maxSize = 4096
|
|
|
|
)
|
2018-03-10 12:53:20 +01:00
|
|
|
|
2018-03-10 16:28:30 +01:00
|
|
|
backendsM.Lock()
|
|
|
|
defer backendsM.Unlock()
|
2018-03-03 12:50:26 +01:00
|
|
|
|
2018-03-10 12:53:20 +01:00
|
|
|
if width > maxSize || height > maxSize {
|
2018-03-23 20:27:10 +01:00
|
|
|
b := &backend{
|
2018-03-10 15:14:59 +01:00
|
|
|
restorable: restorable.NewImage(width, height, false),
|
|
|
|
}
|
2018-03-10 16:05:06 +01:00
|
|
|
return &Image{
|
2018-03-23 20:27:10 +01:00
|
|
|
backend: b,
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
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-03-25 15:41:15 +02:00
|
|
|
if n, ok := b.TryAlloc(width, height); ok {
|
2018-03-15 17:21:33 +01:00
|
|
|
return &Image{
|
2018-03-23 20:27:10 +01:00
|
|
|
backend: b,
|
2018-03-15 17:21:33 +01:00
|
|
|
node: n,
|
2018-03-10 17:30:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-25 15:41:15 +02:00
|
|
|
size := initSize
|
|
|
|
for width > size || height > size {
|
|
|
|
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-03-23 20:27:10 +01:00
|
|
|
n := b.page.Alloc(width, height)
|
2018-03-03 10:51:52 +01:00
|
|
|
if n == nil {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
2018-03-10 16:07:32 +01:00
|
|
|
i := &Image{
|
2018-03-23 20:27:10 +01:00
|
|
|
backend: b,
|
2018-03-10 16:02:23 +01:00
|
|
|
node: n,
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
2018-03-10 16:07:32 +01:00
|
|
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
|
|
|
return i
|
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-10 15:48:10 +01:00
|
|
|
r := restorable.NewImage(width, height, true)
|
2018-03-10 16:07:32 +01:00
|
|
|
i := &Image{
|
2018-03-10 16:02:23 +01:00
|
|
|
backend: &backend{
|
2018-03-10 15:48:10 +01:00
|
|
|
restorable: r,
|
|
|
|
},
|
|
|
|
}
|
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-10 15:48:10 +01:00
|
|
|
r := restorable.NewScreenFramebufferImage(width, height)
|
2018-03-10 16:07:32 +01:00
|
|
|
i := &Image{
|
2018-03-10 16:02:23 +01:00
|
|
|
backend: &backend{
|
2018-03-10 15:48:10 +01:00
|
|
|
restorable: r,
|
|
|
|
},
|
|
|
|
}
|
2018-03-10 16:07:32 +01:00
|
|
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
|
|
|
return i
|
2018-03-10 15:48:10 +01:00
|
|
|
}
|