DSP

android-Computation(RenderScript)

2019-07-13 15:57发布

RenderScript与二进制文件 系统升级或APP升级 可扩展性 (CPU GPU DSP等)
》RenderScript provides a platform-independent computation engine that operates at the 
native level. Use it to accelerate your apps that require extensive computational horsepower.RenderScript is a framework for running computationally intensive tasks at high performance on Android. 
To begin with RenderScript, there are two main concepts you should understand:
High-performance compute kernels are written in a C99-derived language.
A Java API is used for managing the lifetime of RenderScript resources and controlling kernel execution. 》When developing an Android application that uses RenderScript, you can access its API in one of two ways:
android.renderscript - The APIs in this class package are available on devices running Android 3.0 (API level 11) and higher.android.support.v8.renderscript - The APIs in this package are available through a Support Library, which allows you to use them on devices running Android 2.2 (API level 8) and higher.
》Your RenderScript code is compiled and executed in a compact and well-defined runtime layer. The RenderScript runtime APIs offer support for intensive computation that is portable and automatically scalable to the amount of cores available on a processor. 》Note: The standard C functions in the NDK must be guaranteed to run on a CPU, so RenderScript cannot access these libraries, because RenderScript is designed to run on different types of processors.The example below allocates memory for both a primitive type pointer, intPointer, and a pointer to a struct,touchPoints. It also binds the memory to the RenderScript:
private RenderScript myRenderScript;
private ScriptC_example script;
private Resources resources;

public void init(RenderScript rs, Resources res) {
    myRenderScript = rs;
    resources = res;

    //allocate memory for the struct pointer, calling the constructor
    ScriptField_Point touchPoints = new ScriptField_Point(myRenderScript, 2);

    //Create an element manually and allocate memory for the int pointer
    intPointer = Allocation.createSized(myRenderScript, Element.I32(myRenderScript),2);
    //create an instance of the RenderScript, pointing it to the bytecode resource
    mScript = new ScriptC_example(myRenderScript, resources, R.raw.example);
    //bind the struct and int pointers to the RenderScript
    mScript.bind_touchPoints(touchPoints);
    script.bind_intPointer(intPointer);
   ...
}
Reading and writing to global variables is a straightforward process. You can use the accessor methods at the Android framework level or set them directly in the 
RenderScript code. Keep in mind that any changes that you make in your RenderScript code are not propagated back to the Android framework layer.

For example, given the following struct declared in a file named rsfile.rs:
typedef struct Point {
    int x;
    int y;
} Point_t;
Point_t point;
Once memory is already bound, you do not have to rebind the memory to the RenderScript runtime every time you make a change to a value.