diff options
Diffstat (limited to 'graphics/java/android/renderscript')
38 files changed, 2934 insertions, 1003 deletions
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java index b27c7f5..6775c08 100644 --- a/graphics/java/android/renderscript/Allocation.java +++ b/graphics/java/android/renderscript/Allocation.java @@ -35,11 +35,25 @@ public class Allocation extends BaseObj { Bitmap mBitmap; Allocation(int id, RenderScript rs, Type t) { - super(rs); - mID = id; + super(id, rs); mType = t; } + Allocation(int id, RenderScript rs) { + super(id, rs); + } + + @Override + void updateFromNative() { + mRS.validate(); + mName = mRS.nGetName(mID); + int typeID = mRS.nAllocationGetType(mID); + if(typeID != 0) { + mType = new Type(typeID, mRS); + mType.updateFromNative(); + } + } + public Type getType() { return mType; } @@ -76,10 +90,30 @@ public class Allocation extends BaseObj { subData1D(0, mType.getElementCount(), d); } + public void subData(int off, FieldPacker fp) { + int eSize = mType.mElement.getSizeBytes(); + final byte[] data = fp.getData(); + + int count = data.length / eSize; + if ((eSize * count) != data.length) { + throw new IllegalArgumentException("Field packer length " + data.length + + " not divisible by element size " + eSize + "."); + } + data1DChecks(off, count, data.length, data.length); + mRS.nAllocationSubData1D(mID, off, count, data, data.length); + } + private void data1DChecks(int off, int count, int len, int dataSize) { mRS.validate(); - if((off < 0) || (count < 1) || ((off + count) > mType.getElementCount())) { - throw new IllegalArgumentException("Offset or Count out of bounds."); + if(off < 0) { + throw new IllegalArgumentException("Offset must be >= 0."); + } + if(count < 1) { + throw new IllegalArgumentException("Count must be >= 1."); + } + if((off + count) > mType.getElementCount()) { + throw new IllegalArgumentException("Overflow, Available count " + mType.getElementCount() + + ", got " + count + " at offset " + off + "."); } if((len) < dataSize) { throw new IllegalArgumentException("Array too small for allocation type."); @@ -146,8 +180,7 @@ public class Allocation extends BaseObj { public class Adapter1D extends BaseObj { Adapter1D(int id, RenderScript rs) { - super(rs); - mID = id; + super(id, rs); } public void setConstraint(Dimension dim, int value) { @@ -189,8 +222,7 @@ public class Allocation extends BaseObj { public class Adapter2D extends BaseObj { Adapter2D(int id, RenderScript rs) { - super(rs); - mID = id; + super(id, rs); } public void setConstraint(Dimension dim, int value) { @@ -377,6 +409,21 @@ public class Allocation extends BaseObj { Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions); return createFromBitmapBoxed(rs, b, dstFmt, genMips); } + + static public Allocation createFromString(RenderScript rs, String str) + throws IllegalArgumentException { + byte[] allocArray = null; + try { + allocArray = str.getBytes("UTF-8"); + Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length); + alloc.data(allocArray); + return alloc; + } + catch (Exception e) { + Log.e("rs", "could not convert string to utf-8"); + } + return null; + } } diff --git a/graphics/java/android/renderscript/BaseObj.java b/graphics/java/android/renderscript/BaseObj.java index 002fc78..3198e3f 100644 --- a/graphics/java/android/renderscript/BaseObj.java +++ b/graphics/java/android/renderscript/BaseObj.java @@ -24,10 +24,10 @@ import android.util.Log; **/ class BaseObj { - BaseObj(RenderScript rs) { + BaseObj(int id, RenderScript rs) { rs.validate(); mRS = rs; - mID = 0; + mID = id; mDestroyed = false; } @@ -62,7 +62,7 @@ class BaseObj { { if (!mDestroyed) { if(mID != 0 && mRS.isAlive()) { - mRS.nObjDestroyOOB(mID); + mRS.nObjDestroy(mID); } mRS = null; mID = 0; @@ -81,5 +81,10 @@ class BaseObj { mRS.nObjDestroy(mID); } + // If an object came from an a3d file, java fields need to be + // created with objects from the native layer + void updateFromNative() { + } + } diff --git a/graphics/java/android/renderscript/Vector2f.java b/graphics/java/android/renderscript/Byte2.java index 567d57f..95cf88c 100644 --- a/graphics/java/android/renderscript/Vector2f.java +++ b/graphics/java/android/renderscript/Byte2.java @@ -24,12 +24,12 @@ import android.util.Log; * @hide * **/ -public class Vector2f { - public Vector2f() { +public class Byte2 { + public Byte2() { } - public float x; - public float y; + public byte x; + public byte y; } diff --git a/graphics/java/android/renderscript/Vector3f.java b/graphics/java/android/renderscript/Byte3.java index f2842f3..a6c0ca9 100644 --- a/graphics/java/android/renderscript/Vector3f.java +++ b/graphics/java/android/renderscript/Byte3.java @@ -24,13 +24,13 @@ import android.util.Log; * @hide * **/ -public class Vector3f { - public Vector3f() { +public class Byte3 { + public Byte3() { } - public float x; - public float y; - public float z; + public byte x; + public byte y; + public byte z; } diff --git a/graphics/java/android/renderscript/Vector4f.java b/graphics/java/android/renderscript/Byte4.java index fabd959..a5bfc61 100644 --- a/graphics/java/android/renderscript/Vector4f.java +++ b/graphics/java/android/renderscript/Byte4.java @@ -24,14 +24,14 @@ import android.util.Log; * @hide * **/ -public class Vector4f { - public Vector4f() { +public class Byte4 { + public Byte4() { } - public float x; - public float y; - public float z; - public float w; + public byte x; + public byte y; + public byte z; + public byte w; } diff --git a/graphics/java/android/renderscript/Element.java b/graphics/java/android/renderscript/Element.java index 10ef05a..b811479 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 @@ -47,20 +48,22 @@ public class Element extends BaseObj { UNSIGNED_32 (10, 4), //UNSIGNED_64 (11, 8), - UNSIGNED_5_6_5 (12, 2), - UNSIGNED_5_5_5_1 (13, 2), - UNSIGNED_4_4_4_4 (14, 2), - - RS_ELEMENT (15, 4), - RS_TYPE (16, 4), - RS_ALLOCATION (17, 4), - RS_SAMPLER (18, 4), - RS_SCRIPT (19, 4), - RS_MESH (20, 4), - RS_PROGRAM_FRAGMENT (21, 4), - RS_PROGRAM_VERTEX (22, 4), - RS_PROGRAM_RASTER (23, 4), - RS_PROGRAM_STORE (24, 4); + BOOLEAN(12, 1), + + UNSIGNED_5_6_5 (13, 2), + UNSIGNED_5_5_5_1 (14, 2), + UNSIGNED_4_4_4_4 (15, 2), + + RS_ELEMENT (16, 4), + RS_TYPE (17, 4), + RS_ALLOCATION (18, 4), + RS_SAMPLER (19, 4), + RS_SCRIPT (20, 4), + RS_MESH (21, 4), + RS_PROGRAM_FRAGMENT (22, 4), + RS_PROGRAM_VERTEX (23, 4), + RS_PROGRAM_RASTER (24, 4), + RS_PROGRAM_STORE (25, 4); int mID; int mSize; @@ -72,12 +75,6 @@ public class Element extends BaseObj { public enum DataKind { USER (0), - COLOR (1), - POSITION (2), - TEXTURE (3), - NORMAL (4), - INDEX (5), - POINT_SIZE(6), PIXEL_L (7), PIXEL_A (8), @@ -91,41 +88,133 @@ public class Element extends BaseObj { } } - public static Element USER_U8(RenderScript rs) { - if(rs.mElement_USER_U8 == null) { - rs.mElement_USER_U8 = createUser(rs, DataType.UNSIGNED_8); + public static Element BOOLEAN(RenderScript rs) { + if(rs.mElement_BOOLEAN == null) { + rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN); + } + return rs.mElement_BOOLEAN; + } + + public static Element U8(RenderScript rs) { + if(rs.mElement_U8 == null) { + rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8); + } + return rs.mElement_U8; + } + + public static Element I8(RenderScript rs) { + if(rs.mElement_I8 == null) { + rs.mElement_I8 = createUser(rs, DataType.SIGNED_8); + } + return rs.mElement_I8; + } + + public static Element U16(RenderScript rs) { + if(rs.mElement_U16 == null) { + rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16); } - return rs.mElement_USER_U8; + return rs.mElement_U16; } - public static Element USER_I8(RenderScript rs) { - if(rs.mElement_USER_I8 == null) { - rs.mElement_USER_I8 = createUser(rs, DataType.SIGNED_8); + public static Element I16(RenderScript rs) { + if(rs.mElement_I16 == null) { + rs.mElement_I16 = createUser(rs, DataType.SIGNED_16); } - return rs.mElement_USER_I8; + return rs.mElement_I16; } - public static Element USER_U32(RenderScript rs) { - if(rs.mElement_USER_U32 == null) { - rs.mElement_USER_U32 = createUser(rs, DataType.UNSIGNED_32); + public static Element U32(RenderScript rs) { + if(rs.mElement_U32 == null) { + rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32); } - return rs.mElement_USER_U32; + return rs.mElement_U32; } - public static Element USER_I32(RenderScript rs) { - if(rs.mElement_USER_I32 == null) { - rs.mElement_USER_I32 = createUser(rs, DataType.SIGNED_32); + public static Element I32(RenderScript rs) { + if(rs.mElement_I32 == null) { + rs.mElement_I32 = createUser(rs, DataType.SIGNED_32); } - return rs.mElement_USER_I32; + return rs.mElement_I32; } - public static Element USER_F32(RenderScript rs) { - if(rs.mElement_USER_F32 == null) { - rs.mElement_USER_F32 = createUser(rs, DataType.FLOAT_32); + public static Element F32(RenderScript rs) { + if(rs.mElement_F32 == null) { + rs.mElement_F32 = createUser(rs, DataType.FLOAT_32); } - return rs.mElement_USER_F32; + return rs.mElement_F32; } + public static Element ELEMENT(RenderScript rs) { + if(rs.mElement_ELEMENT == null) { + rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT); + } + return rs.mElement_ELEMENT; + } + + public static Element TYPE(RenderScript rs) { + if(rs.mElement_TYPE == null) { + rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE); + } + return rs.mElement_TYPE; + } + + public static Element ALLOCATION(RenderScript rs) { + if(rs.mElement_ALLOCATION == null) { + rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION); + } + return rs.mElement_ALLOCATION; + } + + public static Element SAMPLER(RenderScript rs) { + if(rs.mElement_SAMPLER == null) { + rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER); + } + return rs.mElement_SAMPLER; + } + + public static Element SCRIPT(RenderScript rs) { + if(rs.mElement_SCRIPT == null) { + rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT); + } + return rs.mElement_SCRIPT; + } + + public static Element MESH(RenderScript rs) { + if(rs.mElement_MESH == null) { + rs.mElement_MESH = createUser(rs, DataType.RS_MESH); + } + return rs.mElement_MESH; + } + + public static Element PROGRAM_FRAGMENT(RenderScript rs) { + if(rs.mElement_PROGRAM_FRAGMENT == null) { + rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT); + } + return rs.mElement_PROGRAM_FRAGMENT; + } + + public static Element PROGRAM_VERTEX(RenderScript rs) { + if(rs.mElement_PROGRAM_VERTEX == null) { + rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX); + } + return rs.mElement_PROGRAM_VERTEX; + } + + public static Element PROGRAM_RASTER(RenderScript rs) { + if(rs.mElement_PROGRAM_RASTER == null) { + rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER); + } + return rs.mElement_PROGRAM_RASTER; + } + + public static Element PROGRAM_STORE(RenderScript rs) { + if(rs.mElement_PROGRAM_STORE == null) { + rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE); + } + return rs.mElement_PROGRAM_STORE; + } + + public static Element A_8(RenderScript rs) { if(rs.mElement_A_8 == null) { rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A); @@ -168,168 +257,117 @@ public class Element extends BaseObj { return rs.mElement_RGBA_8888; } - public static Element INDEX_16(RenderScript rs) { - if(rs.mElement_INDEX_16 == null) { - rs.mElement_INDEX_16 = createIndex(rs); - } - return rs.mElement_INDEX_16; - } - - public static Element ATTRIB_POSITION_2(RenderScript rs) { - if(rs.mElement_POSITION_2 == null) { - rs.mElement_POSITION_2 = createAttrib(rs, DataType.FLOAT_32, DataKind.POSITION, 2); + public static Element F32_2(RenderScript rs) { + if(rs.mElement_FLOAT_2 == null) { + rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2); } - return rs.mElement_POSITION_2; + return rs.mElement_FLOAT_2; } - public static Element ATTRIB_POSITION_3(RenderScript rs) { - if(rs.mElement_POSITION_3 == null) { - rs.mElement_POSITION_3 = createAttrib(rs, DataType.FLOAT_32, DataKind.POSITION, 3); + public static Element F32_3(RenderScript rs) { + if(rs.mElement_FLOAT_3 == null) { + rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3); } - return rs.mElement_POSITION_3; + return rs.mElement_FLOAT_3; } - public static Element ATTRIB_TEXTURE_2(RenderScript rs) { - if(rs.mElement_TEXTURE_2 == null) { - rs.mElement_TEXTURE_2 = createAttrib(rs, DataType.FLOAT_32, DataKind.TEXTURE, 2); + public static Element F32_4(RenderScript rs) { + if(rs.mElement_FLOAT_4 == null) { + rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4); } - return rs.mElement_TEXTURE_2; + return rs.mElement_FLOAT_4; } - public static Element ATTRIB_NORMAL_3(RenderScript rs) { - if(rs.mElement_NORMAL_3 == null) { - rs.mElement_NORMAL_3 = createAttrib(rs, DataType.FLOAT_32, DataKind.NORMAL, 3); + public static Element U8_4(RenderScript rs) { + if(rs.mElement_UCHAR_4 == null) { + rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4); } - return rs.mElement_NORMAL_3; + return rs.mElement_UCHAR_4; } - public static Element ATTRIB_COLOR_U8_4(RenderScript rs) { - if(rs.mElement_COLOR_U8_4 == null) { - rs.mElement_COLOR_U8_4 = createAttrib(rs, DataType.UNSIGNED_8, DataKind.COLOR, 4); - } - return rs.mElement_COLOR_U8_4; - } - public static Element ATTRIB_COLOR_F32_4(RenderScript rs) { - if(rs.mElement_COLOR_F32_4 == null) { - rs.mElement_COLOR_F32_4 = createAttrib(rs, DataType.FLOAT_32, DataKind.COLOR, 4); - } - return rs.mElement_COLOR_F32_4; - } - - Element(RenderScript rs, Element[] e, String[] n) { - super(rs); + Element(int id, RenderScript rs, Element[] e, String[] n) { + super(id, rs); mSize = 0; mElements = e; mElementNames = n; - int[] ids = new int[mElements.length]; for (int ct = 0; ct < mElements.length; ct++ ) { mSize += mElements[ct].mSize; - ids[ct] = mElements[ct].mID; } - mID = rs.nElementCreate2(ids, mElementNames); } - Element(RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) { - super(rs); + Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) { + super(id, rs); mSize = dt.mSize * size; mType = dt; mKind = dk; mNormalized = norm; mVectorSize = size; - mID = rs.nElementCreate(dt.mID, dk.mID, norm, size); } - public void destroy() throws IllegalStateException { - super.destroy(); + Element(int id, RenderScript rs) { + super(id, rs); } - public static Element createFromClass(RenderScript rs, Class c) { - rs.validate(); - Field[] fields = c.getFields(); - Builder b = new Builder(rs); - - for(Field f: fields) { - Class fc = f.getType(); - if(fc == int.class) { - b.add(createUser(rs, DataType.SIGNED_32), f.getName()); - } else if(fc == short.class) { - b.add(createUser(rs, DataType.SIGNED_16), f.getName()); - } else if(fc == byte.class) { - b.add(createUser(rs, DataType.SIGNED_8), f.getName()); - } else if(fc == float.class) { - b.add(createUser(rs, DataType.FLOAT_32), f.getName()); - } else { - throw new IllegalArgumentException("Unkown field type"); + @Override + void updateFromNative() { + + // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements + int[] dataBuffer = new int[5]; + mRS.nElementGetNativeData(mID, 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; } } - return b.create(); + + 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(subElementIds[i], mRS); + mElements[i].updateFromNative(); + mSize += mElements[i].mSize; + } + } + } + public void destroy() throws IllegalStateException { + super.destroy(); + } ///////////////////////////////////////// public static Element createUser(RenderScript rs, DataType dt) { - return new Element(rs, dt, DataKind.USER, false, 1); + DataKind dk = DataKind.USER; + boolean norm = false; + int vecSize = 1; + int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize); + return new Element(id, rs, dt, dk, norm, vecSize); } public static Element createVector(RenderScript rs, DataType dt, int size) { if (size < 2 || size > 4) { throw new IllegalArgumentException("Bad size"); } - return new Element(rs, dt, DataKind.USER, false, size); - } - - public static Element createIndex(RenderScript rs) { - return new Element(rs, DataType.UNSIGNED_16, DataKind.INDEX, false, 1); - } - - public static Element createAttrib(RenderScript rs, DataType dt, DataKind dk, int size) { - if (!(dt == DataType.FLOAT_32 || - dt == DataType.UNSIGNED_8 || - dt == DataType.UNSIGNED_16 || - dt == DataType.UNSIGNED_32 || - dt == DataType.SIGNED_8 || - dt == DataType.SIGNED_16 || - dt == DataType.SIGNED_32)) { - throw new IllegalArgumentException("Unsupported DataType"); - } - - if (!(dk == DataKind.COLOR || - dk == DataKind.POSITION || - dk == DataKind.TEXTURE || - dk == DataKind.NORMAL || - dk == DataKind.POINT_SIZE || - dk == DataKind.USER)) { - throw new IllegalArgumentException("Unsupported DataKind"); - } - - if (dk == DataKind.COLOR && - ((dt != DataType.FLOAT_32 && dt != DataType.UNSIGNED_8) || - size < 3 || size > 4)) { - throw new IllegalArgumentException("Bad combo"); - } - if (dk == DataKind.POSITION && (size < 1 || size > 4)) { - throw new IllegalArgumentException("Bad combo"); - } - if (dk == DataKind.TEXTURE && - (dt != DataType.FLOAT_32 || size < 1 || size > 4)) { - throw new IllegalArgumentException("Bad combo"); - } - if (dk == DataKind.NORMAL && - (dt != DataType.FLOAT_32 || size != 3)) { - throw new IllegalArgumentException("Bad combo"); - } - if (dk == DataKind.POINT_SIZE && - (dt != DataType.FLOAT_32 || size != 1)) { - throw new IllegalArgumentException("Bad combo"); - } - + DataKind dk = DataKind.USER; boolean norm = false; - if (dk == DataKind.COLOR && dt == DataType.UNSIGNED_8) { - norm = true; - } - - return new Element(rs, dt, dk, norm, size); + int id = rs.nElementCreate(dt.mID, dk.mID, norm, size); + return new Element(id, rs, dt, dk, norm, size); } public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) { @@ -367,7 +405,9 @@ public class Element extends BaseObj { size = 4; } - return new Element(rs, dt, dk, true, size); + boolean norm = true; + int id = rs.nElementCreate(dt.mID, dk.mID, norm, size); + return new Element(id, rs, dt, dk, norm, size); } public static class Builder { @@ -403,7 +443,13 @@ public class Element extends BaseObj { String[] sin = new String[mCount]; java.lang.System.arraycopy(mElements, 0, ein, 0, mCount); java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount); - return new Element(mRS, ein, sin); + + int[] ids = new int[ein.length]; + for (int ct = 0; ct < ein.length; ct++ ) { + ids[ct] = ein[ct].mID; + } + int id = mRS.nElementCreate2(ids, sin); + return new Element(id, mRS, ein, sin); } } diff --git a/graphics/java/android/renderscript/FieldPacker.java b/graphics/java/android/renderscript/FieldPacker.java index b26e47d..24f0409 100644 --- a/graphics/java/android/renderscript/FieldPacker.java +++ b/graphics/java/android/renderscript/FieldPacker.java @@ -33,21 +33,28 @@ public class FieldPacker { } } - void reset() { + public void reset() { mPos = 0; } + public void reset(int i) { + mPos = i; + } + + public void skip(int i) { + mPos += i; + } - void addI8(byte v) { + public void addI8(byte v) { mData[mPos++] = v; } - void addI16(short v) { + public void addI16(short v) { align(2); mData[mPos++] = (byte)(v & 0xff); mData[mPos++] = (byte)(v >> 8); } - void addI32(int v) { + public void addI32(int v) { align(4); mData[mPos++] = (byte)(v & 0xff); mData[mPos++] = (byte)((v >> 8) & 0xff); @@ -55,7 +62,7 @@ public class FieldPacker { mData[mPos++] = (byte)((v >> 24) & 0xff); } - void addI64(long v) { + public void addI64(long v) { align(8); mData[mPos++] = (byte)(v & 0xff); mData[mPos++] = (byte)((v >> 8) & 0xff); @@ -67,15 +74,17 @@ public class FieldPacker { mData[mPos++] = (byte)((v >> 56) & 0xff); } - void addU8(short v) { + public void addU8(short v) { if ((v < 0) || (v > 0xff)) { + android.util.Log.e("rs", "FieldPacker.addU8( " + v + " )"); throw new IllegalArgumentException("Saving value out of range for type"); } mData[mPos++] = (byte)v; } - void addU16(int v) { + public void addU16(int v) { if ((v < 0) || (v > 0xffff)) { + android.util.Log.e("rs", "FieldPacker.addU16( " + v + " )"); throw new IllegalArgumentException("Saving value out of range for type"); } align(2); @@ -83,8 +92,9 @@ public class FieldPacker { mData[mPos++] = (byte)(v >> 8); } - void addU32(long v) { - if ((v < 0) || (v > 0xffffffff)) { + public void addU32(long v) { + if ((v < 0) || (v > 0xffffffffL)) { + android.util.Log.e("rs", "FieldPacker.addU32( " + v + " )"); throw new IllegalArgumentException("Saving value out of range for type"); } align(4); @@ -94,8 +104,9 @@ public class FieldPacker { mData[mPos++] = (byte)((v >> 24) & 0xff); } - void addU64(long v) { + public void addU64(long v) { if (v < 0) { + android.util.Log.e("rs", "FieldPacker.addU64( " + v + " )"); throw new IllegalArgumentException("Saving value out of range for type"); } align(8); @@ -109,15 +120,139 @@ public class FieldPacker { mData[mPos++] = (byte)((v >> 56) & 0xff); } - void addF32(float v) { + public void addF32(float v) { addI32(Float.floatToRawIntBits(v)); } - void addF64(float v) { + public void addF64(float v) { addI64(Double.doubleToRawLongBits(v)); } - final byte[] getData() { + public void addObj(BaseObj obj) { + if (obj != null) { + addI32(obj.getID()); + } else { + addI32(0); + } + } + + public void addF32(Float2 v) { + addF32(v.x); + addF32(v.y); + } + public void addF32(Float3 v) { + addF32(v.x); + addF32(v.y); + addF32(v.z); + } + public void addF32(Float4 v) { + addF32(v.x); + addF32(v.y); + addF32(v.z); + addF32(v.w); + } + + public void addI8(Byte2 v) { + addI8(v.x); + addI8(v.y); + } + public void addI8(Byte3 v) { + addI8(v.x); + addI8(v.y); + addI8(v.z); + } + public void addI8(Byte4 v) { + addI8(v.x); + addI8(v.y); + addI8(v.z); + addI8(v.w); + } + + public void addU8(Short2 v) { + addU8(v.x); + addU8(v.y); + } + public void addU8(Short3 v) { + addU8(v.x); + addU8(v.y); + addU8(v.z); + } + public void addU8(Short4 v) { + addU8(v.x); + addU8(v.y); + addU8(v.z); + addU8(v.w); + } + + public void addI16(Short2 v) { + addI16(v.x); + addI16(v.y); + } + public void addI16(Short3 v) { + addI16(v.x); + addI16(v.y); + addI16(v.z); + } + public void addI16(Short4 v) { + addI16(v.x); + addI16(v.y); + addI16(v.z); + addI16(v.w); + } + + public void addU16(Int2 v) { + addU16(v.x); + addU16(v.y); + } + public void addU16(Int3 v) { + addU16(v.x); + addU16(v.y); + addU16(v.z); + } + public void addU16(Int4 v) { + addU16(v.x); + addU16(v.y); + addU16(v.z); + addU16(v.w); + } + + public void addI32(Int2 v) { + addI32(v.x); + addI32(v.y); + } + public void addI32(Int3 v) { + addI32(v.x); + addI32(v.y); + addI32(v.z); + } + public void addI32(Int4 v) { + addI32(v.x); + addI32(v.y); + addI32(v.z); + addI32(v.w); + } + + public void addU32(Int2 v) { + addU32(v.x); + addU32(v.y); + } + public void addU32(Int3 v) { + addU32(v.x); + addU32(v.y); + addU32(v.z); + } + public void addU32(Int4 v) { + addU32(v.x); + addU32(v.y); + addU32(v.z); + addU32(v.w); + } + + public void addBoolean(boolean v) { + addI8((byte)(v ? 1 : 0)); + } + + public final byte[] getData() { return mData; } diff --git a/graphics/java/android/renderscript/FileA3D.java b/graphics/java/android/renderscript/FileA3D.java new file mode 100644 index 0000000..2414062 --- /dev/null +++ b/graphics/java/android/renderscript/FileA3D.java @@ -0,0 +1,217 @@ +/* + * Copyright (C) 2008 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. + */ + +package android.renderscript; + +import java.io.IOException; +import java.io.InputStream; + +import android.content.res.Resources; +import android.content.res.AssetManager; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.util.Log; +import android.util.TypedValue; + +/** + * @hide + * + **/ +public class FileA3D extends BaseObj { + + public enum ClassID { + + UNKNOWN, + MESH, + TYPE, + ELEMENT, + ALLOCATION, + PROGRAM_VERTEX, + PROGRAM_RASTER, + PROGRAM_FRAGMENT, + PROGRAM_STORE, + SAMPLER, + ANIMATION, + LIGHT, + ADAPTER_1D, + ADAPTER_2D, + SCRIPT_C; + + public static ClassID toClassID(int intID) { + return ClassID.values()[intID]; + } + } + + // Read only class with index entries + public static class IndexEntry { + RenderScript mRS; + int mIndex; + int mID; + String mName; + ClassID mClassID; + BaseObj mLoadedObj; + + public String getName() { + return mName; + } + + public ClassID getClassID() { + return mClassID; + } + + public BaseObj getObject() { + 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(entry.mClassID == ClassID.UNKNOWN) { + return null; + } + + int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex); + if(objectID == 0) { + return null; + } + + switch (entry.mClassID) { + case MESH: + entry.mLoadedObj = new Mesh(objectID, rs); + break; + case TYPE: + entry.mLoadedObj = new Type(objectID, rs); + break; + case ELEMENT: + entry.mLoadedObj = null; + break; + case ALLOCATION: + entry.mLoadedObj = null; + break; + case PROGRAM_VERTEX: + entry.mLoadedObj = new ProgramVertex(objectID, rs); + break; + case PROGRAM_RASTER: + break; + case PROGRAM_FRAGMENT: + break; + case PROGRAM_STORE: + break; + case SAMPLER: + break; + case ANIMATION: + break; + case LIGHT: + break; + case ADAPTER_1D: + break; + case ADAPTER_2D: + break; + case SCRIPT_C: + break; + } + + entry.mLoadedObj.updateFromNative(); + + return entry.mLoadedObj; + } + + IndexEntry(RenderScript rs, int index, int id, String name, ClassID classID) { + mRS = rs; + mIndex = index; + mID = id; + mName = name; + mClassID = classID; + mLoadedObj = null; + } + } + + IndexEntry[] mFileEntries; + + FileA3D(int id, RenderScript rs) { + super(id, rs); + } + + private void initEntries() { + int numFileEntries = mRS.nFileA3DGetNumIndexEntries(mID); + if(numFileEntries <= 0) { + return; + } + + mFileEntries = new IndexEntry[numFileEntries]; + int[] ids = new int[numFileEntries]; + String[] names = new String[numFileEntries]; + + mRS.nFileA3DGetIndexEntries(mID, numFileEntries, ids, names); + + for(int i = 0; i < numFileEntries; i ++) { + mFileEntries[i] = new IndexEntry(mRS, i, mID, names[i], ClassID.toClassID(ids[i])); + } + } + + public int getNumIndexEntries() { + if(mFileEntries == null) { + return 0; + } + return mFileEntries.length; + } + + public IndexEntry getIndexEntry(int index) { + if(getNumIndexEntries() == 0 || index < 0 || index >= mFileEntries.length) { + return null; + } + return mFileEntries[index]; + } + + static public FileA3D createFromResource(RenderScript rs, Resources res, int id) + throws IllegalArgumentException { + + rs.validate(); + InputStream is = null; + try { + final TypedValue value = new TypedValue(); + is = res.openRawResource(id, value); + + int asset = ((AssetManager.AssetInputStream) is).getAssetInt(); + + int fileId = rs.nFileA3DCreateFromAssetStream(asset); + + if(fileId == 0) { + throw new IllegalStateException("Load failed."); + } + FileA3D fa3d = new FileA3D(fileId, rs); + fa3d.initEntries(); + return fa3d; + + } catch (Exception e) { + // Ignore + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException e) { + // Ignore + } + } + } + + return null; + } +} diff --git a/graphics/java/android/renderscript/Float2.java b/graphics/java/android/renderscript/Float2.java new file mode 100644 index 0000000..889bf7b --- /dev/null +++ b/graphics/java/android/renderscript/Float2.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2009 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. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Float2 { + public Float2() { + } + + public Float2(float initX, float initY) { + x = initX; + y = initY; + } + + public float x; + public float y; +} + + + + diff --git a/graphics/java/android/renderscript/Float3.java b/graphics/java/android/renderscript/Float3.java new file mode 100644 index 0000000..ebe140d --- /dev/null +++ b/graphics/java/android/renderscript/Float3.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2009 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. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Float3 { + public Float3() { + } + public Float3(float initX, float initY, float initZ) { + x = initX; + y = initY; + z = initZ; + } + + public float x; + public float y; + public float z; +} + + + + diff --git a/graphics/java/android/renderscript/Float4.java b/graphics/java/android/renderscript/Float4.java new file mode 100644 index 0000000..847732f --- /dev/null +++ b/graphics/java/android/renderscript/Float4.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2009 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. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Float4 { + public Float4() { + } + + public Float4(float initX, float initY, float initZ, float initW) { + x = initX; + y = initY; + z = initZ; + w = initW; + } + + public float x; + public float y; + public float z; + public float w; +} + + + diff --git a/graphics/java/android/renderscript/Font.java b/graphics/java/android/renderscript/Font.java new file mode 100644 index 0000000..de25014 --- /dev/null +++ b/graphics/java/android/renderscript/Font.java @@ -0,0 +1,165 @@ +/* + * Copyright (C) 2008 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. + */ + +package android.renderscript; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.HashMap; + +import android.content.res.Resources; +import android.content.res.AssetManager; +import android.util.Log; +import android.util.TypedValue; + +/** + * @hide + * + **/ +public class Font extends BaseObj { + + //These help us create a font by family name + private static final String[] sSansNames = { + "sans-serif", "arial", "helvetica", "tahoma", "verdana" + }; + + private static final String[] sSerifNames = { + "serif", "times", "times new roman", "palatino", "georgia", "baskerville", + "goudy", "fantasy", "cursive", "ITC Stone Serif" + }; + + private static final String[] sMonoNames = { + "monospace", "courier", "courier new", "monaco" + }; + + private static class FontFamily { + String[] mNames; + String mNormalFileName; + String mBoldFileName; + String mItalicFileName; + String mBoldItalicFileName; + } + + private static Map<String, FontFamily> sFontFamilyMap; + + public enum Style { + NORMAL, + BOLD, + ITALIC, + BOLD_ITALIC; + } + + private static void addFamilyToMap(FontFamily family) { + for(int i = 0; i < family.mNames.length; i ++) { + sFontFamilyMap.put(family.mNames[i], family); + } + } + + private static void initFontFamilyMap() { + sFontFamilyMap = new HashMap<String, FontFamily>(); + + FontFamily sansFamily = new FontFamily(); + sansFamily.mNames = sSansNames; + sansFamily.mNormalFileName = "DroidSans.ttf"; + sansFamily.mBoldFileName = "DroidSans-Bold.ttf"; + sansFamily.mItalicFileName = "DroidSans.ttf"; + sansFamily.mBoldItalicFileName = "DroidSans-Bold.ttf"; + addFamilyToMap(sansFamily); + + FontFamily serifFamily = new FontFamily(); + serifFamily.mNames = sSerifNames; + serifFamily.mNormalFileName = "DroidSerif-Regular.ttf"; + serifFamily.mBoldFileName = "DroidSerif-Bold.ttf"; + serifFamily.mItalicFileName = "DroidSerif-Italic.ttf"; + serifFamily.mBoldItalicFileName = "DroidSerif-BoldItalic.ttf"; + addFamilyToMap(serifFamily); + + FontFamily monoFamily = new FontFamily(); + monoFamily.mNames = sMonoNames; + monoFamily.mNormalFileName = "DroidSansMono.ttf"; + monoFamily.mBoldFileName = "DroidSansMono.ttf"; + monoFamily.mItalicFileName = "DroidSansMono.ttf"; + monoFamily.mBoldItalicFileName = "DroidSansMono.ttf"; + addFamilyToMap(monoFamily); + } + + static { + initFontFamilyMap(); + } + + static String getFontFileName(String familyName, Style style) { + FontFamily family = sFontFamilyMap.get(familyName); + if(family != null) { + switch(style) { + case NORMAL: + return family.mNormalFileName; + case BOLD: + return family.mBoldFileName; + case ITALIC: + return family.mItalicFileName; + case BOLD_ITALIC: + return family.mBoldItalicFileName; + } + } + // Fallback if we could not find the desired family + return "DroidSans.ttf"; + } + + Font(int id, RenderScript rs) { + super(id, rs); + } + + /** + * Takes a specific file name as an argument + */ + static public Font create(RenderScript rs, Resources res, String fileName, int size) + throws IllegalArgumentException { + + rs.validate(); + try { + int dpi = res.getDisplayMetrics().densityDpi; + int fontId = rs.nFontCreateFromFile(fileName, size, dpi); + + if(fontId == 0) { + throw new IllegalStateException("Failed loading a font"); + } + Font rsFont = new Font(fontId, rs); + + return rsFont; + + } catch (Exception e) { + // Ignore + } + + return null; + } + + /** + * Accepts one of the following family names as an argument + * and will attemp to produce the best match with a system font + * "sans-serif" "arial" "helvetica" "tahoma" "verdana" + * "serif" "times" "times new roman" "palatino" "georgia" "baskerville" + * "goudy" "fantasy" "cursive" "ITC Stone Serif" + * "monospace" "courier" "courier new" "monaco" + * Returns default font if no match could be found + */ + static public Font createFromFamily(RenderScript rs, Resources res, String familyName, Style fontStyle, int size) + throws IllegalArgumentException { + String fileName = getFontFileName(familyName, fontStyle); + return create(rs, res, fileName, size); + } +} diff --git a/graphics/java/android/renderscript/Int2.java b/graphics/java/android/renderscript/Int2.java new file mode 100644 index 0000000..56e2fe9 --- /dev/null +++ b/graphics/java/android/renderscript/Int2.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2009 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. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Int2 { + public Int2() { + } + + public int x; + public int y; +} + + + + diff --git a/graphics/java/android/renderscript/Int3.java b/graphics/java/android/renderscript/Int3.java new file mode 100644 index 0000000..1b27509 --- /dev/null +++ b/graphics/java/android/renderscript/Int3.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Int3 { + public Int3() { + } + + public int x; + public int y; + public int z; +} + + + + diff --git a/graphics/java/android/renderscript/Int4.java b/graphics/java/android/renderscript/Int4.java new file mode 100644 index 0000000..3d6f3f5 --- /dev/null +++ b/graphics/java/android/renderscript/Int4.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Int4 { + public Int4() { + } + + public int x; + public int y; + public int z; + public int w; +} + + + diff --git a/graphics/java/android/renderscript/Light.java b/graphics/java/android/renderscript/Light.java deleted file mode 100644 index aab656f..0000000 --- a/graphics/java/android/renderscript/Light.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (C) 2008 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. - */ - -package android.renderscript; - -import android.util.Config; -import android.util.Log; - -/** - * @hide - * - **/ -public class Light extends BaseObj { - Light(int id, RenderScript rs) { - super(rs); - mID = id; - } - - public void setColor(float r, float g, float b) { - mRS.validate(); - mRS.nLightSetColor(mID, r, g, b); - } - - public void setPosition(float x, float y, float z) { - mRS.validate(); - mRS.nLightSetPosition(mID, x, y, z); - } - - public static class Builder { - RenderScript mRS; - boolean mIsMono; - boolean mIsLocal; - - public Builder(RenderScript rs) { - mRS = rs; - mIsMono = false; - mIsLocal = false; - } - - public void lightSetIsMono(boolean isMono) { - mIsMono = isMono; - } - - public void lightSetIsLocal(boolean isLocal) { - mIsLocal = isLocal; - } - - static synchronized Light internalCreate(RenderScript rs, Builder b) { - rs.nSamplerBegin(); - rs.nLightSetIsMono(b.mIsMono); - rs.nLightSetIsLocal(b.mIsLocal); - int id = rs.nLightCreate(); - return new Light(id, rs); - } - - public Light create() { - mRS.validate(); - return internalCreate(mRS, this); - } - } - -} - diff --git a/graphics/java/android/renderscript/Long2.java b/graphics/java/android/renderscript/Long2.java new file mode 100644 index 0000000..11ead2f --- /dev/null +++ b/graphics/java/android/renderscript/Long2.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2009 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. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Long2 { + public Long2() { + } + + public long x; + public long y; +} + + + + diff --git a/graphics/java/android/renderscript/Long3.java b/graphics/java/android/renderscript/Long3.java new file mode 100644 index 0000000..1604532 --- /dev/null +++ b/graphics/java/android/renderscript/Long3.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Long3 { + public Long3() { + } + + public long x; + public long y; + public long z; +} + + + + diff --git a/graphics/java/android/renderscript/Long4.java b/graphics/java/android/renderscript/Long4.java new file mode 100644 index 0000000..2fd2747 --- /dev/null +++ b/graphics/java/android/renderscript/Long4.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Long4 { + public Long4() { + } + + public long x; + public long y; + public long z; + public long w; +} + + + diff --git a/graphics/java/android/renderscript/Matrix2f.java b/graphics/java/android/renderscript/Matrix2f.java index 4b5e61b..99d23db 100644 --- a/graphics/java/android/renderscript/Matrix2f.java +++ b/graphics/java/android/renderscript/Matrix2f.java @@ -51,6 +51,57 @@ public class Matrix2f { System.arraycopy(mMat, 0, src, 0, 4); } + public void loadRotate(float rot) { + float c, s; + rot *= (float)(java.lang.Math.PI / 180.0f); + c = (float)java.lang.Math.cos(rot); + s = (float)java.lang.Math.sin(rot); + mMat[0] = c; + mMat[1] = -s; + mMat[2] = s; + mMat[3] = c; + } + + public void loadScale(float x, float y) { + loadIdentity(); + mMat[0] = x; + mMat[3] = y; + } + public void loadMultiply(Matrix2f lhs, Matrix2f rhs) { + for (int i=0 ; i<2 ; i++) { + float ri0 = 0; + float ri1 = 0; + for (int j=0 ; j<2 ; j++) { + float rhs_ij = rhs.get(i,j); + ri0 += lhs.get(j,0) * rhs_ij; + ri1 += lhs.get(j,1) * rhs_ij; + } + set(i,0, ri0); + set(i,1, ri1); + } + } + + public void multiply(Matrix2f rhs) { + Matrix2f tmp = new Matrix2f(); + tmp.loadMultiply(this, rhs); + load(tmp); + } + public void rotate(float rot) { + Matrix2f tmp = new Matrix2f(); + tmp.loadRotate(rot); + multiply(tmp); + } + public void scale(float x, float y) { + Matrix2f tmp = new Matrix2f(); + tmp.loadScale(x, y); + multiply(tmp); + } + public void transpose() { + float temp = mMat[1]; + mMat[1] = mMat[2]; + mMat[2] = temp; + } + final float[] mMat; } diff --git a/graphics/java/android/renderscript/Matrix3f.java b/graphics/java/android/renderscript/Matrix3f.java index 19d7b43..961bc5d 100644 --- a/graphics/java/android/renderscript/Matrix3f.java +++ b/graphics/java/android/renderscript/Matrix3f.java @@ -57,6 +57,124 @@ public class Matrix3f { System.arraycopy(mMat, 0, src, 0, 9); } + public void loadRotate(float rot, float x, float y, float z) { + float c, s; + rot *= (float)(java.lang.Math.PI / 180.0f); + c = (float)java.lang.Math.cos(rot); + s = (float)java.lang.Math.sin(rot); + + float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z); + if (!(len != 1)) { + float recipLen = 1.f / len; + x *= recipLen; + y *= recipLen; + z *= recipLen; + } + float nc = 1.0f - c; + float xy = x * y; + float yz = y * z; + float zx = z * x; + float xs = x * s; + float ys = y * s; + float zs = z * s; + mMat[0] = x*x*nc + c; + mMat[3] = xy*nc - zs; + mMat[6] = zx*nc + ys; + mMat[1] = xy*nc + zs; + mMat[4] = y*y*nc + c; + mMat[9] = yz*nc - xs; + mMat[2] = zx*nc - ys; + mMat[6] = yz*nc + xs; + mMat[8] = z*z*nc + c; + } + + public void loadRotate(float rot) { + float c, s; + rot *= (float)(java.lang.Math.PI / 180.0f); + c = (float)java.lang.Math.cos(rot); + s = (float)java.lang.Math.sin(rot); + mMat[0] = c; + mMat[1] = -s; + mMat[3] = s; + mMat[4] = c; + } + + public void loadScale(float x, float y) { + loadIdentity(); + mMat[0] = x; + mMat[4] = y; + } + + public void loadScale(float x, float y, float z) { + loadIdentity(); + mMat[0] = x; + mMat[4] = y; + mMat[8] = z; + } + + public void loadTranslate(float x, float y) { + loadIdentity(); + mMat[6] = x; + mMat[7] = y; + } + + public void loadMultiply(Matrix3f lhs, Matrix3f rhs) { + for (int i=0 ; i<3 ; i++) { + float ri0 = 0; + float ri1 = 0; + float ri2 = 0; + for (int j=0 ; j<3 ; j++) { + float rhs_ij = rhs.get(i,j); + ri0 += lhs.get(j,0) * rhs_ij; + ri1 += lhs.get(j,1) * rhs_ij; + ri2 += lhs.get(j,2) * rhs_ij; + } + set(i,0, ri0); + set(i,1, ri1); + set(i,2, ri2); + } + } + + public void multiply(Matrix3f rhs) { + Matrix3f tmp = new Matrix3f(); + tmp.loadMultiply(this, rhs); + load(tmp); + } + public void rotate(float rot, float x, float y, float z) { + Matrix3f tmp = new Matrix3f(); + tmp.loadRotate(rot, x, y, z); + multiply(tmp); + } + public void rotate(float rot) { + Matrix3f tmp = new Matrix3f(); + tmp.loadRotate(rot); + multiply(tmp); + } + public void scale(float x, float y) { + Matrix3f tmp = new Matrix3f(); + tmp.loadScale(x, y); + multiply(tmp); + } + public void scale(float x, float y, float z) { + Matrix3f tmp = new Matrix3f(); + tmp.loadScale(x, y, z); + multiply(tmp); + } + public void translate(float x, float y) { + Matrix3f tmp = new Matrix3f(); + tmp.loadTranslate(x, y); + multiply(tmp); + } + public void transpose() { + for(int i = 0; i < 2; ++i) { + for(int j = i + 1; j < 3; ++j) { + float temp = mMat[i*3 + j]; + mMat[i*3 + j] = mMat[j*3 + i]; + mMat[j*3 + i] = temp; + } + } + } + final float[] mMat; } diff --git a/graphics/java/android/renderscript/Matrix4f.java b/graphics/java/android/renderscript/Matrix4f.java index ebd5bde..5ffc21a 100644 --- a/graphics/java/android/renderscript/Matrix4f.java +++ b/graphics/java/android/renderscript/Matrix4f.java @@ -179,6 +179,85 @@ public class Matrix4f { tmp.loadTranslate(x, y, z); multiply(tmp); } + private float computeCofactor(int i, int j) { + int c0 = (i+1) % 4; + int c1 = (i+2) % 4; + int c2 = (i+3) % 4; + int r0 = (j+1) % 4; + int r1 = (j+2) % 4; + int r2 = (j+3) % 4; + + float minor = (mMat[c0 + 4*r0] * (mMat[c1 + 4*r1] * mMat[c2 + 4*r2] - + mMat[c1 + 4*r2] * mMat[c2 + 4*r1])) + - (mMat[c0 + 4*r1] * (mMat[c1 + 4*r0] * mMat[c2 + 4*r2] - + mMat[c1 + 4*r2] * mMat[c2 + 4*r0])) + + (mMat[c0 + 4*r2] * (mMat[c1 + 4*r0] * mMat[c2 + 4*r1] - + mMat[c1 + 4*r1] * mMat[c2 + 4*r0])); + + float cofactor = ((i+j) & 1) != 0 ? -minor : minor; + return cofactor; + } + + public boolean inverse() { + + Matrix4f result = new Matrix4f(); + + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + result.mMat[4*i + j] = computeCofactor(i, j); + } + } + + // Dot product of 0th column of source and 0th row of result + float det = mMat[0]*result.mMat[0] + mMat[4]*result.mMat[1] + + mMat[8]*result.mMat[2] + mMat[12]*result.mMat[3]; + + if (Math.abs(det) < 1e-6) { + return false; + } + + det = 1.0f / det; + for (int i = 0; i < 16; ++i) { + mMat[i] = result.mMat[i] * det; + } + + return true; + } + + public boolean inverseTranspose() { + + Matrix4f result = new Matrix4f(); + + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + result.mMat[4*j + i] = computeCofactor(i, j); + } + } + + float det = mMat[0]*result.mMat[0] + mMat[4]*result.mMat[4] + + mMat[8]*result.mMat[8] + mMat[12]*result.mMat[12]; + + if (Math.abs(det) < 1e-6) { + return false; + } + + det = 1.0f / det; + for (int i = 0; i < 16; ++i) { + mMat[i] = result.mMat[i] * det; + } + + return true; + } + + public void transpose() { + for(int i = 0; i < 3; ++i) { + for(int j = i + 1; j < 4; ++j) { + float temp = mMat[i*4 + j]; + mMat[i*4 + j] = mMat[j*4 + i]; + mMat[j*4 + i] = temp; + } + } + } final float[] mMat; } diff --git a/graphics/java/android/renderscript/Mesh.java b/graphics/java/android/renderscript/Mesh.java new file mode 100644 index 0000000..b74c1f8 --- /dev/null +++ b/graphics/java/android/renderscript/Mesh.java @@ -0,0 +1,479 @@ +/* + * Copyright (C) 2008 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. + */ + +package android.renderscript; + +import java.util.Vector; + +import android.util.Config; +import android.util.Log; + +/** + * @hide + * + **/ +public class Mesh extends BaseObj { + + Allocation[] mVertexBuffers; + Allocation[] mIndexBuffers; + Primitive[] mPrimitives; + + Mesh(int id, RenderScript rs) { + super(id, rs); + } + + public int getVertexAllocationCount() { + if(mVertexBuffers == null) { + return 0; + } + return mVertexBuffers.length; + } + public Allocation getVertexAllocation(int slot) { + return mVertexBuffers[slot]; + } + + public int getPrimitiveCount() { + if(mIndexBuffers == null) { + return 0; + } + return mIndexBuffers.length; + } + public Allocation getIndexAllocation(int slot) { + return mIndexBuffers[slot]; + } + public Primitive getPrimitive(int slot) { + return mPrimitives[slot]; + } + + @Override + void updateFromNative() { + mName = mRS.nGetName(mID); + int vtxCount = mRS.nMeshGetVertexBufferCount(mID); + int idxCount = mRS.nMeshGetIndexCount(mID); + + int[] vtxIDs = new int[vtxCount]; + int[] idxIDs = new int[idxCount]; + int[] primitives = new int[idxCount]; + + mRS.nMeshGetVertices(mID, vtxIDs, vtxCount); + mRS.nMeshGetIndices(mID, idxIDs, primitives, vtxCount); + + mVertexBuffers = new Allocation[vtxCount]; + mIndexBuffers = new Allocation[idxCount]; + mPrimitives = new Primitive[idxCount]; + + for(int i = 0; i < vtxCount; i ++) { + if(vtxIDs[i] != 0) { + mVertexBuffers[i] = new Allocation(vtxIDs[i], mRS); + mVertexBuffers[i].updateFromNative(); + } + } + + for(int i = 0; i < idxCount; i ++) { + if(idxIDs[i] != 0) { + mIndexBuffers[i] = new Allocation(idxIDs[i], mRS); + mIndexBuffers[i].updateFromNative(); + } + mPrimitives[i] = Primitive.values()[primitives[i]]; + } + } + + public static class Builder { + RenderScript mRS; + + class Entry { + Type t; + Element e; + int size; + Primitive prim; + } + + int mVertexTypeCount; + Entry[] mVertexTypes; + Vector mIndexTypes; + + public Builder(RenderScript rs) { + mRS = rs; + mVertexTypeCount = 0; + mVertexTypes = new Entry[16]; + mIndexTypes = new Vector(); + } + + public int addVertexType(Type t) throws IllegalStateException { + if (mVertexTypeCount >= mVertexTypes.length) { + throw new IllegalStateException("Max vertex types exceeded."); + } + + int addedIndex = mVertexTypeCount; + mVertexTypes[mVertexTypeCount] = new Entry(); + mVertexTypes[mVertexTypeCount].t = t; + mVertexTypes[mVertexTypeCount].e = null; + mVertexTypeCount++; + return addedIndex; + } + + public int addVertexType(Element e, int size) throws IllegalStateException { + if (mVertexTypeCount >= mVertexTypes.length) { + throw new IllegalStateException("Max vertex types exceeded."); + } + + int addedIndex = mVertexTypeCount; + mVertexTypes[mVertexTypeCount] = new Entry(); + mVertexTypes[mVertexTypeCount].t = null; + mVertexTypes[mVertexTypeCount].e = e; + mVertexTypes[mVertexTypeCount].size = size; + mVertexTypeCount++; + return addedIndex; + } + + public int addIndexType(Type t, Primitive p) { + int addedIndex = mIndexTypes.size(); + Entry indexType = new Entry(); + indexType.t = t; + indexType.e = null; + indexType.size = 0; + indexType.prim = p; + mIndexTypes.addElement(indexType); + return addedIndex; + } + + public int addIndexType(Primitive p) { + int addedIndex = mIndexTypes.size(); + Entry indexType = new Entry(); + indexType.t = null; + indexType.e = null; + indexType.size = 0; + indexType.prim = p; + mIndexTypes.addElement(indexType); + return addedIndex; + } + + public int addIndexType(Element e, int size, Primitive p) { + int addedIndex = mIndexTypes.size(); + Entry indexType = new Entry(); + indexType.t = null; + indexType.e = e; + indexType.size = size; + indexType.prim = p; + mIndexTypes.addElement(indexType); + return addedIndex; + } + + Type newType(Element e, int size) { + Type.Builder tb = new Type.Builder(mRS, e); + tb.add(Dimension.X, size); + return tb.create(); + } + + static synchronized Mesh internalCreate(RenderScript rs, Builder b) { + + int id = rs.nMeshCreate(b.mVertexTypeCount, b.mIndexTypes.size()); + Mesh newMesh = new Mesh(id, rs); + newMesh.mIndexBuffers = new Allocation[b.mIndexTypes.size()]; + newMesh.mPrimitives = new Primitive[b.mIndexTypes.size()]; + newMesh.mVertexBuffers = new Allocation[b.mVertexTypeCount]; + + for(int ct = 0; ct < b.mIndexTypes.size(); ct ++) { + Allocation alloc = null; + Entry entry = (Entry)b.mIndexTypes.elementAt(ct); + if (entry.t != null) { + alloc = Allocation.createTyped(rs, entry.t); + } + else if(entry.e != null) { + alloc = Allocation.createSized(rs, entry.e, entry.size); + } + int allocID = (alloc == null) ? 0 : alloc.getID(); + rs.nMeshBindIndex(id, allocID, entry.prim.mID, ct); + newMesh.mIndexBuffers[ct] = alloc; + newMesh.mPrimitives[ct] = entry.prim; + } + + for(int ct = 0; ct < b.mVertexTypeCount; ct ++) { + Allocation alloc = null; + Entry entry = b.mVertexTypes[ct]; + if (entry.t != null) { + alloc = Allocation.createTyped(rs, entry.t); + } else if(entry.e != null) { + alloc = Allocation.createSized(rs, entry.e, entry.size); + } + rs.nMeshBindVertex(id, alloc.getID(), ct); + newMesh.mVertexBuffers[ct] = alloc; + } + + return newMesh; + } + + public Mesh create() { + mRS.validate(); + Mesh sm = internalCreate(mRS, this); + return sm; + } + } + + public static class AllocationBuilder { + RenderScript mRS; + + class Entry { + Allocation a; + Primitive prim; + } + + int mVertexTypeCount; + Entry[] mVertexTypes; + + Vector mIndexTypes; + + public AllocationBuilder(RenderScript rs) { + mRS = rs; + mVertexTypeCount = 0; + mVertexTypes = new Entry[16]; + mIndexTypes = new Vector(); + } + + public int addVertexAllocation(Allocation a) throws IllegalStateException { + if (mVertexTypeCount >= mVertexTypes.length) { + throw new IllegalStateException("Max vertex types exceeded."); + } + + int addedIndex = mVertexTypeCount; + mVertexTypes[mVertexTypeCount] = new Entry(); + mVertexTypes[mVertexTypeCount].a = a; + mVertexTypeCount++; + return addedIndex; + } + + public int addIndexAllocation(Allocation a, Primitive p) { + int addedIndex = mIndexTypes.size(); + Entry indexType = new Entry(); + indexType.a = a; + indexType.prim = p; + mIndexTypes.addElement(indexType); + return addedIndex; + } + + public int addIndexType(Primitive p) { + int addedIndex = mIndexTypes.size(); + Entry indexType = new Entry(); + indexType.a = null; + indexType.prim = p; + mIndexTypes.addElement(indexType); + return addedIndex; + } + + static synchronized Mesh internalCreate(RenderScript rs, AllocationBuilder b) { + + int id = rs.nMeshCreate(b.mVertexTypeCount, b.mIndexTypes.size()); + Mesh newMesh = new Mesh(id, rs); + newMesh.mIndexBuffers = new Allocation[b.mIndexTypes.size()]; + newMesh.mPrimitives = new Primitive[b.mIndexTypes.size()]; + newMesh.mVertexBuffers = new Allocation[b.mVertexTypeCount]; + + for(int ct = 0; ct < b.mIndexTypes.size(); ct ++) { + Entry entry = (Entry)b.mIndexTypes.elementAt(ct); + int allocID = (entry.a == null) ? 0 : entry.a.getID(); + rs.nMeshBindIndex(id, allocID, entry.prim.mID, ct); + newMesh.mIndexBuffers[ct] = entry.a; + newMesh.mPrimitives[ct] = entry.prim; + } + + for(int ct = 0; ct < b.mVertexTypeCount; ct ++) { + Entry entry = b.mVertexTypes[ct]; + rs.nMeshBindVertex(id, entry.a.mID, ct); + newMesh.mVertexBuffers[ct] = entry.a; + } + + return newMesh; + } + + public Mesh create() { + mRS.validate(); + Mesh sm = internalCreate(mRS, this); + return sm; + } + } + + + public static class TriangleMeshBuilder { + float mVtxData[]; + int mVtxCount; + short mIndexData[]; + int mIndexCount; + RenderScript mRS; + Element mElement; + + float mNX = 0; + float mNY = 0; + float mNZ = -1; + float mS0 = 0; + float mT0 = 0; + float mR = 1; + float mG = 1; + float mB = 1; + float mA = 1; + + int mVtxSize; + int mFlags; + + public static final int COLOR = 0x0001; + public static final int NORMAL = 0x0002; + public static final int TEXTURE_0 = 0x0100; + + public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) { + mRS = rs; + mVtxCount = 0; + mIndexCount = 0; + mVtxData = new float[128]; + mIndexData = new short[128]; + mVtxSize = vtxSize; + mFlags = flags; + + if (vtxSize < 2 || vtxSize > 3) { + throw new IllegalArgumentException("Vertex size out of range."); + } + } + + private void makeSpace(int count) { + if ((mVtxCount + count) >= mVtxData.length) { + float t[] = new float[mVtxData.length * 2]; + System.arraycopy(mVtxData, 0, t, 0, mVtxData.length); + mVtxData = t; + } + } + + private void latch() { + if ((mFlags & COLOR) != 0) { + makeSpace(4); + mVtxData[mVtxCount++] = mR; + mVtxData[mVtxCount++] = mG; + mVtxData[mVtxCount++] = mB; + mVtxData[mVtxCount++] = mA; + } + if ((mFlags & TEXTURE_0) != 0) { + makeSpace(2); + mVtxData[mVtxCount++] = mS0; + mVtxData[mVtxCount++] = mT0; + } + if ((mFlags & NORMAL) != 0) { + makeSpace(3); + mVtxData[mVtxCount++] = mNX; + mVtxData[mVtxCount++] = mNY; + mVtxData[mVtxCount++] = mNZ; + } + } + + public void addVertex(float x, float y) { + if (mVtxSize != 2) { + throw new IllegalStateException("add mistmatch with declared components."); + } + makeSpace(2); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + latch(); + } + + public void addVertex(float x, float y, float z) { + if (mVtxSize != 3) { + throw new IllegalStateException("add mistmatch with declared components."); + } + makeSpace(3); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + mVtxData[mVtxCount++] = z; + latch(); + } + + public void setTexture(float s, float t) { + if ((mFlags & TEXTURE_0) == 0) { + throw new IllegalStateException("add mistmatch with declared components."); + } + mS0 = s; + mT0 = t; + } + + public void setNormal(float x, float y, float z) { + if ((mFlags & NORMAL) == 0) { + throw new IllegalStateException("add mistmatch with declared components."); + } + mNX = x; + mNY = y; + mNZ = z; + } + + public void setColor(float r, float g, float b, float a) { + if ((mFlags & COLOR) == 0) { + throw new IllegalStateException("add mistmatch with declared components."); + } + mR = r; + mG = g; + mB = b; + mA = a; + } + + public void addTriangle(int idx1, int idx2, int idx3) { + if((idx1 >= mVtxCount) || (idx1 < 0) || + (idx2 >= mVtxCount) || (idx2 < 0) || + (idx3 >= mVtxCount) || (idx3 < 0)) { + throw new IllegalStateException("Index provided greater than vertex count."); + } + if ((mIndexCount + 3) >= mIndexData.length) { + short t[] = new short[mIndexData.length * 2]; + System.arraycopy(mIndexData, 0, t, 0, mIndexData.length); + mIndexData = t; + } + mIndexData[mIndexCount++] = (short)idx1; + mIndexData[mIndexCount++] = (short)idx2; + mIndexData[mIndexCount++] = (short)idx3; + } + + public Mesh create(boolean uploadToBufferObject) { + Element.Builder b = new Element.Builder(mRS); + int floatCount = mVtxSize; + b.add(Element.createVector(mRS, + Element.DataType.FLOAT_32, + mVtxSize), "position"); + if ((mFlags & COLOR) != 0) { + floatCount += 4; + b.add(Element.F32_4(mRS), "color"); + } + if ((mFlags & TEXTURE_0) != 0) { + floatCount += 2; + b.add(Element.F32_2(mRS), "texture0"); + } + if ((mFlags & NORMAL) != 0) { + floatCount += 3; + b.add(Element.F32_3(mRS), "normal"); + } + mElement = b.create(); + + Builder smb = new Builder(mRS); + smb.addVertexType(mElement, mVtxCount / floatCount); + smb.addIndexType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE); + + Mesh sm = smb.create(); + + sm.getVertexAllocation(0).data(mVtxData); + if(uploadToBufferObject) { + sm.getVertexAllocation(0).uploadToBufferObject(); + } + + sm.getIndexAllocation(0).data(mIndexData); + sm.getIndexAllocation(0).uploadToBufferObject(); + + return sm; + } + } +} + diff --git a/graphics/java/android/renderscript/Program.java b/graphics/java/android/renderscript/Program.java index 1614ec5..1628a97 100644 --- a/graphics/java/android/renderscript/Program.java +++ b/graphics/java/android/renderscript/Program.java @@ -38,8 +38,7 @@ public class Program extends BaseObj { String mShader; Program(int id, RenderScript rs) { - super(rs); - mID = id; + super(id, rs); } public void bindConstants(Allocation a, int slot) { @@ -91,8 +90,9 @@ public class Program extends BaseObj { mTextureCount = 0; } - public void setShader(String s) { + public BaseProgramBuilder setShader(String s) { mShader = s; + return this; } public void addInput(Element e) throws IllegalStateException { @@ -120,12 +120,13 @@ public class Program extends BaseObj { return mConstantCount++; } - public void setTextureCount(int count) throws IllegalArgumentException { + public BaseProgramBuilder setTextureCount(int count) throws IllegalArgumentException { // Should check for consistant and non-conflicting names... if(count >= MAX_CONSTANT) { throw new IllegalArgumentException("Max texture count exceeded."); } mTextureCount = count; + return this; } protected void initProgram(Program p) { diff --git a/graphics/java/android/renderscript/ProgramFragment.java b/graphics/java/android/renderscript/ProgramFragment.java index 5e04f0c..04091a3 100644 --- a/graphics/java/android/renderscript/ProgramFragment.java +++ b/graphics/java/android/renderscript/ProgramFragment.java @@ -66,6 +66,7 @@ public class ProgramFragment extends Program { public static final int MAX_TEXTURE = 2; RenderScript mRS; boolean mPointSpriteEnable; + boolean mVaryingColorEnable; public enum EnvMode { REPLACE (1), @@ -106,21 +107,28 @@ public class ProgramFragment extends Program { mPointSpriteEnable = false; } - public void setTexture(EnvMode env, Format fmt, int slot) + public Builder setTexture(EnvMode env, Format fmt, int slot) throws IllegalArgumentException { if((slot < 0) || (slot >= MAX_TEXTURE)) { throw new IllegalArgumentException("MAX_TEXTURE exceeded."); } mSlots[slot] = new Slot(env, fmt); + return this; } - public void setPointSpriteTexCoordinateReplacement(boolean enable) { + public Builder setPointSpriteTexCoordinateReplacement(boolean enable) { mPointSpriteEnable = enable; + return this; + } + + public Builder setVaryingColor(boolean enable) { + mVaryingColorEnable = enable; + return this; } public ProgramFragment create() { mRS.validate(); - int[] tmp = new int[MAX_TEXTURE * 2 + 1]; + int[] tmp = new int[MAX_TEXTURE * 2 + 2]; if (mSlots[0] != null) { tmp[0] = mSlots[0].env.mID; tmp[1] = mSlots[0].format.mID; @@ -130,6 +138,7 @@ public class ProgramFragment extends Program { tmp[3] = mSlots[1].format.mID; } tmp[4] = mPointSpriteEnable ? 1 : 0; + tmp[5] = mVaryingColorEnable ? 1 : 0; int id = mRS.nProgramFragmentCreate(tmp); ProgramFragment pf = new ProgramFragment(id, mRS); pf.mTextureCount = MAX_TEXTURE; diff --git a/graphics/java/android/renderscript/ProgramRaster.java b/graphics/java/android/renderscript/ProgramRaster.java index 56f9bf4..08065cf 100644 --- a/graphics/java/android/renderscript/ProgramRaster.java +++ b/graphics/java/android/renderscript/ProgramRaster.java @@ -26,23 +26,33 @@ import android.util.Log; * **/ public class ProgramRaster extends BaseObj { + + public enum CullMode { + BACK (0), + FRONT (1), + NONE (2); + + int mID; + CullMode(int id) { + mID = id; + } + } + boolean mPointSmooth; boolean mLineSmooth; boolean mPointSprite; - float mPointSize; float mLineWidth; - Element mIn; - Element mOut; + CullMode mCullMode; ProgramRaster(int id, RenderScript rs) { - super(rs); - mID = id; + super(id, rs); - mPointSize = 1.0f; mLineWidth = 1.0f; mPointSmooth = false; mLineSmooth = false; mPointSprite = false; + + mCullMode = CullMode.BACK; } public void setLineWidth(float w) { @@ -51,51 +61,43 @@ public class ProgramRaster extends BaseObj { mRS.nProgramRasterSetLineWidth(mID, w); } - public void setPointSize(float s) { + public void setCullMode(CullMode m) { mRS.validate(); - mPointSize = s; - mRS.nProgramRasterSetPointSize(mID, s); + mCullMode = m; + mRS.nProgramRasterSetCullMode(mID, m.mID); } - void internalInit() { - int inID = 0; - int outID = 0; - if (mIn != null) { - inID = mIn.mID; - } - if (mOut != null) { - outID = mOut.mID; - } - mID = mRS.nProgramRasterCreate(inID, outID, mPointSmooth, mLineSmooth, mPointSprite); - } - - public static class Builder { RenderScript mRS; - ProgramRaster mPR; + boolean mPointSprite; + boolean mPointSmooth; + boolean mLineSmooth; - public Builder(RenderScript rs, Element in, Element out) { + public Builder(RenderScript rs) { mRS = rs; - mPR = new ProgramRaster(0, rs); + mPointSmooth = false; + mLineSmooth = false; + mPointSprite = false; } - public void setPointSpriteEnable(boolean enable) { - mPR.mPointSprite = enable; + public Builder setPointSpriteEnable(boolean enable) { + mPointSprite = enable; + return this; } - public void setPointSmoothEnable(boolean enable) { - mPR.mPointSmooth = enable; + public Builder setPointSmoothEnable(boolean enable) { + mPointSmooth = enable; + return this; } - public void setLineSmoothEnable(boolean enable) { - mPR.mLineSmooth = enable; + public Builder setLineSmoothEnable(boolean enable) { + mLineSmooth = enable; + return this; } - static synchronized ProgramRaster internalCreate(RenderScript rs, Builder b) { - b.mPR.internalInit(); - ProgramRaster pr = b.mPR; - b.mPR = new ProgramRaster(0, b.mRS); + int id = rs.nProgramRasterCreate(b.mPointSmooth, b.mLineSmooth, b.mPointSprite); + ProgramRaster pr = new ProgramRaster(id, rs); return pr; } @@ -111,3 +113,4 @@ public class ProgramRaster extends BaseObj { + diff --git a/graphics/java/android/renderscript/ProgramStore.java b/graphics/java/android/renderscript/ProgramStore.java index 69be245..e249842 100644 --- a/graphics/java/android/renderscript/ProgramStore.java +++ b/graphics/java/android/renderscript/ProgramStore.java @@ -76,11 +76,143 @@ public class ProgramStore extends BaseObj { ProgramStore(int id, RenderScript rs) { - super(rs); - mID = id; + super(id, rs); } + public static ProgramStore BlendNone_DepthTest(RenderScript rs) { + if(rs.mProgramStore_BlendNone_DepthTest == null) { + ProgramStore.Builder builder = new ProgramStore.Builder(rs); + builder.setDepthFunc(ProgramStore.DepthFunc.LESS); + builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO); + builder.setDitherEnable(false); + builder.setDepthMask(true); + rs.mProgramStore_BlendNone_DepthTest = builder.create(); + } + return rs.mProgramStore_BlendNone_DepthTest; + } + public static ProgramStore BlendNone_DepthNoDepth(RenderScript rs) { + if(rs.mProgramStore_BlendNone_DepthNoDepth == null) { + ProgramStore.Builder builder = new ProgramStore.Builder(rs); + builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); + builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO); + builder.setDitherEnable(false); + builder.setDepthMask(false); + rs.mProgramStore_BlendNone_DepthNoDepth = builder.create(); + } + return rs.mProgramStore_BlendNone_DepthNoDepth; + } + public static ProgramStore BlendNone_DepthNoTest(RenderScript rs) { + if(rs.mProgramStore_BlendNone_DepthNoTest == null) { + ProgramStore.Builder builder = new ProgramStore.Builder(rs); + builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); + builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO); + builder.setDitherEnable(false); + builder.setDepthMask(true); + rs.mProgramStore_BlendNone_DepthNoTest = builder.create(); + } + return rs.mProgramStore_BlendNone_DepthNoTest; + } + public static ProgramStore BlendNone_DepthNoWrite(RenderScript rs) { + if(rs.mProgramStore_BlendNone_DepthNoWrite == null) { + ProgramStore.Builder builder = new ProgramStore.Builder(rs); + builder.setDepthFunc(ProgramStore.DepthFunc.LESS); + builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO); + builder.setDitherEnable(false); + builder.setDepthMask(false); + rs.mProgramStore_BlendNone_DepthNoWrite = builder.create(); + } + return rs.mProgramStore_BlendNone_DepthNoWrite; + } + + public static ProgramStore BlendAlpha_DepthTest(RenderScript rs) { + if(rs.mProgramStore_BlendAlpha_DepthTest == null) { + ProgramStore.Builder builder = new ProgramStore.Builder(rs); + builder.setDepthFunc(ProgramStore.DepthFunc.LESS); + builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA); + builder.setDitherEnable(false); + builder.setDepthMask(true); + rs.mProgramStore_BlendAlpha_DepthTest = builder.create(); + } + return rs.mProgramStore_BlendAlpha_DepthTest; + } + public static ProgramStore BlendAlpha_DepthNoDepth(RenderScript rs) { + if(rs.mProgramStore_BlendAlpha_DepthNoDepth == null) { + ProgramStore.Builder builder = new ProgramStore.Builder(rs); + builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); + builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA); + builder.setDitherEnable(false); + builder.setDepthMask(false); + rs.mProgramStore_BlendAlpha_DepthNoDepth = builder.create(); + } + return rs.mProgramStore_BlendAlpha_DepthNoDepth; + } + public static ProgramStore BlendAlpha_DepthNoTest(RenderScript rs) { + if(rs.mProgramStore_BlendAlpha_DepthNoTest == null) { + ProgramStore.Builder builder = new ProgramStore.Builder(rs); + builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); + builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA); + builder.setDitherEnable(false); + builder.setDepthMask(true); + rs.mProgramStore_BlendAlpha_DepthNoTest = builder.create(); + } + return rs.mProgramStore_BlendAlpha_DepthNoTest; + } + public static ProgramStore BlendAlpha_DepthNoWrite(RenderScript rs) { + if(rs.mProgramStore_BlendAlpha_DepthNoWrite == null) { + ProgramStore.Builder builder = new ProgramStore.Builder(rs); + builder.setDepthFunc(ProgramStore.DepthFunc.LESS); + builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA); + builder.setDitherEnable(false); + builder.setDepthMask(false); + rs.mProgramStore_BlendAlpha_DepthNoWrite = builder.create(); + } + return rs.mProgramStore_BlendAlpha_DepthNoWrite; + } + public static ProgramStore BlendAdd_DepthTest(RenderScript rs) { + if(rs.mProgramStore_BlendAdd_DepthTest == null) { + ProgramStore.Builder builder = new ProgramStore.Builder(rs); + builder.setDepthFunc(ProgramStore.DepthFunc.LESS); + builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE); + builder.setDitherEnable(false); + builder.setDepthMask(true); + rs.mProgramStore_BlendAdd_DepthTest = builder.create(); + } + return rs.mProgramStore_BlendAdd_DepthTest; + } + public static ProgramStore BlendAdd_DepthNoDepth(RenderScript rs) { + if(rs.mProgramStore_BlendAdd_DepthNoDepth == null) { + ProgramStore.Builder builder = new ProgramStore.Builder(rs); + builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); + builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE); + builder.setDitherEnable(false); + builder.setDepthMask(false); + rs.mProgramStore_BlendAdd_DepthNoDepth = builder.create(); + } + return rs.mProgramStore_BlendAdd_DepthNoDepth; + } + public static ProgramStore BlendAdd_DepthNoTest(RenderScript rs) { + if(rs.mProgramStore_BlendAdd_DepthNoTest == null) { + ProgramStore.Builder builder = new ProgramStore.Builder(rs); + builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); + builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE); + builder.setDitherEnable(false); + builder.setDepthMask(true); + rs.mProgramStore_BlendAdd_DepthNoDepth = builder.create(); + } + return rs.mProgramStore_BlendAdd_DepthNoTest; + } + public static ProgramStore BlendAdd_DepthNoWrite(RenderScript rs) { + if(rs.mProgramStore_BlendAdd_DepthNoWrite == null) { + ProgramStore.Builder builder = new ProgramStore.Builder(rs); + builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); + builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE); + builder.setDitherEnable(false); + builder.setDepthMask(false); + rs.mProgramStore_BlendAdd_DepthNoWrite = builder.create(); + } + return rs.mProgramStore_BlendAdd_DepthNoWrite; + } public static class Builder { RenderScript mRS; @@ -110,32 +242,49 @@ public class ProgramStore extends BaseObj { mColorMaskA = true; mBlendSrc = BlendSrcFunc.ONE; mBlendDst = BlendDstFunc.ZERO; + } - + public Builder(RenderScript rs) { + mRS = rs; + mIn = null; + mOut = null; + mDepthFunc = DepthFunc.ALWAYS; + mDepthMask = false; + mColorMaskR = true; + mColorMaskG = true; + mColorMaskB = true; + mColorMaskA = true; + mBlendSrc = BlendSrcFunc.ONE; + mBlendDst = BlendDstFunc.ZERO; } - public void setDepthFunc(DepthFunc func) { + public Builder setDepthFunc(DepthFunc func) { mDepthFunc = func; + return this; } - public void setDepthMask(boolean enable) { + public Builder setDepthMask(boolean enable) { mDepthMask = enable; + return this; } - public void setColorMask(boolean r, boolean g, boolean b, boolean a) { + public Builder setColorMask(boolean r, boolean g, boolean b, boolean a) { mColorMaskR = r; mColorMaskG = g; mColorMaskB = b; mColorMaskA = a; + return this; } - public void setBlendFunc(BlendSrcFunc src, BlendDstFunc dst) { + public Builder setBlendFunc(BlendSrcFunc src, BlendDstFunc dst) { mBlendSrc = src; mBlendDst = dst; + return this; } - public void setDitherEnable(boolean enable) { + public Builder setDitherEnable(boolean enable) { mDither = enable; + return this; } static synchronized ProgramStore internalCreate(RenderScript rs, Builder b) { @@ -147,17 +296,17 @@ public class ProgramStore extends BaseObj { if (b.mOut != null) { outID = b.mOut.mID; } - rs.nProgramFragmentStoreBegin(inID, outID); - rs.nProgramFragmentStoreDepthFunc(b.mDepthFunc.mID); - rs.nProgramFragmentStoreDepthMask(b.mDepthMask); - rs.nProgramFragmentStoreColorMask(b.mColorMaskR, + rs.nProgramStoreBegin(inID, outID); + rs.nProgramStoreDepthFunc(b.mDepthFunc.mID); + rs.nProgramStoreDepthMask(b.mDepthMask); + rs.nProgramStoreColorMask(b.mColorMaskR, b.mColorMaskG, b.mColorMaskB, b.mColorMaskA); - rs.nProgramFragmentStoreBlendFunc(b.mBlendSrc.mID, b.mBlendDst.mID); - rs.nProgramFragmentStoreDither(b.mDither); + rs.nProgramStoreBlendFunc(b.mBlendSrc.mID, b.mBlendDst.mID); + rs.nProgramStoreDither(b.mDither); - int id = rs.nProgramFragmentStoreCreate(); + int id = rs.nProgramStoreCreate(); return new ProgramStore(id, rs); } diff --git a/graphics/java/android/renderscript/ProgramVertex.java b/graphics/java/android/renderscript/ProgramVertex.java index 1b155d7..c99efd6 100644 --- a/graphics/java/android/renderscript/ProgramVertex.java +++ b/graphics/java/android/renderscript/ProgramVertex.java @@ -46,9 +46,13 @@ public class ProgramVertex extends Program { public Builder(RenderScript rs, Element in, Element out) { mRS = rs; } + public Builder(RenderScript rs) { + mRS = rs; + } - public void setTextureMatrixEnable(boolean enable) { + public Builder setTextureMatrixEnable(boolean enable) { mTextureMatrixEnable = enable; + return this; } public ProgramVertex create() { diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java index a935243..37c01f5 100644 --- a/graphics/java/android/renderscript/RenderScript.java +++ b/graphics/java/android/renderscript/RenderScript.java @@ -57,151 +57,478 @@ public class RenderScript { } } + // Non-threadsafe functions. native void nInitElements(int a8, int rgba4444, int rgba8888, int rgb565); - native int nDeviceCreate(); native void nDeviceDestroy(int dev); native void nDeviceSetConfig(int dev, int param, int value); - native int nContextCreateGL(int dev, int ver, boolean useDepth); - native int nContextCreate(int dev, int ver); - native void nContextDestroy(int con); - native void nContextSetSurface(int w, int h, Surface sur); - native void nContextSetPriority(int p); - native void nContextDump(int bits); - - native void nContextBindRootScript(int script); - native void nContextBindSampler(int sampler, int slot); - native void nContextBindProgramFragmentStore(int pfs); - native void nContextBindProgramFragment(int pf); - native void nContextBindProgramVertex(int pf); - native void nContextBindProgramRaster(int pr); - native void nContextPause(); - native void nContextResume(); - native int nContextGetMessage(int[] data, boolean wait); - native void nContextInitToClient(); - native void nContextDeinitToClient(); - - native void nAssignName(int obj, byte[] name); - native void nObjDestroy(int id); - native void nObjDestroyOOB(int id); - native int nFileOpen(byte[] name); - - - native int nElementCreate(int type, int kind, boolean norm, int vecSize); - native int nElementCreate2(int[] elements, 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 int nAllocationCreateTyped(int type); - native int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp); - native int nAllocationCreateBitmapRef(int type, Bitmap bmp); - native int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp); - native int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream); - - native void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel); - native void nAllocationUploadToBufferObject(int alloc); - - native void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes); - native void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes); - native void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes); - native void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes); - - native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes); - native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes); - native void nAllocationRead(int id, int[] d); - 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 void nAdapter1DBindAllocation(int ad, int alloc); - native void nAdapter1DSetConstraint(int ad, int dim, int value); - native void nAdapter1DData(int ad, int[] d); - native void nAdapter1DData(int ad, float[] d); - native void nAdapter1DSubData(int ad, int off, int count, int[] d); - native void nAdapter1DSubData(int ad, int off, int count, float[] d); - native int nAdapter1DCreate(); - - native void nAdapter2DBindAllocation(int ad, int alloc); - native void nAdapter2DSetConstraint(int ad, int dim, int value); - native void nAdapter2DData(int ad, int[] d); - native void nAdapter2DData(int ad, float[] d); - native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d); - native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d); - native int nAdapter2DCreate(); - - native void nScriptBindAllocation(int script, int alloc, int slot); - native void nScriptSetClearColor(int script, float r, float g, float b, float a); - native void nScriptSetClearDepth(int script, float depth); - native void nScriptSetClearStencil(int script, int stencil); - native void nScriptSetTimeZone(int script, byte[] timeZone); - native void nScriptSetType(int type, boolean writable, String name, int slot); - native void nScriptSetRoot(boolean isRoot); - native void nScriptSetInvokable(String name, int slot); - native void nScriptInvoke(int id, int slot); - - native void nScriptCBegin(); - native void nScriptCSetScript(byte[] script, int offset, int length); - native int nScriptCCreate(); - native void nScriptCAddDefineI32(String name, int value); - native void nScriptCAddDefineF(String name, float value); - - native void nSamplerBegin(); - native void nSamplerSet(int param, int value); - native int nSamplerCreate(); - - native void nProgramFragmentStoreBegin(int in, int out); - native void nProgramFragmentStoreDepthFunc(int func); - native void nProgramFragmentStoreDepthMask(boolean enable); - native void nProgramFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a); - native void nProgramFragmentStoreBlendFunc(int src, int dst); - native void nProgramFragmentStoreDither(boolean enable); - native int nProgramFragmentStoreCreate(); - - native int nProgramRasterCreate(int in, int out, boolean pointSmooth, boolean lineSmooth, boolean pointSprite); - native void nProgramRasterSetLineWidth(int pr, float v); - native void nProgramRasterSetPointSize(int pr, float v); - - native void nProgramBindConstants(int pv, int slot, int mID); - native void nProgramBindTexture(int vpf, int slot, int a); - native void nProgramBindSampler(int vpf, int slot, int s); - - native int nProgramFragmentCreate(int[] params); - native int nProgramFragmentCreate2(String shader, int[] params); - - native int nProgramVertexCreate(boolean texMat); - native int nProgramVertexCreate2(String shader, int[] params); - - native void nLightBegin(); - native void nLightSetIsMono(boolean isMono); - native void nLightSetIsLocal(boolean isLocal); - native int nLightCreate(); - native void nLightSetColor(int l, float r, float g, float b); - native void nLightSetPosition(int l, float x, float y, float z); - - native int nSimpleMeshCreate(int batchID, int idxID, int[] vtxID, int prim); - native void nSimpleMeshBindVertex(int id, int alloc, int slot); - native void nSimpleMeshBindIndex(int id, int alloc); - - native void nAnimationBegin(int attribCount, int keyframeCount); - native void nAnimationAdd(float time, float[] attribs); - native int nAnimationCreate(); + native int nContextGetMessage(int con, int[] data, boolean wait); + native void nContextInitToClient(int con); + native void nContextDeinitToClient(int con); + + + // Methods below are wrapped to protect the non-threadsafe + // lockless fifo. + native int rsnContextCreateGL(int dev, int ver, boolean useDepth); + synchronized int nContextCreateGL(int dev, int ver, boolean useDepth) { + return rsnContextCreateGL(dev, ver, useDepth); + } + native int rsnContextCreate(int dev, int ver); + synchronized int nContextCreate(int dev, int ver) { + return rsnContextCreate(dev, ver); + } + native void rsnContextDestroy(int con); + synchronized void nContextDestroy() { + rsnContextDestroy(mContext); + } + native void rsnContextSetSurface(int con, int w, int h, Surface sur); + synchronized void nContextSetSurface(int w, int h, Surface sur) { + rsnContextSetSurface(mContext, w, h, sur); + } + native void rsnContextSetPriority(int con, int p); + synchronized void nContextSetPriority(int p) { + rsnContextSetPriority(mContext, p); + } + native void rsnContextDump(int con, int bits); + synchronized void nContextDump(int bits) { + rsnContextDump(mContext, bits); + } + native void rsnContextFinish(int con); + synchronized void nContextFinish() { + rsnContextFinish(mContext); + } + + native void rsnContextBindRootScript(int con, int script); + synchronized void nContextBindRootScript(int script) { + rsnContextBindRootScript(mContext, script); + } + native void rsnContextBindSampler(int con, int sampler, int slot); + synchronized void nContextBindSampler(int sampler, int slot) { + rsnContextBindSampler(mContext, sampler, slot); + } + native void rsnContextBindProgramStore(int con, int pfs); + synchronized void nContextBindProgramStore(int pfs) { + rsnContextBindProgramStore(mContext, pfs); + } + native void rsnContextBindProgramFragment(int con, int pf); + synchronized void nContextBindProgramFragment(int pf) { + rsnContextBindProgramFragment(mContext, pf); + } + native void rsnContextBindProgramVertex(int con, int pv); + synchronized void nContextBindProgramVertex(int pv) { + rsnContextBindProgramVertex(mContext, pv); + } + native void rsnContextBindProgramRaster(int con, int pr); + synchronized void nContextBindProgramRaster(int pr) { + rsnContextBindProgramRaster(mContext, pr); + } + native void rsnContextPause(int con); + synchronized void nContextPause() { + rsnContextPause(mContext); + } + native void rsnContextResume(int con); + synchronized void nContextResume() { + rsnContextResume(mContext); + } + + native void rsnAssignName(int con, int obj, byte[] name); + synchronized void nAssignName(int obj, byte[] name) { + rsnAssignName(mContext, obj, name); + } + native String rsnGetName(int con, int obj); + synchronized String nGetName(int obj) { + return rsnGetName(mContext, obj); + } + native void rsnObjDestroy(int con, int id); + synchronized void nObjDestroy(int id) { + rsnObjDestroy(mContext, id); + } + native int rsnFileOpen(int con, byte[] name); + synchronized int nFileOpen(byte[] name) { + return rsnFileOpen(mContext, name); + } + + native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize); + synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) { + return rsnElementCreate(mContext, type, kind, norm, vecSize); + } + native int rsnElementCreate2(int con, int[] elements, String[] names); + synchronized int nElementCreate2(int[] elements, String[] names) { + return rsnElementCreate2(mContext, elements, names); + } + native void rsnElementGetNativeData(int con, int id, int[] elementData); + synchronized void nElementGetNativeData(int id, int[] elementData) { + rsnElementGetNativeData(mContext, id, elementData); + } + native void rsnElementGetSubElements(int con, int id, int[] IDs, String[] names); + synchronized void nElementGetSubElements(int id, int[] IDs, String[] names) { + rsnElementGetSubElements(mContext, id, IDs, names); + } + + native void rsnTypeBegin(int con, int elementID); + synchronized void nTypeBegin(int elementID) { + rsnTypeBegin(mContext, elementID); + } + native void rsnTypeAdd(int con, int dim, int val); + synchronized void nTypeAdd(int dim, int val) { + rsnTypeAdd(mContext, dim, val); + } + native int rsnTypeCreate(int con); + synchronized int nTypeCreate() { + return rsnTypeCreate(mContext); + } + native void rsnTypeFinalDestroy(int con, Type t); + synchronized void nTypeFinalDestroy(Type t) { + rsnTypeFinalDestroy(mContext, t); + } + native void rsnTypeSetupFields(int con, Type t, int[] types, int[] bits, Field[] IDs); + synchronized void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs) { + rsnTypeSetupFields(mContext, t, types, bits, IDs); + } + native void rsnTypeGetNativeData(int con, int id, int[] typeData); + synchronized void nTypeGetNativeData(int id, int[] typeData) { + rsnTypeGetNativeData(mContext, id, typeData); + } + + native int rsnAllocationCreateTyped(int con, int type); + synchronized int nAllocationCreateTyped(int type) { + return rsnAllocationCreateTyped(mContext, type); + } + native int rsnAllocationCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp); + synchronized int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) { + return rsnAllocationCreateFromBitmap(mContext, dstFmt, genMips, bmp); + } + native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp); + synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) { + return rsnAllocationCreateBitmapRef(mContext, type, bmp); + } + native int rsnAllocationCreateFromBitmapBoxed(int con, int dstFmt, boolean genMips, Bitmap bmp); + synchronized int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp) { + return rsnAllocationCreateFromBitmapBoxed(mContext, dstFmt, genMips, bmp); + } + native int rsnAllocationCreateFromAssetStream(int con, int dstFmt, boolean genMips, int assetStream); + synchronized int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream) { + return rsnAllocationCreateFromAssetStream(mContext, dstFmt, genMips, assetStream); + } + + native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel); + synchronized void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel) { + rsnAllocationUploadToTexture(mContext, alloc, genMips, baseMioLevel); + } + native void rsnAllocationUploadToBufferObject(int con, int alloc); + synchronized void nAllocationUploadToBufferObject(int alloc) { + rsnAllocationUploadToBufferObject(mContext, alloc); + } + + native void rsnAllocationSubData1D(int con, int id, int off, int count, int[] d, int sizeBytes); + synchronized void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes) { + rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes); + } + native void rsnAllocationSubData1D(int con, int id, int off, int count, short[] d, int sizeBytes); + synchronized void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes) { + rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes); + } + native void rsnAllocationSubData1D(int con, int id, int off, int count, byte[] d, int sizeBytes); + synchronized void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes) { + rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes); + } + native void rsnAllocationSubData1D(int con, int id, int off, int count, float[] d, int sizeBytes); + synchronized void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes) { + rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes); + } + + native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes); + synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes) { + rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes); + } + native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes); + synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes) { + rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes); + } + native void rsnAllocationRead(int con, int id, int[] d); + synchronized void nAllocationRead(int id, int[] d) { + rsnAllocationRead(mContext, id, d); + } + native void rsnAllocationRead(int con, int id, float[] d); + synchronized void nAllocationRead(int id, float[] d) { + rsnAllocationRead(mContext, id, d); + } + native void rsnAllocationSubDataFromObject(int con, int id, Type t, int offset, Object o); + synchronized void nAllocationSubDataFromObject(int id, Type t, int offset, Object o) { + rsnAllocationSubDataFromObject(mContext, id, t, offset, o); + } + native void rsnAllocationSubReadFromObject(int con, int id, Type t, int offset, Object o); + synchronized void nAllocationSubReadFromObject(int id, Type t, int offset, Object o) { + rsnAllocationSubReadFromObject(mContext, id, t, offset, o); + } + native int rsnAllocationGetType(int con, int id); + synchronized int nAllocationGetType(int id) { + return rsnAllocationGetType(mContext, id); + } + + native int rsnFileA3DCreateFromAssetStream(int con, int assetStream); + synchronized int nFileA3DCreateFromAssetStream(int assetStream) { + return rsnFileA3DCreateFromAssetStream(mContext, assetStream); + } + native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D); + synchronized int nFileA3DGetNumIndexEntries(int fileA3D) { + return rsnFileA3DGetNumIndexEntries(mContext, fileA3D); + } + native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names); + synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) { + rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names); + } + native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index); + synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) { + return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index); + } + + native int rsnFontCreateFromFile(int con, String fileName, int size, int dpi); + synchronized int nFontCreateFromFile(String fileName, int size, int dpi) { + return rsnFontCreateFromFile(mContext, fileName, size, dpi); + } + + native void rsnAdapter1DBindAllocation(int con, int ad, int alloc); + synchronized void nAdapter1DBindAllocation(int ad, int alloc) { + rsnAdapter1DBindAllocation(mContext, ad, alloc); + } + native void rsnAdapter1DSetConstraint(int con, int ad, int dim, int value); + synchronized void nAdapter1DSetConstraint(int ad, int dim, int value) { + rsnAdapter1DSetConstraint(mContext, ad, dim, value); + } + native void rsnAdapter1DData(int con, int ad, int[] d); + synchronized void nAdapter1DData(int ad, int[] d) { + rsnAdapter1DData(mContext, ad, d); + } + native void rsnAdapter1DData(int con, int ad, float[] d); + synchronized void nAdapter1DData(int ad, float[] d) { + rsnAdapter1DData(mContext, ad, d); + } + native void rsnAdapter1DSubData(int con, int ad, int off, int count, int[] d); + synchronized void nAdapter1DSubData(int ad, int off, int count, int[] d) { + rsnAdapter1DSubData(mContext, ad, off, count, d); + } + native void rsnAdapter1DSubData(int con, int ad, int off, int count, float[] d); + synchronized void nAdapter1DSubData(int ad, int off, int count, float[] d) { + rsnAdapter1DSubData(mContext, ad, off, count, d); + } + native int rsnAdapter1DCreate(int con); + synchronized int nAdapter1DCreate() { + return rsnAdapter1DCreate(mContext); + } + + native void rsnAdapter2DBindAllocation(int con, int ad, int alloc); + synchronized void nAdapter2DBindAllocation(int ad, int alloc) { + rsnAdapter2DBindAllocation(mContext, ad, alloc); + } + native void rsnAdapter2DSetConstraint(int con, int ad, int dim, int value); + synchronized void nAdapter2DSetConstraint(int ad, int dim, int value) { + rsnAdapter2DSetConstraint(mContext, ad, dim, value); + } + native void rsnAdapter2DData(int con, int ad, int[] d); + synchronized void nAdapter2DData(int ad, int[] d) { + rsnAdapter2DData(mContext, ad, d); + } + native void rsnAdapter2DData(int con, int ad, float[] d); + synchronized void nAdapter2DData(int ad, float[] d) { + rsnAdapter2DData(mContext, ad, d); + } + native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, int[] d); + synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d) { + rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d); + } + native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, float[] d); + synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d) { + rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d); + } + native int rsnAdapter2DCreate(int con); + synchronized int nAdapter2DCreate() { + return rsnAdapter2DCreate(mContext); + } + + native void rsnScriptBindAllocation(int con, int script, int alloc, int slot); + synchronized void nScriptBindAllocation(int script, int alloc, int slot) { + rsnScriptBindAllocation(mContext, script, alloc, slot); + } + native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone); + synchronized void nScriptSetTimeZone(int script, byte[] timeZone) { + rsnScriptSetTimeZone(mContext, script, timeZone); + } + native void rsnScriptInvoke(int con, int id, int slot); + synchronized void nScriptInvoke(int id, int slot) { + rsnScriptInvoke(mContext, id, slot); + } + native void rsnScriptInvokeV(int con, int id, int slot, byte[] params); + synchronized void nScriptInvokeV(int id, int slot, byte[] params) { + rsnScriptInvokeV(mContext, id, slot, params); + } + native void rsnScriptSetVarI(int con, int id, int slot, int val); + synchronized void nScriptSetVarI(int id, int slot, int val) { + rsnScriptSetVarI(mContext, id, slot, val); + } + native void rsnScriptSetVarF(int con, int id, int slot, float val); + synchronized void nScriptSetVarF(int id, int slot, float val) { + rsnScriptSetVarF(mContext, id, slot, val); + } + native void rsnScriptSetVarV(int con, int id, int slot, byte[] val); + synchronized void nScriptSetVarV(int id, int slot, byte[] val) { + rsnScriptSetVarV(mContext, id, slot, val); + } + + native void rsnScriptCBegin(int con); + synchronized void nScriptCBegin() { + rsnScriptCBegin(mContext); + } + native void rsnScriptCSetScript(int con, byte[] script, int offset, int length); + synchronized void nScriptCSetScript(byte[] script, int offset, int length) { + rsnScriptCSetScript(mContext, script, offset, length); + } + native int rsnScriptCCreate(int con); + synchronized int nScriptCCreate() { + return rsnScriptCCreate(mContext); + } + + native void rsnSamplerBegin(int con); + synchronized void nSamplerBegin() { + rsnSamplerBegin(mContext); + } + native void rsnSamplerSet(int con, int param, int value); + synchronized void nSamplerSet(int param, int value) { + rsnSamplerSet(mContext, param, value); + } + native int rsnSamplerCreate(int con); + synchronized int nSamplerCreate() { + return rsnSamplerCreate(mContext); + } + + native void rsnProgramStoreBegin(int con, int in, int out); + synchronized void nProgramStoreBegin(int in, int out) { + rsnProgramStoreBegin(mContext, in, out); + } + native void rsnProgramStoreDepthFunc(int con, int func); + synchronized void nProgramStoreDepthFunc(int func) { + rsnProgramStoreDepthFunc(mContext, func); + } + native void rsnProgramStoreDepthMask(int con, boolean enable); + synchronized void nProgramStoreDepthMask(boolean enable) { + rsnProgramStoreDepthMask(mContext, enable); + } + native void rsnProgramStoreColorMask(int con, boolean r, boolean g, boolean b, boolean a); + synchronized void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a) { + rsnProgramStoreColorMask(mContext, r, g, b, a); + } + native void rsnProgramStoreBlendFunc(int con, int src, int dst); + synchronized void nProgramStoreBlendFunc(int src, int dst) { + rsnProgramStoreBlendFunc(mContext, src, dst); + } + native void rsnProgramStoreDither(int con, boolean enable); + synchronized void nProgramStoreDither(boolean enable) { + rsnProgramStoreDither(mContext, enable); + } + native int rsnProgramStoreCreate(int con); + synchronized int nProgramStoreCreate() { + return rsnProgramStoreCreate(mContext); + } + + native int rsnProgramRasterCreate(int con, boolean pointSmooth, boolean lineSmooth, boolean pointSprite); + synchronized int nProgramRasterCreate(boolean pointSmooth, boolean lineSmooth, boolean pointSprite) { + return rsnProgramRasterCreate(mContext, pointSmooth, lineSmooth, pointSprite); + } + native void rsnProgramRasterSetLineWidth(int con, int pr, float v); + synchronized void nProgramRasterSetLineWidth(int pr, float v) { + rsnProgramRasterSetLineWidth(mContext, pr, v); + } + native void rsnProgramRasterSetCullMode(int con, int pr, int mode); + synchronized void nProgramRasterSetCullMode(int pr, int mode) { + rsnProgramRasterSetCullMode(mContext, pr, mode); + } + + native void rsnProgramBindConstants(int con, int pv, int slot, int mID); + synchronized void nProgramBindConstants(int pv, int slot, int mID) { + rsnProgramBindConstants(mContext, pv, slot, mID); + } + native void rsnProgramBindTexture(int con, int vpf, int slot, int a); + synchronized void nProgramBindTexture(int vpf, int slot, int a) { + rsnProgramBindTexture(mContext, vpf, slot, a); + } + native void rsnProgramBindSampler(int con, int vpf, int slot, int s); + synchronized void nProgramBindSampler(int vpf, int slot, int s) { + rsnProgramBindSampler(mContext, vpf, slot, s); + } + + native int rsnProgramFragmentCreate(int con, int[] params); + synchronized int nProgramFragmentCreate(int[] params) { + return rsnProgramFragmentCreate(mContext, params); + } + native int rsnProgramFragmentCreate2(int con, String shader, int[] params); + synchronized int nProgramFragmentCreate2(String shader, int[] params) { + return rsnProgramFragmentCreate2(mContext, shader, params); + } + + native int rsnProgramVertexCreate(int con, boolean texMat); + synchronized int nProgramVertexCreate(boolean texMat) { + return rsnProgramVertexCreate(mContext, texMat); + } + native int rsnProgramVertexCreate2(int con, String shader, int[] params); + synchronized int nProgramVertexCreate2(String shader, int[] params) { + return rsnProgramVertexCreate2(mContext, shader, params); + } + + + native int rsnMeshCreate(int con, int vtxCount, int indexCount); + synchronized int nMeshCreate(int vtxCount, int indexCount) { + return rsnMeshCreate(mContext, vtxCount, indexCount); + } + native void rsnMeshBindVertex(int con, int id, int alloc, int slot); + synchronized void nMeshBindVertex(int id, int alloc, int slot) { + rsnMeshBindVertex(mContext, id, alloc, slot); + } + native void rsnMeshBindIndex(int con, int id, int alloc, int prim, int slot); + synchronized void nMeshBindIndex(int id, int alloc, int prim, int slot) { + rsnMeshBindIndex(mContext, id, alloc, prim, slot); + } + native int rsnMeshGetVertexBufferCount(int con, int id); + synchronized int nMeshGetVertexBufferCount(int id) { + return rsnMeshGetVertexBufferCount(mContext, id); + } + native int rsnMeshGetIndexCount(int con, int id); + synchronized int nMeshGetIndexCount(int id) { + return rsnMeshGetIndexCount(mContext, id); + } + native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount); + synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) { + rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount); + } + native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount); + synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) { + rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount); + } + protected int mDev; protected int mContext; @SuppressWarnings({"FieldCanBeLocal"}) protected MessageThread mMessageThread; - Element mElement_USER_U8; - Element mElement_USER_I8; - Element mElement_USER_U16; - Element mElement_USER_I16; - Element mElement_USER_U32; - Element mElement_USER_I32; - Element mElement_USER_F32; + Element mElement_U8; + Element mElement_I8; + Element mElement_U16; + Element mElement_I16; + Element mElement_U32; + Element mElement_I32; + Element mElement_F32; + Element mElement_BOOLEAN; + + Element mElement_ELEMENT; + Element mElement_TYPE; + Element mElement_ALLOCATION; + Element mElement_SAMPLER; + Element mElement_SCRIPT; + Element mElement_MESH; + Element mElement_PROGRAM_FRAGMENT; + Element mElement_PROGRAM_VERTEX; + Element mElement_PROGRAM_RASTER; + Element mElement_PROGRAM_STORE; Element mElement_A_8; Element mElement_RGB_565; @@ -210,13 +537,31 @@ public class RenderScript { Element mElement_RGBA_4444; Element mElement_RGBA_8888; - Element mElement_INDEX_16; - Element mElement_POSITION_2; - Element mElement_POSITION_3; - Element mElement_TEXTURE_2; - Element mElement_NORMAL_3; - Element mElement_COLOR_U8_4; - Element mElement_COLOR_F32_4; + Element mElement_FLOAT_2; + Element mElement_FLOAT_3; + Element mElement_FLOAT_4; + Element mElement_UCHAR_4; + + Sampler mSampler_CLAMP_NEAREST; + Sampler mSampler_CLAMP_LINEAR; + Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR; + Sampler mSampler_WRAP_NEAREST; + Sampler mSampler_WRAP_LINEAR; + Sampler mSampler_WRAP_LINEAR_MIP_LINEAR; + + ProgramStore mProgramStore_BlendNone_DepthTest; + ProgramStore mProgramStore_BlendNone_DepthNoDepth; + ProgramStore mProgramStore_BlendNone_DepthNoTest; + ProgramStore mProgramStore_BlendNone_DepthNoWrite; + ProgramStore mProgramStore_BlendAlpha_DepthTest; + ProgramStore mProgramStore_BlendAlpha_DepthNoDepth; + ProgramStore mProgramStore_BlendAlpha_DepthNoTest; + ProgramStore mProgramStore_BlendAlpha_DepthNoWrite; + ProgramStore mProgramStore_BlendAdd_DepthTest; + ProgramStore mProgramStore_BlendAdd_DepthNoDepth; + ProgramStore mProgramStore_BlendAdd_DepthNoTest; + ProgramStore mProgramStore_BlendAdd_DepthNoWrite; + /////////////////////////////////////////////////////////////////////////////////// // @@ -264,9 +609,9 @@ public class RenderScript { // This function is a temporary solution. The final solution will // used typed allocations where the message id is the type indicator. int[] rbuf = new int[16]; - mRS.nContextInitToClient(); + mRS.nContextInitToClient(mRS.mContext); while(mRun) { - int msg = mRS.nContextGetMessage(rbuf, true); + int msg = mRS.nContextGetMessage(mRS.mContext, rbuf, true); if (msg == 0) { // Should only happen during teardown. // But we want to avoid starving other threads during @@ -282,7 +627,6 @@ public class RenderScript { mRS.mMessageCallback.mID = msg; mRS.mMessageCallback.run(); } - //Log.d(LOG_TAG, "MessageThread msg " + msg + " v1 " + rbuf[0] + " v2 " + rbuf[1] + " v3 " +rbuf[2]); } Log.d(LOG_TAG, "MessageThread exiting."); } @@ -307,12 +651,16 @@ public class RenderScript { nContextDump(bits); } + public void finish() { + nContextFinish(); + } + public void destroy() { validate(); - nContextDeinitToClient(); + nContextDeinitToClient(mContext); mMessageThread.mRun = false; - nContextDestroy(mContext); + nContextDestroy(); mContext = 0; nDeviceDestroy(mDev); @@ -335,3 +683,4 @@ public class RenderScript { } + diff --git a/graphics/java/android/renderscript/RenderScriptGL.java b/graphics/java/android/renderscript/RenderScriptGL.java index d1df23d..61ecc8d 100644 --- a/graphics/java/android/renderscript/RenderScriptGL.java +++ b/graphics/java/android/renderscript/RenderScriptGL.java @@ -74,9 +74,9 @@ public class RenderScriptGL extends RenderScript { nContextBindRootScript(safeID(s)); } - public void contextBindProgramFragmentStore(ProgramStore p) { + public void contextBindProgramStore(ProgramStore p) { validate(); - nContextBindProgramFragmentStore(safeID(p)); + nContextBindProgramStore(safeID(p)); } public void contextBindProgramFragment(ProgramFragment p) { @@ -102,8 +102,7 @@ public class RenderScriptGL extends RenderScript { public class File extends BaseObj { File(int id) { - super(RenderScriptGL.this); - mID = id; + super(id, RenderScriptGL.this); } } diff --git a/graphics/java/android/renderscript/Sampler.java b/graphics/java/android/renderscript/Sampler.java index 40ba722..ccd46bd 100644 --- a/graphics/java/android/renderscript/Sampler.java +++ b/graphics/java/android/renderscript/Sampler.java @@ -47,10 +47,82 @@ public class Sampler extends BaseObj { } Sampler(int id, RenderScript rs) { - super(rs); - mID = id; + super(id, rs); } + public static Sampler CLAMP_NEAREST(RenderScript rs) { + if(rs.mSampler_CLAMP_NEAREST == null) { + Builder b = new Builder(rs); + b.setMin(Value.NEAREST); + b.setMag(Value.NEAREST); + b.setWrapS(Value.CLAMP); + b.setWrapT(Value.CLAMP); + rs.mSampler_CLAMP_NEAREST = b.create(); + } + return rs.mSampler_CLAMP_NEAREST; + } + + public static Sampler CLAMP_LINEAR(RenderScript rs) { + if(rs.mSampler_CLAMP_LINEAR == null) { + Builder b = new Builder(rs); + b.setMin(Value.LINEAR); + b.setMag(Value.LINEAR); + b.setWrapS(Value.CLAMP); + b.setWrapT(Value.CLAMP); + rs.mSampler_CLAMP_LINEAR = b.create(); + } + return rs.mSampler_CLAMP_LINEAR; + } + + public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) { + if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) { + Builder b = new Builder(rs); + b.setMin(Value.LINEAR_MIP_LINEAR); + b.setMag(Value.LINEAR_MIP_LINEAR); + b.setWrapS(Value.CLAMP); + b.setWrapT(Value.CLAMP); + rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create(); + } + return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR; + } + + public static Sampler WRAP_NEAREST(RenderScript rs) { + if(rs.mSampler_WRAP_NEAREST == null) { + Builder b = new Builder(rs); + b.setMin(Value.NEAREST); + b.setMag(Value.NEAREST); + b.setWrapS(Value.WRAP); + b.setWrapT(Value.WRAP); + rs.mSampler_WRAP_NEAREST = b.create(); + } + return rs.mSampler_WRAP_NEAREST; + } + + public static Sampler WRAP_LINEAR(RenderScript rs) { + if(rs.mSampler_WRAP_LINEAR == null) { + Builder b = new Builder(rs); + b.setMin(Value.LINEAR); + b.setMag(Value.LINEAR); + b.setWrapS(Value.WRAP); + b.setWrapT(Value.WRAP); + rs.mSampler_WRAP_LINEAR = b.create(); + } + return rs.mSampler_WRAP_LINEAR; + } + + public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) { + if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) { + Builder b = new Builder(rs); + b.setMin(Value.LINEAR_MIP_LINEAR); + b.setMag(Value.LINEAR_MIP_LINEAR); + b.setWrapS(Value.WRAP); + b.setWrapT(Value.WRAP); + rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create(); + } + return rs.mSampler_WRAP_LINEAR_MIP_LINEAR; + } + + public static class Builder { RenderScript mRS; Value mMin; diff --git a/graphics/java/android/renderscript/Script.java b/graphics/java/android/renderscript/Script.java index 57ccfa3..19c444c 100644 --- a/graphics/java/android/renderscript/Script.java +++ b/graphics/java/android/renderscript/Script.java @@ -42,29 +42,46 @@ public class Script extends BaseObj { } } + protected void invoke(int slot) { + mRS.nScriptInvoke(mID, slot); + } + + protected void invoke(int slot, FieldPacker v) { + if (v != null) { + mRS.nScriptInvokeV(mID, slot, v.getData()); + } else { + mRS.nScriptInvoke(mID, slot); + } + } + + Script(int id, RenderScript rs) { - super(rs); - mID = id; + super(id, rs); } public void bindAllocation(Allocation va, int slot) { mRS.validate(); - mRS.nScriptBindAllocation(mID, va.mID, slot); + if (va != null) { + mRS.nScriptBindAllocation(mID, va.mID, slot); + } else { + mRS.nScriptBindAllocation(mID, 0, slot); + } } - public void setClearColor(float r, float g, float b, float a) { - mRS.validate(); - mRS.nScriptSetClearColor(mID, r, g, b, a); + public void setVar(int index, float v) { + mRS.nScriptSetVarF(mID, index, v); } - public void setClearDepth(float d) { - mRS.validate(); - mRS.nScriptSetClearDepth(mID, d); + public void setVar(int index, int v) { + mRS.nScriptSetVarI(mID, index, v); } - public void setClearStencil(int stencil) { - mRS.validate(); - mRS.nScriptSetClearStencil(mID, stencil); + public void setVar(int index, boolean v) { + mRS.nScriptSetVarI(mID, index, v ? 1 : 0); + } + + public void setVar(int index, FieldPacker v) { + mRS.nScriptSetVarV(mID, index, v.getData()); } public void setTimeZone(String timeZone) { @@ -78,72 +95,54 @@ public class Script extends BaseObj { public static class Builder { RenderScript mRS; - boolean mIsRoot = false; - Type[] mTypes; - String[] mNames; - boolean[] mWritable; - int mInvokableCount = 0; - Invokable[] mInvokables; Builder(RenderScript rs) { mRS = rs; - mTypes = new Type[MAX_SLOT]; - mNames = new String[MAX_SLOT]; - mWritable = new boolean[MAX_SLOT]; - mInvokables = new Invokable[MAX_SLOT]; } + } + + + public static class FieldBase { + protected Element mElement; + protected Type mType; + protected Allocation mAllocation; - public void setType(Type t, int slot) { - mTypes[slot] = t; - mNames[slot] = null; + protected void init(RenderScript rs, int dimx) { + mAllocation = Allocation.createSized(rs, mElement, dimx); + mType = mAllocation.getType(); } - public void setType(Type t, String name, int slot) { - mTypes[slot] = t; - mNames[slot] = name; + protected FieldBase() { } - public Invokable addInvokable(String func) { - Invokable i = new Invokable(); - i.mName = func; - i.mRS = mRS; - i.mSlot = mInvokableCount; - mInvokables[mInvokableCount++] = i; - return i; + public Element getElement() { + return mElement; } - public void setType(boolean writable, int slot) { - mWritable[slot] = writable; + public Type getType() { + return mType; } - void transferCreate() { - mRS.nScriptSetRoot(mIsRoot); - for(int ct=0; ct < mTypes.length; ct++) { - if(mTypes[ct] != null) { - mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct); - } - } - for(int ct=0; ct < mInvokableCount; ct++) { - mRS.nScriptSetInvokable(mInvokables[ct].mName, ct); - } + public Allocation getAllocation() { + return mAllocation; } - void transferObject(Script s) { - s.mIsRoot = mIsRoot; - s.mTypes = mTypes; - s.mInvokables = new Invokable[mInvokableCount]; - for(int ct=0; ct < mInvokableCount; ct++) { - s.mInvokables[ct] = mInvokables[ct]; - s.mInvokables[ct].mScript = s; - } - s.mInvokables = null; + //@Override + public void updateAllocation() { } - public void setRoot(boolean r) { - mIsRoot = r; + + // + /* + public class ScriptField_UserField + extends android.renderscript.Script.FieldBase { + + protected + } - } + */ + } } diff --git a/graphics/java/android/renderscript/ScriptC.java b/graphics/java/android/renderscript/ScriptC.java index bb99e23..5959be4 100644 --- a/graphics/java/android/renderscript/ScriptC.java +++ b/graphics/java/android/renderscript/ScriptC.java @@ -37,11 +37,49 @@ public class ScriptC extends Script { super(id, rs); } + protected ScriptC(RenderScript rs, Resources resources, int resourceID, boolean isRoot) { + super(0, rs); + mID = internalCreate(rs, resources, resourceID); + } + + + private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID) { + byte[] pgm; + int pgmLength; + InputStream is = resources.openRawResource(resourceID); + try { + try { + pgm = new byte[1024]; + pgmLength = 0; + while(true) { + int bytesLeft = pgm.length - pgmLength; + if (bytesLeft == 0) { + byte[] buf2 = new byte[pgm.length * 2]; + System.arraycopy(pgm, 0, buf2, 0, pgm.length); + pgm = buf2; + bytesLeft = pgm.length - pgmLength; + } + int bytesRead = is.read(pgm, pgmLength, bytesLeft); + if (bytesRead <= 0) { + break; + } + pgmLength += bytesRead; + } + } finally { + is.close(); + } + } catch(IOException e) { + throw new Resources.NotFoundException(); + } + + rs.nScriptCBegin(); + rs.nScriptCSetScript(pgm, 0, pgmLength); + return rs.nScriptCCreate(); + } + public static class Builder extends Script.Builder { byte[] mProgram; int mProgramLength; - HashMap<String,Integer> mIntDefines = new HashMap(); - HashMap<String,Float> mFloatDefines = new HashMap(); public Builder(RenderScript rs) { super(rs); @@ -92,66 +130,20 @@ public class ScriptC extends Script { static synchronized ScriptC internalCreate(Builder b) { b.mRS.nScriptCBegin(); - b.transferCreate(); - - for (Entry<String,Integer> e: b.mIntDefines.entrySet()) { - b.mRS.nScriptCAddDefineI32(e.getKey(), e.getValue().intValue()); - } - for (Entry<String,Float> e: b.mFloatDefines.entrySet()) { - b.mRS.nScriptCAddDefineF(e.getKey(), e.getValue().floatValue()); - } + android.util.Log.e("rs", "len = " + b.mProgramLength); b.mRS.nScriptCSetScript(b.mProgram, 0, b.mProgramLength); int id = b.mRS.nScriptCCreate(); ScriptC obj = new ScriptC(id, b.mRS); - b.transferObject(obj); - return obj; } - public void addDefine(String name, int value) { - mIntDefines.put(name, value); - } - - public void addDefine(String name, float value) { - mFloatDefines.put(name, value); - } - - /** - * Takes the all public static final fields for a class, and adds defines - * for them, using the name of the field as the name of the define. - */ - public void addDefines(Class cl) { - addDefines(cl.getFields(), (Modifier.STATIC | Modifier.FINAL | Modifier.PUBLIC), null); - } - - /** - * Takes the all public fields for an object, and adds defines - * for them, using the name of the field as the name of the define. - */ - public void addDefines(Object o) { - addDefines(o.getClass().getFields(), Modifier.PUBLIC, o); - } - - void addDefines(Field[] fields, int mask, Object o) { - for (Field f: fields) { - try { - if ((f.getModifiers() & mask) == mask) { - Class t = f.getType(); - if (t == int.class) { - mIntDefines.put(f.getName(), f.getInt(o)); - } - else if (t == float.class) { - mFloatDefines.put(f.getName(), f.getFloat(o)); - } - } - } catch (IllegalAccessException ex) { - // TODO: Do we want this log? - Log.d(TAG, "addDefines skipping field " + f.getName()); - } - } - } + public void addDefine(String name, int value) {} + public void addDefine(String name, float value) {} + public void addDefines(Class cl) {} + public void addDefines(Object o) {} + void addDefines(Field[] fields, int mask, Object o) {} public ScriptC create() { return internalCreate(this); diff --git a/graphics/java/android/renderscript/Short2.java b/graphics/java/android/renderscript/Short2.java new file mode 100644 index 0000000..426801f --- /dev/null +++ b/graphics/java/android/renderscript/Short2.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2009 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. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Short2 { + public Short2() { + } + + public short x; + public short y; +} + + + + diff --git a/graphics/java/android/renderscript/Short3.java b/graphics/java/android/renderscript/Short3.java new file mode 100644 index 0000000..7b9c305 --- /dev/null +++ b/graphics/java/android/renderscript/Short3.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Short3 { + public Short3() { + } + + public short x; + public short y; + public short z; +} + + + + diff --git a/graphics/java/android/renderscript/Short4.java b/graphics/java/android/renderscript/Short4.java new file mode 100644 index 0000000..9a474e2 --- /dev/null +++ b/graphics/java/android/renderscript/Short4.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Short4 { + public Short4() { + } + + public short x; + public short y; + public short z; + public short w; +} + + + diff --git a/graphics/java/android/renderscript/SimpleMesh.java b/graphics/java/android/renderscript/SimpleMesh.java deleted file mode 100644 index 4a217a9..0000000 --- a/graphics/java/android/renderscript/SimpleMesh.java +++ /dev/null @@ -1,364 +0,0 @@ -/* - * Copyright (C) 2008 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. - */ - -package android.renderscript; - -import android.util.Config; -import android.util.Log; - -/** - * @hide - * - **/ -public class SimpleMesh extends BaseObj { - Type[] mVertexTypes; - Type mIndexType; - //Type mBatcheType; - Primitive mPrimitive; - - SimpleMesh(int id, RenderScript rs) { - super(rs); - mID = id; - } - - public void bindVertexAllocation(Allocation a, int slot) { - mRS.validate(); - mRS.nSimpleMeshBindVertex(mID, a.mID, slot); - } - - public void bindIndexAllocation(Allocation a) { - mRS.validate(); - mRS.nSimpleMeshBindIndex(mID, a.mID); - } - - public Allocation createVertexAllocation(int slot) { - mRS.validate(); - return Allocation.createTyped(mRS, mVertexTypes[slot]); - } - - public Allocation createIndexAllocation() { - mRS.validate(); - return Allocation.createTyped(mRS, mIndexType); - } - - public Type getVertexType(int slot) { - return mVertexTypes[slot]; - } - - public Type getIndexType() { - return mIndexType; - } - - public static class Builder { - RenderScript mRS; - - class Entry { - Type t; - Element e; - int size; - } - - int mVertexTypeCount; - Entry[] mVertexTypes; - Entry mIndexType; - //Entry mBatchType; - Primitive mPrimitive; - - - public Builder(RenderScript rs) { - mRS = rs; - mVertexTypeCount = 0; - mVertexTypes = new Entry[16]; - mIndexType = new Entry(); - } - - public int addVertexType(Type t) throws IllegalStateException { - if (mVertexTypeCount >= mVertexTypes.length) { - throw new IllegalStateException("Max vertex types exceeded."); - } - - int addedIndex = mVertexTypeCount; - mVertexTypes[mVertexTypeCount] = new Entry(); - mVertexTypes[mVertexTypeCount].t = t; - mVertexTypeCount++; - return addedIndex; - } - - public int addVertexType(Element e, int size) throws IllegalStateException { - if (mVertexTypeCount >= mVertexTypes.length) { - throw new IllegalStateException("Max vertex types exceeded."); - } - - int addedIndex = mVertexTypeCount; - mVertexTypes[mVertexTypeCount] = new Entry(); - mVertexTypes[mVertexTypeCount].e = e; - mVertexTypes[mVertexTypeCount].size = size; - mVertexTypeCount++; - return addedIndex; - } - - public void setIndexType(Type t) { - mIndexType.t = t; - mIndexType.e = null; - mIndexType.size = 0; - } - - public void setIndexType(Element e, int size) { - mIndexType.t = null; - mIndexType.e = e; - mIndexType.size = size; - } - - public void setPrimitive(Primitive p) { - mPrimitive = p; - } - - - Type newType(Element e, int size) { - Type.Builder tb = new Type.Builder(mRS, e); - tb.add(Dimension.X, size); - return tb.create(); - } - - static synchronized SimpleMesh internalCreate(RenderScript rs, Builder b) { - Type[] toDestroy = new Type[18]; - int toDestroyCount = 0; - - int indexID = 0; - if (b.mIndexType.t != null) { - indexID = b.mIndexType.t.mID; - } else if (b.mIndexType.size != 0) { - b.mIndexType.t = b.newType(b.mIndexType.e, b.mIndexType.size); - indexID = b.mIndexType.t.mID; - toDestroy[toDestroyCount++] = b.mIndexType.t; - } - - int[] IDs = new int[b.mVertexTypeCount]; - for(int ct=0; ct < b.mVertexTypeCount; ct++) { - if (b.mVertexTypes[ct].t != null) { - IDs[ct] = b.mVertexTypes[ct].t.mID; - } else { - b.mVertexTypes[ct].t = b.newType(b.mVertexTypes[ct].e, b.mVertexTypes[ct].size); - IDs[ct] = b.mVertexTypes[ct].t.mID; - toDestroy[toDestroyCount++] = b.mVertexTypes[ct].t; - } - } - - int id = rs.nSimpleMeshCreate(0, indexID, IDs, b.mPrimitive.mID); - for(int ct=0; ct < toDestroyCount; ct++) { - toDestroy[ct].destroy(); - } - - return new SimpleMesh(id, rs); - } - - public SimpleMesh create() { - mRS.validate(); - SimpleMesh sm = internalCreate(mRS, this); - sm.mVertexTypes = new Type[mVertexTypeCount]; - for(int ct=0; ct < mVertexTypeCount; ct++) { - sm.mVertexTypes[ct] = mVertexTypes[ct].t; - } - sm.mIndexType = mIndexType.t; - sm.mPrimitive = mPrimitive; - return sm; - } - } - - public static class TriangleMeshBuilder { - float mVtxData[]; - int mVtxCount; - short mIndexData[]; - int mIndexCount; - RenderScript mRS; - Element mElement; - - float mNX = 0; - float mNY = 0; - float mNZ = -1; - float mS0 = 0; - float mT0 = 0; - float mR = 1; - float mG = 1; - float mB = 1; - float mA = 1; - - int mVtxSize; - int mFlags; - - public static final int COLOR = 0x0001; - public static final int NORMAL = 0x0002; - public static final int TEXTURE_0 = 0x0100; - - public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) { - mRS = rs; - mVtxCount = 0; - mIndexCount = 0; - mVtxData = new float[128]; - mIndexData = new short[128]; - mVtxSize = vtxSize; - mFlags = flags; - - if (vtxSize < 2 || vtxSize > 3) { - throw new IllegalArgumentException("Vertex size out of range."); - } - } - - private void makeSpace(int count) { - if ((mVtxCount + count) >= mVtxData.length) { - float t[] = new float[mVtxData.length * 2]; - System.arraycopy(mVtxData, 0, t, 0, mVtxData.length); - mVtxData = t; - } - } - - private void latch() { - if ((mFlags & COLOR) != 0) { - makeSpace(4); - mVtxData[mVtxCount++] = mR; - mVtxData[mVtxCount++] = mG; - mVtxData[mVtxCount++] = mB; - mVtxData[mVtxCount++] = mA; - } - if ((mFlags & TEXTURE_0) != 0) { - makeSpace(2); - mVtxData[mVtxCount++] = mS0; - mVtxData[mVtxCount++] = mT0; - } - if ((mFlags & NORMAL) != 0) { - makeSpace(3); - mVtxData[mVtxCount++] = mNX; - mVtxData[mVtxCount++] = mNY; - mVtxData[mVtxCount++] = mNZ; - } - } - - public void addVertex(float x, float y) { - if (mVtxSize != 2) { - throw new IllegalStateException("add mistmatch with declared components."); - } - makeSpace(2); - mVtxData[mVtxCount++] = x; - mVtxData[mVtxCount++] = y; - latch(); - } - - public void addVertex(float x, float y, float z) { - if (mVtxSize != 3) { - throw new IllegalStateException("add mistmatch with declared components."); - } - makeSpace(3); - mVtxData[mVtxCount++] = x; - mVtxData[mVtxCount++] = y; - mVtxData[mVtxCount++] = z; - latch(); - } - - public void setTexture(float s, float t) { - if ((mFlags & TEXTURE_0) == 0) { - throw new IllegalStateException("add mistmatch with declared components."); - } - mS0 = s; - mT0 = t; - } - - public void setNormal(float x, float y, float z) { - if ((mFlags & NORMAL) == 0) { - throw new IllegalStateException("add mistmatch with declared components."); - } - mNX = x; - mNY = y; - mNZ = z; - } - - public void setColor(float r, float g, float b, float a) { - if ((mFlags & COLOR) == 0) { - throw new IllegalStateException("add mistmatch with declared components."); - } - mR = r; - mG = g; - mB = b; - mA = a; - } - - public void addTriangle(int idx1, int idx2, int idx3) { - if((idx1 >= mVtxCount) || (idx1 < 0) || - (idx2 >= mVtxCount) || (idx2 < 0) || - (idx3 >= mVtxCount) || (idx3 < 0)) { - throw new IllegalStateException("Index provided greater than vertex count."); - } - if ((mIndexCount + 3) >= mIndexData.length) { - short t[] = new short[mIndexData.length * 2]; - System.arraycopy(mIndexData, 0, t, 0, mIndexData.length); - mIndexData = t; - } - mIndexData[mIndexCount++] = (short)idx1; - mIndexData[mIndexCount++] = (short)idx2; - mIndexData[mIndexCount++] = (short)idx3; - } - - public SimpleMesh create() { - Element.Builder b = new Element.Builder(mRS); - int floatCount = mVtxSize; - b.add(Element.createAttrib(mRS, - Element.DataType.FLOAT_32, - Element.DataKind.POSITION, - mVtxSize), "position"); - if ((mFlags & COLOR) != 0) { - floatCount += 4; - b.add(Element.createAttrib(mRS, - Element.DataType.FLOAT_32, - Element.DataKind.COLOR, - 4), "color"); - } - if ((mFlags & TEXTURE_0) != 0) { - floatCount += 2; - b.add(Element.createAttrib(mRS, - Element.DataType.FLOAT_32, - Element.DataKind.TEXTURE, - 2), "texture"); - } - if ((mFlags & NORMAL) != 0) { - floatCount += 3; - b.add(Element.createAttrib(mRS, - Element.DataType.FLOAT_32, - Element.DataKind.NORMAL, - 3), "normal"); - } - mElement = b.create(); - - Builder smb = new Builder(mRS); - smb.addVertexType(mElement, mVtxCount / floatCount); - smb.setIndexType(Element.createIndex(mRS), mIndexCount); - smb.setPrimitive(Primitive.TRIANGLE); - SimpleMesh sm = smb.create(); - - Allocation vertexAlloc = sm.createVertexAllocation(0); - Allocation indexAlloc = sm.createIndexAllocation(); - sm.bindVertexAllocation(vertexAlloc, 0); - sm.bindIndexAllocation(indexAlloc); - - vertexAlloc.data(mVtxData); - vertexAlloc.uploadToBufferObject(); - - indexAlloc.data(mIndexData); - indexAlloc.uploadToBufferObject(); - - return sm; - } - } -} - diff --git a/graphics/java/android/renderscript/Type.java b/graphics/java/android/renderscript/Type.java index 62d3867..21053c9 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 @@ -95,8 +97,7 @@ public class Type extends BaseObj { Type(int id, RenderScript rs) { - super(rs); - mID = id; + super(id, rs); mNativeCache = 0; } @@ -108,48 +109,30 @@ public class Type extends BaseObj { super.finalize(); } - public static Type createFromClass(RenderScript rs, Class c, int size) { - 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_32.mID; - arBits[ct] = 32; - } else if(fc == short.class) { - arTypes[ct] = Element.DataType.SIGNED_16.mID; - arBits[ct] = 16; - } else if(fc == byte.class) { - arTypes[ct] = Element.DataType.SIGNED_8.mID; - arBits[ct] = 8; - } else if(fc == float.class) { - arTypes[ct] = Element.DataType.FLOAT_32.mID; - arBits[ct] = 32; - } else { - throw new IllegalArgumentException("Unkown field type"); - } - } - rs.nTypeSetupFields(t, arTypes, arBits, fields); + @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(elementID, mRS); + mElement.updateFromNative(); } - t.mJavaClass = c; - return t; + calcElementCount(); } public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) { - Type t = createFromClass(rs, c, size); - t.setName(scriptName); - return t; + android.util.Log.e("RenderScript", "Calling depricated createFromClass"); + return null; } |