summaryrefslogtreecommitdiffstats
path: root/libs/rs/java
diff options
context:
space:
mode:
authorJason Sams <rjsams@android.com>2010-05-11 14:03:58 -0700
committerJason Sams <rjsams@android.com>2010-05-11 14:03:58 -0700
commit4d3399337d18ef04116bc8a2e5799274655d0c30 (patch)
treea264ecb82632516adb510a2ca49d3cac750d54d0 /libs/rs/java
parent810f5ccb99a8a05a6d1617b19a4efb2399450318 (diff)
downloadframeworks_base-4d3399337d18ef04116bc8a2e5799274655d0c30.zip
frameworks_base-4d3399337d18ef04116bc8a2e5799274655d0c30.tar.gz
frameworks_base-4d3399337d18ef04116bc8a2e5799274655d0c30.tar.bz2
Convert renderscript from using ACC to LLVM for its compiler.
This will also require application to be updated to support the new compiler and data passing models. Change-Id: If078e3a5148af395ba1b936169a407d8c3ad727f
Diffstat (limited to 'libs/rs/java')
-rw-r--r--libs/rs/java/Film/src/com/android/film/FilmRS.java4
-rw-r--r--libs/rs/java/Fountain/res/raw/fountain.c52
-rw-r--r--libs/rs/java/Fountain/res/raw/fountain.rs69
-rw-r--r--libs/rs/java/Fountain/res/raw/fountain2.rs73
-rw-r--r--libs/rs/java/Fountain/res/raw/fountain_bc.bcbin0 -> 2048 bytes
-rw-r--r--libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java88
-rw-r--r--libs/rs/java/Fountain/src/com/android/fountain/ScriptC_Fountain.java49
-rw-r--r--libs/rs/java/Fountain/src/com/android/fountain/ScriptField_Point.java67
-rw-r--r--libs/rs/java/ImageProcessing/res/raw/threshold.rs99
-rw-r--r--libs/rs/java/ImageProcessing/res/raw/threshold2.rs49
-rw-r--r--libs/rs/java/ImageProcessing/res/raw/threshold_bc.bcbin0 -> 1528 bytes
-rw-r--r--libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java145
-rw-r--r--libs/rs/java/ImageProcessing/src/com/android/rs/image/ScriptC_Threshold.java67
13 files changed, 374 insertions, 388 deletions
diff --git a/libs/rs/java/Film/src/com/android/film/FilmRS.java b/libs/rs/java/Film/src/com/android/film/FilmRS.java
index 7d04502..f45684c 100644
--- a/libs/rs/java/Film/src/com/android/film/FilmRS.java
+++ b/libs/rs/java/Film/src/com/android/film/FilmRS.java
@@ -217,8 +217,8 @@ public class FilmRS {
ScriptC.Builder sb = new ScriptC.Builder(mRS);
sb.setScript(mRes, R.raw.filmstrip);
- sb.setRoot(true);
- sb.setType(mStripPositionType, "Pos", 1);
+ //sb.setRoot(true);
+ //sb.setType(mStripPositionType, "Pos", 1);
mScriptStrip = sb.create();
mScriptStrip.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
diff --git a/libs/rs/java/Fountain/res/raw/fountain.c b/libs/rs/java/Fountain/res/raw/fountain.c
deleted file mode 100644
index 73b819b..0000000
--- a/libs/rs/java/Fountain/res/raw/fountain.c
+++ /dev/null
@@ -1,52 +0,0 @@
-// Fountain test script
-#pragma version(1)
-
-int newPart = 0;
-
-int main(int launchID) {
- int ct;
- int count = Control->count;
- int rate = Control->rate;
- float height = getHeight();
- struct point_s * p = (struct point_s *)point;
-
- if (rate) {
- float rMax = ((float)rate) * 0.005f;
- int x = Control->x;
- int y = Control->y;
- int color = ((int)(Control->r * 255.f)) |
- ((int)(Control->g * 255.f)) << 8 |
- ((int)(Control->b * 255.f)) << 16 |
- (0xf0 << 24);
- struct point_s * np = &p[newPart];
-
- while (rate--) {
- vec2Rand((float *)&np->delta.x, rMax);
- np->position.x = x;
- np->position.y = y;
- np->color = color;
- newPart++;
- np++;
- if (newPart >= count) {
- newPart = 0;
- np = &p[newPart];
- }
- }
- }
-
- for (ct=0; ct < count; ct++) {
- float dy = p->delta.y + 0.15f;
- float posy = p->position.y + dy;
- if ((posy > height) && (dy > 0)) {
- dy *= -0.3f;
- }
- p->delta.y = dy;
- p->position.x += p->delta.x;
- p->position.y = posy;
- p++;
- }
-
- uploadToBufferObject(NAMED_PartBuffer);
- drawSimpleMesh(NAMED_PartMesh);
- return 1;
-}
diff --git a/libs/rs/java/Fountain/res/raw/fountain.rs b/libs/rs/java/Fountain/res/raw/fountain.rs
new file mode 100644
index 0000000..5562a77
--- /dev/null
+++ b/libs/rs/java/Fountain/res/raw/fountain.rs
@@ -0,0 +1,69 @@
+// Fountain test script
+#pragma version(1)
+
+#include "../../../../scriptc/rs_types.rsh"
+#include "../../../../scriptc/rs_math.rsh"
+#include "../../../../scriptc/rs_graphics.rsh"
+
+static int newPart = 0;
+
+float4 partColor;
+rs_mesh partMesh;
+rs_allocation partBuffer;
+
+typedef struct __attribute__((packed, aligned(4))) Point_s {
+ float2 delta;
+ rs_position2 pos;
+ rs_color4u color;
+} Point_t;
+Point_t *point;
+
+#pragma rs export_var(point, partColor, partMesh, partBuffer)
+//#pragma rs export_type(Point_s)
+//#pragma rs export_element(point)
+
+int root() {
+ debugPf(1, partColor.x);
+ debugPi(4, partMesh);
+ debugPi(5, partBuffer);
+
+ float height = getHeight();
+ int size = allocGetDimX(partBuffer);
+
+ Point_t * p = point;
+ for (int ct=0; ct < size; ct++) {
+ p->delta.y += 0.15f;
+ p->pos += p->delta;
+ if ((p->pos.y > height) && (p->delta.y > 0)) {
+ p->delta.y *= -0.3f;
+ }
+ p++;
+ }
+
+ uploadToBufferObject(partBuffer);
+ drawSimpleMesh(partMesh);
+ return 1;
+}
+
+void addParticles(int rate, int x, int y)
+{
+ float rMax = ((float)rate) * 0.005f;
+ int size = allocGetDimX(partBuffer);
+
+ rs_color4u c = convertColorTo8888(partColor.x, partColor.y, partColor.z);
+ Point_t * np = &point[newPart];
+
+ float2 p = {x, y};
+ while (rate--) {
+ np->delta = vec2Rand(rMax);
+ np->pos = p;
+ np->color = c;
+ newPart++;
+ np++;
+ if (newPart >= size) {
+ newPart = 0;
+ np = &point[newPart];
+ }
+ }
+}
+
diff --git a/libs/rs/java/Fountain/res/raw/fountain2.rs b/libs/rs/java/Fountain/res/raw/fountain2.rs
deleted file mode 100644
index 5d36e35..0000000
--- a/libs/rs/java/Fountain/res/raw/fountain2.rs
+++ /dev/null
@@ -1,73 +0,0 @@
-// Fountain test script
-#pragma version(1)
-
-#include "../../../../scriptc/rs_types.rsh"
-#include "../../../../scriptc/rs_math.rsh"
-#include "../../../../scriptc/rs_graphics.rsh"
-
-static int newPart = 0;
-
-typedef struct Control_s {
- int x, y;
- int rate;
- int count;
- float r, g, b;
- rs_mesh partMesh;
- rs_allocation partBuffer;
-} Control_t;
-Control_t *Control;
-
-typedef struct Point_s{
- float2 delta;
- rs_position2 pos;
- rs_color4u color;
-} Point_t;
-Point_t *point;
-
-int main(int launchID) {
- int ct;
- int count = Control->count;
- int rate = Control->rate;
- float height = getHeight();
- Point_t * p = point;
-
- if (rate) {
- float rMax = ((float)rate) * 0.005f;
- int color = ((int)(Control->r * 255.f)) |
- ((int)(Control->g * 255.f)) << 8 |
- ((int)(Control->b * 255.f)) << 16 |
- (0xf0 << 24);
- Point_t * np = &p[newPart];
-
- while (rate--) {
- np->delta.x = rand(rMax);
- np->delta.y = rand(rMax);
- //np->delta = vec2Rand(rMax);
- np->pos.x = Control->x;
- np->pos.y = Control->y;
- np->color = color;
- newPart++;
- np++;
- if (newPart >= count) {
- newPart = 0;
- np = &p[newPart];
- }
- }
- }
-
- for (ct=0; ct < count; ct++) {
- float dy = p->delta.y + 0.15f;
- float posy = p->pos.y + dy;
- if ((posy > height) && (dy > 0)) {
- dy *= -0.3f;
- }
- p->delta.y = dy;
- p->pos.x += p->delta.x;
- p->pos.y = posy;
- p++;
- }
-
- uploadToBufferObject(Control->partBuffer);
- drawSimpleMesh(Control->partMesh);
- return 1;
-}
diff --git a/libs/rs/java/Fountain/res/raw/fountain_bc.bc b/libs/rs/java/Fountain/res/raw/fountain_bc.bc
new file mode 100644
index 0000000..f147068
--- /dev/null
+++ b/libs/rs/java/Fountain/res/raw/fountain_bc.bc
Binary files differ
diff --git a/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java b/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java
index 9356579..d193134 100644
--- a/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java
+++ b/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java
@@ -24,16 +24,6 @@ import android.util.Log;
public class FountainRS {
public static final int PART_COUNT = 20000;
- static class SomeData {
- public int x;
- public int y;
- public int rate;
- public int count;
- public float r;
- public float g;
- public float b;
- }
-
public FountainRS() {
}
@@ -43,21 +33,28 @@ public class FountainRS {
initRS();
}
+ Float4 tmpColor = new Float4();
+ boolean holdingColor = false;
public void newTouchPosition(int x, int y, int rate) {
- if (mSD.rate == 0) {
- mSD.r = ((x & 0x1) != 0) ? 0.f : 1.f;
- mSD.g = ((x & 0x2) != 0) ? 0.f : 1.f;
- mSD.b = ((x & 0x4) != 0) ? 0.f : 1.f;
- if ((mSD.r + mSD.g + mSD.b) < 0.9f) {
- mSD.r = 0.8f;
- mSD.g = 0.5f;
- mSD.b = 1.f;
+ if (rate > 0) {
+ if (true/*!holdingColor*/) {
+ tmpColor.x = ((x & 0x1) != 0) ? 0.f : 1.f;
+ tmpColor.y = ((x & 0x2) != 0) ? 0.f : 1.f;
+ tmpColor.z = ((x & 0x4) != 0) ? 0.f : 1.f;
+ if ((tmpColor.x + tmpColor.y + tmpColor.z) < 0.9f) {
+ tmpColor.x = 0.8f;
+ tmpColor.y = 0.5f;
+ tmpColor.z = 1.0f;
+ }
+ android.util.Log.e("rs", "set color " + tmpColor.x + ", " + tmpColor.y + ", " + tmpColor.z);
+ mScript.set_partColor(tmpColor);
}
+ mScript.invokable_addParticles(rate, x, y);
+ holdingColor = true;
+ } else {
+ holdingColor = false;
}
- mSD.rate = rate;
- mSD.x = x;
- mSD.y = y;
- mIntAlloc.data(mSD);
+
}
@@ -65,49 +62,26 @@ public class FountainRS {
private Resources mRes;
+ private ScriptField_Point mPoints;
+ private ScriptC_Fountain mScript;
private RenderScriptGL mRS;
- private Allocation mIntAlloc;
private SimpleMesh mSM;
- private SomeData mSD;
- private Type mSDType;
private void initRS() {
- mSD = new SomeData();
- mSDType = Type.createFromClass(mRS, SomeData.class, 1, "SomeData");
- mIntAlloc = Allocation.createTyped(mRS, mSDType);
- mSD.count = PART_COUNT;
- mIntAlloc.data(mSD);
-
- Element.Builder eb = new Element.Builder(mRS);
- eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "delta");
- eb.add(Element.createAttrib(mRS, Element.DataType.FLOAT_32, Element.DataKind.POSITION, 2), "position");
- eb.add(Element.createAttrib(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.COLOR, 4), "color");
- Element primElement = eb.create();
-
+ mPoints = new ScriptField_Point(mRS, PART_COUNT);
SimpleMesh.Builder smb = new SimpleMesh.Builder(mRS);
- int vtxSlot = smb.addVertexType(primElement, PART_COUNT);
+ int vtxSlot = smb.addVertexType(mPoints.getType());
smb.setPrimitive(Primitive.POINT);
mSM = smb.create();
- mSM.setName("PartMesh");
-
- Allocation partAlloc = mSM.createVertexAllocation(vtxSlot);
- partAlloc.setName("PartBuffer");
- mSM.bindVertexAllocation(partAlloc, 0);
-
- // All setup of named objects should be done by this point
- // because we are about to compile the script.
- ScriptC.Builder sb = new ScriptC.Builder(mRS);
- sb.setScript(mRes, R.raw.fountain);
- sb.setRoot(true);
- sb.setType(mSDType, "Control", 0);
- sb.setType(mSM.getVertexType(0), "point", 1);
- Script script = sb.create();
- script.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-
- script.bindAllocation(mIntAlloc, 0);
- script.bindAllocation(partAlloc, 1);
- mRS.contextBindRootScript(script);
+ mSM.bindVertexAllocation(mPoints.getAllocation(), vtxSlot);
+
+ mScript = new ScriptC_Fountain(mRS, mRes, true);
+ mScript.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+ mScript.set_partMesh(mSM);
+ mScript.set_partBuffer(mPoints.getAllocation());
+ mScript.bind_point(mPoints);
+ mRS.contextBindRootScript(mScript);
}
}
diff --git a/libs/rs/java/Fountain/src/com/android/fountain/ScriptC_Fountain.java b/libs/rs/java/Fountain/src/com/android/fountain/ScriptC_Fountain.java
new file mode 100644
index 0000000..7cff390
--- /dev/null
+++ b/libs/rs/java/Fountain/src/com/android/fountain/ScriptC_Fountain.java
@@ -0,0 +1,49 @@
+
+package com.android.fountain;
+
+import android.content.res.Resources;
+import android.renderscript.*;
+import android.util.Log;
+
+public class ScriptC_Fountain
+ extends android.renderscript.ScriptC
+{
+ public ScriptC_Fountain(RenderScript rs, Resources resources, boolean isRoot) {
+ super(rs, resources, R.raw.fountain_bc, isRoot);
+ }
+
+ public void set_partColor(Float4 v) {
+ FieldPacker fp = new FieldPacker(16);
+ fp.addF32(v);
+ setVar(0, fp);
+ }
+ public void set_partMesh(SimpleMesh v) {
+ setVar(1, v.getID());
+ }
+ public void set_partBuffer(Allocation v) {
+ setVar(2, v.getID());
+ }
+
+ private ScriptField_Point mField_point;
+ public void bind_point(ScriptField_Point f) {
+ mField_point = f;
+ if (f == null) {
+ bindAllocation(null, 3);
+ } else {
+ bindAllocation(f.getAllocation(), 3);
+ }
+ }
+ public ScriptField_Point get_point() {
+ return mField_point;
+ }
+
+
+ public void invokable_addParticles(int count, int x, int y) {
+ FieldPacker fp = new FieldPacker(12);
+ fp.addI32(count);
+ fp.addI32(x);
+ fp.addI32(y);
+ invokeV(0, fp);
+ }
+}
+
diff --git a/libs/rs/java/Fountain/src/com/android/fountain/ScriptField_Point.java b/libs/rs/java/Fountain/src/com/android/fountain/ScriptField_Point.java
new file mode 100644
index 0000000..edd18d5
--- /dev/null
+++ b/libs/rs/java/Fountain/src/com/android/fountain/ScriptField_Point.java
@@ -0,0 +1,67 @@
+
+package com.android.fountain;
+
+import android.content.res.Resources;
+import android.renderscript.*;
+import android.util.Log;
+
+public class ScriptField_Point
+ extends android.renderscript.Script.FieldBase
+{
+
+ static public class Item {
+ Item() {
+ delta = new Float2();
+ pos = new Float2();
+ color = new Short4();
+ }
+
+ public static final int sizeof = (5*4);
+ Float2 delta;
+ Float2 pos;
+ Short4 color;
+ }
+ private Item mItemArray[];
+
+
+ public ScriptField_Point(RenderScript rs, int count) {
+ // Allocate a pack/unpack buffer
+ mIOBuffer = new FieldPacker(Item.sizeof * count);
+ mItemArray = new Item[count];
+
+ Element.Builder eb = new Element.Builder(rs);
+ eb.add(Element.createVector(rs, Element.DataType.FLOAT_32, 2), "delta");
+ eb.add(Element.createAttrib(rs, Element.DataType.FLOAT_32, Element.DataKind.POSITION, 2), "pos");
+ eb.add(Element.createAttrib(rs, Element.DataType.UNSIGNED_8, Element.DataKind.COLOR, 4), "color");
+ mElement = eb.create();
+
+ init(rs, count);
+ }
+
+ private void copyToArray(Item i, int index) {
+ mIOBuffer.reset(index * Item.sizeof);
+ mIOBuffer.addF32(i.delta);
+ mIOBuffer.addF32(i.pos);
+ mIOBuffer.addU8(i.color);
+ }
+
+ public void set(Item i, int index, boolean copyNow) {
+ mItemArray[index] = i;
+ if (copyNow) {
+ copyToArray(i, index);
+ mAllocation.subData1D(index * Item.sizeof, Item.sizeof, mIOBuffer.getData());
+ }
+ }
+
+ public void copyAll() {
+ for (int ct=0; ct < mItemArray.length; ct++) {
+ copyToArray(mItemArray[ct], ct);
+ }
+ mAllocation.data(mIOBuffer.getData());
+ }
+
+
+ private FieldPacker mIOBuffer;
+
+
+}
diff --git a/libs/rs/java/ImageProcessing/res/raw/threshold.rs b/libs/rs/java/ImageProcessing/res/raw/threshold.rs
index 888f0cd..8dc614e 100644
--- a/libs/rs/java/ImageProcessing/res/raw/threshold.rs
+++ b/libs/rs/java/ImageProcessing/res/raw/threshold.rs
@@ -1,62 +1,49 @@
-/*
-// block of defines matching what RS will insert at runtime.
-struct Params_s{
- int inHeight;
- int inWidth;
- int outHeight;
- int outWidth;
- float threshold;
-};
-struct Params_s * Params;
-struct InPixel_s{
- char a;
- char b;
- char g;
- char r;
-};
-struct InPixel_s * InPixel;
-struct OutPixel_s{
- char a;
- char b;
- char g;
- char r;
-};
-struct OutPixel_s * OutPixel;
-*/
-
-struct color_s {
- char b;
- char g;
- char r;
- char a;
-};
-
-void main() {
- int t = uptimeMillis();
-
- struct color_s *in = (struct color_s *) InPixel;
- struct color_s *out = (struct color_s *) OutPixel;
-
- int count = Params->inWidth * Params->inHeight;
- int i;
- float threshold = (Params->threshold * 255.f);
-
- for (i = 0; i < count; i++) {
- float luminance = 0.2125f * in->r +
- 0.7154f * in->g +
- 0.0721f * in->b;
- if (luminance > threshold) {
- *out = *in;
- } else {
- *((int *)out) = *((int *)in) & 0xff000000;
- }
-
+#pragma version(1)
+
+#include "../../../../scriptc/rs_types.rsh"
+#include "../../../../scriptc/rs_math.rsh"
+#include "../../../../scriptc/rs_graphics.rsh"
+
+int height;
+int width;
+float threshold;
+
+typedef struct c4u_s {
+ char r, g, b, a;
+} c4u_t;
+
+//rs_color4u * InPixel;
+//rs_color4u * OutPixel;
+c4u_t * InPixel;
+c4u_t * OutPixel;
+
+#pragma rs export_var(height, width, threshold, InPixel, OutPixel)
+
+void filter() {
+ debugP(0, (void *)height);
+ debugP(0, (void *)width);
+ debugP(0, (void *)((int)threshold));
+ debugP(0, (void *)InPixel);
+ debugP(0, (void *)OutPixel);
+
+ rs_color4u *in = (rs_color4u *)InPixel;
+ rs_color4u *out = (rs_color4u *)OutPixel;
+ //const rs_color4u mask = {0,0,0,0xff};
+
+ int count = width * height;
+ int tf = threshold * 255 * 255;
+ int masks[2] = {0xffffffff, 0xff000000};
+
+ while (count--) {
+ int luminance = 54 * in->x +
+ 182 * in->y +
+ 18 * in->z;
+ int idx = ((uint32_t)(luminance - tf)) >> 31;
+ *((int *)out) = *((int *)in) & masks[idx];
in++;
out++;
}
- t= uptimeMillis() - t;
- debugI32("Filter time", t);
-
sendToClient(&count, 1, 4, 0);
}
+
diff --git a/libs/rs/java/ImageProcessing/res/raw/threshold2.rs b/libs/rs/java/ImageProcessing/res/raw/threshold2.rs
deleted file mode 100644
index 9f687b5..0000000
--- a/libs/rs/java/ImageProcessing/res/raw/threshold2.rs
+++ /dev/null
@@ -1,49 +0,0 @@
-#pragma version(1)
-
-#include "../../../../scriptc/rs_types.rsh"
-#include "../../../../scriptc/rs_math.rsh"
-#include "../../../../scriptc/rs_graphics.rsh"
-
-typedef struct Params_s{
- int inHeight;
- int inWidth;
- int outHeight;
- int outWidth;
- float threshold;
-} Params_t;
-
-Params_t * Params;
-rs_color4u * InPixel;
-rs_color4u * OutPixel;
-
-
-int main() {
- int t = uptimeMillis();
-
- rs_color4u *in = InPixel;
- rs_color4u *out = OutPixel;
-
- int count = Params->inWidth * Params->inHeight;
- int i;
- float threshold = Params->threshold * 255.f;
-
- for (i = 0; i < count; i++) {
- float luminance = 0.2125f * in->x +
- 0.7154f * in->y +
- 0.0721f * in->z;
- if (luminance > threshold) {
- *out = *in;
- } else {
- *((int *)out) = *((int *)in) & 0xff000000;
- }
-
- in++;
- out++;
- }
-
- t= uptimeMillis() - t;
- debugI32("Filter time", t);
-
- sendToClient(&count, 1, 4, 0);
- return 0;
-}
diff --git a/libs/rs/java/ImageProcessing/res/raw/threshold_bc.bc b/libs/rs/java/ImageProcessing/res/raw/threshold_bc.bc
new file mode 100644
index 0000000..55d514c
--- /dev/null
+++ b/libs/rs/java/ImageProcessing/res/raw/threshold_bc.bc
Binary files differ
diff --git a/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java b/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
index 9ce53d8..aec5e23 100644
--- a/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
+++ b/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
@@ -34,19 +34,14 @@ import android.widget.SeekBar;
import java.lang.Math;
public class ImageProcessingActivity extends Activity implements SurfaceHolder.Callback {
- private Bitmap mBitmap;
- private Params mParams;
- private Script.Invokable mInvokable;
- private int[] mInData;
- private int[] mOutData;
+ private Bitmap mBitmapIn;
+ private Bitmap mBitmapOut;
+ private ScriptC_Threshold mScript;
+ private float mThreshold = 0.5f;
@SuppressWarnings({"FieldCanBeLocal"})
private RenderScript mRS;
@SuppressWarnings({"FieldCanBeLocal"})
- private Type mParamsType;
- @SuppressWarnings({"FieldCanBeLocal"})
- private Allocation mParamsAllocation;
- @SuppressWarnings({"FieldCanBeLocal"})
private Type mPixelType;
@SuppressWarnings({"FieldCanBeLocal"})
private Allocation mInPixelsAllocation;
@@ -56,28 +51,9 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C
private SurfaceView mSurfaceView;
private ImageView mDisplayView;
- static class Params {
- public int inWidth;
- public int outWidth;
- public int inHeight;
- public int outHeight;
-
- public float threshold;
- }
-
- static class Pixel {
- public byte a;
- public byte r;
- public byte g;
- public byte b;
- }
-
class FilterCallback extends RenderScript.RSMessage {
private Runnable mAction = new Runnable() {
public void run() {
- mOutPixelsAllocation.readData(mOutData);
- mBitmap.setPixels(mOutData, 0, mParams.outWidth, 0, 0,
- mParams.outWidth, mParams.outHeight);
mDisplayView.invalidate();
}
};
@@ -89,29 +65,35 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C
}
}
+ int in[];
+ int out[];
private void javaFilter() {
- long t = java.lang.System.currentTimeMillis();
- int count = mParams.inWidth * mParams.inHeight;
- float threshold = mParams.threshold * 255.f;
+ final int w = mBitmapIn.getWidth();
+ final int h = mBitmapIn.getHeight();
+ final int count = w * h;
+
+ if (in == null) {
+ in = new int[count];
+ out = new int[count];
+ mBitmapIn.getPixels(in, 0, w, 0, 0, w, h);
+ }
- for (int i = 0; i < count; i++) {
- final float r = (float)((mInData[i] >> 0) & 0xff);
- final float g = (float)((mInData[i] >> 8) & 0xff);
- final float b = (float)((mInData[i] >> 16) & 0xff);
+ int threshold = (int)(mThreshold * 255.f) * 255;
+ //long t = java.lang.System.currentTimeMillis();
- final float luminance = 0.2125f * r +
- 0.7154f * g +
- 0.0721f * b;
+ for (int i = 0; i < count; i++) {
+ final int luminance = 54 * ((in[i] >> 0) & 0xff) +
+ 182* ((in[i] >> 8) & 0xff) +
+ 18 * ((in[i] >> 16) & 0xff);
if (luminance > threshold) {
- mOutData[i] = mInData[i];
+ out[i] = in[i];
} else {
- mOutData[i] = mInData[i] & 0xff000000;
+ out[i] = in[i] & 0xff000000;
}
}
-
- t = java.lang.System.currentTimeMillis() - t;
-
- android.util.Log.v("Img", "frame time ms " + t);
+ //t = java.lang.System.currentTimeMillis() - t;
+ //android.util.Log.v("Img", "frame time ms " + t);
+ mBitmapOut.setPixels(out, 0, w, 0, 0, w, h);
}
@Override
@@ -119,29 +101,31 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
- mBitmap = loadBitmap(R.drawable.data);
+ mBitmapIn = loadBitmap(R.drawable.data);
+ mBitmapOut = loadBitmap(R.drawable.data);
mSurfaceView = (SurfaceView) findViewById(R.id.surface);
mSurfaceView.getHolder().addCallback(this);
mDisplayView = (ImageView) findViewById(R.id.display);
- mDisplayView.setImageBitmap(mBitmap);
+ mDisplayView.setImageBitmap(mBitmapOut);
((SeekBar) findViewById(R.id.threshold)).setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
- mParams.threshold = progress / 100.0f;
- mParamsAllocation.data(mParams);
+ mThreshold = progress / 100.0f;
+ mScript.set_threshold(mThreshold);
+ long t = java.lang.System.currentTimeMillis();
if (true) {
- mInvokable.execute();
+ mScript.invokable_Filter();
} else {
javaFilter();
- mBitmap.setPixels(mOutData, 0, mParams.outWidth, 0, 0,
- mParams.outWidth, mParams.outHeight);
mDisplayView.invalidate();
}
+ t = java.lang.System.currentTimeMillis() - t;
+ android.util.Log.v("Img", "frame time core ms " + t);
}
}
@@ -154,10 +138,8 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C
}
public void surfaceCreated(SurfaceHolder holder) {
- mParams = createParams();
- mInvokable = createScript();
-
- mInvokable.execute();
+ createScript();
+ mScript.invokable_Filter();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
@@ -166,54 +148,19 @@ public class ImageProcessingActivity extends Activity implements SurfaceHolder.C
public void surfaceDestroyed(SurfaceHolder holder) {
}
- private Script.Invokable createScript() {
+ private void createScript() {
mRS = RenderScript.create();
mRS.mMessageCallback = new FilterCallback();
- mParamsType = Type.createFromClass(mRS, Params.class, 1, "Parameters");
- mParamsAllocation = Allocation.createTyped(mRS, mParamsType);
- mParamsAllocation.data(mParams);
-
- final int pixelCount = mParams.inWidth * mParams.inHeight;
-
- mPixelType = Type.createFromClass(mRS, Pixel.class, 1, "Pixel");
- mInPixelsAllocation = Allocation.createSized(mRS,
- Element.createUser(mRS, Element.DataType.SIGNED_32),
- pixelCount);
- mOutPixelsAllocation = Allocation.createSized(mRS,
- Element.createUser(mRS, Element.DataType.SIGNED_32),
- pixelCount);
-
- mInData = new int[pixelCount];
- mBitmap.getPixels(mInData, 0, mParams.inWidth, 0, 0, mParams.inWidth, mParams.inHeight);
- mInPixelsAllocation.data(mInData);
-
- mOutData = new int[pixelCount];
- mOutPixelsAllocation.data(mOutData);
-
- ScriptC.Builder sb = new ScriptC.Builder(mRS);
- sb.setType(mParamsType, "Params", 0);
- sb.setType(mPixelType, "InPixel", 1);
- sb.setType(mPixelType, "OutPixel", 2);
- sb.setType(true, 2);
- Script.Invokable invokable = sb.addInvokable("main");
- sb.setScript(getResources(), R.raw.threshold);
- //sb.setRoot(true);
-
- ScriptC script = sb.create();
- script.bindAllocation(mParamsAllocation, 0);
- script.bindAllocation(mInPixelsAllocation, 1);
- script.bindAllocation(mOutPixelsAllocation, 2);
-
- return invokable;
- }
+ mInPixelsAllocation = Allocation.createBitmapRef(mRS, mBitmapIn);
+ mOutPixelsAllocation = Allocation.createBitmapRef(mRS, mBitmapOut);
- private Params createParams() {
- final Params params = new Params();
- params.inWidth = params.outWidth = mBitmap.getWidth();
- params.inHeight = params.outHeight = mBitmap.getHeight();
- params.threshold = 0.5f;
- return params;
+ mScript = new ScriptC_Threshold(mRS, getResources(), false);
+ mScript.set_width(mBitmapIn.getWidth());
+ mScript.set_height(mBitmapIn.getHeight());
+ mScript.set_threshold(mThreshold);
+ mScript.bind_InPixel(mInPixelsAllocation);
+ mScript.bind_OutPixel(mOutPixelsAllocation);
}
private Bitmap loadBitmap(int resource) {
diff --git a/libs/rs/java/ImageProcessing/src/com/android/rs/image/ScriptC_Threshold.java b/libs/rs/java/ImageProcessing/src/com/android/rs/image/ScriptC_Threshold.java
new file mode 100644
index 0000000..dbaa18c
--- /dev/null
+++ b/libs/rs/java/ImageProcessing/src/com/android/rs/image/ScriptC_Threshold.java
@@ -0,0 +1,67 @@
+
+package com.android.rs.image;
+
+import android.content.res.Resources;
+import android.renderscript.*;
+import android.util.Log;
+
+public class ScriptC_Threshold
+ extends android.renderscript.ScriptC
+{
+ private final static int mFieldIndex_height = 0;
+ private final static int mFieldIndex_width = 1;
+ private final static int mFieldIndex_threshold = 2;
+ private final static int mFieldIndex_InPixel = 3;
+ private final static int mFieldIndex_OutPixel = 4;
+
+ private Allocation mField_InPixel;
+ private Allocation mField_OutPixel;
+
+ public ScriptC_Threshold(RenderScript rs, Resources resources, boolean isRoot) {
+ super(rs, resources, R.raw.threshold_bc, isRoot);
+ }
+
+ public void bind_InPixel(Allocation f) {
+ if (f != null) {
+ //if (f.getType().getElement() != Element.ATTRIB_COLOR_U8_4(mRS)) {
+ //throw new IllegalArgumentException("Element type mismatch.");
+ //}
+ }
+ bindAllocation(f, mFieldIndex_InPixel);
+ mField_InPixel = f;
+ }
+ public Allocation get_InPixel() {
+ return mField_InPixel;
+ }
+
+ public void bind_OutPixel(Allocation f) {
+ if (f != null) {
+ //if (f.getType().getElement() != Element.ATTRIB_COLOR_U8_4(mRS)) {
+ //throw new IllegalArgumentException("Element type mismatch.");
+ //}
+ }
+ bindAllocation(f, mFieldIndex_OutPixel);
+ mField_OutPixel = f;
+ }
+ public Allocation get_OutPixel() {
+ return mField_OutPixel;
+ }
+
+ public void set_height(int v) {
+ setVar(mFieldIndex_height, v);
+ }
+
+ public void set_width(int v) {
+ setVar(mFieldIndex_width, v);
+ }
+
+ public void set_threshold(float v) {
+ setVar(mFieldIndex_threshold, v);
+ }
+
+ private final static int mInvokableIndex_Filter = 0;
+ public void invokable_Filter() {
+ invokeData(mInvokableIndex_Filter);
+ }
+}
+