restorable: Refactoring: Use ReplacePixels(nil, ...) for clearing an image

This commit is contained in:
Hajime Hoshi 2018-08-09 01:16:46 +09:00
parent ebb1d26e67
commit 3daaef2cab
3 changed files with 17 additions and 36 deletions

View File

@ -97,6 +97,12 @@ func (i *Image) Fill(clr color.Color) error {
}
func (i *Image) fill(r, g, b, a uint8) {
if r == 0 && g == 0 && b == 0 && a == 0 {
i.shareableImages[0].ReplacePixels(nil)
i.disposeMipmaps()
return
}
wd, hd := i.Size()
if wd*hd <= 256 {
@ -126,7 +132,7 @@ func (i *Image) fill(r, g, b, a uint8) {
}
op.CompositeMode = CompositeModeCopy
op.Filter = FilterNearest
i.drawImage(emptyImage, op, r == 0 && g == 0 && b == 0 && a == 0)
i.drawImage(emptyImage, op)
}
func (i *Image) disposeMipmaps() {
@ -170,11 +176,11 @@ func (i *Image) disposeMipmaps() {
//
// DrawImage always returns nil as of 1.5.0-alpha.
func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
i.drawImage(img, options, false)
i.drawImage(img, options)
return nil
}
func (i *Image) drawImage(img *Image, options *DrawImageOptions, clear bool) {
func (i *Image) drawImage(img *Image, options *DrawImageOptions) {
i.copyCheck()
if img.isDisposed() {
panic("ebiten: the given image to DrawImage must not be disposed")
@ -316,9 +322,6 @@ func (i *Image) drawImage(img *Image, options *DrawImageOptions, clear bool) {
is := graphicsutil.QuadIndices()
i.shareableImages[0].DrawImage(src, vs, is, options.ColorM.impl, mode, filter)
}
if clear {
i.shareableImages[0].ClearRestorableState()
}
i.disposeMipmaps()
}

View File

@ -155,7 +155,7 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
if pixels != nil {
i.image.ReplacePixels(pixels, x, y, width, height)
} else {
// There is not 'drawImageHistoryItem' for this image and dummyImage.
// There are not 'drawImageHistoryItem's for this image and dummyImage (in clear).
// This means dummyImage might not be restored yet when this image is restored.
// However, that's ok since this image will be stale or have updated pixel data
// and this image can be restored without dummyImage.
@ -216,17 +216,6 @@ func (i *Image) DrawImage(img *Image, vertices []float32, indices []uint16, colo
i.image.DrawImage(img.image, vertices, indices, colorm, mode, filter)
}
// ClearState clears the internal state. This is useful when it is known that the image is cleared and not stale.
//
// TODO: This is a tricky function since it is hard to determine when to call ClearState.
// Refactor this.
func (i *Image) ClearState() {
// TODO: Ensure i.image is already cleared?
i.basePixels = nil
i.drawImageHistory = nil
i.stale = false
}
// appendDrawImageHistory appends a draw-image history item to the image.
func (i *Image) appendDrawImageHistory(image *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
if i.stale || i.volatile || i.screen {

View File

@ -232,22 +232,6 @@ func (i *Image) DrawImage(img *Image, vertices []float32, indices []uint16, colo
// }
}
func (i *Image) ClearRestorableState() {
backendsM.Lock()
defer backendsM.Unlock()
if i.disposed {
panic("shareable: the image must not be disposed")
}
if i.backend == nil {
panic("shareable: the image must have backend")
}
if i.isShared() {
panic("shareable: the image must not be shared")
}
i.backend.restorable.ClearState()
}
func (i *Image) ReplacePixels(p []byte) {
backendsM.Lock()
defer backendsM.Unlock()
@ -259,13 +243,18 @@ func (i *Image) replacePixels(p []byte) {
panic("shareable: the image must not be disposed")
}
if i.backend == nil {
if p == nil {
return
}
i.allocate(true)
}
x, y, w, h := i.region()
if p != nil {
if l := 4 * w * h; len(p) != l {
panic(fmt.Sprintf("shareable: len(p) was %d but must be %d", len(p), l))
}
}
i.backend.restorable.ReplacePixels(p, x, y, w, h)
}