graphics: Try to restore the context on browsers (WIP)

This commit is contained in:
Hajime Hoshi 2017-01-21 02:47:31 +09:00
parent de987be85f
commit bccf76867e
5 changed files with 28 additions and 23 deletions

View File

@ -180,7 +180,7 @@ func (i *Image) ReplacePixels(p []uint8) error {
} }
func (i *Image) IsInvalidated(context *opengl.Context) bool { func (i *Image) IsInvalidated(context *opengl.Context) bool {
return context.IsContextLost(i.texture.native) return !context.IsTexture(i.texture.native)
} }
func (i *Image) createFramebufferIfNeeded(context *opengl.Context) (*framebuffer, error) { func (i *Image) createFramebufferIfNeeded(context *opengl.Context) (*framebuffer, error) {

View File

@ -514,7 +514,3 @@ func (c *Context) Flush() {
return nil return nil
}) })
} }
func (c *Context) IsContextLost(t Texture) bool {
return !c.IsTexture(t)
}

View File

@ -87,6 +87,7 @@ func init() {
type context struct { type context struct {
gl *webgl.Context gl *webgl.Context
loseContext *js.Object
lastProgramID programID lastProgramID programID
} }
@ -116,6 +117,15 @@ func NewContext() (*Context, error) {
} }
c := &Context{} c := &Context{}
c.gl = gl c.gl = gl
// Getting an extension might fail after the context is lost, so
// it is required to get the extension here.
c.loseContext = gl.GetExtension("WEBGL_lose_context")
if c.loseContext != nil {
// This testing function name is temporary.
js.Global.Set("_ebiten_loseContextForTesting", func() {
c.loseContext.Call("loseContext")
})
}
return c, nil return c, nil
} }
@ -408,7 +418,13 @@ func (c *Context) Flush() {
gl.Flush() gl.Flush()
} }
func (c *Context) IsContextLost(t Texture) bool { func (c *Context) IsContextLost() bool {
gl := c.gl gl := c.gl
return gl.IsContextLost() return gl.IsContextLost()
} }
func (c *Context) RestoreContext() {
if c.loseContext != nil {
c.loseContext.Call("restoreContext")
}
}

View File

@ -430,7 +430,3 @@ func (c *Context) Flush() {
gl := c.gl gl := c.gl
gl.Flush() gl.Flush()
} }
func (c *Context) IsContextLost(t Texture) bool {
return !c.IsTexture(t)
}

View File

@ -30,13 +30,11 @@ type userInterface struct {
scale float64 scale float64
deviceScale float64 deviceScale float64
sizeChanged bool sizeChanged bool
contextRestored bool
windowFocus bool windowFocus bool
} }
var currentUI = &userInterface{ var currentUI = &userInterface{
sizeChanged: true, sizeChanged: true,
contextRestored: true,
windowFocus: true, windowFocus: true,
} }
@ -74,8 +72,8 @@ func (u *userInterface) update(g GraphicsContext) error {
if !u.windowFocus { if !u.windowFocus {
return nil return nil
} }
if !u.contextRestored { if glContext.IsContextLost() {
return nil glContext.RestoreContext()
} }
currentInput.updateGamepads() currentInput.updateGamepads()
if u.sizeChanged { if u.sizeChanged {
@ -235,12 +233,11 @@ func initialize() error {
canvas.Call("addEventListener", "webglcontextlost", func(e *js.Object) { canvas.Call("addEventListener", "webglcontextlost", func(e *js.Object) {
e.Call("preventDefault") e.Call("preventDefault")
currentUI.contextRestored = false
}) })
canvas.Call("addEventListener", "webglcontextrestored", func(e *js.Object) { canvas.Call("addEventListener", "webglcontextrestored", func(e *js.Object) {
// TODO: Call preventDefault? // Do nothing.
currentUI.contextRestored = true
}) })
return nil return nil
} }