internal/graphicsdriver/opengl: remove SetGomobileGLContext

This change is needed to initialize the context at opengl.NewGraphics.

Updates #2451
This commit is contained in:
Hajime Hoshi 2022-11-13 14:51:45 +09:00
parent 5cfb7c9469
commit a0a5f2b301
13 changed files with 44 additions and 32 deletions

View File

@ -25,12 +25,15 @@ import (
// NewGraphics creates an implementation of graphicsdriver.Graphics for OpenGL.
// The returned graphics value is nil iff the error is not nil.
func NewGraphics() (graphicsdriver.Graphics, error) {
//
// context is an additional information to initialize the underlying context.
// context type depends on environments.
func NewGraphics(context any) (graphicsdriver.Graphics, error) {
if microsoftgdk.IsXbox() {
return nil, fmt.Errorf("opengl: OpenGL is not supported on Xbox")
}
g := &Graphics{}
if err := g.init(); err != nil {
if err := g.init(context); err != nil {
return nil, err
}
return g, nil

View File

@ -20,6 +20,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
)
func (g *Graphics) init() error {
func (g *Graphics) init(context any) error {
return gl.Init()
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build (android || ios || opengles) && !js
//go:build !android && !ios && !js && opengles
package opengl
@ -20,7 +20,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
)
func (g *Graphics) init() error {
func (g *Graphics) init(context any) error {
g.context.ctx = gl.DefaultContext{}
return nil
}

View File

@ -18,7 +18,7 @@ import (
"syscall/js"
)
func (g *Graphics) init() error {
func (g *Graphics) init(_ any) error {
// Do nothing.
return nil
}

View File

@ -22,6 +22,11 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
)
func (g *Graphics) SetGomobileGLContext(context mgl.Context) {
g.context.ctx = gl.NewGomobileContext(context)
func (g *Graphics) init(context any) error {
if context != nil {
g.context.ctx = gl.NewGomobileContext(context.(mgl.Context))
} else {
g.context.ctx = gl.DefaultContext{}
}
return nil
}

View File

@ -15,12 +15,14 @@
package ui
import (
"golang.org/x/mobile/gl"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
)
type graphicsDriverCreatorImpl struct {
gomobileBuild bool
gomobileContext gl.Context
}
func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) {
@ -28,8 +30,8 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics
return graphics, GraphicsLibraryOpenGL, err
}
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
func (g *graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics(g.gomobileContext)
}
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {

View File

@ -172,7 +172,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics
}
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
return opengl.NewGraphics(nil)
}
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {

View File

@ -40,7 +40,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics
}
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
return opengl.NewGraphics(nil)
}
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {

View File

@ -47,7 +47,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics
}
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
return opengl.NewGraphics(nil)
}
func (g *graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {

View File

@ -17,13 +17,15 @@ package ui
import (
"fmt"
"golang.org/x/mobile/gl"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
)
type graphicsDriverCreatorImpl struct {
gomobileBuild bool
gomobileContext gl.Context
}
func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) {
@ -38,8 +40,8 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics
return nil, GraphicsLibraryUnknown, fmt.Errorf("ui: failed to choose graphics drivers: Metal: %v, OpenGL: %v", err1, err2)
}
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
func (g *graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics(g.gomobileContext)
}
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
@ -47,7 +49,7 @@ func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error)
}
func (g *graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
if g.gomobileBuild {
if g.gomobileContext != nil {
return nil, fmt.Errorf("ui: Metal is not available with gomobile-build")
}
return metal.NewGraphics()

View File

@ -34,7 +34,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics
}
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
return opengl.NewGraphics(nil)
}
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {

View File

@ -35,7 +35,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
"github.com/hajimehoshi/ebiten/v2/internal/thread"
@ -270,24 +269,25 @@ func (u *userInterfaceImpl) run(game Game, mainloop bool) (err error) {
}()
u.context = newContext(game)
var mgl gl.Context
if mainloop {
// When gomobile-build is used, GL functions must be called via
// gl.Context so that they are called on the appropriate thread.
mgl = <-glContextCh
} else {
u.t = thread.NewOSThread()
graphicscommand.SetRenderingThread(u.t)
}
g, err := newGraphicsDriver(&graphicsDriverCreatorImpl{
gomobileBuild: mainloop,
gomobileContext: mgl,
})
if err != nil {
return err
}
u.graphicsDriver = g
if mainloop {
// When gomobile-build is used, GL functions must be called via
// gl.Context so that they are called on the appropriate thread.
ctx := <-glContextCh
g.(*opengl.Graphics).SetGomobileGLContext(ctx)
} else {
u.t = thread.NewOSThread()
graphicscommand.SetRenderingThread(u.t)
}
// If gomobile-build is used, wait for the outside size fixed.
if u.setGBuildSizeCh != nil {
<-u.setGBuildSizeCh

View File

@ -32,7 +32,7 @@ func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, Graphics
}
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
return opengl.NewGraphics(nil)
}
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {