Android RenderScript 使用 Struct 及其下标的赋值
这是一个关于RenderScript如何使用 Struct 的文章,是学习RenderScript 一个必须要掌握的基础知识点。
大纲
1. 如何定义Struct
2. 如何得到指针长度并循环为指针赋值
3. 整体DEMO代码
如何定义Struct
RenderScript 里面定义结构有两种定义方法,参考如下:
1.
typedef struct tempArray
{
float2 position;
float size;
} Array_T;
Array_T *myArray;
2.
//定义一个struct
typedef struct __attribute__((packed, aligned(4))) tempArray {
int temp;
} Array_T;
Array_T *myArray;
RenderScript 定义Struct 成功后,会自动生成一个java文件,如上面的tempArray名称的结构,会生产这个文件:ScriptField_tempArray,代码如下:
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* 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);
&
补充:移动开发 , Android ,