diff --git a/image.go b/image.go index 32749d88c..5ca6785af 100644 --- a/image.go +++ b/image.go @@ -724,7 +724,7 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) { return i, nil } -func newImageWithScreenFramebuffer(width, height int) *Image { +func newScreenFramebufferImage(width, height int) *Image { i := &Image{ mipmap: newMipmap(shareable.NewScreenFramebufferImage(width, height)), filter: FilterDefault, diff --git a/internal/shareable/shareable.go b/internal/shareable/shareable.go index 554c3e690..e25cb97ba 100644 --- a/internal/shareable/shareable.go +++ b/internal/shareable/shareable.go @@ -166,6 +166,7 @@ type Image struct { width int height int disposed bool + screen bool backend *backend @@ -494,6 +495,14 @@ func (i *Image) allocate(shareable bool) { panic("shareable: the image is already allocated") } + if i.screen { + i.backend = &backend{ + restorable: restorable.NewScreenFramebufferImage(i.width, i.height), + } + runtime.SetFinalizer(i, (*Image).disposeFromFinalizer) + return + } + if !shareable || !i.shareable() { i.backend = &backend{ restorable: restorable.NewImage(i.width, i.height), @@ -550,19 +559,13 @@ func (i *Image) Dump(path string) error { } func NewScreenFramebufferImage(width, height int) *Image { - backendsM.Lock() - defer backendsM.Unlock() - - r := restorable.NewScreenFramebufferImage(width, height) + // Actual allocation is done lazily. i := &Image{ - width: width, - height: height, - backend: &backend{ - restorable: r, - }, + width: width, + height: height, + screen: true, neverShared: true, } - runtime.SetFinalizer(i, (*Image).disposeFromFinalizer) return i } diff --git a/uicontext.go b/uicontext.go index 73cbafd9e..a19c57d09 100644 --- a/uicontext.go +++ b/uicontext.go @@ -67,7 +67,7 @@ func (c *uiContext) SetSize(screenWidth, screenHeight int, screenScale float64) w := int(math.Ceil(float64(screenWidth) * screenScale)) h := int(math.Ceil(float64(screenHeight) * screenScale)) px0, py0, px1, py1 := uidriver.Get().ScreenPadding() - c.screen = newImageWithScreenFramebuffer(w+int(math.Ceil(px0+px1)), h+int(math.Ceil(py0+py1))) + c.screen = newScreenFramebufferImage(w+int(math.Ceil(px0+px1)), h+int(math.Ceil(py0+py1))) c.screenWidth = w c.screenHeight = h