From fd9e376ff6a313c629c2b36d6cd9ece677466ce5 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 10 Jul 2019 02:36:59 +0900 Subject: [PATCH] restorable: Add ClearPixels This hides the implementation details of allocating byte slice. This change also adds comments about #897. --- internal/restorable/image.go | 8 ++++++++ internal/shareable/shareable.go | 4 +--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/internal/restorable/image.go b/internal/restorable/image.go index d0d08da7d..73abc71e4 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -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) diff --git a/internal/shareable/shareable.go b/internal/shareable/shareable.go index 4348d656c..07ada52e1 100644 --- a/internal/shareable/shareable.go +++ b/internal/shareable/shareable.go @@ -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 }