shareable: Rename Image -> backend

This commit is contained in:
Hajime Hoshi 2018-03-11 00:02:23 +09:00
parent df3960a97c
commit 6c6b25647a

View File

@ -26,18 +26,18 @@ import (
"github.com/hajimehoshi/ebiten/internal/sync" "github.com/hajimehoshi/ebiten/internal/sync"
) )
type Image struct { type backend struct {
restorable *restorable.Image restorable *restorable.Image
page *packing.Page page *packing.Page
} }
var ( var (
// theSharedImages is a set of actually shared images. // theBackends is a set of actually shared images.
theSharedImages = []*Image{} theBackends = []*backend{}
) )
type ImagePart struct { type ImagePart struct {
shareableImage *Image backend *backend
// If node is nil, the image is not shared. // If node is nil, the image is not shared.
node *packing.Node node *packing.Node
@ -50,17 +50,17 @@ func (s *ImagePart) ensureNotShared() {
x, y, w, h := s.region() x, y, w, h := s.region()
newImg := restorable.NewImage(w, h, false) newImg := restorable.NewImage(w, h, false)
newImg.DrawImage(s.shareableImage.restorable, x, y, w, h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest) newImg.DrawImage(s.backend.restorable, x, y, w, h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
s.Dispose() s.Dispose()
s.shareableImage = &Image{ s.backend = &backend{
restorable: newImg, restorable: newImg,
} }
} }
func (s *ImagePart) region() (x, y, width, height int) { func (s *ImagePart) region() (x, y, width, height int) {
if s.node == nil { if s.node == nil {
w, h := s.shareableImage.restorable.Size() w, h := s.backend.restorable.Size()
return 0, 0, w, h return 0, 0, w, h
} }
return s.node.Region() return s.node.Region()
@ -76,7 +76,7 @@ func (s *ImagePart) DrawImage(img *ImagePart, sx0, sy0, sx1, sy1 int, geom *affi
// Compare i and img after ensuring i is not shared, or // Compare i and img after ensuring i is not shared, or
// i and img might share the same texture even though i != img. // i and img might share the same texture even though i != img.
if s.shareableImage.restorable == img.shareableImage.restorable { if s.backend.restorable == img.backend.restorable {
panic("shareable: Image.DrawImage: img must be different from the receiver") panic("shareable: Image.DrawImage: img must be different from the receiver")
} }
@ -85,7 +85,7 @@ func (s *ImagePart) DrawImage(img *ImagePart, sx0, sy0, sx1, sy1 int, geom *affi
sy0 += dy sy0 += dy
sx1 += dx sx1 += dx
sy1 += dy sy1 += dy
s.shareableImage.restorable.DrawImage(img.shareableImage.restorable, sx0, sy0, sx1, sy1, geom, colorm, mode, filter) s.backend.restorable.DrawImage(img.backend.restorable, sx0, sy0, sx1, sy1, geom, colorm, mode, filter)
} }
func (s *ImagePart) ReplacePixels(p []byte) { func (s *ImagePart) ReplacePixels(p []byte) {
@ -93,7 +93,7 @@ func (s *ImagePart) ReplacePixels(p []byte) {
if l := 4 * w * h; len(p) != l { if l := 4 * w * h; len(p) != l {
panic(fmt.Sprintf("shareable: len(p) was %d but must be %d", len(p), l)) panic(fmt.Sprintf("shareable: len(p) was %d but must be %d", len(p), l))
} }
s.shareableImage.restorable.ReplacePixels(p, x, y, w, h) s.backend.restorable.ReplacePixels(p, x, y, w, h)
} }
func (s *ImagePart) At(x, y int) (color.Color, error) { func (s *ImagePart) At(x, y int) (color.Color, error) {
@ -101,11 +101,11 @@ func (s *ImagePart) At(x, y int) (color.Color, error) {
if x < 0 || y < 0 || x >= w || y >= h { if x < 0 || y < 0 || x >= w || y >= h {
return color.RGBA{}, nil return color.RGBA{}, nil
} }
return s.shareableImage.restorable.At(x+ox, y+oy) return s.backend.restorable.At(x+ox, y+oy)
} }
func (s *ImagePart) isDisposed() bool { func (s *ImagePart) isDisposed() bool {
return s.shareableImage == nil return s.backend == nil
} }
func (s *ImagePart) Dispose() { func (s *ImagePart) Dispose() {
@ -114,23 +114,23 @@ func (s *ImagePart) Dispose() {
} }
defer func() { defer func() {
s.shareableImage = nil s.backend = nil
s.node = nil s.node = nil
}() }()
if s.node == nil { if s.node == nil {
s.shareableImage.restorable.Dispose() s.backend.restorable.Dispose()
return return
} }
s.shareableImage.page.Free(s.node) s.backend.page.Free(s.node)
if !s.shareableImage.page.IsEmpty() { if !s.backend.page.IsEmpty() {
return return
} }
index := -1 index := -1
for i, sh := range theSharedImages { for i, sh := range theBackends {
if sh == s.shareableImage { if sh == s.backend {
index = i index = i
break break
} }
@ -138,11 +138,11 @@ func (s *ImagePart) Dispose() {
if index == -1 { if index == -1 {
panic("not reached") panic("not reached")
} }
theSharedImages = append(theSharedImages[:index], theSharedImages[index+1:]...) theBackends = append(theBackends[:index], theBackends[index+1:]...)
} }
func (s *ImagePart) IsInvalidated() (bool, error) { func (s *ImagePart) IsInvalidated() (bool, error) {
return s.shareableImage.restorable.IsInvalidated() return s.backend.restorable.IsInvalidated()
} }
var shareableImageLock sync.Mutex var shareableImageLock sync.Mutex
@ -154,42 +154,42 @@ func NewImagePart(width, height int) *ImagePart {
defer shareableImageLock.Unlock() defer shareableImageLock.Unlock()
if width > maxSize || height > maxSize { if width > maxSize || height > maxSize {
s := &Image{ s := &backend{
restorable: restorable.NewImage(width, height, false), restorable: restorable.NewImage(width, height, false),
} }
return &ImagePart{ return &ImagePart{
shareableImage: s, backend: s,
} }
} }
for _, s := range theSharedImages { for _, s := range theBackends {
if n := s.page.Alloc(width, height); n != nil { if n := s.page.Alloc(width, height); n != nil {
return &ImagePart{ return &ImagePart{
shareableImage: s, backend: s,
node: n, node: n,
} }
} }
} }
s := &Image{ s := &backend{
restorable: restorable.NewImage(maxSize, maxSize, false), restorable: restorable.NewImage(maxSize, maxSize, false),
page: packing.NewPage(maxSize), page: packing.NewPage(maxSize),
} }
theSharedImages = append(theSharedImages, s) theBackends = append(theBackends, s)
n := s.page.Alloc(width, height) n := s.page.Alloc(width, height)
if n == nil { if n == nil {
panic("not reached") panic("not reached")
} }
return &ImagePart{ return &ImagePart{
shareableImage: s, backend: s,
node: n, node: n,
} }
} }
func NewVolatileImagePart(width, height int) *ImagePart { func NewVolatileImagePart(width, height int) *ImagePart {
r := restorable.NewImage(width, height, true) r := restorable.NewImage(width, height, true)
return &ImagePart{ return &ImagePart{
shareableImage: &Image{ backend: &backend{
restorable: r, restorable: r,
}, },
} }
@ -198,7 +198,7 @@ func NewVolatileImagePart(width, height int) *ImagePart {
func NewScreenFramebufferImagePart(width, height int) *ImagePart { func NewScreenFramebufferImagePart(width, height int) *ImagePart {
r := restorable.NewScreenFramebufferImage(width, height) r := restorable.NewScreenFramebufferImage(width, height)
return &ImagePart{ return &ImagePart{
shareableImage: &Image{ backend: &backend{
restorable: r, restorable: r,
}, },
} }