问题
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
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?
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?
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?
回答1:
Get<Primitive>ArrayElements
may or may not copy the data as it sees fit. TheisCopy
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.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 beforeRelease...
has been called (iff data has not been copied).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.
来源:https://stackoverflow.com/questions/21691356/ndk-does-getbytearrayelements-copy-data-from-java-to-c