Populate java objects with native data from a3d file.
Remove legacy constructor from programraster
Make a3d object creation synchronous

Change-Id: Ic7d7547cf6eee6f9a7c6e3ee12cd104e80056a7b
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index bfa61f3..87735b5 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -45,6 +45,16 @@
         mID = id;
     }
 
+    @Override
+    void updateFromNative() {
+        mRS.validate();
+        int typeID = mRS.nAllocationGetType(mID);
+        if(typeID != 0) {
+            mType = new Type(typeID, mRS);
+            mType.updateFromNative();
+        }
+    }
+
     public Type getType() {
         return mType;
     }
diff --git a/graphics/java/android/renderscript/Element.java b/graphics/java/android/renderscript/Element.java
index 308d663..5d2a059 100644
--- a/graphics/java/android/renderscript/Element.java
+++ b/graphics/java/android/renderscript/Element.java
@@ -17,6 +17,7 @@
 package android.renderscript;
 
 import java.lang.reflect.Field;
+import android.util.Log;
 
 /**
  * @hide
@@ -308,6 +309,45 @@
         mID = rs.nElementCreate(dt.mID, dk.mID, norm, size);
     }
 
+    Element(RenderScript rs, int id) {
+        super(rs);
+        mID = id;
+    }
+
+    @Override
+    void updateFromNative() {
+
+        // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
+        int[] dataBuffer = new int[5];
+        mRS.nElementGetNativeData(mID, dataBuffer);
+        for (DataType dt: DataType.values()) {
+            if(dt.mID == dataBuffer[0]){
+                mType = dt;
+            }
+        }
+        for (DataKind dk: DataKind.values()) {
+            if(dk.mID == dataBuffer[1]){
+                mKind = dk;
+            }
+        }
+
+        mNormalized = dataBuffer[2] == 1 ? true : false;
+        mVectorSize = dataBuffer[3];
+        int numSubElements = dataBuffer[4];
+        if(numSubElements > 0) {
+            mElements = new Element[numSubElements];
+            mElementNames = new String[numSubElements];
+
+            int[] subElementIds = new int[numSubElements];
+            mRS.nElementGetSubElements(mID, subElementIds, mElementNames);
+            for(int i = 0; i < numSubElements; i ++) {
+                mElements[i] = new Element(mRS, subElementIds[i]);
+                mElements[i].updateFromNative();
+            }
+        }
+
+    }
+
     public void destroy() throws IllegalStateException {
         super.destroy();
     }
diff --git a/graphics/java/android/renderscript/FileA3D.java b/graphics/java/android/renderscript/FileA3D.java
index 3b3711b..302a5f4 100644
--- a/graphics/java/android/renderscript/FileA3D.java
+++ b/graphics/java/android/renderscript/FileA3D.java
@@ -56,7 +56,7 @@
     }
 
     // Read only class with index entries
-    public class IndexEntry {
+    public static class IndexEntry {
         RenderScript mRS;
         int mIndex;
         int mID;
@@ -73,34 +73,40 @@
         }
 
         public BaseObj getObject() {
-            if(mLoadedObj != null) {
-                return mLoadedObj;
+            mRS.validate();
+            BaseObj obj = internalCreate(mRS, this);
+            return obj;
+        }
+
+        static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
+            if(entry.mLoadedObj != null) {
+                return entry.mLoadedObj;
             }
 
-            if(mClassID == ClassID.UNKNOWN) {
+            if(entry.mClassID == ClassID.UNKNOWN) {
                 return null;
             }
 
-            int objectID = mRS.nFileA3DGetEntryByIndex(mID, mIndex);
+            int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex);
             if(objectID == 0) {
                 return null;
             }
 
-            switch (mClassID) {
+            switch (entry.mClassID) {
             case MESH:
-                mLoadedObj = new Mesh(objectID, mRS);
+                entry.mLoadedObj = new Mesh(objectID, rs);
                 break;
             case TYPE:
-                mLoadedObj = new Type(objectID, mRS);
+                entry.mLoadedObj = new Type(objectID, rs);
                 break;
             case ELEMENT:
-                mLoadedObj = null;
+                entry.mLoadedObj = null;
                 break;
             case ALLOCATION:
-                mLoadedObj = null;
+                entry.mLoadedObj = null;
                 break;
             case PROGRAM_VERTEX:
-                mLoadedObj = new ProgramVertex(objectID, mRS);
+                entry.mLoadedObj = new ProgramVertex(objectID, rs);
                 break;
             case PROGRAM_RASTER:
                 break;
@@ -122,7 +128,9 @@
                 break;
             }
 
-            return mLoadedObj;
+            entry.mLoadedObj.updateFromNative();
+
+            return entry.mLoadedObj;
         }
 
         IndexEntry(RenderScript rs, int index, int id, String name, ClassID classID) {
diff --git a/graphics/java/android/renderscript/ProgramRaster.java b/graphics/java/android/renderscript/ProgramRaster.java
index 55e6586..6fc9fff 100644
--- a/graphics/java/android/renderscript/ProgramRaster.java
+++ b/graphics/java/android/renderscript/ProgramRaster.java
@@ -74,14 +74,6 @@
         boolean mPointSmooth;
         boolean mLineSmooth;
 
-        // Legacy to not break app in other projects, will be removed in cleanup pass
-        public Builder(RenderScript rs, Element in, Element out) {
-            mRS = rs;
-            mPointSmooth = false;
-            mLineSmooth = false;
-            mPointSprite = false;
-        }
-
         public Builder(RenderScript rs) {
             mRS = rs;
             mPointSmooth = false;
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index 240d544..1135a75 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -90,12 +90,15 @@
 
     native int  nElementCreate(int type, int kind, boolean norm, int vecSize);
     native int  nElementCreate2(int[] elements, String[] names);
+    native void nElementGetNativeData(int id, int[] elementData);
+    native void nElementGetSubElements(int id, int[] IDs, String[] names);
 
     native void nTypeBegin(int elementID);
     native void nTypeAdd(int dim, int val);
     native int  nTypeCreate();
     native void nTypeFinalDestroy(Type t);
     native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs);
+    native void nTypeGetNativeData(int id, int[] typeData);
 
     native int  nAllocationCreateTyped(int type);
     native int  nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp);
@@ -117,6 +120,7 @@
     native void nAllocationRead(int id, float[] d);
     native void nAllocationSubDataFromObject(int id, Type t, int offset, Object o);
     native void nAllocationSubReadFromObject(int id, Type t, int offset, Object o);
+    native int  nAllocationGetType(int id);
 
     native int  nFileA3DCreateFromAssetStream(int assetStream);
     native int  nFileA3DGetNumIndexEntries(int fileA3D);
diff --git a/graphics/java/android/renderscript/Type.java b/graphics/java/android/renderscript/Type.java
index 14422b2..8e45f2b 100644
--- a/graphics/java/android/renderscript/Type.java
+++ b/graphics/java/android/renderscript/Type.java
@@ -16,7 +16,9 @@
 
 package android.renderscript;
 
+
 import java.lang.reflect.Field;
+import android.util.Log;
 
 /**
  * @hide
@@ -108,6 +110,27 @@
         super.finalize();
     }
 
+    @Override
+    void updateFromNative() {
+        // We have 6 integer to obtain mDimX; mDimY; mDimZ;
+        // mDimLOD; mDimFaces; mElement;
+        int[] dataBuffer = new int[6];
+        mRS.nTypeGetNativeData(mID, dataBuffer);
+
+        mDimX = dataBuffer[0];
+        mDimY = dataBuffer[1];
+        mDimZ = dataBuffer[2];
+        mDimLOD = dataBuffer[3] == 1 ? true : false;
+        mDimFaces = dataBuffer[4] == 1 ? true : false;
+
+        int elementID = dataBuffer[5];
+        if(elementID != 0) {
+            mElement = new Element(mRS, elementID);
+            mElement.updateFromNative();
+        }
+        calcElementCount();
+    }
+
     public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) {
         android.util.Log.e("RenderScript", "Calling depricated createFromClass");
         return null;
diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp
index 13360c3..888c76a 100644
--- a/graphics/jni/android_renderscript_RenderScript.cpp
+++ b/graphics/jni/android_renderscript_RenderScript.cpp
@@ -297,6 +297,46 @@
     return (jint)id;
 }
 
+static void
+nElementGetNativeData(JNIEnv *_env, jobject _this, jint id, jintArray _elementData)
+{
+    int dataSize = _env->GetArrayLength(_elementData);
+    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    LOG_API("nElementGetNativeData, con(%p)", con);
+
+    // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
+    assert(dataSize == 5);
+
+    uint32_t elementData[5];
+    rsElementGetNativeData(con, (RsElement)id, elementData, dataSize);
+
+    for(jint i = 0; i < dataSize; i ++) {
+        _env->SetIntArrayRegion(_elementData, i, 1, (const jint*)&elementData[i]);
+    }
+}
+
+
+static void
+nElementGetSubElements(JNIEnv *_env, jobject _this, jint id, jintArray _IDs, jobjectArray _names)
+{
+    int dataSize = _env->GetArrayLength(_IDs);
+    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    LOG_API("nElementGetSubElements, con(%p)", con);
+
+    uint32_t *ids = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t));
+    const char **names = (const char **)malloc((uint32_t)dataSize * sizeof(const char *));
+
+    rsElementGetSubElements(con, (RsElement)id, ids, names, (uint32_t)dataSize);
+
+    for(jint i = 0; i < dataSize; i ++) {
+        _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
+        _env->SetIntArrayRegion(_IDs, i, 1, (const jint*)&ids[i]);
+    }
+
+    free(ids);
+    free(names);
+}
+
 // -----------------------------------
 
 static void
@@ -323,6 +363,26 @@
     return (jint)rsTypeCreate(con);
 }
 
+static void
+nTypeGetNativeData(JNIEnv *_env, jobject _this, jint id, jintArray _typeData)
+{
+    // We are packing 6 items: mDimX; mDimY; mDimZ;
+    // mDimLOD; mDimFaces; mElement; into typeData
+    int elementCount = _env->GetArrayLength(_typeData);
+
+    assert(elementCount == 6);
+
+    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    LOG_API("nTypeCreate, con(%p)", con);
+
+    uint32_t typeData[6];
+    rsTypeGetNativeData(con, (RsType)id, typeData, 6);
+
+    for(jint i = 0; i < elementCount; i ++) {
+        _env->SetIntArrayRegion(_typeData, i, 1, (const jint*)&typeData[i]);
+    }
+}
+
 static void * SF_LoadInt(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
 {
     ((int32_t *)buffer)[0] = _env->GetIntField(_obj, _field);
@@ -708,6 +768,14 @@
     free(bufAlloc);
 }
 
+static jint
+nAllocationGetType(JNIEnv *_env, jobject _this, jint a)
+{
+    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    LOG_API("nAllocationGetType, con(%p), a(%p)", con, (RsAllocation)a);
+    return (jint) rsAllocationGetType(con, (RsAllocation)a);
+}
+
 // -----------------------------------
 
 static int
@@ -1466,12 +1534,15 @@
 
 {"nElementCreate",                 "(IIZI)I",                              (void*)nElementCreate },
 {"nElementCreate2",                "([I[Ljava/lang/String;)I",             (void*)nElementCreate2 },
+{"nElementGetNativeData",         "(I[I)V",                               (void*)nElementGetNativeData },
+{"nElementGetSubElements",        "(I[I[Ljava/lang/String;)V",           (void*)nElementGetSubElements },
 
 {"nTypeBegin",                     "(I)V",                                 (void*)nTypeBegin },
 {"nTypeAdd",                       "(II)V",                                (void*)nTypeAdd },
 {"nTypeCreate",                    "()I",                                  (void*)nTypeCreate },
 {"nTypeFinalDestroy",              "(Landroid/renderscript/Type;)V",       (void*)nTypeFinalDestroy },
 {"nTypeSetupFields",               "(Landroid/renderscript/Type;[I[I[Ljava/lang/reflect/Field;)V", (void*)nTypeSetupFields },
+{"nTypeGetNativeData",             "(I[I)V",                                (void*)nTypeGetNativeData },
 
 {"nAllocationCreateTyped",         "(I)I",                                 (void*)nAllocationCreateTyped },
 {"nAllocationCreateFromBitmap",    "(IZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
@@ -1490,6 +1561,7 @@
 {"nAllocationRead",                "(I[F)V",                               (void*)nAllocationRead_f },
 {"nAllocationSubDataFromObject",   "(ILandroid/renderscript/Type;ILjava/lang/Object;)V",   (void*)nAllocationSubDataFromObject },
 {"nAllocationSubReadFromObject",   "(ILandroid/renderscript/Type;ILjava/lang/Object;)V",   (void*)nAllocationSubReadFromObject },
+{"nAllocationGetType",              "(I)I",                                 (void*)nAllocationGetType},
 
 {"nAdapter1DBindAllocation",       "(II)V",                                (void*)nAdapter1DBindAllocation },
 {"nAdapter1DSetConstraint",        "(III)V",                               (void*)nAdapter1DSetConstraint },
diff --git a/libs/rs/rs.spec b/libs/rs/rs.spec
index 1719029..1b81591 100644
--- a/libs/rs/rs.spec
+++ b/libs/rs/rs.spec
@@ -78,6 +78,19 @@
 	ret RsElement
 	}
 
+ElementGetNativeData {
+	param RsElement elem
+	param uint32_t *elemData
+	param uint32_t elemDataSize
+	}
+
+ElementGetSubElements {
+	param RsElement elem
+	param uint32_t *ids
+	param const char **names
+	param uint32_t dataSize
+	}
+
 TypeBegin {
 	param RsElement type
 	}
@@ -91,6 +104,12 @@
 	ret RsType
 	}
 
+TypeGetNativeData {
+	param RsType type
+	param uint32_t * typeData
+	param uint32_t typeDataSize
+	}
+
 AllocationCreateTyped {
 	param RsType type
 	ret RsAllocation
@@ -231,6 +250,11 @@
 	param const void *data
 	}
 
+AllocationGetType {
+	param RsAllocation va
+	ret const void*
+	}
+
 SamplerBegin {
 	}
 
diff --git a/libs/rs/rsElement.cpp b/libs/rs/rsElement.cpp
index aa20275..37b8bd6 100644
--- a/libs/rs/rsElement.cpp
+++ b/libs/rs/rsElement.cpp
@@ -293,6 +293,32 @@
     return (RsElement)e;
 }
 
+void rsi_ElementGetNativeData(Context *rsc, RsElement elem, uint32_t *elemData, uint32_t elemDataSize)
+{
+    rsAssert(elemDataSize == 5);
+    // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
+    Element *e = static_cast<Element *>(elem);
+
+    (*elemData++) = (uint32_t)e->getType();
+    (*elemData++) = (uint32_t)e->getKind();
+    (*elemData++) = e->getComponent().getIsNormalized() ? 1 : 0;
+    (*elemData++) = e->getComponent().getVectorSize();
+    (*elemData++) = e->getFieldCount();
+
+}
+
+void rsi_ElementGetSubElements(Context *rsc, RsElement elem, uint32_t *ids, const char **names, uint32_t dataSize)
+{
+    Element *e = static_cast<Element *>(elem);
+    rsAssert(e->getFieldCount() == dataSize);
+
+    for(uint32_t i = 0; i < dataSize; i ++) {
+        ids[i] = (uint32_t)e->getField(i);
+        names[i] = e->getFieldName(i);
+    }
+
+}
+
 
 }
 }
diff --git a/libs/rs/rsType.cpp b/libs/rs/rsType.cpp
index 8dca0fe..52e0d52 100644
--- a/libs/rs/rsType.cpp
+++ b/libs/rs/rsType.cpp
@@ -91,7 +91,7 @@
     if (mLODCount != oldLODCount) {
         if(mLODs){
             delete [] mLODs;
-        }        
+        }
         mLODs = new LOD[mLODCount];
     }
 
@@ -340,6 +340,22 @@
     return st;
 }
 
+void rsi_TypeGetNativeData(Context *rsc, RsType type, uint32_t *typeData, uint32_t typeDataSize)
+{
+    rsAssert(typeDataSize == 6);
+    // Pack the data in the follofing way mDimX; mDimY; mDimZ;
+    // mDimLOD; mDimFaces; mElement; into typeData
+    Type *t = static_cast<Type *>(type);
+
+    (*typeData++) = t->getDimX();
+    (*typeData++) = t->getDimY();
+    (*typeData++) = t->getDimZ();
+    (*typeData++) = t->getDimLOD();
+    (*typeData++) = t->getDimFaces() ? 1 : 0;
+    (*typeData++) = (uint32_t)t->getElement();
+
+}
+
 
 }
 }