blob: d80551e837071a89065ec4e2c45829b1bc225584 [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
Jason Sams1bada8c2009-08-09 17:01:55 -070037 public void bindVertexAllocation(Allocation a, int slot) {
38 mRS.nSimpleMeshBindVertex(mID, a.mID, slot);
39 }
40
41 public void bindIndexAllocation(Allocation a) {
42 mRS.nSimpleMeshBindIndex(mID, a.mID);
43 }
44
45 public Allocation createVertexAllocation(int slot) {
46 return Allocation.createTyped(mRS, mVertexTypes[slot]);
47 }
48
49 public Allocation createIndexAllocation() {
50 return Allocation.createTyped(mRS, mIndexType);
51 }
52
53
54 public static class Builder {
55 RenderScript mRS;
56
57 class Entry {
58 Type t;
59 Element e;
60 int size;
61 }
62
63 int mVertexTypeCount;
64 Entry[] mVertexTypes;
65 Entry mIndexType;
66 //Entry mBatchType;
67 Primitive mPrimitive;
68
69
70 public Builder(RenderScript rs) {
71 mRS = rs;
72 mVertexTypeCount = 0;
73 mVertexTypes = new Entry[16];
74 mIndexType = new Entry();
75 }
76
77 public int addVertexType(Type t) throws IllegalStateException {
78 if(mVertexTypeCount >= mVertexTypes.length) {
79 throw new IllegalStateException("Max vertex types exceeded.");
80 }
81
82 int addedIndex = mVertexTypeCount;
83 mVertexTypes[mVertexTypeCount] = new Entry();
84 mVertexTypes[mVertexTypeCount].t = t;
85 mVertexTypeCount++;
86 return addedIndex;
87 }
88
89 public int addVertexType(Element e, int size) throws IllegalStateException {
90 if(mVertexTypeCount >= mVertexTypes.length) {
91 throw new IllegalStateException("Max vertex types exceeded.");
92 }
93
94 int addedIndex = mVertexTypeCount;
95 mVertexTypes[mVertexTypeCount] = new Entry();
96 mVertexTypes[mVertexTypeCount].e = e;
97 mVertexTypes[mVertexTypeCount].size = size;
98 mVertexTypeCount++;
99 return addedIndex;
100 }
101
102 public void setIndexType(Type t) {
103 mIndexType.t = t;
104 mIndexType.e = null;
105 mIndexType.size = 0;
106 }
107
108 public void setIndexType(Element e, int size) {
109 mIndexType.t = null;
110 mIndexType.e = e;
111 mIndexType.size = size;
112 }
113
114 public void setPrimitive(Primitive p) {
115 mPrimitive = p;
116 }
117
118
119 Type newType(Element e, int size) {
120 Type.Builder tb = new Type.Builder(mRS, e);
121 tb.add(Dimension.X, size);
122 return tb.create();
123 }
124
125 static synchronized SimpleMesh internalCreate(RenderScript rs, Builder b) {
126 Type[] toDestroy = new Type[18];
127 int toDestroyCount = 0;
128
129 int indexID = 0;
130 if(b.mIndexType.t != null) {
131 indexID = b.mIndexType.t.mID;
132 } else if(b.mIndexType.size != 0) {
133 b.mIndexType.t = b.newType(b.mIndexType.e, b.mIndexType.size);
134 indexID = b.mIndexType.t.mID;
135 toDestroy[toDestroyCount++] = b.mIndexType.t;
136 }
137
138 int[] IDs = new int[b.mVertexTypeCount];
139 for(int ct=0; ct < b.mVertexTypeCount; ct++) {
140 if(b.mVertexTypes[ct].t != null) {
141 IDs[ct] = b.mVertexTypes[ct].t.mID;
142 } else {
143 b.mVertexTypes[ct].t = b.newType(b.mVertexTypes[ct].e, b.mVertexTypes[ct].size);
144 IDs[ct] = b.mVertexTypes[ct].t.mID;
145 toDestroy[toDestroyCount++] = b.mVertexTypes[ct].t;
146 }
147 }
148
149 int id = rs.nSimpleMeshCreate(0, indexID, IDs, b.mPrimitive.mID);
150 for(int ct=0; ct < toDestroyCount; ct++) {
151 toDestroy[ct].destroy();
152 }
153
154 return new SimpleMesh(id, rs);
155 }
156
157 public SimpleMesh create() {
158 Log.e("rs", "SimpleMesh create");
159 SimpleMesh sm = internalCreate(mRS, this);
160 sm.mVertexTypes = new Type[mVertexTypeCount];
161 for(int ct=0; ct < mVertexTypeCount; ct++) {
162 sm.mVertexTypes[ct] = mVertexTypes[ct].t;
163 }
164 sm.mIndexType = mIndexType.t;
165 sm.mPrimitive = mPrimitive;
166 return sm;
167 }
168 }
169
170}
171