ui: Don't panic at DeviceScaleFactor in init

Fixes #597
This commit is contained in:
Hajime Hoshi 2018-05-04 15:26:47 +09:00
parent d41f959b96
commit 9a06b31556
4 changed files with 44 additions and 5 deletions

View File

@ -24,12 +24,10 @@ var (
) )
func DeviceScale() float64 { func DeviceScale() float64 {
s := 0.0
m.Lock() m.Lock()
defer m.Unlock()
if scale == 0.0 { if scale == 0.0 {
scale = impl() scale = impl()
} }
s = scale return scale
m.Unlock()
return s
} }

View File

@ -64,6 +64,9 @@ import (
) )
func impl() float64 { func impl() float64 {
if !jni.IsJVMAvailable() {
return 0
}
s := 0.0 s := 0.0
if err := jni.RunOnJVM(func(vm, env, ctx uintptr) error { if err := jni.RunOnJVM(func(vm, env, ctx uintptr) error {
s = float64(C.deviceScale(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx))) s = float64(C.deviceScale(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx)))

View File

@ -0,0 +1,34 @@
// Copyright 2018 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 jni
/*
#include <jni.h>
#include <stdbool.h>
JavaVM* current_vm;
static bool isJVMAvailable() {
return current_vm != NULL;
}
*/
import "C"
// IsJVMAvailable returns a boolean value indicating whether JVM is available or not.
//
// In init functions, JVM is not available.
func IsJVMAvailable() bool {
return bool(C.isJVMAvailable())
}

6
run.go
View File

@ -466,7 +466,11 @@ func SetWindowIcon(iconImages []image.Image) {
// DeviceScaleFactor returns a meaningful value on high-DPI display environment, // DeviceScaleFactor returns a meaningful value on high-DPI display environment,
// otherwise DeviceScaleFactor returns 1. // otherwise DeviceScaleFactor returns 1.
// //
// This function is concurrent-safe. // DeviceScaleFactor might return 0 on init function on some devices like Android.
// Don't expect DeviceScaleFactor returns a valid value until main starts.
// Then, it is not recommended to call DeviceScaleFactor from init functions.
//
// DeviceScaleFactor is concurrent-safe.
func DeviceScaleFactor() float64 { func DeviceScaleFactor() float64 {
return devicescale.DeviceScale() return devicescale.DeviceScale()
} }