I have a code which needs to be run from C (uses C library, plus it's performance is crticial and involves tons of loop iterations). The code needs to decode an image and run some operations on it. Final result should be Android/Java's bitmap.
Due to high resolution and a need to display a picture without any scaling this creates performacne issue.
I have pre-allocated bitmaps which I'm reusing, to avoid too many allocations and garbage collections. Currently I've managed to do all stuff required for proof of concept, I've done it like this:
public static void fillNativeBitmap(String src, Bitmap bmp) {
byte[] nativeBytes = getNativeBytesARGB8(src);
bmp.copyPixelsFromBuffer(ByteBuffer.wrap(nativeBytes));
}
JNIEXPORT jbyteArray JNICALL
notImportantForQuestion_getNativeBytesARGB8(
JNIEnv *env,
jclass clazz,
jstring src
) {
char *src_file;
unsigned char *imptr;
src_file = (*env)->GetStringUTFChars(env, src, NULL);
const int total_pixels = xxx;
static unsigned char mptr[xxx];
decodeOneStep(src_file, &imptr); //uses lodepng to decode bitmap file
doStuffWithImage(imptr, mptr);
jbyteArray result = (*env)->NewByteArray(env, total_pixels); //here - unnececeary allocation for GC?
(*env)->SetByteArrayRegion(env, result, 0, total_pixels, mptr);
(*env)->ReleaseStringUTFChars(env, src, src_file);
free(imptr); //I can live with that
return result;
}
I'd like to reuse Java's bitmap buffer, pass it to C and fill it there. However, method for accessing java's array from C takes pointer to "isCopy". Accessing copy of Java's array does not work for me at all, as I don't need java's data from C context, I just need C context to fill Java's data.
What method should I use in this case? How to achieve it in most optimal way? I'm trying to optimise for performance and stability, the later one includes keeping number of allocations as low as possible.
AndroidBitmapfunctions.