graphics: Rename sharedImage -> shareableImage

This commit is contained in:
Hajime Hoshi 2018-03-10 23:38:27 +09:00
parent 9d0ea5c241
commit 1556db74fa
3 changed files with 61 additions and 60 deletions

View File

@ -134,7 +134,7 @@ func (c *graphicsContext) needsRestoring() (bool, error) {
if web.IsBrowser() {
return c.invalidated, nil
}
return c.offscreen.sharedImagePart.IsInvalidated()
return c.offscreen.shareableImagePart.IsInvalidated()
}
func (c *graphicsContext) restoreIfNeeded() error {

View File

@ -51,7 +51,7 @@ type Image struct {
// See strings.Builder for similar examples.
addr *Image
sharedImagePart *sharedImagePart
shareableImagePart *shareableImagePart
filter Filter
}
@ -70,7 +70,7 @@ func (i *Image) copyCheck() {
// Size returns the size of the image.
func (i *Image) Size() (width, height int) {
return i.sharedImagePart.Size()
return i.shareableImagePart.Size()
}
// Clear resets the pixels of the image into 0.
@ -116,7 +116,7 @@ func (i *Image) fill(r, g, b, a uint8) {
}
func (i *Image) isDisposed() bool {
return i.sharedImagePart == nil
return i.shareableImagePart == nil
}
// DrawImage draws the given image on the image i.
@ -154,7 +154,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
if img.isDisposed() {
panic("ebiten: the given image to DrawImage must not be disposed")
}
i.sharedImagePart.ensureNotShared()
i.shareableImagePart.ensureNotShared()
// Compare i and img after ensuring i is not shared, or
// i and img might share the same texture even though i != img.
@ -238,7 +238,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
filter = graphics.Filter(img.filter)
}
i.sharedImagePart.DrawImage(img.sharedImagePart, sx0, sy0, sx1, sy1, geom, options.ColorM.impl, mode, filter)
i.shareableImagePart.DrawImage(img.shareableImagePart, sx0, sy0, sx1, sy1, geom, options.ColorM.impl, mode, filter)
return nil
}
@ -264,7 +264,7 @@ func (i *Image) At(x, y int) color.Color {
if i.isDisposed() {
return color.RGBA{}
}
clr, err := i.sharedImagePart.At(x, y)
clr, err := i.shareableImagePart.At(x, y)
if err != nil {
panic(err)
}
@ -283,8 +283,8 @@ func (i *Image) Dispose() error {
if i.isDisposed() {
return nil
}
i.sharedImagePart.Dispose()
i.sharedImagePart = nil
i.shareableImagePart.Dispose()
i.shareableImagePart = nil
runtime.SetFinalizer(i, nil)
return nil
}
@ -305,7 +305,7 @@ func (i *Image) ReplacePixels(p []byte) error {
if i.isDisposed() {
return nil
}
i.sharedImagePart.ReplacePixels(p)
i.shareableImagePart.ReplacePixels(p)
return nil
}
@ -359,8 +359,8 @@ type DrawImageOptions struct {
func NewImage(width, height int, filter Filter) (*Image, error) {
s := newSharedImagePart(width, height)
i := &Image{
sharedImagePart: s,
filter: filter,
shareableImagePart: s,
filter: filter,
}
i.fill(0, 0, 0, 0)
runtime.SetFinalizer(i, (*Image).Dispose)
@ -371,8 +371,8 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
func newImageWithoutInit(width, height int) *Image {
s := newSharedImagePart(width, height)
i := &Image{
sharedImagePart: s,
filter: FilterDefault,
shareableImagePart: s,
filter: FilterDefault,
}
runtime.SetFinalizer(i, (*Image).Dispose)
return i
@ -396,8 +396,8 @@ func newImageWithoutInit(width, height int) *Image {
func newVolatileImage(width, height int, filter Filter) *Image {
r := restorable.NewImage(width, height, true)
i := &Image{
sharedImagePart: &sharedImagePart{
sharedImage: &sharedImage{
shareableImagePart: &shareableImagePart{
shareableImage: &shareableImage{
restorable: r,
},
},
@ -423,8 +423,8 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
s := newSharedImagePart(width, height)
i := &Image{
sharedImagePart: s,
filter: filter,
shareableImagePart: s,
filter: filter,
}
runtime.SetFinalizer(i, (*Image).Dispose)
@ -435,8 +435,8 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
func newImageWithScreenFramebuffer(width, height int) *Image {
r := restorable.NewScreenFramebufferImage(width, height)
i := &Image{
sharedImagePart: &sharedImagePart{
sharedImage: &sharedImage{
shareableImagePart: &shareableImagePart{
shareableImage: &shareableImage{
restorable: r,
},
},

View File

@ -26,102 +26,103 @@ import (
"github.com/hajimehoshi/ebiten/internal/sync"
)
type sharedImage struct {
type shareableImage struct {
restorable *restorable.Image
page *packing.Page
}
var (
theSharedImages = []*sharedImage{}
// theSharedImages is a set of actually shared images.
theSharedImages = []*shareableImage{}
)
type sharedImagePart struct {
sharedImage *sharedImage
type shareableImagePart struct {
shareableImage *shareableImage
// If node is nil, the image is not shared.
node *packing.Node
}
func (s *sharedImagePart) ensureNotShared() {
func (s *shareableImagePart) ensureNotShared() {
if s.node == nil {
return
}
x, y, w, h := s.region()
newImg := restorable.NewImage(w, h, false)
newImg.DrawImage(s.sharedImage.restorable, x, y, w, h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
newImg.DrawImage(s.shareableImage.restorable, x, y, w, h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
s.Dispose()
s.sharedImage = &sharedImage{
s.shareableImage = &shareableImage{
restorable: newImg,
}
}
func (s *sharedImagePart) region() (x, y, width, height int) {
func (s *shareableImagePart) region() (x, y, width, height int) {
if s.node == nil {
w, h := s.sharedImage.restorable.Size()
w, h := s.shareableImage.restorable.Size()
return 0, 0, w, h
}
return s.node.Region()
}
func (s *sharedImagePart) Size() (width, height int) {
func (s *shareableImagePart) Size() (width, height int) {
_, _, w, h := s.region()
return w, h
}
func (s *sharedImagePart) DrawImage(img *sharedImagePart, sx0, sy0, sx1, sy1 int, geom *affine.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
func (s *shareableImagePart) DrawImage(img *shareableImagePart, sx0, sy0, sx1, sy1 int, geom *affine.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
dx, dy, _, _ := img.region()
sx0 += dx
sy0 += dy
sx1 += dx
sy1 += dy
s.sharedImage.restorable.DrawImage(img.sharedImage.restorable, sx0, sy0, sx1, sy1, geom, colorm, mode, filter)
s.shareableImage.restorable.DrawImage(img.shareableImage.restorable, sx0, sy0, sx1, sy1, geom, colorm, mode, filter)
}
func (s *sharedImagePart) ReplacePixels(p []byte) {
func (s *shareableImagePart) ReplacePixels(p []byte) {
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))
}
s.sharedImage.restorable.ReplacePixels(p, x, y, w, h)
s.shareableImage.restorable.ReplacePixels(p, x, y, w, h)
}
func (s *sharedImagePart) At(x, y int) (color.Color, error) {
func (s *shareableImagePart) At(x, y int) (color.Color, error) {
ox, oy, w, h := s.region()
if x < 0 || y < 0 || x >= w || y >= h {
return color.RGBA{}, nil
}
return s.sharedImage.restorable.At(x+ox, y+oy)
return s.shareableImage.restorable.At(x+ox, y+oy)
}
func (s *sharedImagePart) isDisposed() bool {
return s.sharedImage == nil
func (s *shareableImagePart) isDisposed() bool {
return s.shareableImage == nil
}
func (s *sharedImagePart) Dispose() {
func (s *shareableImagePart) Dispose() {
if s.isDisposed() {
return
}
defer func() {
s.sharedImage = nil
s.shareableImage = nil
s.node = nil
}()
if s.node == nil {
s.sharedImage.restorable.Dispose()
s.shareableImage.restorable.Dispose()
return
}
s.sharedImage.page.Free(s.node)
if !s.sharedImage.page.IsEmpty() {
s.shareableImage.page.Free(s.node)
if !s.shareableImage.page.IsEmpty() {
return
}
index := -1
for i, sh := range theSharedImages {
if sh == s.sharedImage {
if sh == s.shareableImage {
index = i
break
}
@ -132,36 +133,36 @@ func (s *sharedImagePart) Dispose() {
theSharedImages = append(theSharedImages[:index], theSharedImages[index+1:]...)
}
func (s *sharedImagePart) IsInvalidated() (bool, error) {
return s.sharedImage.restorable.IsInvalidated()
func (s *shareableImagePart) IsInvalidated() (bool, error) {
return s.shareableImage.restorable.IsInvalidated()
}
var sharedImageLock sync.Mutex
var shareableImageLock sync.Mutex
func newSharedImagePart(width, height int) *sharedImagePart {
func newSharedImagePart(width, height int) *shareableImagePart {
const maxSize = 2048
sharedImageLock.Lock()
defer sharedImageLock.Unlock()
shareableImageLock.Lock()
defer shareableImageLock.Unlock()
if width > maxSize || height > maxSize {
s := &sharedImage{
s := &shareableImage{
restorable: restorable.NewImage(width, height, false),
}
return &sharedImagePart{
sharedImage: s,
return &shareableImagePart{
shareableImage: s,
}
}
for _, s := range theSharedImages {
if n := s.page.Alloc(width, height); n != nil {
return &sharedImagePart{
sharedImage: s,
node: n,
return &shareableImagePart{
shareableImage: s,
node: n,
}
}
}
s := &sharedImage{
s := &shareableImage{
restorable: restorable.NewImage(maxSize, maxSize, false),
page: packing.NewPage(maxSize),
}
@ -171,8 +172,8 @@ func newSharedImagePart(width, height int) *sharedImagePart {
if n == nil {
panic("not reached")
}
return &sharedImagePart{
sharedImage: s,
node: n,
return &shareableImagePart{
shareableImage: s,
node: n,
}
}