internal/graphicsdriver/opengl: Give up restorign on browsers

Using restorable images is expensive as this sometimes requires to
load pixels from GPU. On browsers, just reloading the application
should be fine when the context lost happens.

Closes #1603
This commit is contained in:
Hajime Hoshi 2021-06-26 16:50:12 +09:00
parent 38ce46328a
commit 8ff42f55a1
3 changed files with 3 additions and 29 deletions

View File

@ -20,7 +20,6 @@ package main
import ( import (
"fmt" "fmt"
"syscall/js" "syscall/js"
"time"
) )
func (g *Game) loseAndRestoreContext() { func (g *Game) loseAndRestoreContext() {
@ -48,17 +47,5 @@ func (g *Game) loseAndRestoreContext() {
ext.Call("loseContext") ext.Call("loseContext")
fmt.Println("Lost the context!") fmt.Println("Lost the context!")
fmt.Println("The context is automatically restored after 3 seconds.")
g.lost = true g.lost = true
// If and only if the context is lost by loseContext, you need to call restoreContext. Note that in usual
// case of context lost, you cannot call restoreContext but the context should be restored automatically.
//
// After the context is lost, update will not be called. Instead, fire the goroutine to restore the context.
go func() {
time.Sleep(3 * time.Second)
ext.Call("restoreContext")
fmt.Println("Restored the context!")
g.lost = false
}()
} }

View File

@ -23,7 +23,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gles" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gles"
"github.com/hajimehoshi/ebiten/v2/internal/jsutil" "github.com/hajimehoshi/ebiten/v2/internal/jsutil"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir" "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
"github.com/hajimehoshi/ebiten/v2/internal/web"
) )
type ( type (
@ -85,7 +84,6 @@ const (
var ( var (
isWebGL2Available = !forceWebGL1 && (js.Global().Get("WebGL2RenderingContext").Truthy() || js.Global().Get("go2cpp").Truthy()) isWebGL2Available = !forceWebGL1 && (js.Global().Get("WebGL2RenderingContext").Truthy() || js.Global().Get("go2cpp").Truthy())
needsRestoring = !web.IsMobileBrowser() && !js.Global().Get("go2cpp").Truthy()
) )
type contextImpl struct { type contextImpl struct {
@ -526,7 +524,8 @@ func (c *context) flush() {
} }
func (c *context) needsRestoring() bool { func (c *context) needsRestoring() bool {
return needsRestoring // Though it is possible to have a logic to restore the graphics data for GPU, do not use it for performance (#1603).
return false
} }
func (c *context) canUsePBO() bool { func (c *context) canUsePBO() bool {

View File

@ -22,7 +22,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
"github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/hooks"
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
) )
var ( var (
@ -58,7 +57,6 @@ type UserInterface struct {
cursorShape driver.CursorShape cursorShape driver.CursorShape
sizeChanged bool sizeChanged bool
contextLost bool
lastDeviceScaleFactor float64 lastDeviceScaleFactor float64
@ -290,11 +288,6 @@ func (u *UserInterface) loop(context driver.UIContext) <-chan error {
var cf js.Func var cf js.Func
f := func() { f := func() {
if u.contextLost {
requestAnimationFrame.Invoke(cf)
return
}
if err := u.update(); err != nil { if err := u.update(); err != nil {
close(reqStopAudioCh) close(reqStopAudioCh)
<-resStopAudioCh <-resStopAudioCh
@ -545,12 +538,7 @@ func setCanvasEventHandlers(v js.Value) {
v.Call("addEventListener", "webglcontextlost", js.FuncOf(func(this js.Value, args []js.Value) interface{} { v.Call("addEventListener", "webglcontextlost", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
e := args[0] e := args[0]
e.Call("preventDefault") e.Call("preventDefault")
theUI.contextLost = true window.Get("location").Call("reload")
restorable.OnContextLost()
return nil
}))
v.Call("addEventListener", "webglcontextrestored", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
theUI.contextLost = false
return nil return nil
})) }))
} }