Call renderscript code
You can use _Script_nameCreates an renderscript object and calls renderscript from the android framework code. This class contains a foreach_root () method, which calls the rsforeach () method. You can pass it the same parameter as the renderscript runtime call. This technology allows your Android Application to transfer high-precision mathematical computing to renderscript.
Call the renderscript method at the android framework level:
1. Allocate the memory required by renderscript in your android framework code. For versions earlier than Android (API Level 13), the input and output memory needs to be allocated. For platform Versions later than Android (API Level 14), you only need to allocate one of the memory or both.
2. Create scriptc _Scritp_nameClass.
3. Call the foreach_root () method and pass in the allocated memory, renderscript, and other optional user-defined data. The output memory will contain the output result of renderscript.
The following example is from the hellcompute example. It processes a bitmap and outputs Its black-and-white version. Run the following steps to install the createscript () method. This method calls the renderscript object, executes the mono. RS script, stores the final processing result bitmap in the output memory, and then displays the processed bitmap on the screen:
Package com. example. Android. Rs. hellocompute;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. Graphics. bitmapfactory;
Import Android. Graphics. Bitmap;
Import Android. renderscript. renderscript;
Import Android. renderscript. allocation;
Import Android. widget. imageview;
Public class hellocompute extends activity {
Private bitmap mbitmapin;
Private bitmap mbitmapout;
Private renderscript Mrs;
Private allocation minallocation;
Private allocation moutallocation;
Private scriptc_mono mscript;
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mbitmapin = loadbitmap (R. drawable. data );
Mbitmapout = bitmap. createbitmap (mbitmapin. getwidth (), mbitmapin. getheight (),
Mbitmapin. getconfig ());
Imageview in = (imageview) findviewbyid (R. Id. displayin );
In. setimagebitmap (mbitmapin );
Imageview out = (imageview) findviewbyid (R. Id. displayout );
Out. setimagebitmap (mbitmapout );
Createscript ();
}
Private void createscript (){
Mrs = renderscript. Create (this );
Minallocation = allocation. createfrombitmap (MRS, mbitmapin,
Allocation. mipmapcontrol. mipmap_none,
Allocation. usage_script );
Moutallocation = allocation. createtyped (MRS, minallocation. GetType ());
Mscript = new scriptc_mono (MRS, getresources (), R. Raw. Mono );
Mscript. foreach_root (minallocation, moutallocation );
Moutallocation. copyto (mbitmapout );
}
Private bitmap loadbitmap (INT Resource ){
Final bitmapfactory. Options = new bitmapfactory. Options ();
Options. inpreferredconfig = bitmap. config. argb_8888;
Return bitmapfactory. decoderesource (getresources (), resource, options );
}
}
The following method calls renderscript from another renderscript file:
1. Allocate the memory required by renderscript In the android framework code. For the android3.2 platform and earlier versions, both the input and output memory must be allocated. After the Android platform version, you can allocate the input and output memory as needed.
2. Call the rsforeach () method and pass in the allocated memory and optional user-defined data. The output memory contains the output result of renderscript.
rs_script script;
rs_allocation in_allocation;
rs_allocation out_allocation;
UserData_t data;
...
Rsforeach (script,
In_allocation, out_allocation, & Data, sizeof (data ));
In this example, assume that the script and memory at the android framework layer have been allocated and bound, and userdata_t is a structure declared beforehand. Transmits the pointer and its size of this structure to an rsforeach () method. This is an optional parameter. If your renderscript requires information other than the input memory, you can use this parameter.
Set floating point Precision
You can define the floating point precision required by calculation rules. This definition is useful if you require less precision than the IEEE 754-2008 standard (the standard used by default. You can use the following compilation commands to define the floating point Precision Level of the script:
1. # pragma rs_fp_full (if not specified, this is the default precision level): indicates the floating point precision that the application needs to describe by the IEEE 754-2008 standard.
2. # pragma rs_fp_relaxed: This compilation command can be used for applications that do not require strict compliance with the IEEE 754-2008 standard. for de-norms (de-modulo) in this mode, flush-to-zero (zeroed) is enabled, and round-towards-zero (rounded to zero) is enabled ).
3. # pragma rs_fp_imprecise: This mode is used for applications without strict precision requirements. This mode follows all the rules in the rs_fp_relaxed mode:
Operation Result-0.0 will use + 0.0 instead of return;
Action on INF and Nan is not defined.