From 39688dd27de385acfcd7dfefe0ecaac458f6e8fd Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 20 Oct 2023 02:43:47 +0900 Subject: [PATCH] internal/processtest: improve test issue2815 WritePixels for 1x1 image is optimized at internal/ui, and Ebitengine doesn't read pixels from GPU in this case. Enlarge the tested image, and use DrawImage to ensure to invalidate the internal cache. Updates #2815 --- internal/processtest/testdata/issue2815.go | 7 +++++-- internal/ui/ui.go | 9 +++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/internal/processtest/testdata/issue2815.go b/internal/processtest/testdata/issue2815.go index c95e208f7..9ef351aad 100644 --- a/internal/processtest/testdata/issue2815.go +++ b/internal/processtest/testdata/issue2815.go @@ -37,14 +37,17 @@ func (g *Game) Update() error { g.end0 = make(chan struct{}) g.end1 = make(chan struct{}) g.errCh = make(chan error) - img := ebiten.NewImage(1, 1) - img.WritePixels([]byte{0xff, 0xff, 0xff, 0xff}) + src := ebiten.NewImage(1, 2) + src.WritePixels([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}) + img := ebiten.NewImage(1, 2) go func() { t := time.Tick(time.Microsecond) loop: for { select { case <-t: + // Call DrawImage every time in order to invalidate the internal pixels cache. + img.DrawImage(src, nil) got := img.At(0, 0).(color.RGBA) want := color.RGBA{0xff, 0xff, 0xff, 0xff} if got != want { diff --git a/internal/ui/ui.go b/internal/ui/ui.go index d60a5234c..b5ec342b4 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -130,15 +130,16 @@ func (u *UserInterface) readPixels(mipmap *mipmap.Mipmap, pixels []byte, region if !ok { ch := make(chan error) u.context.appendDeferredFunc(func() { + defer close(ch) ok, err := mipmap.ReadPixels(u.graphicsDriver, pixels, region) + if err != nil { + ch <- err + return + } if !ok { // This never reaches since this function must be called in a frame. panic("ui: ReadPixels unexpectedly failed") } - if err != nil { - ch <- err - } - close(ch) }) // If this function is called from the game (Update/Draw) goroutine in between two frames,