diff --git a/internal/atlas/image.go b/internal/atlas/image.go index fe530cd28..836bea6d2 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -756,7 +756,7 @@ func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, path stri return i.backend.restorable.Dump(graphicsDriver, path, blackbg, image.Rect(0, 0, i.width, i.height)) } -func EndFrame(graphicsDriver graphicsdriver.Graphics, swapBuffersForGL func()) error { +func EndFrame() error { backendsM.Lock() defer backendsM.Unlock() defer func() { @@ -767,10 +767,6 @@ func EndFrame(graphicsDriver graphicsdriver.Graphics, swapBuffersForGL func()) e panic("atlas: inFrame must be true in EndFrame") } - if err := restorable.EndFrame(graphicsDriver, swapBuffersForGL); err != nil { - return err - } - for _, b := range theBackends { b.sourceInThisFrame = false } @@ -778,6 +774,22 @@ func EndFrame(graphicsDriver graphicsdriver.Graphics, swapBuffersForGL func()) e return nil } +func SwapBuffers(graphicsDriver graphicsdriver.Graphics, swapBuffersForGL func()) error { + func() { + backendsM.Lock() + defer backendsM.Unlock() + + if inFrame { + panic("atlas: inFrame must be false in SwapBuffer") + } + }() + + if err := restorable.SwapBuffers(graphicsDriver, swapBuffersForGL); err != nil { + return err + } + return nil +} + func floorPowerOf2(x int) int { if x <= 0 { return 0 diff --git a/internal/buffered/image.go b/internal/buffered/image.go index b084e1a03..a2151f643 100644 --- a/internal/buffered/image.go +++ b/internal/buffered/image.go @@ -33,14 +33,6 @@ type Image struct { pixels []byte } -func BeginFrame(graphicsDriver graphicsdriver.Graphics) error { - return atlas.BeginFrame(graphicsDriver) -} - -func EndFrame(graphicsDriver graphicsdriver.Graphics, swapBuffersForGL func()) error { - return atlas.EndFrame(graphicsDriver, swapBuffersForGL) -} - func NewImage(width, height int, imageType atlas.ImageType) *Image { return &Image{ width: width, diff --git a/internal/restorable/images.go b/internal/restorable/images.go index 1aa46a3f0..60e1d32ba 100644 --- a/internal/restorable/images.go +++ b/internal/restorable/images.go @@ -56,7 +56,7 @@ var theImages = &images{ shaders: map[*Shader]struct{}{}, } -func EndFrame(graphicsDriver graphicsdriver.Graphics, swapBuffersForGL func()) error { +func SwapBuffers(graphicsDriver graphicsdriver.Graphics, swapBuffersForGL func()) error { if debug.IsDebug { debug.Logf("Internal image sizes:\n") imgs := make([]*graphicscommand.Image, 0, len(theImages.images)) diff --git a/internal/ui/context.go b/internal/ui/context.go index 7e041aa2c..b212f7c28 100644 --- a/internal/ui/context.go +++ b/internal/ui/context.go @@ -19,7 +19,6 @@ import ( "sync" "github.com/hajimehoshi/ebiten/v2/internal/atlas" - "github.com/hajimehoshi/ebiten/v2/internal/buffered" "github.com/hajimehoshi/ebiten/v2/internal/clock" "github.com/hajimehoshi/ebiten/v2/internal/debug" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" @@ -102,14 +101,9 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update debug.Logf("----\n") - if err := buffered.BeginFrame(graphicsDriver); err != nil { + if err := atlas.BeginFrame(graphicsDriver); err != nil { return err } - defer func() { - if err1 := buffered.EndFrame(graphicsDriver, swapBuffersForGL); err == nil && err1 != nil { - err = err1 - } - }() // Flush deferred functions, like reading pixels from GPU. if err := c.flushDeferredFuncs(ui); err != nil { @@ -164,6 +158,14 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update return err } + if err := atlas.EndFrame(); err != nil { + return err + } + + if err := atlas.SwapBuffers(graphicsDriver, swapBuffersForGL); err != nil { + return err + } + return nil }