adding fountain framebuffer object sdk sample
Change-Id: I0156281baf450b4aafcd1d43bf5fd9ef8d4cb6cc
diff --git a/build/sdk.atree b/build/sdk.atree
index 8742b6a..53970a0 100644
--- a/build/sdk.atree
+++ b/build/sdk.atree
@@ -194,6 +194,7 @@
development/samples/XmlAdapters samples/${PLATFORM_NAME}/XmlAdapters
development/samples/RenderScript/Balls samples/${PLATFORM_NAME}/RenderScript/Balls
development/samples/RenderScript/Fountain samples/${PLATFORM_NAME}/RenderScript/Fountain
+development/samples/RenderScript/FountainFbo samples/${PLATFORM_NAME}/RenderScript/FountainFbo
development/samples/RenderScript/HelloCompute samples/${PLATFORM_NAME}/RenderScript/HelloCompute
development/samples/RenderScript/HelloWorld samples/${PLATFORM_NAME}/RenderScript/HelloWorld
development/samples/RenderScript/MiscSamples samples/${PLATFORM_NAME}/RenderScript/MiscSamples
diff --git a/samples/RenderScript/FountainFbo/Android.mk b/samples/RenderScript/FountainFbo/Android.mk
new file mode 100644
index 0000000..55a4fd8
--- /dev/null
+++ b/samples/RenderScript/FountainFbo/Android.mk
@@ -0,0 +1,29 @@
+#
+# Copyright (C) 2008 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.
+#
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src) $(call all-renderscript-files-under, src)
+
+# TODO: build fails with this set
+# LOCAL_SDK_VERSION := current
+
+LOCAL_PACKAGE_NAME := RsFountainFbo
+
+include $(BUILD_PACKAGE)
diff --git a/samples/RenderScript/FountainFbo/AndroidManifest.xml b/samples/RenderScript/FountainFbo/AndroidManifest.xml
new file mode 100644
index 0000000..082744b
--- /dev/null
+++ b/samples/RenderScript/FountainFbo/AndroidManifest.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.example.android.rs.fountainfbo">
+ <uses-sdk android:minSdkVersion="14" />
+ <application
+ android:label="RsFountainFbo"
+ android:hardwareAccelerated="true"
+ android:icon="@drawable/test_pattern">
+ <activity android:name="FountainFbo">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ </application>
+</manifest>
diff --git a/samples/RenderScript/FountainFbo/_index.html b/samples/RenderScript/FountainFbo/_index.html
new file mode 100644
index 0000000..5508657
--- /dev/null
+++ b/samples/RenderScript/FountainFbo/_index.html
@@ -0,0 +1,7 @@
+<p>An example that renders many dots on the screen that follow a user's touch. The dots fall
+to the bottom of the screen when no touch is detected. This example modifies
+the <a href="../Fountain/index.html">Fountain</a> sample to include rendering to a
+a framebuffer object as well as the default framebuffer.</p>
+
+
+
diff --git a/samples/RenderScript/FountainFbo/res/drawable/test_pattern.png b/samples/RenderScript/FountainFbo/res/drawable/test_pattern.png
new file mode 100644
index 0000000..e7d1455
--- /dev/null
+++ b/samples/RenderScript/FountainFbo/res/drawable/test_pattern.png
Binary files differ
diff --git a/samples/RenderScript/FountainFbo/src/com/example/android/rs/fountainfbo/FountainFbo.java b/samples/RenderScript/FountainFbo/src/com/example/android/rs/fountainfbo/FountainFbo.java
new file mode 100644
index 0000000..d8ba30f
--- /dev/null
+++ b/samples/RenderScript/FountainFbo/src/com/example/android/rs/fountainfbo/FountainFbo.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2008 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.
+ */
+
+package com.example.android.rs.fountainfbo;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.util.Log;
+
+public class FountainFbo extends Activity {
+ private static final String LOG_TAG = "libRS_jni";
+ private static final boolean DEBUG = false;
+ private static final boolean LOG_ENABLED = false;
+
+ private FountainFboView mView;
+
+ @Override
+ public void onCreate(Bundle icicle) {
+ super.onCreate(icicle);
+
+ /* Create our Preview view and set it as the content of our Activity */
+ mView = new FountainFboView(this);
+ setContentView(mView);
+ }
+
+ @Override
+ protected void onResume() {
+ Log.e("rs", "onResume");
+
+ /* Ideally a game should implement onResume() and onPause()
+ to take appropriate action when the activity loses focus */
+ super.onResume();
+ mView.resume();
+ }
+
+ @Override
+ protected void onPause() {
+ Log.e("rs", "onPause");
+
+ /* Ideally a game should implement onResume() and onPause()
+ to take appropriate action when the activity loses focus */
+ super.onPause();
+ mView.pause();
+ }
+
+ static void log(String message) {
+ if (LOG_ENABLED) {
+ Log.v(LOG_TAG, message);
+ }
+ }
+}
+
diff --git a/samples/RenderScript/FountainFbo/src/com/example/android/rs/fountainfbo/FountainFboRS.java b/samples/RenderScript/FountainFbo/src/com/example/android/rs/fountainfbo/FountainFboRS.java
new file mode 100644
index 0000000..3bf3ff1
--- /dev/null
+++ b/samples/RenderScript/FountainFbo/src/com/example/android/rs/fountainfbo/FountainFboRS.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2008 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.
+ */
+
+package com.example.android.rs.fountainfbo;
+
+import android.content.res.Resources;
+import android.renderscript.Allocation;
+import android.renderscript.Element;
+import android.renderscript.Mesh;
+import android.renderscript.ProgramFragment;
+import android.renderscript.ProgramFragmentFixedFunction;
+import android.renderscript.RenderScriptGL;
+import android.renderscript.Type;
+
+public class FountainFboRS {
+ public static final int PART_COUNT = 50000;
+
+ public FountainFboRS() {
+ }
+
+ private Resources mRes;
+ private RenderScriptGL mRS;
+ private ScriptC_fountainfbo mScript;
+ private Allocation mColorBuffer;
+ private ProgramFragment mProgramFragment;
+ private ProgramFragment mTextureProgramFragment;
+ public void init(RenderScriptGL rs, Resources res) {
+ mRS = rs;
+ mRes = res;
+
+ ScriptField_Point points = new ScriptField_Point(mRS, PART_COUNT);
+
+ Mesh.AllocationBuilder smb = new Mesh.AllocationBuilder(mRS);
+ smb.addVertexAllocation(points.getAllocation());
+ smb.addIndexSetType(Mesh.Primitive.POINT);
+ Mesh sm = smb.create();
+
+ mScript = new ScriptC_fountainfbo(mRS, mRes, R.raw.fountainfbo);
+ mScript.set_partMesh(sm);
+ mScript.bind_point(points);
+
+ ProgramFragmentFixedFunction.Builder pfb = new ProgramFragmentFixedFunction.Builder(rs);
+ pfb.setVaryingColor(true);
+ mProgramFragment = pfb.create();
+ mScript.set_gProgramFragment(mProgramFragment);
+
+ /* Second fragment shader to use a texture (framebuffer object) to draw with */
+ pfb.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
+ ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
+
+ /* Set the fragment shader in the Renderscript runtime */
+ mTextureProgramFragment = pfb.create();
+ mScript.set_gTextureProgramFragment(mTextureProgramFragment);
+
+ /* Create the allocation for the color buffer */
+ Type.Builder colorBuilder = new Type.Builder(mRS, Element.RGBA_8888(mRS));
+ colorBuilder.setX(256).setY(256);
+ mColorBuffer = Allocation.createTyped(mRS, colorBuilder.create(),
+ Allocation.USAGE_GRAPHICS_TEXTURE |
+ Allocation.USAGE_GRAPHICS_RENDER_TARGET);
+
+ /* Set the allocation in the Renderscript runtime */
+ mScript.set_gColorBuffer(mColorBuffer);
+
+ mRS.bindRootScript(mScript);
+ }
+
+ boolean holdingColor[] = new boolean[10];
+ public void newTouchPosition(float x, float y, float pressure, int id) {
+ if (id >= holdingColor.length) {
+ return;
+ }
+ int rate = (int)(pressure * pressure * 500.f);
+ if (rate > 500) {
+ rate = 500;
+ }
+ if (rate > 0) {
+ mScript.invoke_addParticles(rate, x, y, id, !holdingColor[id]);
+ holdingColor[id] = true;
+ } else {
+ holdingColor[id] = false;
+ }
+
+ }
+}
+
diff --git a/samples/RenderScript/FountainFbo/src/com/example/android/rs/fountainfbo/FountainFboView.java b/samples/RenderScript/FountainFbo/src/com/example/android/rs/fountainfbo/FountainFboView.java
new file mode 100644
index 0000000..6e40da3
--- /dev/null
+++ b/samples/RenderScript/FountainFbo/src/com/example/android/rs/fountainfbo/FountainFboView.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2008 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.
+ */
+
+package com.example.android.rs.fountainfbo;
+
+
+import android.renderscript.RSTextureView;
+import android.renderscript.RenderScriptGL;
+import android.content.Context;
+import android.view.MotionEvent;
+
+public class FountainFboView extends RSTextureView {
+
+ public FountainFboView(Context context) {
+ super(context);
+ }
+
+ private RenderScriptGL mRS;
+ private FountainFboRS mRender;
+
+ @Override
+ protected void onAttachedToWindow() {
+ super.onAttachedToWindow();
+ android.util.Log.e("rs", "onAttachedToWindow");
+ if (mRS == null) {
+ RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
+ mRS = createRenderScriptGL(sc);
+ mRender = new FountainFboRS();
+ mRender.init(mRS, getResources());
+ }
+ }
+
+ @Override
+ protected void onDetachedFromWindow() {
+ super.onDetachedFromWindow();
+ android.util.Log.e("rs", "onDetachedFromWindow");
+ if (mRS != null) {
+ mRS = null;
+ destroyRenderScriptGL();
+ }
+ }
+
+
+ @Override
+ public boolean onTouchEvent(MotionEvent ev)
+ {
+ int act = ev.getActionMasked();
+ if (act == ev.ACTION_UP) {
+ mRender.newTouchPosition(0, 0, 0, ev.getPointerId(0));
+ return false;
+ } else if (act == MotionEvent.ACTION_POINTER_UP) {
+ // only one pointer going up, we can get the index like this
+ int pointerIndex = ev.getActionIndex();
+ int pointerId = ev.getPointerId(pointerIndex);
+ mRender.newTouchPosition(0, 0, 0, pointerId);
+ }
+ int count = ev.getHistorySize();
+ int pcount = ev.getPointerCount();
+
+ for (int p=0; p < pcount; p++) {
+ int id = ev.getPointerId(p);
+ mRender.newTouchPosition(ev.getX(p),
+ ev.getY(p),
+ ev.getPressure(p),
+ id);
+
+ for (int i=0; i < count; i++) {
+ mRender.newTouchPosition(ev.getHistoricalX(p, i),
+ ev.getHistoricalY(p, i),
+ ev.getHistoricalPressure(p, i),
+ id);
+ }
+ }
+ return true;
+ }
+}
+
+
diff --git a/samples/RenderScript/FountainFbo/src/com/example/android/rs/fountainfbo/fountainfbo.rs b/samples/RenderScript/FountainFbo/src/com/example/android/rs/fountainfbo/fountainfbo.rs
new file mode 100644
index 0000000..763f6ba
--- /dev/null
+++ b/samples/RenderScript/FountainFbo/src/com/example/android/rs/fountainfbo/fountainfbo.rs
@@ -0,0 +1,106 @@
+// Fountain test script
+#pragma version(1)
+
+#pragma rs java_package_name(com.example.android.rs.fountainfbo)
+
+#pragma stateFragment(parent)
+
+#include "rs_graphics.rsh"
+
+static int newPart = 0;
+rs_mesh partMesh;
+rs_program_vertex gProgramVertex;
+
+//allocation for color buffer
+rs_allocation gColorBuffer;
+//fragment shader for rendering without a texture (used for rendering to framebuffer object)
+rs_program_fragment gProgramFragment;
+//fragment shader for rendering with a texture (used for rendering to default framebuffer)
+rs_program_fragment gTextureProgramFragment;
+
+typedef struct __attribute__((packed, aligned(4))) Point {
+ float2 delta;
+ float2 position;
+ uchar4 color;
+} Point_t;
+Point_t *point;
+
+int root() {
+ float dt = min(rsGetDt(), 0.1f);
+ rsgClearColor(0.f, 0.f, 0.f, 1.f);
+ const float height = rsgGetHeight();
+ const int size = rsAllocationGetDimX(rsGetAllocation(point));
+ float dy2 = dt * (10.f);
+ Point_t * p = point;
+ for (int ct=0; ct < size; ct++) {
+ p->delta.y += dy2;
+ p->position += p->delta;
+ if ((p->position.y > height) && (p->delta.y > 0)) {
+ p->delta.y *= -0.3f;
+ }
+ p++;
+ }
+ //Tell Renderscript runtime to render to the frame buffer object
+ rsgBindColorTarget(gColorBuffer, 0);
+
+ //Begin rendering on a white background
+ rsgClearColor(1.f, 1.f, 1.f, 1.f);
+ rsgDrawMesh(partMesh);
+
+ //When done, tell Renderscript runtime to stop rendering to framebuffer object
+ rsgClearAllRenderTargets();
+
+ //Bind a new fragment shader that declares the framebuffer object to be used as a texture
+ rsgBindProgramFragment(gTextureProgramFragment);
+
+ //Bind the framebuffer object to the fragment shader at slot 0 as a texture
+ rsgBindTexture(gTextureProgramFragment, 0, gColorBuffer);
+
+ //Draw a quad using the framebuffer object as the texture
+ float startX = 10, startY = 10;
+ float s = 256;
+ rsgDrawQuadTexCoords(startX, startY, 0, 0, 1,
+ startX, startY + s, 0, 0, 0,
+ startX + s, startY + s, 0, 1, 0,
+ startX + s, startY, 0, 1, 1);
+
+ //Rebind the original fragment shader to render as normal
+ rsgBindProgramFragment(gProgramFragment);
+
+ //Render the main scene
+ rsgDrawMesh(partMesh);
+
+ return 1;
+}
+
+static float4 partColor[10];
+void addParticles(int rate, float x, float y, int index, bool newColor)
+{
+ if (newColor) {
+ partColor[index].x = rsRand(0.5f, 1.0f);
+ partColor[index].y = rsRand(1.0f);
+ partColor[index].z = rsRand(1.0f);
+ }
+ float rMax = ((float)rate) * 0.02f;
+ int size = rsAllocationGetDimX(rsGetAllocation(point));
+ uchar4 c = rsPackColorTo8888(partColor[index]);
+
+ Point_t * np = &point[newPart];
+ float2 p = {x, y};
+ while (rate--) {
+ float angle = rsRand(3.14f * 2.f);
+ float len = rsRand(rMax);
+ np->delta.x = len * sin(angle);
+ np->delta.y = len * cos(angle);
+ np->position = p;
+ np->color = c;
+ newPart++;
+ np++;
+ if (newPart >= size) {
+ newPart = 0;
+ np = &point[newPart];
+ }
+ }
+}
+
+