From 1bada8cd6e4f340de93cff4a2439835fc3b1456c Mon Sep 17 00:00:00 2001 From: Jason Sams Date: Sun, 9 Aug 2009 17:01:55 -0700 Subject: Begin implementing SimpleMesh and fix some bugs with refcounting and java object destruction tracking. --- graphics/java/android/renderscript/Allocation.java | 19 ++- graphics/java/android/renderscript/BaseObj.java | 4 +- graphics/java/android/renderscript/Element.java | 7 +- graphics/java/android/renderscript/Primitive.java | 37 +++++ .../java/android/renderscript/ProgramFragment.java | 5 +- .../java/android/renderscript/ProgramStore.java | 5 +- .../java/android/renderscript/ProgramVertex.java | 5 +- .../java/android/renderscript/RenderScript.java | 5 + graphics/java/android/renderscript/Script.java | 5 +- graphics/java/android/renderscript/SimpleMesh.java | 179 +++++++++++++++++++++ graphics/java/android/renderscript/Type.java | 28 ++-- 11 files changed, 279 insertions(+), 20 deletions(-) create mode 100644 graphics/java/android/renderscript/Primitive.java create mode 100644 graphics/java/android/renderscript/SimpleMesh.java (limited to 'graphics/java') diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java index 3b6571a..ede475f 100644 --- a/graphics/java/android/renderscript/Allocation.java +++ b/graphics/java/android/renderscript/Allocation.java @@ -43,8 +43,11 @@ public class Allocation extends BaseObj { } public void destroy() { + if(mDestroyed) { + throw new IllegalStateException("Object already destroyed."); + } + mDestroyed = true; mRS.nAllocationDestroy(mID); - mID = 0; } public void data(int[] d) { @@ -160,17 +163,27 @@ public class Allocation extends BaseObj { mBitmapOptions.inScaled = false; } - static public Allocation createTyped(RenderScript rs, Type type) { + static public Allocation createTyped(RenderScript rs, Type type) + throws IllegalArgumentException { + + if(type.mID == 0) { + throw new IllegalStateException("Bad Type"); + } int id = rs.nAllocationCreateTyped(type.mID); return new Allocation(id, rs); } - static public Allocation createSized(RenderScript rs, Element e, int count) { + static public Allocation createSized(RenderScript rs, Element e, int count) + throws IllegalArgumentException { + int id; if(e.mIsPredefined) { id = rs.nAllocationCreatePredefSized(e.mPredefinedID, count); } else { id = rs.nAllocationCreateSized(e.mID, count); + if(id == 0) { + throw new IllegalStateException("Bad element."); + } } return new Allocation(id, rs); } diff --git a/graphics/java/android/renderscript/BaseObj.java b/graphics/java/android/renderscript/BaseObj.java index f70aee5..f760035 100644 --- a/graphics/java/android/renderscript/BaseObj.java +++ b/graphics/java/android/renderscript/BaseObj.java @@ -27,6 +27,7 @@ class BaseObj { BaseObj(RenderScript rs) { mRS = rs; mID = 0; + mDestroyed = false; } public int getID() { @@ -34,6 +35,7 @@ class BaseObj { } int mID; + boolean mDestroyed; String mName; RenderScript mRS; @@ -57,7 +59,7 @@ class BaseObj { protected void finalize() throws Throwable { - if (mID != 0) { + if (!mDestroyed) { Log.v(RenderScript.LOG_TAG, "Element finalized without having released the RS reference."); } diff --git a/graphics/java/android/renderscript/Element.java b/graphics/java/android/renderscript/Element.java index 409d267..14d9115 100644 --- a/graphics/java/android/renderscript/Element.java +++ b/graphics/java/android/renderscript/Element.java @@ -137,8 +137,11 @@ public class Element extends BaseObj { if(mIsPredefined) { throw new IllegalStateException("Attempting to destroy a predefined Element."); } + if(mDestroyed) { + throw new IllegalStateException("Object already destroyed."); + } + mDestroyed = true; mRS.nElementDestroy(mID); - mID = 0; } @@ -206,7 +209,7 @@ public class Element extends BaseObj { if (en.mIsNormalized) { norm = 1; } - rs.nElementAdd(en.mType.mID, en.mKind.mID, norm, en.mBits); + rs.nElementAdd(en.mKind.mID, en.mType.mID, norm, en.mBits); } } int id = rs.nElementCreate(); diff --git a/graphics/java/android/renderscript/Primitive.java b/graphics/java/android/renderscript/Primitive.java new file mode 100644 index 0000000..7925cac --- /dev/null +++ b/graphics/java/android/renderscript/Primitive.java @@ -0,0 +1,37 @@ +/* + * 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; + +/** + * @hide + **/ +public enum Primitive { + POINT (0), + LINE (1), + LINE_STRIP (2), + TRIANGLE (3), + TRIANGLE_STRIP (4), + TRIANGLE_FAN (5); + + int mID; + Primitive(int id) { + mID = id; + } +} + + + diff --git a/graphics/java/android/renderscript/ProgramFragment.java b/graphics/java/android/renderscript/ProgramFragment.java index c228cf2..09c4d9a 100644 --- a/graphics/java/android/renderscript/ProgramFragment.java +++ b/graphics/java/android/renderscript/ProgramFragment.java @@ -46,8 +46,11 @@ public class ProgramFragment extends BaseObj { } public void destroy() { + if(mDestroyed) { + throw new IllegalStateException("Object already destroyed."); + } + mDestroyed = true; mRS.nProgramFragmentStoreDestroy(mID); - mID = 0; } public void bindTexture(Allocation va, int slot) diff --git a/graphics/java/android/renderscript/ProgramStore.java b/graphics/java/android/renderscript/ProgramStore.java index 9039621..f8b59bd 100644 --- a/graphics/java/android/renderscript/ProgramStore.java +++ b/graphics/java/android/renderscript/ProgramStore.java @@ -81,8 +81,11 @@ public class ProgramStore extends BaseObj { } public void destroy() { + if(mDestroyed) { + throw new IllegalStateException("Object already destroyed."); + } + mDestroyed = true; mRS.nProgramFragmentStoreDestroy(mID); - mID = 0; } diff --git a/graphics/java/android/renderscript/ProgramVertex.java b/graphics/java/android/renderscript/ProgramVertex.java index c4f7759..74c005f 100644 --- a/graphics/java/android/renderscript/ProgramVertex.java +++ b/graphics/java/android/renderscript/ProgramVertex.java @@ -34,8 +34,11 @@ public class ProgramVertex extends BaseObj { } public void destroy() { + if(mDestroyed) { + throw new IllegalStateException("Object already destroyed."); + } + mDestroyed = true; mRS.nProgramVertexDestroy(mID); - mID = 0; } public void bindAllocation(MatrixAllocation va) { diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java index 0fb450e..8fc5ad5 100644 --- a/graphics/java/android/renderscript/RenderScript.java +++ b/graphics/java/android/renderscript/RenderScript.java @@ -178,6 +178,11 @@ public class RenderScript { native void nLightSetColor(int l, float r, float g, float b); native void nLightSetPosition(int l, float x, float y, float z); + native void nSimpleMeshDestroy(int id); + 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); + private int mDev; private int mContext; diff --git a/graphics/java/android/renderscript/Script.java b/graphics/java/android/renderscript/Script.java index 9696cea..1c7b32b 100644 --- a/graphics/java/android/renderscript/Script.java +++ b/graphics/java/android/renderscript/Script.java @@ -28,8 +28,11 @@ public class Script extends BaseObj { } public void destroy() { + if(mDestroyed) { + throw new IllegalStateException("Object already destroyed."); + } + mDestroyed = true; mRS.nScriptDestroy(mID); - mID = 0; } public void bindAllocation(Allocation va, int slot) { diff --git a/graphics/java/android/renderscript/SimpleMesh.java b/graphics/java/android/renderscript/SimpleMesh.java new file mode 100644 index 0000000..484849b --- /dev/null +++ b/graphics/java/android/renderscript/SimpleMesh.java @@ -0,0 +1,179 @@ +/* + * 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 destroy() { + if(mDestroyed) { + throw new IllegalStateException("Object already destroyed."); + } + mDestroyed = true; + mRS.nSimpleMeshDestroy(mID); + } + + public void bindVertexAllocation(Allocation a, int slot) { + mRS.nSimpleMeshBindVertex(mID, a.mID, slot); + } + + public void bindIndexAllocation(Allocation a) { + mRS.nSimpleMeshBindIndex(mID, a.mID); + } + + public Allocation createVertexAllocation(int slot) { + return Allocation.createTyped(mRS, mVertexTypes[slot]); + } + + public Allocation createIndexAllocation() { + return Allocation.createTyped(mRS, 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() { + Log.e("rs", "SimpleMesh create"); + 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; + } + } + +} + diff --git a/graphics/java/android/renderscript/Type.java b/graphics/java/android/renderscript/Type.java index 4078c8a..a5fc603 100644 --- a/graphics/java/android/renderscript/Type.java +++ b/graphics/java/android/renderscript/Type.java @@ -16,31 +16,31 @@ package android.renderscript; - -import java.io.IOException; -import java.io.InputStream; - -import android.content.res.Resources; -import android.os.Bundle; import android.util.Config; import android.util.Log; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; /** * @hide * **/ public class Type extends BaseObj { + Dimension[] mDimensions; + int[] mValues; + Element mElement; + + Type(int id, RenderScript rs) { super(rs); mID = id; } public void destroy() { + if(mDestroyed) { + throw new IllegalStateException("Object already destroyed."); + } + mDestroyed = true; mRS.nTypeDestroy(mID); - mID = 0; } public static class Builder { @@ -85,7 +85,15 @@ public class Type extends BaseObj { } public Type create() { - return internalCreate(mRS, this); + Type t = internalCreate(mRS, this); + t.mElement = mElement; + t.mDimensions = new Dimension[mEntryCount]; + t.mValues = new int[mEntryCount]; + for(int ct=0; ct < mEntryCount; ct++) { + t.mDimensions[ct] = mEntries[ct].mDim; + t.mValues[ct] = mEntries[ct].mValue; + } + return t; } } -- cgit v1.1