internal/ui: bug fix: test failures

BeginFrame and EndFrame must be paired even if an error occurs.
This commit is contained in:
Hajime Hoshi 2024-09-16 23:54:11 +09:00
parent b9dce05ca1
commit d30908522a

View File

@ -97,9 +97,33 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update
debug.FrameLogf("----\n") debug.FrameLogf("----\n")
var needSwapBuffers bool
if err := atlas.BeginFrame(graphicsDriver); err != nil { if err := atlas.BeginFrame(graphicsDriver); err != nil {
return err 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. // Flush deferred functions, like reading pixels from GPU.
if err := c.processFuncsInFrame(ui); err != nil { if err := c.processFuncsInFrame(ui); err != nil {
@ -152,30 +176,11 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update
} }
// Draw the game. // Draw the game.
needSwapBuffers, err := c.drawGame(graphicsDriver, ui, forceDraw) needSwapBuffers, err = c.drawGame(graphicsDriver, ui, forceDraw)
if err != nil { if err != nil {
return err 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 return nil
} }