restorable: Add ClearPixels

This hides the implementation details of allocating byte slice.

This change also adds comments about #897.
This commit is contained in:
Hajime Hoshi 2019-07-10 02:36:59 +09:00
parent 5ec2f66524
commit fd9e376ff6
2 changed files with 9 additions and 3 deletions

View File

@ -281,6 +281,12 @@ func (i *Image) CopyPixels(src *Image) {
i.makeStale()
}
// ClearPixels clears the specified region by ReplacePixels.
func (i *Image) ClearPixels(x, y, width, height int) {
// TODO: Allocating bytes for all pixels are wasteful. Allocate memory only for required regions (#897).
i.ReplacePixels(make([]byte, 4*width*height), x, y, width, height)
}
// ReplacePixels replaces the image pixels with the given pixels slice.
//
// If pixels is nil, ReplacePixels clears the specified reagion.
@ -298,6 +304,8 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
theImages.makeStaleIfDependingOn(i)
if pixels == nil {
// TODO: Allocating bytes for all pixels are wasteful. Allocate memory only for required regions
// (#897).
pixels = make([]byte, 4*width*height)
}
i.image.ReplacePixels(pixels, x, y, width, height)

View File

@ -400,9 +400,7 @@ func (i *Image) dispose(markDisposed bool) {
i.backend.page.Free(i.node)
if !i.backend.page.IsEmpty() {
// As this part can be reused, this should be cleared explicitly.
x, y, w, h := i.region()
// TODO: Now nil cannot be used here (see the test result). Fix this.
i.backend.restorable.ReplacePixels(make([]byte, 4*w*h), x, y, w, h)
i.backend.restorable.ClearPixels(i.region())
return
}