restorable: Bug fix: Reading pixels from a volatile image might fail

Fixes #793
This commit is contained in:
Hajime Hoshi 2019-01-22 03:35:48 +09:00
parent 837571d05c
commit 158f4fb3ed
2 changed files with 26 additions and 2 deletions

View File

@ -241,7 +241,7 @@ func (i *Image) DrawImage(img *Image, vertices []float32, indices []uint16, colo
}
theImages.makeStaleIfDependingOn(i)
if img.stale || img.volatile || i.screen || !IsRestoringEnabled() {
if img.stale || img.volatile || i.screen || !IsRestoringEnabled() || i.volatile {
i.makeStale()
} else {
i.appendDrawImageHistory(img, vertices, indices, colorm, mode, filter, address)
@ -274,7 +274,7 @@ func (i *Image) appendDrawImageHistory(image *Image, vertices []float32, indices
}
func (i *Image) readPixelsFromGPUIfNeeded() {
if i.basePixels == nil || i.drawImageHistory != nil || i.stale {
if i.basePixels == nil || len(i.drawImageHistory) > 0 || i.stale {
graphicscommand.FlushCommands()
i.readPixelsFromGPU()
i.drawImageHistory = nil

View File

@ -652,3 +652,27 @@ func TestReplacePixelsOnly(t *testing.T) {
}
// TODO: How about volatile/screen images?
// Issue #793
func TestReadPixelsFromVolatileImage(t *testing.T) {
const w, h = 16, 16
dst := NewImage(w, h, true)
src := NewImage(w, h, false)
// First, make sure that dst has pixels
dst.ReplacePixels(make([]byte, 4*w*h), 0, 0, w, h)
// Second, draw src to dst. If the implementation is correct, drawImageHistory is created.
fill(src, 0xff, 0xff, 0xff, 0xff)
vs := graphics.QuadVertices(w, h, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
is := graphics.QuadIndices()
dst.DrawImage(src, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
// Read the pixels. If the implementation is correct, dst tries to read its pixels from GPU due to
// drawImageHistory items.
want := byte(0xff)
got, _, _, _ := dst.At(0, 0)
if got != want {
t.Errorf("got: %v, want: %v", got, want)
}
}