uidriver/glfw: Refactoring

This commit is contained in:
Hajime Hoshi 2020-01-13 18:23:18 +09:00
parent c1adf60e69
commit e797aed6c9

View File

@ -29,40 +29,35 @@ package glfw
import "C" import "C"
import ( import (
"sync"
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/metal" "github.com/hajimehoshi/ebiten/internal/graphicsdriver/metal"
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/metal/mtl" "github.com/hajimehoshi/ebiten/internal/graphicsdriver/metal/mtl"
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/opengl" "github.com/hajimehoshi/ebiten/internal/graphicsdriver/opengl"
) )
var ( var graphics driver.Graphics
// isMetalSupported represents whether Metal is supported.
isMetalSupported = true
// isMetalSupportedOnce initializes isMetalSupported. func supportsMetal() bool {
//
// Use sync.Once instead of init function to avoid init-order dependency (#886).
isMetalSupportedOnce sync.Once
)
func (*UserInterface) Graphics() driver.Graphics {
isMetalSupportedOnce.Do(func() {
// On old mac devices like iMac 2011, Metal is not supported (#779). // On old mac devices like iMac 2011, Metal is not supported (#779).
if _, err := mtl.CreateSystemDefaultDevice(); err != nil { if _, err := mtl.CreateSystemDefaultDevice(); err != nil {
isMetalSupported = false return false
return
} }
// 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).
// Use the OpenGL in macOS 10.11 or older. // Use the OpenGL in macOS 10.11 or older.
if C.getMacOSMinorVersion() <= 11 { if C.getMacOSMinorVersion() <= 11 {
isMetalSupported = false return false
}
return true
}
func init() {
if supportsMetal() {
graphics = metal.Get()
return return
} }
}) graphics = opengl.Get()
if isMetalSupported { }
return metal.Get()
} func (*UserInterface) Graphics() driver.Graphics {
return opengl.Get() return graphics
} }