blob: 10ef05a378b11647a43d3d4e7c9581d30c91f5bf [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 {
Jason Samsea84a7c2009-09-04 14:42:41 -070026 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080027 Element[] mElements;
28 String[] mElementNames;
Jason Sams36e612a2009-07-31 16:26:13 -070029
Jason Sams718cd1f2009-12-23 14:35:29 -080030 DataType mType;
31 DataKind mKind;
32 boolean mNormalized;
33 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070034
Jason Sams718cd1f2009-12-23 14:35:29 -080035 int getSizeBytes() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070036
37 public enum DataType {
Jason Sams718cd1f2009-12-23 14:35:29 -080038 //FLOAT_16 (1, 2),
39 FLOAT_32 (2, 4),
40 //FLOAT_64 (3, 8),
41 SIGNED_8 (4, 1),
42 SIGNED_16 (5, 2),
43 SIGNED_32 (6, 4),
44 //SIGNED_64 (7, 8),
45 UNSIGNED_8 (8, 1),
46 UNSIGNED_16 (9, 2),
47 UNSIGNED_32 (10, 4),
48 //UNSIGNED_64 (11, 8),
49
50 UNSIGNED_5_6_5 (12, 2),
51 UNSIGNED_5_5_5_1 (13, 2),
52 UNSIGNED_4_4_4_4 (14, 2),
53
54 RS_ELEMENT (15, 4),
55 RS_TYPE (16, 4),
56 RS_ALLOCATION (17, 4),
57 RS_SAMPLER (18, 4),
58 RS_SCRIPT (19, 4),
59 RS_MESH (20, 4),
60 RS_PROGRAM_FRAGMENT (21, 4),
61 RS_PROGRAM_VERTEX (22, 4),
62 RS_PROGRAM_RASTER (23, 4),
63 RS_PROGRAM_STORE (24, 4);
Jason Sams36e612a2009-07-31 16:26:13 -070064
65 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -080066 int mSize;
67 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -070068 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -080069 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -070070 }
71 }
72
73 public enum DataKind {
74 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -080075 COLOR (1),
76 POSITION (2),
77 TEXTURE (3),
78 NORMAL (4),
79 INDEX (5),
80 POINT_SIZE(6),
81
82 PIXEL_L (7),
83 PIXEL_A (8),
84 PIXEL_LA (9),
85 PIXEL_RGB (10),
86 PIXEL_RGBA (11);
Jason Sams36e612a2009-07-31 16:26:13 -070087
88 int mID;
89 DataKind(int id) {
90 mID = id;
91 }
92 }
93
Jason Sams718cd1f2009-12-23 14:35:29 -080094 public static Element USER_U8(RenderScript rs) {
95 if(rs.mElement_USER_U8 == null) {
96 rs.mElement_USER_U8 = createUser(rs, DataType.UNSIGNED_8);
97 }
98 return rs.mElement_USER_U8;
99 }
100
101 public static Element USER_I8(RenderScript rs) {
102 if(rs.mElement_USER_I8 == null) {
103 rs.mElement_USER_I8 = createUser(rs, DataType.SIGNED_8);
104 }
105 return rs.mElement_USER_I8;
106 }
107
108 public static Element USER_U32(RenderScript rs) {
109 if(rs.mElement_USER_U32 == null) {
110 rs.mElement_USER_U32 = createUser(rs, DataType.UNSIGNED_32);
111 }
112 return rs.mElement_USER_U32;
113 }
114
115 public static Element USER_I32(RenderScript rs) {
116 if(rs.mElement_USER_I32 == null) {
117 rs.mElement_USER_I32 = createUser(rs, DataType.SIGNED_32);
118 }
119 return rs.mElement_USER_I32;
120 }
121
122 public static Element USER_F32(RenderScript rs) {
123 if(rs.mElement_USER_F32 == null) {
124 rs.mElement_USER_F32 = createUser(rs, DataType.FLOAT_32);
125 }
126 return rs.mElement_USER_F32;
127 }
128
129 public static Element A_8(RenderScript rs) {
130 if(rs.mElement_A_8 == null) {
131 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
132 }
133 return rs.mElement_A_8;
134 }
135
136 public static Element RGB_565(RenderScript rs) {
137 if(rs.mElement_RGB_565 == null) {
138 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
139 }
140 return rs.mElement_RGB_565;
141 }
142
143 public static Element RGB_888(RenderScript rs) {
144 if(rs.mElement_RGB_888 == null) {
145 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
146 }
147 return rs.mElement_RGB_888;
148 }
149
150 public static Element RGBA_5551(RenderScript rs) {
151 if(rs.mElement_RGBA_5551 == null) {
152 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
153 }
154 return rs.mElement_RGBA_5551;
155 }
156
157 public static Element RGBA_4444(RenderScript rs) {
158 if(rs.mElement_RGBA_4444 == null) {
159 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
160 }
161 return rs.mElement_RGBA_4444;
162 }
163
164 public static Element RGBA_8888(RenderScript rs) {
165 if(rs.mElement_RGBA_8888 == null) {
166 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
167 }
168 return rs.mElement_RGBA_8888;
169 }
170
171 public static Element INDEX_16(RenderScript rs) {
172 if(rs.mElement_INDEX_16 == null) {
173 rs.mElement_INDEX_16 = createIndex(rs);
174 }
175 return rs.mElement_INDEX_16;
176 }
177
178 public static Element ATTRIB_POSITION_2(RenderScript rs) {
179 if(rs.mElement_POSITION_2 == null) {
180 rs.mElement_POSITION_2 = createAttrib(rs, DataType.FLOAT_32, DataKind.POSITION, 2);
181 }
182 return rs.mElement_POSITION_2;
183 }
184
185 public static Element ATTRIB_POSITION_3(RenderScript rs) {
186 if(rs.mElement_POSITION_3 == null) {
187 rs.mElement_POSITION_3 = createAttrib(rs, DataType.FLOAT_32, DataKind.POSITION, 3);
188 }
189 return rs.mElement_POSITION_3;
190 }
191
192 public static Element ATTRIB_TEXTURE_2(RenderScript rs) {
193 if(rs.mElement_TEXTURE_2 == null) {
194 rs.mElement_TEXTURE_2 = createAttrib(rs, DataType.FLOAT_32, DataKind.TEXTURE, 2);
195 }
196 return rs.mElement_TEXTURE_2;
197 }
198
199 public static Element ATTRIB_NORMAL_3(RenderScript rs) {
200 if(rs.mElement_NORMAL_3 == null) {
201 rs.mElement_NORMAL_3 = createAttrib(rs, DataType.FLOAT_32, DataKind.NORMAL, 3);
202 }
203 return rs.mElement_NORMAL_3;
204 }
205
206 public static Element ATTRIB_COLOR_U8_4(RenderScript rs) {
207 if(rs.mElement_COLOR_U8_4 == null) {
208 rs.mElement_COLOR_U8_4 = createAttrib(rs, DataType.UNSIGNED_8, DataKind.COLOR, 4);
209 }
210 return rs.mElement_COLOR_U8_4;
211 }
212
213 public static Element ATTRIB_COLOR_F32_4(RenderScript rs) {
214 if(rs.mElement_COLOR_F32_4 == null) {
215 rs.mElement_COLOR_F32_4 = createAttrib(rs, DataType.FLOAT_32, DataKind.COLOR, 4);
216 }
217 return rs.mElement_COLOR_F32_4;
218 }
219
220 Element(RenderScript rs, Element[] e, String[] n) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700221 super(rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700222 mSize = 0;
Jason Sams718cd1f2009-12-23 14:35:29 -0800223 mElements = e;
224 mElementNames = n;
225 int[] ids = new int[mElements.length];
226 for (int ct = 0; ct < mElements.length; ct++ ) {
227 mSize += mElements[ct].mSize;
228 ids[ct] = mElements[ct].mID;
229 }
230 mID = rs.nElementCreate2(ids, mElementNames);
231 }
232
233 Element(RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
234 super(rs);
235 mSize = dt.mSize * size;
236 mType = dt;
237 mKind = dk;
238 mNormalized = norm;
239 mVectorSize = size;
240 mID = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Jason Sams36e612a2009-07-31 16:26:13 -0700241 }
242
243 public void destroy() throws IllegalStateException {
Jason Sams7ce033d2009-08-18 14:14:24 -0700244 super.destroy();
Jason Sams36e612a2009-07-31 16:26:13 -0700245 }
246
Jason Sams43ee06852009-08-12 17:54:11 -0700247 public static Element createFromClass(RenderScript rs, Class c) {
Jason Sams771bebb2009-12-07 12:40:12 -0800248 rs.validate();
Jason Sams43ee06852009-08-12 17:54:11 -0700249 Field[] fields = c.getFields();
250 Builder b = new Builder(rs);
Jason Sams36e612a2009-07-31 16:26:13 -0700251
Jason Sams43ee06852009-08-12 17:54:11 -0700252 for(Field f: fields) {
253 Class fc = f.getType();
254 if(fc == int.class) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800255 b.add(createUser(rs, DataType.SIGNED_32), f.getName());
Jason Sams43ee06852009-08-12 17:54:11 -0700256 } else if(fc == short.class) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800257 b.add(createUser(rs, DataType.SIGNED_16), f.getName());
Jason Sams43ee06852009-08-12 17:54:11 -0700258 } else if(fc == byte.class) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800259 b.add(createUser(rs, DataType.SIGNED_8), f.getName());
Jason Sams43ee06852009-08-12 17:54:11 -0700260 } else if(fc == float.class) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800261 b.add(createUser(rs, DataType.FLOAT_32), f.getName());
Jason Sams43ee06852009-08-12 17:54:11 -0700262 } else {
263 throw new IllegalArgumentException("Unkown field type");
264 }
265 }
266 return b.create();
267 }
Jason Sams36e612a2009-07-31 16:26:13 -0700268
Jason Sams718cd1f2009-12-23 14:35:29 -0800269
270 /////////////////////////////////////////
271 public static Element createUser(RenderScript rs, DataType dt) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800272 return new Element(rs, dt, DataKind.USER, false, 1);
273 }
274
275 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800276 if (size < 2 || size > 4) {
277 throw new IllegalArgumentException("Bad size");
Jason Samsea84a7c2009-09-04 14:42:41 -0700278 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800279 return new Element(rs, dt, DataKind.USER, false, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700280 }
281
Jason Sams718cd1f2009-12-23 14:35:29 -0800282 public static Element createIndex(RenderScript rs) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800283 return new Element(rs, DataType.UNSIGNED_16, DataKind.INDEX, false, 1);
Jason Samsea84a7c2009-09-04 14:42:41 -0700284 }
285
Jason Sams718cd1f2009-12-23 14:35:29 -0800286 public static Element createAttrib(RenderScript rs, DataType dt, DataKind dk, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800287 if (!(dt == DataType.FLOAT_32 ||
288 dt == DataType.UNSIGNED_8 ||
289 dt == DataType.UNSIGNED_16 ||
290 dt == DataType.UNSIGNED_32 ||
291 dt == DataType.SIGNED_8 ||
292 dt == DataType.SIGNED_16 ||
293 dt == DataType.SIGNED_32)) {
294 throw new IllegalArgumentException("Unsupported DataType");
295 }
296
297 if (!(dk == DataKind.COLOR ||
298 dk == DataKind.POSITION ||
299 dk == DataKind.TEXTURE ||
300 dk == DataKind.NORMAL ||
Jason Samsa09a6e12010-01-06 11:57:52 -0800301 dk == DataKind.POINT_SIZE ||
302 dk == DataKind.USER)) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800303 throw new IllegalArgumentException("Unsupported DataKind");
304 }
305
306 if (dk == DataKind.COLOR &&
307 ((dt != DataType.FLOAT_32 && dt != DataType.UNSIGNED_8) ||
308 size < 3 || size > 4)) {
309 throw new IllegalArgumentException("Bad combo");
310 }
311 if (dk == DataKind.POSITION && (size < 1 || size > 4)) {
312 throw new IllegalArgumentException("Bad combo");
313 }
314 if (dk == DataKind.TEXTURE &&
315 (dt != DataType.FLOAT_32 || size < 1 || size > 4)) {
316 throw new IllegalArgumentException("Bad combo");
317 }
318 if (dk == DataKind.NORMAL &&
319 (dt != DataType.FLOAT_32 || size != 3)) {
320 throw new IllegalArgumentException("Bad combo");
321 }
322 if (dk == DataKind.POINT_SIZE &&
323 (dt != DataType.FLOAT_32 || size != 1)) {
324 throw new IllegalArgumentException("Bad combo");
325 }
326
327 boolean norm = false;
328 if (dk == DataKind.COLOR && dt == DataType.UNSIGNED_8) {
329 norm = true;
330 }
331
332 return new Element(rs, dt, dk, norm, size);
333 }
334
335 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800336 if (!(dk == DataKind.PIXEL_L ||
337 dk == DataKind.PIXEL_A ||
338 dk == DataKind.PIXEL_LA ||
339 dk == DataKind.PIXEL_RGB ||
340 dk == DataKind.PIXEL_RGBA)) {
341 throw new IllegalArgumentException("Unsupported DataKind");
342 }
343 if (!(dt == DataType.UNSIGNED_8 ||
344 dt == DataType.UNSIGNED_5_6_5 ||
345 dt == DataType.UNSIGNED_4_4_4_4 ||
346 dt == DataType.UNSIGNED_5_5_5_1)) {
347 throw new IllegalArgumentException("Unsupported DataType");
348 }
349 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
350 throw new IllegalArgumentException("Bad kind and type combo");
351 }
352 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
353 throw new IllegalArgumentException("Bad kind and type combo");
354 }
355 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
356 throw new IllegalArgumentException("Bad kind and type combo");
357 }
358
359 int size = 1;
360 if (dk == DataKind.PIXEL_LA) {
361 size = 2;
362 }
363 if (dk == DataKind.PIXEL_RGB) {
364 size = 3;
365 }
366 if (dk == DataKind.PIXEL_RGBA) {
367 size = 4;
368 }
369
370 return new Element(rs, dt, dk, true, size);
371 }
Jason Sams36e612a2009-07-31 16:26:13 -0700372
373 public static class Builder {
374 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800375 Element[] mElements;
376 String[] mElementNames;
377 int mCount;
Jason Sams22534172009-08-04 16:58:20 -0700378
379 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700380 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800381 mCount = 0;
382 mElements = new Element[8];
383 mElementNames = new String[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700384 }
385
Jason Sams718cd1f2009-12-23 14:35:29 -0800386 public void add(Element element, String name) {
387 if(mCount == mElements.length) {
388 Element[] e = new Element[mCount + 8];
389 String[] s = new String[mCount + 8];
390 System.arraycopy(mElements, 0, e, 0, mCount);
391 System.arraycopy(mElementNames, 0, s, 0, mCount);
392 mElements = e;
393 mElementNames = s;
Jason Sams36e612a2009-07-31 16:26:13 -0700394 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800395 mElements[mCount] = element;
396 mElementNames[mCount] = name;
397 mCount++;
Jason Sams07ae4062009-08-27 20:23:34 -0700398 }
399
Jason Sams22534172009-08-04 16:58:20 -0700400 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800401 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800402 Element[] ein = new Element[mCount];
403 String[] sin = new String[mCount];
404 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
405 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
406 return new Element(mRS, ein, sin);
Jason Sams36e612a2009-07-31 16:26:13 -0700407 }
408 }
409
Jason Sams718cd1f2009-12-23 14:35:29 -0800410 static void initPredefined(RenderScript rs) {
411 int a8 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
412 DataKind.PIXEL_A.mID, true, 1);
413 int rgba4444 = rs.nElementCreate(DataType.UNSIGNED_4_4_4_4.mID,
414 DataKind.PIXEL_RGBA.mID, true, 4);
415 int rgba8888 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
416 DataKind.PIXEL_RGBA.mID, true, 4);
417 int rgb565 = rs.nElementCreate(DataType.UNSIGNED_5_6_5.mID,
418 DataKind.PIXEL_RGB.mID, true, 3);
419 rs.nInitElements(a8, rgba4444, rgba8888, rgb565);
420 }
Jason Sams36e612a2009-07-31 16:26:13 -0700421}
422