Use common code for Android GC interaction (thanks Hugh)

This commit is contained in:
Joshua Granick
2014-02-04 10:48:50 -08:00
parent 805331e6c7
commit 1d0b5324e4
3 changed files with 54 additions and 76 deletions

View File

@@ -1,8 +1,39 @@
#include <jni.h>
#ifndef _AndroidCommon_h
#define _AndroidCommon_h
#include <jni.h>
#include <pthread.h>
#include <android/log.h>
#include <hx/CFFI.h>
#ifdef __GNUC__
#define JAVA_EXPORT __attribute__ ((visibility("default"))) JNIEXPORT
#else
#define JAVA_EXPORT JNIEXPORT
#endif
struct AutoHaxe
{
int base;
const char *message;
AutoHaxe(const char *inMessage)
{
base = 0;
message = inMessage;
gc_set_top_of_stack(&base,true);
//__android_log_print(ANDROID_LOG_VERBOSE, "NME", "Enter %s %p", message, pthread_self());
}
~AutoHaxe()
{
//__android_log_print(ANDROID_LOG_VERBOSE, "NME", "Leave %s %p", message, pthread_self());
gc_set_top_of_stack(0,true);
}
};
JNIEnv *GetEnv();
jclass FindClass(const char *className);
#endif
#endif

View File

@@ -478,163 +478,121 @@ bool GetAcceleration(double& outX, double& outY, double& outZ) {
extern "C"
{
#ifdef __GNUC__
#define JAVA_EXPORT __attribute__ ((visibility("default"))) JNIEXPORT
#else
#define JAVA_EXPORT JNIEXPORT
#endif
JAVA_EXPORT int JNICALL Java_org_haxe_lime_Lime_onResize(JNIEnv * env, jobject obj, jint width, jint height)
{
env->GetJavaVM(&gJVM);
int top = 0;
gc_set_top_of_stack(&top,true);
__android_log_print(ANDROID_LOG_INFO, "Resize", "%p %d,%d", lime::sFrame, width, height);
AutoHaxe haxe("onResize");
if (lime::sFrame)
lime::sFrame->onResize(width,height);
gc_set_top_of_stack(0,true);
return lime::GetResult();
}
JAVA_EXPORT int JNICALL Java_org_haxe_lime_Lime_onRender(JNIEnv * env, jobject obj)
{
env->GetJavaVM(&gJVM);
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onRender");
//double t0 = lime::GetTimeStamp();
//__android_log_print(ANDROID_LOG_INFO, "lime", "lime onRender: %p", lime::sStage );
if (lime::sStage)
lime::sStage->OnRender();
//__android_log_print(ANDROID_LOG_INFO, "lime", "Haxe Time: %f", lime::GetTimeStamp()-t0);
gc_set_top_of_stack(0,true);
return lime::GetResult();
}
JAVA_EXPORT int JNICALL Java_org_haxe_lime_Lime_onNormalOrientationFound(JNIEnv * env, jobject obj, jint orientation)
{
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onOrientation");
if (lime::sStage)
lime::sStage->OnNormalOrientationFound(orientation);
gc_set_top_of_stack(0,true);
return lime::GetResult();
}
JAVA_EXPORT int JNICALL Java_org_haxe_lime_Lime_onDeviceOrientationUpdate(JNIEnv * env, jobject obj, jint orientation)
{
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onDeviceOrientation");
if (lime::sStage)
lime::sStage->OnDeviceOrientationUpdate(orientation);
gc_set_top_of_stack(0,true);
return lime::GetResult();
}
JAVA_EXPORT int JNICALL Java_org_haxe_lime_Lime_onOrientationUpdate(JNIEnv * env, jobject obj, jfloat x, jfloat y, jfloat z)
{
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onUpdateOrientation");
if (lime::sStage)
lime::sStage->OnOrientationUpdate(x,y,z);
gc_set_top_of_stack(0,true);
return lime::GetResult();
}
JAVA_EXPORT int JNICALL Java_org_haxe_lime_Lime_onAccelerate(JNIEnv * env, jobject obj, jfloat x, jfloat y, jfloat z)
{
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onAcceration");
if (lime::sStage)
lime::sStage->OnAccelerate(x,y,z);
gc_set_top_of_stack(0,true);
return lime::GetResult();
}
JAVA_EXPORT int JNICALL Java_org_haxe_lime_Lime_onTouch(JNIEnv * env, jobject obj, jint type, jfloat x, jfloat y, jint id, jfloat sizeX, jfloat sizeY)
{
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onTouch");
if (lime::sStage)
lime::sStage->OnTouch(type,x,y,id,sizeX,sizeY);
gc_set_top_of_stack(0,true);
return lime::GetResult();
}
JAVA_EXPORT int JNICALL Java_org_haxe_lime_Lime_onTrackball(JNIEnv * env, jobject obj, jfloat dx, jfloat dy)
{
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onTrackball");
if (lime::sStage)
lime::sStage->OnTrackball(dx,dy);
gc_set_top_of_stack(0,true);
return lime::GetResult();
}
JAVA_EXPORT int JNICALL Java_org_haxe_lime_Lime_onKeyChange(JNIEnv * env, jobject obj, int code, bool down)
{
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onKey");
if (lime::sStage)
lime::sStage->OnKey(code,down);
gc_set_top_of_stack(0,true);
return lime::GetResult();
}
JAVA_EXPORT int JNICALL Java_org_haxe_lime_Lime_onJoyChange(JNIEnv * env, jobject obj, int deviceId, int code, bool down)
{
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onJoy");
if (lime::sStage)
lime::sStage->OnJoy(deviceId,code,down);
gc_set_top_of_stack(0,true);
return lime::GetResult();
}
JAVA_EXPORT int JNICALL Java_org_haxe_lime_Lime_onJoyMotion(JNIEnv * env, jobject obj, int deviceId, int axis, float value)
{
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onJoyMotion");
if (lime::sStage)
lime::sStage->OnJoyMotion(deviceId,axis,value);
gc_set_top_of_stack(0,true);
return lime::GetResult();
}
JAVA_EXPORT int JNICALL Java_org_haxe_lime_Lime_onPoll(JNIEnv * env, jobject obj)
{
env->GetJavaVM(&gJVM);
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onPoll");
if (lime::sStage)
lime::sStage->OnPoll();
gc_set_top_of_stack(0,true);
return lime::GetResult();
}
JAVA_EXPORT double JNICALL Java_org_haxe_lime_Lime_getNextWake(JNIEnv * env, jobject obj)
{
env->GetJavaVM(&gJVM);
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onGetNextWake");
if (lime::sStage)
{
double delta = lime::sStage->GetNextWake()-lime::GetTimeStamp();
gc_set_top_of_stack(0,true);
return delta;
}
gc_set_top_of_stack(0,true);
return lime::sStage->GetNextWake()-lime::GetTimeStamp();
return 3600*100000;
}
JAVA_EXPORT int JNICALL Java_org_haxe_lime_Lime_onActivity(JNIEnv * env, jobject obj, int inVal)
{
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onActivity");
if (lime::sStage)
lime::sStage->onActivityEvent(inVal);
gc_set_top_of_stack(0,true);
return lime::GetResult();
}

View File

@@ -1257,28 +1257,21 @@ DEFINE_PRIM(lime_post_ui_callback,1);
extern "C"
{
#ifdef __GNUC__
#define JAVA_EXPORT __attribute__ ((visibility("default"))) JNIEXPORT
#else
#define JAVA_EXPORT JNIEXPORT
#endif
JAVA_EXPORT void JNICALL Java_org_haxe_lime_Lime_onCallback(JNIEnv * env, jobject obj, jlong handle)
{
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("onCallback");
ELOG("lime onCallback %p",(void *)handle);
AutoGCRoot *root = (AutoGCRoot *)handle;
val_call0( root->get() );
delete root;
gc_set_top_of_stack(0,true);
}
JAVA_EXPORT jobject JNICALL Java_org_haxe_lime_Lime_releaseReference(JNIEnv * env, jobject obj, jlong handle)
{
AutoHaxe haxe("releaseReference");
value val = (value)handle;
RemoveJavaHaxeObjectRef(val);
return 0;
@@ -1306,8 +1299,7 @@ value CallHaxe(JNIEnv * env, jobject obj, jlong handle, jstring function, jobjec
JAVA_EXPORT jobject JNICALL Java_org_haxe_lime_Lime_callObjectFunction(JNIEnv * env, jobject obj, jlong handle, jstring function, jobject args)
{
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("callObject");
value result = CallHaxe(env,obj,handle,function,args);
@@ -1323,22 +1315,19 @@ JAVA_EXPORT jobject JNICALL Java_org_haxe_lime_Lime_callObjectFunction(JNIEnv *
ELOG("only string return is supported");
}
//jobject val = JAnonToHaxe(result);
gc_set_top_of_stack(0,true);
return val;
}
JAVA_EXPORT jdouble JNICALL Java_org_haxe_lime_Lime_callNumericFunction(JNIEnv * env, jobject obj, jlong handle, jstring function, jobject args)
{
int top = 0;
gc_set_top_of_stack(&top,true);
AutoHaxe haxe("callNumeric");
value result = CallHaxe(env,obj,handle,function,args);
double val = val_number(result);
gc_set_top_of_stack(0,true);
return val;
}