From cb79e4eabe7a870a5fc85279d916105c991d4a24 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 6 Oct 2022 15:54:52 +0900 Subject: [PATCH] internal/restorable: improve pixelsForRestore usages Updates #2375 --- internal/restorable/image.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/internal/restorable/image.go b/internal/restorable/image.go index c2d3349da..184b4659f 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -493,12 +493,13 @@ func (i *Image) readPixelsFromGPU(graphicsDriver graphicsdriver.Graphics) error } if !r.Empty() { var pix []byte - if needsRestoring() && i.needsRestoring() && r == image.Rect(0, 0, i.width, i.height) { + if needsRestoring() && i.needsRestoring() { // pixelsForRestore can be reused as basePixels was invalidated. - if i.pixelsForRestore == nil { - i.pixelsForRestore = make([]byte, 4*r.Dx()*r.Dy()) + l := 4 * r.Dx() * r.Dy() + if len(i.pixelsForRestore) < l { + i.pixelsForRestore = make([]byte, l) } - pix = i.pixelsForRestore + pix = i.pixelsForRestore[:l] } else { pix = make([]byte, 4*r.Dx()*r.Dy()) } @@ -625,13 +626,15 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error { if len(i.drawTrianglesHistory) > 0 { i.basePixels = Pixels{} // As basePixels was invalidated, pixelsForRestore can be reused. - if i.pixelsForRestore == nil { - i.pixelsForRestore = make([]byte, 4*w*h) + l := 4 * w * h + if len(i.pixelsForRestore) < l { + i.pixelsForRestore = make([]byte, l) } - if err := gimg.ReadPixels(graphicsDriver, i.pixelsForRestore, 0, 0, w, h); err != nil { + pix := i.pixelsForRestore[:l] + if err := gimg.ReadPixels(graphicsDriver, pix, 0, 0, w, h); err != nil { return err } - i.basePixels.AddOrReplace(i.pixelsForRestore, 0, 0, w, h) + i.basePixels.AddOrReplace(pix, 0, 0, w, h) } i.image = gimg