internal/ui: refactoring: remove Graphics()

This commit is contained in:
Hajime Hoshi 2022-02-13 04:15:06 +09:00
parent 1c57393393
commit e9cfbc1630
10 changed files with 34 additions and 24 deletions

View File

@ -41,7 +41,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
) )
var graphics graphicsdriver.Graphics var theGraphics graphicsdriver.Graphics
func supportsMetal() bool { func supportsMetal() bool {
// On macOS 10.11 El Capitan, there is a rendering issue on Metal (#781). // On macOS 10.11 El Capitan, there is a rendering issue on Metal (#781).
@ -55,13 +55,13 @@ func supportsMetal() bool {
var graphicsOnce sync.Once var graphicsOnce sync.Once
func Graphics() graphicsdriver.Graphics { func graphics() graphicsdriver.Graphics {
graphicsOnce.Do(func() { graphicsOnce.Do(func() {
if supportsMetal() { if supportsMetal() {
graphics = metal.Get() theGraphics = metal.Get()
return return
} }
graphics = opengl.Get() theGraphics = opengl.Get()
}) })
return graphics return theGraphics
} }

View File

@ -22,7 +22,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
) )
func Graphics() graphicsdriver.Graphics { func graphics() graphicsdriver.Graphics {
// Metal might not be supported on emulators on Intel machines. // Metal might not be supported on emulators on Intel machines.
return opengl.Get() return opengl.Get()
} }

View File

@ -22,7 +22,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal"
) )
func Graphics() graphicsdriver.Graphics { func graphics() graphicsdriver.Graphics {
g := metal.Get() g := metal.Get()
if g == nil { if g == nil {
panic("ui: Metal is not available on this iOS device") panic("ui: Metal is not available on this iOS device")

View File

@ -22,6 +22,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
) )
func Graphics() graphicsdriver.Graphics { func graphics() graphicsdriver.Graphics {
return opengl.Get() return opengl.Get()
} }

View File

@ -19,5 +19,5 @@ import (
) )
func init() { func init() {
graphicscommand.SetGraphicsDriver(Graphics()) graphicscommand.SetGraphicsDriver(graphics())
} }

View File

@ -16,6 +16,8 @@ package ui
import ( import (
"errors" "errors"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
) )
type Context interface { type Context interface {
@ -76,3 +78,11 @@ const (
WindowResizingModeOnlyFullscreenEnabled WindowResizingModeOnlyFullscreenEnabled
WindowResizingModeEnabled WindowResizingModeEnabled
) )
func NeedsClearingScreen() bool {
return graphics().NeedsClearingScreen()
}
func FramebufferYDirection() graphicsdriver.YDirection {
return graphics().FramebufferYDirection()
}

View File

@ -684,7 +684,7 @@ func (u *UserInterface) createWindow(width, height int) error {
// Ensure to consume this callback. // Ensure to consume this callback.
u.waitForFramebufferSizeCallback(u.window, nil) u.waitForFramebufferSizeCallback(u.window, nil)
if Graphics().IsGL() { if graphics().IsGL() {
u.window.MakeContextCurrent() u.window.MakeContextCurrent()
} }
@ -735,7 +735,7 @@ func (u *UserInterface) registerWindowSetSizeCallback() {
if err := u.context.ForceUpdateFrame(); err != nil { if err := u.context.ForceUpdateFrame(); err != nil {
return err return err
} }
if Graphics().IsGL() { if graphics().IsGL() {
u.t.Call(func() { u.t.Call(func() {
u.swapBuffers() u.swapBuffers()
}) })
@ -835,7 +835,7 @@ event:
} }
func (u *UserInterface) init() error { func (u *UserInterface) init() error {
if Graphics().IsGL() { if graphics().IsGL() {
glfw.WindowHint(glfw.ClientAPI, glfw.OpenGLAPI) glfw.WindowHint(glfw.ClientAPI, glfw.OpenGLAPI)
glfw.WindowHint(glfw.ContextVersionMajor, 2) glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 1) glfw.WindowHint(glfw.ContextVersionMinor, 1)
@ -856,7 +856,7 @@ func (u *UserInterface) init() error {
transparent = glfw.True transparent = glfw.True
} }
glfw.WindowHint(glfw.TransparentFramebuffer, transparent) glfw.WindowHint(glfw.TransparentFramebuffer, transparent)
Graphics().SetTransparent(u.isInitScreenTransparent()) graphics().SetTransparent(u.isInitScreenTransparent())
// Before creating a window, set it unresizable no matter what u.isInitWindowResizable() is (#1987). // Before creating a window, set it unresizable no matter what u.isInitWindowResizable() is (#1987).
// Making the window resizable here doesn't work correctly when switching to enable resizing. // Making the window resizable here doesn't work correctly when switching to enable resizing.
@ -908,7 +908,7 @@ func (u *UserInterface) init() error {
u.window.Show() u.window.Show()
if g, ok := Graphics().(interface{ SetWindow(uintptr) }); ok { if g, ok := graphics().(interface{ SetWindow(uintptr) }); ok {
g.SetWindow(u.nativeWindow()) g.SetWindow(u.nativeWindow())
} }
@ -1086,7 +1086,7 @@ func (u *UserInterface) loop() error {
// swapBuffers also checks IsGL, so this condition is redundant. // swapBuffers also checks IsGL, so this condition is redundant.
// However, (*thread).Call is not good for performance due to channels. // However, (*thread).Call is not good for performance due to channels.
// Let's avoid this whenever possible (#1367). // Let's avoid this whenever possible (#1367).
if Graphics().IsGL() { if graphics().IsGL() {
u.t.Call(u.swapBuffers) u.t.Call(u.swapBuffers)
} }
@ -1108,7 +1108,7 @@ func (u *UserInterface) loop() error {
// swapBuffers must be called from the main thread. // swapBuffers must be called from the main thread.
func (u *UserInterface) swapBuffers() { func (u *UserInterface) swapBuffers() {
if Graphics().IsGL() { if graphics().IsGL() {
u.window.SwapBuffers() u.window.SwapBuffers()
} }
} }
@ -1165,7 +1165,7 @@ func (u *UserInterface) adjustWindowSizeBasedOnSizeLimitsInDIP(width, height int
func (u *UserInterface) setWindowSizeInDIP(width, height int, fullscreen bool) { func (u *UserInterface) setWindowSizeInDIP(width, height int, fullscreen bool) {
width, height = u.adjustWindowSizeBasedOnSizeLimitsInDIP(width, height) width, height = u.adjustWindowSizeBasedOnSizeLimitsInDIP(width, height)
Graphics().SetFullscreen(fullscreen) graphics().SetFullscreen(fullscreen)
scale := u.deviceScaleFactor(u.currentMonitor()) scale := u.deviceScaleFactor(u.currentMonitor())
if u.windowWidthInDIP == width && u.windowHeightInDIP == height && u.isFullscreen() == fullscreen && u.lastDeviceScaleFactor == scale { if u.windowWidthInDIP == width && u.windowHeightInDIP == height && u.isFullscreen() == fullscreen && u.lastDeviceScaleFactor == scale {
@ -1239,7 +1239,7 @@ func (u *UserInterface) setWindowSizeInDIPImpl(width, height int, fullscreen boo
// Swapping buffer is necesary to prevent the image lag (#1004). // Swapping buffer is necesary to prevent the image lag (#1004).
// TODO: This might not work when vsync is disabled. // TODO: This might not work when vsync is disabled.
if Graphics().IsGL() { if graphics().IsGL() {
glfw.PollEvents() glfw.PollEvents()
u.swapBuffers() u.swapBuffers()
} }
@ -1286,7 +1286,7 @@ func (u *UserInterface) setWindowSizeInDIPImpl(width, height int, fullscreen boo
// updateVsync must be called on the main thread. // updateVsync must be called on the main thread.
func (u *UserInterface) updateVsync() { func (u *UserInterface) updateVsync() {
if Graphics().IsGL() { if graphics().IsGL() {
// SwapInterval is affected by the current monitor of the window. // SwapInterval is affected by the current monitor of the window.
// This needs to be called at least after SetMonitor. // This needs to be called at least after SetMonitor.
// Without SwapInterval after SetMonitor, vsynch doesn't work (#375). // Without SwapInterval after SetMonitor, vsynch doesn't work (#375).
@ -1300,7 +1300,7 @@ func (u *UserInterface) updateVsync() {
glfw.SwapInterval(0) glfw.SwapInterval(0)
} }
} }
Graphics().SetVsyncEnabled(u.fpsMode == FPSModeVsyncOn) graphics().SetVsyncEnabled(u.fpsMode == FPSModeVsyncOn)
} }
// currentMonitor returns the current active monitor. // currentMonitor returns the current active monitor.

View File

@ -316,7 +316,7 @@ func (u *UserInterface) setNativeFullscreen(fullscreen bool) {
} }
func (u *UserInterface) adjustViewSize() { func (u *UserInterface) adjustViewSize() {
if Graphics().IsGL() { if graphics().IsGL() {
return return
} }
C.adjustViewSize(C.uintptr_t(u.window.GetCocoaWindow())) C.adjustViewSize(C.uintptr_t(u.window.GetCocoaWindow()))

View File

@ -279,7 +279,7 @@ func (u *UserInterface) run(context Context, mainloop bool) (err error) {
// When mainloop is true, gomobile-build is used. In this case, GL functions must be called via // When mainloop is true, gomobile-build is used. In this case, GL functions must be called via
// gl.Context so that they are called on the appropriate thread. // gl.Context so that they are called on the appropriate thread.
ctx := <-glContextCh ctx := <-glContextCh
Graphics().(*opengl.Graphics).SetGomobileGLContext(ctx) graphics().(*opengl.Graphics).SetGomobileGLContext(ctx)
} else { } else {
u.t = thread.NewOSThread() u.t = thread.NewOSThread()
graphicscommand.SetMainThread(u.t) graphicscommand.SetMainThread(u.t)

View File

@ -200,7 +200,7 @@ func (c *uiContext) updateFrameImpl(updateCount int) error {
} }
c.game.Draw(c.offscreen) c.game.Draw(c.offscreen)
if ui.Graphics().NeedsClearingScreen() { if ui.NeedsClearingScreen() {
// This clear is needed for fullscreen mode or some mobile platforms (#622). // This clear is needed for fullscreen mode or some mobile platforms (#622).
c.screen.Clear() c.screen.Clear()
} }
@ -208,7 +208,7 @@ func (c *uiContext) updateFrameImpl(updateCount int) error {
op := &DrawImageOptions{} op := &DrawImageOptions{}
s := c.screenScale(ui.Get().DeviceScaleFactor()) s := c.screenScale(ui.Get().DeviceScaleFactor())
switch vd := ui.Graphics().FramebufferYDirection(); vd { switch vd := ui.FramebufferYDirection(); vd {
case graphicsdriver.Upward: case graphicsdriver.Upward:
op.GeoM.Scale(s, -s) op.GeoM.Scale(s, -s)
_, h := c.offscreen.Size() _, h := c.offscreen.Size()