summaryrefslogtreecommitdiffstats
path: root/libs/rs/java/Samples
diff options
context:
space:
mode:
Diffstat (limited to 'libs/rs/java/Samples')
-rw-r--r--libs/rs/java/Samples/AndroidManifest.xml9
-rw-r--r--libs/rs/java/Samples/res/drawable/data.pngbin0 -> 97800 bytes
-rw-r--r--libs/rs/java/Samples/res/drawable/leaf.pngbin0 -> 18837 bytes
-rw-r--r--libs/rs/java/Samples/src/com/android/samples/RsRenderStates.java71
-rw-r--r--libs/rs/java/Samples/src/com/android/samples/RsRenderStatesRS.java207
-rw-r--r--libs/rs/java/Samples/src/com/android/samples/RsRenderStatesView.java94
-rw-r--r--libs/rs/java/Samples/src/com/android/samples/rsrenderstates.rs194
7 files changed, 575 insertions, 0 deletions
diff --git a/libs/rs/java/Samples/AndroidManifest.xml b/libs/rs/java/Samples/AndroidManifest.xml
index 17c6b16..be191f2 100644
--- a/libs/rs/java/Samples/AndroidManifest.xml
+++ b/libs/rs/java/Samples/AndroidManifest.xml
@@ -11,5 +11,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
+
+ <activity android:name="RsRenderStates"
+ android:label="RsStates"
+ android:theme="@android:style/Theme.Black.NoTitleBar">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
</application>
</manifest>
diff --git a/libs/rs/java/Samples/res/drawable/data.png b/libs/rs/java/Samples/res/drawable/data.png
new file mode 100644
index 0000000..8e34714
--- /dev/null
+++ b/libs/rs/java/Samples/res/drawable/data.png
Binary files differ
diff --git a/libs/rs/java/Samples/res/drawable/leaf.png b/libs/rs/java/Samples/res/drawable/leaf.png
new file mode 100644
index 0000000..3cd3775
--- /dev/null
+++ b/libs/rs/java/Samples/res/drawable/leaf.png
Binary files differ
diff --git a/libs/rs/java/Samples/src/com/android/samples/RsRenderStates.java b/libs/rs/java/Samples/src/com/android/samples/RsRenderStates.java
new file mode 100644
index 0000000..391007e
--- /dev/null
+++ b/libs/rs/java/Samples/src/com/android/samples/RsRenderStates.java
@@ -0,0 +1,71 @@
+/*
+ * 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.android.samples;
+
+import android.renderscript.RSSurfaceView;
+import android.renderscript.RenderScript;
+
+import android.app.Activity;
+import android.content.res.Configuration;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.provider.Settings.System;
+import android.util.Config;
+import android.util.Log;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.Window;
+import android.widget.Button;
+import android.widget.ListView;
+
+import java.lang.Runtime;
+
+public class RsRenderStates extends Activity {
+
+ private RsRenderStatesView 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 RsRenderStatesView(this);
+ setContentView(mView);
+ }
+
+ @Override
+ protected void onResume() {
+ // Ideally a game should implement onResume() and onPause()
+ // to take appropriate action when the activity looses focus
+ super.onResume();
+ mView.onResume();
+ }
+
+ @Override
+ protected void onPause() {
+ // Ideally a game should implement onResume() and onPause()
+ // to take appropriate action when the activity looses focus
+ super.onPause();
+ mView.onPause();
+ }
+
+}
+
diff --git a/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesRS.java b/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesRS.java
new file mode 100644
index 0000000..5a6ff23
--- /dev/null
+++ b/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesRS.java
@@ -0,0 +1,207 @@
+/*
+ * 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.android.samples;
+
+import java.io.Writer;
+
+import android.content.res.Resources;
+import android.renderscript.*;
+import android.renderscript.ProgramStore.DepthFunc;
+import android.util.Log;
+import android.graphics.BitmapFactory;
+import android.graphics.Bitmap;
+
+
+public class RsRenderStatesRS {
+
+ int mWidth;
+ int mHeight;
+
+ public RsRenderStatesRS() {
+ }
+
+ public void init(RenderScriptGL rs, Resources res, int width, int height) {
+ mRS = rs;
+ mRes = res;
+ mWidth = width;
+ mHeight = height;
+ mOptionsARGB.inScaled = false;
+ mOptionsARGB.inPreferredConfig = Bitmap.Config.ARGB_8888;
+ mMode = 0;
+ mMaxModes = 4;
+ initRS();
+ }
+
+ private Resources mRes;
+ private RenderScriptGL mRS;
+
+ private Sampler mSampler;
+
+ private ProgramStore mProgStoreBlendNone;
+ private ProgramStore mProgStoreBlendAlpha;
+ private ProgramStore mProgStoreBlendAdd;
+
+ private ProgramFragment mProgFragmentTexture;
+ private ProgramFragment mProgFragmentColor;
+ private ProgramVertex mProgVertex;
+ private ProgramVertex.MatrixAllocation mPVA;
+
+ private Allocation mTexOpaque;
+ private Allocation mTexTransparent;
+
+ private Allocation mAllocPV;
+
+ private Mesh mMbyNMesh;
+
+ Font mFontSans;
+ Font mFontSerif;
+ Font mFontSerifBold;
+ Font mFontSerifItalic;
+ Font mFontSerifBoldItalic;
+ Font mFontMono;
+
+ private Allocation mTextAlloc;
+
+ private ScriptC_Rsrenderstates mScript;
+
+ private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options();
+
+ int mMode;
+ int mMaxModes;
+
+ public void onActionDown(int x, int y) {
+ mMode ++;
+ mMode = mMode % mMaxModes;
+ mScript.set_gDisplayMode(mMode);
+ }
+
+
+ private void initProgramStore() {
+ // Use stock the stock program store object
+ mProgStoreBlendNone = ProgramStore.BlendNone_DepthNoDepth(mRS);
+
+ // Create a custom program store
+ ProgramStore.Builder builder = new ProgramStore.Builder(mRS);
+ builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
+ builder.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
+ ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
+ builder.setDitherEnable(false);
+ builder.setDepthMask(false);
+ mProgStoreBlendAlpha = builder.create();
+
+ mProgStoreBlendAdd = ProgramStore.BlendAdd_DepthNoDepth(mRS);
+
+ mScript.set_gProgStoreBlendNone(mProgStoreBlendNone);
+ mScript.set_gProgStoreBlendAlpha(mProgStoreBlendAlpha);
+ mScript.set_gProgStoreBlendAdd(mProgStoreBlendAdd);
+ }
+
+ private void initProgramFragment() {
+
+ Sampler.Builder bs = new Sampler.Builder(mRS);
+ bs.setMin(Sampler.Value.LINEAR);
+ bs.setMag(Sampler.Value.LINEAR);
+ bs.setWrapS(Sampler.Value.CLAMP);
+ bs.setWrapT(Sampler.Value.CLAMP);
+ mSampler = bs.create();
+
+ ProgramFragment.Builder texBuilder = new ProgramFragment.Builder(mRS);
+ texBuilder.setTexture(ProgramFragment.Builder.EnvMode.REPLACE,
+ ProgramFragment.Builder.Format.RGBA, 0);
+ mProgFragmentTexture = texBuilder.create();
+ mProgFragmentTexture.bindSampler(mSampler, 0);
+
+ ProgramFragment.Builder colBuilder = new ProgramFragment.Builder(mRS);
+ colBuilder.setVaryingColor(false);
+ mProgFragmentColor = colBuilder.create();
+
+ mScript.set_gProgFragmentColor(mProgFragmentColor);
+ mScript.set_gProgFragmentTexture(mProgFragmentTexture);
+ }
+
+ private void initProgramVertex() {
+ ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS);
+ mProgVertex = pvb.create();
+
+ mPVA = new ProgramVertex.MatrixAllocation(mRS);
+ mProgVertex.bindAllocation(mPVA);
+ mPVA.setupOrthoWindow(mWidth, mHeight);
+
+ mScript.set_gProgVertex(mProgVertex);
+ }
+
+ private Allocation loadTextureRGB(int id) {
+ final Allocation allocation = Allocation.createFromBitmapResource(mRS, mRes,
+ id, Element.RGB_565(mRS), false);
+ allocation.uploadToTexture(0);
+ return allocation;
+ }
+
+ private Allocation loadTextureARGB(int id) {
+ Bitmap b = BitmapFactory.decodeResource(mRes, id, mOptionsARGB);
+ final Allocation allocation = Allocation.createFromBitmap(mRS, b, Element.RGBA_8888(mRS), false);
+ allocation.uploadToTexture(0);
+ return allocation;
+ }
+
+ private void loadImages() {
+ mTexOpaque = loadTextureRGB(R.drawable.data);
+ mTexTransparent = loadTextureARGB(R.drawable.leaf);
+
+ mScript.set_gTexOpaque(mTexOpaque);
+ mScript.set_gTexTransparent(mTexTransparent);
+ }
+
+ private void initFonts() {
+ // Sans font by family name
+ mFontSans = Font.createFromFamily(mRS, mRes, "sans-serif", Font.Style.NORMAL, 8);
+ // Create font by file name
+ mFontSerif = Font.create(mRS, mRes, "DroidSerif-Regular.ttf", 8);
+ // Create fonts by family and style
+ mFontSerifBold = Font.createFromFamily(mRS, mRes, "serif", Font.Style.BOLD, 8);
+ mFontSerifItalic = Font.createFromFamily(mRS, mRes, "serif", Font.Style.ITALIC, 8);
+ mFontSerifBoldItalic = Font.createFromFamily(mRS, mRes, "serif", Font.Style.BOLD_ITALIC, 8);
+ mFontMono = Font.createFromFamily(mRS, mRes, "mono", Font.Style.NORMAL, 8);
+
+ mScript.set_gFontSans(mFontSans);
+ mScript.set_gFontSerif(mFontSerif);
+ mScript.set_gFontSerifBold(mFontSerifBold);
+ mScript.set_gFontSerifItalic(mFontSerifItalic);
+ mScript.set_gFontSerifBoldItalic(mFontSerifBoldItalic);
+ mScript.set_gFontMono(mFontMono);
+ }
+
+ private void initRS() {
+
+ mScript = new ScriptC_Rsrenderstates(mRS, mRes, R.raw.rsrenderstates, true);
+
+ initProgramStore();
+ initProgramFragment();
+ initProgramVertex();
+
+ initFonts();
+
+ loadImages();
+
+
+
+ mRS.contextBindRootScript(mScript);
+ }
+}
+
+
+
diff --git a/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesView.java b/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesView.java
new file mode 100644
index 0000000..5548de3
--- /dev/null
+++ b/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesView.java
@@ -0,0 +1,94 @@
+/*
+ * 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.android.samples;
+
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.concurrent.Semaphore;
+
+import android.renderscript.RSSurfaceView;
+import android.renderscript.RenderScript;
+import android.renderscript.RenderScriptGL;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.graphics.Bitmap;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
+import android.os.Handler;
+import android.os.Message;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.view.Surface;
+import android.view.SurfaceHolder;
+import android.view.SurfaceView;
+import android.view.KeyEvent;
+import android.view.MotionEvent;
+
+public class RsRenderStatesView extends RSSurfaceView {
+
+ public RsRenderStatesView(Context context) {
+ super(context);
+ //setFocusable(true);
+ }
+
+ private RenderScriptGL mRS;
+ private RsRenderStatesRS mRender;
+
+
+ public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
+ super.surfaceChanged(holder, format, w, h);
+ if (mRS == null) {
+ mRS = createRenderScript(true);
+ mRS.contextSetSurface(w, h, holder.getSurface());
+ mRender = new RsRenderStatesRS();
+ mRender.init(mRS, getResources(), w, h);
+ }
+ }
+
+ @Override
+ protected void onDetachedFromWindow() {
+ if(mRS != null) {
+ mRS = null;
+ destroyRenderScript();
+ }
+ }
+
+ @Override
+ public boolean onKeyDown(int keyCode, KeyEvent event)
+ {
+ // break point at here
+ // this method doesn't work when 'extends View' include 'extends ScrollView'.
+ return super.onKeyDown(keyCode, event);
+ }
+
+
+ @Override
+ public boolean onTouchEvent(MotionEvent ev)
+ {
+ boolean ret = false;
+ int act = ev.getAction();
+ if (act == ev.ACTION_DOWN) {
+ mRender.onActionDown((int)ev.getX(), (int)ev.getY());
+ ret = true;
+ }
+
+ return ret;
+ }
+}
+
+
diff --git a/libs/rs/java/Samples/src/com/android/samples/rsrenderstates.rs b/libs/rs/java/Samples/src/com/android/samples/rsrenderstates.rs
new file mode 100644
index 0000000..1526a50
--- /dev/null
+++ b/libs/rs/java/Samples/src/com/android/samples/rsrenderstates.rs
@@ -0,0 +1,194 @@
+// Copyright (C) 2009 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.
+
+#pragma version(1)
+
+#pragma rs java_package_name(com.android.samples)
+
+#include "rs_graphics.rsh"
+
+rs_program_vertex gProgVertex;
+rs_program_fragment gProgFragmentColor;
+rs_program_fragment gProgFragmentTexture;
+
+rs_program_store gProgStoreBlendNone;
+rs_program_store gProgStoreBlendAlpha;
+rs_program_store gProgStoreBlendAdd;
+
+rs_allocation gTexOpaque;
+rs_allocation gTexTransparent;
+
+rs_mesh gMbyNMesh;
+
+rs_font gFontSans;
+rs_font gFontSerif;
+rs_font gFontSerifBold;
+rs_font gFontSerifItalic;
+rs_font gFontSerifBoldItalic;
+rs_font gFontMono;
+
+int gDisplayMode;
+
+#pragma rs export_var(gProgVertex, gProgFragmentColor, gProgFragmentTexture)
+#pragma rs export_var(gProgStoreBlendNone, gProgStoreBlendAlpha, gProgStoreBlendAdd)
+#pragma rs export_var(gTexOpaque, gTexTransparent)
+#pragma rs export_var(gMbyNMesh)
+#pragma rs export_var(gFontSans, gFontSerif, gFontSerifBold, gFontSerifItalic, gFontSerifBoldItalic, gFontMono)
+
+//What we are showing
+#pragma rs export_var(gDisplayMode)
+
+void init() {
+}
+
+void displayFontSamples() {
+ rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
+ int yPos = 30;
+ rsgBindFont(gFontSans);
+ rsgDrawText("Sans font sample", 30, yPos);
+ yPos += 30;
+ rsgFontColor(0.5f, 0.9f, 0.5f, 1.0f);
+ rsgBindFont(gFontSerif);
+ rsgDrawText("Serif font sample", 30, yPos);
+ yPos += 30;
+ rsgFontColor(0.7f, 0.7f, 0.7f, 1.0f);
+ rsgBindFont(gFontSerifBold);
+ rsgDrawText("Serif Bold font sample", 30, yPos);
+ yPos += 30;
+ rsgFontColor(0.5f, 0.5f, 0.9f, 1.0f);
+ rsgBindFont(gFontSerifItalic);
+ rsgDrawText("Serif Italic font sample", 30, yPos);
+ yPos += 30;
+ rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
+ rsgBindFont(gFontSerifBoldItalic);
+ rsgDrawText("Serif Bold Italic font sample", 30, yPos);
+ yPos += 30;
+ rsgBindFont(gFontMono);
+ rsgDrawText("Monospace font sample", 30, yPos);
+}
+
+void displayShaderSamples() {
+ // Default vertex sahder
+ rsgBindProgramVertex(gProgVertex);
+ rs_matrix4x4 matrix;
+ rsMatrixLoadIdentity(&matrix);
+ rsgProgramVertexLoadModelMatrix(&matrix);
+
+ // Fragment shader with texture
+ rsgBindProgramStore(gProgStoreBlendNone);
+ rsgBindProgramFragment(gProgFragmentTexture);
+ rsgBindTexture(gProgFragmentTexture, 0, gTexOpaque);
+
+ rsgDrawQuadTexCoords(0, 0, 0, 0, 0,
+ 0, 256, 0, 0, 1,
+ 256, 256, 0, 1, 1,
+ 256, 0, 0, 1, 0);
+
+ rsgDrawQuadTexCoords(200, 0, 0, 0, 0,
+ 200, 128, 0, 0, 1,
+ 328, 128, 0, 1, 1,
+ 328, 0, 0, 1, 0);
+
+ rsgBindProgramStore(gProgStoreBlendAlpha);
+ rsgBindTexture(gProgFragmentTexture, 0, gTexTransparent);
+ rsgDrawQuadTexCoords(0, 200, 0, 0, 0,
+ 0, 328, 0, 0, 1,
+ 128, 328, 0, 1, 1,
+ 128, 200, 0, 1, 0);
+
+ // Fragment program with simple color
+ rsgBindProgramFragment(gProgFragmentColor);
+ rsgProgramFragmentConstantColor(gProgFragmentColor, 0.9, 0.3, 0.3, 1);
+ rsgDrawRect(200, 300, 350, 450, 0);
+ rsgProgramFragmentConstantColor(gProgFragmentColor, 0.3, 0.9, 0.3, 1);
+ rsgDrawRect(50, 400, 400, 600, 0);
+
+ rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
+ rsgBindFont(gFontMono);
+ rsgDrawText("Texture shader", 10, 50);
+ rsgDrawText("Alpha-blended texture shader", 10, 280);
+ rsgDrawText("Flat color shader", 100, 450);
+}
+
+void displayBlendingSamples() {
+ int i;
+
+ rsgBindProgramVertex(gProgVertex);
+ rs_matrix4x4 matrix;
+ rsMatrixLoadIdentity(&matrix);
+ rsgProgramVertexLoadModelMatrix(&matrix);
+
+ rsgBindProgramFragment(gProgFragmentColor);
+
+ rsgBindProgramStore(gProgStoreBlendNone);
+ for(i = 0; i < 3; i ++) {
+ float iPlusOne = (float)(i + 1);
+ rsgProgramFragmentConstantColor(gProgFragmentColor,
+ 0.1f*iPlusOne, 0.2f*iPlusOne, 0.3f*iPlusOne, 1);
+ float yPos = 150 * (float)i;
+ rsgDrawRect(0, yPos, 200, yPos + 200, 0);
+ }
+
+ rsgBindProgramStore(gProgStoreBlendAlpha);
+ for(i = 0; i < 3; i ++) {
+ float iPlusOne = (float)(i + 1);
+ rsgProgramFragmentConstantColor(gProgFragmentColor,
+ 0.2f*iPlusOne, 0.3f*iPlusOne, 0.1f*iPlusOne, 0.5);
+ float yPos = 150 * (float)i;
+ rsgDrawRect(150, yPos, 350, yPos + 200, 0);
+ }
+
+ rsgBindProgramStore(gProgStoreBlendAdd);
+ for(i = 0; i < 3; i ++) {
+ float iPlusOne = (float)(i + 1);
+ rsgProgramFragmentConstantColor(gProgFragmentColor,
+ 0.3f*iPlusOne, 0.1f*iPlusOne, 0.2f*iPlusOne, 0.5);
+ float yPos = 150 * (float)i;
+ rsgDrawRect(300, yPos, 500, yPos + 200, 0);
+ }
+
+
+ rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
+ rsgBindFont(gFontMono);
+ rsgDrawText("No Blending", 10, 50);
+ rsgDrawText("Alpha Blending", 160, 150);
+ rsgDrawText("Additive Blending", 320, 250);
+
+}
+
+void displayMeshSamples() {
+}
+
+int root(int launchID) {
+
+ rsgClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ rsgClearDepth(1.0f);
+
+ switch(gDisplayMode) {
+ case 0:
+ displayFontSamples();
+ break;
+ case 1:
+ displayShaderSamples();
+ break;
+ case 2:
+ displayBlendingSamples();
+ break;
+ case 3:
+ displayMeshSamples();
+ break;
+ }
+
+ return 10;
+}