Create renderscript
Renderscript expands the range of processor kernels available on the device. This capability is obtained through the method called rsforeach () (or the foreach_root () method at the android framework level. It automatically differentiates the operations of the processor kernel available on the access device. Currently, renderscript can only take advantage of the CPU kernel, but in the future, it will be able to run on other types of processors, such as GPU and DSP.
To implement an renderscript, you must create a code containing renderscript. RS file and use the foreach_root () method at the android framework level to call the file (or use the rsforeach () function to call the file at the renderscript level ). Describes how to create a typical renderscript:
Figure 2. renderscript Overview
The following sections describe how to create a simple renderscript and use it in an Android Application. This example uses the hellocompute renderscript sample provided in the SDK Development Guide.
Create a renderscript File
The renderscript code should be stored in the *. RS and *. RSH files in the <project_root>/src/directory. The Code contains the computing logic and all the necessary variables and pointer declarations. Generally, each *. RS file contains the following items:
1. Compile the instruction Declaration (# pragma Rs java_package_name (package. Name), which declares the *. Java class name corresponding to the renderscript reflection;
2. Compile the directive Declaration (# pragma version (1), which declares the version of the renderscript you want to use (currently only 1 );
3. A main function named root (), which is called by the rsforeach function, allows it to call renderscript code and execute it in a valid multi-kernel. The root () function must return void and receive the following parameters:
A. pointer to the memory used for input and output allocated to renderscript. These two pointers are required in earlier versions of the android3.2 (API Level 13) platform. Android4.0 (API Level 14) can be allocated only one of them.
B. The following parameters are optional, but they must be provided at the same time if they are used:
In addition to necessary memory allocation, a renderscript may need a pointer to user-defined data to execute calculations. It can point to a simple raw data type or a complex struct.
The size of user-defined data.
4. An optional Init () method. This method allows initialization before running the root () method, such as initialization variables. This function runs once and is automatically called before other work in renderscript is executed when renderscript is started.
5. Any variables, pointers, and struct to be used in renderscript code (if necessary, they can be declared in the *. RSH file ).
The following code shows how the mono. RS file is implemented:
# Pragma version (1)
# Pragma Rs java_package_name (COM. example. Android. Rs. hellocompute)
// Multipliers to convert a RGB colors to black and white
Const static float3 gmonomult = {0.299f, 0.587f, 0.114f };
Void root (const uchar4 * v_in, uchar4 * v_out ){
// Unpack a color to a float4
Float4 F4 = rsunpackcolor8888 (* v_in );
// Take the dot product of the color and the Multiplier
Float3 mono = dot (f4.rgb, gmonomult );
// Repack the float to a color
* V_out = rspackcolorto8888 (Mono );
}