Skip to content

Commit

Permalink
internal/ui: refactoring: integrate the render thread usages into int…
Browse files Browse the repository at this point in the history
…ernal/graphicscommand

Updates #2664
  • Loading branch information
hajimehoshi committed Jul 29, 2023
1 parent 246bd41 commit 351cab5
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 34 deletions.
4 changes: 2 additions & 2 deletions internal/atlas/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,10 +788,10 @@ 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) error {
func EndFrame(graphicsDriver graphicsdriver.Graphics, swapBuffers func()) error {
backendsM.Lock()

if err := restorable.EndFrame(graphicsDriver); err != nil {
if err := restorable.EndFrame(graphicsDriver, swapBuffers); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/buffered/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
return nil
}

func EndFrame(graphicsDriver graphicsdriver.Graphics) error {
return atlas.EndFrame(graphicsDriver)
func EndFrame(graphicsDriver graphicsdriver.Graphics, swapBuffers func()) error {
return atlas.EndFrame(graphicsDriver, swapBuffers)
}

func NewImage(width, height int, imageType atlas.ImageType) *Image {
Expand Down
13 changes: 10 additions & 3 deletions internal/graphicscommand/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,16 @@ func (q *commandQueue) Enqueue(command command) {
}

// Flush flushes the command queue.
func (q *commandQueue) Flush(graphicsDriver graphicsdriver.Graphics, endFrame bool) (err error) {
func (q *commandQueue) Flush(graphicsDriver graphicsdriver.Graphics, endFrame bool, swapBuffers func()) (err error) {
runOnRenderThread(func() {
err = q.flush(graphicsDriver, endFrame)
if err != nil {
return
}

if endFrame && swapBuffers != nil {
swapBuffers()
}
})
if endFrame {
q.uint32sBuffer.reset()
Expand Down Expand Up @@ -253,9 +260,9 @@ func (q *commandQueue) flush(graphicsDriver graphicsdriver.Graphics, endFrame bo

// FlushCommands flushes the command queue and present the screen if needed.
// If endFrame is true, the current screen might be used to present.
func FlushCommands(graphicsDriver graphicsdriver.Graphics, endFrame bool) error {
func FlushCommands(graphicsDriver graphicsdriver.Graphics, endFrame bool, swapBuffers func()) error {
flushImageBuffers()
return theCommandQueue.Flush(graphicsDriver, endFrame)
return theCommandQueue.Flush(graphicsDriver, endFrame, swapBuffers)
}

// drawTrianglesCommand represents a drawing command to draw an image on another image.
Expand Down
4 changes: 2 additions & 2 deletions internal/graphicscommand/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, buf []byte, r
result: buf,
}
theCommandQueue.Enqueue(c)
if err := theCommandQueue.Flush(graphicsDriver, false); err != nil {
if err := theCommandQueue.Flush(graphicsDriver, false, nil); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -201,7 +201,7 @@ func (i *Image) IsInvalidated(graphicsDriver graphicsdriver.Graphics) (bool, err
image: i,
}
theCommandQueue.Enqueue(c)
if err := theCommandQueue.Flush(graphicsDriver, false); err != nil {
if err := theCommandQueue.Flush(graphicsDriver, false, nil); err != nil {
return false, err
}
return c.result, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/restorable/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

func ResolveStaleImages(graphicsDriver graphicsdriver.Graphics) error {
return resolveStaleImages(graphicsDriver, false)
return resolveStaleImages(graphicsDriver, false, nil)
}

func AppendRegionRemovingDuplicates(regions *[]image.Rectangle, region image.Rectangle) {
Expand Down
8 changes: 4 additions & 4 deletions internal/restorable/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var theImages = &images{
shaders: map[*Shader]struct{}{},
}

func EndFrame(graphicsDriver graphicsdriver.Graphics) error {
func EndFrame(graphicsDriver graphicsdriver.Graphics, swapBuffers func()) error {
if debug.IsDebug {
debug.Logf("Internal image sizes:\n")
imgs := make([]*graphicscommand.Image, 0, len(theImages.images))
Expand All @@ -65,13 +65,13 @@ func EndFrame(graphicsDriver graphicsdriver.Graphics) error {
}
graphicscommand.LogImagesInfo(imgs)
}
return resolveStaleImages(graphicsDriver, true)
return resolveStaleImages(graphicsDriver, true, swapBuffers)
}

// resolveStaleImages flushes the queued draw commands and resolves all stale images.
// If endFrame is true, the current screen might be used to present when flushing the commands.
func resolveStaleImages(graphicsDriver graphicsdriver.Graphics, endFrame bool) error {
if err := graphicscommand.FlushCommands(graphicsDriver, endFrame); err != nil {
func resolveStaleImages(graphicsDriver graphicsdriver.Graphics, endFrame bool, swapBuffers func()) error {
if err := graphicscommand.FlushCommands(graphicsDriver, endFrame, swapBuffers); err != nil {
return err
}
if !needsRestoring() {
Expand Down
12 changes: 6 additions & 6 deletions internal/ui/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,27 @@ func newContext(game Game) *context {
}
}

func (c *context) updateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *userInterfaceImpl) error {
func (c *context) updateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *userInterfaceImpl, swapBuffers func()) error {
// TODO: If updateCount is 0 and vsync is disabled, swapping buffers can be skipped.
return c.updateFrameImpl(graphicsDriver, clock.UpdateFrame(), outsideWidth, outsideHeight, deviceScaleFactor, ui, false)
return c.updateFrameImpl(graphicsDriver, clock.UpdateFrame(), outsideWidth, outsideHeight, deviceScaleFactor, ui, false, swapBuffers)
}

func (c *context) forceUpdateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *userInterfaceImpl) error {
func (c *context) forceUpdateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *userInterfaceImpl, swapBuffers func()) error {
n := 1
if graphicsDriver.IsDirectX() {
// On DirectX, both framebuffers in the swap chain should be updated.
// Or, the rendering result becomes unexpected when the window is resized.
n = 2
}
for i := 0; i < n; i++ {
if err := c.updateFrameImpl(graphicsDriver, 1, outsideWidth, outsideHeight, deviceScaleFactor, ui, true); err != nil {
if err := c.updateFrameImpl(graphicsDriver, 1, outsideWidth, outsideHeight, deviceScaleFactor, ui, true, swapBuffers); err != nil {
return err
}
}
return nil
}

func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, updateCount int, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *userInterfaceImpl, forceDraw bool) (err error) {
func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, updateCount int, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *userInterfaceImpl, forceDraw bool, swapBuffers func()) (err error) {
if err := theGlobalState.error(); err != nil {
return err
}
Expand All @@ -108,7 +108,7 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update
return err
}
defer func() {
if err1 := buffered.EndFrame(graphicsDriver); err == nil && err1 != nil {
if err1 := buffered.EndFrame(graphicsDriver, swapBuffers); err == nil && err1 != nil {
err = err1
}
}()
Expand Down
10 changes: 4 additions & 6 deletions internal/ui/ui_glfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -1087,18 +1087,16 @@ func (u *userInterfaceImpl) updateGame() error {
})
})

if err := u.context.updateFrame(u.graphicsDriver, outsideWidth, outsideHeight, deviceScaleFactor, u); err != nil {
return err
}

u.renderThread.Call(func() {
if err := u.context.updateFrame(u.graphicsDriver, outsideWidth, outsideHeight, deviceScaleFactor, u, func() {
// Call updateVsync even though fpsMode is not updated.
// When toggling to fullscreen, vsync state might be reset unexpectedly (#1787).
u.updateVsyncOnRenderThread()

// This works only for OpenGL.
u.swapBuffersOnRenderThread()
})
}); err != nil {
return err
}

u.bufferOnceSwappedOnce.Do(func() {
u.mainThread.Call(func() {
Expand Down
4 changes: 2 additions & 2 deletions internal/ui/ui_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,11 @@ func (u *userInterfaceImpl) updateImpl(force bool) error {

w, h := u.outsideSize()
if force {
if err := u.context.forceUpdateFrame(u.graphicsDriver, w, h, u.DeviceScaleFactor(), u); err != nil {
if err := u.context.forceUpdateFrame(u.graphicsDriver, w, h, u.DeviceScaleFactor(), u, nil); err != nil {
return err
}
} else {
if err := u.context.updateFrame(u.graphicsDriver, w, h, u.DeviceScaleFactor(), u); err != nil {
if err := u.context.updateFrame(u.graphicsDriver, w, h, u.DeviceScaleFactor(), u, nil); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/ui_mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (u *userInterfaceImpl) update() error {
}()

w, h := u.outsideSize()
if err := u.context.updateFrame(u.graphicsDriver, w, h, deviceScale(), u); err != nil {
if err := u.context.updateFrame(u.graphicsDriver, w, h, deviceScale(), u, nil); err != nil {
return err
}
return nil
Expand Down
8 changes: 3 additions & 5 deletions internal/ui/ui_nintendosdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,11 @@ func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
u.updateInputState()
})

if err := u.context.updateFrame(u.graphicsDriver, float64(C.kScreenWidth), float64(C.kScreenHeight), deviceScaleFactor, u); err != nil {
if err := u.context.updateFrame(u.graphicsDriver, float64(C.kScreenWidth), float64(C.kScreenHeight), deviceScaleFactor, u, func() {
u.egl.swapBuffers()
}); err != nil {
return err
}

u.renderThread.Call(func() {
u.egl.swapBuffers()
})
}
})

Expand Down

0 comments on commit 351cab5

Please sign in to comment.