Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing byte array from Unity to Android (C++) for modification

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?

like image 443
dustymax Avatar asked Oct 18 '19 08:10

dustymax


2 Answers

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;
    }
}
like image 159
Hamid Yusifli Avatar answered Nov 18 '22 12:11

Hamid Yusifli


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
like image 42
Michael Avatar answered Nov 18 '22 12:11

Michael