blob: ad4cf6be5381f140a9edcbd71aa9769601e8f3d1 [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -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
Jason Sams43ee06852009-08-12 17:54:11 -070019import java.lang.reflect.Field;
20
Jason Samsb8c5a842009-07-31 20:40:47 -070021/**
22 * @hide
23 *
24 **/
25public class Type extends BaseObj {
Jason Sams768bc022009-09-21 19:41:04 -070026 int mDimX;
27 int mDimY;
28 int mDimZ;
29 boolean mDimLOD;
30 boolean mDimFaces;
31 int mElementCount;
Jason Sams1bada8c2009-08-09 17:01:55 -070032 Element mElement;
Jason Sams768bc022009-09-21 19:41:04 -070033
Jason Sams43ee06852009-08-12 17:54:11 -070034 private int mNativeCache;
35 Class mJavaClass;
Jason Sams1bada8c2009-08-09 17:01:55 -070036
37
Jason Sams768bc022009-09-21 19:41:04 -070038 public int getX() {
39 return mDimX;
40 }
41 public int getY() {
42 return mDimY;
43 }
44 public int getZ() {
45 return mDimZ;
46 }
47 public boolean getLOD() {
48 return mDimLOD;
49 }
50 public boolean getFaces() {
51 return mDimFaces;
52 }
53 public int getElementCount() {
54 return mElementCount;
55 }
56
57 void calcElementCount() {
58 boolean hasLod = getLOD();
59 int x = getX();
60 int y = getY();
61 int z = getZ();
62 int faces = 1;
63 if(getFaces()) {
64 faces = 6;
65 }
66 if(x == 0) {
67 x = 1;
68 }
69 if(y == 0) {
70 y = 1;
71 }
72 if(z == 0) {
73 z = 1;
74 }
75
76 int count = x * y * z * faces;
77 if(hasLod && (x > 1) && (y > 1) && (z > 1)) {
78 if(x > 1) {
79 x >>= 1;
80 }
81 if(y > 1) {
82 y >>= 1;
83 }
84 if(z > 1) {
85 z >>= 1;
86 }
87
88 count += x * y * z * faces;
89 }
90 mElementCount = count;
91 }
92
93
Jason Samsb8c5a842009-07-31 20:40:47 -070094 Type(int id, RenderScript rs) {
95 super(rs);
96 mID = id;
Jason Sams43ee06852009-08-12 17:54:11 -070097 mNativeCache = 0;
98 }
99
100 protected void finalize() throws Throwable {
Jason Sams1b52aae2009-08-12 18:50:44 -0700101 if(mNativeCache != 0) {
Jason Sams43ee06852009-08-12 17:54:11 -0700102 mRS.nTypeFinalDestroy(this);
103 mNativeCache = 0;
104 }
105 super.finalize();
Jason Samsb8c5a842009-07-31 20:40:47 -0700106 }
107
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700108 public static Type createFromClass(RenderScript rs, Class c, int size) {
Jason Sams43ee06852009-08-12 17:54:11 -0700109 Element e = Element.createFromClass(rs, c);
110 Builder b = new Builder(rs, e);
111 b.add(Dimension.X, size);
112 Type t = b.create();
113 e.destroy();
114
115 // native fields
116 {
117 Field[] fields = c.getFields();
118 int[] arTypes = new int[fields.length];
119 int[] arBits = new int[fields.length];
120
121 for(int ct=0; ct < fields.length; ct++) {
122 Field f = fields[ct];
123 Class fc = f.getType();
124 if(fc == int.class) {
125 arTypes[ct] = Element.DataType.SIGNED.mID;
126 arBits[ct] = 32;
127 } else if(fc == short.class) {
128 arTypes[ct] = Element.DataType.SIGNED.mID;
129 arBits[ct] = 16;
130 } else if(fc == byte.class) {
131 arTypes[ct] = Element.DataType.SIGNED.mID;
132 arBits[ct] = 8;
133 } else if(fc == float.class) {
134 arTypes[ct] = Element.DataType.FLOAT.mID;
135 arBits[ct] = 32;
136 } else {
137 throw new IllegalArgumentException("Unkown field type");
138 }
139 }
140 rs.nTypeSetupFields(t, arTypes, arBits, fields);
141 }
142 t.mJavaClass = c;
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700143 return t;
144 }
145
146 public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) {
147 Type t = createFromClass(rs, c, size);
Jason Sams43ee06852009-08-12 17:54:11 -0700148 t.setName(scriptName);
149 return t;
150 }
151
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700152
Jason Samsb8c5a842009-07-31 20:40:47 -0700153 public static class Builder {
154 RenderScript mRS;
Jason Sams22534172009-08-04 16:58:20 -0700155 Entry[] mEntries;
156 int mEntryCount;
157 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700158
Jason Sams22534172009-08-04 16:58:20 -0700159 class Entry {
160 Dimension mDim;
161 int mValue;
Jason Samsb8c5a842009-07-31 20:40:47 -0700162 }
163
Jason Sams22534172009-08-04 16:58:20 -0700164 public Builder(RenderScript rs, Element e) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700165 if(e.mID == 0) {
166 throw new IllegalArgumentException("Invalid element.");
167 }
168
Jason Sams22534172009-08-04 16:58:20 -0700169 mRS = rs;
170 mEntries = new Entry[4];
171 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700172 }
173
174 public void add(Dimension d, int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700175 if(value < 1) {
176 throw new IllegalArgumentException("Values of less than 1 for Dimensions are not valid.");
177 }
Jason Sams22534172009-08-04 16:58:20 -0700178 if(mEntries.length >= mEntryCount) {
179 Entry[] en = new Entry[mEntryCount + 8];
Romain Guy484d57f2009-08-19 12:10:03 -0700180 System.arraycopy(mEntries, 0, en, 0, mEntries.length);
Jason Sams22534172009-08-04 16:58:20 -0700181 mEntries = en;
182 }
183 mEntries[mEntryCount] = new Entry();
184 mEntries[mEntryCount].mDim = d;
185 mEntries[mEntryCount].mValue = value;
186 mEntryCount++;
187 }
188
189 static synchronized Type internalCreate(RenderScript rs, Builder b) {
190 rs.nTypeBegin(b.mElement.mID);
191 for (int ct=0; ct < b.mEntryCount; ct++) {
192 Entry en = b.mEntries[ct];
193 rs.nTypeAdd(en.mDim.mID, en.mValue);
194 }
195 int id = rs.nTypeCreate();
196 return new Type(id, rs);
Jason Samsb8c5a842009-07-31 20:40:47 -0700197 }
198
199 public Type create() {
Jason Sams1bada8c2009-08-09 17:01:55 -0700200 Type t = internalCreate(mRS, this);
201 t.mElement = mElement;
Jason Sams768bc022009-09-21 19:41:04 -0700202
Jason Sams1bada8c2009-08-09 17:01:55 -0700203 for(int ct=0; ct < mEntryCount; ct++) {
Jason Sams768bc022009-09-21 19:41:04 -0700204 if(mEntries[ct].mDim == Dimension.X) {
205 t.mDimX = mEntries[ct].mValue;
206 }
207 if(mEntries[ct].mDim == Dimension.Y) {
208 t.mDimY = mEntries[ct].mValue;
209 }
210 if(mEntries[ct].mDim == Dimension.Z) {
211 t.mDimZ = mEntries[ct].mValue;
212 }
213 if(mEntries[ct].mDim == Dimension.LOD) {
214 t.mDimLOD = mEntries[ct].mValue != 0;
215 }
216 if(mEntries[ct].mDim == Dimension.FACE) {
217 t.mDimFaces = mEntries[ct].mValue != 0;
218 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700219 }
Jason Sams768bc022009-09-21 19:41:04 -0700220 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700221 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700222 }
223 }
224
225}