From 2da56c00c8363ec3b5ff785786e53d9db5834636 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 2 Jan 2024 16:54:07 +0900 Subject: [PATCH] internal/ui: remove renderThread Updates #2714 --- internal/graphicscommand/thread.go | 14 +++++++++----- internal/ui/run.go | 7 ++----- internal/ui/ui.go | 3 --- internal/ui/ui_mobile.go | 6 ++---- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/internal/graphicscommand/thread.go b/internal/graphicscommand/thread.go index 8cafef1b7..27194d0b5 100644 --- a/internal/graphicscommand/thread.go +++ b/internal/graphicscommand/thread.go @@ -15,16 +15,20 @@ package graphicscommand import ( + "context" + "github.com/hajimehoshi/ebiten/v2/internal/thread" ) var theRenderThread thread.Thread = thread.NewNoopThread() -// SetRenderThread must be called from the rendering thread where e.g. OpenGL works. -// -// TODO: Create thread in this package instead of setting it externally. -func SetRenderThread(thread thread.Thread) { - theRenderThread = thread +// SetOSThreadAsRenderThread sets an OS thread as rendering thread e.g. for OpenGL. +func SetOSThreadAsRenderThread() { + theRenderThread = thread.NewOSThread() +} + +func LoopRenderThread(ctx context.Context) { + _ = theRenderThread.Loop(ctx) } // runOnRenderThread calls f on the rendering thread. diff --git a/internal/ui/run.go b/internal/ui/run.go index b5d8007e1..0596bf999 100644 --- a/internal/ui/run.go +++ b/internal/ui/run.go @@ -35,8 +35,7 @@ func (u *UserInterface) Run(game Game, options *RunOptions) error { func (u *UserInterface) runMultiThread(game Game, options *RunOptions) error { u.mainThread = thread.NewOSThread() - u.renderThread = thread.NewOSThread() - graphicscommand.SetRenderThread(u.renderThread) + graphicscommand.SetOSThreadAsRenderThread() // Set the running state true after the main thread is set, and before initOnMainThread is called (#2742). // TODO: As the existence of the main thread is the same as the value of `running`, this is redundant. @@ -58,7 +57,7 @@ func (u *UserInterface) runMultiThread(game Game, options *RunOptions) error { // Run the render thread. wg.Go(func() error { defer cancel() - _ = u.renderThread.Loop(ctx) + graphicscommand.LoopRenderThread(ctx) return nil }) @@ -76,8 +75,6 @@ func (u *UserInterface) runMultiThread(game Game, options *RunOptions) error { func (u *UserInterface) runSingleThread(game Game, options *RunOptions) error { // Initialize the main thread first so the thread is available at u.run (#809). u.mainThread = thread.NewNoopThread() - u.renderThread = thread.NewNoopThread() - graphicscommand.SetRenderThread(u.renderThread) u.setRunning(true) defer u.setRunning(false) diff --git a/internal/ui/ui.go b/internal/ui/ui.go index b5243f23c..fd4a3ab45 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -82,9 +82,6 @@ type UserInterface struct { mainThread thread.Thread - // TODO: Remove this (#2714). - renderThread thread.Thread - userInterfaceImpl } diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index 271c4f6a7..93b4ddd63 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -38,7 +38,6 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/hook" "github.com/hajimehoshi/ebiten/v2/internal/restorable" - "github.com/hajimehoshi/ebiten/v2/internal/thread" ) var ( @@ -91,7 +90,7 @@ func (u *UserInterface) Update() error { cancel() }() - _ = u.renderThread.Loop(ctx) + graphicscommand.LoopRenderThread(ctx) return nil } @@ -273,8 +272,7 @@ func (u *UserInterface) runMobile(game Game, mainloop bool, options *RunOptions) // gl.Context so that they are called on the appropriate thread. mgl = <-glContextCh } else { - u.renderThread = thread.NewOSThread() - graphicscommand.SetRenderThread(u.renderThread) + graphicscommand.SetOSThreadAsRenderThread() } u.setRunning(true)