mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 19:28:57 +01:00
restorable: Avoid memory allocating when an entire image is cleared
This commit is contained in:
parent
bb0b8ca83b
commit
b2b09ccec0
@ -169,10 +169,16 @@ 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 i.basePixels == nil {
|
if pixels != nil {
|
||||||
i.basePixels = make([]byte, 4*w*h)
|
if i.basePixels == nil {
|
||||||
|
i.basePixels = make([]byte, 4*w*h)
|
||||||
|
}
|
||||||
|
copy(i.basePixels, pixels)
|
||||||
|
} else {
|
||||||
|
// If basePixels is nil, the restored pixels are cleared.
|
||||||
|
// See restore() implementation.
|
||||||
|
i.basePixels = nil
|
||||||
}
|
}
|
||||||
copy(i.basePixels, pixels)
|
|
||||||
i.drawImageHistory = nil
|
i.drawImageHistory = nil
|
||||||
i.stale = false
|
i.stale = false
|
||||||
return
|
return
|
||||||
|
@ -97,6 +97,26 @@ func TestRestore(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRestoreWithoutDraw(t *testing.T) {
|
||||||
|
img0 := NewImage(1024, 1024, false)
|
||||||
|
defer img0.Dispose()
|
||||||
|
|
||||||
|
// If there is no drawing command on img0, img0 is cleared when restored.
|
||||||
|
|
||||||
|
ResolveStaleImages()
|
||||||
|
if err := Restore(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 1024*1024; i++ {
|
||||||
|
want := color.RGBA{0x00, 0x00, 0x00, 0x00}
|
||||||
|
got := byteSliceToColor(img0.BasePixelsForTesting(), i)
|
||||||
|
if !sameColors(got, want, 0) {
|
||||||
|
t.Errorf("got %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRestoreChain(t *testing.T) {
|
func TestRestoreChain(t *testing.T) {
|
||||||
const num = 10
|
const num = 10
|
||||||
imgs := []*Image{}
|
imgs := []*Image{}
|
||||||
|
Loading…
Reference in New Issue
Block a user