From 4d3399337d18ef04116bc8a2e5799274655d0c30 Mon Sep 17 00:00:00 2001 From: Jason Sams Date: Tue, 11 May 2010 14:03:58 -0700 Subject: [PATCH] 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 --- .../android/renderscript/RenderScript.java | 17 +- .../java/android/renderscript/Sampler.java | 80 ++ .../java/android/renderscript/Script.java | 88 +- .../java/android/renderscript/ScriptC.java | 60 +- .../jni/android_renderscript_RenderScript.cpp | 86 +- libs/rs/Android.mk | 2 +- .../Film/src/com/android/film/FilmRS.java | 4 +- libs/rs/java/Fountain/res/raw/fountain.c | 52 - libs/rs/java/Fountain/res/raw/fountain.rs | 69 ++ libs/rs/java/Fountain/res/raw/fountain2.rs | 73 -- libs/rs/java/Fountain/res/raw/fountain_bc.bc | Bin 0 -> 2048 bytes .../src/com/android/fountain/FountainRS.java | 86 +- .../android/fountain/ScriptC_Fountain.java | 49 + .../android/fountain/ScriptField_Point.java | 67 ++ .../java/ImageProcessing/res/raw/threshold.rs | 87 +- .../ImageProcessing/res/raw/threshold2.rs | 49 - .../ImageProcessing/res/raw/threshold_bc.bc | Bin 0 -> 1528 bytes .../rs/image/ImageProcessingActivity.java | 149 +-- .../android/rs/image/ScriptC_Threshold.java | 67 ++ libs/rs/rs.spec | 46 +- libs/rs/rsContext.cpp | 23 - libs/rs/rsContext.h | 2 - libs/rs/rsHandcode.h | 43 + libs/rs/rsProgramFragment.cpp | 2 + libs/rs/rsScript.cpp | 116 ++- libs/rs/rsScript.h | 17 +- libs/rs/rsScriptC.cpp | 244 +---- libs/rs/rsScriptC.h | 22 +- libs/rs/rsScriptC_Lib.cpp | 958 +++++++++--------- libs/rs/rsUtils.h | 32 + libs/rs/scriptc/rs_geom.rsh | 26 - libs/rs/scriptc/rs_graphics.rsh | 21 +- libs/rs/scriptc/rs_math.rsh | 796 +++++++++++---- 33 files changed, 1897 insertions(+), 1536 deletions(-) delete mode 100644 libs/rs/java/Fountain/res/raw/fountain.c create mode 100644 libs/rs/java/Fountain/res/raw/fountain.rs delete mode 100644 libs/rs/java/Fountain/res/raw/fountain2.rs create mode 100644 libs/rs/java/Fountain/res/raw/fountain_bc.bc create mode 100644 libs/rs/java/Fountain/src/com/android/fountain/ScriptC_Fountain.java create mode 100644 libs/rs/java/Fountain/src/com/android/fountain/ScriptField_Point.java delete mode 100644 libs/rs/java/ImageProcessing/res/raw/threshold2.rs create mode 100644 libs/rs/java/ImageProcessing/res/raw/threshold_bc.bc create mode 100644 libs/rs/java/ImageProcessing/src/com/android/rs/image/ScriptC_Threshold.java delete mode 100644 libs/rs/scriptc/rs_geom.rsh diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java index db2a3fd309ad7..e9f8fe6c86dba 100644 --- a/graphics/java/android/renderscript/RenderScript.java +++ b/graphics/java/android/renderscript/RenderScript.java @@ -138,16 +138,17 @@ public class RenderScript { native void nScriptSetClearDepth(int script, float depth); native void nScriptSetClearStencil(int script, int stencil); native void nScriptSetTimeZone(int script, byte[] timeZone); - native void nScriptSetType(int type, boolean writable, String name, int slot); native void nScriptSetRoot(boolean isRoot); - native void nScriptSetInvokable(String name, int slot); native void nScriptInvoke(int id, int slot); + native void nScriptInvokeData(int id, int slot); + native void nScriptInvokeV(int id, int slot, byte[] params); + native void nScriptSetVarI(int id, int slot, int val); + native void nScriptSetVarF(int id, int slot, float val); + native void nScriptSetVarV(int id, int slot, byte[] val); native void nScriptCBegin(); native void nScriptCSetScript(byte[] script, int offset, int length); native int nScriptCCreate(); - native void nScriptCAddDefineI32(String name, int value); - native void nScriptCAddDefineF(String name, float value); native void nSamplerBegin(); native void nSamplerSet(int param, int value); @@ -229,6 +230,13 @@ public class RenderScript { Element mElement_COLOR_U8_4; Element mElement_COLOR_F32_4; + Sampler mSampler_CLAMP_NEAREST; + Sampler mSampler_CLAMP_LINEAR; + Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR; + Sampler mSampler_WRAP_NEAREST; + Sampler mSampler_WRAP_LINEAR; + Sampler mSampler_WRAP_LINEAR_MIP_LINEAR; + /////////////////////////////////////////////////////////////////////////////////// // @@ -293,7 +301,6 @@ public class RenderScript { mRS.mMessageCallback.mID = msg; mRS.mMessageCallback.run(); } - //Log.d(LOG_TAG, "MessageThread msg " + msg + " v1 " + rbuf[0] + " v2 " + rbuf[1] + " v3 " +rbuf[2]); } Log.d(LOG_TAG, "MessageThread exiting."); } diff --git a/graphics/java/android/renderscript/Sampler.java b/graphics/java/android/renderscript/Sampler.java index 40ba7225f95ee..da83d04bb8034 100644 --- a/graphics/java/android/renderscript/Sampler.java +++ b/graphics/java/android/renderscript/Sampler.java @@ -51,6 +51,86 @@ public class Sampler extends BaseObj { mID = id; } + Sampler mSampler_CLAMP_NEAREST; + Sampler mSampler_CLAMP_LINEAR; + Sampler mSampler_CLAMP_LINEAR_MIP; + Sampler mSampler_WRAP_NEAREST; + Sampler mSampler_WRAP_LINEAR; + Sampler mSampler_WRAP_LINEAR_MIP; + + public static Sampler CLAMP_NEAREST(RenderScript rs) { + if(rs.mSampler_CLAMP_NEAREST == null) { + Builder b = new Builder(rs); + b.setMin(Value.NEAREST); + b.setMag(Value.NEAREST); + b.setWrapS(Value.CLAMP); + b.setWrapT(Value.CLAMP); + rs.mSampler_CLAMP_NEAREST = b.create(); + } + return rs.mSampler_CLAMP_NEAREST; + } + + public static Sampler CLAMP_LINEAR(RenderScript rs) { + if(rs.mSampler_CLAMP_LINEAR == null) { + Builder b = new Builder(rs); + b.setMin(Value.LINEAR); + b.setMag(Value.LINEAR); + b.setWrapS(Value.CLAMP); + b.setWrapT(Value.CLAMP); + rs.mSampler_CLAMP_LINEAR = b.create(); + } + return rs.mSampler_CLAMP_LINEAR; + } + + public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) { + if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) { + Builder b = new Builder(rs); + b.setMin(Value.LINEAR_MIP_LINEAR); + b.setMag(Value.LINEAR_MIP_LINEAR); + b.setWrapS(Value.CLAMP); + b.setWrapT(Value.CLAMP); + rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create(); + } + return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR; + } + + public static Sampler WRAP_NEAREST(RenderScript rs) { + if(rs.mSampler_WRAP_NEAREST == null) { + Builder b = new Builder(rs); + b.setMin(Value.NEAREST); + b.setMag(Value.NEAREST); + b.setWrapS(Value.WRAP); + b.setWrapT(Value.WRAP); + rs.mSampler_WRAP_NEAREST = b.create(); + } + return rs.mSampler_WRAP_NEAREST; + } + + public static Sampler WRAP_LINEAR(RenderScript rs) { + if(rs.mSampler_WRAP_LINEAR == null) { + Builder b = new Builder(rs); + b.setMin(Value.LINEAR); + b.setMag(Value.LINEAR); + b.setWrapS(Value.WRAP); + b.setWrapT(Value.WRAP); + rs.mSampler_WRAP_LINEAR = b.create(); + } + return rs.mSampler_WRAP_LINEAR; + } + + public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) { + if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) { + Builder b = new Builder(rs); + b.setMin(Value.LINEAR_MIP_LINEAR); + b.setMag(Value.LINEAR_MIP_LINEAR); + b.setWrapS(Value.WRAP); + b.setWrapT(Value.WRAP); + rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create(); + } + return rs.mSampler_WRAP_LINEAR_MIP_LINEAR; + } + + public static class Builder { RenderScript mRS; Value mMin; diff --git a/graphics/java/android/renderscript/Script.java b/graphics/java/android/renderscript/Script.java index 0d21368242ec1..632a459069a56 100644 --- a/graphics/java/android/renderscript/Script.java +++ b/graphics/java/android/renderscript/Script.java @@ -46,6 +46,15 @@ public class Script extends BaseObj { mRS.nScriptInvoke(mID, slot); } + protected void invokeData(int slot) { + mRS.nScriptInvokeData(mID, slot); + } + + protected void invokeV(int slot, FieldPacker v) { + mRS.nScriptInvokeV(mID, slot, v.getData()); + } + + Script(int id, RenderScript rs) { super(rs); mID = id; @@ -53,7 +62,23 @@ public class Script extends BaseObj { public void bindAllocation(Allocation va, int slot) { mRS.validate(); - mRS.nScriptBindAllocation(mID, va.mID, slot); + if (va != null) { + mRS.nScriptBindAllocation(mID, va.mID, slot); + } else { + mRS.nScriptBindAllocation(mID, 0, slot); + } + } + + public void setVar(int index, float v) { + mRS.nScriptSetVarF(mID, index, v); + } + + public void setVar(int index, int v) { + mRS.nScriptSetVarI(mID, index, v); + } + + public void setVar(int index, FieldPacker v) { + mRS.nScriptSetVarV(mID, index, v.getData()); } public void setClearColor(float r, float g, float b, float a) { @@ -82,71 +107,10 @@ public class Script extends BaseObj { public static class Builder { RenderScript mRS; - boolean mIsRoot = false; - Type[] mTypes; - String[] mNames; - boolean[] mWritable; - int mInvokableCount = 0; - Invokable[] mInvokables; Builder(RenderScript rs) { mRS = rs; - mTypes = new Type[MAX_SLOT]; - mNames = new String[MAX_SLOT]; - mWritable = new boolean[MAX_SLOT]; - mInvokables = new Invokable[MAX_SLOT]; } - - public void setType(Type t, int slot) { - mTypes[slot] = t; - mNames[slot] = null; - } - - public void setType(Type t, String name, int slot) { - mTypes[slot] = t; - mNames[slot] = name; - } - - public Invokable addInvokable(String func) { - Invokable i = new Invokable(); - i.mName = func; - i.mRS = mRS; - i.mSlot = mInvokableCount; - mInvokables[mInvokableCount++] = i; - return i; - } - - public void setType(boolean writable, int slot) { - mWritable[slot] = writable; - } - - void transferCreate() { - mRS.nScriptSetRoot(mIsRoot); - for(int ct=0; ct < mTypes.length; ct++) { - if(mTypes[ct] != null) { - mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct); - } - } - for(int ct=0; ct < mInvokableCount; ct++) { - mRS.nScriptSetInvokable(mInvokables[ct].mName, ct); - } - } - - void transferObject(Script s) { - s.mIsRoot = mIsRoot; - s.mTypes = mTypes; - s.mInvokables = new Invokable[mInvokableCount]; - for(int ct=0; ct < mInvokableCount; ct++) { - s.mInvokables[ct] = mInvokables[ct]; - s.mInvokables[ct].mScript = s; - } - s.mInvokables = null; - } - - public void setRoot(boolean r) { - mIsRoot = r; - } - } diff --git a/graphics/java/android/renderscript/ScriptC.java b/graphics/java/android/renderscript/ScriptC.java index f5d5b2fa53709..c1ba000f1516b 100644 --- a/graphics/java/android/renderscript/ScriptC.java +++ b/graphics/java/android/renderscript/ScriptC.java @@ -81,8 +81,6 @@ public class ScriptC extends Script { public static class Builder extends Script.Builder { byte[] mProgram; int mProgramLength; - HashMap mIntDefines = new HashMap(); - HashMap mFloatDefines = new HashMap(); public Builder(RenderScript rs) { super(rs); @@ -133,66 +131,20 @@ public class ScriptC extends Script { static synchronized ScriptC internalCreate(Builder b) { b.mRS.nScriptCBegin(); - b.transferCreate(); - - for (Entry e: b.mIntDefines.entrySet()) { - b.mRS.nScriptCAddDefineI32(e.getKey(), e.getValue().intValue()); - } - for (Entry e: b.mFloatDefines.entrySet()) { - b.mRS.nScriptCAddDefineF(e.getKey(), e.getValue().floatValue()); - } + android.util.Log.e("rs", "len = " + b.mProgramLength); b.mRS.nScriptCSetScript(b.mProgram, 0, b.mProgramLength); int id = b.mRS.nScriptCCreate(); ScriptC obj = new ScriptC(id, b.mRS); - b.transferObject(obj); - return obj; } - public void addDefine(String name, int value) { - mIntDefines.put(name, value); - } - - public void addDefine(String name, float value) { - mFloatDefines.put(name, value); - } - - /** - * Takes the all public static final fields for a class, and adds defines - * for them, using the name of the field as the name of the define. - */ - public void addDefines(Class cl) { - addDefines(cl.getFields(), (Modifier.STATIC | Modifier.FINAL | Modifier.PUBLIC), null); - } - - /** - * Takes the all public fields for an object, and adds defines - * for them, using the name of the field as the name of the define. - */ - public void addDefines(Object o) { - addDefines(o.getClass().getFields(), Modifier.PUBLIC, o); - } - - void addDefines(Field[] fields, int mask, Object o) { - for (Field f: fields) { - try { - if ((f.getModifiers() & mask) == mask) { - Class t = f.getType(); - if (t == int.class) { - mIntDefines.put(f.getName(), f.getInt(o)); - } - else if (t == float.class) { - mFloatDefines.put(f.getName(), f.getFloat(o)); - } - } - } catch (IllegalAccessException ex) { - // TODO: Do we want this log? - Log.d(TAG, "addDefines skipping field " + f.getName()); - } - } - } + public void addDefine(String name, int value) {} + public void addDefine(String name, float value) {} + public void addDefines(Class cl) {} + public void addDefines(Object o) {} + void addDefines(Field[] fields, int mask, Object o) {} public ScriptC create() { return internalCreate(this); diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp index 45cc72e7e41bc..2a7ad386a39be 100644 --- a/graphics/jni/android_renderscript_RenderScript.cpp +++ b/graphics/jni/android_renderscript_RenderScript.cpp @@ -853,6 +853,33 @@ nScriptBindAllocation(JNIEnv *_env, jobject _this, jint script, jint alloc, jint rsScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot); } +static void +nScriptSetVarI(JNIEnv *_env, jobject _this, jint script, jint slot, jint val) +{ + RsContext con = (RsContext)(_env->GetIntField(_this, gContextId)); + LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i), b(%f), a(%f)", con, (void *)script, slot, val); + rsScriptSetVarI(con, (RsScript)script, slot, val); +} + +static void +nScriptSetVarF(JNIEnv *_env, jobject _this, jint script, jint slot, float val) +{ + RsContext con = (RsContext)(_env->GetIntField(_this, gContextId)); + LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i), b(%f), a(%f)", con, (void *)script, slot, val); + rsScriptSetVarF(con, (RsScript)script, slot, val); +} + +static void +nScriptSetVarV(JNIEnv *_env, jobject _this, jint script, jint slot, jbyteArray data) +{ + RsContext con = (RsContext)(_env->GetIntField(_this, gContextId)); + LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot); + jint len = _env->GetArrayLength(data); + jbyte *ptr = _env->GetByteArrayElements(data, NULL); + rsScriptSetVarV(con, (RsScript)script, slot, ptr, len); + _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); +} + static void nScriptSetClearColor(JNIEnv *_env, jobject _this, jint script, jfloat r, jfloat g, jfloat b, jfloat a) { @@ -894,36 +921,6 @@ nScriptSetTimeZone(JNIEnv *_env, jobject _this, jint script, jbyteArray timeZone } } -static void -nScriptSetType(JNIEnv *_env, jobject _this, jint type, jboolean writable, jstring _str, jint slot) -{ - RsContext con = (RsContext)(_env->GetIntField(_this, gContextId)); - LOG_API("nScriptCAddType, con(%p), type(%p), writable(%i), slot(%i)", con, (RsType)type, writable, slot); - const char* n = NULL; - if (_str) { - n = _env->GetStringUTFChars(_str, NULL); - } - rsScriptSetType(con, (RsType)type, slot, writable, n); - if (n) { - _env->ReleaseStringUTFChars(_str, n); - } -} - -static void -nScriptSetInvoke(JNIEnv *_env, jobject _this, jstring _str, jint slot) -{ - RsContext con = (RsContext)(_env->GetIntField(_this, gContextId)); - LOG_API("nScriptSetInvoke, con(%p)", con); - const char* n = NULL; - if (_str) { - n = _env->GetStringUTFChars(_str, NULL); - } - rsScriptSetInvoke(con, n, slot); - if (n) { - _env->ReleaseStringUTFChars(_str, n); - } -} - static void nScriptInvoke(JNIEnv *_env, jobject _this, jint obj, jint slot) { @@ -932,6 +929,26 @@ nScriptInvoke(JNIEnv *_env, jobject _this, jint obj, jint slot) rsScriptInvoke(con, (RsScript)obj, slot); } +static void +nScriptInvokeData(JNIEnv *_env, jobject _this, jint obj, jint slot) +{ + RsContext con = (RsContext)(_env->GetIntField(_this, gContextId)); + LOG_API("nScriptInvokeData, con(%p), script(%p)", con, (void *)obj); + rsScriptInvokeData(con, (RsScript)obj, slot, 0); +} + + +static void +nScriptInvokeV(JNIEnv *_env, jobject _this, jint script, jint slot, jbyteArray data) +{ + RsContext con = (RsContext)(_env->GetIntField(_this, gContextId)); + LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot); + jint len = _env->GetArrayLength(data); + jbyte *ptr = _env->GetByteArrayElements(data, NULL); + rsScriptInvokeV(con, (RsScript)script, slot, ptr, len); + _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); +} + static void nScriptSetRoot(JNIEnv *_env, jobject _this, jboolean isRoot) { @@ -1424,16 +1441,17 @@ static JNINativeMethod methods[] = { {"nScriptSetClearDepth", "(IF)V", (void*)nScriptSetClearDepth }, {"nScriptSetClearStencil", "(II)V", (void*)nScriptSetClearStencil }, {"nScriptSetTimeZone", "(I[B)V", (void*)nScriptSetTimeZone }, -{"nScriptSetType", "(IZLjava/lang/String;I)V", (void*)nScriptSetType }, {"nScriptSetRoot", "(Z)V", (void*)nScriptSetRoot }, -{"nScriptSetInvokable", "(Ljava/lang/String;I)V", (void*)nScriptSetInvoke }, {"nScriptInvoke", "(II)V", (void*)nScriptInvoke }, +{"nScriptInvokeData", "(II)V", (void*)nScriptInvokeData }, +{"nScriptInvokeV", "(II[B)V", (void*)nScriptInvokeV }, +{"nScriptSetVarI", "(III)V", (void*)nScriptSetVarI }, +{"nScriptSetVarF", "(IIF)V", (void*)nScriptSetVarF }, +{"nScriptSetVarV", "(II[B)V", (void*)nScriptSetVarV }, {"nScriptCBegin", "()V", (void*)nScriptCBegin }, {"nScriptCSetScript", "([BII)V", (void*)nScriptCSetScript }, {"nScriptCCreate", "()I", (void*)nScriptCCreate }, -{"nScriptCAddDefineI32", "(Ljava/lang/String;I)V", (void*)nScriptCAddDefineI32 }, -{"nScriptCAddDefineF", "(Ljava/lang/String;F)V", (void*)nScriptCAddDefineF }, {"nProgramFragmentStoreBegin", "(II)V", (void*)nProgramFragmentStoreBegin }, {"nProgramFragmentStoreDepthFunc", "(I)V", (void*)nProgramFragmentStoreDepthFunc }, diff --git a/libs/rs/Android.mk b/libs/rs/Android.mk index 0b060227ac195..95571e7b0afc6 100644 --- a/libs/rs/Android.mk +++ b/libs/rs/Android.mk @@ -106,7 +106,7 @@ LOCAL_SRC_FILES:= \ rsVertexArray.cpp -LOCAL_SHARED_LIBRARIES += libcutils libutils libEGL libGLESv1_CM libGLESv2 libui libacc +LOCAL_SHARED_LIBRARIES += libcutils libutils libEGL libGLESv1_CM libGLESv2 libui libbcc LOCAL_LDLIBS := -lpthread -ldl LOCAL_MODULE:= libRS LOCAL_MODULE_TAGS := optional 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 7d04502256be2..f45684cd1a314 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 73b819b3df864..0000000000000 --- 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 0000000000000..5562a7767b99b --- /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 5d36e35f9dee3..0000000000000 --- 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 0000000000000000000000000000000000000000..f14706877eaf45b3fc878aac069918924dd217d2 GIT binary patch literal 2048 zcmZ8i4Nx1$72eaGbO)TI6OyrH1U`X+!ByN7$e`MCdP1@gwG0AN2I_W3LjG+=j4%vf z+O$Fvl3e3Pfa^l-N!2vdA=6|^4Wu{&6A2VdT>eQN>~V>xZQ``fnD{3@(@EMrcRG_~ zXZP*f_ujs@_rAA#rKZ@LiUi;b1OS8rARPdJcjopOXRvYsRrsA=BtSmHCluM(*#@tC zKd-bmtu#Ru%JhW1JhjlQwx&i@xru5osZgC#<1=AN!(eofo!*4*K)D6A z23b%=1CR;l!klmlebg5Kc@`@_CC@TVNON!VR9Y$KV#+!b1&?y3MvWA2Y>>@W1cQE*=DBeut9J46+ zkit8>Tz5xA?NOZTPlc^fyvc!gPk3hYsCI5j@x)allIFnl0c>;sCZDrj-MMT;DNM?+F=z$p!LW>+d0u0K_79 z6kQwxg6frgjAIyer$xmvUR8NfDu)pu%YRcvaE!y6)6D~%nJN9pG9D+A_tyN+6`R4C z;BXXX_sbL#TckTBuWTAK@hrfqKxo%f@24T&Wza?%R>=eW6hpvoSe(iqNB55V4H{;) zGiyzyyw@qpqfo6ZjcOz+!PA>W9J9#EpkG@i!O_dCcXPK#u(IkWTRQ#$+9a8w>@bA;L=A--i_H@b&A zd7OBK{7SgDW9%~Kv|9`=TKs4;?XFk6xyc>%Emr(F5Z@ij8Oozf#V684Kgq{W_lH_w zq191|c-TcIjoFjy%9|=>EhYy}8UPQMBg~@VP!hm_oo7|&d3+bP=lS{d5*}lT4sTrg z5@O-iV)qq)Bq}B&g{iT+{hsuzuk?WFoxVQ(p!+qIKIP6>%nDqRc)5DbGxceITw}6( z5uxJ(D<1?e(W3CjL5A`l539%o`?40eh@Xvd0yIN#Q@qT>~!GUlI}IP6H8eT<|j52+Q1}@a^*1LYYq) z0n<6YKKq83$7Dz@B}(yvE|jaS-EfpS=&nkg|C~(vC^;JE=#@E+jxTX^qEp6ubZ2CO zBuAC3QFkbCqf7<WAD;EvSh)jNF)rSCn@Qp)i;c` z+2)J5R{LV33JSjvDy{34_|gyKRr($n^i^4mS$*@0vf@r!MNu8D(jPpkemh|(ZY+Sb zwJ|5ieBRRdTTrd<*{(;AX}V&Dzqogx40*kpm~DO()Y@iU--4DUZe;*y&b~t@x@^7` z_*QbwNdxdxIR76LHoNkEtW)-73w7=&#iBp>VWO9Zc%JKX>r}bTx5!ZprZ6M|fZY8r&M;YVy|TLG0I6Sbr16 z8`5NO-}3GVSD&~3y+zF(rc%fK#-29rjpOoD6!t0;>q>AEuK94CgC_cw4fB_F{prOI zcir3j(I0;P0cfHD$b-684{2r<1r1o4sjh%JSj(D!SyT@mf~6zr%g(fU3duOjER^;K zT2|SQ_0}Quv%TH>4F%{jn3s9E}frs9b3#&_QvPwKWv z4qh78SN&_#u&{N%lQ?6vsXFCXkEF_;Xhh7&Jh%6E#!{kBp+qHi%Ma>!sOD_0H}S5@ I_Oz1!3-{QeivR!s literal 0 HcmV?d00001 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 935657965e116..d193134c1c05a 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"); + mSM.bindVertexAllocation(mPoints.getAllocation(), vtxSlot); - 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); + 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 0000000000000..7cff390c9a793 --- /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 0000000000000..edd18d589d61f --- /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 888f0cdb029ee..8dc614e22a254 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; -*/ +#pragma version(1) -struct color_s { - char b; - char g; - char r; - char a; -}; +#include "../../../../scriptc/rs_types.rsh" +#include "../../../../scriptc/rs_math.rsh" +#include "../../../../scriptc/rs_graphics.rsh" -void main() { - int t = uptimeMillis(); +int height; +int width; +float threshold; - struct color_s *in = (struct color_s *) InPixel; - struct color_s *out = (struct color_s *) OutPixel; +typedef struct c4u_s { + char r, g, b, a; +} c4u_t; - int count = Params->inWidth * Params->inHeight; - int i; - float threshold = (Params->threshold * 255.f); +//rs_color4u * InPixel; +//rs_color4u * OutPixel; +c4u_t * InPixel; +c4u_t * OutPixel; - 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 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 9f687b5ead60e..0000000000000 --- 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 0000000000000000000000000000000000000000..55d514cd0a1ca33cc7e85d8c9c0da961bbdd3108 GIT binary patch literal 1528 zcmZ8heN0nV6uinrulCaPAMVunLGD z>z9-%DxX?6P3l4xmbnC{ASPQ@EUT!XI0#^-t{tYL%bdYQvt`r0=k~|+=G@=;xcA(1 z?>V{0XbRnt;{fc20DuSpk^lgxJF}y3lw;u$nu1QqhriCnQqwsHx4I-nu(312*h^?6 zT8y*^G<23(&@Rs$+33Pk<@H(Us9ChNFW6^@JYtRm77_r2;R(Ly%vcg)L|~%;5Hnnp zVdhiS9v{dqGIROaMG>qkV-A+*h&eM-h{QP)azp1l9+E;DuEF;}1=o#oK&>>|gYo zRagTOn^kCi5dDxsSz&^ApFo>kEY-9Nn}R4yFN_UAw9bw;6KDgYzT#p>ok6t0fHpJ2 z1TX&bYW(HZC$FdkTF-?0AJz~;M-XL*Ij|-wWG<#l^qHwlng&AdJ*#7o_kO)28@=~Y zTy9Yd08=7>Te@u+`*6D%K4fGCu!UlP&rD5vfJhaUw72oI*G4!dI1{X}x?L`!su;Ak z0V`dY0>Cb&F07llmBM^np03-ZJAvpX!k&;Yn}9@E!hzjpHc8Hafm7^In&%#Z+0VVLg7`T$m+j5t9@SmX@%>Q;V8fO5ywg_$L>}@YT-N~T3m2R-X-{MMpn?M66E+_l zTFK^@$4t(;;Ov3-g4luHJp%U_31OaRGS;5ED4a_Gp;%Wg3XF7ANnDe@!*6r4>$1}L zQLSNyFZ&WJ`Pr<0Ez}e~mQ}gfZISXx0*a(vq%4u8aF;eo9fSx-0XQy=S3@MZPP$>w zaO<)~z&;eilT?9f5?-Om>+hWobygyVvz&_tZab%s!sTu5v|DLIG9!ho9MY2O9Uwx) zsRA9a?F5&}i&A|fN(mA(QcVHm{_lYPbvUaBts$n-2H;sh*0}ye1UrPG!z(3xMZ3Z! z*l(K{#tJAd2qtXKQBY*tPzeND($>i#Z@m}YmdcAs9wx1t76mw+f(<=(EdgxI+mpM@ zuq}0@Yp$gdD=oYEZH@@;S7x96&$z4B;C0f;n@YyKGx;q3G{|ndJv2<3=O!=I7%`#< zwBX17#*-G$UX}CdYU0RA93LQm@Z_1e^gh$OlT591ivm3o@#Cp;$5|XW#p3y|B>2G8 z&>0s0;F5*WmDek1WCB0B5%;;Ce(otLjcV7Ie#%p;H`GW+M)ZSw3Gw4w$wuZ5KVoY2 zky=YjG;i^>G`?D$xFcnM)H+^O95!I_t<<;`y1`dWyOq8rT4bBd7tQOr!9<)rKhEOd zO(x>(h1;zB%>e0{+%!(u(skQP zkIh_i&IHPzUt^kR9rbU&Otx+wRmsXjl2MJQ<2vZfIPSJlS82Mzj?;$JFCY?ilb5Ox o|7`DQk+Rqooa)XT3b^rhX literal 0 HcmV?d00001 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 9ce53d814b1b3..aec5e23ca7a49 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; - 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); - - final float luminance = 0.2125f * r + - 0.7154f * g + - 0.0721f * b; - if (luminance > threshold) { - mOutData[i] = mInData[i]; - } else { - mOutData[i] = mInData[i] & 0xff000000; - } + if (in == null) { + in = new int[count]; + out = new int[count]; + mBitmapIn.getPixels(in, 0, w, 0, 0, w, h); } - t = java.lang.System.currentTimeMillis() - t; + int threshold = (int)(mThreshold * 255.f) * 255; + //long t = java.lang.System.currentTimeMillis(); - android.util.Log.v("Img", "frame time ms " + t); + 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) { + out[i] = in[i]; + } else { + out[i] = in[i] & 0xff000000; + } + } + //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); + mInPixelsAllocation = Allocation.createBitmapRef(mRS, mBitmapIn); + mOutPixelsAllocation = Allocation.createBitmapRef(mRS, mBitmapOut); - 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; - } - - 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 0000000000000..dbaa18c2e2356 --- /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); + } +} + diff --git a/libs/rs/rs.spec b/libs/rs/rs.spec index 08aa3697a425e..a589d5bf00f2f 100644 --- a/libs/rs/rs.spec +++ b/libs/rs/rs.spec @@ -272,27 +272,51 @@ ScriptSetClearStencil { param uint32_t stencil } -ScriptSetType { - param RsType type - param uint32_t slot - param bool isWritable - param const char * name - } - -ScriptSetInvoke { - param const char * name - param uint32_t slot - } ScriptInvoke { param RsScript s param uint32_t slot } +ScriptInvokeData { + param RsScript s + param uint32_t slot + param void * data + } + +ScriptInvokeV { + param RsScript s + param uint32_t slot + param const void * data + param uint32_t dataLen + handcodeApi + togglePlay + } + ScriptSetRoot { param bool isRoot } +ScriptSetVarI { + param RsScript s + param uint32_t slot + param int value + } + +ScriptSetVarF { + param RsScript s + param uint32_t slot + param float value + } + +ScriptSetVarV { + param RsScript s + param uint32_t slot + param const void * data + param uint32_t dataLen + handcodeApi + togglePlay + } ScriptCSetScript { diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp index 4107229ddf17b..017500ba6f6d6 100644 --- a/libs/rs/rsContext.cpp +++ b/libs/rs/rsContext.cpp @@ -154,7 +154,6 @@ void Context::checkError(const char *msg) const uint32_t Context::runRootScript() { timerSet(RS_TIMER_CLEAR_SWAP); - rsAssert(mRootScript->mEnviroment.mIsRoot); eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth); eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight); @@ -640,28 +639,6 @@ void Context::removeName(ObjectBase *obj) } } -ObjectBase * Context::lookupName(const char *name) const -{ - for(size_t ct=0; ct < mNames.size(); ct++) { - if (!strcmp(name, mNames[ct]->getName())) { - return mNames[ct]; - } - } - return NULL; -} - -void Context::appendNameDefines(String8 *str) const -{ - char buf[256]; - for (size_t ct=0; ct < mNames.size(); ct++) { - str->append("#define NAMED_"); - str->append(mNames[ct]->getName()); - str->append(" "); - sprintf(buf, "%i\n", (int)mNames[ct]); - str->append(buf); - } -} - bool Context::objDestroyOOBInit() { if (!mObjDestroy.mMutex.init()) { diff --git a/libs/rs/rsContext.h b/libs/rs/rsContext.h index 8e755a902f0d1..8249f5b862e8a 100644 --- a/libs/rs/rsContext.h +++ b/libs/rs/rsContext.h @@ -104,8 +104,6 @@ public: void assignName(ObjectBase *obj, const char *name, uint32_t len); void removeName(ObjectBase *obj); - ObjectBase * lookupName(const char *name) const; - void appendNameDefines(String8 *str) const; uint32_t getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait); bool sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace); diff --git a/libs/rs/rsHandcode.h b/libs/rs/rsHandcode.h index 800eddde7052b..f6e56acabf909 100644 --- a/libs/rs/rsHandcode.h +++ b/libs/rs/rsHandcode.h @@ -1,6 +1,49 @@ #define DATA_SYNC_SIZE 1024 +static inline void rsHCAPI_ScriptInvokeV (RsContext rsc, RsScript va, uint32_t slot, const void * data, uint32_t sizeBytes) +{ + ThreadIO *io = &((Context *)rsc)->mIO; + uint32_t size = sizeof(RS_CMD_ScriptInvokeV); + if (sizeBytes < DATA_SYNC_SIZE) { + size += (sizeBytes + 3) & ~3; + } + RS_CMD_ScriptInvokeV *cmd = static_cast(io->mToCore.reserve(size)); + cmd->s = va; + cmd->slot = slot; + cmd->dataLen = sizeBytes; + cmd->data = data; + if (sizeBytes < DATA_SYNC_SIZE) { + cmd->data = (void *)(cmd+1); + memcpy(cmd+1, data, sizeBytes); + io->mToCore.commit(RS_CMD_ID_ScriptInvokeV, size); + } else { + io->mToCore.commitSync(RS_CMD_ID_ScriptInvokeV, size); + } +} + + +static inline void rsHCAPI_ScriptSetVarV (RsContext rsc, RsScript va, uint32_t slot, const void * data, uint32_t sizeBytes) +{ + ThreadIO *io = &((Context *)rsc)->mIO; + uint32_t size = sizeof(RS_CMD_ScriptSetVarV); + if (sizeBytes < DATA_SYNC_SIZE) { + size += (sizeBytes + 3) & ~3; + } + RS_CMD_ScriptSetVarV *cmd = static_cast(io->mToCore.reserve(size)); + cmd->s = va; + cmd->slot = slot; + cmd->dataLen = sizeBytes; + cmd->data = data; + if (sizeBytes < DATA_SYNC_SIZE) { + cmd->data = (void *)(cmd+1); + memcpy(cmd+1, data, sizeBytes); + io->mToCore.commit(RS_CMD_ID_ScriptSetVarV, size); + } else { + io->mToCore.commitSync(RS_CMD_ID_ScriptSetVarV, size); + } +} + static inline void rsHCAPI_AllocationData (RsContext rsc, RsAllocation va, const void * data, uint32_t sizeBytes) { ThreadIO *io = &((Context *)rsc)->mIO; diff --git a/libs/rs/rsProgramFragment.cpp b/libs/rs/rsProgramFragment.cpp index c17b94c110c59..d192195e5f17b 100644 --- a/libs/rs/rsProgramFragment.cpp +++ b/libs/rs/rsProgramFragment.cpp @@ -328,6 +328,7 @@ RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, { ProgramFragment *pf = new ProgramFragment(rsc, params, paramLength); pf->incUserRef(); + LOGE("rsi_ProgramFragmentCreate %p", pf); return pf; } @@ -337,6 +338,7 @@ RsProgramFragment rsi_ProgramFragmentCreate2(Context *rsc, const char * shaderTe { ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength, params, paramLength); pf->incUserRef(); + LOGE("rsi_ProgramFragmentCreate2 %p", pf); return pf; } diff --git a/libs/rs/rsScript.cpp b/libs/rs/rsScript.cpp index a33933b689c85..1c63c115b80ee 100644 --- a/libs/rs/rsScript.cpp +++ b/libs/rs/rsScript.cpp @@ -30,13 +30,25 @@ Script::Script(Context *rsc) : ObjectBase(rsc) mEnviroment.mClearColor[3] = 1; mEnviroment.mClearDepth = 1; mEnviroment.mClearStencil = 0; - mEnviroment.mIsRoot = false; } Script::~Script() { } +void Script::setVar(uint32_t slot, const void *val, uint32_t len) +{ + int32_t *destPtr = ((int32_t **)mEnviroment.mFieldAddress)[slot]; + if (destPtr) { + //LOGE("setVar f1 %f", ((const float *)destPtr)[0]); + //LOGE("setVar %p %i", destPtr, len); + memcpy(destPtr, val, len); + //LOGE("setVar f2 %f", ((const float *)destPtr)[0]); + } else { + LOGE("Calling setVar on slot = %i which is null", slot); + } +} + namespace android { namespace renderscript { @@ -44,7 +56,9 @@ namespace renderscript { void rsi_ScriptBindAllocation(Context * rsc, RsScript vs, RsAllocation va, uint32_t slot) { Script *s = static_cast