From 2e6828cf6c983b51e11b190aeaf4a9d452130f6e Mon Sep 17 00:00:00 2001 From: Alexey Pelykh Date: Fri, 20 Apr 2012 19:53:58 +0300 Subject: [PATCH] Fix jnigraphics dependency on Android less than 2.2 --- OsmAnd/jni/osmand/Android.mk | 2 +- OsmAnd/jni/osmand/rendering.cpp | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/OsmAnd/jni/osmand/Android.mk b/OsmAnd/jni/osmand/Android.mk index 049d4a866b..68812d8b18 100644 --- a/OsmAnd/jni/osmand/Android.mk +++ b/OsmAnd/jni/osmand/Android.mk @@ -73,6 +73,6 @@ LOCAL_STATIC_LIBRARIES := \ LOCAL_WHOLE_STATIC_LIBRARIES := skia_neon endif -LOCAL_LDLIBS := -lz -llog -ljnigraphics +LOCAL_LDLIBS := -lz -llog -ldl include $(BUILD_SHARED_LIBRARY) \ No newline at end of file diff --git a/OsmAnd/jni/osmand/rendering.cpp b/OsmAnd/jni/osmand/rendering.cpp index 68312bb9d8..96deeb8aa3 100644 --- a/OsmAnd/jni/osmand/rendering.cpp +++ b/OsmAnd/jni/osmand/rendering.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -717,10 +718,32 @@ extern "C" JNIEXPORT jobject JNICALL Java_net_osmand_plus_render_NativeOsmandLib jobject targetBitmap, jboolean useEnglishNames, jobject renderingRuleSearchRequest, jint defaultColor) { setGlobalJniEnv(ienv); + + // libJniGraphics interface + typedef int (*PTR_AndroidBitmap_getInfo)(JNIEnv*, jobject, AndroidBitmapInfo*); + typedef int (*PTR_AndroidBitmap_lockPixels)(JNIEnv*, jobject, void**); + typedef int (*PTR_AndroidBitmap_unlockPixels)(JNIEnv*, jobject); + static PTR_AndroidBitmap_getInfo dl_AndroidBitmap_getInfo = 0; + static PTR_AndroidBitmap_lockPixels dl_AndroidBitmap_lockPixels = 0; + static PTR_AndroidBitmap_unlockPixels dl_AndroidBitmap_unlockPixels = 0; + static void* module_libjnigraphics = 0; + + if(!module_libjnigraphics) + { + module_libjnigraphics = dlopen("jnigraphics", RTLD_LAZY); + if(!module_libjnigraphics) + { + __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "Failed to load jnigraphics via dlopen, will going to crash"); + return NULL; + } + dl_AndroidBitmap_getInfo = (PTR_AndroidBitmap_getInfo)dlsym(module_libjnigraphics, "AndroidBitmap_getInfo"); + dl_AndroidBitmap_lockPixels = (PTR_AndroidBitmap_lockPixels)dlsym(module_libjnigraphics, "AndroidBitmap_lockPixels"); + dl_AndroidBitmap_unlockPixels = (PTR_AndroidBitmap_unlockPixels)dlsym(module_libjnigraphics, "AndroidBitmap_unlockPixels"); + } // Gain information about bitmap AndroidBitmapInfo bitmapInfo; - if(AndroidBitmap_getInfo(getGlobalJniEnv(), targetBitmap, &bitmapInfo) != ANDROID_BITMAP_RESUT_SUCCESS) + if(dl_AndroidBitmap_getInfo(getGlobalJniEnv(), targetBitmap, &bitmapInfo) != ANDROID_BITMAP_RESUT_SUCCESS) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "Failed to execute AndroidBitmap_getInfo"); __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "Creating SkBitmap in native w:%d h:%d s:%d f:%d!", bitmapInfo.width, bitmapInfo.height, bitmapInfo.stride, bitmapInfo.format); @@ -739,7 +762,7 @@ extern "C" JNIEXPORT jobject JNICALL Java_net_osmand_plus_render_NativeOsmandLib } void* lockedBitmapData = NULL; - if(AndroidBitmap_lockPixels(getGlobalJniEnv(), targetBitmap, &lockedBitmapData) != ANDROID_BITMAP_RESUT_SUCCESS || !lockedBitmapData) { + if(dl_AndroidBitmap_lockPixels(getGlobalJniEnv(), targetBitmap, &lockedBitmapData) != ANDROID_BITMAP_RESUT_SUCCESS || !lockedBitmapData) { __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "Failed to execute AndroidBitmap_lockPixels"); } __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "Locked %d bytes at %p", bitmap->getSize(), lockedBitmapData); @@ -775,7 +798,7 @@ extern "C" JNIEXPORT jobject JNICALL Java_net_osmand_plus_render_NativeOsmandLib pushToJavaRenderingContext(renderingContext, &rc); __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "End Rendering image"); - if(AndroidBitmap_unlockPixels(ienv, targetBitmap) != ANDROID_BITMAP_RESUT_SUCCESS) { + if(dl_AndroidBitmap_unlockPixels(ienv, targetBitmap) != ANDROID_BITMAP_RESUT_SUCCESS) { __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "Failed to execute AndroidBitmap_unlockPixels"); }