mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 11:48:55 +01:00
graphics: Rename sharedImage -> shareableImage
This commit is contained in:
parent
9d0ea5c241
commit
1556db74fa
@ -134,7 +134,7 @@ func (c *graphicsContext) needsRestoring() (bool, error) {
|
|||||||
if web.IsBrowser() {
|
if web.IsBrowser() {
|
||||||
return c.invalidated, nil
|
return c.invalidated, nil
|
||||||
}
|
}
|
||||||
return c.offscreen.sharedImagePart.IsInvalidated()
|
return c.offscreen.shareableImagePart.IsInvalidated()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *graphicsContext) restoreIfNeeded() error {
|
func (c *graphicsContext) restoreIfNeeded() error {
|
||||||
|
32
image.go
32
image.go
@ -51,7 +51,7 @@ type Image struct {
|
|||||||
// See strings.Builder for similar examples.
|
// See strings.Builder for similar examples.
|
||||||
addr *Image
|
addr *Image
|
||||||
|
|
||||||
sharedImagePart *sharedImagePart
|
shareableImagePart *shareableImagePart
|
||||||
|
|
||||||
filter Filter
|
filter Filter
|
||||||
}
|
}
|
||||||
@ -70,7 +70,7 @@ func (i *Image) copyCheck() {
|
|||||||
|
|
||||||
// Size returns the size of the image.
|
// Size returns the size of the image.
|
||||||
func (i *Image) Size() (width, height int) {
|
func (i *Image) Size() (width, height int) {
|
||||||
return i.sharedImagePart.Size()
|
return i.shareableImagePart.Size()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear resets the pixels of the image into 0.
|
// 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 {
|
func (i *Image) isDisposed() bool {
|
||||||
return i.sharedImagePart == nil
|
return i.shareableImagePart == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DrawImage draws the given image on the image i.
|
// 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() {
|
if img.isDisposed() {
|
||||||
panic("ebiten: the given image to DrawImage must not be disposed")
|
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
|
// 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.
|
||||||
@ -238,7 +238,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
|
|||||||
filter = graphics.Filter(img.filter)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,7 +264,7 @@ func (i *Image) At(x, y int) color.Color {
|
|||||||
if i.isDisposed() {
|
if i.isDisposed() {
|
||||||
return color.RGBA{}
|
return color.RGBA{}
|
||||||
}
|
}
|
||||||
clr, err := i.sharedImagePart.At(x, y)
|
clr, err := i.shareableImagePart.At(x, y)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -283,8 +283,8 @@ func (i *Image) Dispose() error {
|
|||||||
if i.isDisposed() {
|
if i.isDisposed() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
i.sharedImagePart.Dispose()
|
i.shareableImagePart.Dispose()
|
||||||
i.sharedImagePart = nil
|
i.shareableImagePart = nil
|
||||||
runtime.SetFinalizer(i, nil)
|
runtime.SetFinalizer(i, nil)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -305,7 +305,7 @@ func (i *Image) ReplacePixels(p []byte) error {
|
|||||||
if i.isDisposed() {
|
if i.isDisposed() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
i.sharedImagePart.ReplacePixels(p)
|
i.shareableImagePart.ReplacePixels(p)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -359,7 +359,7 @@ type DrawImageOptions struct {
|
|||||||
func NewImage(width, height int, filter Filter) (*Image, error) {
|
func NewImage(width, height int, filter Filter) (*Image, error) {
|
||||||
s := newSharedImagePart(width, height)
|
s := newSharedImagePart(width, height)
|
||||||
i := &Image{
|
i := &Image{
|
||||||
sharedImagePart: s,
|
shareableImagePart: s,
|
||||||
filter: filter,
|
filter: filter,
|
||||||
}
|
}
|
||||||
i.fill(0, 0, 0, 0)
|
i.fill(0, 0, 0, 0)
|
||||||
@ -371,7 +371,7 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
|
|||||||
func newImageWithoutInit(width, height int) *Image {
|
func newImageWithoutInit(width, height int) *Image {
|
||||||
s := newSharedImagePart(width, height)
|
s := newSharedImagePart(width, height)
|
||||||
i := &Image{
|
i := &Image{
|
||||||
sharedImagePart: s,
|
shareableImagePart: s,
|
||||||
filter: FilterDefault,
|
filter: FilterDefault,
|
||||||
}
|
}
|
||||||
runtime.SetFinalizer(i, (*Image).Dispose)
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
||||||
@ -396,8 +396,8 @@ func newImageWithoutInit(width, height int) *Image {
|
|||||||
func newVolatileImage(width, height int, filter Filter) *Image {
|
func newVolatileImage(width, height int, filter Filter) *Image {
|
||||||
r := restorable.NewImage(width, height, true)
|
r := restorable.NewImage(width, height, true)
|
||||||
i := &Image{
|
i := &Image{
|
||||||
sharedImagePart: &sharedImagePart{
|
shareableImagePart: &shareableImagePart{
|
||||||
sharedImage: &sharedImage{
|
shareableImage: &shareableImage{
|
||||||
restorable: r,
|
restorable: r,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -423,7 +423,7 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
|
|||||||
|
|
||||||
s := newSharedImagePart(width, height)
|
s := newSharedImagePart(width, height)
|
||||||
i := &Image{
|
i := &Image{
|
||||||
sharedImagePart: s,
|
shareableImagePart: s,
|
||||||
filter: filter,
|
filter: filter,
|
||||||
}
|
}
|
||||||
runtime.SetFinalizer(i, (*Image).Dispose)
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
||||||
@ -435,8 +435,8 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
|
|||||||
func newImageWithScreenFramebuffer(width, height int) *Image {
|
func newImageWithScreenFramebuffer(width, height int) *Image {
|
||||||
r := restorable.NewScreenFramebufferImage(width, height)
|
r := restorable.NewScreenFramebufferImage(width, height)
|
||||||
i := &Image{
|
i := &Image{
|
||||||
sharedImagePart: &sharedImagePart{
|
shareableImagePart: &shareableImagePart{
|
||||||
sharedImage: &sharedImage{
|
shareableImage: &shareableImage{
|
||||||
restorable: r,
|
restorable: r,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
77
shared.go
77
shared.go
@ -26,102 +26,103 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/sync"
|
"github.com/hajimehoshi/ebiten/internal/sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type sharedImage struct {
|
type shareableImage struct {
|
||||||
restorable *restorable.Image
|
restorable *restorable.Image
|
||||||
page *packing.Page
|
page *packing.Page
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
theSharedImages = []*sharedImage{}
|
// theSharedImages is a set of actually shared images.
|
||||||
|
theSharedImages = []*shareableImage{}
|
||||||
)
|
)
|
||||||
|
|
||||||
type sharedImagePart struct {
|
type shareableImagePart struct {
|
||||||
sharedImage *sharedImage
|
shareableImage *shareableImage
|
||||||
|
|
||||||
// If node is nil, the image is not shared.
|
// If node is nil, the image is not shared.
|
||||||
node *packing.Node
|
node *packing.Node
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sharedImagePart) ensureNotShared() {
|
func (s *shareableImagePart) ensureNotShared() {
|
||||||
if s.node == nil {
|
if s.node == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
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.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.Dispose()
|
||||||
s.sharedImage = &sharedImage{
|
s.shareableImage = &shareableImage{
|
||||||
restorable: newImg,
|
restorable: newImg,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sharedImagePart) region() (x, y, width, height int) {
|
func (s *shareableImagePart) region() (x, y, width, height int) {
|
||||||
if s.node == nil {
|
if s.node == nil {
|
||||||
w, h := s.sharedImage.restorable.Size()
|
w, h := s.shareableImage.restorable.Size()
|
||||||
return 0, 0, w, h
|
return 0, 0, w, h
|
||||||
}
|
}
|
||||||
return s.node.Region()
|
return s.node.Region()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sharedImagePart) Size() (width, height int) {
|
func (s *shareableImagePart) Size() (width, height int) {
|
||||||
_, _, w, h := s.region()
|
_, _, w, h := s.region()
|
||||||
return w, h
|
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()
|
dx, dy, _, _ := img.region()
|
||||||
sx0 += dx
|
sx0 += dx
|
||||||
sy0 += dy
|
sy0 += dy
|
||||||
sx1 += dx
|
sx1 += dx
|
||||||
sy1 += dy
|
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()
|
x, y, w, h := s.region()
|
||||||
if l := 4 * w * h; len(p) != l {
|
if l := 4 * w * h; len(p) != l {
|
||||||
panic(fmt.Sprintf("ebiten: len(p) was %d but must be %d", 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()
|
ox, oy, w, h := s.region()
|
||||||
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.sharedImage.restorable.At(x+ox, y+oy)
|
return s.shareableImage.restorable.At(x+ox, y+oy)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sharedImagePart) isDisposed() bool {
|
func (s *shareableImagePart) isDisposed() bool {
|
||||||
return s.sharedImage == nil
|
return s.shareableImage == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sharedImagePart) Dispose() {
|
func (s *shareableImagePart) Dispose() {
|
||||||
if s.isDisposed() {
|
if s.isDisposed() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
s.sharedImage = nil
|
s.shareableImage = nil
|
||||||
s.node = nil
|
s.node = nil
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if s.node == nil {
|
if s.node == nil {
|
||||||
s.sharedImage.restorable.Dispose()
|
s.shareableImage.restorable.Dispose()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.sharedImage.page.Free(s.node)
|
s.shareableImage.page.Free(s.node)
|
||||||
if !s.sharedImage.page.IsEmpty() {
|
if !s.shareableImage.page.IsEmpty() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
index := -1
|
index := -1
|
||||||
for i, sh := range theSharedImages {
|
for i, sh := range theSharedImages {
|
||||||
if sh == s.sharedImage {
|
if sh == s.shareableImage {
|
||||||
index = i
|
index = i
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -132,36 +133,36 @@ func (s *sharedImagePart) Dispose() {
|
|||||||
theSharedImages = append(theSharedImages[:index], theSharedImages[index+1:]...)
|
theSharedImages = append(theSharedImages[:index], theSharedImages[index+1:]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sharedImagePart) IsInvalidated() (bool, error) {
|
func (s *shareableImagePart) IsInvalidated() (bool, error) {
|
||||||
return s.sharedImage.restorable.IsInvalidated()
|
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
|
const maxSize = 2048
|
||||||
|
|
||||||
sharedImageLock.Lock()
|
shareableImageLock.Lock()
|
||||||
defer sharedImageLock.Unlock()
|
defer shareableImageLock.Unlock()
|
||||||
|
|
||||||
if width > maxSize || height > maxSize {
|
if width > maxSize || height > maxSize {
|
||||||
s := &sharedImage{
|
s := &shareableImage{
|
||||||
restorable: restorable.NewImage(width, height, false),
|
restorable: restorable.NewImage(width, height, false),
|
||||||
}
|
}
|
||||||
return &sharedImagePart{
|
return &shareableImagePart{
|
||||||
sharedImage: s,
|
shareableImage: s,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, s := range theSharedImages {
|
for _, s := range theSharedImages {
|
||||||
if n := s.page.Alloc(width, height); n != nil {
|
if n := s.page.Alloc(width, height); n != nil {
|
||||||
return &sharedImagePart{
|
return &shareableImagePart{
|
||||||
sharedImage: s,
|
shareableImage: s,
|
||||||
node: n,
|
node: n,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s := &sharedImage{
|
s := &shareableImage{
|
||||||
restorable: restorable.NewImage(maxSize, maxSize, false),
|
restorable: restorable.NewImage(maxSize, maxSize, false),
|
||||||
page: packing.NewPage(maxSize),
|
page: packing.NewPage(maxSize),
|
||||||
}
|
}
|
||||||
@ -171,8 +172,8 @@ func newSharedImagePart(width, height int) *sharedImagePart {
|
|||||||
if n == nil {
|
if n == nil {
|
||||||
panic("not reached")
|
panic("not reached")
|
||||||
}
|
}
|
||||||
return &sharedImagePart{
|
return &shareableImagePart{
|
||||||
sharedImage: s,
|
shareableImage: s,
|
||||||
node: n,
|
node: n,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user