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

This commit is contained in:
Alex Sakhartchouk
2010-07-15 15:19:07 -07:00
committed by Android (Google) Code Review
10 changed files with 236 additions and 21 deletions

View File

@ -45,6 +45,16 @@ public class Allocation extends BaseObj {
mID = id;
}
@Override
void updateFromNative() {
mRS.validate();
int typeID = mRS.nAllocationGetType(mID);
if(typeID != 0) {
mType = new Type(typeID, mRS);
mType.updateFromNative();
}
}
public Type getType() {
return mType;
}

View File

@ -17,6 +17,7 @@
package android.renderscript;
import java.lang.reflect.Field;
import android.util.Log;
/**
* @hide
@ -308,6 +309,45 @@ public class Element extends BaseObj {
mID = rs.nElementCreate(dt.mID, dk.mID, norm, size);
}
Element(RenderScript rs, int id) {
super(rs);
mID = id;
}
@Override
void updateFromNative() {
// we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
int[] dataBuffer = new int[5];
mRS.nElementGetNativeData(mID, dataBuffer);
for (DataType dt: DataType.values()) {
if(dt.mID == dataBuffer[0]){
mType = dt;
}
}
for (DataKind dk: DataKind.values()) {
if(dk.mID == dataBuffer[1]){
mKind = dk;
}
}
mNormalized = dataBuffer[2] == 1 ? true : false;
mVectorSize = dataBuffer[3];
int numSubElements = dataBuffer[4];
if(numSubElements > 0) {
mElements = new Element[numSubElements];
mElementNames = new String[numSubElements];
int[] subElementIds = new int[numSubElements];
mRS.nElementGetSubElements(mID, subElementIds, mElementNames);
for(int i = 0; i < numSubElements; i ++) {
mElements[i] = new Element(mRS, subElementIds[i]);
mElements[i].updateFromNative();
}
}
}
public void destroy() throws IllegalStateException {
super.destroy();
}

View File

@ -56,7 +56,7 @@ public class FileA3D extends BaseObj {
}
// Read only class with index entries
public class IndexEntry {
public static class IndexEntry {
RenderScript mRS;
int mIndex;
int mID;
@ -73,34 +73,40 @@ public class FileA3D extends BaseObj {
}
public BaseObj getObject() {
if(mLoadedObj != null) {
return mLoadedObj;
mRS.validate();
BaseObj obj = internalCreate(mRS, this);
return obj;
}
static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
if(entry.mLoadedObj != null) {
return entry.mLoadedObj;
}
if(mClassID == ClassID.UNKNOWN) {
if(entry.mClassID == ClassID.UNKNOWN) {
return null;
}
int objectID = mRS.nFileA3DGetEntryByIndex(mID, mIndex);
int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex);
if(objectID == 0) {
return null;
}
switch (mClassID) {
switch (entry.mClassID) {
case MESH:
mLoadedObj = new Mesh(objectID, mRS);
entry.mLoadedObj = new Mesh(objectID, rs);
break;
case TYPE:
mLoadedObj = new Type(objectID, mRS);
entry.mLoadedObj = new Type(objectID, rs);
break;
case ELEMENT:
mLoadedObj = null;
entry.mLoadedObj = null;
break;
case ALLOCATION:
mLoadedObj = null;
entry.mLoadedObj = null;
break;
case PROGRAM_VERTEX:
mLoadedObj = new ProgramVertex(objectID, mRS);
entry.mLoadedObj = new ProgramVertex(objectID, rs);
break;
case PROGRAM_RASTER:
break;
@ -122,7 +128,9 @@ public class FileA3D extends BaseObj {
break;
}
return mLoadedObj;
entry.mLoadedObj.updateFromNative();
return entry.mLoadedObj;
}
IndexEntry(RenderScript rs, int index, int id, String name, ClassID classID) {

View File

@ -74,14 +74,6 @@ public class ProgramRaster extends BaseObj {
boolean mPointSmooth;
boolean mLineSmooth;
// Legacy to not break app in other projects, will be removed in cleanup pass
public Builder(RenderScript rs, Element in, Element out) {
mRS = rs;
mPointSmooth = false;
mLineSmooth = false;
mPointSprite = false;
}
public Builder(RenderScript rs) {
mRS = rs;
mPointSmooth = false;

View File

@ -90,12 +90,15 @@ public class RenderScript {
native int nElementCreate(int type, int kind, boolean norm, int vecSize);
native int nElementCreate2(int[] elements, String[] names);
native void nElementGetNativeData(int id, int[] elementData);
native void nElementGetSubElements(int id, int[] IDs, String[] names);
native void nTypeBegin(int elementID);
native void nTypeAdd(int dim, int val);
native int nTypeCreate();
native void nTypeFinalDestroy(Type t);
native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs);
native void nTypeGetNativeData(int id, int[] typeData);
native int nAllocationCreateTyped(int type);
native int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp);
@ -117,6 +120,7 @@ public class RenderScript {
native void nAllocationRead(int id, float[] d);
native void nAllocationSubDataFromObject(int id, Type t, int offset, Object o);
native void nAllocationSubReadFromObject(int id, Type t, int offset, Object o);
native int nAllocationGetType(int id);
native int nFileA3DCreateFromAssetStream(int assetStream);
native int nFileA3DGetNumIndexEntries(int fileA3D);

View File

@ -16,7 +16,9 @@
package android.renderscript;
import java.lang.reflect.Field;
import android.util.Log;
/**
* @hide
@ -108,6 +110,27 @@ public class Type extends BaseObj {
super.finalize();
}
@Override
void updateFromNative() {
// We have 6 integer to obtain mDimX; mDimY; mDimZ;
// mDimLOD; mDimFaces; mElement;
int[] dataBuffer = new int[6];
mRS.nTypeGetNativeData(mID, dataBuffer);
mDimX = dataBuffer[0];
mDimY = dataBuffer[1];
mDimZ = dataBuffer[2];
mDimLOD = dataBuffer[3] == 1 ? true : false;
mDimFaces = dataBuffer[4] == 1 ? true : false;
int elementID = dataBuffer[5];
if(elementID != 0) {
mElement = new Element(mRS, elementID);
mElement.updateFromNative();
}
calcElementCount();
}
public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) {
android.util.Log.e("RenderScript", "Calling depricated createFromClass");
return null;

View File

@ -297,6 +297,46 @@ nElementCreate2(JNIEnv *_env, jobject _this, jintArray _ids, jobjectArray _names
return (jint)id;
}
static void
nElementGetNativeData(JNIEnv *_env, jobject _this, jint id, jintArray _elementData)
{
int dataSize = _env->GetArrayLength(_elementData);
RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
LOG_API("nElementGetNativeData, con(%p)", con);
// we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
assert(dataSize == 5);
uint32_t elementData[5];
rsElementGetNativeData(con, (RsElement)id, elementData, dataSize);
for(jint i = 0; i < dataSize; i ++) {
_env->SetIntArrayRegion(_elementData, i, 1, (const jint*)&elementData[i]);
}
}
static void
nElementGetSubElements(JNIEnv *_env, jobject _this, jint id, jintArray _IDs, jobjectArray _names)
{
int dataSize = _env->GetArrayLength(_IDs);
RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
LOG_API("nElementGetSubElements, con(%p)", con);
uint32_t *ids = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t));
const char **names = (const char **)malloc((uint32_t)dataSize * sizeof(const char *));
rsElementGetSubElements(con, (RsElement)id, ids, names, (uint32_t)dataSize);
for(jint i = 0; i < dataSize; i ++) {
_env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
_env->SetIntArrayRegion(_IDs, i, 1, (const jint*)&ids[i]);
}
free(ids);
free(names);
}
// -----------------------------------
static void
@ -323,6 +363,26 @@ nTypeCreate(JNIEnv *_env, jobject _this)
return (jint)rsTypeCreate(con);
}
static void
nTypeGetNativeData(JNIEnv *_env, jobject _this, jint id, jintArray _typeData)
{
// We are packing 6 items: mDimX; mDimY; mDimZ;
// mDimLOD; mDimFaces; mElement; into typeData
int elementCount = _env->GetArrayLength(_typeData);
assert(elementCount == 6);
RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
LOG_API("nTypeCreate, con(%p)", con);
uint32_t typeData[6];
rsTypeGetNativeData(con, (RsType)id, typeData, 6);
for(jint i = 0; i < elementCount; i ++) {
_env->SetIntArrayRegion(_typeData, i, 1, (const jint*)&typeData[i]);
}
}
static void * SF_LoadInt(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
{
((int32_t *)buffer)[0] = _env->GetIntField(_obj, _field);
@ -708,6 +768,14 @@ nAllocationSubReadFromObject(JNIEnv *_env, jobject _this, jint alloc, jobject _t
free(bufAlloc);
}
static jint
nAllocationGetType(JNIEnv *_env, jobject _this, jint a)
{
RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
LOG_API("nAllocationGetType, con(%p), a(%p)", con, (RsAllocation)a);
return (jint) rsAllocationGetType(con, (RsAllocation)a);
}
// -----------------------------------
static int
@ -1466,12 +1534,15 @@ static JNINativeMethod methods[] = {
{"nElementCreate", "(IIZI)I", (void*)nElementCreate },
{"nElementCreate2", "([I[Ljava/lang/String;)I", (void*)nElementCreate2 },
{"nElementGetNativeData", "(I[I)V", (void*)nElementGetNativeData },
{"nElementGetSubElements", "(I[I[Ljava/lang/String;)V", (void*)nElementGetSubElements },
{"nTypeBegin", "(I)V", (void*)nTypeBegin },
{"nTypeAdd", "(II)V", (void*)nTypeAdd },
{"nTypeCreate", "()I", (void*)nTypeCreate },
{"nTypeFinalDestroy", "(Landroid/renderscript/Type;)V", (void*)nTypeFinalDestroy },
{"nTypeSetupFields", "(Landroid/renderscript/Type;[I[I[Ljava/lang/reflect/Field;)V", (void*)nTypeSetupFields },
{"nTypeGetNativeData", "(I[I)V", (void*)nTypeGetNativeData },
{"nAllocationCreateTyped", "(I)I", (void*)nAllocationCreateTyped },
{"nAllocationCreateFromBitmap", "(IZLandroid/graphics/Bitmap;)I", (void*)nAllocationCreateFromBitmap },
@ -1490,6 +1561,7 @@ static JNINativeMethod methods[] = {
{"nAllocationRead", "(I[F)V", (void*)nAllocationRead_f },
{"nAllocationSubDataFromObject", "(ILandroid/renderscript/Type;ILjava/lang/Object;)V", (void*)nAllocationSubDataFromObject },
{"nAllocationSubReadFromObject", "(ILandroid/renderscript/Type;ILjava/lang/Object;)V", (void*)nAllocationSubReadFromObject },
{"nAllocationGetType", "(I)I", (void*)nAllocationGetType},
{"nAdapter1DBindAllocation", "(II)V", (void*)nAdapter1DBindAllocation },
{"nAdapter1DSetConstraint", "(III)V", (void*)nAdapter1DSetConstraint },

View File

@ -78,6 +78,19 @@ ElementCreate2 {
ret RsElement
}
ElementGetNativeData {
param RsElement elem
param uint32_t *elemData
param uint32_t elemDataSize
}
ElementGetSubElements {
param RsElement elem
param uint32_t *ids
param const char **names
param uint32_t dataSize
}
TypeBegin {
param RsElement type
}
@ -91,6 +104,12 @@ TypeCreate {
ret RsType
}
TypeGetNativeData {
param RsType type
param uint32_t * typeData
param uint32_t typeDataSize
}
AllocationCreateTyped {
param RsType type
ret RsAllocation
@ -231,6 +250,11 @@ Adapter2DSubData {
param const void *data
}
AllocationGetType {
param RsAllocation va
ret const void*
}
SamplerBegin {
}

View File

@ -293,6 +293,32 @@ RsElement rsi_ElementCreate2(Context *rsc,
return (RsElement)e;
}
void rsi_ElementGetNativeData(Context *rsc, RsElement elem, uint32_t *elemData, uint32_t elemDataSize)
{
rsAssert(elemDataSize == 5);
// we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
Element *e = static_cast<Element *>(elem);
(*elemData++) = (uint32_t)e->getType();
(*elemData++) = (uint32_t)e->getKind();
(*elemData++) = e->getComponent().getIsNormalized() ? 1 : 0;
(*elemData++) = e->getComponent().getVectorSize();
(*elemData++) = e->getFieldCount();
}
void rsi_ElementGetSubElements(Context *rsc, RsElement elem, uint32_t *ids, const char **names, uint32_t dataSize)
{
Element *e = static_cast<Element *>(elem);
rsAssert(e->getFieldCount() == dataSize);
for(uint32_t i = 0; i < dataSize; i ++) {
ids[i] = (uint32_t)e->getField(i);
names[i] = e->getFieldName(i);
}
}
}
}

View File

@ -91,7 +91,7 @@ void Type::compute()
if (mLODCount != oldLODCount) {
if(mLODs){
delete [] mLODs;
}
}
mLODs = new LOD[mLODCount];
}
@ -340,6 +340,22 @@ RsType rsi_TypeCreate(Context *rsc)
return st;
}
void rsi_TypeGetNativeData(Context *rsc, RsType type, uint32_t *typeData, uint32_t typeDataSize)
{
rsAssert(typeDataSize == 6);
// Pack the data in the follofing way mDimX; mDimY; mDimZ;
// mDimLOD; mDimFaces; mElement; into typeData
Type *t = static_cast<Type *>(type);
(*typeData++) = t->getDimX();
(*typeData++) = t->getDimY();
(*typeData++) = t->getDimZ();
(*typeData++) = t->getDimLOD();
(*typeData++) = t->getDimFaces() ? 1 : 0;
(*typeData++) = (uint32_t)t->getElement();
}
}
}