Changeset 864


Ignore:
Timestamp:
Aug 27, 2011, 8:29:40 PM (8 years ago)
Author:
sam
Message:

android: keep a pointer on the global Java VM instead of the current
environment, so back-to-jvm techniques can work from any thread.

Location:
trunk/src
Files:
1 added
3 edited
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile.am

    r862 r864  
    1010    world.cpp world.h sample.cpp sample.h sampler.cpp sampler.h \
    1111    text.cpp text.h emitter.cpp emitter.h numeric.h hash.cpp hash.h \
    12     worldentity.cpp worldentity.h image.cpp image.h gradient.cpp gradient.h \
     12    worldentity.cpp worldentity.h gradient.cpp gradient.h \
    1313    platform.cpp platform.h sprite.cpp sprite.h \
    1414    \
     
    2121    shader/shader.cpp shader/shader.h \
    2222    \
     23    image/image.cpp image/image.h \
     24    \
    2325    loldebug.h \
    2426    debug/fps.cpp debug/fps.h debug/sphere.cpp debug/sphere.h \
  • trunk/src/androidapp.cpp

    r863 r864  
    2626namespace lol
    2727{
    28 jobject g_ctx;
    29 JNIEnv *g_env;
     28JavaVM *g_vm;
     29jobject g_activity;
    3030};
    3131
     
    3333JNI_OnLoad(JavaVM* vm, void* reserved)
    3434{
     35    g_vm = vm;
    3536    return JNI_VERSION_1_4;
    3637}
     
    4041{
    4142    env->NewGlobalRef(thiz); /* FIXME: never released! */
    42     g_ctx = thiz;
     43    g_activity = thiz;
    4344}
    4445
     
    4647Java_org_zoy_LolEngine_LolRenderer_nativeInit(JNIEnv* env)
    4748{
    48     /* We cannot use JNI_OnLoad for this because we're in a different
    49      * thread now. */
    50     g_env = env;
    51 
    5249    Log::Info("initialising renderer");
    5350    Ticker::Setup(30.0f);
  • trunk/src/core.h

    r862 r864  
    5050#include "layer.h"
    5151#include "shader/shader.h"
    52 #include "image.h"
     52#include "image/image.h"
    5353
    5454// Managers
  • trunk/src/image/image.cpp

    • Property svn:keywords set to Id
    r863 r864  
    3636
    3737#if defined ANDROID_NDK
    38 extern JNIEnv *g_env;
    39 extern jobject g_ctx;
     38extern JavaVM *g_vm;
     39extern jobject g_activity;
    4040#endif
    4141
     
    127127    data->format = data->img->format->Amask ? FORMAT_RGBA : FORMAT_RGB;
    128128#elif defined ANDROID_NDK
    129     jclass cls = g_env->GetObjectClass(g_ctx);
     129    JNIEnv *env;
     130    jint res = g_vm->GetEnv((void **)&env, JNI_VERSION_1_2);
     131    if (res < 0)
     132    {
     133#if !LOL_RELEASE
     134        Log::Error("could not get JVM environment\n");
     135#endif
     136        exit(1);
     137    }
     138    jclass cls = env->GetObjectClass(g_activity);
    130139    jmethodID mid;
    131140
    132     mid = g_env->GetMethodID(cls, "openImage",
    133                              "(Ljava/lang/String;)Landroid/graphics/Bitmap;");
    134     jstring name = g_env->NewStringUTF(path);
    135     data->bmp = g_env->CallObjectMethod(g_ctx, mid, name);
    136     g_env->DeleteLocalRef(name);
     141    mid = env->GetMethodID(cls, "openImage",
     142                           "(Ljava/lang/String;)Landroid/graphics/Bitmap;");
     143    jstring name = env->NewStringUTF(path);
     144    data->bmp = env->CallObjectMethod(g_activity, mid, name);
     145    env->DeleteLocalRef(name);
    137146    if (!data->bmp)
    138147    {
     
    142151        exit(1);
    143152    }
    144     g_env->NewGlobalRef(data->bmp);
     153    env->NewGlobalRef(data->bmp);
    145154
    146155    /* Get image dimensions */
    147     mid = g_env->GetMethodID(cls, "getWidth", "(Landroid/graphics/Bitmap;)I");
    148     data->size.x = g_env->CallIntMethod(g_ctx, mid, data->bmp);
    149     mid = g_env->GetMethodID(cls, "getHeight", "(Landroid/graphics/Bitmap;)I");
    150     data->size.y = g_env->CallIntMethod(g_ctx, mid, data->bmp);
     156    mid = env->GetMethodID(cls, "getWidth", "(Landroid/graphics/Bitmap;)I");
     157    data->size.x = env->CallIntMethod(g_activity, mid, data->bmp);
     158    mid = env->GetMethodID(cls, "getHeight", "(Landroid/graphics/Bitmap;)I");
     159    data->size.y = env->CallIntMethod(g_activity, mid, data->bmp);
    151160
    152161    /* Get pixels */
    153     data->array = g_env->NewIntArray(data->size.x * data->size.y);
    154     g_env->NewGlobalRef(data->array);
    155     mid = g_env->GetMethodID(cls, "getPixels", "(Landroid/graphics/Bitmap;[I)V");
    156     g_env->CallVoidMethod(g_ctx, mid, data->bmp, data->array);
    157 
    158     data->pixels = g_env->GetIntArrayElements(data->array, 0);
     162    data->array = env->NewIntArray(data->size.x * data->size.y);
     163    env->NewGlobalRef(data->array);
     164    mid = env->GetMethodID(cls, "getPixels", "(Landroid/graphics/Bitmap;[I)V");
     165    env->CallVoidMethod(g_activity, mid, data->bmp, data->array);
     166
     167    data->pixels = env->GetIntArrayElements(data->array, 0);
    159168    for (int n = 0; n < data->size.x * data->size.y; n++)
    160169    {
     
    344353    SDL_FreeSurface(data->img);
    345354#elif defined ANDROID_NDK
    346     jclass cls = g_env->GetObjectClass(g_ctx);
     355    JNIEnv *env;
     356    jint res = g_vm->GetEnv((void **)&env, JNI_VERSION_1_2);
     357    if (res < 0)
     358    {
     359#if !LOL_RELEASE
     360        Log::Error("could not get JVM environment\n");
     361#endif
     362        exit(1);
     363    }
     364    jclass cls = env->GetObjectClass(g_activity);
    347365    jmethodID mid;
    348366
    349     g_env->ReleaseIntArrayElements(data->array, data->pixels, 0);
    350     g_env->DeleteGlobalRef(data->array);
     367    env->ReleaseIntArrayElements(data->array, data->pixels, 0);
     368    env->DeleteGlobalRef(data->array);
    351369
    352370    /* Free image */
    353     mid = g_env->GetMethodID(cls, "closeImage", "(Landroid/graphics/Bitmap;)V");
    354     g_env->CallVoidMethod(g_ctx, mid, data->bmp);
    355     g_env->DeleteGlobalRef(data->bmp);
     371    mid = env->GetMethodID(cls, "closeImage", "(Landroid/graphics/Bitmap;)V");
     372    env->CallVoidMethod(g_activity, mid, data->bmp);
     373    env->DeleteGlobalRef(data->bmp);
    356374#elif defined __CELLOS_LV2__
    357375    free(data->pixels);
  • trunk/src/image/image.h

    • Property svn:keywords set to Id
Note: See TracChangeset for help on using the changeset viewer.