Reduce native allocation calls amount by reusing previously allocated buffer if possible

This commit is contained in:
Alexey Pelykh 2012-03-01 14:25:12 +02:00
parent b9f1dfc3e9
commit 075642c19f
2 changed files with 19 additions and 7 deletions

View file

@ -694,10 +694,11 @@ void loadJNIRendering(){
JUnidecode_unidecode = globalEnv()->GetStaticMethodID(JUnidecodeClass, "unidecode", "(Ljava/lang/String;)Ljava/lang/String;"); JUnidecode_unidecode = globalEnv()->GetStaticMethodID(JUnidecodeClass, "unidecode", "(Ljava/lang/String;)Ljava/lang/String;");
} }
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void* bitmapData = NULL;
size_t bitmapDataSize = 0;
JNIEXPORT jobject JNICALL Java_net_osmand_plus_render_NativeOsmandLibrary_generateRendering( JNIEnv* ienv, jobject obj, JNIEXPORT jobject JNICALL Java_net_osmand_plus_render_NativeOsmandLibrary_generateRendering( JNIEnv* ienv, jobject obj,
jobject renderingContext, jint searchResult, jint requestedBitmapWidth, jint requestedBitmapHeight, jboolean isTransparent, jobject renderingContext, jint searchResult, jint requestedBitmapWidth, jint requestedBitmapHeight, jboolean isTransparent,
jboolean useEnglishNames, jobject renderingRuleSearchRequest, jint defaultColor) { jboolean useEnglishNames, jobject renderingRuleSearchRequest, jint defaultColor) {
@ -712,11 +713,19 @@ JNIEXPORT jobject JNICALL Java_net_osmand_plus_render_NativeOsmandLibrary_genera
else else
bitmap->setConfig(SkBitmap::kRGB_565_Config, requestedBitmapWidth, requestedBitmapHeight); bitmap->setConfig(SkBitmap::kRGB_565_Config, requestedBitmapWidth, requestedBitmapHeight);
void* bitmapData = malloc(bitmap->getSize()); // re]allocate buffer only if size changed in greated direction?
if(bitmapData != NULL && bitmapDataSize != bitmap->getSize()) {
//TODO: Quite possible increase of speed - [re]allocate buffer only if size changed in greated direction? free(bitmapData);
sprintf(debugMessage, "Allocated %d bytes at %p", bitmap->getSize(), bitmapData); bitmapData = NULL;
__android_log_print(ANDROID_LOG_WARN, "net.osmand", debugMessage); bitmapDataSize = 0;
}
if(bitmapData == NULL && bitmapDataSize == 0) {
bitmapDataSize = bitmap->getSize();
bitmapData = malloc(bitmapDataSize);
sprintf(debugMessage, "Allocated %d bytes at %p", bitmapDataSize, bitmapData);
__android_log_print(ANDROID_LOG_WARN, "net.osmand", debugMessage);
}
bitmap->setPixels(bitmapData); bitmap->setPixels(bitmapData);
@ -773,6 +782,7 @@ JNIEXPORT jobject JNICALL Java_net_osmand_plus_render_NativeOsmandLibrary_genera
#else #else
sprintf(debugMessage, "Native ok (init %d, rendering %d) ", initObjects.getElapsedTime(), rc.nativeOperations.getElapsedTime()); sprintf(debugMessage, "Native ok (init %d, rendering %d) ", initObjects.getElapsedTime(), rc.nativeOperations.getElapsedTime());
#endif #endif
__android_log_print(ANDROID_LOG_WARN, "net.osmand", debugMessage);
// Allocate ctor paramters // Allocate ctor paramters
jobject bitmapBuffer = ienv->NewDirectByteBuffer(bitmapData, bitmap->getSize()); jobject bitmapBuffer = ienv->NewDirectByteBuffer(bitmapData, bitmap->getSize());
@ -788,6 +798,8 @@ JNIEXPORT jobject JNICALL Java_net_osmand_plus_render_NativeOsmandLibrary_releas
jobject results) { jobject results) {
setGlobalEnv(ienv); setGlobalEnv(ienv);
// Not needed
/*
jclass resultClass = ienv->FindClass("net/osmand/plus/render/NativeOsmandLibrary$RenderingGenerationResult"); jclass resultClass = ienv->FindClass("net/osmand/plus/render/NativeOsmandLibrary$RenderingGenerationResult");
if(!resultClass) if(!resultClass)
resultClass = ienv->FindClass("net/osmand/render/NativeOsmandLibrary$RenderingGenerationResult"); resultClass = ienv->FindClass("net/osmand/render/NativeOsmandLibrary$RenderingGenerationResult");
@ -796,6 +808,7 @@ JNIEXPORT jobject JNICALL Java_net_osmand_plus_render_NativeOsmandLibrary_releas
void *buffer = ienv->GetDirectBufferAddress(bitmapBuffer); void *buffer = ienv->GetDirectBufferAddress(bitmapBuffer);
free(buffer); free(buffer);
*/
} }
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -8,7 +8,6 @@ import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.nio.ByteBuffer;
import net.osmand.Algoritms; import net.osmand.Algoritms;
import net.osmand.LogUtil; import net.osmand.LogUtil;