mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
graphics: Refactoring: Remove (*Image).restorable
This commit is contained in:
parent
2def3e5e6e
commit
afda41a5ed
@ -134,7 +134,7 @@ func (c *graphicsContext) needsRestoring() (bool, error) {
|
||||
if web.IsBrowser() {
|
||||
return c.invalidated, nil
|
||||
}
|
||||
return c.offscreen.restorable.IsInvalidated()
|
||||
return c.offscreen.sharedImagePart.IsInvalidated()
|
||||
}
|
||||
|
||||
func (c *graphicsContext) restoreIfNeeded() error {
|
||||
|
118
image.go
118
image.go
@ -52,8 +52,6 @@ type Image struct {
|
||||
// See strings.Builder for similar examples.
|
||||
addr *Image
|
||||
|
||||
// restorable and sharedImagePart are exclusive.
|
||||
restorable *restorable.Image
|
||||
sharedImagePart *sharedImagePart
|
||||
|
||||
filter Filter
|
||||
@ -73,7 +71,7 @@ func (i *Image) copyCheck() {
|
||||
|
||||
// Size returns the size of the image.
|
||||
func (i *Image) Size() (width, height int) {
|
||||
_, _, w, h := i.region()
|
||||
_, _, w, h := i.sharedImagePart.region()
|
||||
return w, h
|
||||
}
|
||||
|
||||
@ -100,23 +98,6 @@ func (i *Image) Fill(clr color.Color) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Image) ensureNotShared() {
|
||||
if i.sharedImagePart == nil {
|
||||
return
|
||||
}
|
||||
if i.restorable != nil {
|
||||
panic("not reached")
|
||||
}
|
||||
|
||||
s := i.sharedImagePart
|
||||
x, y, w, h := s.region()
|
||||
|
||||
i.restorable = restorable.NewImage(w, h, false)
|
||||
i.sharedImagePart = nil
|
||||
i.restorable.DrawImage(s.image(), x, y, w, h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
|
||||
s.Dispose()
|
||||
}
|
||||
|
||||
func (i *Image) fill(r, g, b, a uint8) {
|
||||
wd, hd := i.Size()
|
||||
ws, hs := emptyImage.Size()
|
||||
@ -137,7 +118,7 @@ func (i *Image) fill(r, g, b, a uint8) {
|
||||
}
|
||||
|
||||
func (i *Image) isDisposed() bool {
|
||||
return i.restorable == nil && i.sharedImagePart == nil
|
||||
return i.sharedImagePart == nil
|
||||
}
|
||||
|
||||
// DrawImage draws the given image on the image i.
|
||||
@ -175,10 +156,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.ensureNotShared()
|
||||
if i.restorable == nil {
|
||||
panic("not reached")
|
||||
}
|
||||
i.sharedImagePart.ensureNotShared()
|
||||
|
||||
// Compare i and img after ensuring i is not shared, or
|
||||
// i and img might share the same texture even though i != img.
|
||||
@ -253,7 +231,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
|
||||
geom = g
|
||||
}
|
||||
|
||||
dx, dy, _, _ := img.region()
|
||||
dx, dy, _, _ := img.sharedImagePart.region()
|
||||
sx0 += dx
|
||||
sy0 += dy
|
||||
sx1 += dx
|
||||
@ -268,7 +246,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
|
||||
filter = graphics.Filter(img.filter)
|
||||
}
|
||||
|
||||
i.restorable.DrawImage(img.restorableImage(), sx0, sy0, sx1, sy1, geom, options.ColorM.impl, mode, filter)
|
||||
i.sharedImagePart.image().DrawImage(img.sharedImagePart.image(), sx0, sy0, sx1, sy1, geom, options.ColorM.impl, mode, filter)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -294,15 +272,6 @@ func (i *Image) At(x, y int) color.Color {
|
||||
if i.isDisposed() {
|
||||
return color.RGBA{}
|
||||
}
|
||||
switch {
|
||||
case i.restorable != nil:
|
||||
// TODO: Error should be delayed until flushing. Do not panic here.
|
||||
clr, err := i.restorable.At(x, y)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return clr
|
||||
case i.sharedImagePart != nil:
|
||||
ox, oy, w, h := i.sharedImagePart.region()
|
||||
if x < 0 || y < 0 || x >= w || y >= h {
|
||||
return color.RGBA{}
|
||||
@ -312,9 +281,6 @@ func (i *Image) At(x, y int) color.Color {
|
||||
panic(err)
|
||||
}
|
||||
return clr
|
||||
default:
|
||||
panic("not reached")
|
||||
}
|
||||
}
|
||||
|
||||
// Dispose disposes the image data. After disposing, most of image functions do nothing and returns meaningless values.
|
||||
@ -329,41 +295,12 @@ func (i *Image) Dispose() error {
|
||||
if i.isDisposed() {
|
||||
return nil
|
||||
}
|
||||
switch {
|
||||
case i.restorable != nil:
|
||||
i.restorable.Dispose()
|
||||
i.restorable = nil
|
||||
case i.sharedImagePart != nil:
|
||||
i.sharedImagePart.Dispose()
|
||||
i.sharedImagePart = nil
|
||||
default:
|
||||
panic("not reached")
|
||||
}
|
||||
runtime.SetFinalizer(i, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Image) region() (x, y, width, height int) {
|
||||
if i.restorable != nil {
|
||||
w, h := i.restorable.Size()
|
||||
return 0, 0, w, h
|
||||
}
|
||||
if i.sharedImagePart != nil {
|
||||
return i.sharedImagePart.region()
|
||||
}
|
||||
panic("not reached")
|
||||
}
|
||||
|
||||
func (i *Image) restorableImage() *restorable.Image {
|
||||
if i.restorable != nil {
|
||||
return i.restorable
|
||||
}
|
||||
if i.sharedImagePart != nil {
|
||||
return i.sharedImagePart.image()
|
||||
}
|
||||
panic("not reached")
|
||||
}
|
||||
|
||||
// ReplacePixels replaces the pixels of the image with p.
|
||||
//
|
||||
// The given p must represent RGBA pre-multiplied alpha values. len(p) must equal to 4 * (image width) * (image height).
|
||||
@ -380,11 +317,11 @@ func (i *Image) ReplacePixels(p []byte) error {
|
||||
if i.isDisposed() {
|
||||
return nil
|
||||
}
|
||||
x, y, w, h := i.region()
|
||||
x, y, w, h := i.sharedImagePart.region()
|
||||
if l := 4 * w * h; len(p) != l {
|
||||
panic(fmt.Sprintf("ebiten: len(p) was %d but must be %d", len(p), l))
|
||||
}
|
||||
i.restorableImage().ReplacePixels(p, x, y, w, h)
|
||||
i.sharedImagePart.image().ReplacePixels(p, x, y, w, h)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -436,20 +373,11 @@ type DrawImageOptions struct {
|
||||
//
|
||||
// Error returned by NewImage is always nil as of 1.5.0-alpha.
|
||||
func NewImage(width, height int, filter Filter) (*Image, error) {
|
||||
var i *Image
|
||||
s := newSharedImagePart(width, height)
|
||||
if s != nil {
|
||||
i = &Image{
|
||||
i := &Image{
|
||||
sharedImagePart: s,
|
||||
filter: filter,
|
||||
}
|
||||
} else {
|
||||
r := restorable.NewImage(width, height, false)
|
||||
i = &Image{
|
||||
restorable: r,
|
||||
filter: filter,
|
||||
}
|
||||
}
|
||||
i.fill(0, 0, 0, 0)
|
||||
runtime.SetFinalizer(i, (*Image).Dispose)
|
||||
return i, nil
|
||||
@ -457,20 +385,11 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
|
||||
|
||||
// newImageWithoutInit creates an empty image without initialization.
|
||||
func newImageWithoutInit(width, height int) *Image {
|
||||
var i *Image
|
||||
s := newSharedImagePart(width, height)
|
||||
if s != nil {
|
||||
i = &Image{
|
||||
i := &Image{
|
||||
sharedImagePart: s,
|
||||
filter: FilterDefault,
|
||||
}
|
||||
} else {
|
||||
r := restorable.NewImage(width, height, false)
|
||||
i = &Image{
|
||||
restorable: r,
|
||||
filter: FilterDefault,
|
||||
}
|
||||
}
|
||||
runtime.SetFinalizer(i, (*Image).Dispose)
|
||||
return i
|
||||
}
|
||||
@ -493,7 +412,11 @@ 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{
|
||||
restorable: r,
|
||||
},
|
||||
},
|
||||
filter: filter,
|
||||
}
|
||||
i.fill(0, 0, 0, 0)
|
||||
@ -514,20 +437,11 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
|
||||
|
||||
width, height := size.X, size.Y
|
||||
|
||||
var i *Image
|
||||
s := newSharedImagePart(width, height)
|
||||
if s != nil {
|
||||
i = &Image{
|
||||
i := &Image{
|
||||
sharedImagePart: s,
|
||||
filter: filter,
|
||||
}
|
||||
} else {
|
||||
r := restorable.NewImage(width, height, false)
|
||||
i = &Image{
|
||||
restorable: r,
|
||||
filter: filter,
|
||||
}
|
||||
}
|
||||
runtime.SetFinalizer(i, (*Image).Dispose)
|
||||
|
||||
_ = i.ReplacePixels(graphicsutil.CopyImage(source))
|
||||
@ -537,7 +451,11 @@ 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{
|
||||
restorable: r,
|
||||
},
|
||||
},
|
||||
filter: FilterDefault,
|
||||
}
|
||||
runtime.SetFinalizer(i, (*Image).Dispose)
|
||||
|
59
shared.go
59
shared.go
@ -15,6 +15,8 @@
|
||||
package ebiten
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||
"github.com/hajimehoshi/ebiten/internal/packing"
|
||||
"github.com/hajimehoshi/ebiten/internal/restorable"
|
||||
"github.com/hajimehoshi/ebiten/internal/sync"
|
||||
@ -31,22 +33,62 @@ var (
|
||||
|
||||
type sharedImagePart struct {
|
||||
sharedImage *sharedImage
|
||||
|
||||
// If node is nil, the image is not shared.
|
||||
node *packing.Node
|
||||
}
|
||||
|
||||
func (s *sharedImagePart) 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)
|
||||
|
||||
s.Dispose()
|
||||
s.sharedImage = &sharedImage{
|
||||
restorable: newImg,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *sharedImagePart) image() *restorable.Image {
|
||||
return s.sharedImage.restorable
|
||||
}
|
||||
|
||||
func (s *sharedImagePart) region() (x, y, width, height int) {
|
||||
if s.node == nil {
|
||||
w, h := s.sharedImage.restorable.Size()
|
||||
return 0, 0, w, h
|
||||
}
|
||||
return s.node.Region()
|
||||
}
|
||||
|
||||
func (s *sharedImagePart) isDisposed() bool {
|
||||
return s.sharedImage == nil
|
||||
}
|
||||
|
||||
func (s *sharedImagePart) Dispose() {
|
||||
s.sharedImage.page.Free(s.node)
|
||||
if s.sharedImage.page.IsEmpty() {
|
||||
if s.isDisposed() {
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
s.sharedImage = nil
|
||||
s.node = nil
|
||||
}()
|
||||
|
||||
if s.node == nil {
|
||||
s.sharedImage.restorable.Dispose()
|
||||
s.sharedImage.restorable = nil
|
||||
return
|
||||
}
|
||||
|
||||
s.sharedImage.page.Free(s.node)
|
||||
if !s.sharedImage.page.IsEmpty() {
|
||||
return
|
||||
}
|
||||
|
||||
index := -1
|
||||
for i, sh := range theSharedImages {
|
||||
if sh == s.sharedImage {
|
||||
@ -59,6 +101,9 @@ func (s *sharedImagePart) Dispose() {
|
||||
}
|
||||
theSharedImages = append(theSharedImages[:index], theSharedImages[index+1:]...)
|
||||
}
|
||||
|
||||
func (s *sharedImagePart) IsInvalidated() (bool, error) {
|
||||
return s.sharedImage.restorable.IsInvalidated()
|
||||
}
|
||||
|
||||
var sharedImageLock sync.Mutex
|
||||
@ -70,8 +115,14 @@ func newSharedImagePart(width, height int) *sharedImagePart {
|
||||
defer sharedImageLock.Unlock()
|
||||
|
||||
if width > maxSize || height > maxSize {
|
||||
return nil
|
||||
s := &sharedImage{
|
||||
restorable: restorable.NewImage(width, height, false),
|
||||
}
|
||||
return &sharedImagePart{
|
||||
sharedImage: s,
|
||||
}
|
||||
}
|
||||
|
||||
for _, s := range theSharedImages {
|
||||
if n := s.page.Alloc(width, height); n != nil {
|
||||
return &sharedImagePart{
|
||||
|
Loading…
Reference in New Issue
Block a user