blob: f8017946ebcc4538dd934807f7ca5108034e3acc [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
Jason Samsa70f4162010-03-26 15:33:42 -0700129 public static Element USER_ELEMENT(RenderScript rs) {
130 if(rs.mElement_USER_ELEMENT == null) {
131 rs.mElement_USER_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
132 }
133 return rs.mElement_USER_ELEMENT;
134 }
135
136 public static Element USER_TYPE(RenderScript rs) {
137 if(rs.mElement_USER_TYPE == null) {
138 rs.mElement_USER_TYPE = createUser(rs, DataType.RS_TYPE);
139 }
140 return rs.mElement_USER_TYPE;
141 }
142
143 public static Element USER_ALLOCATION(RenderScript rs) {
144 if(rs.mElement_USER_ALLOCATION == null) {
145 rs.mElement_USER_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
146 }
147 return rs.mElement_USER_ALLOCATION;
148 }
149
150 public static Element USER_SAMPLER(RenderScript rs) {
151 if(rs.mElement_USER_SAMPLER == null) {
152 rs.mElement_USER_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
153 }
154 return rs.mElement_USER_SAMPLER;
155 }
156
157 public static Element USER_SCRIPT(RenderScript rs) {
158 if(rs.mElement_USER_SCRIPT == null) {
159 rs.mElement_USER_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
160 }
161 return rs.mElement_USER_SCRIPT;
162 }
163
164 public static Element USER_MESH(RenderScript rs) {
165 if(rs.mElement_USER_MESH == null) {
166 rs.mElement_USER_MESH = createUser(rs, DataType.RS_MESH);
167 }
168 return rs.mElement_USER_MESH;
169 }
170
171 public static Element USER_PROGRAM_FRAGMENT(RenderScript rs) {
172 if(rs.mElement_USER_PROGRAM_FRAGMENT == null) {
173 rs.mElement_USER_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
174 }
175 return rs.mElement_USER_PROGRAM_FRAGMENT;
176 }
177
178 public static Element USER_PROGRAM_VERTEX(RenderScript rs) {
179 if(rs.mElement_USER_PROGRAM_VERTEX == null) {
180 rs.mElement_USER_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
181 }
182 return rs.mElement_USER_PROGRAM_VERTEX;
183 }
184
185 public static Element USER_PROGRAM_RASTER(RenderScript rs) {
186 if(rs.mElement_USER_PROGRAM_RASTER == null) {
187 rs.mElement_USER_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
188 }
189 return rs.mElement_USER_PROGRAM_RASTER;
190 }
191
192 public static Element USER_PROGRAM_STORE(RenderScript rs) {
193 if(rs.mElement_USER_PROGRAM_STORE == null) {
194 rs.mElement_USER_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
195 }
196 return rs.mElement_USER_PROGRAM_STORE;
197 }
198
199
Jason Sams718cd1f2009-12-23 14:35:29 -0800200 public static Element A_8(RenderScript rs) {
201 if(rs.mElement_A_8 == null) {
202 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
203 }
204 return rs.mElement_A_8;
205 }
206
207 public static Element RGB_565(RenderScript rs) {
208 if(rs.mElement_RGB_565 == null) {
209 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
210 }
211 return rs.mElement_RGB_565;
212 }
213
214 public static Element RGB_888(RenderScript rs) {
215 if(rs.mElement_RGB_888 == null) {
216 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
217 }
218 return rs.mElement_RGB_888;
219 }
220
221 public static Element RGBA_5551(RenderScript rs) {
222 if(rs.mElement_RGBA_5551 == null) {
223 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
224 }
225 return rs.mElement_RGBA_5551;
226 }
227
228 public static Element RGBA_4444(RenderScript rs) {
229 if(rs.mElement_RGBA_4444 == null) {
230 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
231 }
232 return rs.mElement_RGBA_4444;
233 }
234
235 public static Element RGBA_8888(RenderScript rs) {
236 if(rs.mElement_RGBA_8888 == null) {
237 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
238 }
239 return rs.mElement_RGBA_8888;
240 }
241
242 public static Element INDEX_16(RenderScript rs) {
243 if(rs.mElement_INDEX_16 == null) {
244 rs.mElement_INDEX_16 = createIndex(rs);
245 }
246 return rs.mElement_INDEX_16;
247 }
248
249 public static Element ATTRIB_POSITION_2(RenderScript rs) {
250 if(rs.mElement_POSITION_2 == null) {
251 rs.mElement_POSITION_2 = createAttrib(rs, DataType.FLOAT_32, DataKind.POSITION, 2);
252 }
253 return rs.mElement_POSITION_2;
254 }
255
256 public static Element ATTRIB_POSITION_3(RenderScript rs) {
257 if(rs.mElement_POSITION_3 == null) {
258 rs.mElement_POSITION_3 = createAttrib(rs, DataType.FLOAT_32, DataKind.POSITION, 3);
259 }
260 return rs.mElement_POSITION_3;
261 }
262
263 public static Element ATTRIB_TEXTURE_2(RenderScript rs) {
264 if(rs.mElement_TEXTURE_2 == null) {
265 rs.mElement_TEXTURE_2 = createAttrib(rs, DataType.FLOAT_32, DataKind.TEXTURE, 2);
266 }
267 return rs.mElement_TEXTURE_2;
268 }
269
270 public static Element ATTRIB_NORMAL_3(RenderScript rs) {
271 if(rs.mElement_NORMAL_3 == null) {
272 rs.mElement_NORMAL_3 = createAttrib(rs, DataType.FLOAT_32, DataKind.NORMAL, 3);
273 }
274 return rs.mElement_NORMAL_3;
275 }
276
277 public static Element ATTRIB_COLOR_U8_4(RenderScript rs) {
278 if(rs.mElement_COLOR_U8_4 == null) {
279 rs.mElement_COLOR_U8_4 = createAttrib(rs, DataType.UNSIGNED_8, DataKind.COLOR, 4);
280 }
281 return rs.mElement_COLOR_U8_4;
282 }
283
284 public static Element ATTRIB_COLOR_F32_4(RenderScript rs) {
285 if(rs.mElement_COLOR_F32_4 == null) {
286 rs.mElement_COLOR_F32_4 = createAttrib(rs, DataType.FLOAT_32, DataKind.COLOR, 4);
287 }
288 return rs.mElement_COLOR_F32_4;
289 }
290
291 Element(RenderScript rs, Element[] e, String[] n) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700292 super(rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700293 mSize = 0;
Jason Sams718cd1f2009-12-23 14:35:29 -0800294 mElements = e;
295 mElementNames = n;
296 int[] ids = new int[mElements.length];
297 for (int ct = 0; ct < mElements.length; ct++ ) {
298 mSize += mElements[ct].mSize;
299 ids[ct] = mElements[ct].mID;
300 }
301 mID = rs.nElementCreate2(ids, mElementNames);
302 }
303
304 Element(RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
305 super(rs);
306 mSize = dt.mSize * size;
307 mType = dt;
308 mKind = dk;
309 mNormalized = norm;
310 mVectorSize = size;
311 mID = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Jason Sams36e612a2009-07-31 16:26:13 -0700312 }
313
314 public void destroy() throws IllegalStateException {
Jason Sams7ce033d2009-08-18 14:14:24 -0700315 super.destroy();
Jason Sams36e612a2009-07-31 16:26:13 -0700316 }
317
Jason Sams718cd1f2009-12-23 14:35:29 -0800318 /////////////////////////////////////////
319 public static Element createUser(RenderScript rs, DataType dt) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800320 return new Element(rs, dt, DataKind.USER, false, 1);
321 }
322
323 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800324 if (size < 2 || size > 4) {
325 throw new IllegalArgumentException("Bad size");
Jason Samsea84a7c2009-09-04 14:42:41 -0700326 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800327 return new Element(rs, dt, DataKind.USER, false, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700328 }
329
Jason Sams718cd1f2009-12-23 14:35:29 -0800330 public static Element createIndex(RenderScript rs) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800331 return new Element(rs, DataType.UNSIGNED_16, DataKind.INDEX, false, 1);
Jason Samsea84a7c2009-09-04 14:42:41 -0700332 }
333
Jason Sams718cd1f2009-12-23 14:35:29 -0800334 public static Element createAttrib(RenderScript rs, DataType dt, DataKind dk, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800335 if (!(dt == DataType.FLOAT_32 ||
336 dt == DataType.UNSIGNED_8 ||
337 dt == DataType.UNSIGNED_16 ||
338 dt == DataType.UNSIGNED_32 ||
339 dt == DataType.SIGNED_8 ||
340 dt == DataType.SIGNED_16 ||
341 dt == DataType.SIGNED_32)) {
342 throw new IllegalArgumentException("Unsupported DataType");
343 }
344
345 if (!(dk == DataKind.COLOR ||
346 dk == DataKind.POSITION ||
347 dk == DataKind.TEXTURE ||
348 dk == DataKind.NORMAL ||
Jason Samsa09a6e12010-01-06 11:57:52 -0800349 dk == DataKind.POINT_SIZE ||
350 dk == DataKind.USER)) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800351 throw new IllegalArgumentException("Unsupported DataKind");
352 }
353
354 if (dk == DataKind.COLOR &&
355 ((dt != DataType.FLOAT_32 && dt != DataType.UNSIGNED_8) ||
356 size < 3 || size > 4)) {
357 throw new IllegalArgumentException("Bad combo");
358 }
359 if (dk == DataKind.POSITION && (size < 1 || size > 4)) {
360 throw new IllegalArgumentException("Bad combo");
361 }
362 if (dk == DataKind.TEXTURE &&
363 (dt != DataType.FLOAT_32 || size < 1 || size > 4)) {
364 throw new IllegalArgumentException("Bad combo");
365 }
366 if (dk == DataKind.NORMAL &&
367 (dt != DataType.FLOAT_32 || size != 3)) {
368 throw new IllegalArgumentException("Bad combo");
369 }
370 if (dk == DataKind.POINT_SIZE &&
371 (dt != DataType.FLOAT_32 || size != 1)) {
372 throw new IllegalArgumentException("Bad combo");
373 }
374
375 boolean norm = false;
376 if (dk == DataKind.COLOR && dt == DataType.UNSIGNED_8) {
377 norm = true;
378 }
379
380 return new Element(rs, dt, dk, norm, size);
381 }
382
383 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800384 if (!(dk == DataKind.PIXEL_L ||
385 dk == DataKind.PIXEL_A ||
386 dk == DataKind.PIXEL_LA ||
387 dk == DataKind.PIXEL_RGB ||
388 dk == DataKind.PIXEL_RGBA)) {
389 throw new IllegalArgumentException("Unsupported DataKind");
390 }
391 if (!(dt == DataType.UNSIGNED_8 ||
392 dt == DataType.UNSIGNED_5_6_5 ||
393 dt == DataType.UNSIGNED_4_4_4_4 ||
394 dt == DataType.UNSIGNED_5_5_5_1)) {
395 throw new IllegalArgumentException("Unsupported DataType");
396 }
397 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
398 throw new IllegalArgumentException("Bad kind and type combo");
399 }
400 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
401 throw new IllegalArgumentException("Bad kind and type combo");
402 }
403 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
404 throw new IllegalArgumentException("Bad kind and type combo");
405 }
406
407 int size = 1;
408 if (dk == DataKind.PIXEL_LA) {
409 size = 2;
410 }
411 if (dk == DataKind.PIXEL_RGB) {
412 size = 3;
413 }
414 if (dk == DataKind.PIXEL_RGBA) {
415 size = 4;
416 }
417
418 return new Element(rs, dt, dk, true, size);
419 }
Jason Sams36e612a2009-07-31 16:26:13 -0700420
421 public static class Builder {
422 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800423 Element[] mElements;
424 String[] mElementNames;
425 int mCount;
Jason Sams22534172009-08-04 16:58:20 -0700426
427 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700428 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800429 mCount = 0;
430 mElements = new Element[8];
431 mElementNames = new String[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700432 }
433
Jason Sams718cd1f2009-12-23 14:35:29 -0800434 public void add(Element element, String name) {
435 if(mCount == mElements.length) {
436 Element[] e = new Element[mCount + 8];
437 String[] s = new String[mCount + 8];
438 System.arraycopy(mElements, 0, e, 0, mCount);
439 System.arraycopy(mElementNames, 0, s, 0, mCount);
440 mElements = e;
441 mElementNames = s;
Jason Sams36e612a2009-07-31 16:26:13 -0700442 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800443 mElements[mCount] = element;
444 mElementNames[mCount] = name;
445 mCount++;
Jason Sams07ae4062009-08-27 20:23:34 -0700446 }
447
Jason Sams22534172009-08-04 16:58:20 -0700448 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800449 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800450 Element[] ein = new Element[mCount];
451 String[] sin = new String[mCount];
452 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
453 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
454 return new Element(mRS, ein, sin);
Jason Sams36e612a2009-07-31 16:26:13 -0700455 }
456 }
457
Jason Sams718cd1f2009-12-23 14:35:29 -0800458 static void initPredefined(RenderScript rs) {
459 int a8 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
460 DataKind.PIXEL_A.mID, true, 1);
461 int rgba4444 = rs.nElementCreate(DataType.UNSIGNED_4_4_4_4.mID,
462 DataKind.PIXEL_RGBA.mID, true, 4);
463 int rgba8888 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
464 DataKind.PIXEL_RGBA.mID, true, 4);
465 int rgb565 = rs.nElementCreate(DataType.UNSIGNED_5_6_5.mID,
466 DataKind.PIXEL_RGB.mID, true, 3);
467 rs.nInitElements(a8, rgba4444, rgba8888, rgb565);
468 }
Jason Sams36e612a2009-07-31 16:26:13 -0700469}
470