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.
|
|
|
|
|
|
|
|
package ebiten
|
|
|
|
|
|
|
|
import (
|
2018-03-10 15:36:07 +01:00
|
|
|
"fmt"
|
2018-03-10 15:27:16 +01:00
|
|
|
"image/color"
|
|
|
|
|
|
|
|
"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 15:38:27 +01:00
|
|
|
type shareableImage struct {
|
2018-03-03 10:51:52 +01:00
|
|
|
restorable *restorable.Image
|
2018-03-10 12:53:20 +01:00
|
|
|
page *packing.Page
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2018-03-10 15:38:27 +01:00
|
|
|
// theSharedImages is a set of actually shared images.
|
|
|
|
theSharedImages = []*shareableImage{}
|
2018-03-03 10:51:52 +01:00
|
|
|
)
|
|
|
|
|
2018-03-10 15:38:27 +01:00
|
|
|
type shareableImagePart struct {
|
|
|
|
shareableImage *shareableImage
|
2018-03-10 15:14:59 +01:00
|
|
|
|
|
|
|
// If node is nil, the image is not shared.
|
|
|
|
node *packing.Node
|
|
|
|
}
|
|
|
|
|
2018-03-10 15:38:27 +01:00
|
|
|
func (s *shareableImagePart) 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 15:38:27 +01:00
|
|
|
newImg.DrawImage(s.shareableImage.restorable, x, y, w, h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
|
2018-03-10 15:14:59 +01:00
|
|
|
|
|
|
|
s.Dispose()
|
2018-03-10 15:38:27 +01:00
|
|
|
s.shareableImage = &shareableImage{
|
2018-03-10 15:14:59 +01:00
|
|
|
restorable: newImg,
|
|
|
|
}
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 15:38:27 +01:00
|
|
|
func (s *shareableImagePart) region() (x, y, width, height int) {
|
2018-03-10 15:14:59 +01:00
|
|
|
if s.node == nil {
|
2018-03-10 15:38:27 +01:00
|
|
|
w, h := s.shareableImage.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 15:38:27 +01:00
|
|
|
func (s *shareableImagePart) Size() (width, height int) {
|
2018-03-10 15:36:07 +01:00
|
|
|
_, _, w, h := s.region()
|
|
|
|
return w, h
|
|
|
|
}
|
|
|
|
|
2018-03-10 15:38:27 +01:00
|
|
|
func (s *shareableImagePart) DrawImage(img *shareableImagePart, sx0, sy0, sx1, sy1 int, geom *affine.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
|
2018-03-10 15:36:07 +01:00
|
|
|
dx, dy, _, _ := img.region()
|
|
|
|
sx0 += dx
|
|
|
|
sy0 += dy
|
|
|
|
sx1 += dx
|
|
|
|
sy1 += dy
|
2018-03-10 15:38:27 +01:00
|
|
|
s.shareableImage.restorable.DrawImage(img.shareableImage.restorable, sx0, sy0, sx1, sy1, geom, colorm, mode, filter)
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 15:38:27 +01:00
|
|
|
func (s *shareableImagePart) ReplacePixels(p []byte) {
|
2018-03-10 15:36:07 +01:00
|
|
|
x, y, w, h := s.region()
|
|
|
|
if l := 4 * w * h; len(p) != l {
|
|
|
|
panic(fmt.Sprintf("ebiten: len(p) was %d but must be %d", len(p), l))
|
|
|
|
}
|
2018-03-10 15:38:27 +01:00
|
|
|
s.shareableImage.restorable.ReplacePixels(p, x, y, w, h)
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 15:38:27 +01:00
|
|
|
func (s *shareableImagePart) At(x, y int) (color.Color, error) {
|
2018-03-10 15:36:07 +01:00
|
|
|
ox, oy, w, h := s.region()
|
|
|
|
if x < 0 || y < 0 || x >= w || y >= h {
|
|
|
|
return color.RGBA{}, nil
|
|
|
|
}
|
2018-03-10 15:38:27 +01:00
|
|
|
return s.shareableImage.restorable.At(x+ox, y+oy)
|
2018-03-10 15:27:16 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 15:38:27 +01:00
|
|
|
func (s *shareableImagePart) isDisposed() bool {
|
|
|
|
return s.shareableImage == nil
|
2018-03-10 15:14:59 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 15:38:27 +01:00
|
|
|
func (s *shareableImagePart) Dispose() {
|
2018-03-10 15:14:59 +01:00
|
|
|
if s.isDisposed() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2018-03-10 15:38:27 +01:00
|
|
|
s.shareableImage = nil
|
2018-03-10 15:14:59 +01:00
|
|
|
s.node = nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
if s.node == nil {
|
2018-03-10 15:38:27 +01:00
|
|
|
s.shareableImage.restorable.Dispose()
|
2018-03-10 15:14:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-10 15:38:27 +01:00
|
|
|
s.shareableImage.page.Free(s.node)
|
|
|
|
if !s.shareableImage.page.IsEmpty() {
|
2018-03-10 15:14:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
index := -1
|
|
|
|
for i, sh := range theSharedImages {
|
2018-03-10 15:38:27 +01:00
|
|
|
if sh == s.shareableImage {
|
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")
|
|
|
|
}
|
|
|
|
theSharedImages = append(theSharedImages[:index], theSharedImages[index+1:]...)
|
|
|
|
}
|
|
|
|
|
2018-03-10 15:38:27 +01:00
|
|
|
func (s *shareableImagePart) IsInvalidated() (bool, error) {
|
|
|
|
return s.shareableImage.restorable.IsInvalidated()
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 15:38:27 +01:00
|
|
|
var shareableImageLock sync.Mutex
|
2018-03-03 12:50:26 +01:00
|
|
|
|
2018-03-10 15:38:27 +01:00
|
|
|
func newSharedImagePart(width, height int) *shareableImagePart {
|
2018-03-10 12:53:20 +01:00
|
|
|
const maxSize = 2048
|
|
|
|
|
2018-03-10 15:38:27 +01:00
|
|
|
shareableImageLock.Lock()
|
|
|
|
defer shareableImageLock.Unlock()
|
2018-03-03 12:50:26 +01:00
|
|
|
|
2018-03-10 12:53:20 +01:00
|
|
|
if width > maxSize || height > maxSize {
|
2018-03-10 15:38:27 +01:00
|
|
|
s := &shareableImage{
|
2018-03-10 15:14:59 +01:00
|
|
|
restorable: restorable.NewImage(width, height, false),
|
|
|
|
}
|
2018-03-10 15:38:27 +01:00
|
|
|
return &shareableImagePart{
|
|
|
|
shareableImage: s,
|
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-03 10:51:52 +01:00
|
|
|
for _, s := range theSharedImages {
|
2018-03-09 08:02:57 +01:00
|
|
|
if n := s.page.Alloc(width, height); n != nil {
|
2018-03-10 15:38:27 +01:00
|
|
|
return &shareableImagePart{
|
|
|
|
shareableImage: s,
|
|
|
|
node: n,
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-10 15:38:27 +01:00
|
|
|
s := &shareableImage{
|
2018-03-10 12:53:20 +01:00
|
|
|
restorable: restorable.NewImage(maxSize, maxSize, false),
|
|
|
|
page: packing.NewPage(maxSize),
|
2018-03-06 18:18:08 +01:00
|
|
|
}
|
2018-03-09 08:02:57 +01:00
|
|
|
theSharedImages = append(theSharedImages, s)
|
|
|
|
|
|
|
|
n := s.page.Alloc(width, height)
|
2018-03-03 10:51:52 +01:00
|
|
|
if n == nil {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
2018-03-10 15:38:27 +01:00
|
|
|
return &shareableImagePart{
|
|
|
|
shareableImage: s,
|
|
|
|
node: n,
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
}
|