I'm in the need to draw/blit/display a native ARGB buffer (allocated/
malloced by my native library) at the Java application side.
Please take into account that the buffer content is changed
dynamically at runtime, using drawing functions of my native library.
I'm searching the most efficient method to realize such blit
operation.

One method could be to use a Bitmap:

- during View constructor I can create a 32bit bitmap: Bitmap mBitmap
= Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
- during View constructor I can create a direct buffer, that will
contain the copy of my native buffer: IntBuffer mDirectBuf =
IntBuffer.allocate(width * height);
- at runtime I call my native drawing function
- when I need to display my native buffer I call a method of my native
library that copies its internal native buffer into the passed Java
direct buffer (previously created):

JNIEXPORT void JNICALL
Java_javax_microedition_AmanithVGJNI_vgGetSurfacePixelsAM(JNIEnv
*jenv, jobject obj, jintArray mDirectBuf) {

    int width = vgGetSurfaceWidthAM();
    int height = vgGetSurfaceHeightAM();
    // get access to the internal malloced 32 ARGB buffer
    unsigned int *pixels = vgGetSurfacePixelsAM();

    (*jenv)->SetIntArrayRegion(jenv, mDirectBuf, 0, width * height,
(const jint *)pixels);
}

then from Java side I copy the content of the updated direct buffer
into the Bitmap, using the copyPixelsFromBuffer method:
mBitmap.copyPixelsFromBuffer(mDirectBuf).
Finally I display the Bitmap on the screen using canvas.drawBitmap()
method.

It seems to me that there are too many buffer copies. I'm wondering if
it's available a better solution, for example:

- during View constructor I can create a 32bit bitmap: Bitmap mBitmap
= Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
- using native code to create a direct buffer linked with the native
(i.e. malloced) buffer:

JNIEXPORT jobject JNICALL
Java_javax_microedition_AmanithVGJNI_vgCreateDirectBufferAM(JNIEnv
*jenv, jobject obj) {

    int width = vgGetSurfaceWidthAM();
    int height = vgGetSurfaceHeightAM();
    // get access to the internal malloced 32 ARGB buffer
    unsigned int *pixels = vgGetSurfacePixelsAM();

    return (*jenv)->NewDirectByteBuffer(jenv, pixels, width * height *
sizeof(unsigned int));
}

- during View constructor I can create the direct buffer, that will
contain the direct link to my native buffer: IntBuffer mDirectBuf =
AmanithVGJNI_vgCreateDirectBufferAM();
- at runtime I call my native drawing function
- when I need to display my native buffer now I copy the content of
the direct buffer into the Bitmap, using the copyPixelsFromBuffer
method: mBitmap.copyPixelsFromBuffer(mDirectBuf).
Finally I display the Bitmap on the screen using canvas.drawBitmap()
method.

This method avoids the use of redundand buffer/copies, but it has the
problem that JavaVM will try to deallocate the direct buffer created
with native vgCreateDirectBufferAM method; unfortunately the native
buffer MUST be deallocated from the native library, and I don't want
Java put his hands on it. Is there a solution?

Any better idea to share with me ?

Many thanks in advance!

-- 
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: http://groups.google.com/group/android-porting

Reply via email to