devicescale: Use app.RunOnJVM and remove internal/jni

This commit is contained in:
Hajime Hoshi 2018-08-06 23:29:31 +09:00
parent ed06944e1b
commit 59601e6900
5 changed files with 16 additions and 159 deletions

9
go.mod
View File

@ -7,16 +7,15 @@ require (
github.com/gopherjs/gopherjs v0.0.0-20180628210949-0892b62f0d9f
github.com/gopherjs/webgl v0.0.0-20180508003723-39bd6d41eeb5
github.com/hajimehoshi/go-mp3 v0.1.1
github.com/hajimehoshi/go-mplusbitmap v1.0.0
github.com/hajimehoshi/oto v0.1.1
github.com/hajimehoshi/go-mplusbitmap v1.0.1
github.com/hajimehoshi/oto v0.1.2
github.com/jakecoffman/cp v0.1.0
github.com/jfreymuth/oggvorbis v1.0.0
github.com/jfreymuth/vorbis v1.0.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/theckman/go-flock v0.4.0
golang.org/x/exp v0.0.0-20180710024300-14dda7b62fcd // indirect
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81
golang.org/x/mobile v0.0.0-20180719123216-371a4e8cb797
golang.org/x/sys v0.0.0-20180715085529-ac767d655b30
golang.org/x/mobile v0.0.0-20180806140643-507816974b79
golang.org/x/sys v0.0.0-20180806082429-34b17bdb4300
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)

16
go.sum
View File

@ -12,10 +12,11 @@ github.com/gopherjs/webgl v0.0.0-20180508003723-39bd6d41eeb5 h1:vrKguNTgy5fq7lTz
github.com/gopherjs/webgl v0.0.0-20180508003723-39bd6d41eeb5/go.mod h1:obh2agNa9TmQ5C1MrSr2jgLIqV0b4Cl96m/ig2VAXwM=
github.com/hajimehoshi/go-mp3 v0.1.1 h1:Y33fAdTma70fkrxnc9u50Uq0lV6eZ+bkAlssdMmCwUc=
github.com/hajimehoshi/go-mp3 v0.1.1/go.mod h1:4i+c5pDNKDrxl1iu9iG90/+fhP37lio6gNhjCx9WBJw=
github.com/hajimehoshi/go-mplusbitmap v1.0.0 h1:RTVrmfV5be1Qcgu2wTRnF7Oi/C/QlPh9IQsOredV90M=
github.com/hajimehoshi/go-mplusbitmap v1.0.0/go.mod h1:p0ABuCLcowP4ovBofe0b5Bra2t2ITZOBNpZZca5RCfA=
github.com/hajimehoshi/oto v0.1.1 h1:EG+WxxeAfde1mI0adhLYvGbKgDCxm7bCTd6g+JIA6vI=
github.com/hajimehoshi/go-mplusbitmap v1.0.1 h1:Q7vA2o/9uIXS0isGPeXc4ahLMFBfZl592OZgb/MH90w=
github.com/hajimehoshi/go-mplusbitmap v1.0.1/go.mod h1:zW+y7DzqcVGNiGVPga2gau26lxIzv7zvtOEjgMiz6pU=
github.com/hajimehoshi/oto v0.1.1/go.mod h1:hUiLWeBQnbDu4pZsAhOnGqMI1ZGibS6e2qhQdfpwz04=
github.com/hajimehoshi/oto v0.1.2 h1:qYOk4cOkcFLWov5OtekLWvgP8PCLb5iDrHWnuvMg7LM=
github.com/hajimehoshi/oto v0.1.2/go.mod h1:1iS1lbBMU/SpoU8yZkduCxbqV/hVdGRiNwgbvYO3sG4=
github.com/jakecoffman/cp v0.1.0 h1:sgSYEGUgfwiT447fRjloa2c5b6UyYP+7muR3gQK+Ep0=
github.com/jakecoffman/cp v0.1.0/go.mod h1:a3xPx9N8RyFAACD644t2dj/nK4SuLg1v+jL61m2yVo4=
github.com/jfreymuth/oggvorbis v1.0.0 h1:aOpiihGrFLXpsh2osOlEvTcg5/aluzGQeC7m3uYWOZ0=
@ -27,15 +28,16 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/browser v0.0.0-20170505125900-c90ca0c84f15/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/theckman/go-flock v0.4.0 h1:bcqNkS4RTQBGWybG7IBimUMxnLz53Qes1+D4QaOhzJc=
github.com/theckman/go-flock v0.4.0/go.mod h1:kjuth3y9VJ2aNlkNEO99G/8lp9fMIKaGyBmh84IBheM=
golang.org/x/exp v0.0.0-20180710024300-14dda7b62fcd h1:nLIcFw7GiqKXUS7HiChg6OAYWgASB2H97dZKd1GhDSs=
golang.org/x/exp v0.0.0-20180710024300-14dda7b62fcd/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81 h1:00VmoueYNlNz/aHIilyyQz/MHSqGoWJzpFv/HW8xpzI=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/mobile v0.0.0-20180719123216-371a4e8cb797 h1:gmTaV4xxZIYHrypO/QiWsueG4XyBko/adxuEujz5FIs=
golang.org/x/mobile v0.0.0-20180719123216-371a4e8cb797/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/sys v0.0.0-20180715085529-ac767d655b30 h1:4bYUqrXBoiI7UFQeibUwFhvcHfaEeL75O3lOcZa964o=
golang.org/x/sys v0.0.0-20180715085529-ac767d655b30/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/mobile v0.0.0-20180806140643-507816974b79 h1:t2JRgCWkY7Qaa1J2jal+wqC9OjbyHCHwIA9rVlRUSMo=
golang.org/x/mobile v0.0.0-20180806140643-507816974b79/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/sys v0.0.0-20180806082429-34b17bdb4300 h1:eJa+6+7jje7fOYUrLnwKNR9kcpvLANj1Asw0Ou1pBiI=
golang.org/x/sys v0.0.0-20180806082429-34b17bdb4300/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -60,15 +60,13 @@ import "C"
import (
"fmt"
"github.com/hajimehoshi/ebiten/internal/jni"
"golang.org/x/mobile/app"
)
func impl() float64 {
if !jni.IsJVMAvailable() {
panic("devicescale: JVM is not available yet: is this called from init funcitons?")
}
s := 0.0
if err := jni.RunOnJVM(func(vm, env, ctx uintptr) error {
if err := app.RunOnJVM(func(vm, env, ctx uintptr) error {
// TODO: This might be crash when this is called from init(). How can we detect this?
s = float64(C.deviceScale(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx)))
return nil
}); err != nil {

View File

@ -1,34 +0,0 @@
// 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())
}

View File

@ -1,108 +0,0 @@
// Copyright 2015 The Go Authors. All rights reserved.
// This file is copied from golang.org/x/mobile/internal/mobileinit/ctx_android.go
// and editted.
// This file is licensed under the 3-clause BSD license.
package jni
/*
#include <jni.h>
#include <stdlib.h>
// These definitions are duplicated with those in ctx_android.go of golang.org/x/mobile/internal/mobileinit package.
// To be exact, this might cause undefined behavior, but some compilers including GCC and Clang work as a common extension.
// (J.5.11 Multiple external definitions)
JavaVM* current_vm;
jobject current_ctx;
static char* lockJNI(uintptr_t* envp, int* attachedp) {
JNIEnv* env;
if (current_vm == NULL) {
return "no current JVM";
}
*attachedp = 0;
switch ((*current_vm)->GetEnv(current_vm, (void**)&env, JNI_VERSION_1_6)) {
case JNI_OK:
break;
case JNI_EDETACHED:
if ((*current_vm)->AttachCurrentThread(current_vm, &env, 0) != 0) {
return "cannot attach to JVM";
}
*attachedp = 1;
break;
case JNI_EVERSION:
return "bad JNI version";
default:
return "unknown JNI error from GetEnv";
}
*envp = (uintptr_t)env;
return NULL;
}
static char* checkException(uintptr_t jnienv) {
jthrowable exc;
JNIEnv* env = (JNIEnv*)jnienv;
if (!(*env)->ExceptionCheck(env)) {
return NULL;
}
exc = (*env)->ExceptionOccurred(env);
(*env)->ExceptionClear(env);
jclass clazz = (*env)->FindClass(env, "java/lang/Throwable");
jmethodID toString = (*env)->GetMethodID(env, clazz, "toString", "()Ljava/lang/String;");
jobject msgStr = (*env)->CallObjectMethod(env, exc, toString);
return (char*)(*env)->GetStringUTFChars(env, msgStr, 0);
}
static void unlockJNI() {
(*current_vm)->DetachCurrentThread(current_vm);
}
*/
import "C"
import (
"errors"
"runtime"
"unsafe"
)
// RunOnJVM executes fn on the current VM context.
//
// RunOnJVM should not be called on init function since the current VM might not be initialized yet.
func RunOnJVM(fn func(vm, env, ctx uintptr) error) error {
errch := make(chan error)
go func() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
env := C.uintptr_t(0)
attached := C.int(0)
if errStr := C.lockJNI(&env, &attached); errStr != nil {
errch <- errors.New(C.GoString(errStr))
return
}
if attached != 0 {
defer C.unlockJNI()
}
vm := uintptr(unsafe.Pointer(C.current_vm))
if err := fn(vm, uintptr(env), uintptr(C.current_ctx)); err != nil {
errch <- err
return
}
if exc := C.checkException(env); exc != nil {
errch <- errors.New(C.GoString(exc))
C.free(unsafe.Pointer(exc))
return
}
errch <- nil
}()
return <-errch
}