Android RenderScript uses Struct and its underlying value assignment

Source: Internet
Author: User

This is an article about how RenderScript uses Struct. It is a basic knowledge point to learn RenderScript.
Outline
1. How to define Struct
2. How to get the pointer length and assign values to the pointer cyclically
3. Overall DEMO code
How to define Struct
There are two methods to define the structure in RenderScript. refer to the following:
1.
Typedef struct tempArray
{
Float2 position;
Float size;
} Array_T;

Array_T * myArray;

2.
// Define a struct
Typedef struct _ attribute _ (packed, aligned (4) tempArray {
Int temp;
} Array_T;
Array_T * myArray;

 
After RenderScript defines Struct, a java file is automatically generated. The structure of the tempArray name above will produce this file: ScriptField_tempArray. The Code is as follows:
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License ");
* You may not use this file before t in compliance with the License.
* You may obtain a copy of the License
*
* Http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* Distributed under the License is distributed on an "as is" BASIS,
* Without warranties or conditions of any kind, either express or implied.
* See the License for the specific language governing permissions and
* Limitations under the License.
*/

/*
* This file is auto-generated. do not modify!
* The source Renderscript file:/home/terry/workspace/RenderScriptsArray/src/com/xuzhi/renderScriptArray/array. rs
*/
Package com. xuzhi. renderScriptArray;

Import android. renderscript .*;
Import android. content. res. Resources;

/**
* @ Hide
*/
Public class ScriptField_tempArray extends android. renderscript. Script. FieldBase {
Static public class Item {
Public static final int sizeof = 4;

Int temp;

Item (){
}

}

Private Item mItemArray [];
Private FieldPacker mIOBuffer;
Public static Element createElement (RenderScript rs ){
Element. Builder eb = new Element. Builder (rs );
Eb. add (Element. I32 (rs), "temp ");
Return eb. create ();
}

Public ScriptField_tempArray (RenderScript rs, int count ){
MItemArray = null;
MIOBuffer = null;
MElement = createElement (rs );
Init (rs, count );
}

Public ScriptField_tempArray (RenderScript rs, int count, int usages ){
MItemArray = null;
MIOBuffer = null;
MElement = createElement (rs );
Init (rs, count, usages );
}

Private void copyToArrayLocal (Item I, FieldPacker fp ){
Fp. addI32 (I. temp );
}

Private void copyToArray (Item I, int index ){
If (mIOBuffer = null) mIOBuffer = new FieldPacker (Item. sizeof * getType (). getX ()/* count */);
MIOBuffer. reset (index * Item. sizeof );
CopyToArrayLocal (I, mIOBuffer );
}

Public synchronized 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 );
FieldPacker fp = new FieldPacker (Item. sizeof );
CopyToArrayLocal (I, fp );
MAllocation. setFromFieldPacker (index, fp );
}

}

Public synchronized Item get (int index ){
If (mItemArray = null) return null;
Return mItemArray [index];
}

Public synchronized void set_temp (int index, int 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]. temp = v;
If (copyNow ){
MIOBuffer. reset (index * Item. sizeof );
MIOBuffer. addI32 (v );
FieldPacker fp = new FieldPacker (4 );
Fp. addI32 (v );
MAllocation. setFromFieldPacker (index, 0, fp );
}

}

Public synchronized int get_temp (int index ){
If (mItemArray = null) return 0;
Return mItemArray [index]. temp;
}

Public synchronized void copyAll (){
For (int ct = 0; ct <mItemArray. length; ct ++) copyToArray (mItemArray [ct], ct );
MAllocation. setFromFieldPacker (0, mIOBuffer );
}

Public synchronized 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 generated code provides you with the function of allocating memory and operating similar arrays.
 
How to get the pointer length and assign values to the pointer cyclically
RenderScript has two functions specifically used to obtain the pointer length:
RsGetAllocation: returns a pointer to an allocated address.
RsAllocationGetDimX: gets the length of the returned pointer.
By combining these two functions, we can return the pointer length. The Code is as follows:
Const int size = rsAllocationGetDimX (rsGetAllocation (myArray ));
 
Assign a value to the internal variable of the pointer after obtaining the length. The Code is as follows:
For (int I = 0; I <size; I ++ ){
Array-> temp = I; // cyclic value assignment

RsDebug ("current value is ===>", array-> temp); // print the current value
// Point to the next pointer
Array ++;

}

 
Overall DEMO code
This DEMO does not have any interface. It just shows how to use struct and print the pointer subscript value. The rs File Code involved is as follows:
# Pragma version (1)
# Pragma rs java_package_name (com. xuzhi. renderScriptArray)

# Include "rs_graphics.rsh"

Static int initialized = 0;

// Define a struct
Typedef struct _ attribute _ (packed, aligned (4) tempArray {
Int temp;
} Array_T;
Array_T * myArray;


Static void initArray (){
Array_T * array = myArray;
// Get the struct Length
// 1. Return a pointer with the allocated address
// 2. Get the length of the returned pointer
Const int size = rsAllocationGetDimX (rsGetAllocation (myArray ));
For (int I = 0; I <size; I ++ ){
Array-> temp = I; // cyclic value assignment
RsDebug ("current value is ===>", array-> temp); // print the current value
// Point to the next pointer
Array ++;
}
}

Int root (){
RsgClearColor (0.0f, 1.0f, 0.0f, 1.0f );
If (initialized = 0 ){
InitArray ();
Initialized = 1;
}

Return 16;

}

 
The java code is as follows:
Package com. xuzhi. renderScriptArray;

Import android. app. Activity;
Import android. content. Context;
Import android. content. res. Resources;
Import android. OS. Bundle;
Import android. renderscript. Allocation;
Import android. renderscript. RSSurfaceView;
Import android. renderscript. RenderScriptGL;

Public class RenderScriptsArrayActivity extends Activity {

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (new renderScriptView (this ));
}


Public class renderScriptRS {

Private ScriptC_array mScript;

Private ScriptField_tempArray array;

RenderScriptGL mRS;

Public renderScriptRS (RenderScriptGL rs, Resources resource ){
// TODO Auto-generated constructor stub
MRS = rs;
// Initialize struct and specify the number of Subscripts for it
Array = new ScriptField_tempArray (mRS, 10, Allocation. USAGE_SCRIPT | Allocation. USAGE_GRAPHICS_VERTEX );

// Implement CcriptC
MScript = new ScriptC_array (mRS, resource, R. raw. array );
// Bind struct
MScript. bind_myArray (array );
// Bind the script
MRS. bindRootScript (mScript );

}
}



Public class renderScriptView extends RSSurfaceView {
Private RenderScriptGL mRS;
Private renderScriptRS mRender;

Public renderScriptView (Context context ){
Super (context );
// TODO Auto-generated constructor stub
}

@ Override
Protected void onAttachedToWindow (){
Super. onAttachedToWindow ();
Android. util. Log. e ("rs", "onAttachedToWindow ");
If (mRS = null ){
RenderScriptGL. SurfaceConfig SC = new RenderScriptGL. SurfaceConfig ();
MRS = createRenderScriptGL (SC );
MRender = new renderScriptRS (mRS, getResources ());
}
}

@ Override
Protected void onDetachedFromWindow (){
Super. onDetachedFromWindow ();
If (mRS! = Null ){
MRS = null;
DestroyRenderScriptGL ();
}
}

}
}

 
The comments are added, and the most important four sections of code are marked in red. Note that.
Running result:
03-09 15:47:50. 492: D/RenderScript (2298): current value is ==> 0 0x0
03-09 15:47:50. 507: D/RenderScript (2298): current value is ==> 1 0x1
03-09 15:47:50. 507: D/RenderScript (2298): current value is ==> 2 0x2
03-09 15:47:50. 507: D/RenderScript (2298): current value is ==> 3 0x3
03-09 15:47:50. 507: D/RenderScript (2298): current value is ==> 4 0x4
03-09 15:47:50. 507: D/RenderScript (2298): current value is ==> 5 0x5
03-09 15:47:50. 507: D/RenderScript (2298): current value is ==> 6 0x6
03-09 15:47:50. 507: D/RenderScript (2298): current value is ==> 7 0x7
03-09 15:47:50. 507: D/RenderScript (2298): current value is ==> 8 0x8
03-09 15:47:50. 507: D/RenderScript (2298): current value is ==> 9 0x9


From Terry _ Dragon
 

 

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.