Move to smart pointers for refcounting RS contexts

Change-Id: I0dc6adc4e02c7427a4234c549d3555a501fe5f90
diff --git a/cpp/Allocation.cpp b/cpp/Allocation.cpp
index d6f79b0..396018b 100644
--- a/cpp/Allocation.cpp
+++ b/cpp/Allocation.cpp
@@ -14,23 +14,12 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "libRS_cpp"
-
-#include <utils/Log.h>
-#include <malloc.h>
-
 #include "RenderScript.h"
-#include "Element.h"
-#include "Type.h"
-#include "Allocation.h"
 
 using namespace android;
 using namespace renderscriptCpp;
 
 void * Allocation::getIDSafe() const {
-    //if (mAdaptedAllocation != NULL) {
-        //return mAdaptedAllocation.getID();
-    //}
     return getID();
 }
 
@@ -47,7 +36,7 @@
     }
 }
 
-Allocation::Allocation(void *id, RenderScript *rs, sp<const Type> t, uint32_t usage) :
+Allocation::Allocation(void *id, sp<RS> rs, sp<const Type> t, uint32_t usage) :
         BaseObj(id, rs) {
 
     if ((usage & ~(RS_ALLOCATION_USAGE_SCRIPT |
@@ -129,7 +118,7 @@
 void Allocation::updateFromNative() {
     BaseObj::updateFromNative();
 
-    const void *typeID = rsaAllocationGetType(mRS->mContext, getID());
+    const void *typeID = rsaAllocationGetType(mRS->getContext(), getID());
     if(typeID != NULL) {
         sp<const Type> old = mType;
         sp<Type> t = new Type((void *)typeID, mRS);
@@ -149,79 +138,25 @@
     default:
         ALOGE("Source must be exactly one usage type.");
     }
-    rsAllocationSyncAll(mRS->mContext, getIDSafe(), srcLocation);
+    rsAllocationSyncAll(mRS->getContext(), getIDSafe(), srcLocation);
 }
 
 void Allocation::ioSendOutput() {
     if ((mUsage & RS_ALLOCATION_USAGE_IO_OUTPUT) == 0) {
         ALOGE("Can only send buffer if IO_OUTPUT usage specified.");
     }
-    rsAllocationIoSend(mRS->mContext, getID());
+    rsAllocationIoSend(mRS->getContext(), getID());
 }
 
 void Allocation::ioGetInput() {
     if ((mUsage & RS_ALLOCATION_USAGE_IO_INPUT) == 0) {
         ALOGE("Can only send buffer if IO_OUTPUT usage specified.");
     }
-    rsAllocationIoReceive(mRS->mContext, getID());
+    rsAllocationIoReceive(mRS->getContext(), getID());
 }
 
-/*
-void copyFrom(BaseObj[] d) {
-    mRS.validate();
-    validateIsObject();
-    if (d.length != mCurrentCount) {
-        ALOGE("Array size mismatch, allocation sizeX = " +
-                                             mCurrentCount + ", array length = " + d.length);
-    }
-    int i[] = new int[d.length];
-    for (int ct=0; ct < d.length; ct++) {
-        i[ct] = d[ct].getID();
-    }
-    copy1DRangeFromUnchecked(0, mCurrentCount, i);
-}
-*/
-
-
-/*
-void Allocation::setFromFieldPacker(int xoff, FieldPacker fp) {
-    mRS.validate();
-    int eSize = mType.mElement.getSizeBytes();
-    final byte[] data = fp.getData();
-
-    int count = data.length / eSize;
-    if ((eSize * count) != data.length) {
-        ALOGE("Field packer length " + data.length +
-                                           " not divisible by element size " + eSize + ".");
-    }
-    copy1DRangeFromUnchecked(xoff, count, data);
-}
-
-void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
-    mRS.validate();
-    if (component_number >= mType.mElement.mElements.length) {
-        ALOGE("Component_number " + component_number + " out of range.");
-    }
-    if(xoff < 0) {
-        ALOGE("Offset must be >= 0.");
-    }
-
-    final byte[] data = fp.getData();
-    int eSize = mType.mElement.mElements[component_number].getSizeBytes();
-    eSize *= mType.mElement.mArraySizes[component_number];
-
-    if (data.length != eSize) {
-        ALOGE("Field packer sizelength " + data.length +
-                                           " does not match component size " + eSize + ".");
-    }
-
-    mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
-                                 component_number, data, data.length);
-}
-*/
-
 void Allocation::generateMipmaps() {
-    rsAllocationGenerateMipmaps(mRS->mContext, getID());
+    rsAllocationGenerateMipmaps(mRS->getContext(), getID());
 }
 
 void Allocation::copy1DRangeFromUnchecked(uint32_t off, size_t count, const void *data,
@@ -240,7 +175,7 @@
         return;
     }
 
-    rsAllocation1DData(mRS->mContext, getIDSafe(), off, mSelectedLOD, count, data, dataLen);
+    rsAllocation1DData(mRS->getContext(), getIDSafe(), off, mSelectedLOD, count, data, dataLen);
 }
 
 void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const int32_t *d, size_t dataLen) {
@@ -266,7 +201,7 @@
 void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const Allocation *data,
         uint32_t dataOff) {
 
-    rsAllocationCopy2DRange(mRS->mContext, getIDSafe(), off, 0,
+    rsAllocationCopy2DRange(mRS->getContext(), getIDSafe(), off, 0,
                             mSelectedLOD, mSelectedFace,
                             count, 1, data->getIDSafe(), dataOff, 0,
                             data->mSelectedLOD, data->mSelectedFace);
@@ -285,28 +220,28 @@
 void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
                                  const int8_t *data, size_t dataLen) {
     validate2DRange(xoff, yoff, w, h);
-    rsAllocation2DData(mRS->mContext, getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
+    rsAllocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
                        w, h, data, dataLen);
 }
 
 void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
                                  const int16_t *data, size_t dataLen) {
     validate2DRange(xoff, yoff, w, h);
-    rsAllocation2DData(mRS->mContext, getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
+    rsAllocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
                        w, h, data, dataLen);
 }
 
 void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
                                  const int32_t *data, size_t dataLen) {
     validate2DRange(xoff, yoff, w, h);
-    rsAllocation2DData(mRS->mContext, getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
+    rsAllocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
                        w, h, data, dataLen);
 }
 
 void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
                                  const float *data, size_t dataLen) {
     validate2DRange(xoff, yoff, w, h);
-    rsAllocation2DData(mRS->mContext, getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
+    rsAllocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
                        w, h, data, dataLen);
 }
 
@@ -314,37 +249,13 @@
                                  const Allocation *data, size_t dataLen,
                                  uint32_t dataXoff, uint32_t dataYoff) {
     validate2DRange(xoff, yoff, w, h);
-    rsAllocationCopy2DRange(mRS->mContext, getIDSafe(), xoff, yoff,
+    rsAllocationCopy2DRange(mRS->getContext(), getIDSafe(), xoff, yoff,
                             mSelectedLOD, mSelectedFace,
                             w, h, data->getIDSafe(), dataXoff, dataYoff,
                             data->mSelectedLOD, data->mSelectedFace);
 }
 
 /*
-void copyTo(byte[] d) {
-    validateIsInt8();
-    mRS.validate();
-    mRS.nAllocationRead(getID(), d);
-}
-
-void copyTo(short[] d) {
-    validateIsInt16();
-    mRS.validate();
-    mRS.nAllocationRead(getID(), d);
-}
-
-void copyTo(int[] d) {
-    validateIsInt32();
-    mRS.validate();
-    mRS.nAllocationRead(getID(), d);
-}
-
-void copyTo(float[] d) {
-    validateIsFloat32();
-    mRS.validate();
-    mRS.nAllocationRead(getID(), d);
-}
-
 void resize(int dimX) {
     if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
         throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
@@ -378,9 +289,9 @@
 */
 
 
-android::sp<Allocation> Allocation::createTyped(RenderScript *rs, sp<const Type> type,
+android::sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
                         RsAllocationMipmapControl mips, uint32_t usage) {
-    void *id = rsAllocationCreateTyped(rs->mContext, type->getID(), mips, usage, 0);
+    void *id = rsAllocationCreateTyped(rs->getContext(), type->getID(), mips, usage, 0);
     if (id == 0) {
         ALOGE("Allocation creation failed.");
         return NULL;
@@ -388,92 +299,31 @@
     return new Allocation(id, rs, type, usage);
 }
 
-android::sp<Allocation> Allocation::createTyped(RenderScript *rs, sp<const Type> type,
+android::sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
                                     RsAllocationMipmapControl mips, uint32_t usage, void *pointer) {
-    void *id = rsAllocationCreateTyped(rs->mContext, type->getID(), mips, usage, (uint32_t)pointer);
+    void *id = rsAllocationCreateTyped(rs->getContext(), type->getID(), mips, usage, (uint32_t)pointer);
     if (id == 0) {
         ALOGE("Allocation creation failed.");
     }
     return new Allocation(id, rs, type, usage);
 }
 
-android::sp<Allocation> Allocation::createTyped(RenderScript *rs, sp<const Type> type,
+android::sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
         uint32_t usage) {
     return createTyped(rs, type, RS_ALLOCATION_MIPMAP_NONE, usage);
 }
 
-android::sp<Allocation> Allocation::createSized(RenderScript *rs, sp<const Element> e,
+android::sp<Allocation> Allocation::createSized(sp<RS> rs, sp<const Element> e,
         size_t count, uint32_t usage) {
 
     Type::Builder b(rs, e);
     b.setX(count);
     sp<const Type> t = b.create();
 
-    void *id = rsAllocationCreateTyped(rs->mContext, t->getID(),
+    void *id = rsAllocationCreateTyped(rs->getContext(), t->getID(),
         RS_ALLOCATION_MIPMAP_NONE, usage, 0);
     if (id == 0) {
         ALOGE("Allocation creation failed.");
     }
     return new Allocation(id, rs, t, usage);
 }
-
-
-/*
-SurfaceTexture getSurfaceTexture() {
-    if ((mUsage & USAGE_GRAPHICS_SURFACE_TEXTURE_INPUT_OPAQUE) == 0) {
-        throw new RSInvalidStateException("Allocation is not a surface texture.");
-    }
-
-    int id = mRS.nAllocationGetSurfaceTextureID(getID());
-    return new SurfaceTexture(id);
-
-}
-
-void setSurfaceTexture(SurfaceTexture sur) {
-    if ((mUsage & USAGE_IO_OUTPUT) == 0) {
-        throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
-    }
-
-    mRS.validate();
-    mRS.nAllocationSetSurfaceTexture(getID(), sur);
-}
-
-
-static Allocation createFromBitmapResource(RenderScript rs,
-                                                  Resources res,
-                                                  int id,
-                                                  MipmapControl mips,
-                                                  int usage) {
-
-    rs.validate();
-    Bitmap b = BitmapFactory.decodeResource(res, id);
-    Allocation alloc = createFromBitmap(rs, b, mips, usage);
-    b.recycle();
-    return alloc;
-}
-
-static Allocation createFromBitmapResource(RenderScript rs,
-                                                  Resources res,
-                                                  int id) {
-    return createFromBitmapResource(rs, res, id,
-                                    MipmapControl.MIPMAP_NONE,
-                                    USAGE_GRAPHICS_TEXTURE);
-}
-
-static Allocation createFromString(RenderScript rs,
-                                          String str,
-                                          int usage) {
-    rs.validate();
-    byte[] allocArray = NULL;
-    try {
-        allocArray = str.getBytes("UTF-8");
-        Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
-        alloc.copyFrom(allocArray);
-        return alloc;
-    }
-    catch (Exception e) {
-        throw new RSRuntimeException("Could not convert string to utf-8.");
-    }
-}
-*/
-
diff --git a/cpp/Allocation.h b/cpp/Allocation.h
deleted file mode 100644
index 7a893f8..0000000
--- a/cpp/Allocation.h
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * Copyright (C) 2012 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_ALLOCATION_H__
-#define __ANDROID_ALLOCATION_H__
-
-#include <pthread.h>
-#include <rs.h>
-
-#include "RenderScript.h"
-#include "Type.h"
-#include "Element.h"
-
-namespace android {
-namespace renderscriptCpp {
-
-class Allocation : public BaseObj {
-protected:
-    android::sp<const Type> mType;
-    uint32_t mUsage;
-    android::sp<Allocation> mAdaptedAllocation;
-
-    bool mConstrainedLOD;
-    bool mConstrainedFace;
-    bool mConstrainedY;
-    bool mConstrainedZ;
-    bool mReadAllowed;
-    bool mWriteAllowed;
-    uint32_t mSelectedY;
-    uint32_t mSelectedZ;
-    uint32_t mSelectedLOD;
-    RsAllocationCubemapFace mSelectedFace;
-
-    uint32_t mCurrentDimX;
-    uint32_t mCurrentDimY;
-    uint32_t mCurrentDimZ;
-    uint32_t mCurrentCount;
-
-
-    void * getIDSafe() const;
-    void updateCacheInfo(sp<const Type> t);
-
-    Allocation(void *id, RenderScript *rs, sp<const Type> t, uint32_t usage);
-
-    void validateIsInt32();
-    void validateIsInt16();
-    void validateIsInt8();
-    void validateIsFloat32();
-    void validateIsObject();
-
-    virtual void updateFromNative();
-
-    void validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h);
-
-public:
-    android::sp<const Type> getType() {
-        return mType;
-    }
-
-    void syncAll(RsAllocationUsageType srcLocation);
-    void ioSendOutput();
-    void ioGetInput();
-
-    //void copyFrom(BaseObj[] d);
-    //void copyFromUnchecked(int[] d);
-    //void copyFromUnchecked(short[] d);
-    //void copyFromUnchecked(byte[] d);
-    //void copyFromUnchecked(float[] d);
-    //void copyFrom(int[] d);
-    //void copyFrom(short[] d);
-    //void copyFrom(byte[] d);
-    //void copyFrom(float[] d);
-    //void setFromFieldPacker(int xoff, FieldPacker fp);
-    //void setFromFieldPacker(int xoff, int component_number, FieldPacker fp);
-    void generateMipmaps();
-    void copy1DRangeFromUnchecked(uint32_t off, size_t count, const void *data, size_t dataLen);
-    void copy1DRangeFrom(uint32_t off, size_t count, const int32_t* d, size_t dataLen);
-    void copy1DRangeFrom(uint32_t off, size_t count, const int16_t* d, size_t dataLen);
-    void copy1DRangeFrom(uint32_t off, size_t count, const int8_t* d, size_t dataLen);
-    void copy1DRangeFrom(uint32_t off, size_t count, const float* d, size_t dataLen);
-    void copy1DRangeFrom(uint32_t off, size_t count, const Allocation *data, uint32_t dataOff);
-
-    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                         const int32_t *data, size_t dataLen);
-    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                         const int16_t *data, size_t dataLen);
-    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                         const int8_t *data, size_t dataLen);
-    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                         const float *data, size_t dataLen);
-    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                         const Allocation *data, size_t dataLen,
-                         uint32_t dataXoff, uint32_t dataYoff);
-
-    //void copyTo(byte[] d);
-    //void copyTo(short[] d);
-    //void copyTo(int[] d);
-    //void copyTo(float[] d);
-    void resize(int dimX);
-    void resize(int dimX, int dimY);
-
-    static sp<Allocation> createTyped(RenderScript *rs, sp<const Type> type,
-                                   RsAllocationMipmapControl mips, uint32_t usage);
-    static sp<Allocation> createTyped(RenderScript *rs, sp<const Type> type,
-                                   RsAllocationMipmapControl mips, uint32_t usage, void * pointer);
-
-    static sp<Allocation> createTyped(RenderScript *rs, sp<const Type> type,
-                                   uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
-    static sp<Allocation> createSized(RenderScript *rs, sp<const Element> e, size_t count,
-                                   uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
-    //SurfaceTexture *getSurfaceTexture();
-    //void setSurfaceTexture(SurfaceTexture *sur);
-
-};
-
-}
-}
-
-#endif
diff --git a/cpp/Android.mk b/cpp/Android.mk
index ada2866..8756ce7 100644
--- a/cpp/Android.mk
+++ b/cpp/Android.mk
@@ -1,6 +1,18 @@
 LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
 
+ifeq "REL" "$(PLATFORM_VERSION_CODENAME)"
+  RS_VERSION := $(PLATFORM_SDK_VERSION)
+else
+  # Increment by 1 whenever this is not a final release build, since we want to
+  # be able to see the RS version number change during development.
+  # See build/core/version_defaults.mk for more information about this.
+  RS_VERSION := "(1 + $(PLATFORM_SDK_VERSION))"
+endif
+local_cflags_for_rs_cpp += -DRS_VERSION=$(RS_VERSION)
+
+LOCAL_CFLAGS += $(local_cflags_for_rs_cpp)
+
 LOCAL_SRC_FILES:= \
 	RenderScript.cpp \
 	BaseObj.cpp \
diff --git a/cpp/BaseObj.cpp b/cpp/BaseObj.cpp
index a370e8d..4a9faac 100644
--- a/cpp/BaseObj.cpp
+++ b/cpp/BaseObj.cpp
@@ -16,10 +16,7 @@
 
 #define LOG_TAG "libRS_cpp"
 
-#include <rs.h>
-
 #include "RenderScript.h"
-#include "BaseObj.h"
 
 using namespace android;
 using namespace renderscriptCpp;
@@ -36,7 +33,7 @@
 }
 
 
-BaseObj::BaseObj(void *id, RenderScript *rs) {
+BaseObj::BaseObj(void *id, sp<RS> rs) {
     mRS = rs;
     mID = id;
 }
@@ -48,14 +45,14 @@
 }
 
 BaseObj::~BaseObj() {
-    rsObjDestroy(mRS->mContext, mID);
+    rsObjDestroy(mRS->getContext(), mID);
     mRS = NULL;
     mID = NULL;
 }
 
 void BaseObj::updateFromNative() {
     const char *name = NULL;
-    rsaGetName(mRS, mID, &name);
+    rsaGetName(mRS->getContext(), mID, &name);
     mName = name;
 }
 
@@ -65,6 +62,3 @@
         return true;
     return mID == obj->mID;
 }
-
-
-
diff --git a/cpp/BaseObj.h b/cpp/BaseObj.h
deleted file mode 100644
index 23ae85c..0000000
--- a/cpp/BaseObj.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * Copyright (C) 2012 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_BASE_OBJ_H__
-#define __ANDROID_BASE_OBJ_H__
-
-
-#include "utils/RefBase.h"
-#include <pthread.h>
-#include <rs.h>
-
-#include "RenderScript.h"
-
-namespace android {
-namespace renderscriptCpp {
-
-
-class BaseObj : public android::LightRefBase<BaseObj> {
-protected:
-    friend class Element;
-    friend class Type;
-    friend class Allocation;
-    friend class Script;
-    friend class ScriptC;
-
-    void *mID;
-    RenderScript *mRS;
-    android::String8 mName;
-
-    void * getID() const;
-
-    BaseObj(void *id, RenderScript *rs);
-    void checkValid();
-
-    static void * getObjID(sp<const BaseObj> o);
-
-public:
-
-    virtual ~BaseObj();
-    virtual void updateFromNative();
-    virtual bool equals(const BaseObj *obj);
-};
-
-}
-}
-#endif
diff --git a/cpp/Element.cpp b/cpp/Element.cpp
index 2be4166..99b1069 100644
--- a/cpp/Element.cpp
+++ b/cpp/Element.cpp
@@ -14,14 +14,10 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "libRS_cpp"
-
-#include <utils/Log.h>
 #include <malloc.h>
 #include <string.h>
 
 #include "RenderScript.h"
-#include "Element.h"
 
 using namespace android;
 using namespace renderscriptCpp;
@@ -67,7 +63,7 @@
 }
 
 
-#define CREATE_USER(N, T) sp<const Element> Element::N(RenderScript *rs) { \
+#define CREATE_USER(N, T) sp<const Element> Element::N(sp<RS> rs) { \
     return createUser(rs, RS_TYPE_##T); \
 }
 CREATE_USER(BOOLEAN, BOOLEAN);
@@ -95,7 +91,7 @@
 CREATE_USER(MATRIX_3X3, MATRIX_3X3);
 CREATE_USER(MATRIX_2X2, MATRIX_2X2);
 
-#define CREATE_PIXEL(N, T, K) sp<const Element> Element::N(RenderScript *rs) { \
+#define CREATE_PIXEL(N, T, K) sp<const Element> Element::N(sp<RS> rs) { \
     return createPixel(rs, RS_TYPE_##T, RS_KIND_##K); \
 }
 CREATE_PIXEL(A_8, UNSIGNED_8, PIXEL_A);
@@ -104,13 +100,13 @@
 CREATE_PIXEL(RGBA_4444, UNSIGNED_4_4_4_4, PIXEL_RGBA);
 CREATE_PIXEL(RGBA_8888, UNSIGNED_8, PIXEL_RGBA);
 
-#define CREATE_VECTOR(N, T) sp<const Element> Element::N##_2(RenderScript *rs) { \
+#define CREATE_VECTOR(N, T) sp<const Element> Element::N##_2(sp<RS> rs) { \
     return createVector(rs, RS_TYPE_##T, 2); \
 } \
-sp<const Element> Element::N##_3(RenderScript *rs) { \
+sp<const Element> Element::N##_3(sp<RS> rs) { \
     return createVector(rs, RS_TYPE_##T, 3); \
 } \
-sp<const Element> Element::N##_4(RenderScript *rs) { \
+sp<const Element> Element::N##_4(sp<RS> rs) { \
     return createVector(rs, RS_TYPE_##T, 4); \
 }
 CREATE_VECTOR(U8, UNSIGNED_8);
@@ -148,8 +144,8 @@
     }
 }
 
-Element::Element(void *id, RenderScript *rs,
-                 android::Vector<sp</*const*/ Element> > &elements,
+Element::Element(void *id, sp<RS> rs,
+                 android::Vector<sp<Element> > &elements,
                  android::Vector<android::String8> &elementNames,
                  android::Vector<uint32_t> &arraySizes) : BaseObj(id, rs) {
     mSizeBytes = 0;
@@ -222,7 +218,7 @@
     return 0;
 }
 
-Element::Element(void *id, RenderScript *rs,
+Element::Element(void *id, sp<RS> rs,
                  RsDataType dt, RsDataKind dk, bool norm, uint32_t size) :
     BaseObj(id, rs)
 {
@@ -247,68 +243,25 @@
 Element::~Element() {
 }
 
-   /*
-    Element(int id, RenderScript rs) {
-        super(id, rs);
-    }
-    */
-
 void Element::updateFromNative() {
     BaseObj::updateFromNative();
-/*
-    // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
-    int[] dataBuffer = new int[5];
-    mRS.nElementGetNativeData(getID(), dataBuffer);
-
-    mNormalized = dataBuffer[2] == 1 ? true : false;
-    mVectorSize = dataBuffer[3];
-    mSize = 0;
-    for (DataType dt: DataType.values()) {
-        if(dt.mID == dataBuffer[0]){
-            mType = dt;
-            mSize = mType.mSize * mVectorSize;
-        }
-    }
-    for (DataKind dk: DataKind.values()) {
-        if(dk.mID == dataBuffer[1]){
-            mKind = dk;
-        }
-    }
-
-    int numSubElements = dataBuffer[4];
-    if(numSubElements > 0) {
-        mElements = new Element[numSubElements];
-        mElementNames = new String[numSubElements];
-        mArraySizes = new int[numSubElements];
-        mOffsetInBytes = new int[numSubElements];
-
-        int[] subElementIds = new int[numSubElements];
-        mRS.nElementGetSubElements(getID(), subElementIds, mElementNames, mArraySizes);
-        for(int i = 0; i < numSubElements; i ++) {
-            mElements[i] = new Element(subElementIds[i], mRS);
-            mElements[i].updateFromNative();
-            mOffsetInBytes[i] = mSize;
-            mSize += mElements[i].mSize * mArraySizes[i];
-        }
-    }
-    */
     updateVisibleSubElements();
 }
 
-sp<const Element> Element::createUser(RenderScript *rs, RsDataType dt) {
-    void * id = rsElementCreate(rs->mContext, dt, RS_KIND_USER, false, 1);
+sp<const Element> Element::createUser(sp<RS> rs, RsDataType dt) {
+    void * id = rsElementCreate(rs->getContext(), dt, RS_KIND_USER, false, 1);
     return new Element(id, rs, dt, RS_KIND_USER, false, 1);
 }
 
-sp<const Element> Element::createVector(RenderScript *rs, RsDataType dt, uint32_t size) {
+sp<const Element> Element::createVector(sp<RS> rs, RsDataType dt, uint32_t size) {
     if (size < 2 || size > 4) {
         rs->throwError("Vector size out of range 2-4.");
     }
-    void *id = rsElementCreate(rs->mContext, dt, RS_KIND_USER, false, size);
+    void *id = rsElementCreate(rs->getContext(), dt, RS_KIND_USER, false, size);
     return new Element(id, rs, dt, RS_KIND_USER, false, size);
 }
 
-sp<const Element> Element::createPixel(RenderScript *rs, RsDataType dt, RsDataKind dk) {
+sp<const Element> Element::createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk) {
     if (!(dk == RS_KIND_PIXEL_L ||
           dk == RS_KIND_PIXEL_A ||
           dk == RS_KIND_PIXEL_LA ||
@@ -355,7 +308,7 @@
         break;
     }
 
-    void * id = rsElementCreate(rs->mContext, dt, dk, true, size);
+    void * id = rsElementCreate(rs->getContext(), dt, dk, true, size);
     return new Element(id, rs, dt, dk, true, size);
 }
 
@@ -375,7 +328,7 @@
             (mVectorSize == e->mVectorSize));
 }
 
-Element::Builder::Builder(RenderScript *rs) {
+Element::Builder::Builder(sp<RS> rs) {
     mRS = rs;
     mSkipPadding = false;
 }
@@ -417,7 +370,7 @@
         sizeArray[ct] = mElementNames[ct].length();
     }
 
-    void *id = rsElementCreate2(mRS->mContext,
+    void *id = rsElementCreate2(mRS->getContext(),
                                 (RsElement *)elementArray, fieldCount,
                                 nameArray, fieldCount * sizeof(size_t),  sizeArray,
                                 (const uint32_t *)mArraySizes.array(), fieldCount);
diff --git a/cpp/Element.h b/cpp/Element.h
deleted file mode 100644
index dea3570..0000000
--- a/cpp/Element.h
+++ /dev/null
@@ -1,204 +0,0 @@
-/*
- * Copyright (C) 2012 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_ELEMENT_H__
-#define __ANDROID_ELEMENT_H__
-
-#include <rs.h>
-#include "RenderScript.h"
-#include "BaseObj.h"
-
-namespace android {
-namespace renderscriptCpp {
-
-class Element : public BaseObj {
-public:
-    /**
-     * Return if a element is too complex for use as a data source for a Mesh or
-     * a Program.
-     *
-     * @return boolean
-     */
-    bool isComplex();
-
-    /**
-    * @hide
-    * @return number of sub-elements in this element
-    */
-    size_t getSubElementCount() {
-        return mVisibleElementMap.size();
-    }
-
-    /**
-    * @hide
-    * @param index index of the sub-element to return
-    * @return sub-element in this element at given index
-    */
-    sp<const Element> getSubElement(uint32_t index);
-
-    /**
-    * @hide
-    * @param index index of the sub-element
-    * @return sub-element in this element at given index
-    */
-    const char * getSubElementName(uint32_t index);
-
-    /**
-    * @hide
-    * @param index index of the sub-element
-    * @return array size of sub-element in this element at given index
-    */
-    size_t getSubElementArraySize(uint32_t index);
-
-    /**
-    * @hide
-    * @param index index of the sub-element
-    * @return offset in bytes of sub-element in this element at given index
-    */
-    uint32_t getSubElementOffsetBytes(uint32_t index);
-
-    /**
-    * @hide
-    * @return element data type
-    */
-    RsDataType getDataType() const {
-        return mType;
-    }
-
-    /**
-    * @hide
-    * @return element data kind
-    */
-    RsDataKind getDataKind() const {
-        return mKind;
-    }
-
-    size_t getSizeBytes() const {
-        return mSizeBytes;
-    }
-
-
-    static sp<const Element> BOOLEAN(RenderScript *rs);
-    static sp<const Element> U8(RenderScript *rs);
-    static sp<const Element> I8(RenderScript *rs);
-    static sp<const Element> U16(RenderScript *rs);
-    static sp<const Element> I16(RenderScript *rs);
-    static sp<const Element> U32(RenderScript *rs);
-    static sp<const Element> I32(RenderScript *rs);
-    static sp<const Element> U64(RenderScript *rs);
-    static sp<const Element> I64(RenderScript *rs);
-    static sp<const Element> F32(RenderScript *rs);
-    static sp<const Element> F64(RenderScript *rs);
-    static sp<const Element> ELEMENT(RenderScript *rs);
-    static sp<const Element> TYPE(RenderScript *rs);
-    static sp<const Element> ALLOCATION(RenderScript *rs);
-    static sp<const Element> SAMPLER(RenderScript *rs);
-    static sp<const Element> SCRIPT(RenderScript *rs);
-    static sp<const Element> MESH(RenderScript *rs);
-    static sp<const Element> PROGRAM_FRAGMENT(RenderScript *rs);
-    static sp<const Element> PROGRAM_VERTEX(RenderScript *rs);
-    static sp<const Element> PROGRAM_RASTER(RenderScript *rs);
-    static sp<const Element> PROGRAM_STORE(RenderScript *rs);
-
-    static sp<const Element> A_8(RenderScript *rs);
-    static sp<const Element> RGB_565(RenderScript *rs);
-    static sp<const Element> RGB_888(RenderScript *rs);
-    static sp<const Element> RGBA_5551(RenderScript *rs);
-    static sp<const Element> RGBA_4444(RenderScript *rs);
-    static sp<const Element> RGBA_8888(RenderScript *rs);
-
-    static sp<const Element> F32_2(RenderScript *rs);
-    static sp<const Element> F32_3(RenderScript *rs);
-    static sp<const Element> F32_4(RenderScript *rs);
-    static sp<const Element> F64_2(RenderScript *rs);
-    static sp<const Element> F64_3(RenderScript *rs);
-    static sp<const Element> F64_4(RenderScript *rs);
-    static sp<const Element> U8_2(RenderScript *rs);
-    static sp<const Element> U8_3(RenderScript *rs);
-    static sp<const Element> U8_4(RenderScript *rs);
-    static sp<const Element> I8_2(RenderScript *rs);
-    static sp<const Element> I8_3(RenderScript *rs);
-    static sp<const Element> I8_4(RenderScript *rs);
-    static sp<const Element> U16_2(RenderScript *rs);
-    static sp<const Element> U16_3(RenderScript *rs);
-    static sp<const Element> U16_4(RenderScript *rs);
-    static sp<const Element> I16_2(RenderScript *rs);
-    static sp<const Element> I16_3(RenderScript *rs);
-    static sp<const Element> I16_4(RenderScript *rs);
-    static sp<const Element> U32_2(RenderScript *rs);
-    static sp<const Element> U32_3(RenderScript *rs);
-    static sp<const Element> U32_4(RenderScript *rs);
-    static sp<const Element> I32_2(RenderScript *rs);
-    static sp<const Element> I32_3(RenderScript *rs);
-    static sp<const Element> I32_4(RenderScript *rs);
-    static sp<const Element> U64_2(RenderScript *rs);
-    static sp<const Element> U64_3(RenderScript *rs);
-    static sp<const Element> U64_4(RenderScript *rs);
-    static sp<const Element> I64_2(RenderScript *rs);
-    static sp<const Element> I64_3(RenderScript *rs);
-    static sp<const Element> I64_4(RenderScript *rs);
-    static sp<const Element> MATRIX_4X4(RenderScript *rs);
-    static sp<const Element> MATRIX_3X3(RenderScript *rs);
-    static sp<const Element> MATRIX_2X2(RenderScript *rs);
-
-    Element(void *id, RenderScript *rs,
-            android::Vector<sp</*const*/ Element> > &elements,
-            android::Vector<android::String8> &elementNames,
-            android::Vector<uint32_t> &arraySizes);
-    Element(void *id, RenderScript *rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
-    Element(RenderScript *rs);
-    virtual ~Element();
-
-    void updateFromNative();
-    static sp<const Element> createUser(RenderScript *rs, RsDataType dt);
-    static sp<const Element> createVector(RenderScript *rs, RsDataType dt, uint32_t size);
-    static sp<const Element> createPixel(RenderScript *rs, RsDataType dt, RsDataKind dk);
-    bool isCompatible(sp<const Element>e);
-
-    class Builder {
-    private:
-        RenderScript *mRS;
-        android::Vector<sp</*const*/ Element> > mElements;
-        android::Vector<android::String8> mElementNames;
-        android::Vector<uint32_t> mArraySizes;
-        bool mSkipPadding;
-
-    public:
-        Builder(RenderScript *rs);
-        ~Builder();
-        void add(sp</*const*/ Element>, android::String8 &name, uint32_t arraySize = 1);
-        sp<const Element> create();
-    };
-
-private:
-    void updateVisibleSubElements();
-
-    android::Vector<sp</*const*/ Element> > mElements;
-    android::Vector<android::String8> mElementNames;
-    android::Vector<uint32_t> mArraySizes;
-    android::Vector<uint32_t> mVisibleElementMap;
-    android::Vector<uint32_t> mOffsetInBytes;
-
-    RsDataType mType;
-    RsDataKind mKind;
-    bool mNormalized;
-    size_t mSizeBytes;
-    size_t mVectorSize;
-};
-
-}
-}
-#endif
diff --git a/cpp/RenderScript.cpp b/cpp/RenderScript.cpp
index 889f27b..3160b4d 100644
--- a/cpp/RenderScript.cpp
+++ b/cpp/RenderScript.cpp
@@ -19,6 +19,7 @@
 #include <utils/Log.h>
 #include <malloc.h>
 #include <string.h>
+#include <pthread.h>
 
 #include "RenderScript.h"
 #include "rs.h"
@@ -26,10 +27,10 @@
 using namespace android;
 using namespace renderscriptCpp;
 
-bool RenderScript::gInitialized = false;
-pthread_mutex_t RenderScript::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
+bool RS::gInitialized = false;
+pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
 
-RenderScript::RenderScript() {
+RS::RS() {
     mDev = NULL;
     mContext = NULL;
     mErrorFunc = NULL;
@@ -39,7 +40,7 @@
     memset(&mElements, 0, sizeof(mElements));
 }
 
-RenderScript::~RenderScript() {
+RS::~RS() {
     mMessageRun = false;
 
     rsContextDeinitToClient(mContext);
@@ -53,7 +54,11 @@
     mDev = NULL;
 }
 
-bool RenderScript::init(int targetApi) {
+bool RS::init() {
+    return RS::init(RS_VERSION);
+}
+
+bool RS::init(int targetApi) {
     mDev = rsDeviceCreate();
     if (mDev == 0) {
         ALOGE("Device creation failed");
@@ -66,12 +71,11 @@
         return false;
     }
 
-
     pid_t mNativeMessageThreadId;
 
     int status = pthread_create(&mMessageThreadId, NULL, threadProc, this);
     if (status) {
-        ALOGE("Failed to start RenderScript message thread.");
+        ALOGE("Failed to start RS message thread.");
         return false;
     }
     // Wait for the message thread to be active.
@@ -82,15 +86,15 @@
     return true;
 }
 
-void RenderScript::throwError(const char *err) const {
+void RS::throwError(const char *err) const {
     ALOGE("RS CPP error: %s", err);
     int * v = NULL;
     v[0] = 0;
 }
 
 
-void * RenderScript::threadProc(void *vrsc) {
-    RenderScript *rs = static_cast<RenderScript *>(vrsc);
+void * RS::threadProc(void *vrsc) {
+    RS *rs = static_cast<RS *>(vrsc);
     size_t rbuf_size = 256;
     void * rbuf = malloc(rbuf_size);
 
@@ -110,7 +114,7 @@
             rbuf = realloc(rbuf, rbuf_size);
         }
         if (!rbuf) {
-            ALOGE("RenderScript::message handler realloc error %zu", rbuf_size);
+            ALOGE("RS::message handler realloc error %zu", rbuf_size);
             // No clean way to recover now?
         }
         rsContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
@@ -139,30 +143,21 @@
             break;
 
         default:
-            ALOGE("RenderScript unknown message type %i", r);
+            ALOGE("RS unknown message type %i", r);
         }
     }
 
     if (rbuf) {
         free(rbuf);
     }
-    ALOGE("RenderScript Message thread exiting.");
+    ALOGE("RS Message thread exiting.");
     return NULL;
 }
 
-void RenderScript::setErrorHandler(ErrorHandlerFunc_t func) {
+void RS::setErrorHandler(ErrorHandlerFunc_t func) {
     mErrorFunc = func;
 }
 
-void RenderScript::setMessageHandler(MessageHandlerFunc_t func) {
+void RS::setMessageHandler(MessageHandlerFunc_t func) {
     mMessageFunc  = func;
 }
-
-void RenderScript::contextDump() {
-}
-
-void RenderScript::finish() {
-
-}
-
-
diff --git a/cpp/RenderScript.h b/cpp/RenderScript.h
index e6e7279..9e74922 100644
--- a/cpp/RenderScript.h
+++ b/cpp/RenderScript.h
@@ -17,145 +17,7 @@
 #ifndef ANDROID_RENDERSCRIPT_H
 #define ANDROID_RENDERSCRIPT_H
 
-
-#include <pthread.h>
-#include <utils/String8.h>
-#include <utils/Vector.h>
-
 #include "rsDefines.h"
+#include "rsCppStructs.h"
 
-namespace android {
-namespace renderscriptCpp {
-
-class Element;
-class Type;
-class Allocation;
-
-class RenderScript {
-    friend class BaseObj;
-    friend class Allocation;
-    friend class Element;
-    friend class Type;
-    friend class Script;
-    friend class ScriptC;
-
-public:
-    RenderScript();
-    virtual ~RenderScript();
-
-    typedef void (*ErrorHandlerFunc_t)(uint32_t errorNum, const char *errorText);
-    typedef void (*MessageHandlerFunc_t)(uint32_t msgNum, const void *msgData, size_t msgLen);
-
-
-    void setErrorHandler(ErrorHandlerFunc_t func);
-    ErrorHandlerFunc_t getErrorHandler() {return mErrorFunc;}
-
-    void setMessageHandler(MessageHandlerFunc_t func);
-    MessageHandlerFunc_t getMessageHandler() {return mMessageFunc;}
-
-    bool init(int targetApi);
-    void contextDump();
-    void finish();
-
-private:
-    static bool gInitialized;
-    static pthread_mutex_t gInitMutex;
-
-    pthread_t mMessageThreadId;
-    pid_t mNativeMessageThreadId;
-    bool mMessageRun;
-
-    RsDevice mDev;
-    RsContext mContext;
-
-    ErrorHandlerFunc_t mErrorFunc;
-    MessageHandlerFunc_t mMessageFunc;
-
-    struct {
-        Element *U8;
-        Element *I8;
-        Element *U16;
-        Element *I16;
-        Element *U32;
-        Element *I32;
-        Element *U64;
-        Element *I64;
-        Element *F32;
-        Element *F64;
-        Element *BOOLEAN;
-
-        Element *ELEMENT;
-        Element *TYPE;
-        Element *ALLOCATION;
-        Element *SAMPLER;
-        Element *SCRIPT;
-        Element *MESH;
-        Element *PROGRAM_FRAGMENT;
-        Element *PROGRAM_VERTEX;
-        Element *PROGRAM_RASTER;
-        Element *PROGRAM_STORE;
-
-        Element *A_8;
-        Element *RGB_565;
-        Element *RGB_888;
-        Element *RGBA_5551;
-        Element *RGBA_4444;
-        Element *RGBA_8888;
-
-        Element *FLOAT_2;
-        Element *FLOAT_3;
-        Element *FLOAT_4;
-
-        Element *DOUBLE_2;
-        Element *DOUBLE_3;
-        Element *DOUBLE_4;
-
-        Element *UCHAR_2;
-        Element *UCHAR_3;
-        Element *UCHAR_4;
-
-        Element *CHAR_2;
-        Element *CHAR_3;
-        Element *CHAR_4;
-
-        Element *USHORT_2;
-        Element *USHORT_3;
-        Element *USHORT_4;
-
-        Element *SHORT_2;
-        Element *SHORT_3;
-        Element *SHORT_4;
-
-        Element *UINT_2;
-        Element *UINT_3;
-        Element *UINT_4;
-
-        Element *INT_2;
-        Element *INT_3;
-        Element *INT_4;
-
-        Element *ULONG_2;
-        Element *ULONG_3;
-        Element *ULONG_4;
-
-        Element *LONG_2;
-        Element *LONG_3;
-        Element *LONG_4;
-
-        Element *MATRIX_4X4;
-        Element *MATRIX_3X3;
-        Element *MATRIX_2X2;
-    } mElements;
-
-
-
-    void throwError(const char *err) const;
-
-    static void * threadProc(void *);
-
-};
-
-}
-}
 #endif
-
diff --git a/cpp/Script.cpp b/cpp/Script.cpp
index 63d5fab..074c463 100644
--- a/cpp/Script.cpp
+++ b/cpp/Script.cpp
@@ -14,22 +14,16 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "libRS_cpp"
-
 #include <utils/Log.h>
 #include <malloc.h>
 
 #include "RenderScript.h"
-#include "Element.h"
-#include "Type.h"
-#include "Allocation.h"
-#include "Script.h"
 
 using namespace android;
 using namespace renderscriptCpp;
 
 void Script::invoke(uint32_t slot, const void *v, size_t len) const {
-    rsScriptInvokeV(mRS->mContext, getID(), slot, v, len);
+    rsScriptInvokeV(mRS->getContext(), getID(), slot, v, len);
 }
 
 void Script::forEach(uint32_t slot, sp<const Allocation> ain, sp<const Allocation> aout,
@@ -39,34 +33,28 @@
     }
     void *in_id = BaseObj::getObjID(ain);
     void *out_id = BaseObj::getObjID(aout);
-    rsScriptForEach(mRS->mContext, getID(), slot, in_id, out_id, usr, usrLen);
+    rsScriptForEach(mRS->getContext(), getID(), slot, in_id, out_id, usr, usrLen);
 }
 
 
-Script::Script(void *id, RenderScript *rs) : BaseObj(id, rs) {
+Script::Script(void *id, sp<RS> rs) : BaseObj(id, rs) {
 }
 
 
 void Script::bindAllocation(sp<Allocation> va, uint32_t slot) const {
-    rsScriptBindAllocation(mRS->mContext, getID(), BaseObj::getObjID(va), slot);
+    rsScriptBindAllocation(mRS->getContext(), getID(), BaseObj::getObjID(va), slot);
 }
 
 
 void Script::setVar(uint32_t index, sp<const BaseObj> o) const {
-    rsScriptSetVarObj(mRS->mContext, getID(), index, (o == NULL) ? 0 : o->getID());
+    rsScriptSetVarObj(mRS->getContext(), getID(), index, (o == NULL) ? 0 : o->getID());
 }
 
 void Script::setVar(uint32_t index, const void *v, size_t len) const {
-    rsScriptSetVarV(mRS->mContext, getID(), index, v, len);
+    rsScriptSetVarV(mRS->getContext(), getID(), index, v, len);
 }
 
-
-
-void Script::FieldBase::init(RenderScript *rs, uint32_t dimx, uint32_t usages) {
+void Script::FieldBase::init(sp<RS> rs, uint32_t dimx, uint32_t usages) {
     mAllocation = Allocation::createSized(rs, mElement, dimx, RS_ALLOCATION_USAGE_SCRIPT | usages);
 }
 
-//Script::FieldBase::FieldBase() {
-//}
-
-
diff --git a/cpp/Script.h b/cpp/Script.h
deleted file mode 100644
index 235560a..0000000
--- a/cpp/Script.h
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * Copyright (C) 2008-2012 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_SCRIPT_H__
-#define __ANDROID_SCRIPT_H__
-
-#include <pthread.h>
-#include <rs.h>
-
-#include "RenderScript.h"
-#include "Allocation.h"
-
-namespace android {
-namespace renderscriptCpp {
-
-
-class Type;
-class Element;
-class Allocation;
-
-class Script : public BaseObj {
-private:
-
-protected:
-    Script(void *id, RenderScript *rs);
-    void forEach(uint32_t slot, sp<const Allocation> in, sp<const Allocation> out,
-            const void *v, size_t) const;
-    void bindAllocation(sp<Allocation> va, uint32_t slot) const;
-    void setVar(uint32_t index, const void *, size_t len) const;
-    void setVar(uint32_t index, sp<const BaseObj> o) const;
-    void invoke(uint32_t slot, const void *v, size_t len) const;
-
-
-    void invoke(uint32_t slot) const {
-        invoke(slot, NULL, 0);
-    }
-    void setVar(uint32_t index, float v) const {
-        setVar(index, &v, sizeof(v));
-    }
-    void setVar(uint32_t index, double v) const {
-        setVar(index, &v, sizeof(v));
-    }
-    void setVar(uint32_t index, int32_t v) const {
-        setVar(index, &v, sizeof(v));
-    }
-    void setVar(uint32_t index, int64_t v) const {
-        setVar(index, &v, sizeof(v));
-    }
-    void setVar(uint32_t index, bool v) const {
-        setVar(index, &v, sizeof(v));
-    }
-
-public:
-    class FieldBase {
-    protected:
-        sp<const Element> mElement;
-        sp<Allocation> mAllocation;
-
-        void init(RenderScript *rs, uint32_t dimx, uint32_t usages = 0);
-
-    public:
-        sp<const Element> getElement() {
-            return mElement;
-        }
-
-        sp<const Type> getType() {
-            return mAllocation->getType();
-        }
-
-        sp<const Allocation> getAllocation() {
-            return mAllocation;
-        }
-
-        //void updateAllocation();
-    };
-};
-
-}
-}
-#endif
diff --git a/cpp/ScriptC.cpp b/cpp/ScriptC.cpp
index 39b17d4..3a1909d 100644
--- a/cpp/ScriptC.cpp
+++ b/cpp/ScriptC.cpp
@@ -14,22 +14,20 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "libRS_cpp"
-
 #include <utils/Log.h>
 #include <malloc.h>
 
-#include "ScriptC.h"
+#include "RenderScript.h"
 
 using namespace android;
 using namespace renderscriptCpp;
 
-ScriptC::ScriptC(RenderScript *rs,
+ScriptC::ScriptC(sp<RS> rs,
                  const void *codeTxt, size_t codeLength,
                  const char *cachedName, size_t cachedNameLength,
                  const char *cacheDir, size_t cacheDirLength)
 : Script(NULL, rs) {
-    mID = rsScriptCCreate(rs->mContext, cachedName, cachedNameLength,
+    mID = rsScriptCCreate(rs->getContext(), cachedName, cachedNameLength,
                           cacheDir, cacheDirLength, (const char *)codeTxt, codeLength);
 }
 
diff --git a/cpp/ScriptC.h b/cpp/ScriptC.h
deleted file mode 100644
index 8711b8d..0000000
--- a/cpp/ScriptC.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (C) 2008-2012 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_SCRIPTC_H__
-#define __ANDROID_SCRIPTC_H__
-
-#include <pthread.h>
-#include <rs.h>
-
-#include "Script.h"
-
-namespace android {
-namespace renderscriptCpp {
-
-class ScriptC : public Script {
-protected:
-    ScriptC(RenderScript *rs,
-            const void *codeTxt, size_t codeLength,
-            const char *cachedName, size_t cachedNameLength,
-            const char *cacheDir, size_t cacheDirLength);
-
-};
-
-
-}
-}
-#endif
diff --git a/cpp/Type.cpp b/cpp/Type.cpp
index 23327b0..1b64c30 100644
--- a/cpp/Type.cpp
+++ b/cpp/Type.cpp
@@ -21,8 +21,6 @@
 #include <string.h>
 
 #include "RenderScript.h"
-#include "Element.h"
-#include "Type.h"
 
 using namespace android;
 using namespace renderscriptCpp;
@@ -64,7 +62,7 @@
 }
 
 
-Type::Type(void *id, RenderScript *rs) : BaseObj(id, rs) {
+Type::Type(void *id, sp<RS> rs) : BaseObj(id, rs) {
     mDimX = 0;
     mDimY = 0;
     mDimZ = 0;
@@ -96,7 +94,7 @@
     */
 }
 
-Type::Builder::Builder(RenderScript *rs, sp<const Element> e) {
+Type::Builder::Builder(sp<RS> rs, sp<const Element> e) {
     mRS = rs;
     mElement = e;
     mDimX = 0;
@@ -148,7 +146,7 @@
         }
     }
 
-    void * id = rsTypeCreate(mRS->mContext, mElement->getID(), mDimX, mDimY, mDimZ,
+    void * id = rsTypeCreate(mRS->getContext(), mElement->getID(), mDimX, mDimY, mDimZ,
             mDimMipmaps, mDimFaces);
     Type *t = new Type(id, mRS);
     t->mElement = mElement;
diff --git a/cpp/Type.h b/cpp/Type.h
deleted file mode 100644
index 0890c04..0000000
--- a/cpp/Type.h
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * Copyright (C) 2012 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_TYPE_H__
-#define __ANDROID_TYPE_H__
-
-#include <rs.h>
-#include "RenderScript.h"
-#include "Element.h"
-
-namespace android {
-namespace renderscriptCpp {
-
-class Type : public BaseObj {
-protected:
-    friend class Allocation;
-
-    uint32_t mDimX;
-    uint32_t mDimY;
-    uint32_t mDimZ;
-    bool mDimMipmaps;
-    bool mDimFaces;
-    size_t mElementCount;
-    sp<const Element> mElement;
-
-    void calcElementCount();
-    virtual void updateFromNative();
-
-public:
-
-    sp<const Element> getElement() const {
-        return mElement;
-    }
-
-    uint32_t getX() const {
-        return mDimX;
-    }
-
-    uint32_t getY() const {
-        return mDimY;
-    }
-
-    uint32_t getZ() const {
-        return mDimZ;
-    }
-
-    bool hasMipmaps() const {
-        return mDimMipmaps;
-    }
-
-    bool hasFaces() const {
-        return mDimFaces;
-    }
-
-    size_t getCount() const {
-        return mElementCount;
-    }
-
-    size_t getSizeBytes() const {
-        return mElementCount * mElement->getSizeBytes();
-    }
-
-
-    Type(void *id, RenderScript *rs);
-
-
-    class Builder {
-    protected:
-        RenderScript *mRS;
-        uint32_t mDimX;
-        uint32_t mDimY;
-        uint32_t mDimZ;
-        bool mDimMipmaps;
-        bool mDimFaces;
-        sp<const Element> mElement;
-
-    public:
-        Builder(RenderScript *rs, sp<const Element> e);
-
-        void setX(uint32_t value);
-        void setY(int value);
-        void setMipmaps(bool value);
-        void setFaces(bool value);
-        sp<const Type> create();
-    };
-
-};
-
-}
-}
-#endif
diff --git a/cpp/rsCppStructs.h b/cpp/rsCppStructs.h
new file mode 100644
index 0000000..6d0c89d
--- /dev/null
+++ b/cpp/rsCppStructs.h
@@ -0,0 +1,529 @@
+/*
+ * Copyright (C) 2012 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_RSCPPSTRUCTS_H
+#define ANDROID_RSCPPSTRUCTS_H
+
+#include <utils/String8.h>
+#include <utils/Vector.h>
+#include "utils/RefBase.h"
+
+#include <rs.h>
+
+namespace android {
+namespace renderscriptCpp {
+
+typedef void (*ErrorHandlerFunc_t)(uint32_t errorNum, const char *errorText);
+typedef void (*MessageHandlerFunc_t)(uint32_t msgNum, const void *msgData, size_t msgLen);
+
+class RS;
+class BaseObj;
+class Element;
+class Type;
+class Allocation;
+class Script;
+class ScriptC;
+
+class RS : public android::LightRefBase<RS> {
+
+ public:
+    RS();
+    virtual ~RS();
+
+    bool init();
+
+    void setErrorHandler(ErrorHandlerFunc_t func);
+    ErrorHandlerFunc_t getErrorHandler() { return mErrorFunc; }
+
+    void setMessageHandler(MessageHandlerFunc_t func);
+    MessageHandlerFunc_t getMessageHandler() { return mMessageFunc; }
+
+    void throwError(const char *err) const;
+
+    RsContext getContext() { return mContext; }
+
+ private:
+    bool init(int targetApi);
+    static void * threadProc(void *);
+
+    static bool gInitialized;
+    static pthread_mutex_t gInitMutex;
+
+    pthread_t mMessageThreadId;
+    pid_t mNativeMessageThreadId;
+    bool mMessageRun;
+
+    RsDevice mDev;
+    RsContext mContext;
+
+    ErrorHandlerFunc_t mErrorFunc;
+    MessageHandlerFunc_t mMessageFunc;
+
+    struct {
+        Element *U8;
+        Element *I8;
+        Element *U16;
+        Element *I16;
+        Element *U32;
+        Element *I32;
+        Element *U64;
+        Element *I64;
+        Element *F32;
+        Element *F64;
+        Element *BOOLEAN;
+
+        Element *ELEMENT;
+        Element *TYPE;
+        Element *ALLOCATION;
+        Element *SAMPLER;
+        Element *SCRIPT;
+        Element *MESH;
+        Element *PROGRAM_FRAGMENT;
+        Element *PROGRAM_VERTEX;
+        Element *PROGRAM_RASTER;
+        Element *PROGRAM_STORE;
+
+        Element *A_8;
+        Element *RGB_565;
+        Element *RGB_888;
+        Element *RGBA_5551;
+        Element *RGBA_4444;
+        Element *RGBA_8888;
+
+        Element *FLOAT_2;
+        Element *FLOAT_3;
+        Element *FLOAT_4;
+
+        Element *DOUBLE_2;
+        Element *DOUBLE_3;
+        Element *DOUBLE_4;
+
+        Element *UCHAR_2;
+        Element *UCHAR_3;
+        Element *UCHAR_4;
+
+        Element *CHAR_2;
+        Element *CHAR_3;
+        Element *CHAR_4;
+
+        Element *USHORT_2;
+        Element *USHORT_3;
+        Element *USHORT_4;
+
+        Element *SHORT_2;
+        Element *SHORT_3;
+        Element *SHORT_4;
+
+        Element *UINT_2;
+        Element *UINT_3;
+        Element *UINT_4;
+
+        Element *INT_2;
+        Element *INT_3;
+        Element *INT_4;
+
+        Element *ULONG_2;
+        Element *ULONG_3;
+        Element *ULONG_4;
+
+        Element *LONG_2;
+        Element *LONG_3;
+        Element *LONG_4;
+
+        Element *MATRIX_4X4;
+        Element *MATRIX_3X3;
+        Element *MATRIX_2X2;
+    } mElements;
+
+};
+
+class BaseObj : public android::LightRefBase<BaseObj> {
+protected:
+    void *mID;
+    sp<RS> mRS;
+    String8 mName;
+
+    BaseObj(void *id, sp<RS> rs);
+    void checkValid();
+
+    static void * getObjID(sp<const BaseObj> o);
+
+public:
+
+    void * getID() const;
+    virtual ~BaseObj();
+    virtual void updateFromNative();
+    virtual bool equals(const BaseObj *obj);
+};
+
+
+class Allocation : public BaseObj {
+protected:
+    android::sp<const Type> mType;
+    uint32_t mUsage;
+    android::sp<Allocation> mAdaptedAllocation;
+
+    bool mConstrainedLOD;
+    bool mConstrainedFace;
+    bool mConstrainedY;
+    bool mConstrainedZ;
+    bool mReadAllowed;
+    bool mWriteAllowed;
+    uint32_t mSelectedY;
+    uint32_t mSelectedZ;
+    uint32_t mSelectedLOD;
+    RsAllocationCubemapFace mSelectedFace;
+
+    uint32_t mCurrentDimX;
+    uint32_t mCurrentDimY;
+    uint32_t mCurrentDimZ;
+    uint32_t mCurrentCount;
+
+    void * getIDSafe() const;
+    void updateCacheInfo(sp<const Type> t);
+
+    Allocation(void *id, sp<RS> rs, sp<const Type> t, uint32_t usage);
+
+    void validateIsInt32();
+    void validateIsInt16();
+    void validateIsInt8();
+    void validateIsFloat32();
+    void validateIsObject();
+
+    virtual void updateFromNative();
+
+    void validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h);
+
+public:
+    android::sp<const Type> getType() {
+        return mType;
+    }
+
+    void syncAll(RsAllocationUsageType srcLocation);
+    void ioSendOutput();
+    void ioGetInput();
+
+    void generateMipmaps();
+    void copy1DRangeFromUnchecked(uint32_t off, size_t count, const void *data, size_t dataLen);
+    void copy1DRangeFrom(uint32_t off, size_t count, const int32_t* d, size_t dataLen);
+    void copy1DRangeFrom(uint32_t off, size_t count, const int16_t* d, size_t dataLen);
+    void copy1DRangeFrom(uint32_t off, size_t count, const int8_t* d, size_t dataLen);
+    void copy1DRangeFrom(uint32_t off, size_t count, const float* d, size_t dataLen);
+    void copy1DRangeFrom(uint32_t off, size_t count, const Allocation *data, uint32_t dataOff);
+
+    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
+                         const int32_t *data, size_t dataLen);
+    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
+                         const int16_t *data, size_t dataLen);
+    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
+                         const int8_t *data, size_t dataLen);
+    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
+                         const float *data, size_t dataLen);
+    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
+                         const Allocation *data, size_t dataLen,
+                         uint32_t dataXoff, uint32_t dataYoff);
+
+    void resize(int dimX);
+    void resize(int dimX, int dimY);
+
+    static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
+                                   RsAllocationMipmapControl mips, uint32_t usage);
+    static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
+                                   RsAllocationMipmapControl mips, uint32_t usage, void * pointer);
+
+    static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
+                                   uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
+    static sp<Allocation> createSized(sp<RS> rs, sp<const Element> e, size_t count,
+                                   uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
+
+};
+
+class Element : public BaseObj {
+public:
+    bool isComplex();
+    size_t getSubElementCount() {
+        return mVisibleElementMap.size();
+    }
+
+    sp<const Element> getSubElement(uint32_t index);
+    const char * getSubElementName(uint32_t index);
+    size_t getSubElementArraySize(uint32_t index);
+    uint32_t getSubElementOffsetBytes(uint32_t index);
+    RsDataType getDataType() const {
+        return mType;
+    }
+
+    RsDataKind getDataKind() const {
+        return mKind;
+    }
+
+    size_t getSizeBytes() const {
+        return mSizeBytes;
+    }
+
+    static sp<const Element> BOOLEAN(sp<RS> rs);
+    static sp<const Element> U8(sp<RS> rs);
+    static sp<const Element> I8(sp<RS> rs);
+    static sp<const Element> U16(sp<RS> rs);
+    static sp<const Element> I16(sp<RS> rs);
+    static sp<const Element> U32(sp<RS> rs);
+    static sp<const Element> I32(sp<RS> rs);
+    static sp<const Element> U64(sp<RS> rs);
+    static sp<const Element> I64(sp<RS> rs);
+    static sp<const Element> F32(sp<RS> rs);
+    static sp<const Element> F64(sp<RS> rs);
+    static sp<const Element> ELEMENT(sp<RS> rs);
+    static sp<const Element> TYPE(sp<RS> rs);
+    static sp<const Element> ALLOCATION(sp<RS> rs);
+    static sp<const Element> SAMPLER(sp<RS> rs);
+    static sp<const Element> SCRIPT(sp<RS> rs);
+    static sp<const Element> MESH(sp<RS> rs);
+    static sp<const Element> PROGRAM_FRAGMENT(sp<RS> rs);
+    static sp<const Element> PROGRAM_VERTEX(sp<RS> rs);
+    static sp<const Element> PROGRAM_RASTER(sp<RS> rs);
+    static sp<const Element> PROGRAM_STORE(sp<RS> rs);
+
+    static sp<const Element> A_8(sp<RS> rs);
+    static sp<const Element> RGB_565(sp<RS> rs);
+    static sp<const Element> RGB_888(sp<RS> rs);
+    static sp<const Element> RGBA_5551(sp<RS> rs);
+    static sp<const Element> RGBA_4444(sp<RS> rs);
+    static sp<const Element> RGBA_8888(sp<RS> rs);
+
+    static sp<const Element> F32_2(sp<RS> rs);
+    static sp<const Element> F32_3(sp<RS> rs);
+    static sp<const Element> F32_4(sp<RS> rs);
+    static sp<const Element> F64_2(sp<RS> rs);
+    static sp<const Element> F64_3(sp<RS> rs);
+    static sp<const Element> F64_4(sp<RS> rs);
+    static sp<const Element> U8_2(sp<RS> rs);
+    static sp<const Element> U8_3(sp<RS> rs);
+    static sp<const Element> U8_4(sp<RS> rs);
+    static sp<const Element> I8_2(sp<RS> rs);
+    static sp<const Element> I8_3(sp<RS> rs);
+    static sp<const Element> I8_4(sp<RS> rs);
+    static sp<const Element> U16_2(sp<RS> rs);
+    static sp<const Element> U16_3(sp<RS> rs);
+    static sp<const Element> U16_4(sp<RS> rs);
+    static sp<const Element> I16_2(sp<RS> rs);
+    static sp<const Element> I16_3(sp<RS> rs);
+    static sp<const Element> I16_4(sp<RS> rs);
+    static sp<const Element> U32_2(sp<RS> rs);
+    static sp<const Element> U32_3(sp<RS> rs);
+    static sp<const Element> U32_4(sp<RS> rs);
+    static sp<const Element> I32_2(sp<RS> rs);
+    static sp<const Element> I32_3(sp<RS> rs);
+    static sp<const Element> I32_4(sp<RS> rs);
+    static sp<const Element> U64_2(sp<RS> rs);
+    static sp<const Element> U64_3(sp<RS> rs);
+    static sp<const Element> U64_4(sp<RS> rs);
+    static sp<const Element> I64_2(sp<RS> rs);
+    static sp<const Element> I64_3(sp<RS> rs);
+    static sp<const Element> I64_4(sp<RS> rs);
+    static sp<const Element> MATRIX_4X4(sp<RS> rs);
+    static sp<const Element> MATRIX_3X3(sp<RS> rs);
+    static sp<const Element> MATRIX_2X2(sp<RS> rs);
+
+    Element(void *id, sp<RS> rs,
+            android::Vector<sp<Element> > &elements,
+            android::Vector<android::String8> &elementNames,
+            android::Vector<uint32_t> &arraySizes);
+    Element(void *id, sp<RS> rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
+    Element(sp<RS> rs);
+    virtual ~Element();
+
+    void updateFromNative();
+    static sp<const Element> createUser(sp<RS> rs, RsDataType dt);
+    static sp<const Element> createVector(sp<RS> rs, RsDataType dt, uint32_t size);
+    static sp<const Element> createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk);
+    bool isCompatible(sp<const Element>e);
+
+    class Builder {
+    private:
+        sp<RS> mRS;
+        android::Vector<sp<Element> > mElements;
+        android::Vector<android::String8> mElementNames;
+        android::Vector<uint32_t> mArraySizes;
+        bool mSkipPadding;
+
+    public:
+        Builder(sp<RS> rs);
+        ~Builder();
+        void add(sp<Element>, android::String8 &name, uint32_t arraySize = 1);
+        sp<const Element> create();
+    };
+
+private:
+    void updateVisibleSubElements();
+
+    android::Vector<sp</*const*/ Element> > mElements;
+    android::Vector<android::String8> mElementNames;
+    android::Vector<uint32_t> mArraySizes;
+    android::Vector<uint32_t> mVisibleElementMap;
+    android::Vector<uint32_t> mOffsetInBytes;
+
+    RsDataType mType;
+    RsDataKind mKind;
+    bool mNormalized;
+    size_t mSizeBytes;
+    size_t mVectorSize;
+};
+
+class Type : public BaseObj {
+protected:
+    friend class Allocation;
+
+    uint32_t mDimX;
+    uint32_t mDimY;
+    uint32_t mDimZ;
+    bool mDimMipmaps;
+    bool mDimFaces;
+    size_t mElementCount;
+    sp<const Element> mElement;
+
+    void calcElementCount();
+    virtual void updateFromNative();
+
+public:
+
+    sp<const Element> getElement() const {
+        return mElement;
+    }
+
+    uint32_t getX() const {
+        return mDimX;
+    }
+
+    uint32_t getY() const {
+        return mDimY;
+    }
+
+    uint32_t getZ() const {
+        return mDimZ;
+    }
+
+    bool hasMipmaps() const {
+        return mDimMipmaps;
+    }
+
+    bool hasFaces() const {
+        return mDimFaces;
+    }
+
+    size_t getCount() const {
+        return mElementCount;
+    }
+
+    size_t getSizeBytes() const {
+        return mElementCount * mElement->getSizeBytes();
+    }
+
+    Type(void *id, sp<RS> rs);
+
+
+    class Builder {
+    protected:
+        sp<RS> mRS;
+        uint32_t mDimX;
+        uint32_t mDimY;
+        uint32_t mDimZ;
+        bool mDimMipmaps;
+        bool mDimFaces;
+        sp<const Element> mElement;
+
+    public:
+        Builder(sp<RS> rs, sp<const Element> e);
+
+        void setX(uint32_t value);
+        void setY(int value);
+        void setMipmaps(bool value);
+        void setFaces(bool value);
+        sp<const Type> create();
+    };
+
+};
+
+class Script : public BaseObj {
+private:
+
+protected:
+    Script(void *id, sp<RS> rs);
+    void forEach(uint32_t slot, sp<const Allocation> in, sp<const Allocation> out,
+            const void *v, size_t) const;
+    void bindAllocation(sp<Allocation> va, uint32_t slot) const;
+    void setVar(uint32_t index, const void *, size_t len) const;
+    void setVar(uint32_t index, sp<const BaseObj> o) const;
+    void invoke(uint32_t slot, const void *v, size_t len) const;
+
+
+    void invoke(uint32_t slot) const {
+        invoke(slot, NULL, 0);
+    }
+    void setVar(uint32_t index, float v) const {
+        setVar(index, &v, sizeof(v));
+    }
+    void setVar(uint32_t index, double v) const {
+        setVar(index, &v, sizeof(v));
+    }
+    void setVar(uint32_t index, int32_t v) const {
+        setVar(index, &v, sizeof(v));
+    }
+    void setVar(uint32_t index, int64_t v) const {
+        setVar(index, &v, sizeof(v));
+    }
+    void setVar(uint32_t index, bool v) const {
+        setVar(index, &v, sizeof(v));
+    }
+
+public:
+    class FieldBase {
+    protected:
+        sp<const Element> mElement;
+        sp<Allocation> mAllocation;
+
+        void init(sp<RS> rs, uint32_t dimx, uint32_t usages = 0);
+
+    public:
+        sp<const Element> getElement() {
+            return mElement;
+        }
+
+        sp<const Type> getType() {
+            return mAllocation->getType();
+        }
+
+        sp<const Allocation> getAllocation() {
+            return mAllocation;
+        }
+
+        //void updateAllocation();
+    };
+};
+
+class ScriptC : public Script {
+protected:
+    ScriptC(sp<RS> rs,
+            const void *codeTxt, size_t codeLength,
+            const char *cachedName, size_t cachedNameLength,
+            const char *cacheDir, size_t cacheDirLength);
+
+};
+
+
+}
+}
+
+#endif
diff --git a/rsScriptC.cpp b/rsScriptC.cpp
index b291045..18e4af2 100644
--- a/rsScriptC.cpp
+++ b/rsScriptC.cpp
@@ -250,8 +250,14 @@
     bitcodeLen = BT->getTranslatedBitcodeSize();
 #endif
 
+    if (!cacheDir) {
+        // MUST BE FIXED BEFORE ANYTHING USING C++ API IS RELEASED
+        cacheDir = getenv("EXTERNAL_STORAGE");
+        ALOGV("Cache dir changed to %s", cacheDir);
+    }
+
     // ensure that cache dir exists
-    if (!createCacheDir(cacheDir)) {
+    if (cacheDir && !createCacheDir(cacheDir)) {
       return false;
     }