I'm trying to figure out the best way to pass large amounts (20Kb - 2MB) of data from C++ to Java on Android. I'm using Djinni to generate as much of the bridge code as possible. The options I'm considering are:
Use AHardwareBuffer - looks like an abstraction around shared memory and is intended primarily for EGL images, and other GLES/Vulkan rendering needs but could be used here. I'm uneasy because of the word "hardware" here as I'm not interfacing with hardware. Perhaps "hardware" just implies this memory can be safely mapped to a device and my use case is also ok? This solution would require hand coding some JNI code.
Have Java provide the buffer - If java passes C++
bytes[]
the data isn’t copied, only the address of the array. This is clunky as Java has to first ask C++ how big the buffer should be before allocating it. This solution could use auto-generated JNI code.Use NewDirectByteBuffer / java.nio.ByteBuffer.allocateDirect - Sources, without citations, claim that this is a bad option as its hard to manage and performs poorly for small amounts of data. It is what Google recommends though. This solution would require hand coding some JNI code.
I'm leaning towards option #2 as it doesn't require handwriting any JNI code and seems "good enough". I'm also assuming that this approach would work well in reverse (i.e. no/low overhead sharing of a buffer from Java to C++). Hoping to get y'alls thoughts here before prototyping/benchmarking as doing so will be a somewhat sizable investment.