mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
restorable: Pixels() should return immediately when it doesn't have to access GPU
Fixes #763
This commit is contained in:
parent
454a7d8ef9
commit
fbf7007056
@ -106,6 +106,10 @@ func (i *Image) BasePixelsForTesting() []byte {
|
|||||||
return i.basePixels
|
return i.basePixels
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pixels returns the image's pixel bytes.
|
||||||
|
//
|
||||||
|
// Pixels tries to read pixels from GPU if needed.
|
||||||
|
// It is assured that GPU is not accessed if only the image's ReplacePixels is called.
|
||||||
func (i *Image) Pixels() []byte {
|
func (i *Image) Pixels() []byte {
|
||||||
i.readPixelsFromGPUIfNeeded()
|
i.readPixelsFromGPUIfNeeded()
|
||||||
return i.basePixels
|
return i.basePixels
|
||||||
@ -189,14 +193,16 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
|
|||||||
i.stale = false
|
i.stale = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if i.basePixels == nil {
|
|
||||||
i.makeStale()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(i.drawImageHistory) > 0 {
|
if len(i.drawImageHistory) > 0 {
|
||||||
i.makeStale()
|
i.makeStale()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if i.stale {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if i.basePixels == nil {
|
||||||
|
i.basePixels = make([]byte, 4*w*h)
|
||||||
|
}
|
||||||
idx := 4 * (y*w + x)
|
idx := 4 * (y*w + x)
|
||||||
if pixels != nil {
|
if pixels != nil {
|
||||||
for j := 0; j < height; j++ {
|
for j := 0; j < height; j++ {
|
||||||
|
@ -657,4 +657,26 @@ func TestClear(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReplacePixelsOnly(t *testing.T) {
|
||||||
|
const w, h = 128, 128
|
||||||
|
img := NewImage(w, h, false)
|
||||||
|
defer img.Dispose()
|
||||||
|
|
||||||
|
for i := 0; i < w*h; i += 5 {
|
||||||
|
img.ReplacePixels([]byte{1, 2, 3, 4}, i%w, i/w, 1, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BasePixelsForTesting is available without GPU accessing.
|
||||||
|
for i := 0; i < w*h; i++ {
|
||||||
|
var want color.RGBA
|
||||||
|
if i%5 == 0 {
|
||||||
|
want = color.RGBA{1, 2, 3, 4}
|
||||||
|
}
|
||||||
|
got := byteSliceToColor(img.BasePixelsForTesting(), i)
|
||||||
|
if !sameColors(got, want, 0) {
|
||||||
|
t.Errorf("got %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: How about volatile/screen images?
|
// TODO: How about volatile/screen images?
|
||||||
|
@ -69,7 +69,10 @@ func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) {
|
|||||||
// ReplacePixels on a part of image deletes other region that are rendered by DrawImage (#593, 758).
|
// ReplacePixels on a part of image deletes other region that are rendered by DrawImage (#593, 758).
|
||||||
// TODO: Add validations to ensure that an image cannot accept DrawImage after ReplacePixels on a part of
|
// TODO: Add validations to ensure that an image cannot accept DrawImage after ReplacePixels on a part of
|
||||||
// it.
|
// it.
|
||||||
newImg.ReplacePixels(oldImg.Pixels(), 0, 0, w, h)
|
//
|
||||||
|
// Pixels() returns immediately as long as only oldImg.ReplacePixels is called.
|
||||||
|
pix := oldImg.Pixels()
|
||||||
|
newImg.ReplacePixels(pix, 0, 0, w, h)
|
||||||
oldImg.Dispose()
|
oldImg.Dispose()
|
||||||
b.restorable = newImg
|
b.restorable = newImg
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user