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"
)
type Image struct {
type backend struct {
restorable *restorable.Image
page *packing.Page
}
var (
// theSharedImages is a set of actually shared images.
theSharedImages = []*Image{}
// theBackends is a set of actually shared images.
theBackends = []*backend{}
)
type ImagePart struct {
shareableImage *Image
backend *backend
// If node is nil, the image is not shared.
node *packing.Node
@ -50,17 +50,17 @@ func (s *ImagePart) ensureNotShared() {
x, y, w, h := s.region()
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.shareableImage = &Image{
s.backend = &backend{
restorable: newImg,
}
}
func (s *ImagePart) region() (x, y, width, height int) {
if s.node == nil {
w, h := s.shareableImage.restorable.Size()
w, h := s.backend.restorable.Size()
return 0, 0, w, h
}
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
// 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")
}
@ -85,7 +85,7 @@ func (s *ImagePart) DrawImage(img *ImagePart, sx0, sy0, sx1, sy1 int, geom *affi
sy0 += dy
sx1 += dx
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) {
@ -93,7 +93,7 @@ func (s *ImagePart) ReplacePixels(p []byte) {
if l := 4 * w * h; 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) {
@ -101,11 +101,11 @@ func (s *ImagePart) At(x, y int) (color.Color, error) {
if x < 0 || y < 0 || x >= w || y >= h {
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 {
return s.shareableImage == nil
return s.backend == nil
}
func (s *ImagePart) Dispose() {
@ -114,23 +114,23 @@ func (s *ImagePart) Dispose() {
}
defer func() {
s.shareableImage = nil
s.backend = nil
s.node = nil
}()
if s.node == nil {
s.shareableImage.restorable.Dispose()
s.backend.restorable.Dispose()
return
}
s.shareableImage.page.Free(s.node)
if !s.shareableImage.page.IsEmpty() {
s.backend.page.Free(s.node)
if !s.backend.page.IsEmpty() {
return
}
index := -1
for i, sh := range theSharedImages {
if sh == s.shareableImage {
for i, sh := range theBackends {
if sh == s.backend {
index = i
break
}
@ -138,11 +138,11 @@ func (s *ImagePart) Dispose() {
if index == -1 {
panic("not reached")
}
theSharedImages = append(theSharedImages[:index], theSharedImages[index+1:]...)
theBackends = append(theBackends[:index], theBackends[index+1:]...)
}
func (s *ImagePart) IsInvalidated() (bool, error) {
return s.shareableImage.restorable.IsInvalidated()
return s.backend.restorable.IsInvalidated()
}
var shareableImageLock sync.Mutex
@ -154,34 +154,34 @@ func NewImagePart(width, height int) *ImagePart {
defer shareableImageLock.Unlock()
if width > maxSize || height > maxSize {
s := &Image{
s := &backend{
restorable: restorable.NewImage(width, height, false),
}
return &ImagePart{
shareableImage: s,
backend: s,
}
}
for _, s := range theSharedImages {
for _, s := range theBackends {
if n := s.page.Alloc(width, height); n != nil {
return &ImagePart{
shareableImage: s,
backend: s,
node: n,
}
}
}
s := &Image{
s := &backend{
restorable: restorable.NewImage(maxSize, maxSize, false),
page: packing.NewPage(maxSize),
}
theSharedImages = append(theSharedImages, s)
theBackends = append(theBackends, s)
n := s.page.Alloc(width, height)
if n == nil {
panic("not reached")
}
return &ImagePart{
shareableImage: s,
backend: s,
node: n,
}
}
@ -189,7 +189,7 @@ func NewImagePart(width, height int) *ImagePart {
func NewVolatileImagePart(width, height int) *ImagePart {
r := restorable.NewImage(width, height, true)
return &ImagePart{
shareableImage: &Image{
backend: &backend{
restorable: r,
},
}
@ -198,7 +198,7 @@ func NewVolatileImagePart(width, height int) *ImagePart {
func NewScreenFramebufferImagePart(width, height int) *ImagePart {
r := restorable.NewScreenFramebufferImage(width, height)
return &ImagePart{
shareableImage: &Image{
backend: &backend{
restorable: r,
},
}