Skip to content

Commit

Permalink
restorable: Add ClearPixels
Browse files Browse the repository at this point in the history
This hides the implementation details of allocating byte slice.

This change also adds comments about #897.
  • Loading branch information
hajimehoshi committed Jul 9, 2019
1 parent 5ec2f66 commit fd9e376
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 8 additions & 0 deletions internal/restorable/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions internal/shareable/shareable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit fd9e376

Please # to comment.