From 151dfffad6f57561fa2ffa9b84d93468a43f1581 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 23 Mar 2022 02:45:57 +0900 Subject: [PATCH] internal/restorable: simplify canDetectContextLostExplicitly This constant was set with some wrong assumptions: 1. On Android, recovering was needed. 2. On iOS, OpenGL ES was used when a. The architecture was 386 or amd64 == an emulator is used b. The build tag ebitengl was not specified c. gomobile-build was used 3. On browsers, recovering was needed. 1., 2b, and 2c are correct. 2a. is not correct: Now emulators are available on all the architectures with both Metal and OpenGL. 3. is not correct: Ebiten no longer recovers the contest lost. Now, Ebiten can detect a context lost explicitly when 1. On Android 2. On iOS and on gomobile-build (When gomobile-build is used, OpenGL should always be used) Based on this fact, this change changes the constant to a variable, and fixes the logic to set the variable. --- internal/restorable/const_gles.go | 20 -------------------- internal/restorable/const_js.go | 18 ------------------ internal/restorable/const_others.go | 22 ---------------------- internal/restorable/images.go | 10 +++++++--- 4 files changed, 7 insertions(+), 63 deletions(-) delete mode 100644 internal/restorable/const_gles.go delete mode 100644 internal/restorable/const_js.go delete mode 100644 internal/restorable/const_others.go diff --git a/internal/restorable/const_gles.go b/internal/restorable/const_gles.go deleted file mode 100644 index 9b8dcd35a..000000000 --- a/internal/restorable/const_gles.go +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2020 The Ebiten 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 && ebitengl) -// +build android ios,ebitengl - -package restorable - -const canDetectContextLostExplicitly = true diff --git a/internal/restorable/const_js.go b/internal/restorable/const_js.go deleted file mode 100644 index 71de15d31..000000000 --- a/internal/restorable/const_js.go +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2020 The Ebiten 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. - -package restorable - -// canDetectContextLostExplicitly reports whether the context lost can be detected by handlers in an explicit way. -const canDetectContextLostExplicitly = true diff --git a/internal/restorable/const_others.go b/internal/restorable/const_others.go deleted file mode 100644 index 90c07cb79..000000000 --- a/internal/restorable/const_others.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2020 The Ebiten 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 && !js && (!ios || !ebitengl) -// +build !android -// +build !js -// +build !ios !ebitengl - -package restorable - -const canDetectContextLostExplicitly = false diff --git a/internal/restorable/images.go b/internal/restorable/images.go index 218dc060a..0601df01c 100644 --- a/internal/restorable/images.go +++ b/internal/restorable/images.go @@ -17,6 +17,7 @@ package restorable import ( "image" "path/filepath" + "runtime" "github.com/hajimehoshi/ebiten/v2/internal/debug" "github.com/hajimehoshi/ebiten/v2/internal/graphicscommand" @@ -292,8 +293,11 @@ func MaxImageSize(graphicsDriver graphicsdriver.Graphics) int { // OnContextLost is called when the context lost is detected in an explicit way. func OnContextLost() { - if !canDetectContextLostExplicitly { - panic("restorable: OnContextLost cannot be called in this environment") - } + canDetectContextLostExplicitly = true theImages.contextLost = true } + +// canDetectContextLostExplicitly reports whether Ebiten can detect a context lost in an explicit way. +// On Android, a context lost can be detected via GLSurfaceView.Renderer.onSurfaceCreated. +// On iOS w/ OpenGL ES, this can be detected only when gomobile-build is used. +var canDetectContextLostExplicitly = runtime.GOOS == "android"