blob: 484849ba6404f6034ae7b4de64dceda2f92b2e94 [file] [log] [blame]
Jason Sams1bada8c2009-08-09 17:01:55 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
19import android.util.Config;
20import android.util.Log;
21
22/**
23 * @hide
24 *
25 **/
26public class SimpleMesh extends BaseObj {
27 Type[] mVertexTypes;
28 Type mIndexType;
29 //Type mBatcheType;
30 Primitive mPrimitive;
31
32 SimpleMesh(int id, RenderScript rs) {
33 super(rs);
34 mID = id;
35 }
36
37 public void destroy() {
38 if(mDestroyed) {
39 throw new IllegalStateException("Object already destroyed.");
40 }
41 mDestroyed = true;
42 mRS.nSimpleMeshDestroy(mID);
43 }
44
45 public void bindVertexAllocation(Allocation a, int slot) {
46 mRS.nSimpleMeshBindVertex(mID, a.mID, slot);
47 }
48
49 public void bindIndexAllocation(Allocation a) {
50 mRS.nSimpleMeshBindIndex(mID, a.mID);
51 }
52
53 public Allocation createVertexAllocation(int slot) {
54 return Allocation.createTyped(mRS, mVertexTypes[slot]);
55 }
56
57 public Allocation createIndexAllocation() {
58 return Allocation.createTyped(mRS, mIndexType);
59 }
60
61
62 public static class Builder {
63 RenderScript mRS;
64
65 class Entry {
66 Type t;
67 Element e;
68 int size;
69 }
70
71 int mVertexTypeCount;
72 Entry[] mVertexTypes;
73 Entry mIndexType;
74 //Entry mBatchType;
75 Primitive mPrimitive;
76
77
78 public Builder(RenderScript rs) {
79 mRS = rs;
80 mVertexTypeCount = 0;
81 mVertexTypes = new Entry[16];
82 mIndexType = new Entry();
83 }
84
85 public int addVertexType(Type t) throws IllegalStateException {
86 if(mVertexTypeCount >= mVertexTypes.length) {
87 throw new IllegalStateException("Max vertex types exceeded.");
88 }
89
90 int addedIndex = mVertexTypeCount;
91 mVertexTypes[mVertexTypeCount] = new Entry();
92 mVertexTypes[mVertexTypeCount].t = t;
93 mVertexTypeCount++;
94 return addedIndex;
95 }
96
97 public int addVertexType(Element e, int size) throws IllegalStateException {
98 if(mVertexTypeCount >= mVertexTypes.length) {
99 throw new IllegalStateException("Max vertex types exceeded.");
100 }
101
102 int addedIndex = mVertexTypeCount;
103 mVertexTypes[mVertexTypeCount] = new Entry();
104 mVertexTypes[mVertexTypeCount].e = e;
105 mVertexTypes[mVertexTypeCount].size = size;
106 mVertexTypeCount++;
107 return addedIndex;
108 }
109
110 public void setIndexType(Type t) {
111 mIndexType.t = t;
112 mIndexType.e = null;
113 mIndexType.size = 0;
114 }
115
116 public void setIndexType(Element e, int size) {
117 mIndexType.t = null;
118 mIndexType.e = e;
119 mIndexType.size = size;
120 }
121
122 public void setPrimitive(Primitive p) {
123 mPrimitive = p;
124 }
125
126
127 Type newType(Element e, int size) {
128 Type.Builder tb = new Type.Builder(mRS, e);
129 tb.add(Dimension.X, size);
130 return tb.create();
131 }
132
133 static synchronized SimpleMesh internalCreate(RenderScript rs, Builder b) {
134 Type[] toDestroy = new Type[18];
135 int toDestroyCount = 0;
136
137 int indexID = 0;
138 if(b.mIndexType.t != null) {
139 indexID = b.mIndexType.t.mID;
140 } else if(b.mIndexType.size != 0) {
141 b.mIndexType.t = b.newType(b.mIndexType.e, b.mIndexType.size);
142 indexID = b.mIndexType.t.mID;
143 toDestroy[toDestroyCount++] = b.mIndexType.t;
144 }
145
146 int[] IDs = new int[b.mVertexTypeCount];
147 for(int ct=0; ct < b.mVertexTypeCount; ct++) {
148 if(b.mVertexTypes[ct].t != null) {
149 IDs[ct] = b.mVertexTypes[ct].t.mID;
150 } else {
151 b.mVertexTypes[ct].t = b.newType(b.mVertexTypes[ct].e, b.mVertexTypes[ct].size);
152 IDs[ct] = b.mVertexTypes[ct].t.mID;
153 toDestroy[toDestroyCount++] = b.mVertexTypes[ct].t;
154 }
155 }
156
157 int id = rs.nSimpleMeshCreate(0, indexID, IDs, b.mPrimitive.mID);
158 for(int ct=0; ct < toDestroyCount; ct++) {
159 toDestroy[ct].destroy();
160 }
161
162 return new SimpleMesh(id, rs);
163 }
164
165 public SimpleMesh create() {
166 Log.e("rs", "SimpleMesh create");
167 SimpleMesh sm = internalCreate(mRS, this);
168 sm.mVertexTypes = new Type[mVertexTypeCount];
169 for(int ct=0; ct < mVertexTypeCount; ct++) {
170 sm.mVertexTypes[ct] = mVertexTypes[ct].t;
171 }
172 sm.mIndexType = mIndexType.t;
173 sm.mPrimitive = mPrimitive;
174 return sm;
175 }
176 }
177
178}
179