3 RenderScript运行时层与反射层
3.1 RenderScript运行时层
RenderScript运行时层是指.rs代码运行时所在的层级。当对安卓项目进行编译的时候,.rs或者.rsh中编写的代码都会被llvm编译器编译成字节码。当该安卓应用在设备上运行的时候,这些字节码将会被设备上另外一个llvm编译(just-in-time)成机器码。这些机器码是针对该设备进行了优化的,且缓存在设备上,等到下次被应用的时候就不需要重新编译了,以加快速度。虽然RenderScript运行时层很像NDK,但是由于NDK中的C函数只针对CPU设计,与RenderScript还能够在GPU和DSP上运行的目标不同,因此在RenderScript中不能使用NDK中的C函数。
RenderScript运行时库的特性包括:
3.2 反射层
反射层由安卓编译工具基于开发者编写的.rs/.rsh文件自动生成的,反射层的作用就是给Android framework层提供对RenderScript运行时层操作的Java接口,包括内存分配、计算任务启动、数据交互等。
每一个.rs文件都会被映射成继承于ScriptC的类:ScriptC_RenderScript_filename,该类被生成在gen目录下与.rs文件相同的包下。该类就是.rs文件的Java版本。该类主要包含.rs中的如下内容:
Struct映射的详细解释
反射层生成的Struct主要包括:
可以使用或caozuofu.html" target="_blank">操作符来指定在多个内存区分配该内存,这样做表示向RenderScript表明:我想在多个内存区来访问该数据。
代码举例:
.rs文件,文件名:script.rs
class="code_img_closed" src="/Upload/Images/2016081905/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('b6b9fa34-3eb5-4eb3-9217-ae35db5bbca1',event)" src="/Upload/Images/2016081905/2B1B950FA3DF188F.gif" alt="" />#pragma version(1) #pragma rs java_package_name(com.example.renderscripttest) #pragma rs_fp_relaxed uint32_t width; uint32_t height; rs_allocation inBitmap; rs_allocation rgbBitmap; rs_allocation yuvBitmap; //multipliers to convert a RGB colors to black and white const static float3 gMonoMult = {0.299f, 0.587f, 0.114f}; typedef struct Point_2D{ int x; int y; }Point; static Point *spPoint; static Point sPoint; Point point; Point *pPoint; //google sample void root(const uchar4 *v_in, uchar4 *v_out) { //unpack a color to a float4 float4 f4 = rsUnpackColor8888(*v_in); //take the dot product of the color and the multiplier float3 mono = dot(f4.rgb, gMonoMult); //repack the float to a color *v_out = rsPackColorTo8888(mono); } void __attribute((kernel)) setPoint(const uint2 in, uint32_t x, uint32_t y){ rsDebug("lyh", point.x); point.x = 9; //struct is used point.y = 12; rsSendToClient(0, &point, 1); rsDebug("willhua", point.x); } uchar4 __attribute__((kernel)) halveBitmap(uchar4 in){ uchar4 out = in; out.r = in.r / 2; out.r = in.r / 2; out.r = in.r / 2; return out; } uchar4 __attribute__((kernel)) averageBitmap(uchar4 in, uint32_t x, uint32_t y){ uchar4 out = in; uchar4 left = in; uchar4 top = in; uchar4 right = in; uchar4 bottom = in; if(x - 1 > -1){ //access other element left = rsGetElementAt_uchar4(inBitmap, x - 1, y); } if(y - 1 > -1){ top = rsGetElementAt_uchar4(inBitmap, x , y - 1); } if(x + 1 < width){ right = rsGetElementAt_uchar4(inBitmap, x + 1, y); } if(y + 1 < height){ bottom = rsGetElementAt_uchar4(inBitmap, x, y + 1); } out.r = (left.r + top.r + right.r + bottom.r) / 4; out.g = (left.g + top.g + right.g + bottom.g) / 4; out.b = (left.b + top.b + right.b + bottom.b) / 4; return out; }View Code
反射层生成的ScriptC子类:
/* * Copyright (C) 2011-2014 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: G:\\Files\\EclipseWorkSpace\\RenderScriptTest\\src\\com\\example\\renderscripttest\\script.rs */ package com.example.renderscripttest; import android.support.v8.renderscript.*; import android.content.res.Resources; /** * @hide */ public class ScriptC_script extends ScriptC { private static final String __rs_resource_name = "script"; // Constructor public ScriptC_script(RenderScript rs) { this(rs, rs.getApplicationContext().getResources(), rs.getApplicationContext().getResources().getIdentifier( __rs_resource_name, "raw", rs.getApplicationContext().getPackageName())); } public ScriptC_script(RenderScript rs, Resources resources, int id) { super(rs, resources, id); __U32 = Element.U32(rs); __ALLOCATION = Element.ALLOCATION(rs); __ScriptField_Point_2D = ScriptField_Point_2D.createElement(rs); __U8_4 = Element.U8_4(rs); __U32_2 = Element.U32_2(rs); } private Element __ALLOCATION; private Element __ScriptField_Point_2D; private Element __U32; private Element __U32_2; private Element __U8_4; private FieldPacker __rs_fp_ALLOCATION; private FieldPacker __rs_fp_ScriptField_Point_2D; private FieldPacker __rs_fp_U32; private final static int mExportVarIdx_width = 0; private long mExportVar_width; public synchronized void set_width(long v) { if (__rs_fp_U32!= null) { __rs_fp_U32.reset(); } else { __rs_fp_U32 = new FieldPacker(4); } __rs_fp_U32.addU32(v); setVar(mExportVarIdx_width, __rs_fp_U32); mExportVar_width = v; } public long get_width() { return mExportVar_width; } public Script.FieldID getFieldID_width() { return createFieldID(mExportVarIdx_width, null); } private final static int mExportVarIdx_height = 1; private long mExportVar_height; public synchronized void set_height(long v) { if (__rs_fp_U32!= null) { __rs_fp_U32.reset(); } else { __rs_fp_U32 = new FieldPacker(4); } __rs_fp_U32.addU32(v); setVar(mExportVarIdx_height, __rs_fp_U32); mExportVar_height = v; } public long get_height() { return mExportVar_height; } public Script.FieldID getFieldID_height() { return createFieldID(mExportVarIdx_height, null); } private final static int mExportVarIdx_inBitmap = 2; private Allocation mExportVar_inBitmap; public synchronized void set_inBitmap(Allocation v) { setVar(mExportVarIdx_inBitmap, v); mExportVar_inBitmap = v; } public Allocation get_inBitmap() { return mExportVar_inBitmap; } public Script.FieldID getFieldID_inBitmap() { return createFieldID(mExportVarIdx_inBitmap, null); } private final static int mExportVarIdx_rgbBitmap = 3; private Allocation mExportVar_rgbBitmap; public synchronized void set_rgbBitmap(Allocation v) { setVar(mExportVarIdx_rgbBitmap, v); mExportVar_rgbBitmap = v; } public Allocation get_rgbBitmap() { return mExportVar_rgbBitmap; } public Script.FieldID getFieldID_rgbBitmap() { return createFieldID(mExportVarIdx_rgbBitmap, null); } private final static int mExportVarIdx_yuvBitmap = 4; private Allocation mExportVar_yuvBitmap; public synchronized void set_yuvBitmap(Allocation v) { setVar(mExportVarIdx_yuvBitmap, v); mExportVar_yuvBitmap = v; } public Allocation get_yuvBitmap() { return mExportVar_yuvBitmap; } public Script.FieldID getFieldID_yuvBitmap() { return createFieldID(mExportVarIdx_yuvBitmap, null); } private final static int mExportVarIdx_point = 5; private ScriptField_Point_2D.Item mExportVar_point; public synchronized void set_point(ScriptField_Point_2D.Item v) { mExportVar_point = v; FieldPacker fp = new FieldPacker(8); fp.addI32(v.x); fp.addI32(v.y); int []__dimArr = new int[1]; __dimArr[0] = 1; setVar(mExportVarIdx_point, fp, __ScriptField_Point_2D, __dimArr); } public ScriptField_Point_2D.Item get_point() { return mExportVar_point; } public Script.FieldID getFieldID_point() { return createFieldID(mExportVarIdx_point, null); } private final static int mExportVarIdx_pPoint = 6; private ScriptField_Point_2D mExportVar_pPoint; public void bind_pPoint(ScriptField_Point_2D v) { mExportVar_pPoint = v; if (v == null) bindAllocation(null, mExportVarIdx_pPoint); else bindAllocation(v.getAllocation(), mExportVarIdx_pPoint); } public ScriptField_Point_2D get_pPoint() { return mExportVar_pPoint; } private final static int mExportForEachIdx_root = 0; public Script.KernelID getKernelID_root() { return createKernelID(mExportForEachIdx_root, 3, null, null); } public void forEach_root(Allocation ain, Allocation aout) { forEach_root(ain, aout, null); } public void forEach_root(Allocation ain, Allocation aout, Script.LaunchOptions sc) { // check ain if (!ain.getType().getElement().isCompatible(__U8_4)) { throw new RSRuntimeException("Type mismatch with U8_4!"); } // check aout if (!aout.getType().getElement().isCompatible(__U8_4)) { throw new RSRuntimeException("Type mismatch with U8_4!"); } Type t0, t1; // Verify dimensions t0 = ain.getType(); t1 = aout.getType(); if ((t0.getCount() != t1.getCount()) || (t0.getX() != t1.getX()) || (t0.getY() != t1.getY()) || (t0.getZ() != t1.getZ()) || (t0.hasFaces() != t1.hasFaces()) || (t0.hasMipmaps() != t1.hasMipmaps())) { throw new RSRuntimeException("Dimension mismatch between parameters ain and aout!"); } forEach(mExportForEachIdx_root, ain, aout, null, sc); } private final static int mExportForEachIdx_setPoint = 1; public Script.KernelID getKernelID_setPoint() { return createKernelID(mExportForEachIdx_setPoint, 57, null, null); } public void forEach_setPoint(Allocation ain) { forEach_setPoint(ain, null); } public void forEach_setPoint(Allocation ain, Script.LaunchOptions sc) { // check ain if (!ain.getType().getElement().isCompatible(__U32_2)) { throw new RSRuntimeException("Type mismatch with U32_2!"); } forEach(mExportForEachIdx_setPoint, ain, null, null, sc); } private final static int mExportForEachIdx_halveBitmap = 2; public Script.KernelID getKernelID_halveBitmap() { return createKernelID(mExportForEachIdx_halveBitmap, 35, null, null); } public void forEach_halveBitmap(Allocation ain, Allocation aout) { forEach_halveBitmap(ain, aout, null); } public void forEach_halveBitmap(Allocation ain, Allocation aout, Script.LaunchOptions sc) { // check ain if (!ain.getType().getElement().isCompatible(__U8_4)) { throw new RSRuntimeException("Type mismatch with U8_4!"); } // check aout if (!aout.getType().getElement().isCompatible(__U8_4)) { throw new RSRuntimeException("Type mismatch with U8_4!"); } Type t0, t1; // Verify dimensions t0 = ain.getType(); t1 = aout.getType(); if ((t0.getCount() != t1.getCount()) || (t0.getX() != t1.getX()) || (t0.getY() != t1.getY()) || (t0.getZ() != t1.getZ()) || (t0.hasFaces() != t1.hasFaces()) || (t0.hasMipmaps() != t1.hasMipmaps())) { throw new RSRuntimeException("Dimension mismatch between parameters ain and aout!"); } forEach(mExportForEachIdx_halveBitmap, ain, aout, null, sc); } private final static int mExportForEachIdx_averageBitmap = 3; public Script.KernelID getKernelID_averageBitmap() { return createKernelID(mExportForEachIdx_averageBitmap, 59, null, null); } public void forEach_averageBitmap(Allocation ain, Allocation aout) { forEach_averageBitmap(ain, aout, null); } public void forEach_averageBitmap(Allocation ain, Allocation aout, Script.LaunchOptions sc) { // check ain if (!ain.getType().getElement().isCompatible(__U8_4)) { throw new RSRuntimeException("Type mismatch with U8_4!"); } // check aout if (!aout.getType().getElement().isCompatible(__U8_4)) { throw new RSRuntimeException("Type mismatch with U8_4!"); } Type t0, t1; // Verify dimensions t0 = ain.getType(); t1 = aout.getType(); if ((t0.getCount() != t1.getCount()) || (t0.getX() != t1.getX()) || (t0.getY() != t1.getY()) || (t0.getZ() != t1.getZ()) || (t0.hasFaces() != t1.hasFaces()) || (t0.hasMipmaps() != t1.hasMipmaps())) { throw new RSRuntimeException("Dimension mismatch between parameters ain and aout!"); } forEach(mExportForEachIdx_averageBitmap, ain, aout, null, sc); } }View Code
反射层生成的struct,Point_2D, 对应的类:
/* * Copyright (C) 2011-2014 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: G:\\Files\\EclipseWorkSpace\\RenderScriptTest\\src\\com\\example\\renderscripttest\\script.rs */ package com.example.renderscripttest; import android.support.v8.renderscript.*; import android.content.res.Resources; /** * @hide */ public class ScriptField_Point_2D extends android.support.v8.renderscript.Script.FieldBase { static public class Item { public static final int sizeof = 8; int x; int y; Item() { } } private Item mItemArray[]; private FieldPacker mIOBuffer; private static java.lang.ref.WeakReference<Element> mElementCache = new java.lang.ref.WeakReference<Element>(null); public static Element createElement(RenderScript rs) { Element.Builder eb = new Element.Builder(rs); eb.add(Element.I32(rs), "x"); eb.add(Element.I32(rs), "y"); return eb.create(); } private ScriptField_Point_2D(RenderScript rs) { mItemArray = null; mIOBuffer = null; mElement = createElement(rs); } public ScriptField_Point_2D(RenderScript rs, int count) { mItemArray = null; mIOBuffer = null; mElement = createElement(rs); init(rs, count); } public ScriptField_Point_2D(RenderScript rs, int count, int usages) { mItemArray = null; mIOBuffer = null; mElement = createElement(rs); init(rs, count, usages); } public static ScriptField_Point_2D create1D(RenderScript rs, int dimX, int usages) { ScriptField_Point_2D obj = new ScriptField_Point_2D(rs); obj.mAllocation = Allocation.createSized(rs, obj.mElement, dimX, usages); return obj; } public static ScriptField_Point_2D create1D(RenderScript rs, int dimX) { return create1D(rs, dimX, Allocation.USAGE_SCRIPT); } public static ScriptField_Point_2D create2D(RenderScript rs, int dimX, int dimY) { return create2D(rs, dimX, dimY, Allocation.USAGE_SCRIPT); } public static ScriptField_Point_2D create2D(RenderScript rs, int dimX, int dimY, int usages) { ScriptField_Point_2D obj = new ScriptField_Point_2D(rs); Type.Builder b = new Type.Builder(rs, obj.mElement); b.setX(dimX); b.setY(dimY); Type t = b.create(); obj.mAllocation = Allocation.createTyped(rs, t, usages); return obj; } public static Type.Builder createTypeBuilder(RenderScript rs) { Element e = createElement(rs); return new Type.Builder(rs, e); } public static ScriptField_Point_2D createCustom(RenderScript rs, Type.Builder tb, int usages) { ScriptField_Point_2D obj = new ScriptField_Point_2D(rs); Type t = tb.create(); if (t.getElement() != obj.mElement) { throw new RSIllegalArgumentException("Type.Builder did not match expected element type."); } obj.mAllocation = Allocation.createTyped(rs, t, usages); return obj; } private void copyToArrayLocal(Item i, FieldPacker fp) { fp.addI32(i.x); fp.addI32(i.y); } private void copyToArray(Item i, int index) { if (mIOBuffer == null) mIOBuffer = new FieldPacker(mElement.getBytesSize() * getType().getX()/* count */); mIOBuffer.reset(index * mElement.getBytesSize()); 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(mElement.getBytesSize()); 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_x(int index, int v, boolean copyNow) { if (mIOBuffer == null) mIOBuffer = new FieldPacker(mElement.getBytesSize() * getType().getX()/* count */); if (mItemArray == null) mItemArray = new Item[getType().getX() /* count */]; if (mItemArray[index] == null) mItemArray[index] = new Item(); mItemArray[index].x = v; if (copyNow) { mIOBuffer.reset(index * mElement.getBytesSize()); mIOBuffer.addI32(v); FieldPacker fp = new FieldPacker(4); fp.addI32(v); mAllocation.setFromFieldPacker(index, 0, fp); } } public synchronized void set_y(int index, int v, boolean copyNow) { if (mIOBuffer == null) mIOBuffer = new FieldPacker(mElement.getBytesSize() * getType().getX()/* count */); if (mItemArray == null) mItemArray = new Item[getType().getX() /* count */]; if (mItemArray[index] == null) mItemArray[index] = new Item(); mItemArray[index].y = v; if (copyNow) { mIOBuffer.reset(index * mElement.getBytesSize() + 4); mIOBuffer.addI32(v); FieldPacker fp = new FieldPacker(4); fp.addI32(v); mAllocation.setFromFieldPacker(index, 1, fp); } } public synchronized int get_x(int index) { if (mItemArray == null) return 0; return mItemArray[index].x; } public synchronized int get_y(int index) { if (mItemArray == null) return 0; return mItemArray[index].y; } public synchronized void copyAll() { for (int ct = 0; ct < mItemArray.length; ct++) copyToArray(mItemArray[ct], ct); mAllocation.setFromFieldPacker(0, mIOBuffer); } }View Code
4 内存
我们已经知道,应用自身运行在安卓VM中,而RenderScript部分代码则运行在本地,且其内存是由上层的应用分配的。
4.1 内存分配API
内存API主要包含三个类:Element,Type与Allocation。他们三者对应的关系能用malloc函数的使用来很好的说明,例如:
int *array = (int *)malloc(sizeof(int) * 10);
malloc函数的参数可以分成两个部分:第一个就是sizeof(int),该过程指定每个内存单元需要多个内存;第二个就是*10,该过程指定需要分配多个这样的内存单元。对应的内存分配APIs就是Element类,表示的是一个内存单元,就像一个float或者一个struct所表示的内存。而Type表示的就是“*10”部分,就像一个Element序列一样。Allocation则用来执行由Type所描述的内存分配,且最终代表分配得到的内存。
大部分情况下都不需要直接使用这些API,因为系统在自动生成反射层的时候已经做好了封装,需要做的就是调用对应类的构造函数并把分配的内存绑定到RenderScript而已。但是比如当你加载一张图片到RenderScript层或者给一个指针分配内存时,就需要应用到这些API。
4.2 静态内存
4.2.1 静态内存的分配
这里的静态内存指的是在RenderScript中声明的非静态的全局变量(静态的或者局部的变量就无法在Android framework层访问,也就不讨论),他们在编译时就分配了内存,在RenderScript代码中可以直接使用它们而不需要在Android Framework层给他们分配内存。在Android Framework层也可以通过反射层生成的函数来访问他们。如果这些变量在RenderScript中被初始化,那么他们也将在Android Framework层中被进行同样的初始化。
注意:如果在RenderScript中使用到了RenderScript中预定义了的一些含有指针的结构体,比如rs_program_fragment和rs_allocation,那么就需要先在Android Framework层构造一个该结构对应类的实例,然后调用set方法把内存绑定到RenderScript运行时,而不能直接在RenderScript层操作。但是这个对于用户自定义的包含指针的结果无效,因为根本就无法自定义包含指针的结构。
4.2.2 静态内存的读写
在RenderScript层对静态分配的内存的写操作是单向的。当你在RenderScript层修改了某个变量的值,出于性能方面的考虑,这个变化不会反应给安卓层。在安卓层调用get方法获得的是安卓层最后一次通过set方法设置的值,除非通过rsSendToClient()等手段,否则安卓层是永远获取不到RenderScript对静态分配的内存的修改的。但是,当安卓层修改了某个变量的值后,该值的变化随后就会自动同步到RenderScript层。
下面是读写示例。假如在rsfile.rs中定义了全局变量point:
typedef struct Point { int x; int y; } Point_t; Point_t point;
那么在RenderScript中可以如下直接给变量赋值:
point.x = 1;
point.y = 1;
在Android framework层中可以这样修改该变量的值,且修改的值会通知到RenderScript层:
ScriptC_rsfile mScript; ... Item i = new ScriptField_Point.Item(); i.x = 1; i.y = 1; mScript.set_point(i);
在RenderScript中读取变量的值:
rsDebug("Printing out a Point", point.x, point.y);
在Android framework中读取变量的值。再一次强调:读取到的永远是Android framework层最后一次调用set方法给该变量赋的值,而如果在Android framework层没有用set方法给变量赋过值的话,那么读取的将是null,下面代码也会报空指针错误
Log.i("TAGNAME", "Printing out a Point: " + mScript.get_point().x + " " + mScript.get_point().y);
System.out.println(point.get_x() + " " + point.get_y());
4.3 动态内存
4.3.1 动态内存的分配
对于动态内存,比如指针,就必须在Android Framework层中给它分配内存。需要两个过程:分配内存与绑定内存。这样做的好处在于:安卓VM能够完全掌握RenderScript内存的分配与回收。
无论在Android Framework层还是RenderScript中,都可以通过该指针来访问分配的内存。
为了给RenderScript动态分配内存,最常见的做法是调用Script.FieldBase的构造函数,当然好也可以手动创建Allocation来实现,为了简单起见,应该使用Script.FieldBase.当获取分配的内存对象后,就可以通过反射层的bind方法把该内存绑定到RenderScript。下面代码是两种实现方式的例子:
private RenderScript myRenderScript; private ScriptC_example mScript; private Resources resources;
public void init(RenderScript rs, Resources res) { myRenderScript = rs; resources = res; //使用反射层生成的类分配内存 ScriptField_Point touchPoints = new ScriptField_Point(myRenderScript, 2); //自己使用API分配内存 intPointer = Allocation.createSized(myRenderScript, Element.I32(myRenderScript), 2); mScript = new ScriptC_example(myRenderScript, resources, R.raw.example); //绑定内存 mScript.bind_touchPoints(touchPoints); mScript.bind_intPointer(intPointer); ... }
4.3.2 动态内存的读写
对于动态内存,在Android framework层可以通过反射层的set/get方法来读/写内存,在RenderScript中也可以像往常一样读/写,且任意一方的写操作都会通知到另外一方。
下面是示例。假设在rsfile.rs定义了如下全局指针:
typedef struct Point { int x; int y; } Point_t; Point_t *point;
只要你已经在Android framework层给分配了内存,那么就可以像往常一样使用它,且任何的修改都会通知到Android framework层。
point[index].x = 1;
point[index].y = 1;
在Android framework 层通过反射层提供的方法读写:
ScriptField_Point p = new ScriptField_Point(mRS, 1); Item i = new ScriptField_Point.Item(); i.x=100; i.y = 100; p.set(i, 0, true); mScript.bind_point(p); points.get_x(0); //read x and y from index 0 points.get_x(0);
内存只需要绑定一次就可以了,不需要每次修改值的时候再次绑定
5 基本应用场景
5.1 RenderScript层回调Android framework层
前面我们提到过,RenderScript中的invokable不能有返回值,以及对于静态内存,RenderScript层对其修改不会通知到Android framework层。对于这两种情况,配套使用RSMessageHandler和rsSendToClient是很好的解决方案。对于二者的关系,从命名就可以看出一二,RSMessageHandler相当于常用的handleMessage函数,而rsSendToClient则相当于Handler.sendMessage,只是RSMessageHandler运行在Android framework层,而rsSendToClient运行在RenderScript层。
RenderScript.RSMessageHandler它implements Runnable,在使用过程中只需要重写run函数即可。其有三个重要的字段:
使用范例:
mRenderScript.setMessageHandler(new RSMessageHandler(){ @Override public void run(){ switch (mID) { case type: //do something break; default: break; } } });
注意:run函数不是运行在主线程,所以在run函数中不能直接做操作主界面UI的操作。
5.2 在RenderScript kernel中访问更多的元素
我们提到过,对于一个kernel,最多只能有一个输入Allocation,假如需要在kernel中访问更多的Allocation,那怎么办呢?
在kernel中,仅容许对当前元素进行操作,即当前坐标(x,y)表示的元素,如果想访问其他元素,则需要定义一个全局的输入allocation,然后使用rsGetElementAt_type()来获取其他元素,比如:下面的averageBitmap就访问了全局变量inBitmap的数据:
rs_allocation inBitmap; uchar4 __attribute__((kernel)) averageBitmap(uchar4 in, uint32_t x, uint32_t y){ uchar4 out = in; uchar4 left = in; uchar4 top = in; uchar4 right = in; uchar4 bottom = in; if(x - 1 > -1){ //access other element left = rsGetElementAt_uchar4(inBitmap, x - 1, y); } if(y - 1 > -1){ top = rsGetElementAt_uchar4(inBitmap, x , y - 1); } if(x + 1 < width){ right = rsGetElementAt_uchar4(inBitmap, x + 1, y); } if(y + 1 < height){ bottom = rsGetElementAt_uchar4(inBitmap, x, y + 1); } out.r = (left.r + top.r + right.r + bottom.r) / 4; out.g = (left.g + top.g + right.g + bottom.g) / 4; out.b = (left.b + top.b + right.b + bottom.b) / 4; return out; }