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 package graphicscommand
var theThread Thread import (
"github.com/hajimehoshi/ebiten/v2/internal/thread"
)
var theThread Thread = thread.NewNoopThread()
type Thread interface { type Thread interface {
Call(f func()) Call(f func())
@ -29,12 +33,5 @@ func SetRenderingThread(thread Thread) {
// runOnRenderingThread calls f on the rendering thread, and returns an error if any. // runOnRenderingThread calls f on the rendering thread, and returns an error if any.
func runOnRenderingThread(f func()) { 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) theThread.Call(f)
} }

View File

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

View File

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