internal/restorable: Delay initializing emptyImage

Now NeedsRestorable can always return a correct value.
This commit is contained in:
Hajime Hoshi 2021-09-09 03:26:45 +09:00
parent f08aea7b9c
commit a3570331dd
2 changed files with 11 additions and 7 deletions

View File

@ -33,11 +33,6 @@ func SetGraphicsDriver(driver driver.Graphics) {
}
func NeedsRestoring() bool {
if theGraphicsDriver == nil {
// This happens on initialization.
// Return true for fail-safe
return true
}
return theGraphicsDriver.NeedsRestoring()
}

View File

@ -108,7 +108,13 @@ type Image struct {
var emptyImage *Image
func init() {
func ensureEmptyImage() *Image {
if emptyImage != nil {
return emptyImage
}
// Initialize the empty image lazily. Some functions like NeedsRestoring might not work at the initial phase.
// w and h are the empty image's size. They indicate the 1x1 image with 1px padding around.
const w, h = 3, 3
emptyImage = &Image{
@ -126,6 +132,7 @@ func init() {
// This operation is also important when restoring emptyImage.
emptyImage.ReplacePixels(pix, 0, 0, w, h)
theImages.add(emptyImage)
return emptyImage
}
// NewImage creates an empty image with the given size.
@ -229,6 +236,8 @@ func quadVertices(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1, cr, cg, cb, ca float32
}
func clearImage(i *graphicscommand.Image) {
emptyImage := ensureEmptyImage()
if i == emptyImage.image {
panic("restorable: fillImage cannot be called on emptyImage")
}
@ -585,7 +594,7 @@ func (i *Image) restore() error {
gimg := graphicscommand.NewImage(w, h)
// Clear the image explicitly.
if i != emptyImage {
if i != ensureEmptyImage() {
// As clearImage uses emptyImage, clearImage cannot be called on emptyImage.
// It is OK to skip this since emptyImage has its entire pixel information.
clearImage(gimg)