internal/atlas: split EndFrame into EndFrame and SwapBuffers

This enables to do something asynchronously while executing SwapBuffers
in a different goroutine.

This is a preparation for HandleInput.

Updates #1704
This commit is contained in:
Hajime Hoshi 2023-10-24 00:18:26 +09:00
parent f1999e5742
commit b94c3fa9bb
4 changed files with 27 additions and 21 deletions

View File

@ -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

View File

@ -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,

View File

@ -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))

View File

@ -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
}