Implement reflecting Java objects into the ACC enviroment.
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index ca35a40..50d39b7 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -16,6 +16,8 @@
 
 package android.renderscript;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.Array;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -33,9 +35,12 @@
  *
  **/
 public class Allocation extends BaseObj {
-    Allocation(int id, RenderScript rs) {
+    Type mType;
+
+    Allocation(int id, RenderScript rs, Type t) {
         super(rs);
         mID = id;
+        mType = t;
     }
 
     public void uploadToTexture(int baseMipLevel) {
@@ -82,6 +87,10 @@
         mRS.nAllocationRead(mID, d);
     }
 
+    public void data(Object o) {
+        mRS.nAllocationDataFromObject(mID, mType, o);
+    }
+
 
     public class Adapter1D extends BaseObj {
         Adapter1D(int id, RenderScript rs) {
@@ -179,7 +188,7 @@
             throw new IllegalStateException("Bad Type");
         }
         int id = rs.nAllocationCreateTyped(type.mID);
-        return new Allocation(id, rs);
+        return new Allocation(id, rs, type);
     }
 
     static public Allocation createSized(RenderScript rs, Element e, int count)
@@ -194,7 +203,7 @@
                 throw new IllegalStateException("Bad element.");
             }
         }
-        return new Allocation(id, rs);
+        return new Allocation(id, rs, null);
     }
 
     static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
@@ -204,7 +213,7 @@
         }
 
         int id = rs.nAllocationCreateFromBitmap(dstFmt.mPredefinedID, genMips, b);
-        return new Allocation(id, rs);
+        return new Allocation(id, rs, null);
     }
 
     static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
@@ -214,7 +223,7 @@
         }
 
         int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mPredefinedID, genMips, b);
-        return new Allocation(id, rs);
+        return new Allocation(id, rs, null);
     }
 
     static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
@@ -230,8 +239,20 @@
         Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
         return createFromBitmapBoxed(rs, b, dstFmt, genMips);
     }
-
-
+/*
+    public static Allocation createFromObject(RenderScript rs, Object o) {
+        Class c = o.getClass();
+        Type t;
+        if(c.isArray()) {
+            t = Type.createFromClass(rs, c, Array.getLength(o));
+        } else {
+            t = Type.createFromClass(rs, c, 1);
+        }
+        Allocation alloc = createTyped(rs, t);
+        t.destroy();
+        return alloc;
+    } 
+*/
 }
 
 
diff --git a/graphics/java/android/renderscript/Element.java b/graphics/java/android/renderscript/Element.java
index 14d9115..9155da8 100644
--- a/graphics/java/android/renderscript/Element.java
+++ b/graphics/java/android/renderscript/Element.java
@@ -16,6 +16,10 @@
 
 package android.renderscript;
 
+import android.util.Config;
+import android.util.Log;
+
+import java.lang.reflect.Field;
 
 /**
  * @hide
@@ -144,7 +148,26 @@
         mRS.nElementDestroy(mID);
     }
 
+    public static Element createFromClass(RenderScript rs, Class c) {
+        Field[] fields = c.getFields();
+        Builder b = new Builder(rs);
 
+        for(Field f: fields) {
+            Class fc = f.getType();
+            if(fc == int.class) {
+                b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 32, f.getName());
+            } else if(fc == short.class) {
+                b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 16, f.getName());
+            } else if(fc == byte.class) {
+                b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 8, f.getName());
+            } else if(fc == float.class) {
+                b.add(Element.DataType.FLOAT, Element.DataKind.USER, false, 32, f.getName());
+            } else {
+                throw new IllegalArgumentException("Unkown field type");
+            }
+        }
+        return b.create();
+    }
 
 
     public static class Builder {
@@ -158,6 +181,7 @@
             Element.DataKind mKind;
             boolean mIsNormalized;
             int mBits;
+            String mName;
         }
 
         public Builder(RenderScript rs) {
@@ -188,16 +212,22 @@
             return this;
         }
 
-        public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits) {
+        public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits, String name) {
             Entry en = new Entry();
             en.mType = dt;
             en.mKind = dk;
             en.mIsNormalized = isNormalized;
             en.mBits = bits;
+            en.mName = name;
             addEntry(en);
             return this;
         }
 
+        public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits) {
+            add(dt, dk, isNormalized, bits, null);
+            return this;
+        }
+
         static synchronized Element internalCreate(RenderScript rs, Builder b) {
             rs.nElementBegin();
             for (int ct=0; ct < b.mEntryCount; ct++) {
@@ -209,7 +239,7 @@
                     if (en.mIsNormalized) {
                         norm = 1;
                     }
-                    rs.nElementAdd(en.mKind.mID, en.mType.mID, norm, en.mBits);
+                    rs.nElementAdd(en.mKind.mID, en.mType.mID, norm, en.mBits, en.mName);
                 }
             }
             int id = rs.nElementCreate();
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index 77486b1..31bfc57 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -18,9 +18,11 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.Field;
 
 import android.content.res.Resources;
 import android.graphics.Bitmap;
+import android.renderscript.Type;
 import android.util.Config;
 import android.util.Log;
 import android.view.Surface;
@@ -76,7 +78,7 @@
 
     native void nElementBegin();
     native void nElementAddPredefined(int predef);
-    native void nElementAdd(int kind, int type, int norm, int bits);
+    native void nElementAdd(int kind, int type, int norm, int bits, String s);
     native int  nElementCreate();
     native int  nElementGetPredefined(int predef);
     native void nElementDestroy(int obj);
@@ -85,6 +87,8 @@
     native void nTypeAdd(int dim, int val);
     native int  nTypeCreate();
     native void nTypeDestroy(int id);
+    native void nTypeFinalDestroy(Type t);
+    native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs);
 
     native int  nAllocationCreateTyped(int type);
     native int  nAllocationCreatePredefSized(int predef, int count);
@@ -102,6 +106,7 @@
     native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d);
     native void nAllocationRead(int id, int[] d);
     native void nAllocationRead(int id, float[] d);
+    native void nAllocationDataFromObject(int id, Type t, Object o);
 
     native void nTriangleMeshDestroy(int id);
     native void nTriangleMeshBegin(int vertex, int index);
diff --git a/graphics/java/android/renderscript/Script.java b/graphics/java/android/renderscript/Script.java
index 1c7b32b..42c58ce 100644
--- a/graphics/java/android/renderscript/Script.java
+++ b/graphics/java/android/renderscript/Script.java
@@ -21,6 +21,7 @@
  **/
 public class Script extends BaseObj {
     boolean mIsRoot;
+    Type[] mTypes;
 
     Script(int id, RenderScript rs) {
         super(rs);
@@ -62,21 +63,40 @@
     public static class Builder {
         RenderScript mRS;
         boolean mIsRoot = false;
+        Type[] mTypes;
+        int mTypeCount;
 
         Builder(RenderScript rs) {
             mRS = rs;
+            mTypes = new Type[4];
+            mTypeCount = 0;
         }
 
         public void addType(Type t) {
-            mRS.nScriptCAddType(t.mID);
+            if(mTypeCount >= mTypes.length) {
+                Type[] nt = new Type[mTypeCount * 2];
+                for(int ct=0; ct < mTypeCount; ct++) {
+                    nt[ct] = mTypes[ct];
+                }
+                mTypes = nt;
+            }
+            mTypes[mTypeCount] = t;
+            mTypeCount++;
         }
 
         void transferCreate() {
             mRS.nScriptCSetRoot(mIsRoot);
+            for(int ct=0; ct < mTypeCount; ct++) {
+                mRS.nScriptCAddType(mTypes[ct].mID);
+            }
         }
 
         void transferObject(Script s) {
             s.mIsRoot = mIsRoot;
+            s.mTypes = new Type[mTypeCount];
+            for(int ct=0; ct < mTypeCount; ct++) {
+                s.mTypes[ct] = mTypes[ct];
+            }
         }
 
         public void setRoot(boolean r) {
diff --git a/graphics/java/android/renderscript/Type.java b/graphics/java/android/renderscript/Type.java
index a5fc603..2ee164f 100644
--- a/graphics/java/android/renderscript/Type.java
+++ b/graphics/java/android/renderscript/Type.java
@@ -16,10 +16,12 @@
 
 package android.renderscript;
 
+import java.lang.reflect.Field;
+
+import android.renderscript.Element;
 import android.util.Config;
 import android.util.Log;
 
-
 /**
  * @hide
  *
@@ -28,11 +30,22 @@
     Dimension[] mDimensions;
     int[] mValues;
     Element mElement;
+    private int mNativeCache;
+    Class mJavaClass;
 
 
     Type(int id, RenderScript rs) {
         super(rs);
         mID = id;
+        mNativeCache = 0;
+    }
+
+    protected void finalize() throws Throwable {
+        if(mNativeCache) {
+            mRS.nTypeFinalDestroy(this);
+            mNativeCache = 0;
+        }
+        super.finalize();
     }
 
     public void destroy() {
@@ -43,6 +56,45 @@
         mRS.nTypeDestroy(mID);
     }
 
+    public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) {
+        Element e = Element.createFromClass(rs, c);
+        Builder b = new Builder(rs, e);
+        b.add(Dimension.X, size);
+        Type t = b.create();
+        e.destroy();
+
+        // native fields
+        {
+            Field[] fields = c.getFields();
+            int[] arTypes = new int[fields.length];
+            int[] arBits = new int[fields.length];
+
+            for(int ct=0; ct < fields.length; ct++) {
+                Field f = fields[ct];
+                Class fc = f.getType();
+                if(fc == int.class) {
+                    arTypes[ct] = Element.DataType.SIGNED.mID;
+                    arBits[ct] = 32;
+                } else if(fc == short.class) {
+                    arTypes[ct] = Element.DataType.SIGNED.mID;
+                    arBits[ct] = 16;
+                } else if(fc == byte.class) {
+                    arTypes[ct] = Element.DataType.SIGNED.mID;
+                    arBits[ct] = 8;
+                } else if(fc == float.class) {
+                    arTypes[ct] = Element.DataType.FLOAT.mID;
+                    arBits[ct] = 32;
+                } else {
+                    throw new IllegalArgumentException("Unkown field type");
+                }
+            }
+            rs.nTypeSetupFields(t, arTypes, arBits, fields);
+        }
+        t.mJavaClass = c;
+        t.setName(scriptName);
+        return t;
+    }
+
     public static class Builder {
         RenderScript mRS;
         Entry[] mEntries;
diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp
index 24bbea5..ef0cf68 100644
--- a/graphics/jni/android_renderscript_RenderScript.cpp
+++ b/graphics/jni/android_renderscript_RenderScript.cpp
@@ -50,6 +50,7 @@
 
 static jfieldID gContextId = 0;
 static jfieldID gNativeBitmapID = 0;
+static jfieldID gTypeNativeCache = 0;
 
 static void _nInit(JNIEnv *_env, jclass _this)
 {
@@ -57,6 +58,9 @@
 
     jclass bitmapClass = _env->FindClass("android/graphics/Bitmap");
     gNativeBitmapID = _env->GetFieldID(bitmapClass, "mNativeBitmap", "I");
+
+    jclass typeClass = _env->FindClass("android/renderscript/Type");
+    gTypeNativeCache = _env->GetFieldID(typeClass, "mNativeCache", "I");
 }
 
 
@@ -149,11 +153,18 @@
 }
 
 static void
-nElementAdd(JNIEnv *_env, jobject _this, jint kind, jint type, jint norm, jint bits)
+nElementAdd(JNIEnv *_env, jobject _this, jint kind, jint type, jint norm, jint bits, jstring name)
 {
     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    const char* n = NULL;
+    if (name) {
+        n = _env->GetStringUTFChars(name, NULL);
+    }
     LOG_API("nElementAdd, con(%p), kind(%i), type(%i), norm(%i), bits(%i)", con, kind, type, norm, bits);
-    rsElementAdd((RsDataKind)kind, (RsDataType)type, norm != 0, (size_t)bits);
+    rsElementAdd((RsDataKind)kind, (RsDataType)type, norm != 0, (size_t)bits, n);
+    if (n) {
+        _env->ReleaseStringUTFChars(name, n);
+    }
 }
 
 static jint
@@ -214,6 +225,91 @@
     rsTypeDestroy((RsType)eID);
 }
 
+static void * SF_LoadInt(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
+{
+    ((int32_t *)buffer)[0] = _env->GetIntField(_obj, _field);
+    return ((uint8_t *)buffer) + 4;
+}
+
+static void * SF_LoadShort(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
+{
+    ((int16_t *)buffer)[0] = _env->GetShortField(_obj, _field);
+    return ((uint8_t *)buffer) + 2;
+}
+
+static void * SF_LoadByte(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
+{
+    ((int8_t *)buffer)[0] = _env->GetByteField(_obj, _field);
+    return ((uint8_t *)buffer) + 1;
+}
+
+static void * SF_LoadFloat(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
+{
+    ((float *)buffer)[0] = _env->GetFloatField(_obj, _field);
+    return ((uint8_t *)buffer) + 4;
+}
+
+struct TypeFieldCache {
+    jfieldID field;
+    int bits;
+    void * (*ptr)(JNIEnv *, jobject, jfieldID, void *buffer);
+};
+
+struct TypeCache {
+    int fieldCount;
+    int size;
+    TypeFieldCache fields[1];
+};
+
+//{"nTypeFinalDestroy",              "(Landroid/renderscript/Type;)V",       (void*)nTypeFinalDestroy },
+static void
+nTypeFinalDestroy(JNIEnv *_env, jobject _this, jobject _type)
+{
+    TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
+    free(tc);
+}
+
+// native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs);
+static void
+nTypeSetupFields(JNIEnv *_env, jobject _this, jobject _type, jintArray _types, jintArray _bits, jobjectArray _IDs)
+{
+    int fieldCount = _env->GetArrayLength(_types);
+    size_t structSize = sizeof(TypeCache) + (sizeof(TypeFieldCache) * (fieldCount-1));
+    TypeCache *tc = (TypeCache *)malloc(structSize);
+    memset(tc, 0, structSize);
+
+    TypeFieldCache *tfc = &tc->fields[0];
+    tc->fieldCount = fieldCount;
+    _env->SetIntField(_type, gTypeNativeCache, (jint)tc);
+
+    jint *fType = _env->GetIntArrayElements(_types, NULL);
+    jint *fBits = _env->GetIntArrayElements(_bits, NULL);
+    for (int ct=0; ct < fieldCount; ct++) {
+        jobject field = _env->GetObjectArrayElement(_IDs, ct);
+        tfc[ct].field = _env->FromReflectedField(field);
+        tfc[ct].bits = fBits[ct];
+
+        switch(fType[ct]) {
+        case RS_TYPE_FLOAT:
+            tfc[ct].ptr = SF_LoadFloat;
+            break;
+        case RS_TYPE_UNSIGNED:
+        case RS_TYPE_SIGNED:
+            switch(tfc[ct].bits) {
+            case 32:    tfc[ct].ptr = SF_LoadInt;   break;
+            case 16:    tfc[ct].ptr = SF_LoadShort; break;
+            case 8:     tfc[ct].ptr = SF_LoadByte;  break;
+            }
+            break;
+        }
+        tc->size += 4;
+    }
+
+    _env->ReleaseIntArrayElements(_types, fType, JNI_ABORT);
+    _env->ReleaseIntArrayElements(_bits, fBits, JNI_ABORT);
+}
+
+
 // -----------------------------------
 
 static jint
@@ -412,6 +508,26 @@
 }
 
 
+//{"nAllocationDataFromObject",      "(ILandroid/renderscript/Type;Ljava/lang/Object;)V",   (void*)nAllocationDataFromObject },
+static void
+nAllocationDataFromObject(JNIEnv *_env, jobject _this, jint alloc, jobject _type, jobject _o)
+{
+    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    LOG_API("nAllocationDataFromObject con(%p), alloc(%p)", con, (RsAllocation)alloc);
+
+    const TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
+
+    void * bufAlloc = malloc(tc->size);
+    void * buf = bufAlloc;
+    for (int ct=0; ct < tc->fieldCount; ct++) {
+        const TypeFieldCache *tfc = &tc->fields[ct];
+        buf = tfc->ptr(_env, _o, tfc->field, buf);
+    }
+    rsAllocationData((RsAllocation)alloc, bufAlloc);
+    const uint32_t * tmp = (const uint32_t *)bufAlloc;
+    free(bufAlloc);
+}
+
 // -----------------------------------
 
 static void
@@ -1195,7 +1311,7 @@
 
 {"nElementBegin",                  "()V",                                  (void*)nElementBegin },
 {"nElementAddPredefined",          "(I)V",                                 (void*)nElementAddPredefined },
-{"nElementAdd",                    "(IIII)V",                              (void*)nElementAdd },
+{"nElementAdd",                    "(IIIILjava/lang/String;)V",            (void*)nElementAdd },
 {"nElementCreate",                 "()I",                                  (void*)nElementCreate },
 {"nElementGetPredefined",          "(I)I",                                 (void*)nElementGetPredefined },
 {"nElementDestroy",                "(I)V",                                 (void*)nElementDestroy },
@@ -1204,6 +1320,8 @@
 {"nTypeAdd",                       "(II)V",                                (void*)nTypeAdd },
 {"nTypeCreate",                    "()I",                                  (void*)nTypeCreate },
 {"nTypeDestroy",                   "(I)V",                                 (void*)nTypeDestroy },
+{"nTypeFinalDestroy",              "(Landroid/renderscript/Type;)V",       (void*)nTypeFinalDestroy },
+{"nTypeSetupFields",               "(Landroid/renderscript/Type;[I[I[Ljava/lang/reflect/Field;)V", (void*)nTypeSetupFields },
 
 {"nAllocationCreateTyped",         "(I)I",                                 (void*)nAllocationCreateTyped },
 {"nAllocationCreatePredefSized",   "(II)I",                                (void*)nAllocationCreatePredefSized },
@@ -1220,6 +1338,7 @@
 {"nAllocationSubData2D",           "(IIIII[F)V",                           (void*)nAllocationSubData2D_f },
 {"nAllocationRead",                "(I[I)V",                               (void*)nAllocationRead_i },
 {"nAllocationRead",                "(I[F)V",                               (void*)nAllocationRead_f },
+{"nAllocationDataFromObject",      "(ILandroid/renderscript/Type;Ljava/lang/Object;)V",   (void*)nAllocationDataFromObject },
 
 {"nTriangleMeshDestroy",           "(I)V",                                 (void*)nTriangleMeshDestroy },
 {"nTriangleMeshBegin",             "(II)V",                                (void*)nTriangleMeshBegin },