Merge "Begin adding async allocation creation."
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index 6c08ce5..f8ad5cc 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -194,11 +194,15 @@
         mRS.nAllocationRead(mID, d);
     }
 
-    public void resize(int dimX) {
+    public synchronized void resize(int dimX) {
         if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
             throw new IllegalStateException("Resize only support for 1D allocations at this time.");
         }
         mRS.nAllocationResize1D(mID, dimX);
+
+        int typeID = mRS.nAllocationGetType(mID);
+        mType = new Type(typeID, mRS);
+        mType.updateFromNative();
     }
 
     /*
diff --git a/graphics/java/android/renderscript/Script.java b/graphics/java/android/renderscript/Script.java
index 53a33e4..430789a 100644
--- a/graphics/java/android/renderscript/Script.java
+++ b/graphics/java/android/renderscript/Script.java
@@ -112,12 +112,10 @@
 
     public static class FieldBase {
         protected Element mElement;
-        protected Type mType;
         protected Allocation mAllocation;
 
         protected void init(RenderScript rs, int dimx) {
             mAllocation = Allocation.createSized(rs, mElement, dimx);
-            mType = mAllocation.getType();
         }
 
         protected FieldBase() {
@@ -128,7 +126,7 @@
         }
 
         public Type getType() {
-            return mType;
+            return mAllocation.getType();
         }
 
         public Allocation getAllocation() {
@@ -138,19 +136,6 @@
         //@Override
         public void updateAllocation() {
         }
-
-
-        //
-        /*
-        public class ScriptField_UserField
-            extends android.renderscript.Script.FieldBase {
-
-            protected
-
-        }
-
-        */
-
     }
 }
 
diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp
index 16d5bfe..dd108c0 100644
--- a/graphics/jni/android_renderscript_RenderScript.cpp
+++ b/graphics/jni/android_renderscript_RenderScript.cpp
@@ -377,7 +377,7 @@
 nAllocationCreateTyped(JNIEnv *_env, jobject _this, RsContext con, jint e)
 {
     LOG_API("nAllocationCreateTyped, con(%p), e(%p)", con, (RsElement)e);
-    return (jint) rsAllocationCreateTyped(con, (RsElement)e);
+    return (jint) rsaAllocationCreateTyped(con, (RsElement)e);
 }
 
 static void
@@ -428,7 +428,7 @@
         const int w = bitmap.width();
         const int h = bitmap.height();
         const void* ptr = bitmap.getPixels();
-        jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
+        jint id = (jint)rsaAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
         bitmap.unlockPixels();
         return id;
     }
@@ -488,7 +488,7 @@
         const int w = bitmap.width();
         const int h = bitmap.height();
         const void* ptr = bitmap.getPixels();
-        jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
+        jint id = (jint)rsaAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
         bitmap.unlockPixels();
         return id;
     }
diff --git a/libs/rs/RenderScript.h b/libs/rs/RenderScript.h
index d078d46..1d67329 100644
--- a/libs/rs/RenderScript.h
+++ b/libs/rs/RenderScript.h
@@ -290,6 +290,8 @@
 // Async commands for returning new IDS
 RsType rsaTypeCreate(RsContext, RsElement, uint32_t dimCount,
                      const RsDimension *dims, const uint32_t *vals);
+RsAllocation rsaAllocationCreateTyped(RsContext rsc, RsType vtype);
+RsAllocation rsaAllocationCreateFromBitmap(RsContext con, uint32_t w, uint32_t h, RsElement _dst, RsElement _src,  bool genMips, const void *data);
 
 
 #ifndef NO_RS_FUNCS
diff --git a/libs/rs/rs.spec b/libs/rs/rs.spec
index eb2942e..3e131b2 100644
--- a/libs/rs/rs.spec
+++ b/libs/rs/rs.spec
@@ -104,17 +104,6 @@
 	param uint32_t typeDataSize
 	}
 
-AllocationCreateTyped {
-	param RsType type
-	ret RsAllocation
-	}
-
-AllocationCreateSized {
-	param RsElement e
-	param size_t count
-	ret RsAllocation
-	}
-
 AllocationUpdateFromBitmap {
 	param RsAllocation alloc
 	param RsElement srcFmt
@@ -129,17 +118,6 @@
 	ret RsAllocation
 	}
 
-AllocationCreateFromBitmap {
-	param uint32_t width
-	param uint32_t height
-	param RsElement dstFmt
-	param RsElement srcFmt
-	param bool genMips
-	param const void * data
-	ret RsAllocation
-	}
-
-
 AllocationUploadToTexture {
 	param RsAllocation alloc
 	param bool genMipMaps
diff --git a/libs/rs/rsAllocation.cpp b/libs/rs/rsAllocation.cpp
index fc41a72..b4872e3 100644
--- a/libs/rs/rsAllocation.cpp
+++ b/libs/rs/rsAllocation.cpp
@@ -563,24 +563,6 @@
 namespace android {
 namespace renderscript {
 
-RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
-{
-    const Type * type = static_cast<const Type *>(vtype);
-
-    Allocation * alloc = new Allocation(rsc, type);
-    alloc->incUserRef();
-    return alloc;
-}
-
-RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
-{
-    Type * type = new Type(rsc);
-    type->setDimX(count);
-    type->setElement(static_cast<Element *>(e));
-    type->compute();
-    return rsi_AllocationCreateTyped(rsc, type);
-}
-
 void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel)
 {
     Allocation *alloc = static_cast<Allocation *>(va);
@@ -786,42 +768,6 @@
     }
 }
 
-RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src,  bool genMips, const void *data)
-{
-    const Element *src = static_cast<const Element *>(_src);
-    const Element *dst = static_cast<const Element *>(_dst);
-
-    //LOGE("%p rsi_AllocationCreateFromBitmap %i %i %i", rsc, w, h, genMips);
-    RsDimension dims[] = {RS_DIMENSION_X, RS_DIMENSION_Y, RS_DIMENSION_LOD};
-    uint32_t dimValues[] = {w, h, genMips};
-    RsType type = rsaTypeCreate(rsc, _dst, 3, dims, dimValues);
-
-    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
-    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
-    if (texAlloc == NULL) {
-        LOGE("Memory allocation failure");
-        return NULL;
-    }
-
-    ElementConverter_t cvt = pickConverter(dst, src);
-    if (cvt) {
-        cvt(texAlloc->getPtr(), data, w * h);
-        if (genMips) {
-            Adapter2D adapt(rsc, texAlloc);
-            Adapter2D adapt2(rsc, texAlloc);
-            for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
-                adapt.setLOD(lod);
-                adapt2.setLOD(lod + 1);
-                mip(adapt2, adapt);
-            }
-        }
-    } else {
-        rsc->setError(RS_ERROR_BAD_VALUE, "Unsupported bitmap format");
-    }
-
-    return texAlloc;
-}
-
 void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes)
 {
     Allocation *a = static_cast<Allocation *>(va);
@@ -882,3 +828,49 @@
 
 }
 }
+
+RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype)
+{
+    Context *rsc = static_cast<Context *>(con);
+    Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype));
+    alloc->incUserRef();
+    return alloc;
+}
+
+RsAllocation rsaAllocationCreateFromBitmap(RsContext con, uint32_t w, uint32_t h, RsElement _dst, RsElement _src,  bool genMips, const void *data)
+{
+    Context *rsc = static_cast<Context *>(con);
+    const Element *src = static_cast<const Element *>(_src);
+    const Element *dst = static_cast<const Element *>(_dst);
+
+    //LOGE("%p rsi_AllocationCreateFromBitmap %i %i %i", rsc, w, h, genMips);
+    RsDimension dims[] = {RS_DIMENSION_X, RS_DIMENSION_Y, RS_DIMENSION_LOD};
+    uint32_t dimValues[] = {w, h, genMips};
+    RsType type = rsaTypeCreate(rsc, _dst, 3, dims, dimValues);
+
+    RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, type);
+    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
+    if (texAlloc == NULL) {
+        LOGE("Memory allocation failure");
+        return NULL;
+    }
+
+    ElementConverter_t cvt = pickConverter(dst, src);
+    if (cvt) {
+        cvt(texAlloc->getPtr(), data, w * h);
+        if (genMips) {
+            Adapter2D adapt(rsc, texAlloc);
+            Adapter2D adapt2(rsc, texAlloc);
+            for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
+                adapt.setLOD(lod);
+                adapt2.setLOD(lod + 1);
+                mip(adapt2, adapt);
+            }
+        }
+    } else {
+        rsc->setError(RS_ERROR_BAD_VALUE, "Unsupported bitmap format");
+    }
+
+    return texAlloc;
+}
+
diff --git a/libs/rs/rsElement.cpp b/libs/rs/rsElement.cpp
index dc021fc..096115c 100644
--- a/libs/rs/rsElement.cpp
+++ b/libs/rs/rsElement.cpp
@@ -399,6 +399,7 @@
     rsAssert(e->getFieldCount() == dataSize);
 
     for(uint32_t i = 0; i < dataSize; i ++) {
+        e->getField(i)->incUserRef();
         ids[i] = (uint32_t)e->getField(i);
         names[i] = e->getFieldName(i);
     }
diff --git a/libs/rs/rsFont.cpp b/libs/rs/rsFont.cpp
index d171a48..9e76215 100644
--- a/libs/rs/rsFont.cpp
+++ b/libs/rs/rsFont.cpp
@@ -511,10 +511,7 @@
     mRSC->mStateElement.elementBuilderAdd(gammaElem, "Gamma", 1);
     const Element *constInput = mRSC->mStateElement.elementBuilderCreate(mRSC);
 
-    Type *inputType = new Type(mRSC);
-    inputType->setElement(constInput);
-    inputType->setDimX(1);
-    inputType->compute();
+    Type *inputType = Type::getType(mRSC, constInput, 1, 0, 0, false, false);
 
     uint32_t tmp[4];
     tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
@@ -546,11 +543,7 @@
     const Element *alphaElem = Element::create(mRSC, RS_TYPE_UNSIGNED_8, RS_KIND_PIXEL_A, true, 1);
 
     // We will allocate a texture to initially hold 32 character bitmaps
-    Type *texType = new Type(mRSC);
-    texType->setElement(alphaElem);
-    texType->setDimX(1024);
-    texType->setDimY(256);
-    texType->compute();
+    Type *texType = Type::getType(mRSC, alphaElem, 1024, 256, 0, false, false);
 
     Allocation *cacheAlloc = new Allocation(mRSC, texType);
     mTextTexture.set(cacheAlloc);
@@ -578,11 +571,8 @@
 {
     // Now lets write index data
     const Element *indexElem = Element::create(mRSC, RS_TYPE_UNSIGNED_16, RS_KIND_USER, false, 1);
-    Type *indexType = new Type(mRSC);
     uint32_t numIndicies = mMaxNumberOfQuads * 6;
-    indexType->setDimX(numIndicies);
-    indexType->setElement(indexElem);
-    indexType->compute();
+    Type *indexType = Type::getType(mRSC, indexElem, numIndicies, 0, 0, false, false);
 
     Allocation *indexAlloc = new Allocation(mRSC, indexType);
     uint16_t *indexPtr = (uint16_t*)indexAlloc->getPtr();
@@ -612,10 +602,9 @@
     mRSC->mStateElement.elementBuilderAdd(texElem, "texture0", 1);
     const Element *vertexDataElem = mRSC->mStateElement.elementBuilderCreate(mRSC);
 
-    Type *vertexDataType = new Type(mRSC);
-    vertexDataType->setDimX(mMaxNumberOfQuads * 4);
-    vertexDataType->setElement(vertexDataElem);
-    vertexDataType->compute();
+    Type *vertexDataType = Type::getType(mRSC, vertexDataElem,
+                                         mMaxNumberOfQuads * 4,
+                                         0, 0, false, false);
 
     Allocation *vertexAlloc = new Allocation(mRSC, vertexDataType);
     mTextMeshPtr = (float*)vertexAlloc->getPtr();
diff --git a/libs/rs/rsObjectBase.cpp b/libs/rs/rsObjectBase.cpp
index 724172e..44dc042 100644
--- a/libs/rs/rsObjectBase.cpp
+++ b/libs/rs/rsObjectBase.cpp
@@ -120,8 +120,15 @@
 
 bool ObjectBase::decUserRef() const
 {
-    //LOGV("ObjectBase %p decU ref %i, %i", this, mUserRefCount, mSysRefCount);
     rsAssert(mUserRefCount > 0);
+#if RS_OBJECT_DEBUG
+    LOGV("ObjectBase %p decU ref %i, %i", this, mUserRefCount, mSysRefCount);
+    if (mUserRefCount <= 0) {
+        mStack.dump();
+    }
+#endif
+
+
     if ((android_atomic_dec(&mUserRefCount) <= 1) &&
         (android_atomic_acquire_load(&mSysRefCount) <= 0)) {
         return checkDelete(this);
diff --git a/libs/rs/rsObjectBase.h b/libs/rs/rsObjectBase.h
index 5f03db5..5cc275a 100644
--- a/libs/rs/rsObjectBase.h
+++ b/libs/rs/rsObjectBase.h
@@ -21,9 +21,7 @@
 
 #define RS_OBJECT_DEBUG 0
 
-#if RS_OBJECT_DEBUG
-    #include <utils/CallStack.h>
-#endif
+#include <utils/CallStack.h>
 
 namespace android {
 namespace renderscript {
diff --git a/libs/rs/rsProgramFragment.cpp b/libs/rs/rsProgramFragment.cpp
index 33399d5..800854b 100644
--- a/libs/rs/rsProgramFragment.cpp
+++ b/libs/rs/rsProgramFragment.cpp
@@ -198,10 +198,7 @@
     rsc->mStateElement.elementBuilderAdd(colorElem, "Color", 1);
     const Element *constInput = rsc->mStateElement.elementBuilderCreate(rsc);
 
-    Type *inputType = new Type(rsc);
-    inputType->setElement(constInput);
-    inputType->setDimX(1);
-    inputType->compute();
+    Type *inputType = Type::getType(rsc, constInput, 1, 0, 0, false, false);
 
     uint32_t tmp[4];
     tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
diff --git a/libs/rs/rsProgramVertex.cpp b/libs/rs/rsProgramVertex.cpp
index d12439f..4e64008 100644
--- a/libs/rs/rsProgramVertex.cpp
+++ b/libs/rs/rsProgramVertex.cpp
@@ -257,10 +257,7 @@
     rsc->mStateElement.elementBuilderAdd(f2Elem, "texture0", 1);
     const Element *attrElem = rsc->mStateElement.elementBuilderCreate(rsc);
 
-    Type *inputType = new Type(rsc);
-    inputType->setElement(constInput);
-    inputType->setDimX(1);
-    inputType->compute();
+    Type *inputType = Type::getType(rsc, constInput, 1, 0, 0, false, false);
 
     String8 shaderString(RS_SHADER_INTERNAL);
     shaderString.append("varying vec4 varColor;\n");
diff --git a/libs/rs/rsType.cpp b/libs/rs/rsType.cpp
index 82ad33e..7ef2464 100644
--- a/libs/rs/rsType.cpp
+++ b/libs/rs/rsType.cpp
@@ -276,20 +276,12 @@
         return NULL;
     }
 
-    Type *type = new Type(rsc);
-    type->mDimX = stream->loadU32();
-    type->mDimY = stream->loadU32();
-    type->mDimZ = stream->loadU32();
-
-    uint8_t temp = stream->loadU8();
-    type->mDimLOD = temp != 0;
-
-    temp = stream->loadU8();
-    type->mFaces = temp != 0;
-
-    type->setElement(elem);
-
-    return type;
+    uint32_t x = stream->loadU32();
+    uint32_t y = stream->loadU32();
+    uint32_t z = stream->loadU32();
+    uint8_t lod = stream->loadU8();
+    uint8_t faces = stream->loadU8();
+    return Type::getType(rsc, elem, x, y, z, lod != 0, faces !=0 );
 }
 
 bool Type::getIsNp2() const
@@ -325,56 +317,55 @@
     return false;
 }
 
-Type * Type::cloneAndResize1D(Context *rsc, uint32_t dimX) const
+Type * Type::getType(Context *rsc, const Element *e,
+                     uint32_t dimX, uint32_t dimY, uint32_t dimZ,
+                     bool dimLOD, bool dimFaces)
 {
     TypeState * stc = &rsc->mStateType;
+
+    ObjectBase::asyncLock();
     for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) {
         Type *t = stc->mTypes[ct];
-        if (t->getElement() != mElement.get()) continue;
+        if (t->getElement() != e) continue;
         if (t->getDimX() != dimX) continue;
-        if (t->getDimY() != mDimY) continue;
-        if (t->getDimZ() != mDimZ) continue;
-        if (t->getDimLOD() != mDimLOD) continue;
-        if (t->getDimFaces() != mFaces) continue;
+        if (t->getDimY() != dimY) continue;
+        if (t->getDimZ() != dimZ) continue;
+        if (t->getDimLOD() != dimLOD) continue;
+        if (t->getDimFaces() != dimFaces) continue;
         t->incUserRef();
+        ObjectBase::asyncUnlock();
         return t;
     }
+    ObjectBase::asyncUnlock();
+
 
     Type *nt = new Type(rsc);
-    nt->mElement.set(mElement);
+    nt->mElement.set(e);
     nt->mDimX = dimX;
-    nt->mDimY = mDimY;
-    nt->mDimZ = mDimZ;
-    nt->mDimLOD = mDimLOD;
-    nt->mFaces = mFaces;
+    nt->mDimY = dimY;
+    nt->mDimZ = dimZ;
+    nt->mDimLOD = dimLOD;
+    nt->mFaces = dimFaces;
     nt->compute();
+    nt->incUserRef();
+
+    ObjectBase::asyncLock();
+    stc->mTypes.push(nt);
+    ObjectBase::asyncUnlock();
+
     return nt;
 }
 
+Type * Type::cloneAndResize1D(Context *rsc, uint32_t dimX) const
+{
+    return getType(rsc, mElement.get(), dimX,
+                   mDimY, mDimZ, mDimLOD, mFaces);
+}
+
 Type * Type::cloneAndResize2D(Context *rsc, uint32_t dimX, uint32_t dimY) const
 {
-    TypeState * stc = &rsc->mStateType;
-    for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) {
-        Type *t = stc->mTypes[ct];
-        if (t->getElement() != mElement.get()) continue;
-        if (t->getDimX() != dimX) continue;
-        if (t->getDimY() != dimY) continue;
-        if (t->getDimZ() != mDimZ) continue;
-        if (t->getDimLOD() != mDimLOD) continue;
-        if (t->getDimFaces() != mFaces) continue;
-        t->incUserRef();
-        return t;
-    }
-
-    Type *nt = new Type(rsc);
-    nt->mElement.set(mElement);
-    nt->mDimX = dimX;
-    nt->mDimY = dimY;
-    nt->mDimZ = mDimZ;
-    nt->mDimLOD = mDimLOD;
-    nt->mFaces = mFaces;
-    nt->compute();
-    return nt;
+    return getType(rsc, mElement.get(), dimX, dimY,
+                   mDimZ, mDimLOD, mFaces);
 }
 
 
@@ -396,7 +387,7 @@
     (*typeData++) = t->getDimLOD();
     (*typeData++) = t->getDimFaces() ? 1 : 0;
     (*typeData++) = (uint32_t)t->getElement();
-
+    t->getElement()->incUserRef();
 }
 
 
@@ -430,34 +421,6 @@
         }
     }
 
-    ObjectBase::asyncLock();
-    for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) {
-        Type *t = stc->mTypes[ct];
-        if (t->getElement() != e) continue;
-        if (t->getDimX() != dimX) continue;
-        if (t->getDimY() != dimY) continue;
-        if (t->getDimZ() != dimZ) continue;
-        if (t->getDimLOD() != dimLOD) continue;
-        if (t->getDimFaces() != dimFaces) continue;
-        t->incUserRef();
-        ObjectBase::asyncUnlock();
-        return t;
-    }
-    ObjectBase::asyncUnlock();
-
-    Type * st = new Type(rsc);
-    st->incUserRef();
-    st->setDimX(dimX);
-    st->setDimY(dimY);
-    st->setDimZ(dimZ);
-    st->setElement(e);
-    st->setDimLOD(dimLOD);
-    st->setDimFaces(dimFaces);
-    st->compute();
-
-    ObjectBase::asyncLock();
-    stc->mTypes.push(st);
-    ObjectBase::asyncUnlock();
-    return st;
+    return Type::getType(rsc, e, dimX, dimY, dimZ, dimLOD, dimFaces);
 }
 
diff --git a/libs/rs/rsType.h b/libs/rs/rsType.h
index 6b89413..0ca5bb6 100644
--- a/libs/rs/rsType.h
+++ b/libs/rs/rsType.h
@@ -28,9 +28,6 @@
 class Type : public ObjectBase
 {
 public:
-    Type(Context *);
-    virtual ~Type();
-
     Type * createTex2D(const Element *, size_t w, size_t h, bool mip);
 
 
@@ -58,15 +55,6 @@
     uint32_t getLODCount() const {return mLODCount;}
     bool getIsNp2() const;
 
-
-    void setElement(const Element *e) {mElement.set(e);}
-    void setDimX(uint32_t v) {mDimX = v;}
-    void setDimY(uint32_t v) {mDimY = v;}
-    void setDimZ(uint32_t v) {mDimZ = v;}
-    void setDimFaces(bool v) {mFaces = v;}
-    void setDimLOD(bool v) {mDimLOD = v;}
-
-
     void clear();
     void compute();
 
@@ -82,6 +70,10 @@
     Type * cloneAndResize1D(Context *rsc, uint32_t dimX) const;
     Type * cloneAndResize2D(Context *rsc, uint32_t dimX, uint32_t dimY) const;
 
+    static Type * getType(Context *rsc, const Element *e,
+                      uint32_t dimX, uint32_t dimY, uint32_t dimZ,
+                      bool dimLOD, bool dimFaces);
+
 protected:
     struct LOD {
         size_t mX;
@@ -124,10 +116,13 @@
     bool isValidGLComponent(uint32_t fieldIdx);
     void makeGLComponents();
 
+
 protected:
     virtual void preDestroy();
+    virtual ~Type();
 
 private:
+    Type(Context *);
     Type(const Type &);
 };