Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Passing Texture2D pointer to Android Plugin to write on

Discussion in 'Android' started by Twil75, Jan 14, 2013.

  1. Twil75

    Twil75

    Joined:
    Aug 30, 2012
    Posts:
    22
    Hello,

    I would like to call an android plugin function with in parameter a texture2D pointer (tex.GetNativeTexturePtr()).

    I had try to pass GetNativeTexturePtr as Byte array like below but it crash app:

    Function declaration in NativeJavaBridge.cpp ("[B" if for specify a byte array in parameter):
    Code (csharp):
    1. getMyFunc   =  jni_env->GetMethodID(cls_JavaClass, "getMyFunc", "([B)Ljava/lang/String;"); ....
    2. ....
    3. const char* myFuncr(unsigned char *ptr)
    4. {
    5. ...
    6. jstring str_ret     = (jstring)jni_env->CallObjectMethod(JavaClass, getMyFunc, ptr);
    7. ....
    8. }
    And in JavaClass.java:
    Code (csharp):
    1. public String getMyFunc(byte[] ptr)
    2. {
    3. ...i want here write on texture buffer....
    4. }
    5.  
    someone know how to do that ?

    Regards,
    Laurent
     
  2. Twil75

    Twil75

    Joined:
    Aug 30, 2012
    Posts:
    22
    Ok, i found:

    Unity side: tex.GetNativeTextureID()

    Java side:
    glBindTexture(GL_TEXTURE_2D, textureID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, inputBuffer);

    Using a power of 2 texture.
     
  3. lucas.utterback

    lucas.utterback

    Joined:
    Jan 7, 2014
    Posts:
    7
    Do you care to share the code you used to make this work? It sounds like it is exactly what I am needing, but from my understanding, I have been unable to get it to work just yet. Any help would be immensely appreciated!
     
  4. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,602
    tex.GetNativeTextureID() is simply uint
    tex.GetNativeTexturePtr() is also "uint" on gles (it is just casted to pointer)
    so i would advise to pass it as IntPtr for example (or plain int if you want).
     
  5. lucas.utterback

    lucas.utterback

    Joined:
    Jan 7, 2014
    Posts:
    7
    That is what I have running through right now. I know the ptr address is relative to the project and is coming through as "14". With this, I have an output on the Java side that is proper for the pixels (because this is where the decode of the video happens), but on the unity side, its results are always the same(~0.81), so basically not being set. Here is my current code that is not working:

    Unity Side:

    1.) Create the AndroidJavaClass:
    static AndroidJavaClass m_ojc = new AndroidJavaClass ("com.company.app");

    2.1.) Make calls across the bridge to send the pointer:
    OSHookBridge.ModifyImageViewTexturePointer(m_texture.GetNativeTexturePtr());

    2.2.) Here is the call from the AndroidJavaClass
    public static void ModifyImageViewTexturePointer(IntPtr texturePtr)
    {
    m_ojc.CallStatic("ModifyImageViewTexturePointerRemoteCall", texturePtr.ToInt32());
    }


    The Android Java Side:

    3.) The call on the other side of the bridge happens here and is receiving the pointer "14"
    public static void ModifyImageViewTexturePointerRemoteCall(int texturePtr)
    {
    s_decodeThread.ModifyImageViewTexturePointer(texturePtr);
    }

    4.) Which then attempts to use the recommendations from you to update the buffer with another buffer
    public void ModifyImageViewTexturePointer(int texturePtr)
    {
    byte [] bytes = LoadColorTest(); // This is where I create a pattern of colors in pixels
    ByteBuffer buffer = ByteBuffer.wrap(bytes); // Here I wrap it in a ByteBuffer for the GL Calls below

    glBindTexture(GL_TEXTURE_2D, texturePtr);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1920, 1080, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    }

    I truly appreciate your help. If you can see the issue here, I will immensely appreciate it! In the meantime, i will be looking for answers.
     
  6. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,602
    assuming you have everything correct: there is a funky caveat in using textures like this: if you call texture.Apply *system-memory* data will be uploaded to gl (killing yours).
    If not that - bug report with small repro project and drop case number here - i'll take a look
     
  7. lucas.utterback

    lucas.utterback

    Joined:
    Jan 7, 2014
    Posts:
    7
    OK, I spent some time and simplified the code to just have what we are talking about. Had to do this for company secrecy of course. The bug case is 591782. I have it simplified to trying to make a collection of red pixels override the pixels in the texture2d pointer every frame on the android side. It is trying to do so, but no result. I thank you in advance for any info you share.
     
  8. lucas.utterback

    lucas.utterback

    Joined:
    Jan 7, 2014
    Posts:
    7
    So today I have taken a run at a number of other branches to play a video stream, but to no avail. Any idea from the code you saw? I have added boatloads of checks and it is indeed finding the texture from the pointer sent to Java, it "changes" data in there, but still not getting back to Unity with a change.
     
  9. lucas.utterback

    lucas.utterback

    Joined:
    Jan 7, 2014
    Posts:
    7
    I have started a new thread for this discussion here <LINK>
     
  10. toritobravom

    toritobravom

    Joined:
    Jun 4, 2013
    Posts:
    1
    Did you get it?

    I am trying, but it doesn't work for me. I try to send a Texture from Android to Unity, controlled by a button, but it doesn't work for me.

    I pass "GetNativeTextureID()" to Java and then I use the same method as you:

    public void ModifyImageViewTexturePointer(int textureID)
    {
    Bitmap bitmap = convertFileToBitmap("fondo.bmp");
    ByteBuffer buffer = bitmapToBuffer(bitmap);

    glBindTexture(GL_TEXTURE_2D, textureID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1920, 1080, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    }

    Then, I don't know how to continue, I have tried several ways in my Unity script, but I don't get the solution.
     
  11. DattMurgavich

    DattMurgavich

    Joined:
    Jun 11, 2014
    Posts:
    1

    For anyone Googling, etc for this, the following worked for me:


    // Flip texture to account for GL coordinate system
    Bitmap src= ...
    Matrix flip= new Matrix();
    flip.setScale(1.0f, -1.0f);
    Bitmap flipped= Bitmap.createBitmap(src, 0,0,src.getWidth(), src.getHeight(), flip, false);

    // Bind texture and then copy data
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, glTexId);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, flipped, 0);
     
  12. 3D Solar

    3D Solar

    Joined:
    Mar 9, 2013
    Posts:
    3
    Hi we try the same send a Texture from Android to Unity with a push button but its not working can you share the demo code, did you get it?