graphics: Now images from NewImage also shares a texture

This commit is contained in:
Hajime Hoshi 2018-03-03 21:00:46 +09:00
parent 2903f32d9b
commit d944d51c14

View File

@ -106,21 +106,20 @@ func (i *Image) Fill(clr color.Color) error {
return nil return nil
} }
func (img *Image) ensureNotShared() { func (i *Image) ensureNotShared() {
if img.sharedImagePart == nil { if i.sharedImagePart == nil {
return return
} }
if img.restorable == nil { if i.restorable != nil {
// The image is already disposed. panic("not reached")
return
} }
s := img.sharedImagePart s := i.sharedImagePart
x, y, w, h := s.region() x, y, w, h := s.region()
img.restorable = restorable.NewImage(w, h, false) i.restorable = restorable.NewImage(w, h, false)
img.sharedImagePart = nil i.sharedImagePart = nil
img.restorable.DrawImage(s.image(), x, y, w, h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest) i.restorable.DrawImage(s.image(), x, y, w, h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
s.Dispose() s.Dispose()
} }
@ -180,6 +179,10 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
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.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.
if i == img { if i == img {
@ -439,12 +442,20 @@ 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) {
checkSize(width, height) checkSize(width, height)
// TODO: Is it possible to use the shared texture here? (#514) var i *Image
s := newSharedImagePart(width, height)
if s != nil {
i = &Image{
sharedImagePart: s,
filter: filter,
}
} else {
r := restorable.NewImage(width, height, false) r := restorable.NewImage(width, height, false)
i := &Image{ i = &Image{
restorable: r, restorable: r,
filter: filter, 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
@ -453,12 +464,20 @@ 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 {
checkSize(width, height) checkSize(width, height)
// TODO: Is it possible to use the shared texture here? (#514) var i *Image
s := newSharedImagePart(width, height)
if s != nil {
i = &Image{
sharedImagePart: s,
filter: FilterDefault,
}
} else {
r := restorable.NewImage(width, height, false) r := restorable.NewImage(width, height, false)
i := &Image{ i = &Image{
restorable: r, restorable: r,
filter: FilterDefault, filter: FilterDefault,
} }
}
runtime.SetFinalizer(i, (*Image).Dispose) runtime.SetFinalizer(i, (*Image).Dispose)
return i return i
} }
@ -506,15 +525,15 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
var i *Image var i *Image
s := newSharedImagePart(width, height) s := newSharedImagePart(width, height)
if s == nil { if s != nil {
r := restorable.NewImage(width, height, false)
i = &Image{ i = &Image{
restorable: r, sharedImagePart: s,
filter: filter, filter: filter,
} }
} else { } else {
r := restorable.NewImage(width, height, false)
i = &Image{ i = &Image{
sharedImagePart: s, restorable: r,
filter: filter, filter: filter,
} }
} }