blob: 110eceb0be131eaef1826e46481d3496b86df79f [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/**
Robert Ly11518ac2011-02-09 13:57:06 -080023 * <p>The most basic data type. An element represents one cell of a memory allocation.
Alex Sakhartchouk34769772011-02-28 16:01:28 -080024 * Element is the basic data type of Renderscript. An element can be of two forms: Basic elements or Complex forms.
Robert Ly11518ac2011-02-09 13:57:06 -080025 * Examples of basic elements are:</p>
26 * <ul>
27 * <li>Single float value</li>
28 * <li>4 element float vector</li>
29 * <li>single RGB-565 color</li>
30 * <li>single unsigned int 16</li>
31 * </ul>
Alex Sakhartchouk34769772011-02-28 16:01:28 -080032 * <p>Complex elements contain a list of sub-elements and names that
Robert Ly11518ac2011-02-09 13:57:06 -080033 * represents a structure of data. The fields can be accessed by name
34 * from a script or shader. The memory layout is defined and ordered. Data
Stephen Hinesf257e512011-06-14 14:54:29 -070035 * alignment is determined by the most basic primitive type. i.e. a float4
36 * vector will be aligned to sizeof(float) and not sizeof(float4). The
Jason Samsa1b13ed2010-11-12 14:58:37 -080037 * ordering of elements in memory will be the order in which they were added
Robert Ly11518ac2011-02-09 13:57:06 -080038 * with each component aligned as necessary. No re-ordering will be done.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080039 *
Robert Ly11518ac2011-02-09 13:57:06 -080040 * <p>The primary source of elements are from scripts. A script that exports a
41 * bind point for a data structure generates a Renderscript element to represent the
42 * data exported by the script. The other common source of elements is from bitmap formats.</p>
Jason Sams36e612a2009-07-31 16:26:13 -070043 **/
44public class Element extends BaseObj {
Jason Samsea84a7c2009-09-04 14:42:41 -070045 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080046 Element[] mElements;
47 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -070048 int[] mArraySizes;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070049 int[] mOffsetInBytes;
Jason Sams36e612a2009-07-31 16:26:13 -070050
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -080051 int[] mVisibleElementMap;
52
Jason Sams718cd1f2009-12-23 14:35:29 -080053 DataType mType;
54 DataKind mKind;
55 boolean mNormalized;
56 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070057
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -080058 private void updateVisibleSubElements() {
59 if (mElements == null) {
60 return;
61 }
62
63 int noPaddingFieldCount = 0;
64 int fieldCount = mElementNames.length;
65 // Find out how many elements are not padding
66 for (int ct = 0; ct < fieldCount; ct ++) {
67 if (mElementNames[ct].charAt(0) != '#') {
68 noPaddingFieldCount ++;
69 }
70 }
71 mVisibleElementMap = new int[noPaddingFieldCount];
72
73 // Make a map that points us at non-padding elements
74 for (int ct = 0, ctNoPadding = 0; ct < fieldCount; ct ++) {
75 if (mElementNames[ct].charAt(0) != '#') {
76 mVisibleElementMap[ctNoPadding ++] = ct;
77 }
78 }
79 }
80
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070081 /**
82 * @hide
83 * @return element size in bytes
84 */
85 public int getSizeBytes() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070086
Jason Samsa1b13ed2010-11-12 14:58:37 -080087
88 /**
89 * DataType represents the basic type information for a basic element. The
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -080090 * naming convention follows. For numeric types it is FLOAT,
91 * SIGNED, or UNSIGNED followed by the _BITS where BITS is the
92 * size of the data. BOOLEAN is a true / false (1,0)
93 * represented in an 8 bit container. The UNSIGNED variants
94 * with multiple bit definitions are for packed graphical data
95 * formats and represent vectors with per vector member sizes
96 * which are treated as a single unit for packing and alignment
97 * purposes.
Jason Samsa1b13ed2010-11-12 14:58:37 -080098 *
99 * MATRIX the three matrix types contain FLOAT_32 elements and are treated
100 * as 32 bits for alignment purposes.
101 *
102 * RS_* objects. 32 bit opaque handles.
103 */
Jason Sams36e612a2009-07-31 16:26:13 -0700104 public enum DataType {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800105 /**
106 * @hide
107 * new enum
108 */
109 NONE (0, 0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800110 //FLOAT_16 (1, 2),
111 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -0700112 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800113 SIGNED_8 (4, 1),
114 SIGNED_16 (5, 2),
115 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -0700116 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800117 UNSIGNED_8 (8, 1),
118 UNSIGNED_16 (9, 2),
119 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -0700120 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800121
Jason Samsf110d4b2010-06-21 17:42:41 -0700122 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -0800123
Jason Samsf110d4b2010-06-21 17:42:41 -0700124 UNSIGNED_5_6_5 (13, 2),
125 UNSIGNED_5_5_5_1 (14, 2),
126 UNSIGNED_4_4_4_4 (15, 2),
127
Jason Sams1d45c472010-08-25 14:31:48 -0700128 MATRIX_4X4 (16, 64),
129 MATRIX_3X3 (17, 36),
130 MATRIX_2X2 (18, 16),
131
132 RS_ELEMENT (1000, 4),
133 RS_TYPE (1001, 4),
134 RS_ALLOCATION (1002, 4),
135 RS_SAMPLER (1003, 4),
136 RS_SCRIPT (1004, 4),
137 RS_MESH (1005, 4),
138 RS_PROGRAM_FRAGMENT (1006, 4),
139 RS_PROGRAM_VERTEX (1007, 4),
140 RS_PROGRAM_RASTER (1008, 4),
141 RS_PROGRAM_STORE (1009, 4);
Jason Sams36e612a2009-07-31 16:26:13 -0700142
143 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800144 int mSize;
145 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700146 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800147 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700148 }
149 }
150
Jason Samsa1b13ed2010-11-12 14:58:37 -0800151 /**
152 * The special interpretation of the data if required. This is primarly
153 * useful for graphical data. USER indicates no special interpretation is
154 * expected. PIXEL is used in conjunction with the standard data types for
155 * representing texture formats.
156 */
Jason Sams36e612a2009-07-31 16:26:13 -0700157 public enum DataKind {
158 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800159
160 PIXEL_L (7),
161 PIXEL_A (8),
162 PIXEL_LA (9),
163 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700164 PIXEL_RGBA (11),
165 PIXEL_DEPTH (12);
Jason Sams36e612a2009-07-31 16:26:13 -0700166
167 int mID;
168 DataKind(int id) {
169 mID = id;
170 }
171 }
172
Jason Samsa1b13ed2010-11-12 14:58:37 -0800173 /**
174 * Return if a element is too complex for use as a data source for a Mesh or
175 * a Program.
176 *
177 * @return boolean
178 */
Jason Samsc1d62102010-11-04 14:32:19 -0700179 public boolean isComplex() {
180 if (mElements == null) {
181 return false;
182 }
183 for (int ct=0; ct < mElements.length; ct++) {
184 if (mElements[ct].mElements != null) {
185 return true;
186 }
187 }
188 return false;
189 }
190
Jason Samsa1b13ed2010-11-12 14:58:37 -0800191 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700192 * @hide
193 * @return number of sub-elements in this element
194 */
195 public int getSubElementCount() {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800196 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700197 return 0;
198 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800199 return mVisibleElementMap.length;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700200 }
201
202 /**
203 * @hide
204 * @param index index of the sub-element to return
205 * @return sub-element in this element at given index
206 */
207 public Element getSubElement(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800208 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700209 throw new RSIllegalArgumentException("Element contains no sub-elements");
210 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800211 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700212 throw new RSIllegalArgumentException("Illegal sub-element index");
213 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800214 return mElements[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700215 }
216
217 /**
218 * @hide
219 * @param index index of the sub-element
220 * @return sub-element in this element at given index
221 */
222 public String getSubElementName(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800223 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700224 throw new RSIllegalArgumentException("Element contains no sub-elements");
225 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800226 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700227 throw new RSIllegalArgumentException("Illegal sub-element index");
228 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800229 return mElementNames[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700230 }
231
232 /**
233 * @hide
234 * @param index index of the sub-element
235 * @return array size of sub-element in this element at given index
236 */
237 public int getSubElementArraySize(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800238 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700239 throw new RSIllegalArgumentException("Element contains no sub-elements");
240 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800241 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700242 throw new RSIllegalArgumentException("Illegal sub-element index");
243 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800244 return mArraySizes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700245 }
246
247 /**
248 * @hide
249 * @param index index of the sub-element
250 * @return offset in bytes of sub-element in this element at given index
251 */
252 public int getSubElementOffsetBytes(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800253 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700254 throw new RSIllegalArgumentException("Element contains no sub-elements");
255 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800256 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700257 throw new RSIllegalArgumentException("Illegal sub-element index");
258 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800259 return mOffsetInBytes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700260 }
261
262 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800263 * @hide
264 * @return element data type
265 */
266 public DataType getDataType() {
267 return mType;
268 }
269
270 /**
271 * @hide
272 * @return element data kind
273 */
274 public DataKind getDataKind() {
275 return mKind;
276 }
277
278 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800279 * Utility function for returning an Element containing a single Boolean.
280 *
281 * @param rs Context to which the element will belong.
282 *
283 * @return Element
284 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700285 public static Element BOOLEAN(RenderScript rs) {
286 if(rs.mElement_BOOLEAN == null) {
287 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
288 }
289 return rs.mElement_BOOLEAN;
290 }
291
Jason Samsa1b13ed2010-11-12 14:58:37 -0800292 /**
293 * Utility function for returning an Element containing a single UNSIGNED_8.
294 *
295 * @param rs Context to which the element will belong.
296 *
297 * @return Element
298 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700299 public static Element U8(RenderScript rs) {
300 if(rs.mElement_U8 == null) {
301 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800302 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700303 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800304 }
305
Jason Samsa1b13ed2010-11-12 14:58:37 -0800306 /**
307 * Utility function for returning an Element containing a single SIGNED_8.
308 *
309 * @param rs Context to which the element will belong.
310 *
311 * @return Element
312 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700313 public static Element I8(RenderScript rs) {
314 if(rs.mElement_I8 == null) {
315 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800316 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700317 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800318 }
319
Jason Samse29f3e72010-06-08 15:40:48 -0700320 public static Element U16(RenderScript rs) {
321 if(rs.mElement_U16 == null) {
322 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
323 }
324 return rs.mElement_U16;
325 }
326
327 public static Element I16(RenderScript rs) {
328 if(rs.mElement_I16 == null) {
329 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
330 }
331 return rs.mElement_I16;
332 }
333
Jason Sams8cb39de2010-06-01 15:47:01 -0700334 public static Element U32(RenderScript rs) {
335 if(rs.mElement_U32 == null) {
336 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800337 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700338 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800339 }
340
Jason Sams8cb39de2010-06-01 15:47:01 -0700341 public static Element I32(RenderScript rs) {
342 if(rs.mElement_I32 == null) {
343 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800344 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700345 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800346 }
347
Stephen Hines52d83632010-10-11 16:10:42 -0700348 public static Element U64(RenderScript rs) {
349 if(rs.mElement_U64 == null) {
350 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
351 }
352 return rs.mElement_U64;
353 }
354
Stephen Hinesef1dac22010-10-01 15:39:33 -0700355 public static Element I64(RenderScript rs) {
356 if(rs.mElement_I64 == null) {
357 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
358 }
359 return rs.mElement_I64;
360 }
361
Jason Sams8cb39de2010-06-01 15:47:01 -0700362 public static Element F32(RenderScript rs) {
363 if(rs.mElement_F32 == null) {
364 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800365 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700366 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800367 }
368
Stephen Hines02f417052010-09-30 15:19:22 -0700369 public static Element F64(RenderScript rs) {
370 if(rs.mElement_F64 == null) {
371 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
372 }
373 return rs.mElement_F64;
374 }
375
Jason Sams8cb39de2010-06-01 15:47:01 -0700376 public static Element ELEMENT(RenderScript rs) {
377 if(rs.mElement_ELEMENT == null) {
378 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700379 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700380 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700381 }
382
Jason Sams8cb39de2010-06-01 15:47:01 -0700383 public static Element TYPE(RenderScript rs) {
384 if(rs.mElement_TYPE == null) {
385 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700386 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700387 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700388 }
389
Jason Sams8cb39de2010-06-01 15:47:01 -0700390 public static Element ALLOCATION(RenderScript rs) {
391 if(rs.mElement_ALLOCATION == null) {
392 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700393 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700394 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700395 }
396
Jason Sams8cb39de2010-06-01 15:47:01 -0700397 public static Element SAMPLER(RenderScript rs) {
398 if(rs.mElement_SAMPLER == null) {
399 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700400 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700401 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700402 }
403
Jason Sams8cb39de2010-06-01 15:47:01 -0700404 public static Element SCRIPT(RenderScript rs) {
405 if(rs.mElement_SCRIPT == null) {
406 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700407 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700408 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700409 }
410
Jason Sams8cb39de2010-06-01 15:47:01 -0700411 public static Element MESH(RenderScript rs) {
412 if(rs.mElement_MESH == null) {
413 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700414 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700415 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700416 }
417
Jason Sams8cb39de2010-06-01 15:47:01 -0700418 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
419 if(rs.mElement_PROGRAM_FRAGMENT == null) {
420 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700421 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700422 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700423 }
424
Jason Sams8cb39de2010-06-01 15:47:01 -0700425 public static Element PROGRAM_VERTEX(RenderScript rs) {
426 if(rs.mElement_PROGRAM_VERTEX == null) {
427 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700428 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700429 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700430 }
431
Jason Sams8cb39de2010-06-01 15:47:01 -0700432 public static Element PROGRAM_RASTER(RenderScript rs) {
433 if(rs.mElement_PROGRAM_RASTER == null) {
434 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700435 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700436 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700437 }
438
Jason Sams8cb39de2010-06-01 15:47:01 -0700439 public static Element PROGRAM_STORE(RenderScript rs) {
440 if(rs.mElement_PROGRAM_STORE == null) {
441 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700442 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700443 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700444 }
445
446
Jason Sams718cd1f2009-12-23 14:35:29 -0800447 public static Element A_8(RenderScript rs) {
448 if(rs.mElement_A_8 == null) {
449 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
450 }
451 return rs.mElement_A_8;
452 }
453
454 public static Element RGB_565(RenderScript rs) {
455 if(rs.mElement_RGB_565 == null) {
456 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
457 }
458 return rs.mElement_RGB_565;
459 }
460
461 public static Element RGB_888(RenderScript rs) {
462 if(rs.mElement_RGB_888 == null) {
463 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
464 }
465 return rs.mElement_RGB_888;
466 }
467
468 public static Element RGBA_5551(RenderScript rs) {
469 if(rs.mElement_RGBA_5551 == null) {
470 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
471 }
472 return rs.mElement_RGBA_5551;
473 }
474
475 public static Element RGBA_4444(RenderScript rs) {
476 if(rs.mElement_RGBA_4444 == null) {
477 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
478 }
479 return rs.mElement_RGBA_4444;
480 }
481
482 public static Element RGBA_8888(RenderScript rs) {
483 if(rs.mElement_RGBA_8888 == null) {
484 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
485 }
486 return rs.mElement_RGBA_8888;
487 }
488
Jason Sams8cb39de2010-06-01 15:47:01 -0700489 public static Element F32_2(RenderScript rs) {
490 if(rs.mElement_FLOAT_2 == null) {
491 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800492 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700493 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800494 }
495
Jason Sams8cb39de2010-06-01 15:47:01 -0700496 public static Element F32_3(RenderScript rs) {
497 if(rs.mElement_FLOAT_3 == null) {
498 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800499 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700500 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800501 }
502
Jason Sams8cb39de2010-06-01 15:47:01 -0700503 public static Element F32_4(RenderScript rs) {
504 if(rs.mElement_FLOAT_4 == null) {
505 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800506 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700507 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800508 }
509
Stephen Hines836c4a52011-06-01 14:38:10 -0700510 public static Element F64_2(RenderScript rs) {
511 if(rs.mElement_DOUBLE_2 == null) {
512 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
513 }
514 return rs.mElement_DOUBLE_2;
515 }
516
517 public static Element F64_3(RenderScript rs) {
518 if(rs.mElement_DOUBLE_3 == null) {
519 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
520 }
521 return rs.mElement_DOUBLE_3;
522 }
523
524 public static Element F64_4(RenderScript rs) {
525 if(rs.mElement_DOUBLE_4 == null) {
526 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
527 }
528 return rs.mElement_DOUBLE_4;
529 }
530
531 public static Element U8_2(RenderScript rs) {
532 if(rs.mElement_UCHAR_2 == null) {
533 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
534 }
535 return rs.mElement_UCHAR_2;
536 }
537
538 public static Element U8_3(RenderScript rs) {
539 if(rs.mElement_UCHAR_3 == null) {
540 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
541 }
542 return rs.mElement_UCHAR_3;
543 }
544
Jason Sams8cb39de2010-06-01 15:47:01 -0700545 public static Element U8_4(RenderScript rs) {
546 if(rs.mElement_UCHAR_4 == null) {
547 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800548 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700549 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800550 }
551
Stephen Hines836c4a52011-06-01 14:38:10 -0700552 public static Element I8_2(RenderScript rs) {
553 if(rs.mElement_CHAR_2 == null) {
554 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
555 }
556 return rs.mElement_CHAR_2;
557 }
558
559 public static Element I8_3(RenderScript rs) {
560 if(rs.mElement_CHAR_3 == null) {
561 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
562 }
563 return rs.mElement_CHAR_3;
564 }
565
566 public static Element I8_4(RenderScript rs) {
567 if(rs.mElement_CHAR_4 == null) {
568 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
569 }
570 return rs.mElement_CHAR_4;
571 }
572
573 public static Element U16_2(RenderScript rs) {
574 if(rs.mElement_USHORT_2 == null) {
575 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
576 }
577 return rs.mElement_USHORT_2;
578 }
579
580 public static Element U16_3(RenderScript rs) {
581 if(rs.mElement_USHORT_3 == null) {
582 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
583 }
584 return rs.mElement_USHORT_3;
585 }
586
587 public static Element U16_4(RenderScript rs) {
588 if(rs.mElement_USHORT_4 == null) {
589 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
590 }
591 return rs.mElement_USHORT_4;
592 }
593
594 public static Element I16_2(RenderScript rs) {
595 if(rs.mElement_SHORT_2 == null) {
596 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
597 }
598 return rs.mElement_SHORT_2;
599 }
600
601 public static Element I16_3(RenderScript rs) {
602 if(rs.mElement_SHORT_3 == null) {
603 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
604 }
605 return rs.mElement_SHORT_3;
606 }
607
608 public static Element I16_4(RenderScript rs) {
609 if(rs.mElement_SHORT_4 == null) {
610 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
611 }
612 return rs.mElement_SHORT_4;
613 }
614
615 public static Element U32_2(RenderScript rs) {
616 if(rs.mElement_UINT_2 == null) {
617 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
618 }
619 return rs.mElement_UINT_2;
620 }
621
622 public static Element U32_3(RenderScript rs) {
623 if(rs.mElement_UINT_3 == null) {
624 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
625 }
626 return rs.mElement_UINT_3;
627 }
628
629 public static Element U32_4(RenderScript rs) {
630 if(rs.mElement_UINT_4 == null) {
631 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
632 }
633 return rs.mElement_UINT_4;
634 }
635
636 public static Element I32_2(RenderScript rs) {
637 if(rs.mElement_INT_2 == null) {
638 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
639 }
640 return rs.mElement_INT_2;
641 }
642
643 public static Element I32_3(RenderScript rs) {
644 if(rs.mElement_INT_3 == null) {
645 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
646 }
647 return rs.mElement_INT_3;
648 }
649
650 public static Element I32_4(RenderScript rs) {
651 if(rs.mElement_INT_4 == null) {
652 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
653 }
654 return rs.mElement_INT_4;
655 }
656
657 public static Element U64_2(RenderScript rs) {
658 if(rs.mElement_ULONG_2 == null) {
659 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
660 }
661 return rs.mElement_ULONG_2;
662 }
663
664 public static Element U64_3(RenderScript rs) {
665 if(rs.mElement_ULONG_3 == null) {
666 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
667 }
668 return rs.mElement_ULONG_3;
669 }
670
671 public static Element U64_4(RenderScript rs) {
672 if(rs.mElement_ULONG_4 == null) {
673 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
674 }
675 return rs.mElement_ULONG_4;
676 }
677
678 public static Element I64_2(RenderScript rs) {
679 if(rs.mElement_LONG_2 == null) {
680 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
681 }
682 return rs.mElement_LONG_2;
683 }
684
685 public static Element I64_3(RenderScript rs) {
686 if(rs.mElement_LONG_3 == null) {
687 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
688 }
689 return rs.mElement_LONG_3;
690 }
691
692 public static Element I64_4(RenderScript rs) {
693 if(rs.mElement_LONG_4 == null) {
694 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
695 }
696 return rs.mElement_LONG_4;
697 }
698
Jason Sams1d45c472010-08-25 14:31:48 -0700699 public static Element MATRIX_4X4(RenderScript rs) {
700 if(rs.mElement_MATRIX_4X4 == null) {
701 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
702 }
703 return rs.mElement_MATRIX_4X4;
704 }
705 public static Element MATRIX4X4(RenderScript rs) {
706 return MATRIX_4X4(rs);
707 }
708
709 public static Element MATRIX_3X3(RenderScript rs) {
710 if(rs.mElement_MATRIX_3X3 == null) {
711 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
712 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800713 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700714 }
715
716 public static Element MATRIX_2X2(RenderScript rs) {
717 if(rs.mElement_MATRIX_2X2 == null) {
718 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
719 }
720 return rs.mElement_MATRIX_2X2;
721 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800722
Jason Sams70d4e502010-09-02 17:35:23 -0700723 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700724 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700725 mSize = 0;
Jason Sams718cd1f2009-12-23 14:35:29 -0800726 mElements = e;
727 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700728 mArraySizes = as;
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800729 mType = DataType.NONE;
730 mKind = DataKind.USER;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700731 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800732 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700733 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700734 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800735 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800736 updateVisibleSubElements();
Jason Sams718cd1f2009-12-23 14:35:29 -0800737 }
738
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700739 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
740 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800741 if ((dt != DataType.UNSIGNED_5_6_5) &&
742 (dt != DataType.UNSIGNED_4_4_4_4) &&
743 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800744 if (size == 3) {
745 mSize = dt.mSize * 4;
746 } else {
747 mSize = dt.mSize * size;
748 }
Jason Sams252c0782011-01-11 17:42:52 -0800749 } else {
750 mSize = dt.mSize;
751 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800752 mType = dt;
753 mKind = dk;
754 mNormalized = norm;
755 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700756 }
757
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700758 Element(int id, RenderScript rs) {
759 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700760 }
761
762 @Override
763 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800764 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700765
766 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
767 int[] dataBuffer = new int[5];
Jason Sams06d69de2010-11-09 17:11:40 -0800768 mRS.nElementGetNativeData(getID(), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700769
770 mNormalized = dataBuffer[2] == 1 ? true : false;
771 mVectorSize = dataBuffer[3];
772 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700773 for (DataType dt: DataType.values()) {
774 if(dt.mID == dataBuffer[0]){
775 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700776 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700777 }
778 }
779 for (DataKind dk: DataKind.values()) {
780 if(dk.mID == dataBuffer[1]){
781 mKind = dk;
782 }
783 }
784
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700785 int numSubElements = dataBuffer[4];
786 if(numSubElements > 0) {
787 mElements = new Element[numSubElements];
788 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700789 mArraySizes = new int[numSubElements];
790 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700791
792 int[] subElementIds = new int[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700793 mRS.nElementGetSubElements(getID(), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700794 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700795 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700796 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700797 mOffsetInBytes[i] = mSize;
798 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700799 }
800 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800801 updateVisibleSubElements();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700802 }
803
Jason Samsa1b13ed2010-11-12 14:58:37 -0800804 /**
805 * Create a custom Element of the specified DataType. The DataKind will be
806 * set to USER and the vector size to 1 indicating non-vector.
807 *
808 * @param rs The context associated with the new Element.
809 * @param dt The DataType for the new element.
810 * @return Element
811 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800812 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700813 DataKind dk = DataKind.USER;
814 boolean norm = false;
815 int vecSize = 1;
816 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
817 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800818 }
819
Jason Samsa1b13ed2010-11-12 14:58:37 -0800820 /**
821 * Create a custom vector element of the specified DataType and vector size.
822 * DataKind will be set to USER.
823 *
824 * @param rs The context associated with the new Element.
825 * @param dt The DataType for the new element.
826 * @param size Vector size for the new Element. Range 2-4 inclusive
827 * supported.
828 *
829 * @return Element
830 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800831 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800832 if (size < 2 || size > 4) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800833 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700834 }
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700835 DataKind dk = DataKind.USER;
836 boolean norm = false;
837 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
838 return new Element(id, rs, dt, dk, norm, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700839 }
840
Jason Samsa1b13ed2010-11-12 14:58:37 -0800841 /**
842 * Create a new pixel Element type. A matching DataType and DataKind must
843 * be provided. The DataType and DataKind must contain the same number of
844 * components. Vector size will be set to 1.
845 *
846 * @param rs The context associated with the new Element.
847 * @param dt The DataType for the new element.
848 * @param dk The DataKind to specify the mapping of each component in the
849 * DataType.
850 *
851 * @return Element
852 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800853 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800854 if (!(dk == DataKind.PIXEL_L ||
855 dk == DataKind.PIXEL_A ||
856 dk == DataKind.PIXEL_LA ||
857 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700858 dk == DataKind.PIXEL_RGBA ||
859 dk == DataKind.PIXEL_DEPTH)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700860 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800861 }
862 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700863 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800864 dt == DataType.UNSIGNED_5_6_5 ||
865 dt == DataType.UNSIGNED_4_4_4_4 ||
866 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700867 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800868 }
869 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700870 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800871 }
872 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700873 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800874 }
875 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700876 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800877 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700878 if (dt == DataType.UNSIGNED_16 &&
879 dk != DataKind.PIXEL_DEPTH) {
880 throw new RSIllegalArgumentException("Bad kind and type combo");
881 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800882
883 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700884 switch (dk) {
885 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800886 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700887 break;
888 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800889 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700890 break;
891 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800892 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700893 break;
894 case PIXEL_DEPTH:
895 size = 2;
896 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800897 }
898
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700899 boolean norm = true;
900 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
901 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800902 }
Jason Sams36e612a2009-07-31 16:26:13 -0700903
Jason Samsa1b13ed2010-11-12 14:58:37 -0800904 /**
Stephen Hinesf257e512011-06-14 14:54:29 -0700905 * Check if the current Element is compatible with another Element.
906 * Primitive Elements are compatible if they share the same underlying
907 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
908 * must be equal in order to be compatible. This requires strict name
909 * equivalence for all sub-Elements (in addition to structural equivalence).
910 *
911 * @param e The Element to check compatibility with.
912 *
913 * @return boolean true if the Elements are compatible, otherwise false.
914 */
915 public boolean isCompatible(Element e) {
916 // Try strict BaseObj equality to start with.
917 if (this.equals(e)) {
918 return true;
919 }
920
921 // Ignore mKind because it is allowed to be different (user vs. pixel).
922 // We also ignore mNormalized because it can be different. The mType
923 // field must be non-null since we require name equivalence for
924 // user-created Elements.
925 return ((mSize == e.mSize) &&
926 (mType != null) &&
927 (mType == e.mType) &&
928 (mVectorSize == e.mVectorSize));
929 }
930
931 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800932 * Builder class for producing complex elements with matching field and name
933 * pairs. The builder starts empty. The order in which elements are added
934 * is retained for the layout in memory.
935 *
936 */
Jason Sams36e612a2009-07-31 16:26:13 -0700937 public static class Builder {
938 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800939 Element[] mElements;
940 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700941 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800942 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800943 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -0700944
Jason Samsa1b13ed2010-11-12 14:58:37 -0800945 /**
946 * Create a builder object.
947 *
948 * @param rs
949 */
Jason Sams22534172009-08-04 16:58:20 -0700950 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700951 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800952 mCount = 0;
953 mElements = new Element[8];
954 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -0700955 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700956 }
957
Jason Samsa1b13ed2010-11-12 14:58:37 -0800958 /**
959 * Add an array of elements to this element.
960 *
961 * @param element
962 * @param name
963 * @param arraySize
964 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800965 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -0700966 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -0700967 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -0700968 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800969
970 // Skip padding fields after a vector 3 type.
971 if (mSkipPadding != 0) {
972 if (name.startsWith("#padding_")) {
973 mSkipPadding = 0;
974 return this;
975 }
976 }
977
978 if (element.mVectorSize == 3) {
979 mSkipPadding = 1;
980 } else {
981 mSkipPadding = 0;
982 }
983
Jason Sams718cd1f2009-12-23 14:35:29 -0800984 if(mCount == mElements.length) {
985 Element[] e = new Element[mCount + 8];
986 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -0700987 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -0800988 System.arraycopy(mElements, 0, e, 0, mCount);
989 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700990 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -0800991 mElements = e;
992 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -0700993 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -0700994 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800995 mElements[mCount] = element;
996 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -0700997 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -0800998 mCount++;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800999 return this;
Jason Sams07ae4062009-08-27 20:23:34 -07001000 }
1001
Jason Samsa1b13ed2010-11-12 14:58:37 -08001002 /**
1003 * Add a single element to this Element.
1004 *
1005 * @param element
1006 * @param name
1007 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001008 public Builder add(Element element, String name) {
1009 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -07001010 }
1011
Jason Samsa1b13ed2010-11-12 14:58:37 -08001012 /**
1013 * Create the element from this builder.
1014 *
1015 *
1016 * @return Element
1017 */
Jason Sams22534172009-08-04 16:58:20 -07001018 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -08001019 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -08001020 Element[] ein = new Element[mCount];
1021 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -07001022 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -08001023 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1024 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001025 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001026
1027 int[] ids = new int[ein.length];
1028 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Sams06d69de2010-11-09 17:11:40 -08001029 ids[ct] = ein[ct].getID();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001030 }
Jason Sams70d4e502010-09-02 17:35:23 -07001031 int id = mRS.nElementCreate2(ids, sin, asin);
1032 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -07001033 }
1034 }
Jason Sams36e612a2009-07-31 16:26:13 -07001035}
1036