当前位置:操作系统 > 安卓/Android >>

Android高级Renderscript---Andvanced Renderscript(二)

函数

函数被反射到位于project_root/gen/package/name/ScriptC_renderscript_filename的脚本类中。例如,如果在Renderscript代码中声明了以下函数:

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;

}

那么将会在对应的脚本类中自动生产以下代码:

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);

}

函数不能有返回值,因为Renderscript系统被设计成异步的。当Android框架代码调用进入Renderscript时,该调用会被放到队列中,并在可能的时候才会被执行。这种限制允许Renderscript系统执行没有固定中断的功能,并提高效率。如果函数允许有返回值,那么调用将会被阻塞,一直到该函数被返回。

如果想要让Renderscript代码给Android框架返回一个值,请使用rsSendToClient()函数。

变量

被支持的变量类型会反射到位于project_root/gen/package/name/ScriptC_renderscript_filename的脚本类中,同时会给每个变量生成一组访问器方法。例如,如果在Renderscript代码中声明了以下变量:

uint32_t unsignedInteger=1;

那么就会生成下列代码:

privatelong mExportVar_unsignedInteger;publicvoid set_unsignedInteger(long v){    mExportVar_unsignedInteger = v;    setVar(mExportVarIdx_unsignedInteger, v);} publiclong get_unsignedInteger(){    return mExportVar_unsignedInteger;}结构(Structs)

结构会被反射成一个它自己的类,位于<project_root>/gen/com/example/renderscript/ScriptField_struct_name中,该类代表了一个struct数组,并允许给结果成员分配内存。例如,如果声明了以下结构:

typedef struct Point {

   float2 position;

   float size;

} Point_t;

那么在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(inde

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,