Android advanced renderscript-andvanced renderscript (III)

Source: Internet
Author: User

Pointer

The pointer is reflected to <project_root>/GEN/package/name/scriptc_renderscript_filename. You can declare a pointer to a struct or any other type supported by renderscript, but the struct cannot contain pointers or nested arrays. For example, if you declare the following pointer to struct and int32_t:

Typedef struct point {


Float2 position;


Float size;

} Point_t;

 

Point_t * touchpoints;

Int32_t * intpointer;

Then there will be the followingCodeGeneration:

 
PrivateScriptfield_pointMexportvar_touchpoints;
PublicVoidBind_touchpoints(Scriptfield_pointV){
 
Mexportvar_touchpoints=V;
      If   (  v  =  null  )   bindallocation   (  null  ,   mexportvaridx_touchpoints   );  
ElseBindallocation(V.Getallocation(),Mexportvaridx_touchpoints);
 
}
 
 
PublicScriptfield_pointGet_touchpoints(){
 
ReturnMexportvar_touchpoints;
 
}
 
 
PrivateAllocationMexportvar_intpointer;
 
PublicVoidBind_intpointer(AllocationV){
Mexportvar_intpointer=V;
 
If(V=Null)Bindallocation(Null,Mexportvaridx_intpointer);
ElseBindallocation(V,Mexportvaridx_intpointer);
 
}
 
 
PublicAllocationGet_intpointer(){
 
ReturnMexportvar_intpointer;
 
}

A get method and a special method named bind_pointer_name (which replaces the Set () method) are generated. This method allows you to bind the memory allocated in the android vm to the renderscript Runtime (the memory cannot be allocated in the. RS file ).

Memory Allocation API

Applications using renderscriptProgramStill running in Android VM. But in fact, the renderscript code runs in the native system and needs to access the memory allocated in the android VM. To achieve this goal, you must bind the memory allocated by the vm to the renderscript runtime. This process is called binding. It allows renderscript to work seamlessly with the memory requested by it during runtime, but cannot be explicitly allocated. The final result is the same as calling the malloc method in C. Android
The VM can recycle the runtime layer of renderscript like the shared memory. You only need to bind the dynamically allocated memory. In the static allocation, renderscript code is automatically created during compilation.

There is a set of APIs to support such memory allocation systems, which allow the android VM to allocate memory and provide functions similar to those called by malloc. These classes describe how the memory should be allocated and how to execute the allocation processing. To better understand how these classes work, it is advantageous to first think about the simple calling of their related malloc:

Array = (int *) malloc (sizeof (INT) * 10 );

This malloc call can be divided into two parts: 1. Size of the memory unit to be allocated; 2. Number of such memory units to be allocated (10 ). The android framework provides classes that represent malloc.

The element class represents the sizeof (INT) part of the malloc call and encapsulates a memory allocation unit, such as a floating point value or a struct. The type class encapsulates the number of element classes and elements to be allocated (10 in the preceding example ). You can regard the type class as an array of element classes. The allocation class allocates the actual memory based on the given type class and represents the actually allocated memory.

In most cases, you do not need to directly call the APIs for memory allocation. The class at the reflection layer will automatically use these APIs to generate code, and all you need to do for memory allocation is to call the constructor declared in a class at the reflection layer, then, bind the allocated memory to renderscript. Sometimes, you may want to directly use these classes for memory allocation, such as loading a bitmap from a resource or allocating a memory pointer to the original data type. The following table details the three memory management classes:

Android object type

Description

element

An element object represents a memory allocation unit, there are two forms: basic and complex ).

A basic element object contains a single valid renderscript data type data component. Examples of basic element data types include: Float value, float4 vector, or a single RGB-565 color;

A composite element contains a list of basic elements, which are created by struct declared in renderscript code. For example, an allocated memory can contain multiple structures ordered in the memory. Each structure is considered as its element, rather than the data type in the structure.

type

the type class is a memory allocation template, it consists of one element and multiple dimensions. It describes the memory layout (basically an array of element objects ), but it does not allocate memory to the data it describes

A type consisting of five dimensions: X, Y, Z, and low level of detail ), faces (the surface of the cube body ). Within the available memory limit, you can assign any positive integer value to x, y, and z. To allocate a one-dimensional memory space, the X dimension value must be greater than 0, while the Y and Z dimensions must be equal to the X dimension to indicate that the X dimension does not exist. For example, x = 10, y = 1 indicates that the allocated memory is composed of two dimensions, x = 10, and y = 0 indicates a dimension. Both the level of detail and faces are boolean values to indicate whether they exist or do not exist.

Allocation

An allocation will provide the memory for the application based on the memory description represented by the type. The allocated memory can exist in multiple memory spaces at the same time. If a memory space is modified, the memory must be explicitly synchronized so that only the memory is updated among all other existing memories.

Update the allocated data using either of the following methods: type check and non-type check. For a simple array, use the copyfrom function to copy the array from the Android system and copy it to the native layer memory for storage. The non-type check variant allows the Android system to copy struct arrays because it does not support struct. For example, if there is an array of N floating point numbers, the data contained in the float [N] Or byte [N * 4] array can be copied.

 

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.