How to pass "processed" value of a variable from RenderScript layer to Java layer in android v4.1 and up?

StackOverflow https://stackoverflow.com/questions/20249035

  •  05-08-2022
  •  | 
  •  

Question

I'm sending two variables to the RenderScript(RS) layer from android application. A function defined in RS layer accepts and adds these two variables and stores the result in a third variable. Now, I would like to get the value of this result_variable in the application space. (Android version 4.1+)

PS: I have tried using global-pointers in RS layer (as suggested by Google) to achieve the same however to no use.

Was it helpful?

Solution

You should be using an Allocation to transfer data between both sides (Java and RenderScript). If you declare an rs_allocation in your .rs file with a simple function:

rs_allocation ai;
void foo() {
    int i = rsGetElementAt_int(ai, 0);
    rsSetElementAt_int(ai, i + 1, 0);
}

You then create and attach an appropriate Allocation from the Java side:

RenderScript mRS = RenderScript.create(mCtx);
ScriptC_s mScript = new ScriptC_s(mRS);
Type t = new Type.Builder(mRS, Element.I32(mRS)).setX(1).create();
Allocation ai1 = Allocation.createTyped(mRS, t);
int i1[] = new int[1];
mScript.set_ai(ai1);
i1[0] = 777;
ai1.copyFrom(i1);  // Copies contents of i1 into ai1 Allocation
mScript.invoke_foo();
ai1.copyTo(i1);  // Copies contents of ai1 back to i1
// i1[0] now has the value 778

Obviously, you can use larger multidimensional buffers as well, but you just access them using 1 dimension from Java. You can also read/write these buffers from kernels (hence the Allocation parameters to forEach functions).

OTHER TIPS

Found answer to this following few Google links and few SO links. I'll try to summarise it (considering you have the basic renderscript-setup in this regard in place) for future-readers seeking answer to this:

  1. Call below API from RenderScript (Note: this requires including "rs_core.rsh" in your rs file)

    rsSendToClient(cmdID, &data, sizeof(data));

  2. In your Java file, define RSMessageHandler and override its run() method and put a switch-case construct on cmdID set in rsSendToClient. Then, link/set this handler with your RenderScript:

    mRenderScript.setMessageHandler(mRSMessageHandler);

    This answer gives more details on that.

I've also put a comment to the above answer on how to use the Data being sent from the RenderScript to Java Layer.

Hope this clears the method to pass the "processed" data from RenderScript to Java layer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top