summaryrefslogtreecommitdiffstats
path: root/libs/rs/java/Fountain
diff options
context:
space:
mode:
authorJack Palevich <jackpal@google.com>2009-05-28 13:38:16 -0700
committerJack Palevich <jackpal@google.com>2009-05-28 13:38:16 -0700
commit43702d8925c54360ad5f9f66b0d35d61d59f6910 (patch)
treeb96ba49c0a5301c897724308420ee683bf19b2cb /libs/rs/java/Fountain
parentc26ba7928022ff08a5ff3e90aa9e8f4242350435 (diff)
downloadframeworks_base-43702d8925c54360ad5f9f66b0d35d61d59f6910.zip
frameworks_base-43702d8925c54360ad5f9f66b0d35d61d59f6910.tar.gz
frameworks_base-43702d8925c54360ad5f9f66b0d35d61d59f6910.tar.bz2
Add support for setting scripts, rather than having a hard-coded script.
Move the test script into a resource file. Add APIs for reading a script from a resource, InputStream, string, or byte array.
Diffstat (limited to 'libs/rs/java/Fountain')
-rw-r--r--libs/rs/java/Fountain/res/raw/fountain.c111
-rw-r--r--libs/rs/java/Fountain/src/com/android/fountain/FountainView.java2
-rw-r--r--libs/rs/java/Fountain/src/com/android/fountain/RenderScript.java63
3 files changed, 168 insertions, 8 deletions
diff --git a/libs/rs/java/Fountain/res/raw/fountain.c b/libs/rs/java/Fountain/res/raw/fountain.c
new file mode 100644
index 0000000..76f8dcf
--- /dev/null
+++ b/libs/rs/java/Fountain/res/raw/fountain.c
@@ -0,0 +1,111 @@
+// Fountain test script
+
+main(con, ft, launchID) {
+ int count, touch, x, y, rate, maxLife, lifeShift;
+ int life;
+ int ct, ct2;
+ int newPart;
+ int drawCount;
+ int dx, dy, idx;
+ int partPtr;
+ int vertPtr;
+ int posx,posy;
+ int c;
+
+ count = loadI32(con, 0, 1);
+ touch = loadI32(con, 0, 2);
+ x = loadI32(con, 0, 3);
+ y = 480 - loadI32(con, 0, 4);
+
+ rate = 4;
+ maxLife = (count / rate) - 1;
+ lifeShift = 0;
+ {
+ life = maxLife;
+ while (life > 255) {
+ life = life >> 1;
+ lifeShift ++;
+ }
+ }
+
+ contextBindProgramFragment(con, loadI32(con, 0, 7));
+ drawRect(con, 0, 256, 0, 512);
+ contextBindProgramFragment(con, loadI32(con, 0, 6));
+
+ if (touch) {
+ newPart = loadI32(con, 2, 0);
+ for (ct2=0; ct2<rate; ct2++) {
+ dx = scriptRand(con, 0x10000) - 0x8000;
+ dy = scriptRand(con, 0x10000) - 0x8000;
+
+ idx = newPart * 5 + 1;
+ storeI32(con, 2, idx, dx);
+ storeI32(con, 2, idx + 1, dy);
+ storeI32(con, 2, idx + 2, maxLife);
+ storeI32(con, 2, idx + 3, x << 16);
+ storeI32(con, 2, idx + 4, y << 16);
+
+ newPart++;
+ if (newPart >= count) {
+ newPart = 0;
+ }
+ }
+ storeI32(con, 2, 0, newPart);
+ }
+
+ // Emulate intrinsic perf...
+ partPtr = loadVp(con, 2, 4);
+ vertPtr = loadVp(con, 1, 0);
+
+ drawCount = 0;
+ for (ct=0; ct < count; ct++) {
+ //int srcIdx = ct * 5 + 1;
+ //int dstIdx = ct * 3 * 3;
+
+ dx = * (int* )(partPtr + 0); //loadEnvI32(con, 2, srcIdx);
+ dy = * (int* )(partPtr + 4); //loadEnvI32(con, 2, srcIdx + 1);
+ life = * (int* )(partPtr + 8); //loadEnvI32(con, 2, srcIdx + 2);
+ posx = * (int* )(partPtr + 12); //loadEnvI32(con, 2, srcIdx + 3);
+ posy = * (int* )(partPtr + 16); //loadEnvI32(con, 2, srcIdx + 4);
+
+ if (life) {
+ if (posy > 0) {
+ c = 0xffafcf | ((life >> lifeShift) << 24);
+
+ * (int* )(vertPtr) = c; //storeEnvU32(con, 1, dstIdx, c);
+ * (int* )(vertPtr + 4) = posx; //storeEnvI32(con, 1, dstIdx + 1, posx);
+ * (int* )(vertPtr + 8) = posy; //storeEnvI32(con, 1, dstIdx + 2, posy);
+
+ * (int* )(vertPtr + 12) = c; //storeEnvU32(con, 1, dstIdx + 3, c);
+ * (int* )(vertPtr + 16) = posx + 0x10000; //storeEnvI32(con, 1, dstIdx + 4, posx + 0x10000);
+ * (int* )(vertPtr + 20) = posy + dy * 4; //storeEnvI32(con, 1, dstIdx + 5, posy);
+
+ * (int* )(vertPtr + 24) = c; //storeEnvU32(con, 1, dstIdx + 6, c);
+ * (int* )(vertPtr + 28) = posx - 0x10000; //storeEnvI32(con, 1, dstIdx + 7, posx + 0x0800);
+ * (int* )(vertPtr + 32) = posy + dy * 4; //storeEnvI32(con, 1, dstIdx + 8, posy + 0x10000);
+
+ vertPtr = vertPtr + 36;
+ drawCount ++;
+ } else {
+ if (dy < 0) {
+ dy = (-dy) >> 1;
+ }
+ }
+
+ posx = posx + dx;
+ posy = posy + dy;
+ dy = dy - 0x400;
+ life --;
+
+ * (int* )(partPtr + 0) = dx; //storeEnvI32(con, 2, srcIdx, dx);
+ * (int* )(partPtr + 4) = dy; //storeEnvI32(con, 2, srcIdx + 1, dy);
+ * (int* )(partPtr + 8) = life; //storeEnvI32(con, 2, srcIdx + 2, life);
+ * (int* )(partPtr + 12) = posx; //storeEnvI32(con, 2, srcIdx + 3, posx);
+ * (int* )(partPtr + 16) = posy; //storeEnvI32(con, 2, srcIdx + 4, posy);
+ }
+
+ partPtr = partPtr + 20;
+ }
+
+ drawTriangleArray(con, loadI32(con, 0, 5), drawCount);
+}
diff --git a/libs/rs/java/Fountain/src/com/android/fountain/FountainView.java b/libs/rs/java/Fountain/src/com/android/fountain/FountainView.java
index c244d07..bc34080 100644
--- a/libs/rs/java/Fountain/src/com/android/fountain/FountainView.java
+++ b/libs/rs/java/Fountain/src/com/android/fountain/FountainView.java
@@ -111,7 +111,7 @@ public class FountainView extends RSSurfaceView {
mRS.scriptCBegin();
mRS.scriptCSetClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- mRS.scriptCSetScript("");
+ mRS.scriptCSetScript(getResources(), R.raw.fountain);
mRS.scriptCSetRoot(true);
mScript = mRS.scriptCCreate();
diff --git a/libs/rs/java/Fountain/src/com/android/fountain/RenderScript.java b/libs/rs/java/Fountain/src/com/android/fountain/RenderScript.java
index a626053..16a94cb 100644
--- a/libs/rs/java/Fountain/src/com/android/fountain/RenderScript.java
+++ b/libs/rs/java/Fountain/src/com/android/fountain/RenderScript.java
@@ -16,7 +16,11 @@
package com.android.fountain;
+import java.io.InputStream;
+import java.io.IOException;
+
import android.os.Bundle;
+import android.content.res.Resources;
import android.util.Log;
import android.util.Config;
import android.view.Menu;
@@ -119,7 +123,7 @@ public class RenderScript {
native private void nScriptCSetClearStencil(int stencil);
native private void nScriptCAddType(int type);
native private void nScriptCSetRoot(boolean isRoot);
- native private void nScriptCSetScript(String s);
+ native private void nScriptCSetScript(byte[] script, int offset, int length);
native private int nScriptCCreate();
@@ -147,7 +151,7 @@ public class RenderScript {
///////////////////////////////////////////////////////////////////////////////////
- //
+ //
RenderScript(Surface sur) {
mSurface = sur;
@@ -164,14 +168,14 @@ public class RenderScript {
protected void finalize() throws Throwable
{
if (mID != 0) {
- Log.v(LOG_TAG,
+ Log.v(LOG_TAG,
"Element finalized without having released the RS reference.");
}
super.finalize();
}
}
-
+
//////////////////////////////////////////////////////////////////////////////////
// Element
@@ -470,8 +474,8 @@ public class RenderScript {
break;
default:
Log.e(LOG_TAG, "allocationCreateFromBitmap, unknown bitmap format");
- }
- */
+ }
+ */
srcFmt = ElementPredefined.RGBA_8888.mID;
@@ -609,7 +613,52 @@ public class RenderScript {
}
public void scriptCSetScript(String s) {
- nScriptCSetScript(s);
+ try {
+ scriptCSetScript(s.getBytes("UTF-8"));
+ } catch (java.io.UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void scriptCSetScript(byte[] utf8Bytes) {
+ scriptCSetScript(utf8Bytes, 0, utf8Bytes.length);
+ }
+
+ public void scriptCSetScript(byte[] utf8Bytes, int offset, int length) {
+ nScriptCSetScript(utf8Bytes, offset, length);
+ }
+
+ public void scriptCSetScript(Resources resources, int id) {
+ InputStream is = resources.openRawResource(id);
+ try {
+ try {
+ scriptCSetScript(is);
+ } finally {
+ is.close();
+ }
+ } catch(IOException e) {
+ throw new Resources.NotFoundException();
+ }
+ }
+
+ public void scriptCSetScript(InputStream is) throws IOException {
+ byte[] buf = new byte[1024];
+ int currentPos = 0;
+ while(true) {
+ int bytesLeft = buf.length - currentPos;
+ if (bytesLeft == 0) {
+ byte[] buf2 = new byte[buf.length * 2];
+ System.arraycopy(buf, 0, buf2, 0, buf.length);
+ buf = buf2;
+ bytesLeft = buf.length - currentPos;
+ }
+ int bytesRead = is.read(buf, currentPos, bytesLeft);
+ if (bytesRead <= 0) {
+ break;
+ }
+ currentPos += bytesRead;
+ }
+ nScriptCSetScript(buf, 0, currentPos);
}
public Script scriptCCreate() {