I'm trying to use a native library to modify the contents of a byte array (actually uint16 array). I have the array in Unity (C#) and a native library in C++.
I've tried a couple of things, the best I could manage is successfully calling into the native code and being able to return a boolean back to C#. The problem comes when I pass an array and mutate it in C++. No matter what I do, the array appears unmodified in C#.
Here is what I have on the Unity side:
// In Update().
using (AndroidJavaClass processingClass = new AndroidJavaClass(
"com.postprocessing.PostprocessingJniHelper"))
{
if (postprocessingClass == null) {
Debug.LogError("Could not find the postprocessing class.");
return;
}
short[] dataShortIn = ...; // My original data.
short[] dataShortOut = new short[dataShortIn.Length];
Buffer.BlockCopy(dataShortIn, 0, dataShortOut, 0, dataShortIn.Length);
bool success = postprocessingClass.CallStatic<bool>(
"postprocess", TextureSize.x, TextureSize.y,
dataShortIn, dataShortOut);
Debug.Log("Processed successfully: " + success);
}
The Unity project has a postprocessing.aar in Plugins/Android and is enabled for the Android build platform. I have a JNI layer in Java (which is called successfully):
public final class PostprocessingJniHelper {
// Load JNI methods
static {
System.loadLibrary("postprocessing_jni");
}
public static native boolean postprocess(
int width, int height, short[] inData, short[] outData);
private PostprocessingJniHelper() {}
}
The Java code above calls this code in C++.
extern "C" {
JNIEXPORT jboolean JNICALL POSTPROCESSING_JNI_METHOD_HELPER(postprocess)(
JNIEnv *env, jclass thiz, jint width, jint height, jshortArray inData, jshortArray outData) {
jshort *inPtr = env->GetShortArrayElements(inData, nullptr);
jshort *outPtr = env->GetShortArrayElements(outData, nullptr);
jboolean status = false;
if (inPtr != nullptr && outPtr != nullptr) {
status = PostprocessNative(
reinterpret_cast<const uint16_t *>(inPtr), width, height,
reinterpret_cast<uint16_t *>(outPtr));
}
env->ReleaseShortArrayElements(inData, inPtr, JNI_ABORT);
env->ReleaseShortArrayElements(outData, outPtr, 0);
return status;
}
The core C++ function PostprocessNative
seems to also be called successfully (verified by the return value), but all modifications to the data_out are not reflected back in Unity.
bool PostprocessNative(const uint16_t* data_in, int width,
int height, uint16_t* data_out) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
data_out[x + y * width] = 10;
}
}
// Changing the return value here is correctly reflected in C#.
return false;
}
I expect all values of the short[] to be 10, but they are whatever they were before calling JNI.
Is this a correct way to pass a Unity array of shorts into C++ for modification?