blob: 62d3867ceec9e099e2c6ca5336434998e42ce7af [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
Jason Samse17964e2010-01-04 16:52:27 -080037 public Element getElement() {
38 return mElement;
39 }
Jason Sams1bada8c2009-08-09 17:01:55 -070040
Jason Sams768bc022009-09-21 19:41:04 -070041 public int getX() {
42 return mDimX;
43 }
44 public int getY() {
45 return mDimY;
46 }
47 public int getZ() {
48 return mDimZ;
49 }
50 public boolean getLOD() {
51 return mDimLOD;
52 }
53 public boolean getFaces() {
54 return mDimFaces;
55 }
56 public int getElementCount() {
57 return mElementCount;
58 }
59
60 void calcElementCount() {
61 boolean hasLod = getLOD();
62 int x = getX();
63 int y = getY();
64 int z = getZ();
65 int faces = 1;
66 if(getFaces()) {
67 faces = 6;
68 }
69 if(x == 0) {
70 x = 1;
71 }
72 if(y == 0) {
73 y = 1;
74 }
75 if(z == 0) {
76 z = 1;
77 }
78
79 int count = x * y * z * faces;
80 if(hasLod && (x > 1) && (y > 1) && (z > 1)) {
81 if(x > 1) {
82 x >>= 1;
83 }
84 if(y > 1) {
85 y >>= 1;
86 }
87 if(z > 1) {
88 z >>= 1;
89 }
90
91 count += x * y * z * faces;
92 }
93 mElementCount = count;
94 }
95
96
Jason Samsb8c5a842009-07-31 20:40:47 -070097 Type(int id, RenderScript rs) {
98 super(rs);
99 mID = id;
Jason Sams43ee06852009-08-12 17:54:11 -0700100 mNativeCache = 0;
101 }
102
103 protected void finalize() throws Throwable {
Jason Sams1b52aae2009-08-12 18:50:44 -0700104 if(mNativeCache != 0) {
Jason Sams43ee06852009-08-12 17:54:11 -0700105 mRS.nTypeFinalDestroy(this);
106 mNativeCache = 0;
107 }
108 super.finalize();
Jason Samsb8c5a842009-07-31 20:40:47 -0700109 }
110
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700111 public static Type createFromClass(RenderScript rs, Class c, int size) {
Jason Sams43ee06852009-08-12 17:54:11 -0700112 Element e = Element.createFromClass(rs, c);
113 Builder b = new Builder(rs, e);
114 b.add(Dimension.X, size);
115 Type t = b.create();
116 e.destroy();
117
118 // native fields
119 {
120 Field[] fields = c.getFields();
121 int[] arTypes = new int[fields.length];
122 int[] arBits = new int[fields.length];
123
124 for(int ct=0; ct < fields.length; ct++) {
125 Field f = fields[ct];
126 Class fc = f.getType();
127 if(fc == int.class) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800128 arTypes[ct] = Element.DataType.SIGNED_32.mID;
Jason Sams43ee06852009-08-12 17:54:11 -0700129 arBits[ct] = 32;
130 } else if(fc == short.class) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800131 arTypes[ct] = Element.DataType.SIGNED_16.mID;
Jason Sams43ee06852009-08-12 17:54:11 -0700132 arBits[ct] = 16;
133 } else if(fc == byte.class) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800134 arTypes[ct] = Element.DataType.SIGNED_8.mID;
Jason Sams43ee06852009-08-12 17:54:11 -0700135 arBits[ct] = 8;
136 } else if(fc == float.class) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800137 arTypes[ct] = Element.DataType.FLOAT_32.mID;
Jason Sams43ee06852009-08-12 17:54:11 -0700138 arBits[ct] = 32;
139 } else {
140 throw new IllegalArgumentException("Unkown field type");
141 }
142 }
143 rs.nTypeSetupFields(t, arTypes, arBits, fields);
144 }
145 t.mJavaClass = c;
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700146 return t;
147 }
148
149 public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) {
150 Type t = createFromClass(rs, c, size);
Jason Sams43ee06852009-08-12 17:54:11 -0700151 t.setName(scriptName);
152 return t;
153 }
154
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700155
Jason Samsb8c5a842009-07-31 20:40:47 -0700156 public static class Builder {
157 RenderScript mRS;
Jason Sams22534172009-08-04 16:58:20 -0700158 Entry[] mEntries;
159 int mEntryCount;
160 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700161
Jason Sams22534172009-08-04 16:58:20 -0700162 class Entry {
163 Dimension mDim;
164 int mValue;
Jason Samsb8c5a842009-07-31 20:40:47 -0700165 }
166
Jason Sams22534172009-08-04 16:58:20 -0700167 public Builder(RenderScript rs, Element e) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700168 if(e.mID == 0) {
169 throw new IllegalArgumentException("Invalid element.");
170 }
171
Jason Sams22534172009-08-04 16:58:20 -0700172 mRS = rs;
173 mEntries = new Entry[4];
174 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700175 }
176
177 public void add(Dimension d, int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700178 if(value < 1) {
179 throw new IllegalArgumentException("Values of less than 1 for Dimensions are not valid.");
180 }
Jason Sams22534172009-08-04 16:58:20 -0700181 if(mEntries.length >= mEntryCount) {
182 Entry[] en = new Entry[mEntryCount + 8];
Romain Guy484d57f2009-08-19 12:10:03 -0700183 System.arraycopy(mEntries, 0, en, 0, mEntries.length);
Jason Sams22534172009-08-04 16:58:20 -0700184 mEntries = en;
185 }
186 mEntries[mEntryCount] = new Entry();
187 mEntries[mEntryCount].mDim = d;
188 mEntries[mEntryCount].mValue = value;
189 mEntryCount++;
190 }
191
192 static synchronized Type internalCreate(RenderScript rs, Builder b) {
193 rs.nTypeBegin(b.mElement.mID);
194 for (int ct=0; ct < b.mEntryCount; ct++) {
195 Entry en = b.mEntries[ct];
196 rs.nTypeAdd(en.mDim.mID, en.mValue);
197 }
198 int id = rs.nTypeCreate();
199 return new Type(id, rs);
Jason Samsb8c5a842009-07-31 20:40:47 -0700200 }
201
202 public Type create() {
Jason Sams1bada8c2009-08-09 17:01:55 -0700203 Type t = internalCreate(mRS, this);
204 t.mElement = mElement;
Jason Sams768bc022009-09-21 19:41:04 -0700205
Jason Sams1bada8c2009-08-09 17:01:55 -0700206 for(int ct=0; ct < mEntryCount; ct++) {
Jason Sams768bc022009-09-21 19:41:04 -0700207 if(mEntries[ct].mDim == Dimension.X) {
208 t.mDimX = mEntries[ct].mValue;
209 }
210 if(mEntries[ct].mDim == Dimension.Y) {
211 t.mDimY = mEntries[ct].mValue;
212 }
213 if(mEntries[ct].mDim == Dimension.Z) {
214 t.mDimZ = mEntries[ct].mValue;
215 }
216 if(mEntries[ct].mDim == Dimension.LOD) {
217 t.mDimLOD = mEntries[ct].mValue != 0;
218 }
219 if(mEntries[ct].mDim == Dimension.FACE) {
220 t.mDimFaces = mEntries[ct].mValue != 0;
221 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700222 }
Jason Sams768bc022009-09-21 19:41:04 -0700223 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700224 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700225 }
226 }
227
228}