From 243c224003e016646444d08c765e0646471637ab Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 13 Sep 2022 06:09:29 -0700 Subject: [PATCH] internal/restorable: bug fix: wrong panic on mixing DrawTriangles and WritePixels When DrawTriangles is called and then WritePixels is called on a sub-image, a panic happened. However, this panic actually happens only when the graphics driver requires restoring (e.g. OpenGL ES on Android). The situation was very limited, but this was a real problem on Android. This panic was introduced to prevent a rendering bug by a inmature graphics drivers, but we should no longer need this. This change just removes the panic. Updates #292 --- internal/restorable/image.go | 3 ++- internal/restorable/images_test.go | 9 ++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/internal/restorable/image.go b/internal/restorable/image.go index b7fb7083c..9e44bfb4a 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -337,7 +337,8 @@ func (i *Image) WritePixels(pixels []byte, x, y, width, height int) { // drawTrianglesHistory and basePixels cannot be mixed. if len(i.drawTrianglesHistory) > 0 { - panic("restorable: WritePixels for a part after DrawTriangles is forbidden") + i.makeStale(image.Rect(0, 0, i.width, i.height)) + return } if i.stale { diff --git a/internal/restorable/images_test.go b/internal/restorable/images_test.go index 5ba9880d1..e0cc72f42 100644 --- a/internal/restorable/images_test.go +++ b/internal/restorable/images_test.go @@ -801,13 +801,7 @@ func TestAllowWritePixelsAfterDrawTriangles(t *testing.T) { // WritePixels for a whole image doesn't panic. } -func TestDisallowWritePixelsForPartAfterDrawTriangles(t *testing.T) { - defer func() { - if r := recover(); r == nil { - t.Errorf("WritePixels for a part after DrawTriangles must panic but not") - } - }() - +func TestAllowWritePixelsForPartAfterDrawTriangles(t *testing.T) { const w, h = 16, 16 src := restorable.NewImage(w, h, restorable.ImageTypeRegular) dst := restorable.NewImage(w, h, restorable.ImageTypeRegular) @@ -822,6 +816,7 @@ func TestDisallowWritePixelsForPartAfterDrawTriangles(t *testing.T) { } dst.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{src}, [graphics.ShaderImageCount - 1][2]float32{}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeSourceOver, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, nil, nil, false) dst.WritePixels(make([]byte, 4), 0, 0, 1, 1) + // WritePixels for a part of image doesn't panic. } func TestExtend(t *testing.T) {