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 return i, nil
} }
func newImageWithScreenFramebuffer(width, height int) *Image { func newScreenFramebufferImage(width, height int) *Image {
i := &Image{ i := &Image{
mipmap: newMipmap(shareable.NewScreenFramebufferImage(width, height)), mipmap: newMipmap(shareable.NewScreenFramebufferImage(width, height)),
filter: FilterDefault, filter: FilterDefault,

View File

@ -166,6 +166,7 @@ type Image struct {
width int width int
height int height int
disposed bool disposed bool
screen bool
backend *backend backend *backend
@ -494,6 +495,14 @@ func (i *Image) allocate(shareable bool) {
panic("shareable: the image is already allocated") 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() { if !shareable || !i.shareable() {
i.backend = &backend{ i.backend = &backend{
restorable: restorable.NewImage(i.width, i.height), restorable: restorable.NewImage(i.width, i.height),
@ -550,19 +559,13 @@ func (i *Image) Dump(path string) error {
} }
func NewScreenFramebufferImage(width, height int) *Image { func NewScreenFramebufferImage(width, height int) *Image {
backendsM.Lock() // Actual allocation is done lazily.
defer backendsM.Unlock()
r := restorable.NewScreenFramebufferImage(width, height)
i := &Image{ i := &Image{
width: width, width: width,
height: height, height: height,
backend: &backend{ screen: true,
restorable: r,
},
neverShared: true, neverShared: true,
} }
runtime.SetFinalizer(i, (*Image).disposeFromFinalizer)
return i return i
} }

View File

@ -67,7 +67,7 @@ func (c *uiContext) SetSize(screenWidth, screenHeight int, screenScale float64)
w := int(math.Ceil(float64(screenWidth) * screenScale)) w := int(math.Ceil(float64(screenWidth) * screenScale))
h := int(math.Ceil(float64(screenHeight) * screenScale)) h := int(math.Ceil(float64(screenHeight) * screenScale))
px0, py0, px1, py1 := uidriver.Get().ScreenPadding() 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.screenWidth = w
c.screenHeight = h c.screenHeight = h