shareable: Allocate the screen framebuffer image lazily

This improves consistency.
This commit is contained in:
Hajime Hoshi 2019-08-25 00:43:26 +09:00
parent ef56d0a535
commit e931494c66
3 changed files with 15 additions and 12 deletions

View File

@ -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,

View File

@ -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,
},
screen: true,
neverShared: true,
}
runtime.SetFinalizer(i, (*Image).disposeFromFinalizer)
return i
}

View File

@ -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