Android advanced renderscript-andvanced renderscript (4)

Source: Internet
Author: User

Work with memory

Non-static and global variables declared in renderscript will be allocated memory during compilation. In renderscriptCodeYou can directly use these variables to work without allocating memory to them at the android framework layer. The android framework layer can also use the accessors produced in the reflection layer class to access these variables. If these variables are initialized at the renderscript runtime layer, the values at the android framework layer are also used to initialize the corresponding values. If the global variable is marked as const, the set method is not generated.

Note:If you use the renderscript struct containing pointers, such as rs_program_fragment and rs_allocation, you must first obtain an object of the android framework layer class, then, the Set Method of the struct is called to bind the memory to the renderscript runtime. You cannot directly operate these structs at the renderscript runtime layer. This restriction does not apply to user-defined struct containing pointers because they cannot be exported to the first position of the reflective layer class. If your view declares a non-static, global struct containing pointers, a compilation error is generated.

Renderscript also supports pointers, but the memory must be allocated explicitly in the android framework code. When a global pointer is declared in the. RS file, you need to allocate the memory through the appropriate reflection layer class and bind the memory to the native renderscript layer. You can interact with the memory from the android framework layer. Just like the renderscript layer, it provides you with the most appropriate method to flexibly modify variables.

Allocate and bind dynamic memory to renderscript

To allocate dynamic memory, you need to call the constructor of the script. fieldbase class. This is the most common method. Another alternative method is to manually create an allocation object, which requires content such as the original type pointer. At any time, a simple and effective method is to use the script. fieldbase class constructor. After obtaining the memory allocation, call the bind method of the reflected pointer to bind the allocated memory to the renderscript runtime.

In the following example, the original type pointer intpointer and struct pointer touchpoints are allocated with memory and the allocated memory is bound to 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 );

 


...

}

Read/write memory

You can read and write static and dynamically allocated memory at the renderscript runtime and Android framework layer.

The static memory allocated at the renderscript Runtime is restricted by one-way communication. When the renderscript code changes the value of a variable, it does not notify the android framework layer in reverse direction for efficiency considerations. The latest value set at the android framework layer is always returned when the get method is called. However, when the android framework code modifies a variable, this change will be automatically notified to the renderscript runtime, or the synchronization will be completed at the last time. If you want to send data to the android framework layer from the renderscript runtime, you can use the rssendtoclient () function to overcome this restriction.

When you use dynamically allocated memory for work, if you use the pointer associated with it to modify the allocated memory, any changes to the renderscript runtime layer will be reversely transmitted to the android framework layer. When an object is modified at the android framework layer, this change is immediately transmitted to the renderscript runtime layer.

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.