Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NDK: Does GetByteArrayElements copy data from Java to C++? [closed]

I've read this link about the GetByteArrayElements: FAQ: How do I share raw data with native code? http://developer.android.com/training/articles/perf-jni.html

  1. It said that GetByteArrayElements will return a actual pointers to the raw data in the Dalvik heap. So I can manipulate the raw source in C++ and speed up the process, am I right?

  2. And so, ReleaseByteArrayElements won't copy the data also? Or since GetByteArrayElements return a pointer and I don't even need to release it after manipulation of data just like using GetDirectBufferAddress for FloatBuffer?

  3. If it doesn't have to copy any data from Java to C++, is that possible to pass in and manipulate float array via GetByteArrayElements? Plz answer at NDK: Passing Jfloat array from Java to C++ via GetByteArrayElements?

like image 783
Keith Mak Avatar asked Feb 11 '14 01:02

Keith Mak


1 Answers

  1. Get<Primitive>ArrayElements may or may not copy the data as it sees fit. The isCopy output parameter will tell you whether it has been copied. If data is not copied, then you have obtained a pointer to the data directly in the Dalvik heap. Read more here.

  2. You always need to call the corresponding Release<Primitive>ArrayElements, regardless of whether a copy was made. Copying data back to the VM array isn't the only cleanup that might need to be done, although (according to the JNI documentation already linked) it is feasible that changes can be seen on the Java side before Release... has been called (iff data has not been copied).

  3. I don't believe the VM is going to allow you to make the conversions that would be necessary to do what you are thinking. As I see it, either way you go, you will need to convert a byte array to a float or a float to a byte array in Java, which you cannot accomplish by type casting. The data is going to be copied at some point.

Edit:

What you are wanting to do is possible using ByteBuffer.allocateDirect, ByteBuffer.asFloatBuffer, and GetDirectBufferAddress. In Java, you can use the FloatBuffer to interpret data as floats, and the array will be available directly in native code using GetDirectBufferAddress. I posted an answer to your other question as further explanation.

like image 108
Dave Avatar answered Oct 28 '22 08:10

Dave