internal/graphicsdriver/opengl: replace Get with NewGraphics (#2146)

This is a prepartion to return an error when a graphics driver, especially
DirectX, fails to initialize.

Updates #2142
This commit is contained in:
Hajime Hoshi 2022-06-17 02:02:29 +09:00 committed by GitHub
parent 195b060911
commit a6d415ebf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 78 additions and 67 deletions

View File

@ -23,13 +23,15 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
)
var theGraphics Graphics
func Get() *Graphics {
// NewGraphics creates an implementation of graphicsdriver.Graphcis for OpenGL.
// The returned graphics value is nil iff the error is not nil.
func NewGraphics() (graphicsdriver.Graphics, error) {
if microsoftgdk.IsXbox() {
return nil
return nil, fmt.Errorf("opengl: OpenGL is not supported on Xbox")
}
return &theGraphics
g := &Graphics{}
g.init()
return g, nil
}
type activatedTexture struct {

View File

@ -23,8 +23,8 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gles"
)
func init() {
theGraphics.context.ctx = gles.DefaultContext{}
func (g *Graphics) init() {
g.context.ctx = gles.DefaultContext{}
}
func (g *Graphics) SetGomobileGLContext(context gl.Context) {

View File

@ -0,0 +1,22 @@
// Copyright 2022 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !android && !ios
// +build !android,!ios
package opengl
func (g *Graphics) init() {
// Do nothing.
}

View File

@ -22,8 +22,8 @@ import (
)
type graphicsDriverGetter interface {
getAuto() graphicsdriver.Graphics
getOpenGL() graphicsdriver.Graphics
newAuto() (graphicsdriver.Graphics, error)
newOpenGL() (graphicsdriver.Graphics, error)
getDirectX() graphicsdriver.Graphics
getMetal() graphicsdriver.Graphics
}
@ -33,15 +33,23 @@ func chooseGraphicsDriver(getter graphicsDriverGetter) (graphicsdriver.Graphics,
switch env := os.Getenv(envName); env {
case "", "auto":
if g := getter.getAuto(); g != nil {
return g, nil
g, err := getter.newAuto()
if err != nil {
return nil, err
}
if g == nil {
return nil, fmt.Errorf("ui: no graphics library is available")
case "opengl":
if g := getter.getOpenGL(); g != nil {
return g, nil
}
return g, nil
case "opengl":
g, err := getter.newOpenGL()
if err != nil {
return nil, err
}
if g == nil {
return nil, fmt.Errorf("ui: %s=%s is specified but OpenGL is not available", envName, env)
}
return g, nil
case "directx":
if g := getter.getDirectX(); g != nil {
return g, nil

View File

@ -23,15 +23,12 @@ type graphicsDriverGetterImpl struct {
gomobileBuild bool
}
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
return g.getOpenGL()
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
return g.newOpenGL()
}
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
if g := opengl.Get(); g != nil {
return g
}
return nil
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {

View File

@ -27,15 +27,12 @@ import (
type graphicsDriverGetterImpl struct{}
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
return g.getOpenGL()
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
return g.newOpenGL()
}
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
if g := opengl.Get(); g != nil {
return g
}
return nil
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {

View File

@ -258,18 +258,15 @@ type graphicsDriverGetterImpl struct {
transparent bool
}
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
if m := g.getMetal(); m != nil {
return m
return m, nil
}
return g.getOpenGL()
return g.newOpenGL()
}
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
if g := opengl.Get(); g != nil {
return g
}
return nil
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {

View File

@ -35,15 +35,12 @@ type graphicsDriverGetterImpl struct {
transparent bool
}
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
return g.getOpenGL()
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
return g.newOpenGL()
}
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
if g := opengl.Get(); g != nil {
return g
}
return nil
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {

View File

@ -35,18 +35,15 @@ type graphicsDriverGetterImpl struct {
transparent bool
}
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
if d := g.getDirectX(); d != nil {
return d
return d, nil
}
return g.getOpenGL()
return g.newOpenGL()
}
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
if g := opengl.Get(); g != nil {
return g
}
return nil
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (g *graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {

View File

@ -27,18 +27,15 @@ type graphicsDriverGetterImpl struct {
gomobileBuild bool
}
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
if m := g.getMetal(); m != nil {
return m
return m, nil
}
return g.getOpenGL()
return g.newOpenGL()
}
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
if g := opengl.Get(); g != nil {
return g
}
return nil
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {

View File

@ -27,15 +27,12 @@ import (
type graphicsDriverGetterImpl struct{}
func (g *graphicsDriverGetterImpl) getAuto() graphicsdriver.Graphics {
return g.getOpenGL()
func (g *graphicsDriverGetterImpl) newAuto() (graphicsdriver.Graphics, error) {
return g.newOpenGL()
}
func (*graphicsDriverGetterImpl) getOpenGL() graphicsdriver.Graphics {
if g := opengl.Get(); g != nil {
return g
}
return nil
func (*graphicsDriverGetterImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (*graphicsDriverGetterImpl) getDirectX() graphicsdriver.Graphics {