mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 11:48:55 +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() {
|
if web.IsBrowser() {
|
||||||
return c.invalidated, nil
|
return c.invalidated, nil
|
||||||
}
|
}
|
||||||
return c.offscreen.restorable.IsInvalidated()
|
return c.offscreen.sharedImagePart.IsInvalidated()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *graphicsContext) restoreIfNeeded() error {
|
func (c *graphicsContext) restoreIfNeeded() error {
|
||||||
|
118
image.go
118
image.go
@ -52,8 +52,6 @@ type Image struct {
|
|||||||
// See strings.Builder for similar examples.
|
// See strings.Builder for similar examples.
|
||||||
addr *Image
|
addr *Image
|
||||||
|
|
||||||
// restorable and sharedImagePart are exclusive.
|
|
||||||
restorable *restorable.Image
|
|
||||||
sharedImagePart *sharedImagePart
|
sharedImagePart *sharedImagePart
|
||||||
|
|
||||||
filter Filter
|
filter Filter
|
||||||
@ -73,7 +71,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) {
|
||||||
_, _, w, h := i.region()
|
_, _, w, h := i.sharedImagePart.region()
|
||||||
return w, h
|
return w, h
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,23 +98,6 @@ func (i *Image) Fill(clr color.Color) error {
|
|||||||
return nil
|
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) {
|
func (i *Image) fill(r, g, b, a uint8) {
|
||||||
wd, hd := i.Size()
|
wd, hd := i.Size()
|
||||||
ws, hs := emptyImage.Size()
|
ws, hs := emptyImage.Size()
|
||||||
@ -137,7 +118,7 @@ func (i *Image) fill(r, g, b, a uint8) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) isDisposed() bool {
|
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.
|
// 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() {
|
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.ensureNotShared()
|
i.sharedImagePart.ensureNotShared()
|
||||||
if i.restorable == nil {
|
|
||||||
panic("not reached")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
||||||
@ -253,7 +231,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
|
|||||||
geom = g
|
geom = g
|
||||||
}
|
}
|
||||||
|
|
||||||
dx, dy, _, _ := img.region()
|
dx, dy, _, _ := img.sharedImagePart.region()
|
||||||
sx0 += dx
|
sx0 += dx
|
||||||
sy0 += dy
|
sy0 += dy
|
||||||
sx1 += dx
|
sx1 += dx
|
||||||
@ -268,7 +246,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
|
|||||||
filter = graphics.Filter(img.filter)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,15 +272,6 @@ func (i *Image) At(x, y int) color.Color {
|
|||||||
if i.isDisposed() {
|
if i.isDisposed() {
|
||||||
return color.RGBA{}
|
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()
|
ox, oy, w, h := i.sharedImagePart.region()
|
||||||
if x < 0 || y < 0 || x >= w || y >= h {
|
if x < 0 || y < 0 || x >= w || y >= h {
|
||||||
return color.RGBA{}
|
return color.RGBA{}
|
||||||
@ -312,9 +281,6 @@ func (i *Image) At(x, y int) color.Color {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return clr
|
return clr
|
||||||
default:
|
|
||||||
panic("not reached")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dispose disposes the image data. After disposing, most of image functions do nothing and returns meaningless values.
|
// 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() {
|
if i.isDisposed() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
switch {
|
|
||||||
case i.restorable != nil:
|
|
||||||
i.restorable.Dispose()
|
|
||||||
i.restorable = nil
|
|
||||||
case i.sharedImagePart != nil:
|
|
||||||
i.sharedImagePart.Dispose()
|
i.sharedImagePart.Dispose()
|
||||||
i.sharedImagePart = nil
|
i.sharedImagePart = nil
|
||||||
default:
|
|
||||||
panic("not reached")
|
|
||||||
}
|
|
||||||
runtime.SetFinalizer(i, nil)
|
runtime.SetFinalizer(i, nil)
|
||||||
return 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.
|
// 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).
|
// 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() {
|
if i.isDisposed() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
x, y, w, h := i.region()
|
x, y, w, h := i.sharedImagePart.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))
|
||||||
}
|
}
|
||||||
i.restorableImage().ReplacePixels(p, x, y, w, h)
|
i.sharedImagePart.image().ReplacePixels(p, x, y, w, h)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -436,20 +373,11 @@ type DrawImageOptions struct {
|
|||||||
//
|
//
|
||||||
// Error returned by NewImage is always nil as of 1.5.0-alpha.
|
// Error returned by NewImage is always nil as of 1.5.0-alpha.
|
||||||
func NewImage(width, height int, filter Filter) (*Image, error) {
|
func NewImage(width, height int, filter Filter) (*Image, error) {
|
||||||
var i *Image
|
|
||||||
s := newSharedImagePart(width, height)
|
s := newSharedImagePart(width, height)
|
||||||
if s != nil {
|
i := &Image{
|
||||||
i = &Image{
|
|
||||||
sharedImagePart: s,
|
sharedImagePart: s,
|
||||||
filter: filter,
|
filter: filter,
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
r := restorable.NewImage(width, height, false)
|
|
||||||
i = &Image{
|
|
||||||
restorable: r,
|
|
||||||
filter: filter,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
i.fill(0, 0, 0, 0)
|
i.fill(0, 0, 0, 0)
|
||||||
runtime.SetFinalizer(i, (*Image).Dispose)
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
||||||
return i, nil
|
return i, nil
|
||||||
@ -457,20 +385,11 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
|
|||||||
|
|
||||||
// newImageWithoutInit creates an empty image without initialization.
|
// newImageWithoutInit creates an empty image without initialization.
|
||||||
func newImageWithoutInit(width, height int) *Image {
|
func newImageWithoutInit(width, height int) *Image {
|
||||||
var i *Image
|
|
||||||
s := newSharedImagePart(width, height)
|
s := newSharedImagePart(width, height)
|
||||||
if s != nil {
|
i := &Image{
|
||||||
i = &Image{
|
|
||||||
sharedImagePart: s,
|
sharedImagePart: s,
|
||||||
filter: FilterDefault,
|
filter: FilterDefault,
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
r := restorable.NewImage(width, height, false)
|
|
||||||
i = &Image{
|
|
||||||
restorable: r,
|
|
||||||
filter: FilterDefault,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
runtime.SetFinalizer(i, (*Image).Dispose)
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
@ -493,7 +412,11 @@ 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{
|
||||||
|
sharedImage: &sharedImage{
|
||||||
restorable: r,
|
restorable: r,
|
||||||
|
},
|
||||||
|
},
|
||||||
filter: filter,
|
filter: filter,
|
||||||
}
|
}
|
||||||
i.fill(0, 0, 0, 0)
|
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
|
width, height := size.X, size.Y
|
||||||
|
|
||||||
var i *Image
|
|
||||||
s := newSharedImagePart(width, height)
|
s := newSharedImagePart(width, height)
|
||||||
if s != nil {
|
i := &Image{
|
||||||
i = &Image{
|
|
||||||
sharedImagePart: s,
|
sharedImagePart: s,
|
||||||
filter: filter,
|
filter: filter,
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
r := restorable.NewImage(width, height, false)
|
|
||||||
i = &Image{
|
|
||||||
restorable: r,
|
|
||||||
filter: filter,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
runtime.SetFinalizer(i, (*Image).Dispose)
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
||||||
|
|
||||||
_ = i.ReplacePixels(graphicsutil.CopyImage(source))
|
_ = i.ReplacePixels(graphicsutil.CopyImage(source))
|
||||||
@ -537,7 +451,11 @@ 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{
|
||||||
|
sharedImage: &sharedImage{
|
||||||
restorable: r,
|
restorable: r,
|
||||||
|
},
|
||||||
|
},
|
||||||
filter: FilterDefault,
|
filter: FilterDefault,
|
||||||
}
|
}
|
||||||
runtime.SetFinalizer(i, (*Image).Dispose)
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
||||||
|
59
shared.go
59
shared.go
@ -15,6 +15,8 @@
|
|||||||
package ebiten
|
package ebiten
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||||
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||||
"github.com/hajimehoshi/ebiten/internal/packing"
|
"github.com/hajimehoshi/ebiten/internal/packing"
|
||||||
"github.com/hajimehoshi/ebiten/internal/restorable"
|
"github.com/hajimehoshi/ebiten/internal/restorable"
|
||||||
"github.com/hajimehoshi/ebiten/internal/sync"
|
"github.com/hajimehoshi/ebiten/internal/sync"
|
||||||
@ -31,22 +33,62 @@ var (
|
|||||||
|
|
||||||
type sharedImagePart struct {
|
type sharedImagePart struct {
|
||||||
sharedImage *sharedImage
|
sharedImage *sharedImage
|
||||||
|
|
||||||
|
// If node is nil, the image is not shared.
|
||||||
node *packing.Node
|
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 {
|
func (s *sharedImagePart) image() *restorable.Image {
|
||||||
return s.sharedImage.restorable
|
return s.sharedImage.restorable
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sharedImagePart) region() (x, y, width, height int) {
|
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()
|
return s.node.Region()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *sharedImagePart) isDisposed() bool {
|
||||||
|
return s.sharedImage == nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *sharedImagePart) Dispose() {
|
func (s *sharedImagePart) Dispose() {
|
||||||
s.sharedImage.page.Free(s.node)
|
if s.isDisposed() {
|
||||||
if s.sharedImage.page.IsEmpty() {
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
s.sharedImage = nil
|
||||||
|
s.node = nil
|
||||||
|
}()
|
||||||
|
|
||||||
|
if s.node == nil {
|
||||||
s.sharedImage.restorable.Dispose()
|
s.sharedImage.restorable.Dispose()
|
||||||
s.sharedImage.restorable = nil
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sharedImage.page.Free(s.node)
|
||||||
|
if !s.sharedImage.page.IsEmpty() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
index := -1
|
index := -1
|
||||||
for i, sh := range theSharedImages {
|
for i, sh := range theSharedImages {
|
||||||
if sh == s.sharedImage {
|
if sh == s.sharedImage {
|
||||||
@ -59,6 +101,9 @@ func (s *sharedImagePart) Dispose() {
|
|||||||
}
|
}
|
||||||
theSharedImages = append(theSharedImages[:index], theSharedImages[index+1:]...)
|
theSharedImages = append(theSharedImages[:index], theSharedImages[index+1:]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *sharedImagePart) IsInvalidated() (bool, error) {
|
||||||
|
return s.sharedImage.restorable.IsInvalidated()
|
||||||
}
|
}
|
||||||
|
|
||||||
var sharedImageLock sync.Mutex
|
var sharedImageLock sync.Mutex
|
||||||
@ -70,8 +115,14 @@ func newSharedImagePart(width, height int) *sharedImagePart {
|
|||||||
defer sharedImageLock.Unlock()
|
defer sharedImageLock.Unlock()
|
||||||
|
|
||||||
if width > maxSize || height > maxSize {
|
if width > maxSize || height > maxSize {
|
||||||
return nil
|
s := &sharedImage{
|
||||||
|
restorable: restorable.NewImage(width, height, false),
|
||||||
}
|
}
|
||||||
|
return &sharedImagePart{
|
||||||
|
sharedImage: 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 &sharedImagePart{
|
||||||
|
Loading…
Reference in New Issue
Block a user