blob: 3061b1e20475a43c56855ce87f7a562e3bd4c55f [file] [log] [blame]
Jason Sams36e612a2009-07-31 16:26:13 -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;
Jason Sams36e612a2009-07-31 16:26:13 -070020
21/**
22 * @hide
23 *
24 **/
25public class Element extends BaseObj {
26 final int mPredefinedID;
27 final boolean mIsPredefined;
28
29 public static final Element USER_U8 = new Element(0);
30 public static final Element USER_I8 = new Element(1);
31 public static final Element USER_U16 = new Element(2);
32 public static final Element USER_I16 = new Element(3);
33 public static final Element USER_U32 = new Element(4);
34 public static final Element USER_I32 = new Element(5);
35 public static final Element USER_FLOAT = new Element(6);
36
37 public static final Element A_8 = new Element(7);
38 public static final Element RGB_565 = new Element(8);
39 public static final Element RGB_888 = new Element(11);
40 public static final Element RGBA_5551 = new Element(9);
41 public static final Element RGBA_4444 = new Element(10);
42 public static final Element RGBA_8888 = new Element(12);
43
44 public static final Element INDEX_16 = new Element(13);
45 public static final Element INDEX_32 = new Element(14);
46 public static final Element XY_F32 = new Element(15);
47 public static final Element XYZ_F32 = new Element(16);
48 public static final Element ST_XY_F32 = new Element(17);
49 public static final Element ST_XYZ_F32 = new Element(18);
50 public static final Element NORM_XYZ_F32 = new Element(19);
51 public static final Element NORM_ST_XYZ_F32 = new Element(20);
52
53 void initPredef(RenderScript rs) {
54 mID = rs.nElementGetPredefined(mPredefinedID);
55 }
56
57 static void init(RenderScript rs) {
58 USER_U8.initPredef(rs);
59 USER_I8.initPredef(rs);
60 USER_U16.initPredef(rs);
61 USER_I16.initPredef(rs);
62 USER_U32.initPredef(rs);
63 USER_I32.initPredef(rs);
64 USER_FLOAT.initPredef(rs);
65
66 A_8.initPredef(rs);
67 RGB_565.initPredef(rs);
68 RGB_888.initPredef(rs);
69 RGBA_5551.initPredef(rs);
70 RGBA_4444.initPredef(rs);
71 RGBA_8888.initPredef(rs);
72
73 INDEX_16.initPredef(rs);
74 INDEX_32.initPredef(rs);
75 XY_F32.initPredef(rs);
76 XYZ_F32.initPredef(rs);
77 ST_XY_F32.initPredef(rs);
78 ST_XYZ_F32.initPredef(rs);
79 NORM_XYZ_F32.initPredef(rs);
80 NORM_ST_XYZ_F32.initPredef(rs);
81 }
82
83
84 public enum DataType {
85 FLOAT (0),
86 UNSIGNED (1),
87 SIGNED (2);
88
89 int mID;
90 DataType(int id) {
91 mID = id;
92 }
93 }
94
95 public enum DataKind {
96 USER (0),
97 RED (1),
98 GREEN (2),
99 BLUE (3),
100 ALPHA (4),
101 LUMINANCE (5),
102 INTENSITY (6),
103 X (7),
104 Y (8),
105 Z (9),
106 W (10),
107 S (11),
108 T (12),
109 Q (13),
110 R (14),
111 NX (15),
112 NY (16),
113 NZ (17),
114 INDEX (18);
115
116 int mID;
117 DataKind(int id) {
118 mID = id;
119 }
120 }
121
122
123 Element(int predef) {
124 super(null);
125 mID = 0;
126 mPredefinedID = predef;
127 mIsPredefined = true;
128 }
129
130 Element(int id, RenderScript rs) {
131 super(rs);
132 mID = id;
133 mPredefinedID = 0;
134 mIsPredefined = false;
135 }
136
137 public void destroy() throws IllegalStateException {
138 if(mIsPredefined) {
139 throw new IllegalStateException("Attempting to destroy a predefined Element.");
140 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700141 if(mDestroyed) {
142 throw new IllegalStateException("Object already destroyed.");
143 }
144 mDestroyed = true;
Jason Sams36e612a2009-07-31 16:26:13 -0700145 mRS.nElementDestroy(mID);
Jason Sams36e612a2009-07-31 16:26:13 -0700146 }
147
Jason Sams43ee06852009-08-12 17:54:11 -0700148 public static Element createFromClass(RenderScript rs, Class c) {
149 Field[] fields = c.getFields();
150 Builder b = new Builder(rs);
Jason Sams36e612a2009-07-31 16:26:13 -0700151
Jason Sams43ee06852009-08-12 17:54:11 -0700152 for(Field f: fields) {
153 Class fc = f.getType();
154 if(fc == int.class) {
155 b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 32, f.getName());
156 } else if(fc == short.class) {
157 b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 16, f.getName());
158 } else if(fc == byte.class) {
159 b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 8, f.getName());
160 } else if(fc == float.class) {
161 b.add(Element.DataType.FLOAT, Element.DataKind.USER, false, 32, f.getName());
162 } else {
163 throw new IllegalArgumentException("Unkown field type");
164 }
165 }
166 return b.create();
167 }
Jason Sams36e612a2009-07-31 16:26:13 -0700168
169
170 public static class Builder {
171 RenderScript mRS;
Jason Sams22534172009-08-04 16:58:20 -0700172 Entry[] mEntries;
173 int mEntryCount;
Jason Sams36e612a2009-07-31 16:26:13 -0700174
Jason Sams22534172009-08-04 16:58:20 -0700175 private class Entry {
176 Element mElement;
177 Element.DataType mType;
178 Element.DataKind mKind;
179 boolean mIsNormalized;
180 int mBits;
Jason Sams43ee06852009-08-12 17:54:11 -0700181 String mName;
Jason Sams22534172009-08-04 16:58:20 -0700182 }
183
184 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700185 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700186 mEntryCount = 0;
187 mEntries = new Entry[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700188 }
189
Jason Sams22534172009-08-04 16:58:20 -0700190 void addEntry(Entry e) {
191 if(mEntries.length >= mEntryCount) {
192 Entry[] en = new Entry[mEntryCount + 8];
Romain Guy81e46402009-08-14 18:58:33 -0700193 System.arraycopy(mEntries, 0, en, 0, mEntries.length);
Jason Sams22534172009-08-04 16:58:20 -0700194 mEntries = en;
Jason Sams36e612a2009-07-31 16:26:13 -0700195 }
Jason Sams22534172009-08-04 16:58:20 -0700196 mEntries[mEntryCount] = e;
197 mEntryCount++;
Jason Sams36e612a2009-07-31 16:26:13 -0700198 }
199
Jason Sams22534172009-08-04 16:58:20 -0700200 public Builder add(Element e) throws IllegalArgumentException {
Jason Sams36e612a2009-07-31 16:26:13 -0700201 if(!e.mIsPredefined) {
202 throw new IllegalArgumentException("add requires a predefined Element.");
203 }
Jason Sams22534172009-08-04 16:58:20 -0700204 Entry en = new Entry();
205 en.mElement = e;
206 addEntry(en);
Jason Sams36e612a2009-07-31 16:26:13 -0700207 return this;
208 }
209
Jason Sams43ee06852009-08-12 17:54:11 -0700210 public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits, String name) {
Jason Sams22534172009-08-04 16:58:20 -0700211 Entry en = new Entry();
212 en.mType = dt;
213 en.mKind = dk;
214 en.mIsNormalized = isNormalized;
215 en.mBits = bits;
Jason Sams43ee06852009-08-12 17:54:11 -0700216 en.mName = name;
Jason Sams22534172009-08-04 16:58:20 -0700217 addEntry(en);
Jason Sams36e612a2009-07-31 16:26:13 -0700218 return this;
219 }
220
Jason Sams43ee06852009-08-12 17:54:11 -0700221 public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits) {
222 add(dt, dk, isNormalized, bits, null);
223 return this;
224 }
225
Jason Sams334ea0c2009-08-17 13:56:09 -0700226 public Builder addFloat(Element.DataKind dk) {
227 add(DataType.FLOAT, dk, false, 32, null);
228 return this;
229 }
230
231 public Builder addFloat(Element.DataKind dk, String name) {
232 add(DataType.FLOAT, dk, false, 32, name);
233 return this;
234 }
235
236 public Builder addFloatXY() {
237 add(DataType.FLOAT, DataKind.X, false, 32, null);
238 add(DataType.FLOAT, DataKind.Y, false, 32, null);
239 return this;
240 }
241
242 public Builder addFloatXYZ() {
243 add(DataType.FLOAT, DataKind.X, false, 32, null);
244 add(DataType.FLOAT, DataKind.Y, false, 32, null);
245 add(DataType.FLOAT, DataKind.Z, false, 32, null);
246 return this;
247 }
248
249 public Builder addFloatRGB() {
250 add(DataType.FLOAT, DataKind.RED, false, 32, null);
251 add(DataType.FLOAT, DataKind.GREEN, false, 32, null);
252 add(DataType.FLOAT, DataKind.BLUE, false, 32, null);
253 return this;
254 }
255
256 public Builder addFloatRGBA() {
257 add(DataType.FLOAT, DataKind.RED, false, 32, null);
258 add(DataType.FLOAT, DataKind.GREEN, false, 32, null);
259 add(DataType.FLOAT, DataKind.BLUE, false, 32, null);
260 add(DataType.FLOAT, DataKind.ALPHA, false, 32, null);
261 return this;
262 }
263
264 public Builder addUNorm8RGBA() {
265 add(DataType.UNSIGNED, DataKind.RED, true, 8, null);
266 add(DataType.UNSIGNED, DataKind.GREEN, true, 8, null);
267 add(DataType.UNSIGNED, DataKind.BLUE, true, 8, null);
268 add(DataType.UNSIGNED, DataKind.ALPHA, true, 8, null);
269 return this;
270 }
271
Jason Sams22534172009-08-04 16:58:20 -0700272 static synchronized Element internalCreate(RenderScript rs, Builder b) {
273 rs.nElementBegin();
274 for (int ct=0; ct < b.mEntryCount; ct++) {
275 Entry en = b.mEntries[ct];
276 if(en.mElement != null) {
277 rs.nElementAddPredefined(en.mElement.mPredefinedID);
278 } else {
279 int norm = 0;
280 if (en.mIsNormalized) {
281 norm = 1;
282 }
Jason Sams43ee06852009-08-12 17:54:11 -0700283 rs.nElementAdd(en.mKind.mID, en.mType.mID, norm, en.mBits, en.mName);
Jason Sams22534172009-08-04 16:58:20 -0700284 }
Jason Sams36e612a2009-07-31 16:26:13 -0700285 }
Jason Sams22534172009-08-04 16:58:20 -0700286 int id = rs.nElementCreate();
287 return new Element(id, rs);
Jason Sams36e612a2009-07-31 16:26:13 -0700288 }
289
Jason Sams22534172009-08-04 16:58:20 -0700290 public Element create() {
291 return internalCreate(mRS, this);
Jason Sams36e612a2009-07-31 16:26:13 -0700292 }
293 }
294
295}
296