From d30908522abfbf38a10653c010fe5aa5b61e7a9b Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 16 Sep 2024 23:54:11 +0900 Subject: [PATCH] internal/ui: bug fix: test failures BeginFrame and EndFrame must be paired even if an error occurs. --- internal/ui/context.go | 45 +++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/internal/ui/context.go b/internal/ui/context.go index 8c4795886..17aea82a8 100644 --- a/internal/ui/context.go +++ b/internal/ui/context.go @@ -97,9 +97,33 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update debug.FrameLogf("----\n") + var needSwapBuffers bool + if err := atlas.BeginFrame(graphicsDriver); err != nil { return err } + defer func() { + if err1 := atlas.EndFrame(); err1 != nil && err == nil { + err = err1 + return + } + + if needSwapBuffers { + if err1 := atlas.SwapBuffers(graphicsDriver); err1 != nil && err == nil { + err = err1 + return + } + } else { + now := time.Now() + defer func() { + c.lastSwapBufferTime = now + }() + if delta := time.Second/60 - now.Sub(c.lastSwapBufferTime); delta > 0 { + // When swapping buffers is skipped and Draw is called too early, sleep for a while to suppress CPU usages (#2890). + time.Sleep(delta) + } + } + }() // Flush deferred functions, like reading pixels from GPU. if err := c.processFuncsInFrame(ui); err != nil { @@ -152,30 +176,11 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update } // Draw the game. - needSwapBuffers, err := c.drawGame(graphicsDriver, ui, forceDraw) + needSwapBuffers, err = c.drawGame(graphicsDriver, ui, forceDraw) if err != nil { return err } - if err := atlas.EndFrame(); err != nil { - return err - } - - if needSwapBuffers { - if err := atlas.SwapBuffers(graphicsDriver); err != nil { - return err - } - } else { - now := time.Now() - defer func() { - c.lastSwapBufferTime = now - }() - if delta := time.Second/60 - now.Sub(c.lastSwapBufferTime); delta > 0 { - // When swapping buffers is skipped and Draw is called too early, sleep for a while to suppress CPU usages (#2890). - time.Sleep(delta) - } - } - return nil }