restorable: Merge two 'copy' into one

Fixes #983
This commit is contained in:
Hajime Hoshi 2019-11-16 02:10:51 +09:00
parent aa6fc67736
commit e42cff071c
3 changed files with 15 additions and 13 deletions

View File

@ -155,15 +155,13 @@ func (i *Image) Pixels() []byte {
return c.result return c.result
} }
func (i *Image) ReplacePixels(p []byte, x, y, width, height int) { func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
// ReplacePixels for a part might invalidate the current image that are drawn by DrawTriangles (#593, #738). // ReplacePixels for a part might invalidate the current image that are drawn by DrawTriangles (#593, #738).
if i.lastCommand == lastCommandDrawTriangles { if i.lastCommand == lastCommandDrawTriangles {
if x != 0 || y != 0 || i.width != width || i.height != height { if x != 0 || y != 0 || i.width != width || i.height != height {
panic("graphicscommand: ReplacePixels for a part after DrawTriangles is forbidden") panic("graphicscommand: ReplacePixels for a part after DrawTriangles is forbidden")
} }
} }
pixels := make([]byte, len(p))
copy(pixels, p)
c := &replacePixelsCommand{ c := &replacePixelsCommand{
dst: i, dst: i,
pixels: pixels, pixels: pixels,

View File

@ -328,8 +328,15 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
// For this purpuse, images should remember which part of that is used for DrawTriangles. // For this purpuse, images should remember which part of that is used for DrawTriangles.
theImages.makeStaleIfDependingOn(i) theImages.makeStaleIfDependingOn(i)
// TODO: Avoid copying if possible (#983)
var copiedPixels []byte
if pixels != nil { if pixels != nil {
i.image.ReplacePixels(pixels, x, y, width, height) copiedPixels = make([]byte, len(pixels))
copy(copiedPixels, pixels)
}
if pixels != nil {
i.image.ReplacePixels(copiedPixels, x, y, width, height)
} else { } else {
// TODO: When pixels == nil, we don't have to care the pixel state there. In such cases, the image // TODO: When pixels == nil, we don't have to care the pixel state there. In such cases, the image
// accepts only ReplacePixels and not Fill or DrawTriangles. // accepts only ReplacePixels and not Fill or DrawTriangles.
@ -339,7 +346,7 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
if x == 0 && y == 0 && width == w && height == h { if x == 0 && y == 0 && width == w && height == h {
if pixels != nil { if pixels != nil {
i.basePixels.AddOrReplace(pixels, 0, 0, w, h) i.basePixels.AddOrReplace(copiedPixels, 0, 0, w, h)
} else { } else {
i.basePixels.Remove(0, 0, w, h) i.basePixels.Remove(0, 0, w, h)
} }
@ -361,7 +368,7 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
} }
if pixels != nil { if pixels != nil {
i.basePixels.AddOrReplace(pixels, x, y, width, height) i.basePixels.AddOrReplace(copiedPixels, x, y, width, height)
} else { } else {
i.basePixels.Remove(x, y, width, height) i.basePixels.Remove(x, y, width, height)
} }

View File

@ -37,16 +37,13 @@ func (rtp *rectToPixels) addOrReplace(pixels []byte, x, y, width, height int) {
rtp.m = map[image.Rectangle][]byte{} rtp.m = map[image.Rectangle][]byte{}
} }
copied := make([]byte, len(pixels))
copy(copied, pixels)
newr := image.Rect(x, y, x+width, y+height) newr := image.Rect(x, y, x+width, y+height)
for r := range rtp.m { for r := range rtp.m {
if r == newr { if r == newr {
// Replace the region. // Replace the region.
rtp.m[r] = copied rtp.m[r] = pixels
if r == rtp.lastR { if r == rtp.lastR {
rtp.lastPix = copied rtp.lastPix = pixels
} }
return return
} }
@ -56,9 +53,9 @@ func (rtp *rectToPixels) addOrReplace(pixels []byte, x, y, width, height int) {
} }
// Add the region. // Add the region.
rtp.m[newr] = copied rtp.m[newr] = pixels
if newr == rtp.lastR { if newr == rtp.lastR {
rtp.lastPix = copied rtp.lastPix = pixels
} }
} }