devicescale: Use app.RunOnJVM and remove internal/jni

This commit is contained in:
Hajime Hoshi 2018-08-06 23:29:31 +09:00
parent 77e905eb25
commit ed99719fdb
5 changed files with 14 additions and 156 deletions

9
go.mod
View File

@ -5,17 +5,16 @@ require (
github.com/go-gl/glfw v0.0.0-20180426074136-46a8d530c326
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/gopherjs/gopherwasm v0.1.1
github.com/hajimehoshi/go-mp3 v0.1.1
github.com/hajimehoshi/go-mp3 v0.1.0
github.com/hajimehoshi/go-mplusbitmap v1.0.1
github.com/hajimehoshi/oto v0.1.1
github.com/hajimehoshi/oto v0.0.0-20180806144010-f48c4541b748
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-20180618222554-6621de06e1e9
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
)

11
go.sum
View File

@ -8,10 +8,13 @@ github.com/gopherjs/gopherjs v0.0.0-20180628210949-0892b62f0d9f h1:FDM3EtwZLyhW4
github.com/gopherjs/gopherjs v0.0.0-20180628210949-0892b62f0d9f/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gopherjs/gopherwasm v0.1.1 h1:R/3+SfgCFStiql6ICfyfke1WtpglfjIvTEBux8R1euc=
github.com/gopherjs/gopherwasm v0.1.1/go.mod h1:kx4n9a+MzHH0BJJhvlsQ65hqLFXDO/m256AsaDPQ+/4=
github.com/hajimehoshi/go-mp3 v0.1.0/go.mod h1:SUuVIxBoRGgFS+RYeGIJoiD2wUWy7EajN5tmFlh2ncc=
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.1 h1:Q7vA2o/9uIXS0isGPeXc4ahLMFBfZl592OZgb/MH90w=
github.com/hajimehoshi/go-mplusbitmap v1.0.1/go.mod h1:zW+y7DzqcVGNiGVPga2gau26lxIzv7zvtOEjgMiz6pU=
github.com/hajimehoshi/oto v0.0.0-20180806144010-f48c4541b748 h1:Z1gfFEdnDHvVFalVt91q1M8tcekQw6l9uvABUFanJhc=
github.com/hajimehoshi/oto v0.0.0-20180806144010-f48c4541b748/go.mod h1:1iS1lbBMU/SpoU8yZkduCxbqV/hVdGRiNwgbvYO3sG4=
github.com/hajimehoshi/oto v0.1.1 h1:EG+WxxeAfde1mI0adhLYvGbKgDCxm7bCTd6g+JIA6vI=
github.com/hajimehoshi/oto v0.1.1/go.mod h1:hUiLWeBQnbDu4pZsAhOnGqMI1ZGibS6e2qhQdfpwz04=
github.com/jakecoffman/cp v0.1.0 h1:sgSYEGUgfwiT447fRjloa2c5b6UyYP+7muR3gQK+Ep0=
@ -32,9 +35,9 @@ golang.org/x/exp v0.0.0-20180710024300-14dda7b62fcd h1:nLIcFw7GiqKXUS7HiChg6OAYW
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-20180618222554-6621de06e1e9 h1:bUF6cVglKSOJhdMhq4QwlWraGO2BHzA0tX5EYnBybJQ=
golang.org/x/mobile v0.0.0-20180618222554-6621de06e1e9/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
}