am 7541a9a2: am f1ff8b2f: Merge "Cleanup of the code in view to do creation/destruction in the right places." into honeycomb

* commit '7541a9a25036410f17718d86fe57ea33c0f789d3':
  Cleanup of the code in view to do creation/destruction in the right places.
diff --git a/Android.mk b/Android.mk
index 3835852..25c8beb 100644
--- a/Android.mk
+++ b/Android.mk
@@ -130,6 +130,28 @@
 
 include $(BUILD_SHARED_LIBRARY)
 
+# Now build a host version for serialization
+include $(CLEAR_VARS)
+LOCAL_CFLAGS += -DANDROID_RS_SERIALIZE
+
+LOCAL_SRC_FILES:= \
+	rsAllocation.cpp \
+	rsComponent.cpp \
+	rsElement.cpp \
+	rsFileA3D.cpp \
+	rsObjectBase.cpp \
+	rsMesh.cpp \
+	rsStream.cpp \
+	rsType.cpp
+
+LOCAL_STATIC_LIBRARIES := libcutils libutils
+
+LOCAL_LDLIBS := -lpthread
+LOCAL_MODULE:= libRSserialize
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_HOST_STATIC_LIBRARY)
+
 # include the java examples
 include $(addprefix $(LOCAL_PATH)/,$(addsuffix /Android.mk,\
     java \
diff --git a/RenderScript.h b/RenderScript.h
index f160ef1..bb5e4aa 100644
--- a/RenderScript.h
+++ b/RenderScript.h
@@ -365,6 +365,9 @@
 RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
                                                RsAllocationMipmapControl mips,
                                                const void *data, uint32_t usages);
+#ifdef ANDROID_RS_SERIALIZE
+#define NO_RS_FUNCS
+#endif
 
 #ifndef NO_RS_FUNCS
 #include "rsgApiFuncDecl.h"
diff --git a/java/Samples/AndroidManifest.xml b/java/Samples/AndroidManifest.xml
index c08a264..8dad161 100644
--- a/java/Samples/AndroidManifest.xml
+++ b/java/Samples/AndroidManifest.xml
@@ -21,5 +21,14 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+
+        <activity android:name="RsBench"
+                  android:label="RsBenchmark"
+                  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/java/Samples/src/com/android/samples/RsBench.java b/java/Samples/src/com/android/samples/RsBench.java
new file mode 100644
index 0000000..a29dddc
--- /dev/null
+++ b/java/Samples/src/com/android/samples/RsBench.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 RsBench extends Activity {
+
+    private RsBenchView 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 RsBenchView(this);
+        setContentView(mView);
+    }
+
+    @Override
+    protected void 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() {
+        // Ideally a game should implement onResume() and onPause()
+        // to take appropriate action when the activity loses focus
+        super.onPause();
+        mView.pause();
+    }
+
+}
+
diff --git a/java/Samples/src/com/android/samples/RsBenchRS.java b/java/Samples/src/com/android/samples/RsBenchRS.java
new file mode 100644
index 0000000..1afcee3
--- /dev/null
+++ b/java/Samples/src/com/android/samples/RsBenchRS.java
@@ -0,0 +1,429 @@
+/*
+ * 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.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.renderscript.*;
+import android.renderscript.Allocation.MipmapControl;
+import android.renderscript.Program.TextureType;
+import android.renderscript.ProgramStore.DepthFunc;
+import android.renderscript.ProgramStore.BlendSrcFunc;
+import android.renderscript.ProgramStore.BlendDstFunc;
+import android.renderscript.Sampler.Value;
+import android.util.Log;
+
+
+public class RsBenchRS {
+
+    int mWidth;
+    int mHeight;
+
+    public RsBenchRS() {
+    }
+
+    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 = 0;
+        initRS();
+    }
+
+    private Resources mRes;
+    private RenderScriptGL mRS;
+
+    private Sampler mLinearClamp;
+    private Sampler mLinearWrap;
+    private Sampler mMipLinearWrap;
+    private Sampler mNearestClamp;
+    private Sampler mMipLinearAniso8;
+    private Sampler mMipLinearAniso15;
+
+    private ProgramStore mProgStoreBlendNoneDepth;
+    private ProgramStore mProgStoreBlendNone;
+    private ProgramStore mProgStoreBlendAlpha;
+    private ProgramStore mProgStoreBlendAdd;
+
+    private ProgramFragment mProgFragmentTexture;
+    private ProgramFragment mProgFragmentColor;
+
+    private ProgramVertex mProgVertex;
+    private ProgramVertexFixedFunction.Constants mPVA;
+
+    // Custom shaders
+    private ProgramVertex mProgVertexCustom;
+    private ProgramFragment mProgFragmentCustom;
+    private ProgramFragment mProgFragmentMultitex;
+    private ProgramVertex mProgVertexPixelLight;
+    private ProgramVertex mProgVertexPixelLightMove;
+    private ProgramFragment mProgFragmentPixelLight;
+    private ScriptField_VertexShaderConstants_s mVSConst;
+    private ScriptField_FragentShaderConstants_s mFSConst;
+    private ScriptField_VertexShaderConstants3_s mVSConstPixel;
+    private ScriptField_FragentShaderConstants3_s mFSConstPixel;
+
+    private ProgramVertex mProgVertexCube;
+    private ProgramFragment mProgFragmentCube;
+
+    private ProgramRaster mCullBack;
+    private ProgramRaster mCullFront;
+    private ProgramRaster mCullNone;
+
+    private Allocation mTexTorus;
+    private Allocation mTexOpaque;
+    private Allocation mTexTransparent;
+    private Allocation mTexChecker;
+    private Allocation mTexCube;
+
+    private Mesh m10by10Mesh;
+    private Mesh m100by100Mesh;
+    private Mesh mWbyHMesh;
+    private Mesh mTorus;
+
+    Font mFontSans;
+    Font mFontSerif;
+    Font mFontSerifBold;
+    Font mFontSerifItalic;
+    Font mFontSerifBoldItalic;
+    Font mFontMono;
+    private Allocation mTextAlloc;
+
+    private ScriptC_rsbench 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);
+    }
+
+    ProgramStore BLEND_ADD_DEPTH_NONE(RenderScript rs) {
+        ProgramStore.Builder builder = new ProgramStore.Builder(rs);
+        builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
+        builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE);
+        builder.setDitherEnabled(false);
+        builder.setDepthMaskEnabled(false);
+        return builder.create();
+    }
+
+    private Mesh getMbyNMesh(float width, float height, int wResolution, int hResolution) {
+
+        Mesh.TriangleMeshBuilder tmb = new Mesh.TriangleMeshBuilder(mRS,
+                                           2, Mesh.TriangleMeshBuilder.TEXTURE_0);
+
+        for (int y = 0; y <= hResolution; y++) {
+            final float normalizedY = (float)y / hResolution;
+            final float yOffset = (normalizedY - 0.5f) * height;
+            for (int x = 0; x <= wResolution; x++) {
+                float normalizedX = (float)x / wResolution;
+                float xOffset = (normalizedX - 0.5f) * width;
+                tmb.setTexture((float)x % 2, (float)y % 2);
+                tmb.addVertex(xOffset, yOffset);
+             }
+        }
+
+        for (int y = 0; y < hResolution; y++) {
+            final int curY = y * (wResolution + 1);
+            final int belowY = (y + 1) * (wResolution + 1);
+            for (int x = 0; x < wResolution; x++) {
+                int curV = curY + x;
+                int belowV = belowY + x;
+                tmb.addTriangle(curV, belowV, curV + 1);
+                tmb.addTriangle(belowV, belowV + 1, curV + 1);
+            }
+        }
+
+        return tmb.create(true);
+    }
+
+    private void initProgramStore() {
+        // Use stock the stock program store object
+        mProgStoreBlendNoneDepth = ProgramStore.BLEND_NONE_DEPTH_TEST(mRS);
+        mProgStoreBlendNone = ProgramStore.BLEND_NONE_DEPTH_NONE(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.setDitherEnabled(false);
+        builder.setDepthMaskEnabled(false);
+        mProgStoreBlendAlpha = builder.create();
+
+        mProgStoreBlendAdd = BLEND_ADD_DEPTH_NONE(mRS);
+
+        mScript.set_gProgStoreBlendNoneDepth(mProgStoreBlendNoneDepth);
+        mScript.set_gProgStoreBlendNone(mProgStoreBlendNone);
+        mScript.set_gProgStoreBlendAlpha(mProgStoreBlendAlpha);
+        mScript.set_gProgStoreBlendAdd(mProgStoreBlendAdd);
+    }
+
+    private void initProgramFragment() {
+
+        ProgramFragmentFixedFunction.Builder texBuilder = new ProgramFragmentFixedFunction.Builder(mRS);
+        texBuilder.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
+                              ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
+        mProgFragmentTexture = texBuilder.create();
+        mProgFragmentTexture.bindSampler(mLinearClamp, 0);
+
+        ProgramFragmentFixedFunction.Builder colBuilder = new ProgramFragmentFixedFunction.Builder(mRS);
+        colBuilder.setVaryingColor(false);
+        mProgFragmentColor = colBuilder.create();
+
+        mScript.set_gProgFragmentColor(mProgFragmentColor);
+        mScript.set_gProgFragmentTexture(mProgFragmentTexture);
+    }
+
+    private void initProgramVertex() {
+        ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS);
+        mProgVertex = pvb.create();
+
+        mPVA = new ProgramVertexFixedFunction.Constants(mRS);
+        ((ProgramVertexFixedFunction)mProgVertex).bindConstants(mPVA);
+        Matrix4f proj = new Matrix4f();
+        proj.loadOrthoWindow(mWidth, mHeight);
+        mPVA.setProjection(proj);
+
+        mScript.set_gProgVertex(mProgVertex);
+    }
+
+    private void initCustomShaders() {
+        mVSConst = new ScriptField_VertexShaderConstants_s(mRS, 1);
+        mFSConst = new ScriptField_FragentShaderConstants_s(mRS, 1);
+        mScript.bind_gVSConstants(mVSConst);
+        mScript.bind_gFSConstants(mFSConst);
+
+        mVSConstPixel = new ScriptField_VertexShaderConstants3_s(mRS, 1);
+        mFSConstPixel = new ScriptField_FragentShaderConstants3_s(mRS, 1);
+        mScript.bind_gVSConstPixel(mVSConstPixel);
+        mScript.bind_gFSConstPixel(mFSConstPixel);
+
+        // Initialize the shader builder
+        ProgramVertex.Builder pvbCustom = new ProgramVertex.Builder(mRS);
+        // Specify the resource that contains the shader string
+        pvbCustom.setShader(mRes, R.raw.shaderv);
+        // Use a script field to specify the input layout
+        pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
+        // Define the constant input layout
+        pvbCustom.addConstant(mVSConst.getAllocation().getType());
+        mProgVertexCustom = pvbCustom.create();
+        // Bind the source of constant data
+        mProgVertexCustom.bindConstants(mVSConst.getAllocation(), 0);
+
+        ProgramFragment.Builder pfbCustom = new ProgramFragment.Builder(mRS);
+        // Specify the resource that contains the shader string
+        pfbCustom.setShader(mRes, R.raw.shaderf);
+        // Tell the builder how many textures we have
+        pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
+        // Define the constant input layout
+        pfbCustom.addConstant(mFSConst.getAllocation().getType());
+        mProgFragmentCustom = pfbCustom.create();
+        // Bind the source of constant data
+        mProgFragmentCustom.bindConstants(mFSConst.getAllocation(), 0);
+
+        // Cubemap test shaders
+        pvbCustom = new ProgramVertex.Builder(mRS);
+        pvbCustom.setShader(mRes, R.raw.shadercubev);
+        pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
+        pvbCustom.addConstant(mVSConst.getAllocation().getType());
+        mProgVertexCube = pvbCustom.create();
+        mProgVertexCube.bindConstants(mVSConst.getAllocation(), 0);
+
+        pfbCustom = new ProgramFragment.Builder(mRS);
+        pfbCustom.setShader(mRes, R.raw.shadercubef);
+        pfbCustom.addTexture(Program.TextureType.TEXTURE_CUBE);
+        mProgFragmentCube = pfbCustom.create();
+
+        pvbCustom = new ProgramVertex.Builder(mRS);
+        pvbCustom.setShader(mRes, R.raw.shader2v);
+        pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
+        pvbCustom.addConstant(mVSConstPixel.getAllocation().getType());
+        mProgVertexPixelLight = pvbCustom.create();
+        mProgVertexPixelLight.bindConstants(mVSConstPixel.getAllocation(), 0);
+
+        pvbCustom = new ProgramVertex.Builder(mRS);
+        pvbCustom.setShader(mRes, R.raw.shader2movev);
+        pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
+        pvbCustom.addConstant(mVSConstPixel.getAllocation().getType());
+        mProgVertexPixelLightMove = pvbCustom.create();
+        mProgVertexPixelLightMove.bindConstants(mVSConstPixel.getAllocation(), 0);
+
+        pfbCustom = new ProgramFragment.Builder(mRS);
+        pfbCustom.setShader(mRes, R.raw.shader2f);
+        pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
+        pfbCustom.addConstant(mFSConstPixel.getAllocation().getType());
+        mProgFragmentPixelLight = pfbCustom.create();
+        mProgFragmentPixelLight.bindConstants(mFSConstPixel.getAllocation(), 0);
+
+        pfbCustom = new ProgramFragment.Builder(mRS);
+        pfbCustom.setShader(mRes, R.raw.multitexf);
+        for (int texCount = 0; texCount < 3; texCount ++) {
+            pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
+        }
+        mProgFragmentMultitex = pfbCustom.create();
+
+        mScript.set_gProgVertexCustom(mProgVertexCustom);
+        mScript.set_gProgFragmentCustom(mProgFragmentCustom);
+        mScript.set_gProgVertexCube(mProgVertexCube);
+        mScript.set_gProgFragmentCube(mProgFragmentCube);
+        mScript.set_gProgVertexPixelLight(mProgVertexPixelLight);
+        mScript.set_gProgVertexPixelLightMove(mProgVertexPixelLightMove);
+        mScript.set_gProgFragmentPixelLight(mProgFragmentPixelLight);
+        mScript.set_gProgFragmentMultitex(mProgFragmentMultitex);
+    }
+
+    private Allocation loadTextureRGB(int id) {
+        return Allocation.createFromBitmapResource(mRS, mRes, id,
+                Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
+                Allocation.USAGE_GRAPHICS_TEXTURE);
+    }
+
+    private Allocation loadTextureARGB(int id) {
+        Bitmap b = BitmapFactory.decodeResource(mRes, id, mOptionsARGB);
+        return Allocation.createFromBitmap(mRS, b,
+                Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
+                Allocation.USAGE_GRAPHICS_TEXTURE);
+    }
+
+    private void loadImages() {
+        mTexTorus = loadTextureRGB(R.drawable.torusmap);
+        mTexOpaque = loadTextureRGB(R.drawable.data);
+        mTexTransparent = loadTextureARGB(R.drawable.leaf);
+        mTexChecker = loadTextureRGB(R.drawable.checker);
+        Bitmap b = BitmapFactory.decodeResource(mRes, R.drawable.cubemap_test);
+        mTexCube = Allocation.createCubemapFromBitmap(mRS, b);
+
+        mScript.set_gTexTorus(mTexTorus);
+        mScript.set_gTexOpaque(mTexOpaque);
+        mScript.set_gTexTransparent(mTexTransparent);
+        mScript.set_gTexChecker(mTexChecker);
+        mScript.set_gTexCube(mTexCube);
+    }
+
+    private void initFonts() {
+        // Sans font by family name
+        mFontSans = Font.create(mRS, mRes, "sans-serif", Font.Style.NORMAL, 8);
+        mFontSerif = Font.create(mRS, mRes, "serif", Font.Style.NORMAL, 8);
+        // Create fonts by family and style
+        mFontSerifBold = Font.create(mRS, mRes, "serif", Font.Style.BOLD, 8);
+        mFontSerifItalic = Font.create(mRS, mRes, "serif", Font.Style.ITALIC, 8);
+        mFontSerifBoldItalic = Font.create(mRS, mRes, "serif", Font.Style.BOLD_ITALIC, 8);
+        mFontMono = Font.create(mRS, mRes, "mono", Font.Style.NORMAL, 8);
+
+        mTextAlloc = Allocation.createFromString(mRS, "String from allocation", Allocation.USAGE_SCRIPT);
+
+        mScript.set_gFontSans(mFontSans);
+        mScript.set_gFontSerif(mFontSerif);
+        mScript.set_gFontSerifBold(mFontSerifBold);
+        mScript.set_gFontSerifItalic(mFontSerifItalic);
+        mScript.set_gFontSerifBoldItalic(mFontSerifBoldItalic);
+        mScript.set_gFontMono(mFontMono);
+        mScript.set_gTextAlloc(mTextAlloc);
+    }
+
+    private void initMesh() {
+        m10by10Mesh = getMbyNMesh(mWidth, mHeight, 10, 10);
+        mScript.set_g10by10Mesh(m10by10Mesh);
+        m100by100Mesh = getMbyNMesh(mWidth, mHeight, 100, 100);
+        mScript.set_g100by100Mesh(m100by100Mesh);
+        mWbyHMesh= getMbyNMesh(mWidth, mHeight, mWidth/4, mHeight/4);
+        mScript.set_gWbyHMesh(mWbyHMesh);
+
+        FileA3D model = FileA3D.createFromResource(mRS, mRes, R.raw.torus);
+        FileA3D.IndexEntry entry = model.getIndexEntry(0);
+        if (entry == null || entry.getEntryType() != FileA3D.EntryType.MESH) {
+            Log.e("rs", "could not load model");
+        } else {
+            mTorus = (Mesh)entry.getObject();
+            mScript.set_gTorusMesh(mTorus);
+        }
+    }
+
+    private void initSamplers() {
+        Sampler.Builder bs = new Sampler.Builder(mRS);
+        bs.setMinification(Sampler.Value.LINEAR);
+        bs.setMagnification(Sampler.Value.LINEAR);
+        bs.setWrapS(Sampler.Value.WRAP);
+        bs.setWrapT(Sampler.Value.WRAP);
+        mLinearWrap = bs.create();
+
+        mLinearClamp = Sampler.CLAMP_LINEAR(mRS);
+        mNearestClamp = Sampler.CLAMP_NEAREST(mRS);
+        mMipLinearWrap = Sampler.WRAP_LINEAR_MIP_LINEAR(mRS);
+
+        bs = new Sampler.Builder(mRS);
+        bs.setMinification(Sampler.Value.LINEAR_MIP_LINEAR);
+        bs.setMagnification(Sampler.Value.LINEAR);
+        bs.setWrapS(Sampler.Value.WRAP);
+        bs.setWrapT(Sampler.Value.WRAP);
+        bs.setAnisotropy(8.0f);
+        mMipLinearAniso8 = bs.create();
+        bs.setAnisotropy(15.0f);
+        mMipLinearAniso15 = bs.create();
+
+        mScript.set_gLinearClamp(mLinearClamp);
+        mScript.set_gLinearWrap(mLinearWrap);
+        mScript.set_gMipLinearWrap(mMipLinearWrap);
+        mScript.set_gMipLinearAniso8(mMipLinearAniso8);
+        mScript.set_gMipLinearAniso15(mMipLinearAniso15);
+        mScript.set_gNearestClamp(mNearestClamp);
+    }
+
+    private void initProgramRaster() {
+        mCullBack = ProgramRaster.CULL_BACK(mRS);
+        mCullFront = ProgramRaster.CULL_FRONT(mRS);
+        mCullNone = ProgramRaster.CULL_NONE(mRS);
+
+        mScript.set_gCullBack(mCullBack);
+        mScript.set_gCullFront(mCullFront);
+        mScript.set_gCullNone(mCullNone);
+    }
+
+    private void initRS() {
+
+        mScript = new ScriptC_rsbench(mRS, mRes, R.raw.rsbench);
+
+        mMaxModes = mScript.get_gMaxModes();
+
+        initSamplers();
+        initProgramStore();
+        initProgramFragment();
+        initProgramVertex();
+        initFonts();
+        loadImages();
+        initMesh();
+        initProgramRaster();
+        initCustomShaders();
+
+        mRS.bindRootScript(mScript);
+    }
+}
+
+
+
diff --git a/java/Samples/src/com/android/samples/RsBenchView.java b/java/Samples/src/com/android/samples/RsBenchView.java
new file mode 100644
index 0000000..0a56668
--- /dev/null
+++ b/java/Samples/src/com/android/samples/RsBenchView.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.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 RsBenchView extends RSSurfaceView {
+
+    public RsBenchView(Context context) {
+        super(context);
+    }
+
+    private RenderScriptGL mRS;
+    private RsBenchRS mRender;
+
+
+    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
+        super.surfaceChanged(holder, format, w, h);
+        if (mRS == null) {
+            RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
+            sc.setDepth(16, 24);
+            mRS = createRenderScriptGL(sc);
+            mRS.setSurface(holder, w, h);
+            mRender = new RsBenchRS();
+            mRender.init(mRS, getResources(), w, h);
+        }
+    }
+
+    @Override
+    protected void onDetachedFromWindow() {
+        if (mRS != null) {
+            mRS = null;
+            destroyRenderScriptGL();
+        }
+    }
+
+    @Override
+    public boolean onKeyDown(int keyCode, KeyEvent event) {
+        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/java/Samples/src/com/android/samples/rsbench.rs b/java/Samples/src/com/android/samples/rsbench.rs
new file mode 100644
index 0000000..a1368e6
--- /dev/null
+++ b/java/Samples/src/com/android/samples/rsbench.rs
@@ -0,0 +1,789 @@
+// 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"
+#include "shader_def.rsh"
+
+const int gMaxModes = 26;
+
+rs_program_vertex gProgVertex;
+rs_program_fragment gProgFragmentColor;
+rs_program_fragment gProgFragmentTexture;
+
+rs_program_store gProgStoreBlendNoneDepth;
+rs_program_store gProgStoreBlendNone;
+rs_program_store gProgStoreBlendAlpha;
+rs_program_store gProgStoreBlendAdd;
+
+rs_allocation gTexOpaque;
+rs_allocation gTexTorus;
+rs_allocation gTexTransparent;
+rs_allocation gTexChecker;
+rs_allocation gTexCube;
+
+rs_mesh g10by10Mesh;
+rs_mesh g100by100Mesh;
+rs_mesh gWbyHMesh;
+rs_mesh gTorusMesh;
+
+rs_font gFontSans;
+rs_font gFontSerif;
+rs_font gFontSerifBold;
+rs_font gFontSerifItalic;
+rs_font gFontSerifBoldItalic;
+rs_font gFontMono;
+rs_allocation gTextAlloc;
+
+int gDisplayMode;
+
+rs_sampler gLinearClamp;
+rs_sampler gLinearWrap;
+rs_sampler gMipLinearWrap;
+rs_sampler gMipLinearAniso8;
+rs_sampler gMipLinearAniso15;
+rs_sampler gNearestClamp;
+
+rs_program_raster gCullBack;
+rs_program_raster gCullFront;
+rs_program_raster gCullNone;
+
+// Custom vertex shader compunents
+VertexShaderConstants *gVSConstants;
+FragentShaderConstants *gFSConstants;
+VertexShaderConstants3 *gVSConstPixel;
+FragentShaderConstants3 *gFSConstPixel;
+// Export these out to easily set the inputs to shader
+VertexShaderInputs *gVSInputs;
+// Custom shaders we use for lighting
+rs_program_vertex gProgVertexCustom;
+rs_program_fragment gProgFragmentCustom;
+rs_program_vertex gProgVertexPixelLight;
+rs_program_vertex gProgVertexPixelLightMove;
+rs_program_fragment gProgFragmentPixelLight;
+rs_program_vertex gProgVertexCube;
+rs_program_fragment gProgFragmentCube;
+rs_program_fragment gProgFragmentMultitex;
+
+float gDt = 0;
+
+void init() {
+}
+
+static const char *sampleText = "This is a sample of small text for performace";
+// Offsets for multiple layer of text
+static int textOffsets[] = { 0,  0, -5, -5, 5,  5, -8, -8, 8,  8};
+static float textColors[] = {1.0f, 1.0f, 1.0f, 1.0f,
+                             0.5f, 0.7f, 0.5f, 1.0f,
+                             0.7f, 0.5f, 0.5f, 1.0f,
+                             0.5f, 0.5f, 0.7f, 1.0f,
+                             0.5f, 0.6f, 0.7f, 1.0f,
+};
+
+static void displayFontSamples(int fillNum) {
+
+    rs_font fonts[5];
+    rsSetObject(&fonts[0], gFontSans);
+    rsSetObject(&fonts[1], gFontSerif);
+    rsSetObject(&fonts[2], gFontSerifBold);
+    rsSetObject(&fonts[3], gFontSerifBoldItalic);
+    rsSetObject(&fonts[4], gFontSans);
+
+    uint width = rsgGetWidth();
+    uint height = rsgGetHeight();
+    int left = 0, right = 0, top = 0, bottom = 0;
+    rsgMeasureText(sampleText, &left, &right, &top, &bottom);
+
+    int textHeight = top - bottom;
+    int textWidth = right - left;
+    int numVerticalLines = height / textHeight;
+    int yPos = top;
+
+    int xOffset = 0, yOffset = 0;
+    for(int fillI = 0; fillI < fillNum; fillI ++) {
+        rsgBindFont(fonts[fillI]);
+        xOffset = textOffsets[fillI * 2];
+        yOffset = textOffsets[fillI * 2 + 1];
+        float *colPtr = textColors + fillI * 4;
+        rsgFontColor(colPtr[0], colPtr[1], colPtr[2], colPtr[3]);
+        for (int h = 0; h < 4; h ++) {
+            yPos = top + yOffset;
+            for (int v = 0; v < numVerticalLines; v ++) {
+                rsgDrawText(sampleText, xOffset + textWidth * h, yPos);
+                yPos += textHeight;
+            }
+        }
+    }
+
+    for (int i = 0; i < 5; i ++) {
+        rsClearObject(&fonts[i]);
+    }
+}
+
+static void bindProgramVertexOrtho() {
+    // Default vertex sahder
+    rsgBindProgramVertex(gProgVertex);
+    // Setup the projection matrix
+    rs_matrix4x4 proj;
+    rsMatrixLoadOrtho(&proj, 0, rsgGetWidth(), rsgGetHeight(), 0, -500, 500);
+    rsgProgramVertexLoadProjectionMatrix(&proj);
+}
+
+static void displaySingletexFill(bool blend, int quadCount) {
+    bindProgramVertexOrtho();
+    rs_matrix4x4 matrix;
+    rsMatrixLoadIdentity(&matrix);
+    rsgProgramVertexLoadModelMatrix(&matrix);
+
+    // Fragment shader with texture
+    if (!blend) {
+        rsgBindProgramStore(gProgStoreBlendNone);
+    } else {
+        rsgBindProgramStore(gProgStoreBlendAlpha);
+    }
+    rsgBindProgramFragment(gProgFragmentTexture);
+    rsgBindSampler(gProgFragmentTexture, 0, gLinearClamp);
+    rsgBindTexture(gProgFragmentTexture, 0, gTexOpaque);
+
+    for (int i = 0; i < quadCount; i ++) {
+        float startX = 10 * i, startY = 10 * i;
+        float width = rsgGetWidth() - startX, height = rsgGetHeight() - startY;
+        rsgDrawQuadTexCoords(startX, startY, 0, 0, 0,
+                             startX, startY + height, 0, 0, 1,
+                             startX + width, startY + height, 0, 1, 1,
+                             startX + width, startY, 0, 1, 0);
+    }
+}
+
+static void displayBlendingSamples() {
+    int i;
+
+    bindProgramVertexOrtho();
+    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);
+
+}
+
+static void displayMeshSamples(int meshNum) {
+
+    bindProgramVertexOrtho();
+    rs_matrix4x4 matrix;
+    rsMatrixLoadTranslate(&matrix, rsgGetWidth()/2, rsgGetHeight()/2, 0);
+    rsgProgramVertexLoadModelMatrix(&matrix);
+
+    // Fragment shader with texture
+    rsgBindProgramStore(gProgStoreBlendNone);
+    rsgBindProgramFragment(gProgFragmentTexture);
+    rsgBindSampler(gProgFragmentTexture, 0, gLinearClamp);
+    rsgBindTexture(gProgFragmentTexture, 0, gTexOpaque);
+
+    if (meshNum == 0) {
+        rsgDrawMesh(g10by10Mesh);
+    } else if (meshNum == 1) {
+        rsgDrawMesh(g100by100Mesh);
+    } else if (meshNum == 2) {
+        rsgDrawMesh(gWbyHMesh);
+    }
+}
+
+static void displayTextureSamplers() {
+
+    bindProgramVertexOrtho();
+    rs_matrix4x4 matrix;
+    rsMatrixLoadIdentity(&matrix);
+    rsgProgramVertexLoadModelMatrix(&matrix);
+
+    // Fragment shader with texture
+    rsgBindProgramStore(gProgStoreBlendNone);
+    rsgBindProgramFragment(gProgFragmentTexture);
+    rsgBindTexture(gProgFragmentTexture, 0, gTexOpaque);
+
+    // Linear clamp
+    rsgBindSampler(gProgFragmentTexture, 0, gLinearClamp);
+    float startX = 0, startY = 0;
+    float width = 300, height = 300;
+    rsgDrawQuadTexCoords(startX, startY, 0, 0, 0,
+                         startX, startY + height, 0, 0, 1.1,
+                         startX + width, startY + height, 0, 1.1, 1.1,
+                         startX + width, startY, 0, 1.1, 0);
+
+    // Linear Wrap
+    rsgBindSampler(gProgFragmentTexture, 0, gLinearWrap);
+    startX = 0; startY = 300;
+    width = 300; height = 300;
+    rsgDrawQuadTexCoords(startX, startY, 0, 0, 0,
+                         startX, startY + height, 0, 0, 1.1,
+                         startX + width, startY + height, 0, 1.1, 1.1,
+                         startX + width, startY, 0, 1.1, 0);
+
+    // Nearest
+    rsgBindSampler(gProgFragmentTexture, 0, gNearestClamp);
+    startX = 300; startY = 0;
+    width = 300; height = 300;
+    rsgDrawQuadTexCoords(startX, startY, 0, 0, 0,
+                         startX, startY + height, 0, 0, 1.1,
+                         startX + width, startY + height, 0, 1.1, 1.1,
+                         startX + width, startY, 0, 1.1, 0);
+
+    rsgBindSampler(gProgFragmentTexture, 0, gMipLinearWrap);
+    startX = 300; startY = 300;
+    width = 300; height = 300;
+    rsgDrawQuadTexCoords(startX, startY, 0, 0, 0,
+                         startX, startY + height, 0, 0, 1.5,
+                         startX + width, startY + height, 0, 1.5, 1.5,
+                         startX + width, startY, 0, 1.5, 0);
+
+    rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
+    rsgBindFont(gFontMono);
+    rsgDrawText("Filtering: linear clamp", 10, 290);
+    rsgDrawText("Filtering: linear wrap", 10, 590);
+    rsgDrawText("Filtering: nearest clamp", 310, 290);
+    rsgDrawText("Filtering: miplinear wrap", 310, 590);
+}
+
+static float gTorusRotation = 0;
+static void updateModelMatrix(rs_matrix4x4 *matrix, void *buffer) {
+    if (buffer == 0) {
+        rsgProgramVertexLoadModelMatrix(matrix);
+    } else {
+        rsgAllocationSyncAll(rsGetAllocation(buffer));
+    }
+}
+
+static void drawToruses(int numMeshes, rs_matrix4x4 *matrix, void *buffer) {
+
+    if (numMeshes == 1) {
+        rsMatrixLoadTranslate(matrix, 0.0f, 0.0f, -7.5f);
+        rsMatrixRotate(matrix, gTorusRotation, 1.0f, 0.0f, 0.0f);
+        updateModelMatrix(matrix, buffer);
+        rsgDrawMesh(gTorusMesh);
+        return;
+    }
+
+    if (numMeshes == 2) {
+        rsMatrixLoadTranslate(matrix, -1.6f, 0.0f, -7.5f);
+        rsMatrixRotate(matrix, gTorusRotation, 1.0f, 0.0f, 0.0f);
+        updateModelMatrix(matrix, buffer);
+        rsgDrawMesh(gTorusMesh);
+
+        rsMatrixLoadTranslate(matrix, 1.6f, 0.0f, -7.5f);
+        rsMatrixRotate(matrix, gTorusRotation, 1.0f, 0.0f, 0.0f);
+        updateModelMatrix(matrix, buffer);
+        rsgDrawMesh(gTorusMesh);
+        return;
+    }
+
+    float startX = -5.0f;
+    float startY = -1.5f;
+    float startZ = -15.0f;
+    float dist = 3.2f;
+
+    for (int h = 0; h < 4; h ++) {
+        for (int v = 0; v < 2; v ++) {
+            // Position our model on the screen
+            rsMatrixLoadTranslate(matrix, startX + dist * h, startY + dist * v, startZ);
+            rsMatrixRotate(matrix, gTorusRotation, 1.0f, 0.0f, 0.0f);
+            updateModelMatrix(matrix, buffer);
+            rsgDrawMesh(gTorusMesh);
+        }
+    }
+}
+
+
+// Quick hack to get some geometry numbers
+static void displaySimpleGeoSamples(bool useTexture, int numMeshes) {
+    rsgBindProgramVertex(gProgVertex);
+    rsgBindProgramRaster(gCullBack);
+    // Setup the projection matrix with 30 degree field of view
+    rs_matrix4x4 proj;
+    float aspect = (float)rsgGetWidth() / (float)rsgGetHeight();
+    rsMatrixLoadPerspective(&proj, 30.0f, aspect, 0.1f, 100.0f);
+    rsgProgramVertexLoadProjectionMatrix(&proj);
+
+    // Fragment shader with texture
+    rsgBindProgramStore(gProgStoreBlendNoneDepth);
+    if (useTexture) {
+        rsgBindProgramFragment(gProgFragmentTexture);
+    } else {
+        rsgBindProgramFragment(gProgFragmentColor);
+        rsgProgramFragmentConstantColor(gProgFragmentColor, 0.1, 0.7, 0.1, 1);
+    }
+    rsgBindSampler(gProgFragmentTexture, 0, gLinearClamp);
+    rsgBindTexture(gProgFragmentTexture, 0, gTexTorus);
+
+    // Apply a rotation to our mesh
+    gTorusRotation += 50.0f * gDt;
+    if (gTorusRotation > 360.0f) {
+        gTorusRotation -= 360.0f;
+    }
+
+    rs_matrix4x4 matrix;
+    drawToruses(numMeshes, &matrix, 0);
+}
+
+float gLight0Rotation = 0;
+float gLight1Rotation = 0;
+
+static void setupCustomShaderLights() {
+    float4 light0Pos = {-5.0f, 5.0f, -10.0f, 1.0f};
+    float4 light1Pos = {2.0f, 5.0f, 15.0f, 1.0f};
+    float4 light0DiffCol = {0.9f, 0.7f, 0.7f, 1.0f};
+    float4 light0SpecCol = {0.9f, 0.6f, 0.6f, 1.0f};
+    float4 light1DiffCol = {0.5f, 0.5f, 0.9f, 1.0f};
+    float4 light1SpecCol = {0.5f, 0.5f, 0.9f, 1.0f};
+
+    gLight0Rotation += 50.0f * gDt;
+    if (gLight0Rotation > 360.0f) {
+        gLight0Rotation -= 360.0f;
+    }
+    gLight1Rotation -= 50.0f * gDt;
+    if (gLight1Rotation > 360.0f) {
+        gLight1Rotation -= 360.0f;
+    }
+
+    rs_matrix4x4 l0Mat;
+    rsMatrixLoadRotate(&l0Mat, gLight0Rotation, 1.0f, 0.0f, 0.0f);
+    light0Pos = rsMatrixMultiply(&l0Mat, light0Pos);
+    rs_matrix4x4 l1Mat;
+    rsMatrixLoadRotate(&l1Mat, gLight1Rotation, 0.0f, 0.0f, 1.0f);
+    light1Pos = rsMatrixMultiply(&l1Mat, light1Pos);
+
+    // Set light 0 properties
+    gVSConstants->light0_Posision = light0Pos;
+    gVSConstants->light0_Diffuse = 1.0f;
+    gVSConstants->light0_Specular = 0.5f;
+    gVSConstants->light0_CosinePower = 10.0f;
+    // Set light 1 properties
+    gVSConstants->light1_Posision = light1Pos;
+    gVSConstants->light1_Diffuse = 1.0f;
+    gVSConstants->light1_Specular = 0.7f;
+    gVSConstants->light1_CosinePower = 25.0f;
+    rsgAllocationSyncAll(rsGetAllocation(gVSConstants));
+
+    // Update fragment shader constants
+    // Set light 0 colors
+    gFSConstants->light0_DiffuseColor = light0DiffCol;
+    gFSConstants->light0_SpecularColor = light0SpecCol;
+    // Set light 1 colors
+    gFSConstants->light1_DiffuseColor = light1DiffCol;
+    gFSConstants->light1_SpecularColor = light1SpecCol;
+    rsgAllocationSyncAll(rsGetAllocation(gFSConstants));
+
+    // Set light 0 properties for per pixel lighting
+    gFSConstPixel->light0_Posision = light0Pos;
+    gFSConstPixel->light0_Diffuse = 1.0f;
+    gFSConstPixel->light0_Specular = 0.5f;
+    gFSConstPixel->light0_CosinePower = 10.0f;
+    gFSConstPixel->light0_DiffuseColor = light0DiffCol;
+    gFSConstPixel->light0_SpecularColor = light0SpecCol;
+    // Set light 1 properties
+    gFSConstPixel->light1_Posision = light1Pos;
+    gFSConstPixel->light1_Diffuse = 1.0f;
+    gFSConstPixel->light1_Specular = 0.7f;
+    gFSConstPixel->light1_CosinePower = 25.0f;
+    gFSConstPixel->light1_DiffuseColor = light1DiffCol;
+    gFSConstPixel->light1_SpecularColor = light1SpecCol;
+    rsgAllocationSyncAll(rsGetAllocation(gFSConstPixel));
+}
+
+static void displayCustomShaderSamples(int numMeshes) {
+
+    // Update vertex shader constants
+    // Load model matrix
+    // Apply a rotation to our mesh
+    gTorusRotation += 50.0f * gDt;
+    if (gTorusRotation > 360.0f) {
+        gTorusRotation -= 360.0f;
+    }
+
+    // Setup the projection matrix
+    float aspect = (float)rsgGetWidth() / (float)rsgGetHeight();
+    rsMatrixLoadPerspective(&gVSConstants->proj, 30.0f, aspect, 0.1f, 100.0f);
+    setupCustomShaderLights();
+
+    rsgBindProgramVertex(gProgVertexCustom);
+
+    // Fragment shader with texture
+    rsgBindProgramStore(gProgStoreBlendNoneDepth);
+    rsgBindProgramFragment(gProgFragmentCustom);
+    rsgBindSampler(gProgFragmentCustom, 0, gLinearClamp);
+    rsgBindTexture(gProgFragmentCustom, 0, gTexTorus);
+
+    // Use back face culling
+    rsgBindProgramRaster(gCullBack);
+
+    drawToruses(numMeshes, &gVSConstants->model, gVSConstants);
+}
+
+static void displayPixelLightSamples(int numMeshes, bool heavyVertex) {
+
+    // Update vertex shader constants
+    // Load model matrix
+    // Apply a rotation to our mesh
+    gTorusRotation += 30.0f * gDt;
+    if (gTorusRotation > 360.0f) {
+        gTorusRotation -= 360.0f;
+    }
+
+    gVSConstPixel->time = rsUptimeMillis()*0.005;
+
+    // Setup the projection matrix
+    float aspect = (float)rsgGetWidth() / (float)rsgGetHeight();
+    rsMatrixLoadPerspective(&gVSConstPixel->proj, 30.0f, aspect, 0.1f, 100.0f);
+    setupCustomShaderLights();
+
+    if (heavyVertex) {
+        rsgBindProgramVertex(gProgVertexPixelLightMove);
+    } else {
+        rsgBindProgramVertex(gProgVertexPixelLight);
+    }
+
+    // Fragment shader with texture
+    rsgBindProgramStore(gProgStoreBlendNoneDepth);
+    rsgBindProgramFragment(gProgFragmentPixelLight);
+    rsgBindSampler(gProgFragmentPixelLight, 0, gLinearClamp);
+    rsgBindTexture(gProgFragmentPixelLight, 0, gTexTorus);
+
+    // Use back face culling
+    rsgBindProgramRaster(gCullBack);
+
+    drawToruses(numMeshes, &gVSConstPixel->model, gVSConstPixel);
+}
+
+static void displayMultitextureSample(bool blend, int quadCount) {
+    bindProgramVertexOrtho();
+    rs_matrix4x4 matrix;
+    rsMatrixLoadIdentity(&matrix);
+    rsgProgramVertexLoadModelMatrix(&matrix);
+
+    // Fragment shader with texture
+    if (!blend) {
+        rsgBindProgramStore(gProgStoreBlendNone);
+    } else {
+        rsgBindProgramStore(gProgStoreBlendAlpha);
+    }
+    rsgBindProgramFragment(gProgFragmentMultitex);
+    rsgBindSampler(gProgFragmentMultitex, 0, gLinearClamp);
+    rsgBindSampler(gProgFragmentMultitex, 1, gLinearWrap);
+    rsgBindSampler(gProgFragmentMultitex, 2, gLinearClamp);
+    rsgBindTexture(gProgFragmentMultitex, 0, gTexChecker);
+    rsgBindTexture(gProgFragmentMultitex, 1, gTexTorus);
+    rsgBindTexture(gProgFragmentMultitex, 2, gTexTransparent);
+
+    for (int i = 0; i < quadCount; i ++) {
+        float startX = 10 * i, startY = 10 * i;
+        float width = rsgGetWidth() - startX, height = rsgGetHeight() - startY;
+        rsgDrawQuadTexCoords(startX, startY, 0, 0, 0,
+                             startX, startY + height, 0, 0, 1,
+                             startX + width, startY + height, 0, 1, 1,
+                             startX + width, startY, 0, 1, 0);
+    }
+}
+
+static float gAnisoTime = 0.0f;
+static uint anisoMode = 0;
+static void displayAnisoSample() {
+
+    gAnisoTime += gDt;
+
+    rsgBindProgramVertex(gProgVertex);
+    float aspect = (float)rsgGetWidth() / (float)rsgGetHeight();
+    rs_matrix4x4 proj;
+    rsMatrixLoadPerspective(&proj, 30.0f, aspect, 0.1f, 100.0f);
+    rsgProgramVertexLoadProjectionMatrix(&proj);
+
+    rs_matrix4x4 matrix;
+    // Fragment shader with texture
+    rsgBindProgramStore(gProgStoreBlendNone);
+    rsgBindProgramFragment(gProgFragmentTexture);
+    rsMatrixLoadTranslate(&matrix, 0.0f, 0.0f, -10.0f);
+    rsMatrixRotate(&matrix, -80, 1.0f, 0.0f, 0.0f);
+    rsgProgramVertexLoadModelMatrix(&matrix);
+
+    rsgBindProgramRaster(gCullNone);
+
+    rsgBindTexture(gProgFragmentTexture, 0, gTexChecker);
+
+    if (gAnisoTime >= 5.0f) {
+        gAnisoTime = 0.0f;
+        anisoMode ++;
+        anisoMode = anisoMode % 3;
+    }
+
+    if (anisoMode == 0) {
+        rsgBindSampler(gProgFragmentTexture, 0, gMipLinearAniso8);
+    } else if (anisoMode == 1) {
+        rsgBindSampler(gProgFragmentTexture, 0, gMipLinearAniso15);
+    } else {
+        rsgBindSampler(gProgFragmentTexture, 0, gMipLinearWrap);
+    }
+
+    float startX = -15;
+    float startY = -15;
+    float width = 30;
+    float height = 30;
+    rsgDrawQuadTexCoords(startX, startY, 0, 0, 0,
+                         startX, startY + height, 0, 0, 10,
+                         startX + width, startY + height, 0, 10, 10,
+                         startX + width, startY, 0, 10, 0);
+
+    rsgBindProgramRaster(gCullBack);
+
+    rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
+    rsgBindFont(gFontMono);
+    if (anisoMode == 0) {
+        rsgDrawText("Anisotropic filtering 8", 10, 40);
+    } else if (anisoMode == 1) {
+        rsgDrawText("Anisotropic filtering 15", 10, 40);
+    } else {
+        rsgDrawText("Miplinear filtering", 10, 40);
+    }
+}
+
+static bool checkInit() {
+
+    static int countdown = 5;
+
+    if (countdown == 0) {
+        gDt = 0;
+        countdown --;
+    }
+    // Perform all the uploads so we only measure rendered time
+    if(countdown > 1) {
+        displayFontSamples(5);
+        displaySingletexFill(true, 3);
+        displayBlendingSamples();
+        displayMeshSamples(0);
+        displayMeshSamples(1);
+        displayMeshSamples(2);
+        displayTextureSamplers();
+        displayMultitextureSample(true, 5);
+        displayAnisoSample();
+        displayPixelLightSamples(1, false);
+        displayPixelLightSamples(1, true);
+        countdown --;
+        rsgClearColor(0.2f, 0.2f, 0.2f, 0.0f);
+
+        // Now use text metrics to center the text
+        uint width = rsgGetWidth();
+        uint height = rsgGetHeight();
+        int left = 0, right = 0, top = 0, bottom = 0;
+
+        rsgFontColor(0.9f, 0.9f, 0.95f, 1.0f);
+        rsgBindFont(gFontSerifBoldItalic);
+
+        const char* text = "Initializing";
+        rsgMeasureText(text, &left, &right, &top, &bottom);
+        int centeredPosX = width / 2 - (right - left) / 2;
+        int centeredPosY = height / 2 - (top - bottom) / 2;
+        rsgDrawText(text, centeredPosX, centeredPosY);
+
+        return false;
+    }
+
+    return true;
+}
+
+static int frameCount = 0;
+static int totalFramesRendered = 0;
+static int benchMode = 0;
+
+#define testTime 5.0f
+static float curTestTime = testTime;
+
+static const char *testNames[] = {
+    "Finished text fill 1",
+    "Finished text fill 2",
+    "Finished text fill 3",
+    "Finished text fill 4",
+    "Finished text fill 5",
+    "Finished 25.6k geo flat color",
+    "Finished 51.2k geo flat color",
+    "Finished 204.8k geo raster load flat color",
+    "Finished 25.6k geo texture",
+    "Finished 51.2k geo texture",
+    "Finished 204.8k geo raster load texture",
+    "Finished full screen mesh 10 by 10",
+    "Finished full screen mesh 100 by 100",
+    "Finished full screen mesh W / 4 by H / 4",
+    "Finished 25.6k geo heavy vertex",
+    "Finished 51.2k geo heavy vertex",
+    "Finished 204.8k geo raster load heavy vertex",
+    "Finished singletexture 5x fill",
+    "Finished 3tex multitexture 5x fill",
+    "Finished blend singletexture 5x fill",
+    "Finished blend 3tex multitexture 5x fill",
+    "Finished 25.6k geo heavy fragment",
+    "Finished 51.2k geo heavy fragment",
+    "Finished 204.8k geo raster load heavy fragment",
+    "Finished 25.6k geo heavy fragment, heavy vertex",
+    "Finished 51.2k geo heavy fragment, heavy vertex",
+    "Finished 204.8k geo raster load heavy fragment, heavy vertex",
+};
+
+int root(int launchID) {
+
+    gDt = rsGetDt();
+
+    rsgClearColor(0.2f, 0.2f, 0.2f, 0.0f);
+    rsgClearDepth(1.0f);
+
+    if(!checkInit()) {
+        return 1;
+    }
+
+    curTestTime -= gDt;
+    if(curTestTime < 0.0f) {
+        float fps = (float)(frameCount) / (testTime - curTestTime);
+        rsDebug(testNames[benchMode], fps);
+        benchMode ++;
+        curTestTime = testTime;
+        totalFramesRendered += frameCount;
+        frameCount = 0;
+        gTorusRotation = 0;
+
+        if (benchMode > gMaxModes) {
+            benchMode = 0;
+        }
+    }
+
+    switch (benchMode) {
+    case 0:
+        displayFontSamples(1);
+        break;
+    case 1:
+        displayFontSamples(2);
+        break;
+    case 2:
+        displayFontSamples(3);
+        break;
+    case 3:
+        displayFontSamples(4);
+        break;
+    case 4:
+        displayFontSamples(5);
+        break;
+    case 5:
+        displaySimpleGeoSamples(false, 1);
+        break;
+    case 6:
+        displaySimpleGeoSamples(false, 2);
+        break;
+    case 7:
+        displaySimpleGeoSamples(false, 8);
+        break;
+    case 8:
+        displaySimpleGeoSamples(true, 1);
+        break;
+    case 9:
+        displaySimpleGeoSamples(true, 2);
+        break;
+    case 10:
+        displaySimpleGeoSamples(true, 8);
+        break;
+    case 11:
+        displayMeshSamples(0);
+        break;
+    case 12:
+        displayMeshSamples(1);
+        break;
+    case 13:
+        displayMeshSamples(2);
+        break;
+    case 14:
+        displayCustomShaderSamples(1);
+        break;
+    case 15:
+        displayCustomShaderSamples(2);
+        break;
+    case 16:
+        displayCustomShaderSamples(8);
+        break;
+    case 17:
+        displaySingletexFill(false, 5);
+        break;
+    case 18:
+        displayMultitextureSample(false, 5);
+        break;
+    case 19:
+        displaySingletexFill(true, 5);
+        break;
+    case 20:
+        displayMultitextureSample(true, 5);
+        break;
+    case 21:
+        displayPixelLightSamples(1, false);
+        break;
+    case 22:
+        displayPixelLightSamples(2, false);
+        break;
+    case 23:
+        displayPixelLightSamples(8, false);
+        break;
+    case 24:
+        displayPixelLightSamples(1, true);
+        break;
+    case 25:
+        displayPixelLightSamples(2, true);
+        break;
+    case 26:
+        displayPixelLightSamples(8, true);
+        break;
+
+    }
+
+    frameCount ++;
+
+    return 1;
+}
diff --git a/rsAdapter.cpp b/rsAdapter.cpp
index 8d363fd..6e8ca70 100644
--- a/rsAdapter.cpp
+++ b/rsAdapter.cpp
@@ -15,11 +15,7 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
-#else
-#include "rsContextHostStub.h"
-#endif
 
 using namespace android;
 using namespace android::renderscript;
diff --git a/rsAllocation.cpp b/rsAllocation.cpp
index c598f03..54dcbcb 100644
--- a/rsAllocation.cpp
+++ b/rsAllocation.cpp
@@ -13,20 +13,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#ifndef ANDROID_RS_BUILD_FOR_HOST
-#include "rsContext.h"
 
+#include "rsContext.h"
+#ifndef ANDROID_RS_SERIALIZE
 #include <GLES/gl.h>
 #include <GLES2/gl2.h>
 #include <GLES/glext.h>
-#else
-#include "rsContextHostStub.h"
-
-#include <OpenGL/gl.h>
-#include <OpenGl/glext.h>
-#endif
-
-#include "utils/StopWatch.h"
+#endif //ANDROID_RS_SERIALIZE
 
 static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va);
 
@@ -83,7 +76,7 @@
         mPtr = NULL;
     }
     freeScriptMemory();
-
+#ifndef ANDROID_RS_SERIALIZE
     if (mBufferID) {
         // Causes a SW crash....
         //LOGV(" mBufferID %i", mBufferID);
@@ -94,6 +87,7 @@
         glDeleteTextures(1, &mTextureID);
         mTextureID = 0;
     }
+#endif //ANDROID_RS_SERIALIZE
 }
 
 void Allocation::setCpuWritable(bool) {
@@ -118,6 +112,7 @@
 }
 
 uint32_t Allocation::getGLTarget() const {
+#ifndef ANDROID_RS_SERIALIZE
     if (getIsTexture()) {
         if (mType->getDimFaces()) {
             return GL_TEXTURE_CUBE_MAP;
@@ -128,6 +123,7 @@
     if (getIsBufferObject()) {
         return GL_ARRAY_BUFFER;
     }
+#endif //ANDROID_RS_SERIALIZE
     return 0;
 }
 
@@ -158,7 +154,7 @@
 }
 
 void Allocation::uploadToTexture(const Context *rsc) {
-
+#ifndef ANDROID_RS_SERIALIZE
     mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
     GLenum type = mType->getElement()->getComponent().getGLType();
     GLenum format = mType->getElement()->getComponent().getGLFormat();
@@ -195,8 +191,10 @@
     }
 
     rsc->checkError("Allocation::uploadToTexture");
+#endif //ANDROID_RS_SERIALIZE
 }
 
+#ifndef ANDROID_RS_SERIALIZE
 const static GLenum gFaceOrder[] = {
     GL_TEXTURE_CUBE_MAP_POSITIVE_X,
     GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
@@ -205,10 +203,12 @@
     GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
     GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
 };
+#endif //ANDROID_RS_SERIALIZE
 
 void Allocation::update2DTexture(const void *ptr, uint32_t xoff, uint32_t yoff,
                                  uint32_t lod, RsAllocationCubemapFace face,
                                  uint32_t w, uint32_t h) {
+#ifndef ANDROID_RS_SERIALIZE
     GLenum type = mType->getElement()->getComponent().getGLType();
     GLenum format = mType->getElement()->getComponent().getGLFormat();
     GLenum target = (GLenum)getGLTarget();
@@ -220,9 +220,11 @@
         t = gFaceOrder[face];
     }
     glTexSubImage2D(t, lod, xoff, yoff, w, h, format, type, ptr);
+#endif //ANDROID_RS_SERIALIZE
 }
 
 void Allocation::upload2DTexture(bool isFirstUpload) {
+#ifndef ANDROID_RS_SERIALIZE
     GLenum type = mType->getElement()->getComponent().getGLType();
     GLenum format = mType->getElement()->getComponent().getGLFormat();
 
@@ -258,10 +260,9 @@
     }
 
     if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
-#ifndef ANDROID_RS_BUILD_FOR_HOST
         glGenerateMipmap(target);
-#endif //ANDROID_RS_BUILD_FOR_HOST
     }
+#endif //ANDROID_RS_SERIALIZE
 }
 
 void Allocation::deferedUploadToBufferObject(const Context *rsc) {
@@ -270,6 +271,7 @@
 }
 
 void Allocation::uploadToBufferObject(const Context *rsc) {
+#ifndef ANDROID_RS_SERIALIZE
     rsAssert(!mType->getDimY());
     rsAssert(!mType->getDimZ());
 
@@ -288,6 +290,7 @@
     glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
     glBindBuffer(target, 0);
     rsc->checkError("Allocation::uploadToBufferObject");
+#endif //ANDROID_RS_SERIALIZE
 }
 
 void Allocation::uploadCheck(Context *rsc) {
@@ -386,7 +389,7 @@
     ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
 
     if (sizeBytes != e->getSizeBytes()) {
-        LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
+        LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
         rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
         return;
     }
@@ -429,7 +432,7 @@
     ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
 
     if (sizeBytes != e->getSizeBytes()) {
-        LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
+        LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
         rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
         return;
     }
@@ -445,10 +448,13 @@
 }
 
 void Allocation::addProgramToDirty(const Program *p) {
+#ifndef ANDROID_RS_SERIALIZE
     mToDirtyList.push(p);
+#endif //ANDROID_RS_SERIALIZE
 }
 
 void Allocation::removeProgramToDirty(const Program *p) {
+#ifndef ANDROID_RS_SERIALIZE
     for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
         if (mToDirtyList[ct] == p) {
             mToDirtyList.removeAt(ct);
@@ -456,6 +462,7 @@
         }
     }
     rsAssert(0);
+#endif //ANDROID_RS_SERIALIZE
 }
 
 void Allocation::dumpLOGV(const char *prefix) const {
@@ -530,9 +537,11 @@
 }
 
 void Allocation::sendDirty() const {
+#ifndef ANDROID_RS_SERIALIZE
     for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
         mToDirtyList[ct]->forceDirty();
     }
+#endif //ANDROID_RS_SERIALIZE
 }
 
 void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
@@ -591,7 +600,7 @@
 
 /////////////////
 //
-
+#ifndef ANDROID_RS_SERIALIZE
 
 namespace android {
 namespace renderscript {
@@ -674,8 +683,6 @@
     }
 }
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
-
 void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
     Allocation *a = static_cast<Allocation *>(va);
     a->syncAll(rsc, src);
@@ -739,8 +746,6 @@
     a->resize2D(rsc, dimX, dimY);
 }
 
-#endif //ANDROID_RS_BUILD_FOR_HOST
-
 }
 }
 
@@ -840,3 +845,5 @@
     texAlloc->deferedUploadToTexture(rsc);
     return texAlloc;
 }
+
+#endif //ANDROID_RS_SERIALIZE
diff --git a/rsAnimation.cpp b/rsAnimation.cpp
index 6abda3c..48b4f02 100644
--- a/rsAnimation.cpp
+++ b/rsAnimation.cpp
@@ -14,12 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
-#else
-#include "rsContextHostStub.h"
-#endif //ANDROID_RS_BUILD_FOR_HOST
-
 #include "rsAnimation.h"
 
 
diff --git a/rsComponent.cpp b/rsComponent.cpp
index 81ade5d..4c4987a 100644
--- a/rsComponent.cpp
+++ b/rsComponent.cpp
@@ -16,10 +16,8 @@
 
 #include "rsComponent.h"
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
+#ifndef ANDROID_RS_SERIALIZE
 #include <GLES/gl.h>
-#else
-#include <OpenGL/gl.h>
 #endif
 
 using namespace android;
@@ -183,6 +181,7 @@
 }
 
 uint32_t Component::getGLType() const {
+#ifndef ANDROID_RS_SERIALIZE
     switch (mType) {
     case RS_TYPE_UNSIGNED_5_6_5:    return GL_UNSIGNED_SHORT_5_6_5;
     case RS_TYPE_UNSIGNED_5_5_5_1:  return GL_UNSIGNED_SHORT_5_5_5_1;
@@ -196,11 +195,12 @@
     case RS_TYPE_SIGNED_16:     return GL_SHORT;
     default:    break;
     }
-
+#endif //ANDROID_RS_SERIALIZE
     return 0;
 }
 
 uint32_t Component::getGLFormat() const {
+#ifndef ANDROID_RS_SERIALIZE
     switch (mKind) {
     case RS_KIND_PIXEL_L: return GL_LUMINANCE;
     case RS_KIND_PIXEL_A: return GL_ALPHA;
@@ -209,6 +209,7 @@
     case RS_KIND_PIXEL_RGBA: return GL_RGBA;
     default: break;
     }
+#endif //ANDROID_RS_SERIALIZE
     return 0;
 }
 
diff --git a/rsContext.h b/rsContext.h
index 9f94f26..c5e32a6 100644
--- a/rsContext.h
+++ b/rsContext.h
@@ -18,16 +18,16 @@
 #define ANDROID_RS_CONTEXT_H
 
 #include "rsUtils.h"
-#include "rsMutex.h"
-
-#include "rsThreadIO.h"
 #include "rsType.h"
-#include "rsMatrix.h"
 #include "rsAllocation.h"
 #include "rsMesh.h"
+
+#ifndef ANDROID_RS_SERIALIZE
+#include "rsMutex.h"
+#include "rsThreadIO.h"
+#include "rsMatrix.h"
 #include "rsDevice.h"
 #include "rsScriptC.h"
-#include "rsAllocation.h"
 #include "rsAdapter.h"
 #include "rsSampler.h"
 #include "rsFont.h"
@@ -42,6 +42,7 @@
 #include "rsLocklessFifo.h"
 
 #include <ui/egl/android_natives.h>
+#endif // ANDROID_RS_SERIALIZE
 
 // ---------------------------------------------------------------------------
 namespace android {
@@ -66,6 +67,8 @@
 #define CHECK_OBJ_OR_NULL(o)
 #endif
 
+#ifndef ANDROID_RS_SERIALIZE
+
 class Context {
 public:
     static Context * createContext(Device *, const RsSurfaceConfig *sc);
@@ -321,6 +324,39 @@
     uint32_t mAverageFPS;
 };
 
-}
-}
+#else
+
+class Context {
+public:
+    Context() {
+        mObjHead = NULL;
+    }
+    ~Context() {
+        ObjectBase::zeroAllUserRef(this);
+    }
+
+    ElementState mStateElement;
+    TypeState mStateType;
+
+    struct {
+        bool mLogTimes;
+        bool mLogScripts;
+        bool mLogObjects;
+        bool mLogShaders;
+        bool mLogShadersAttr;
+        bool mLogShadersUniforms;
+        bool mLogVisual;
+    } props;
+
+    void setError(RsError e, const char *msg = NULL) {  }
+
+    mutable const ObjectBase * mObjHead;
+
+protected:
+
+};
+#endif //ANDROID_RS_SERIALIZE
+
+} // renderscript
+} // android
 #endif
diff --git a/rsContextHostStub.h b/rsContextHostStub.h
deleted file mode 100644
index 8cfb38b..0000000
--- a/rsContextHostStub.h
+++ /dev/null
@@ -1,159 +0,0 @@
-/*
- * 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.
- */
-
-#ifndef ANDROID_RS_CONTEXT_HOST_STUB_H
-#define ANDROID_RS_CONTEXT_HOST_STUB_H
-
-#include "rsUtils.h"
-//#include "rsMutex.h"
-
-//#include "rsThreadIO.h"
-#include "rsType.h"
-#include "rsMatrix.h"
-#include "rsAllocation.h"
-#include "rsMesh.h"
-//#include "rsDevice.h"
-#include "rsScriptC.h"
-#include "rsAllocation.h"
-#include "rsAdapter.h"
-#include "rsSampler.h"
-#include "rsProgramFragment.h"
-#include "rsProgramStore.h"
-#include "rsProgramRaster.h"
-#include "rsProgramVertex.h"
-#include "rsShaderCache.h"
-#include "rsVertexArray.h"
-
-//#include "rsgApiStructs.h"
-//#include "rsLocklessFifo.h"
-
-//#include <ui/egl/android_natives.h>
-
-// ---------------------------------------------------------------------------
-namespace android {
-namespace renderscript {
-
-class Device;
-
-class Context {
-public:
-    Context(Device *, bool isGraphics, bool useDepth) {
-        mObjHead = NULL;
-    }
-    ~Context() {
-    }
-
-
-    //StructuredAllocationContext mStateAllocation;
-    ElementState mStateElement;
-    TypeState mStateType;
-    SamplerState mStateSampler;
-    //ProgramFragmentState mStateFragment;
-    ProgramStoreState mStateFragmentStore;
-    //ProgramRasterState mStateRaster;
-    //ProgramVertexState mStateVertex;
-    VertexArrayState mStateVertexArray;
-
-    //ScriptCState mScriptC;
-    ShaderCache mShaderCache;
-
-    RsSurfaceConfig mUserSurfaceConfig;
-
-    //bool setupCheck();
-
-    ProgramFragment * getDefaultProgramFragment() const {
-        return NULL;
-    }
-    ProgramVertex * getDefaultProgramVertex() const {
-        return NULL;
-    }
-    ProgramStore * getDefaultProgramStore() const {
-        return NULL;
-    }
-    ProgramRaster * getDefaultProgramRaster() const {
-        return NULL;
-    }
-
-    uint32_t getWidth() const {return 0;}
-    uint32_t getHeight() const {return 0;}
-
-    // Timers
-    enum Timers {
-        RS_TIMER_IDLE,
-        RS_TIMER_INTERNAL,
-        RS_TIMER_SCRIPT,
-        RS_TIMER_CLEAR_SWAP,
-        _RS_TIMER_TOTAL
-    };
-
-    bool checkVersion1_1() const {return false; }
-    bool checkVersion2_0() const {return false; }
-
-    struct {
-        bool mLogTimes;
-        bool mLogScripts;
-        bool mLogObjects;
-        bool mLogShaders;
-        bool mLogShadersAttr;
-        bool mLogShadersUniforms;
-        bool mLogVisual;
-    } props;
-
-    void dumpDebug() const {    }
-    void checkError(const char *) const {  };
-    void setError(RsError e, const char *msg = NULL) {  }
-
-    mutable const ObjectBase * mObjHead;
-
-    bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
-    bool ext_GL_NV_texture_npot_2D_mipmap() const {return mGL.GL_NV_texture_npot_2D_mipmap;}
-    float ext_texture_max_aniso() const {return mGL.EXT_texture_max_aniso; }
-    uint32_t getMaxFragmentTextures() const {return mGL.mMaxFragmentTextureImageUnits;}
-    uint32_t getMaxFragmentUniformVectors() const {return mGL.mMaxFragmentUniformVectors;}
-    uint32_t getMaxVertexUniformVectors() const {return mGL.mMaxVertexUniformVectors;}
-    uint32_t getMaxVertexAttributes() const {return mGL.mMaxVertexAttribs;}
-
-protected:
-
-    struct {
-        const uint8_t * mVendor;
-        const uint8_t * mRenderer;
-        const uint8_t * mVersion;
-        const uint8_t * mExtensions;
-
-        uint32_t mMajorVersion;
-        uint32_t mMinorVersion;
-
-        int32_t mMaxVaryingVectors;
-        int32_t mMaxTextureImageUnits;
-
-        int32_t mMaxFragmentTextureImageUnits;
-        int32_t mMaxFragmentUniformVectors;
-
-        int32_t mMaxVertexAttribs;
-        int32_t mMaxVertexUniformVectors;
-        int32_t mMaxVertexTextureUnits;
-
-        bool OES_texture_npot;
-        bool GL_NV_texture_npot_2D_mipmap;
-        float EXT_texture_max_aniso;
-    } mGL;
-
-};
-
-}
-}
-#endif
diff --git a/rsDevice.cpp b/rsDevice.cpp
index dd96445..d7d03f6 100644
--- a/rsDevice.cpp
+++ b/rsDevice.cpp
@@ -15,11 +15,7 @@
  */
 
 #include "rsDevice.h"
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
-#else
-#include "rsContextHostStub.h"
-#endif
 
 using namespace android;
 using namespace android::renderscript;
diff --git a/rsElement.cpp b/rsElement.cpp
index 6ae8bb8..477cb61 100644
--- a/rsElement.cpp
+++ b/rsElement.cpp
@@ -15,13 +15,7 @@
  */
 
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
-#include <GLES/gl.h>
-#else
-#include "rsContextHostStub.h"
-#include <OpenGL/gl.h>
-#endif
 
 using namespace android;
 using namespace android::renderscript;
@@ -65,7 +59,7 @@
 
 void Element::dumpLOGV(const char *prefix) const {
     ObjectBase::dumpLOGV(prefix);
-    LOGV("%s Element: fieldCount: %i,  size bytes: %i", prefix, mFieldCount, getSizeBytes());
+    LOGV("%s Element: fieldCount: %zu,  size bytes: %zu", prefix, mFieldCount, getSizeBytes());
     for (uint32_t ct = 0; ct < mFieldCount; ct++) {
         LOGV("%s Element field index: %u ------------------", prefix, ct);
         LOGV("%s name: %s, offsetBits: %u, arraySize: %u",
diff --git a/rsFileA3D.cpp b/rsFileA3D.cpp
index d34ddd6..cd02c24 100644
--- a/rsFileA3D.cpp
+++ b/rsFileA3D.cpp
@@ -15,12 +15,7 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
-#else
-#include "rsContextHostStub.h"
-#endif
-
 #include "rsFileA3D.h"
 
 #include "rsMesh.h"
@@ -249,31 +244,31 @@
             entry->mRsObj = Allocation::createFromStream(mRSC, mReadStream);
             break;
         case RS_A3D_CLASS_ID_PROGRAM_VERTEX:
-            entry->mRsObj = ProgramVertex::createFromStream(mRSC, mReadStream);
+            //entry->mRsObj = ProgramVertex::createFromStream(mRSC, mReadStream);
             break;
         case RS_A3D_CLASS_ID_PROGRAM_RASTER:
-            entry->mRsObj = ProgramRaster::createFromStream(mRSC, mReadStream);
+            //entry->mRsObj = ProgramRaster::createFromStream(mRSC, mReadStream);
             break;
         case RS_A3D_CLASS_ID_PROGRAM_FRAGMENT:
-            entry->mRsObj = ProgramFragment::createFromStream(mRSC, mReadStream);
+            //entry->mRsObj = ProgramFragment::createFromStream(mRSC, mReadStream);
             break;
         case RS_A3D_CLASS_ID_PROGRAM_STORE:
-            entry->mRsObj = ProgramStore::createFromStream(mRSC, mReadStream);
+            //entry->mRsObj = ProgramStore::createFromStream(mRSC, mReadStream);
             break;
         case RS_A3D_CLASS_ID_SAMPLER:
-            entry->mRsObj = Sampler::createFromStream(mRSC, mReadStream);
+            //entry->mRsObj = Sampler::createFromStream(mRSC, mReadStream);
             break;
         case RS_A3D_CLASS_ID_ANIMATION:
-            entry->mRsObj = Animation::createFromStream(mRSC, mReadStream);
+            //entry->mRsObj = Animation::createFromStream(mRSC, mReadStream);
             break;
         case RS_A3D_CLASS_ID_ADAPTER_1D:
-            entry->mRsObj = Adapter1D::createFromStream(mRSC, mReadStream);
+            //entry->mRsObj = Adapter1D::createFromStream(mRSC, mReadStream);
             break;
         case RS_A3D_CLASS_ID_ADAPTER_2D:
-            entry->mRsObj = Adapter2D::createFromStream(mRSC, mReadStream);
+            //entry->mRsObj = Adapter2D::createFromStream(mRSC, mReadStream);
             break;
         case RS_A3D_CLASS_ID_SCRIPT_C:
-            return NULL;
+            break;
     }
     if (entry->mRsObj) {
         entry->mRsObj->incUserRef();
diff --git a/rsFont.cpp b/rsFont.cpp
index 7fdfbe0..8a5ab99 100644
--- a/rsFont.cpp
+++ b/rsFont.cpp
@@ -15,11 +15,7 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
-#else
-#include "rsContextHostStub.h"
-#endif
 
 #include "rsFont.h"
 #include "rsProgramFragment.h"
diff --git a/rsMesh.cpp b/rsMesh.cpp
index baf4c53..76fe62d 100644
--- a/rsMesh.cpp
+++ b/rsMesh.cpp
@@ -14,17 +14,11 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
-
+#ifndef ANDROID_RS_SERIALIZE
 #include <GLES/gl.h>
 #include <GLES2/gl2.h>
 #include <GLES/glext.h>
-#else
-#include "rsContextHostStub.h"
-
-#include <OpenGL/gl.h>
-#include <OpenGl/glext.h>
 #endif
 
 using namespace android;
@@ -35,10 +29,13 @@
     mPrimitivesCount = 0;
     mVertexBuffers = NULL;
     mVertexBufferCount = 0;
+
+#ifndef ANDROID_RS_SERIALIZE
     mAttribs = NULL;
     mAttribAllocationIndex = NULL;
 
     mAttribCount = 0;
+#endif
 }
 
 Mesh::~Mesh() {
@@ -53,12 +50,97 @@
         delete[] mPrimitives;
     }
 
+#ifndef ANDROID_RS_SERIALIZE
     if (mAttribs) {
         delete[] mAttribs;
         delete[] mAttribAllocationIndex;
     }
+#endif
 }
 
+void Mesh::serialize(OStream *stream) const {
+    // Need to identify ourselves
+    stream->addU32((uint32_t)getClassId());
+
+    String8 name(getName());
+    stream->addString(&name);
+
+    // Store number of vertex streams
+    stream->addU32(mVertexBufferCount);
+    for (uint32_t vCount = 0; vCount < mVertexBufferCount; vCount ++) {
+        mVertexBuffers[vCount]->serialize(stream);
+    }
+
+    stream->addU32(mPrimitivesCount);
+    // Store the primitives
+    for (uint32_t pCount = 0; pCount < mPrimitivesCount; pCount ++) {
+        Primitive_t * prim = mPrimitives[pCount];
+
+        stream->addU8((uint8_t)prim->mPrimitive);
+
+        if (prim->mIndexBuffer.get()) {
+            stream->addU32(1);
+            prim->mIndexBuffer->serialize(stream);
+        } else {
+            stream->addU32(0);
+        }
+    }
+}
+
+Mesh *Mesh::createFromStream(Context *rsc, IStream *stream) {
+    // First make sure we are reading the correct object
+    RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
+    if (classID != RS_A3D_CLASS_ID_MESH) {
+        LOGE("mesh loading skipped due to invalid class id");
+        return NULL;
+    }
+
+    Mesh * mesh = new Mesh(rsc);
+
+    String8 name;
+    stream->loadString(&name);
+    mesh->setName(name.string(), name.size());
+
+    mesh->mVertexBufferCount = stream->loadU32();
+    if (mesh->mVertexBufferCount) {
+        mesh->mVertexBuffers = new ObjectBaseRef<Allocation>[mesh->mVertexBufferCount];
+
+        for (uint32_t vCount = 0; vCount < mesh->mVertexBufferCount; vCount ++) {
+            Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream);
+            mesh->mVertexBuffers[vCount].set(vertexAlloc);
+        }
+    }
+
+    mesh->mPrimitivesCount = stream->loadU32();
+    if (mesh->mPrimitivesCount) {
+        mesh->mPrimitives = new Primitive_t *[mesh->mPrimitivesCount];
+
+        // load all primitives
+        for (uint32_t pCount = 0; pCount < mesh->mPrimitivesCount; pCount ++) {
+            Primitive_t * prim = new Primitive_t;
+            mesh->mPrimitives[pCount] = prim;
+
+            prim->mPrimitive = (RsPrimitive)stream->loadU8();
+
+            // Check to see if the index buffer was stored
+            uint32_t isIndexPresent = stream->loadU32();
+            if (isIndexPresent) {
+                Allocation *indexAlloc = Allocation::createFromStream(rsc, stream);
+                prim->mIndexBuffer.set(indexAlloc);
+            }
+        }
+    }
+
+#ifndef ANDROID_RS_SERIALIZE
+    mesh->updateGLPrimitives();
+    mesh->initVertexAttribs();
+    mesh->uploadAll(rsc);
+#endif
+    return mesh;
+}
+
+#ifndef ANDROID_RS_SERIALIZE
+
 bool Mesh::isValidGLComponent(const Element *elem, uint32_t fieldIdx) {
     // Do not create attribs for padding
     if (elem->getFieldName(fieldIdx)[0] == '#') {
@@ -224,86 +306,6 @@
     }
 }
 
-void Mesh::serialize(OStream *stream) const {
-    // Need to identify ourselves
-    stream->addU32((uint32_t)getClassId());
-
-    String8 name(getName());
-    stream->addString(&name);
-
-    // Store number of vertex streams
-    stream->addU32(mVertexBufferCount);
-    for (uint32_t vCount = 0; vCount < mVertexBufferCount; vCount ++) {
-        mVertexBuffers[vCount]->serialize(stream);
-    }
-
-    stream->addU32(mPrimitivesCount);
-    // Store the primitives
-    for (uint32_t pCount = 0; pCount < mPrimitivesCount; pCount ++) {
-        Primitive_t * prim = mPrimitives[pCount];
-
-        stream->addU8((uint8_t)prim->mPrimitive);
-
-        if (prim->mIndexBuffer.get()) {
-            stream->addU32(1);
-            prim->mIndexBuffer->serialize(stream);
-        } else {
-            stream->addU32(0);
-        }
-    }
-}
-
-Mesh *Mesh::createFromStream(Context *rsc, IStream *stream) {
-    // First make sure we are reading the correct object
-    RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
-    if (classID != RS_A3D_CLASS_ID_MESH) {
-        LOGE("mesh loading skipped due to invalid class id");
-        return NULL;
-    }
-
-    Mesh * mesh = new Mesh(rsc);
-
-    String8 name;
-    stream->loadString(&name);
-    mesh->setName(name.string(), name.size());
-
-    mesh->mVertexBufferCount = stream->loadU32();
-    if (mesh->mVertexBufferCount) {
-        mesh->mVertexBuffers = new ObjectBaseRef<Allocation>[mesh->mVertexBufferCount];
-
-        for (uint32_t vCount = 0; vCount < mesh->mVertexBufferCount; vCount ++) {
-            Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream);
-            mesh->mVertexBuffers[vCount].set(vertexAlloc);
-        }
-    }
-
-    mesh->mPrimitivesCount = stream->loadU32();
-    if (mesh->mPrimitivesCount) {
-        mesh->mPrimitives = new Primitive_t *[mesh->mPrimitivesCount];
-
-        // load all primitives
-        for (uint32_t pCount = 0; pCount < mesh->mPrimitivesCount; pCount ++) {
-            Primitive_t * prim = new Primitive_t;
-            mesh->mPrimitives[pCount] = prim;
-
-            prim->mPrimitive = (RsPrimitive)stream->loadU8();
-
-            // Check to see if the index buffer was stored
-            uint32_t isIndexPresent = stream->loadU32();
-            if (isIndexPresent) {
-                Allocation *indexAlloc = Allocation::createFromStream(rsc, stream);
-                prim->mIndexBuffer.set(indexAlloc);
-            }
-        }
-    }
-
-    mesh->updateGLPrimitives();
-    mesh->initVertexAttribs();
-    mesh->uploadAll(rsc);
-
-    return mesh;
-}
-
 void Mesh::computeBBox() {
     float *posPtr = NULL;
     uint32_t vectorSize = 0;
@@ -347,13 +349,6 @@
     }
 }
 
-
-MeshContext::MeshContext() {
-}
-
-MeshContext::~MeshContext() {
-}
-
 namespace android {
 namespace renderscript {
 
@@ -428,3 +423,5 @@
         }
     }
 }
+
+#endif
diff --git a/rsMesh.h b/rsMesh.h
index 410b70b..3e080e2 100644
--- a/rsMesh.h
+++ b/rsMesh.h
@@ -50,15 +50,18 @@
     Primitive_t ** mPrimitives;
     uint32_t mPrimitivesCount;
 
+    virtual void serialize(OStream *stream) const;
+    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_MESH; }
+    static Mesh *createFromStream(Context *rsc, IStream *stream);
+
+#ifndef ANDROID_RS_SERIALIZE
     void render(Context *) const;
     void renderPrimitive(Context *, uint32_t primIndex) const;
     void renderPrimitiveRange(Context *, uint32_t primIndex, uint32_t start, uint32_t len) const;
     void uploadAll(Context *);
     void updateGLPrimitives();
 
-    virtual void serialize(OStream *stream) const;
-    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_MESH; }
-    static Mesh *createFromStream(Context *rsc, IStream *stream);
+
 
     // Bounding volumes
     float mBBoxMin[3];
@@ -76,12 +79,15 @@
     // buffer, it lets us properly map it
     uint32_t *mAttribAllocationIndex;
     uint32_t mAttribCount;
+#endif
 };
 
 class MeshContext {
 public:
-    MeshContext();
-    ~MeshContext();
+    MeshContext() {
+    }
+    ~MeshContext() {
+    }
 };
 
 }
diff --git a/rsObjectBase.cpp b/rsObjectBase.cpp
index aec2f67..f428f94 100644
--- a/rsObjectBase.cpp
+++ b/rsObjectBase.cpp
@@ -15,13 +15,7 @@
  */
 
 #include "rsObjectBase.h"
-
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
-#else
-#include "rsContextHostStub.h"
-#endif
-
 
 using namespace android;
 using namespace android::renderscript;
diff --git a/rsProgram.cpp b/rsProgram.cpp
index 39b85e3..4ef05bf 100644
--- a/rsProgram.cpp
+++ b/rsProgram.cpp
@@ -14,15 +14,11 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
+#ifndef ANDROID_RS_SERIALIZE
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
-#else
-#include "rsContextHostStub.h"
-#include <OpenGL/gl.h>
-#include <OpenGL/glext.h>
-#endif //ANDROID_RS_BUILD_FOR_HOST
+#endif //ANDROID_RS_SERIALIZE
 
 #include "rsProgram.h"
 
diff --git a/rsProgramFragment.cpp b/rsProgramFragment.cpp
index 22cd5d3..ff314b7 100644
--- a/rsProgramFragment.cpp
+++ b/rsProgramFragment.cpp
@@ -14,17 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
+#ifndef ANDROID_RS_SERIALIZE
 #include <GLES/gl.h>
 #include <GLES/glext.h>
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
-#else
-#include "rsContextHostStub.h"
-#include <OpenGL/gl.h>
-#include <OpenGL/glext.h>
-#endif //ANDROID_RS_BUILD_FOR_HOST
+#endif //ANDROID_RS_SERIALIZE
 
 #include "rsProgramFragment.h"
 
diff --git a/rsProgramRaster.cpp b/rsProgramRaster.cpp
index f2b5b9a..ace1572 100644
--- a/rsProgramRaster.cpp
+++ b/rsProgramRaster.cpp
@@ -14,15 +14,11 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
+#ifndef ANDROID_RS_SERIALIZE
 #include <GLES/gl.h>
 #include <GLES/glext.h>
-#else
-#include "rsContextHostStub.h"
-#include <OpenGL/gl.h>
-#include <OpenGl/glext.h>
-#endif //ANDROID_RS_BUILD_FOR_HOST
+#endif //ANDROID_RS_SERIALIZE
 
 #include "rsProgramRaster.h"
 
diff --git a/rsProgramStore.cpp b/rsProgramStore.cpp
index 72ac574..09b759d 100644
--- a/rsProgramStore.cpp
+++ b/rsProgramStore.cpp
@@ -14,15 +14,11 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
+#ifndef ANDROID_RS_SERIALIZE
 #include <GLES/gl.h>
 #include <GLES/glext.h>
-#else
-#include "rsContextHostStub.h"
-#include <OpenGL/gl.h>
-#include <OpenGl/glext.h>
-#endif //ANDROID_RS_BUILD_FOR_HOST
+#endif //ANDROID_RS_SERIALIZE
 
 #include "rsProgramStore.h"
 
diff --git a/rsProgramVertex.cpp b/rsProgramVertex.cpp
index ad2beaf..403c2a6 100644
--- a/rsProgramVertex.cpp
+++ b/rsProgramVertex.cpp
@@ -14,17 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
+#ifndef ANDROID_RS_SERIALIZE
 #include <GLES/gl.h>
 #include <GLES/glext.h>
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
-#else
-#include "rsContextHostStub.h"
-#include <OpenGL/gl.h>
-#include <OpenGL/glext.h>
-#endif //ANDROID_RS_BUILD_FOR_HOST
+#endif //ANDROID_RS_SERIALIZE
 
 #include "rsProgramVertex.h"
 
diff --git a/rsSampler.cpp b/rsSampler.cpp
index c80aecc..db2383a 100644
--- a/rsSampler.cpp
+++ b/rsSampler.cpp
@@ -14,15 +14,11 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
+#include "rsContext.h"
+#ifndef ANDROID_RS_SERIALIZE
 #include <GLES/gl.h>
 #include <GLES/glext.h>
-#include "rsContext.h"
-#else
-#include "rsContextHostStub.h"
-#include <OpenGL/gl.h>
-#include <OpenGL/glext.h>
-#endif //ANDROID_RS_BUILD_FOR_HOST
+#endif //ANDROID_RS_SERIALIZE
 
 #include "rsSampler.h"
 
diff --git a/rsScriptC.cpp b/rsScriptC.cpp
index fc673a2..445a4e4 100644
--- a/rsScriptC.cpp
+++ b/rsScriptC.cpp
@@ -485,12 +485,7 @@
     }
 
 #if 1
-    if (bccLinkBC(s->mBccScript,
-                  resName,
-                  NULL /*rs_runtime_lib_bc*/,
-                  1 /*rs_runtime_lib_bc_size*/
-                    /*"1" means skip buffer here, and let libbcc decide*/,
-                  0) != 0) {
+    if (bccLinkFile(s->mBccScript, "/system/lib/libclcore.bc", 0) != 0) {
         LOGE("bcc: FAILS to link bitcode");
         return false;
     }
diff --git a/rsShaderCache.cpp b/rsShaderCache.cpp
index b958021..e8d89c2 100644
--- a/rsShaderCache.cpp
+++ b/rsShaderCache.cpp
@@ -14,14 +14,11 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
+#ifndef ANDROID_RS_SERIALIZE
 #include <GLES/gl.h>
 #include <GLES2/gl2.h>
-#else
-#include "rsContextHostStub.h"
-#include <OpenGL/gl.h>
-#endif //ANDROID_RS_BUILD_FOR_HOST
+#endif //ANDROID_RS_SERIALIZE
 
 using namespace android;
 using namespace android::renderscript;
diff --git a/rsStream.cpp b/rsStream.cpp
index 49ed567..b9df0cc 100644
--- a/rsStream.cpp
+++ b/rsStream.cpp
@@ -15,12 +15,7 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
-#else
-#include "rsContextHostStub.h"
-#endif
-
 #include "rsStream.h"
 
 using namespace android;
diff --git a/rsType.cpp b/rsType.cpp
index d7b5f12..cd2be94 100644
--- a/rsType.cpp
+++ b/rsType.cpp
@@ -14,13 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
-#include <GLES/gl.h>
-#else
-#include "rsContextHostStub.h"
-#include <OpenGL/gl.h>
-#endif
 
 using namespace android;
 using namespace android::renderscript;
@@ -146,7 +140,7 @@
 void Type::dumpLOGV(const char *prefix) const {
     char buf[1024];
     ObjectBase::dumpLOGV(prefix);
-    LOGV("%s   Type: x=%i y=%i z=%i mip=%i face=%i", prefix, mDimX, mDimY, mDimZ, mDimLOD, mFaces);
+    LOGV("%s   Type: x=%zu y=%zu z=%zu mip=%i face=%i", prefix, mDimX, mDimY, mDimZ, mDimLOD, mFaces);
     snprintf(buf, sizeof(buf), "%s element: ", prefix);
     mElement->dumpLOGV(buf);
 }
diff --git a/rsUtils.h b/rsUtils.h
index 0699b57..3b60af5 100644
--- a/rsUtils.h
+++ b/rsUtils.h
@@ -32,7 +32,7 @@
 #include <time.h>
 #include <cutils/atomic.h>
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
+#ifndef ANDROID_RS_SERIALIZE
 #include <EGL/egl.h>
 #endif
 
diff --git a/rsVertexArray.cpp b/rsVertexArray.cpp
index d9393fe..354ee89 100644
--- a/rsVertexArray.cpp
+++ b/rsVertexArray.cpp
@@ -14,13 +14,10 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_RS_BUILD_FOR_HOST
 #include "rsContext.h"
+#ifndef ANDROID_RS_SERIALIZE
 #include <GLES/gl.h>
 #include <GLES2/gl2.h>
-#else
-#include "rsContextHostStub.h"
-#include <OpenGL/gl.h>
 #endif
 
 using namespace android;
diff --git a/scriptc/rs_core.rsh b/scriptc/rs_core.rsh
index 464e1d9..4768bbe 100644
--- a/scriptc/rs_core.rsh
+++ b/scriptc/rs_core.rsh
@@ -295,6 +295,256 @@
 extern void __attribute__((overloadable)) rsMatrixTranspose(rs_matrix2x2 *m);
 
 /////////////////////////////////////////////////////
+// quaternion ops
+/////////////////////////////////////////////////////
+
+static void __attribute__((overloadable))
+rsQuaternionSet(rs_quaternion *q, float w, float x, float y, float z) {
+    q->w = w;
+    q->x = x;
+    q->y = y;
+    q->z = z;
+}
+
+static void __attribute__((overloadable))
+rsQuaternionSet(rs_quaternion *q, const rs_quaternion *rhs) {
+    q->w = rhs->w;
+    q->x = rhs->x;
+    q->y = rhs->y;
+    q->z = rhs->z;
+}
+
+static void __attribute__((overloadable))
+rsQuaternionMultiply(rs_quaternion *q, float s) {
+    q->w *= s;
+    q->x *= s;
+    q->y *= s;
+    q->z *= s;
+}
+
+static void __attribute__((overloadable))
+rsQuaternionMultiply(rs_quaternion *q, const rs_quaternion *rhs) {
+    q->w = -q->x*rhs->x - q->y*rhs->y - q->z*rhs->z + q->w*rhs->w;
+    q->x =  q->x*rhs->w + q->y*rhs->z - q->z*rhs->y + q->w*rhs->x;
+    q->y = -q->x*rhs->z + q->y*rhs->w + q->z*rhs->x + q->w*rhs->y;
+    q->z =  q->x*rhs->y - q->y*rhs->x + q->z*rhs->w + q->w*rhs->z;
+}
+
+static void
+rsQuaternionAdd(rs_quaternion *q, const rs_quaternion *rhs) {
+    q->w *= rhs->w;
+    q->x *= rhs->x;
+    q->y *= rhs->y;
+    q->z *= rhs->z;
+}
+
+static void
+rsQuaternionLoadRotateUnit(rs_quaternion *q, float rot, float x, float y, float z) {
+    rot *= (float)(M_PI / 180.0f) * 0.5f;
+    float c = cos(rot);
+    float s = sin(rot);
+
+    q->w = c;
+    q->x = x * s;
+    q->y = y * s;
+    q->z = z * s;
+}
+
+static void
+rsQuaternionLoadRotate(rs_quaternion *q, float rot, float x, float y, float z) {
+    const float len = x*x + y*y + z*z;
+    if (len != 1) {
+        const float recipLen = 1.f / sqrt(len);
+        x *= recipLen;
+        y *= recipLen;
+        z *= recipLen;
+    }
+    rsQuaternionLoadRotateUnit(q, rot, x, y, z);
+}
+
+static void
+rsQuaternionConjugate(rs_quaternion *q) {
+    q->x = -q->x;
+    q->y = -q->y;
+    q->z = -q->z;
+}
+
+static float
+rsQuaternionDot(const rs_quaternion *q0, const rs_quaternion *q1) {
+    return q0->w*q1->w + q0->x*q1->x + q0->y*q1->y + q0->z*q1->z;
+}
+
+static void
+rsQuaternionNormalize(rs_quaternion *q) {
+    const float len = rsQuaternionDot(q, q);
+    if (len != 1) {
+        const float recipLen = 1.f / sqrt(len);
+        rsQuaternionMultiply(q, recipLen);
+    }
+}
+
+static void
+rsQuaternionSlerp(rs_quaternion *q, const rs_quaternion *q0, const rs_quaternion *q1, float t) {
+    if (t <= 0.0f) {
+        rsQuaternionSet(q, q0);
+        return;
+    }
+    if (t >= 1.0f) {
+        rsQuaternionSet(q, q1);
+        return;
+    }
+
+    rs_quaternion tempq0, tempq1;
+    rsQuaternionSet(&tempq0, q0);
+    rsQuaternionSet(&tempq1, q1);
+
+    float angle = rsQuaternionDot(q0, q1);
+    if (angle < 0) {
+        rsQuaternionMultiply(&tempq0, -1.0f);
+        angle *= -1.0f;
+    }
+
+    float scale, invScale;
+    if (angle + 1.0f > 0.05f) {
+        if (1.0f - angle >= 0.05f) {
+            float theta = acos(angle);
+            float invSinTheta = 1.0f / sin(theta);
+            scale = sin(theta * (1.0f - t)) * invSinTheta;
+            invScale = sin(theta * t) * invSinTheta;
+        } else {
+            scale = 1.0f - t;
+            invScale = t;
+        }
+    } else {
+        rsQuaternionSet(&tempq1, tempq0.z, -tempq0.y, tempq0.x, -tempq0.w);
+        scale = sin(M_PI * (0.5f - t));
+        invScale = sin(M_PI * t);
+    }
+
+    rsQuaternionSet(q, tempq0.w*scale + tempq1.w*invScale, tempq0.x*scale + tempq1.x*invScale,
+                        tempq0.y*scale + tempq1.y*invScale, tempq0.z*scale + tempq1.z*invScale);
+}
+
+static void rsQuaternionGetMatrixUnit(rs_matrix4x4 *m, const rs_quaternion *q) {
+    float x2 = 2.0f * q->x * q->x;
+    float y2 = 2.0f * q->y * q->y;
+    float z2 = 2.0f * q->z * q->z;
+    float xy = 2.0f * q->x * q->y;
+    float wz = 2.0f * q->w * q->z;
+    float xz = 2.0f * q->x * q->z;
+    float wy = 2.0f * q->w * q->y;
+    float wx = 2.0f * q->w * q->x;
+    float yz = 2.0f * q->y * q->z;
+
+    m->m[0] = 1.0f - y2 - z2;
+    m->m[1] = xy - wz;
+    m->m[2] = xz + wy;
+    m->m[3] = 0.0f;
+
+    m->m[4] = xy + wz;
+    m->m[5] = 1.0f - x2 - z2;
+    m->m[6] = yz - wx;
+    m->m[7] = 0.0f;
+
+    m->m[8] = xz - wy;
+    m->m[9] = yz - wx;
+    m->m[10] = 1.0f - x2 - y2;
+    m->m[11] = 0.0f;
+
+    m->m[12] = 0.0f;
+    m->m[13] = 0.0f;
+    m->m[14] = 0.0f;
+    m->m[15] = 1.0f;
+}
+
+/////////////////////////////////////////////////////
+// utility funcs
+/////////////////////////////////////////////////////
+__inline__ static void __attribute__((overloadable, always_inline))
+rsExtractFrustumPlanes(const rs_matrix4x4 *modelViewProj,
+                         float4 *left, float4 *right,
+                         float4 *top, float4 *bottom,
+                         float4 *near, float4 *far) {
+    // x y z w = a b c d in the plane equation
+    left->x = modelViewProj->m[3] + modelViewProj->m[0];
+    left->y = modelViewProj->m[7] + modelViewProj->m[4];
+    left->z = modelViewProj->m[11] + modelViewProj->m[8];
+    left->w = modelViewProj->m[15] + modelViewProj->m[12];
+
+    right->x = modelViewProj->m[3] - modelViewProj->m[0];
+    right->y = modelViewProj->m[7] - modelViewProj->m[4];
+    right->z = modelViewProj->m[11] - modelViewProj->m[8];
+    right->w = modelViewProj->m[15] - modelViewProj->m[12];
+
+    top->x = modelViewProj->m[3] - modelViewProj->m[1];
+    top->y = modelViewProj->m[7] - modelViewProj->m[5];
+    top->z = modelViewProj->m[11] - modelViewProj->m[9];
+    top->w = modelViewProj->m[15] - modelViewProj->m[13];
+
+    bottom->x = modelViewProj->m[3] + modelViewProj->m[1];
+    bottom->y = modelViewProj->m[7] + modelViewProj->m[5];
+    bottom->z = modelViewProj->m[11] + modelViewProj->m[9];
+    bottom->w = modelViewProj->m[15] + modelViewProj->m[13];
+
+    near->x = modelViewProj->m[3] + modelViewProj->m[2];
+    near->y = modelViewProj->m[7] + modelViewProj->m[6];
+    near->z = modelViewProj->m[11] + modelViewProj->m[10];
+    near->w = modelViewProj->m[15] + modelViewProj->m[14];
+
+    far->x = modelViewProj->m[3] - modelViewProj->m[2];
+    far->y = modelViewProj->m[7] - modelViewProj->m[6];
+    far->z = modelViewProj->m[11] - modelViewProj->m[10];
+    far->w = modelViewProj->m[15] - modelViewProj->m[14];
+
+    float len = length(left->xyz);
+    *left /= len;
+    len = length(right->xyz);
+    *right /= len;
+    len = length(top->xyz);
+    *top /= len;
+    len = length(bottom->xyz);
+    *bottom /= len;
+    len = length(near->xyz);
+    *near /= len;
+    len = length(far->xyz);
+    *far /= len;
+}
+
+__inline__ static bool __attribute__((overloadable, always_inline))
+rsIsSphereInFrustum(float4 *sphere,
+                      float4 *left, float4 *right,
+                      float4 *top, float4 *bottom,
+                      float4 *near, float4 *far) {
+
+    float distToCenter = dot(left->xyz, sphere->xyz) + left->w;
+    if (distToCenter < -sphere->w) {
+        return false;
+    }
+    distToCenter = dot(right->xyz, sphere->xyz) + right->w;
+    if (distToCenter < -sphere->w) {
+        return false;
+    }
+    distToCenter = dot(top->xyz, sphere->xyz) + top->w;
+    if (distToCenter < -sphere->w) {
+        return false;
+    }
+    distToCenter = dot(bottom->xyz, sphere->xyz) + bottom->w;
+    if (distToCenter < -sphere->w) {
+        return false;
+    }
+    distToCenter = dot(near->xyz, sphere->xyz) + near->w;
+    if (distToCenter < -sphere->w) {
+        return false;
+    }
+    distToCenter = dot(far->xyz, sphere->xyz) + far->w;
+    if (distToCenter < -sphere->w) {
+        return false;
+    }
+    return true;
+}
+
+
+/////////////////////////////////////////////////////
 // int ops
 /////////////////////////////////////////////////////
 
diff --git a/scriptc/rs_types.rsh b/scriptc/rs_types.rsh
index 367af46..a010096 100644
--- a/scriptc/rs_types.rsh
+++ b/scriptc/rs_types.rsh
@@ -73,6 +73,8 @@
     float m[4];
 } rs_matrix2x2;
 
+typedef float4 rs_quaternion;
+
 #define RS_PACKED __attribute__((packed, aligned(4)))
 
 #endif