Skip to content

Commit

Permalink
internal/ui: bug fix: test failures
Browse files Browse the repository at this point in the history
BeginFrame and EndFrame must be paired even if an error occurs.
  • Loading branch information
hajimehoshi committed Sep 16, 2024
1 parent b9dce05 commit d309085
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions internal/ui/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit d309085

Please sign in to comment.