Android advanced Renderscript --- Andvanced Renderscript (2)

Source: Internet
Author: User

Function

The function is reflected to the script class in project_root/gen/package/name/ScriptC_renderscript_filename. For example, if the following functions are declared in Renderscript code:

Void touch (float x, float y, float pressure, int id ){

If (id> = 10 ){

Return;

}

 

TouchPos [id]. x = x;

TouchPos [id]. y = y;

TouchPressure [id] = pressure;

}

The following code is automatically generated in the corresponding script class:

Public void invoke_touch (float x, float y, float pressure, int id ){

FieldPacker touch_fp = new FieldPacker (16 );

Touch_fp.addF32 (x );

Touch_fp.addF32 (y );

Touch_fp.addF32 (pressure );

Touch_fp.addI32 (id );

Invoke (mExportFuncIdx_touch, touch_fp );

}

Functions cannot return values because the Renderscript system is designed to be asynchronous. When the Android framework code call enters the Renderscript, the call will be put into the queue and executed only when possible. This restriction allows the Renderscript system to execute functions without fixed interruptions and improve efficiency. If the function allows a return value, the call will be blocked until the function is returned.

If you want the Renderscript code to return a value to the Android framework, use the rsSendToClient () function.

Variable

The supported variable types are reflected in the script class of project_root/gen/package/name/ScriptC_renderscript_filename. A set of accessors are generated for each variable. For example, if the following variables are declared in the Renderscript code:

Uint32_t unsignedInteger = 1;

The following code is generated:

Privatelong tables; publicvoid set_unsignedInteger (long v) {region = v; setVar (Region, v);} publiclong get_unsignedInteger () {return Mexico portvar_unsignedinteger;} structure (Structs)

The structure is reflected into a class of its own, which is located in <project_root>/gen/com/example/renderscript/ScriptField_struct_name. This class represents a struct array, and allows the result member to be allocated memory. For example, if the following structure is declared:

Typedef struct Point {

Float2 position;

Float size;

} Point_t;

The following code is generated in ScriptField_Pont.java:

Package com. example. android. rs. hellocompute;

Import android. renderscript .*;

Import android. content. res. Resources;

 

/**

* @ Hide

*/

Public class ScriptField_Point extends android. renderscript. Script. FieldBase {

 

Static public class Item {

Public static final int sizeof = 12;

 

Float2 position;

Float size;

 

Item (){

Position = new Float2 ();

}

}

 

Private Item mItemArray [];

Private FieldPacker mIOBuffer;

Public static Element createElement (RenderScript rs ){

Element. Builder eb = new Element. Builder (rs );

Eb. add (Element. F32_2 (rs), "position ");

Eb. add (Element. F32 (rs), "size ");

Return eb. create ();

}

 

Public ScriptField_Point (RenderScript rs, int count ){

MItemArray = null;

MIOBuffer = null;

MElement = createElement (rs );

Init (rs, count );

}

 

Public ScriptField_Point (RenderScript rs, int count, int usages ){

MItemArray = null;

MIOBuffer = null;

MElement = createElement (rs );

Init (rs, count, usages );

}

 

Private void copyToArray (Item I, int index ){

If (mIOBuffer = null) mIOBuffer = new FieldPacker (Item. sizeof * getType (). getX ()/* count

*/);

MIOBuffer. reset (index * Item. sizeof );

MIOBuffer. addF32 (I. position );

MIOBuffer. addF32 (I. size );

}

 

Public void set (Item I, int index, boolean copyNow ){

If (mItemArray = null) mItemArray = new Item [getType (). getX ()/* count */];

MItemArray [index] = I;

If (copyNow ){

CopyToArray (I, index );

MAllocation. setFromFieldPacker (index, mIOBuffer );

}

}

 

Public Item get (int index ){

If (mItemArray = null) return null;

Return mItemArray [index];

}

 

Public void set_position (int index, Float2 v, boolean copyNow ){

If (mIOBuffer = null) mIOBuffer = new FieldPacker (Item. sizeof * getType (). getX ()/* count */);

If (mItemArray = null) mItemArray = new Item [getType (). getX ()/* count */];

If (mItemArray [index] = null) mItemArray [index] = new Item ();

MItemArray [index]. position = v;

If (copyNow ){

MIOBuffer. reset (index * Item. sizeof );

MIOBuffer. addF32 (v );

FieldPacker fp = new FieldPacker (8 );

Fp. addF32 (v );

MAllocation. setFromFieldPacker (index, 0, fp );

}

}

 

Public void set_size (int index, float v, boolean copyNow ){

If (mIOBuffer = null) mIOBuffer = new FieldPacker (Item. sizeof * getType (). getX ()/* count */);

If (mItemArray = null) mItemArray = new Item [getType (). getX ()/* count */];

If (mItemArray [index] = null) mItemArray [index] = new Item ();

MItemArray [index]. size = v;

If (copyNow ){

MIOBuffer. reset (index * Item. sizeof + 8 );

MIOBuffer. addF32 (v );

FieldPacker fp = new FieldPacker (4 );

Fp. addF32 (v );

MAllocation. setFromFieldPacker (index, 1, fp );

}

}

 

Public Float2 get_position (int index ){

If (mItemArray = null) return null;

Return mItemArray [index]. position;

}

 

Public float get_size (int index ){

If (mItemArray = null) return 0;

Return mItemArray [index]. size;

}

 

Public void copyAll (){

For (int ct = 0; ct <mItemArray. length; ct ++) copyToArray (mItemArray [ct], ct );

MAllocation. setFromFieldPacker (0, mIOBuffer );

}

 

Public void resize (int newSize ){

If (mItemArray! = Null ){

Int oldSize = mItemArray. length;

Int copySize = Math. min (oldSize, newSize );

If (newSize = oldSize) return;

Item ni [] = new Item [newSize];

System. arraycopy (mItemArray, 0, ni, 0, copySize );

MItemArray = ni;

}

MAllocation. resize (newSize );

If (mIOBuffer! = Null) mIOBuffer = new FieldPacker (Item. sizeof * getType (). getX ()/* count */);

}

}

The Code provided for you can easily allocate the memory to the requested structure during the Renderscript runtime and interact with these structures in the memory. Each structure class defines the following methods and constructor:

1. the overloaded constructor can be used to allocate memory. The ScriptField_struct_name (Renderscript rs, int count) constructor allows the count parameter to define the number of struct for memory allocation. ScriptField_struct_name (RenderScript rs, int count, int usages) defines an additional parameter: usages, which is used to specify the memory space that the memory matches. There are four possible Memory Spaces:

A. USAGE_SCRIPT: allocated in the memory space of the script. If no memory space is specified, this is the default memory space.

B. USAGE_GRAPHICS_TEXTURE: allocated in the GPU's texture memory space.

C. USAGE_GRAPHICS_VERTEX: allocate resources in the GPU's Vertex memory space.

D. USAGE_GRAPHICS_CONSTANTS: allocates resources in the fixed memory space of the GPU. The allocated space is used by different program objects.

Multiple Memory Spaces can be specified through the OR operator. The intention is to tell Renderscript that you want to access the data in the specified memory space when running. In the following example, memory is allocated to the custom data type in both the script and Vertex memory space:

ScriptField_Point touchPoints = newScriptField_Point (myRenderscript, 2, Allocation. USAGE_SCRIPT | Allocation. USAGE_GRAPHICS_VERTEX); 2. A static nested class: Item, which allows you to create a struct instance in the form of an object. If you want struct to work better in your Android code, this nested class will play a role. When an object operation is completed, the object can be put into the allocated memory by calling the set (Item I, int index, boolean copyNow) method, set the Item to the expected position in the array. For the latest written memory, Renderscript can be automatically accessed during runtime.

3. Each field in the struct has an accessor method for obtaining and setting values. Each method of these accessors has an index parameter to specify the struct in the array to be read and written. Each setter method also has a copyNow parameter, which specifies whether to synchronize the memory immediately to Renderscript runtime. To synchronize memory that has not been synchronized, call the copyAll () method. Www.2cto.com

4. Description of creating a struct in memory using the createElement () method. This description is used to allocate memory to the memory it represents.

5. The resize () method works with the reallc () method in C language. It allows you to expand the previously allocated memory space and keep the previously created value in the memory space.

6. The copyAll () method synchronizes the memory set at the framework layer to the Renderscript runtime. When calling the set Method of a member, you can specify a Boolean copyNow parameter. If this parameter is true, the memory needs to be synchronized when calling this method. If this parameter is set to false, you can call the copyAll () method to synchronize the unsynchronized memory of all attributes with the Renderscript runtime.

 

By FireOfStar


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.