internal/ui: refactoring

This commit is contained in:
Hajime Hoshi 2022-12-30 13:26:42 +09:00
parent 3f753b7086
commit fa4916d063
3 changed files with 12 additions and 16 deletions

View File

@ -14,7 +14,11 @@
package graphicscommand
var theThread Thread
import (
"github.com/hajimehoshi/ebiten/v2/internal/thread"
)
var theThread Thread = thread.NewNoopThread()
type Thread interface {
Call(f func())
@ -29,12 +33,5 @@ func SetRenderingThread(thread Thread) {
// runOnRenderingThread calls f on the rendering thread, and returns an error if any.
func runOnRenderingThread(f func()) {
// The thread is nil when 1) GOOS=js or 2) using golang.org/x/mobile/gl.
// When golang.org/x/mobile/gl is used, all the GL functions are called via Context, which already runs on an
// appropriate thread.
if theThread == nil {
f()
return
}
theThread.Call(f)
}

View File

@ -18,12 +18,6 @@ import (
"context"
)
// Thread defines threading behavior in Ebitengine.
type Thread interface {
Call(func())
Loop(context.Context) error
}
// OSThread represents an OS thread.
type OSThread struct {
funcs chan func()

View File

@ -17,6 +17,7 @@
package ui
import (
stdcontext "context"
"errors"
"fmt"
"image"
@ -32,7 +33,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
"github.com/hajimehoshi/ebiten/v2/internal/thread"
)
func driverCursorModeToGLFWCursorMode(mode CursorMode) int {
@ -108,10 +108,15 @@ type userInterfaceImpl struct {
defaultFramebufferSizeCallback glfw.FramebufferSizeCallback
framebufferSizeCallbackCh chan struct{}
mainThread thread.Thread
mainThread threadInterface
m sync.RWMutex
}
type threadInterface interface {
Loop(ctx stdcontext.Context) error
Call(f func())
}
const (
maxInt = int(^uint(0) >> 1)
minInt = -maxInt - 1