diff options
author | Jason Sams <rjsams@android.com> | 2009-08-12 17:54:11 -0700 |
---|---|---|
committer | Jason Sams <rjsams@android.com> | 2009-08-12 17:54:11 -0700 |
commit | 43ee06857bb7f99446d1d84f8789016c5d105558 (patch) | |
tree | cc88d57d27c7ea1c1d0a9e21a49f3fc16908e1cc /graphics/java | |
parent | a9f1dd021f8f6ee777bc4d27913bd40c42e753af (diff) | |
download | frameworks_base-43ee06857bb7f99446d1d84f8789016c5d105558.zip frameworks_base-43ee06857bb7f99446d1d84f8789016c5d105558.tar.gz frameworks_base-43ee06857bb7f99446d1d84f8789016c5d105558.tar.bz2 |
Implement reflecting Java objects into the ACC enviroment.
Diffstat (limited to 'graphics/java')
-rw-r--r-- | graphics/java/android/renderscript/Allocation.java | 35 | ||||
-rw-r--r-- | graphics/java/android/renderscript/Element.java | 36 | ||||
-rw-r--r-- | graphics/java/android/renderscript/RenderScript.java | 7 | ||||
-rw-r--r-- | graphics/java/android/renderscript/Script.java | 22 | ||||
-rw-r--r-- | graphics/java/android/renderscript/Type.java | 54 |
5 files changed, 141 insertions, 13 deletions
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 @@ import android.util.Log; * **/ 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 @@ public class Allocation extends BaseObj { 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 @@ public class Allocation extends BaseObj { 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 @@ public class Allocation extends BaseObj { 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 @@ public class Allocation extends BaseObj { } 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 @@ public class Allocation extends BaseObj { } 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 @@ public class Allocation extends BaseObj { 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 @@ public class Element extends BaseObj { 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 @@ public class Element extends BaseObj { Element.DataKind mKind; boolean mIsNormalized; int mBits; + String mName; } public Builder(RenderScript rs) { @@ -188,16 +212,22 @@ public class Element extends BaseObj { 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 @@ public class Element extends BaseObj { 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 @@ package android.renderscript; 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 @@ public class RenderScript { 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 @@ public class RenderScript { 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 @@ public class RenderScript { 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 @@ package android.renderscript; **/ public class Script extends BaseObj { boolean mIsRoot; + Type[] mTypes; Script(int id, RenderScript rs) { super(rs); @@ -62,21 +63,40 @@ public class Script extends BaseObj { 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 @@ public class Type extends BaseObj { 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 @@ public class Type extends BaseObj { 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; |