diff --git a/image.go b/image.go index 9e3b72d37..e5eb3a958 100644 --- a/image.go +++ b/image.go @@ -66,12 +66,10 @@ func (m *mipmap) level(r image.Rectangle, level int) *shareable.Image { if w2 == 0 || h2 == 0 { return nil } - var s *shareable.Image + s := shareable.NewImage(w2, h2) if m.orig.IsVolatile() { // TODO: As s is cleared every frame, is there any reason to keep it? - s = shareable.NewVolatileImage(w2, h2) - } else { - s = shareable.NewImage(w2, h2) + s.MakeVolatile() } var src *shareable.Image @@ -743,8 +741,10 @@ func NewImage(width, height int, filter Filter) (*Image, error) { // // If width or height is less than 1 or more than device-dependent maximum size, newVolatileImage panics. func newVolatileImage(width, height int) *Image { + s := shareable.NewImage(width, height) + s.MakeVolatile() i := &Image{ - mipmap: newMipmap(shareable.NewVolatileImage(width, height)), + mipmap: newMipmap(s), } i.addr = i runtime.SetFinalizer(i, (*Image).Dispose) diff --git a/internal/shareable/shareable.go b/internal/shareable/shareable.go index f0471c73e..6bc3207fa 100644 --- a/internal/shareable/shareable.go +++ b/internal/shareable/shareable.go @@ -481,22 +481,13 @@ func (i *Image) allocate(shareable bool) { return } -func NewVolatileImage(width, height int) *Image { +func (i *Image) MakeVolatile() { backendsM.Lock() defer backendsM.Unlock() - r := restorable.NewImage(width, height) - r.MakeVolatile() - i := &Image{ - width: width, - height: height, - backend: &backend{ - restorable: r, - }, - neverShared: true, - } - runtime.SetFinalizer(i, (*Image).Dispose) - return i + i.ensureNotShared() + i.backend.restorable.MakeVolatile() + i.neverShared = true } func NewScreenFramebufferImage(width, height int) *Image { diff --git a/internal/shareable/shareable_test.go b/internal/shareable/shareable_test.go index 3fec99807..b6bae2219 100644 --- a/internal/shareable/shareable_test.go +++ b/internal/shareable/shareable_test.go @@ -140,7 +140,8 @@ func Disabled_TestReshared(t *testing.T) { } img2.ReplacePixels(pix) - img3 := NewVolatileImage(size, size) + img3 := NewImage(size, size) + img3.MakeVolatile() defer img3.Dispose() img1.ReplacePixels(make([]byte, 4*size*size)) want = false