blob: 4b007f2120e0c5d1cb7a6bae424b5283c8dfd922 [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;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070020import android.util.Log;
Jason Sams36e612a2009-07-31 16:26:13 -070021
22/**
23 * @hide
24 *
Jason Samsa1b13ed2010-11-12 14:58:37 -080025 * Element is the basic data type of RenderScript. An element can be of 2
26 * forms. Basic elements contain a single component of data. This can be of
27 * any of the legal RS types. Examples of basic element types.
28 * Single float value
29 * 4 element float vector
30 * single RGB-565 color
31 * single unsigned int 16
32 *
33 * Complex elements will contain a list of sub-elements and names. This in
34 * effect represents a structure of data. The fields can be accessed by name
35 * from a script or shader. The memory layout is defined and ordered. Data
36 * alignment is determinied by the most basic primitive type. i.e. a float4
37 * vector will be alligned to sizeof(float) and not sizeof(float4). The
38 * ordering of elements in memory will be the order in which they were added
39 * with each component aligned as necessary. No re-ordering will be done.
40 *
41 * The primary source of elements will be from scripts. A script that exports a
42 * bind point for a data structure will generate a RS element to represent the
43 * data exported by the script.
Jason Sams36e612a2009-07-31 16:26:13 -070044 **/
45public class Element extends BaseObj {
Jason Samsea84a7c2009-09-04 14:42:41 -070046 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080047 Element[] mElements;
48 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -070049 int[] mArraySizes;
Jason Sams36e612a2009-07-31 16:26:13 -070050
Jason Sams718cd1f2009-12-23 14:35:29 -080051 DataType mType;
52 DataKind mKind;
53 boolean mNormalized;
54 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070055
Jason Sams718cd1f2009-12-23 14:35:29 -080056 int getSizeBytes() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070057
Jason Samsa1b13ed2010-11-12 14:58:37 -080058
59 /**
60 * DataType represents the basic type information for a basic element. The
61 * naming convention follows. For numeric types its FLOAT, SIGNED, UNSIGNED
62 * followed by the _BITS where BITS is the size of the data. BOOLEAN is a
63 * true / false (1,0) represented in an 8 bit container. The UNSIGNED
64 * variants with multiple bit definitions are for packed graphical data
65 * formats and represents vectors with per vector member sizes which are
66 * treated as a single unit for packing and alignment purposes.
67 *
68 * MATRIX the three matrix types contain FLOAT_32 elements and are treated
69 * as 32 bits for alignment purposes.
70 *
71 * RS_* objects. 32 bit opaque handles.
72 */
Jason Sams36e612a2009-07-31 16:26:13 -070073 public enum DataType {
Jason Sams718cd1f2009-12-23 14:35:29 -080074 //FLOAT_16 (1, 2),
75 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -070076 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080077 SIGNED_8 (4, 1),
78 SIGNED_16 (5, 2),
79 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -070080 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080081 UNSIGNED_8 (8, 1),
82 UNSIGNED_16 (9, 2),
83 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -070084 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080085
Jason Samsf110d4b2010-06-21 17:42:41 -070086 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -080087
Jason Samsf110d4b2010-06-21 17:42:41 -070088 UNSIGNED_5_6_5 (13, 2),
89 UNSIGNED_5_5_5_1 (14, 2),
90 UNSIGNED_4_4_4_4 (15, 2),
91
Jason Sams1d45c472010-08-25 14:31:48 -070092 MATRIX_4X4 (16, 64),
93 MATRIX_3X3 (17, 36),
94 MATRIX_2X2 (18, 16),
95
96 RS_ELEMENT (1000, 4),
97 RS_TYPE (1001, 4),
98 RS_ALLOCATION (1002, 4),
99 RS_SAMPLER (1003, 4),
100 RS_SCRIPT (1004, 4),
101 RS_MESH (1005, 4),
102 RS_PROGRAM_FRAGMENT (1006, 4),
103 RS_PROGRAM_VERTEX (1007, 4),
104 RS_PROGRAM_RASTER (1008, 4),
105 RS_PROGRAM_STORE (1009, 4);
Jason Sams36e612a2009-07-31 16:26:13 -0700106
107 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800108 int mSize;
109 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700110 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800111 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700112 }
113 }
114
Jason Samsa1b13ed2010-11-12 14:58:37 -0800115 /**
116 * The special interpretation of the data if required. This is primarly
117 * useful for graphical data. USER indicates no special interpretation is
118 * expected. PIXEL is used in conjunction with the standard data types for
119 * representing texture formats.
120 */
Jason Sams36e612a2009-07-31 16:26:13 -0700121 public enum DataKind {
122 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800123
124 PIXEL_L (7),
125 PIXEL_A (8),
126 PIXEL_LA (9),
127 PIXEL_RGB (10),
128 PIXEL_RGBA (11);
Jason Sams36e612a2009-07-31 16:26:13 -0700129
130 int mID;
131 DataKind(int id) {
132 mID = id;
133 }
134 }
135
Jason Samsa1b13ed2010-11-12 14:58:37 -0800136 /**
137 * Return if a element is too complex for use as a data source for a Mesh or
138 * a Program.
139 *
140 * @return boolean
141 */
Jason Samsc1d62102010-11-04 14:32:19 -0700142 public boolean isComplex() {
143 if (mElements == null) {
144 return false;
145 }
146 for (int ct=0; ct < mElements.length; ct++) {
147 if (mElements[ct].mElements != null) {
148 return true;
149 }
150 }
151 return false;
152 }
153
Jason Samsa1b13ed2010-11-12 14:58:37 -0800154 /**
155 * Utility function for returning an Element containing a single Boolean.
156 *
157 * @param rs Context to which the element will belong.
158 *
159 * @return Element
160 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700161 public static Element BOOLEAN(RenderScript rs) {
162 if(rs.mElement_BOOLEAN == null) {
163 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
164 }
165 return rs.mElement_BOOLEAN;
166 }
167
Jason Samsa1b13ed2010-11-12 14:58:37 -0800168 /**
169 * Utility function for returning an Element containing a single UNSIGNED_8.
170 *
171 * @param rs Context to which the element will belong.
172 *
173 * @return Element
174 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700175 public static Element U8(RenderScript rs) {
176 if(rs.mElement_U8 == null) {
177 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800178 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700179 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800180 }
181
Jason Samsa1b13ed2010-11-12 14:58:37 -0800182 /**
183 * Utility function for returning an Element containing a single SIGNED_8.
184 *
185 * @param rs Context to which the element will belong.
186 *
187 * @return Element
188 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700189 public static Element I8(RenderScript rs) {
190 if(rs.mElement_I8 == null) {
191 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800192 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700193 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800194 }
195
Jason Samse29f3e72010-06-08 15:40:48 -0700196 public static Element U16(RenderScript rs) {
197 if(rs.mElement_U16 == null) {
198 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
199 }
200 return rs.mElement_U16;
201 }
202
203 public static Element I16(RenderScript rs) {
204 if(rs.mElement_I16 == null) {
205 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
206 }
207 return rs.mElement_I16;
208 }
209
Jason Sams8cb39de2010-06-01 15:47:01 -0700210 public static Element U32(RenderScript rs) {
211 if(rs.mElement_U32 == null) {
212 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800213 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700214 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800215 }
216
Jason Sams8cb39de2010-06-01 15:47:01 -0700217 public static Element I32(RenderScript rs) {
218 if(rs.mElement_I32 == null) {
219 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800220 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700221 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800222 }
223
Stephen Hines52d83632010-10-11 16:10:42 -0700224 public static Element U64(RenderScript rs) {
225 if(rs.mElement_U64 == null) {
226 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
227 }
228 return rs.mElement_U64;
229 }
230
Stephen Hinesef1dac22010-10-01 15:39:33 -0700231 public static Element I64(RenderScript rs) {
232 if(rs.mElement_I64 == null) {
233 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
234 }
235 return rs.mElement_I64;
236 }
237
Jason Sams8cb39de2010-06-01 15:47:01 -0700238 public static Element F32(RenderScript rs) {
239 if(rs.mElement_F32 == null) {
240 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800241 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700242 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800243 }
244
Stephen Hines02f417052010-09-30 15:19:22 -0700245 public static Element F64(RenderScript rs) {
246 if(rs.mElement_F64 == null) {
247 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
248 }
249 return rs.mElement_F64;
250 }
251
Jason Sams8cb39de2010-06-01 15:47:01 -0700252 public static Element ELEMENT(RenderScript rs) {
253 if(rs.mElement_ELEMENT == null) {
254 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700255 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700256 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700257 }
258
Jason Sams8cb39de2010-06-01 15:47:01 -0700259 public static Element TYPE(RenderScript rs) {
260 if(rs.mElement_TYPE == null) {
261 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700262 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700263 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700264 }
265
Jason Sams8cb39de2010-06-01 15:47:01 -0700266 public static Element ALLOCATION(RenderScript rs) {
267 if(rs.mElement_ALLOCATION == null) {
268 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700269 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700270 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700271 }
272
Jason Sams8cb39de2010-06-01 15:47:01 -0700273 public static Element SAMPLER(RenderScript rs) {
274 if(rs.mElement_SAMPLER == null) {
275 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700276 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700277 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700278 }
279
Jason Sams8cb39de2010-06-01 15:47:01 -0700280 public static Element SCRIPT(RenderScript rs) {
281 if(rs.mElement_SCRIPT == null) {
282 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700283 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700284 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700285 }
286
Jason Sams8cb39de2010-06-01 15:47:01 -0700287 public static Element MESH(RenderScript rs) {
288 if(rs.mElement_MESH == null) {
289 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700290 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700291 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700292 }
293
Jason Sams8cb39de2010-06-01 15:47:01 -0700294 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
295 if(rs.mElement_PROGRAM_FRAGMENT == null) {
296 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700297 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700298 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700299 }
300
Jason Sams8cb39de2010-06-01 15:47:01 -0700301 public static Element PROGRAM_VERTEX(RenderScript rs) {
302 if(rs.mElement_PROGRAM_VERTEX == null) {
303 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700304 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700305 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700306 }
307
Jason Sams8cb39de2010-06-01 15:47:01 -0700308 public static Element PROGRAM_RASTER(RenderScript rs) {
309 if(rs.mElement_PROGRAM_RASTER == null) {
310 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700311 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700312 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700313 }
314
Jason Sams8cb39de2010-06-01 15:47:01 -0700315 public static Element PROGRAM_STORE(RenderScript rs) {
316 if(rs.mElement_PROGRAM_STORE == null) {
317 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700318 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700319 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700320 }
321
322
Jason Sams718cd1f2009-12-23 14:35:29 -0800323 public static Element A_8(RenderScript rs) {
324 if(rs.mElement_A_8 == null) {
325 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
326 }
327 return rs.mElement_A_8;
328 }
329
330 public static Element RGB_565(RenderScript rs) {
331 if(rs.mElement_RGB_565 == null) {
332 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
333 }
334 return rs.mElement_RGB_565;
335 }
336
337 public static Element RGB_888(RenderScript rs) {
338 if(rs.mElement_RGB_888 == null) {
339 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
340 }
341 return rs.mElement_RGB_888;
342 }
343
344 public static Element RGBA_5551(RenderScript rs) {
345 if(rs.mElement_RGBA_5551 == null) {
346 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
347 }
348 return rs.mElement_RGBA_5551;
349 }
350
351 public static Element RGBA_4444(RenderScript rs) {
352 if(rs.mElement_RGBA_4444 == null) {
353 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
354 }
355 return rs.mElement_RGBA_4444;
356 }
357
358 public static Element RGBA_8888(RenderScript rs) {
359 if(rs.mElement_RGBA_8888 == null) {
360 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
361 }
362 return rs.mElement_RGBA_8888;
363 }
364
Jason Sams8cb39de2010-06-01 15:47:01 -0700365 public static Element F32_2(RenderScript rs) {
366 if(rs.mElement_FLOAT_2 == null) {
367 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800368 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700369 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800370 }
371
Jason Sams8cb39de2010-06-01 15:47:01 -0700372 public static Element F32_3(RenderScript rs) {
373 if(rs.mElement_FLOAT_3 == null) {
374 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800375 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700376 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800377 }
378
Jason Sams8cb39de2010-06-01 15:47:01 -0700379 public static Element F32_4(RenderScript rs) {
380 if(rs.mElement_FLOAT_4 == null) {
381 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800382 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700383 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800384 }
385
Jason Sams8cb39de2010-06-01 15:47:01 -0700386 public static Element U8_4(RenderScript rs) {
387 if(rs.mElement_UCHAR_4 == null) {
388 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800389 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700390 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800391 }
392
Jason Sams1d45c472010-08-25 14:31:48 -0700393 public static Element MATRIX_4X4(RenderScript rs) {
394 if(rs.mElement_MATRIX_4X4 == null) {
395 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
396 }
397 return rs.mElement_MATRIX_4X4;
398 }
399 public static Element MATRIX4X4(RenderScript rs) {
400 return MATRIX_4X4(rs);
401 }
402
403 public static Element MATRIX_3X3(RenderScript rs) {
404 if(rs.mElement_MATRIX_3X3 == null) {
405 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
406 }
407 return rs.mElement_MATRIX_4X4;
408 }
409
410 public static Element MATRIX_2X2(RenderScript rs) {
411 if(rs.mElement_MATRIX_2X2 == null) {
412 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
413 }
414 return rs.mElement_MATRIX_2X2;
415 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800416
Jason Sams70d4e502010-09-02 17:35:23 -0700417 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700418 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700419 mSize = 0;
Jason Sams718cd1f2009-12-23 14:35:29 -0800420 mElements = e;
421 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700422 mArraySizes = as;
Jason Sams718cd1f2009-12-23 14:35:29 -0800423 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700424 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800425 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800426 }
427
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700428 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
429 super(id, rs);
Jason Sams718cd1f2009-12-23 14:35:29 -0800430 mSize = dt.mSize * size;
431 mType = dt;
432 mKind = dk;
433 mNormalized = norm;
434 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700435 }
436
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700437 Element(int id, RenderScript rs) {
438 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700439 }
440
441 @Override
442 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800443 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700444
445 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
446 int[] dataBuffer = new int[5];
Jason Sams06d69de2010-11-09 17:11:40 -0800447 mRS.nElementGetNativeData(getID(), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700448
449 mNormalized = dataBuffer[2] == 1 ? true : false;
450 mVectorSize = dataBuffer[3];
451 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700452 for (DataType dt: DataType.values()) {
453 if(dt.mID == dataBuffer[0]){
454 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700455 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700456 }
457 }
458 for (DataKind dk: DataKind.values()) {
459 if(dk.mID == dataBuffer[1]){
460 mKind = dk;
461 }
462 }
463
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700464 int numSubElements = dataBuffer[4];
465 if(numSubElements > 0) {
466 mElements = new Element[numSubElements];
467 mElementNames = new String[numSubElements];
468
469 int[] subElementIds = new int[numSubElements];
Jason Sams06d69de2010-11-09 17:11:40 -0800470 mRS.nElementGetSubElements(getID(), subElementIds, mElementNames);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700471 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700472 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700473 mElements[i].updateFromNative();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700474 mSize += mElements[i].mSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700475 }
476 }
477
478 }
479
Jason Samsc1d62102010-11-04 14:32:19 -0700480 public void destroy() {
Jason Sams7ce033d2009-08-18 14:14:24 -0700481 super.destroy();
Jason Sams36e612a2009-07-31 16:26:13 -0700482 }
483
Jason Samsa1b13ed2010-11-12 14:58:37 -0800484 /**
485 * Create a custom Element of the specified DataType. The DataKind will be
486 * set to USER and the vector size to 1 indicating non-vector.
487 *
488 * @param rs The context associated with the new Element.
489 * @param dt The DataType for the new element.
490 * @return Element
491 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800492 public static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700493 DataKind dk = DataKind.USER;
494 boolean norm = false;
495 int vecSize = 1;
496 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
497 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800498 }
499
Jason Samsa1b13ed2010-11-12 14:58:37 -0800500 /**
501 * Create a custom vector element of the specified DataType and vector size.
502 * DataKind will be set to USER.
503 *
504 * @param rs The context associated with the new Element.
505 * @param dt The DataType for the new element.
506 * @param size Vector size for the new Element. Range 2-4 inclusive
507 * supported.
508 *
509 * @return Element
510 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800511 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800512 if (size < 2 || size > 4) {
Jason Samsc1d62102010-11-04 14:32:19 -0700513 throw new RSIllegalArgumentException("Vector size out of rance 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700514 }
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700515 DataKind dk = DataKind.USER;
516 boolean norm = false;
517 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
518 return new Element(id, rs, dt, dk, norm, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700519 }
520
Jason Samsa1b13ed2010-11-12 14:58:37 -0800521 /**
522 * Create a new pixel Element type. A matching DataType and DataKind must
523 * be provided. The DataType and DataKind must contain the same number of
524 * components. Vector size will be set to 1.
525 *
526 * @param rs The context associated with the new Element.
527 * @param dt The DataType for the new element.
528 * @param dk The DataKind to specify the mapping of each component in the
529 * DataType.
530 *
531 * @return Element
532 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800533 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800534 if (!(dk == DataKind.PIXEL_L ||
535 dk == DataKind.PIXEL_A ||
536 dk == DataKind.PIXEL_LA ||
537 dk == DataKind.PIXEL_RGB ||
538 dk == DataKind.PIXEL_RGBA)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700539 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800540 }
541 if (!(dt == DataType.UNSIGNED_8 ||
542 dt == DataType.UNSIGNED_5_6_5 ||
543 dt == DataType.UNSIGNED_4_4_4_4 ||
544 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700545 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800546 }
547 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700548 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800549 }
550 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700551 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800552 }
553 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700554 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800555 }
556
557 int size = 1;
558 if (dk == DataKind.PIXEL_LA) {
559 size = 2;
560 }
561 if (dk == DataKind.PIXEL_RGB) {
562 size = 3;
563 }
564 if (dk == DataKind.PIXEL_RGBA) {
565 size = 4;
566 }
567
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700568 boolean norm = true;
569 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
570 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800571 }
Jason Sams36e612a2009-07-31 16:26:13 -0700572
Jason Samsa1b13ed2010-11-12 14:58:37 -0800573 /**
574 * Builder class for producing complex elements with matching field and name
575 * pairs. The builder starts empty. The order in which elements are added
576 * is retained for the layout in memory.
577 *
578 */
Jason Sams36e612a2009-07-31 16:26:13 -0700579 public static class Builder {
580 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800581 Element[] mElements;
582 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700583 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800584 int mCount;
Jason Sams22534172009-08-04 16:58:20 -0700585
Jason Samsa1b13ed2010-11-12 14:58:37 -0800586 /**
587 * Create a builder object.
588 *
589 * @param rs
590 */
Jason Sams22534172009-08-04 16:58:20 -0700591 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700592 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800593 mCount = 0;
594 mElements = new Element[8];
595 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -0700596 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700597 }
598
Jason Samsa1b13ed2010-11-12 14:58:37 -0800599 /**
600 * Add an array of elements to this element.
601 *
602 * @param element
603 * @param name
604 * @param arraySize
605 */
Jason Sams70d4e502010-09-02 17:35:23 -0700606 public void add(Element element, String name, int arraySize) {
607 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -0700608 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -0700609 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800610 if(mCount == mElements.length) {
611 Element[] e = new Element[mCount + 8];
612 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -0700613 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -0800614 System.arraycopy(mElements, 0, e, 0, mCount);
615 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700616 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -0800617 mElements = e;
618 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -0700619 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -0700620 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800621 mElements[mCount] = element;
622 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -0700623 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -0800624 mCount++;
Jason Sams07ae4062009-08-27 20:23:34 -0700625 }
626
Jason Samsa1b13ed2010-11-12 14:58:37 -0800627 /**
628 * Add a single element to this Element.
629 *
630 * @param element
631 * @param name
632 */
Jason Sams70d4e502010-09-02 17:35:23 -0700633 public void add(Element element, String name) {
634 add(element, name, 1);
635 }
636
Jason Samsa1b13ed2010-11-12 14:58:37 -0800637 /**
638 * Create the element from this builder.
639 *
640 *
641 * @return Element
642 */
Jason Sams22534172009-08-04 16:58:20 -0700643 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800644 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800645 Element[] ein = new Element[mCount];
646 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -0700647 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -0800648 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
649 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700650 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700651
652 int[] ids = new int[ein.length];
653 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Sams06d69de2010-11-09 17:11:40 -0800654 ids[ct] = ein[ct].getID();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700655 }
Jason Sams70d4e502010-09-02 17:35:23 -0700656 int id = mRS.nElementCreate2(ids, sin, asin);
657 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -0700658 }
659 }
660
Jason Sams718cd1f2009-12-23 14:35:29 -0800661 static void initPredefined(RenderScript rs) {
662 int a8 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
663 DataKind.PIXEL_A.mID, true, 1);
664 int rgba4444 = rs.nElementCreate(DataType.UNSIGNED_4_4_4_4.mID,
665 DataKind.PIXEL_RGBA.mID, true, 4);
666 int rgba8888 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
667 DataKind.PIXEL_RGBA.mID, true, 4);
668 int rgb565 = rs.nElementCreate(DataType.UNSIGNED_5_6_5.mID,
669 DataKind.PIXEL_RGB.mID, true, 3);
670 rs.nInitElements(a8, rgba4444, rgba8888, rgb565);
671 }
Jason Sams36e612a2009-07-31 16:26:13 -0700672}
673