6

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?

dustymax
  • 330
  • 3
  • 11
  • Probably a copy/paste typo, but your `PostprocessNative()` takes **data_out** and modifies **packed_out**. – Alex Cohn Oct 19 '19 at 12:24
  • Make sure that **width** and **height** are passed correctly all the way from Unity to C++. I would add some `__android_log_print()` on the C++ side to make sure the loop is actually executing. – Alex Cohn Oct 19 '19 at 12:27
  • @AlexCohn Yup, it was a typo. Fixed now, thanks! – dustymax Oct 20 '19 at 14:17
  • @AlexCohn I tried hard-coding the image dimensions and got the same result. I know the image is passed in because if I iterate over larger bounds in C++ I get a segfault. – dustymax Oct 20 '19 at 14:18
  • While your question is focused on the Java->C++->Java transition, have you considered that maybe the Java->Unity transition could be at fault? Is it an option to bypass the JVM and just integrate with the native code directly? – Botje Oct 21 '19 at 07:56
  • @Zheden `"You can store the data in a byte[]. This allows very fast access from managed code. On the native side, however, you're not guaranteed to be able to access the data without having to copy it."` See [perf-jni](https://developer.android.com/training/articles/perf-jni.html) and my answer [here](https://stackoverflow.com/questions/41825305/how-to-debug-segv-accerr/41885866#41885866) about the stack and heap. – Jon Goodwin Oct 21 '19 at 20:20
  • Unlike regular `byte[] buffers`,in `ByteBuffer` the storage is *not allocated* on the **managed heap**, and can **always** be accessed *directly* from *native code*. – Jon Goodwin Oct 21 '19 at 20:49

2 Answers2

3

Firstly, you did not provide any information about your configuration. What is your scripting backend: Mono or IL2CPP?

Secondly, why don't you call C++ code directly from C#?

1) Go to: [File] > [build Settings] > [Player Settings] > [Player] and turn on [Allow 'unsafe' Code] property.

2) After building the library, copy the output .so file(s) into the Assets/Plugins/Android directory in your Unity project.

enter image description here

C# code:

using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;
using System;


public class CallNativeCode : MonoBehaviour
{
    [DllImport("NativeCode")]
    unsafe private static extern bool PostprocessNative(int width, int height, short* data_out);

    public short[] dataShortOut;
    public Text TxtOut;

    public void Update()
    {
        dataShortOut = new short[100];
        bool o = true;

        unsafe
        {
            fixed (short* ptr = dataShortOut)
            {
                o = PostprocessNative(10, 10, ptr);
            }
        }

        TxtOut.text = "Function out:  " + o + " Array element 0: " + dataShortOut[0];
    }
}

C++ code:

#include <stdint.h>
#include <android/log.h>

#define LOG(...) __android_log_print(ANDROID_LOG_VERBOSE, "HamidYusifli", __VA_ARGS__)


extern "C"
{
    bool PostprocessNative(int width, int height, short *data_out)
    {
        for (int y = 0; y < height; ++y)
        {
            for (int x = 0; x < width; ++x)
            {
                data_out[x + y * width] = 10;
            }
        }

        LOG("Log: %d", data_out[0]);

        // Changing the return value here is correctly reflected in C#.
        return false;
    }
}
Hamid Yusifli
  • 9,688
  • 2
  • 24
  • 48
1

GetShortArrayElements may pin the Java array in memory, or return a copy of the data. So you're supposed to call ReleaseShortArrayElements when you're done using the pointers.

env->ReleaseShortArrayElements(inData, inPtr, JNI_ABORT); // free the buffer without copying back the possible changes
env->ReleaseShortArrayElements(outData, outPtr, 0);       // copy back the content and free the buffer
Michael
  • 57,169
  • 9
  • 80
  • 125