blob: 195814cdf41252e9250529a5e9bdd446af9d7dc7 [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
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -080087 /**
88 * @hide
89 * @return element vector size
90 */
91 public int getVectorSize() {return mVectorSize;}
92
Jason Samsa1b13ed2010-11-12 14:58:37 -080093
94 /**
95 * DataType represents the basic type information for a basic element. The
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -080096 * naming convention follows. For numeric types it is FLOAT,
97 * SIGNED, or UNSIGNED followed by the _BITS where BITS is the
98 * size of the data. BOOLEAN is a true / false (1,0)
99 * represented in an 8 bit container. The UNSIGNED variants
100 * with multiple bit definitions are for packed graphical data
101 * formats and represent vectors with per vector member sizes
102 * which are treated as a single unit for packing and alignment
103 * purposes.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800104 *
105 * MATRIX the three matrix types contain FLOAT_32 elements and are treated
106 * as 32 bits for alignment purposes.
107 *
108 * RS_* objects. 32 bit opaque handles.
109 */
Jason Sams36e612a2009-07-31 16:26:13 -0700110 public enum DataType {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800111 /**
112 * @hide
113 * new enum
114 */
115 NONE (0, 0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800116 //FLOAT_16 (1, 2),
117 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -0700118 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800119 SIGNED_8 (4, 1),
120 SIGNED_16 (5, 2),
121 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -0700122 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800123 UNSIGNED_8 (8, 1),
124 UNSIGNED_16 (9, 2),
125 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -0700126 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800127
Jason Samsf110d4b2010-06-21 17:42:41 -0700128 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -0800129
Jason Samsf110d4b2010-06-21 17:42:41 -0700130 UNSIGNED_5_6_5 (13, 2),
131 UNSIGNED_5_5_5_1 (14, 2),
132 UNSIGNED_4_4_4_4 (15, 2),
133
Jason Sams1d45c472010-08-25 14:31:48 -0700134 MATRIX_4X4 (16, 64),
135 MATRIX_3X3 (17, 36),
136 MATRIX_2X2 (18, 16),
137
138 RS_ELEMENT (1000, 4),
139 RS_TYPE (1001, 4),
140 RS_ALLOCATION (1002, 4),
141 RS_SAMPLER (1003, 4),
142 RS_SCRIPT (1004, 4),
143 RS_MESH (1005, 4),
144 RS_PROGRAM_FRAGMENT (1006, 4),
145 RS_PROGRAM_VERTEX (1007, 4),
146 RS_PROGRAM_RASTER (1008, 4),
147 RS_PROGRAM_STORE (1009, 4);
Jason Sams36e612a2009-07-31 16:26:13 -0700148
149 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800150 int mSize;
151 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700152 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800153 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700154 }
155 }
156
Jason Samsa1b13ed2010-11-12 14:58:37 -0800157 /**
158 * The special interpretation of the data if required. This is primarly
159 * useful for graphical data. USER indicates no special interpretation is
160 * expected. PIXEL is used in conjunction with the standard data types for
161 * representing texture formats.
162 */
Jason Sams36e612a2009-07-31 16:26:13 -0700163 public enum DataKind {
164 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800165
166 PIXEL_L (7),
167 PIXEL_A (8),
168 PIXEL_LA (9),
169 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700170 PIXEL_RGBA (11),
171 PIXEL_DEPTH (12);
Jason Sams36e612a2009-07-31 16:26:13 -0700172
173 int mID;
174 DataKind(int id) {
175 mID = id;
176 }
177 }
178
Jason Samsa1b13ed2010-11-12 14:58:37 -0800179 /**
180 * Return if a element is too complex for use as a data source for a Mesh or
181 * a Program.
182 *
183 * @return boolean
184 */
Jason Samsc1d62102010-11-04 14:32:19 -0700185 public boolean isComplex() {
186 if (mElements == null) {
187 return false;
188 }
189 for (int ct=0; ct < mElements.length; ct++) {
190 if (mElements[ct].mElements != null) {
191 return true;
192 }
193 }
194 return false;
195 }
196
Jason Samsa1b13ed2010-11-12 14:58:37 -0800197 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700198 * @hide
199 * @return number of sub-elements in this element
200 */
201 public int getSubElementCount() {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800202 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700203 return 0;
204 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800205 return mVisibleElementMap.length;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700206 }
207
208 /**
209 * @hide
210 * @param index index of the sub-element to return
211 * @return sub-element in this element at given index
212 */
213 public Element getSubElement(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800214 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700215 throw new RSIllegalArgumentException("Element contains no sub-elements");
216 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800217 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700218 throw new RSIllegalArgumentException("Illegal sub-element index");
219 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800220 return mElements[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700221 }
222
223 /**
224 * @hide
225 * @param index index of the sub-element
226 * @return sub-element in this element at given index
227 */
228 public String getSubElementName(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800229 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700230 throw new RSIllegalArgumentException("Element contains no sub-elements");
231 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800232 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700233 throw new RSIllegalArgumentException("Illegal sub-element index");
234 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800235 return mElementNames[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700236 }
237
238 /**
239 * @hide
240 * @param index index of the sub-element
241 * @return array size of sub-element in this element at given index
242 */
243 public int getSubElementArraySize(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800244 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700245 throw new RSIllegalArgumentException("Element contains no sub-elements");
246 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800247 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700248 throw new RSIllegalArgumentException("Illegal sub-element index");
249 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800250 return mArraySizes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700251 }
252
253 /**
254 * @hide
255 * @param index index of the sub-element
256 * @return offset in bytes of sub-element in this element at given index
257 */
258 public int getSubElementOffsetBytes(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800259 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700260 throw new RSIllegalArgumentException("Element contains no sub-elements");
261 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800262 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700263 throw new RSIllegalArgumentException("Illegal sub-element index");
264 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800265 return mOffsetInBytes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700266 }
267
268 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800269 * @hide
270 * @return element data type
271 */
272 public DataType getDataType() {
273 return mType;
274 }
275
276 /**
277 * @hide
278 * @return element data kind
279 */
280 public DataKind getDataKind() {
281 return mKind;
282 }
283
284 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800285 * Utility function for returning an Element containing a single Boolean.
286 *
287 * @param rs Context to which the element will belong.
288 *
289 * @return Element
290 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700291 public static Element BOOLEAN(RenderScript rs) {
292 if(rs.mElement_BOOLEAN == null) {
293 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
294 }
295 return rs.mElement_BOOLEAN;
296 }
297
Jason Samsa1b13ed2010-11-12 14:58:37 -0800298 /**
299 * Utility function for returning an Element containing a single UNSIGNED_8.
300 *
301 * @param rs Context to which the element will belong.
302 *
303 * @return Element
304 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700305 public static Element U8(RenderScript rs) {
306 if(rs.mElement_U8 == null) {
307 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800308 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700309 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800310 }
311
Jason Samsa1b13ed2010-11-12 14:58:37 -0800312 /**
313 * Utility function for returning an Element containing a single SIGNED_8.
314 *
315 * @param rs Context to which the element will belong.
316 *
317 * @return Element
318 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700319 public static Element I8(RenderScript rs) {
320 if(rs.mElement_I8 == null) {
321 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800322 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700323 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800324 }
325
Jason Samse29f3e72010-06-08 15:40:48 -0700326 public static Element U16(RenderScript rs) {
327 if(rs.mElement_U16 == null) {
328 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
329 }
330 return rs.mElement_U16;
331 }
332
333 public static Element I16(RenderScript rs) {
334 if(rs.mElement_I16 == null) {
335 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
336 }
337 return rs.mElement_I16;
338 }
339
Jason Sams8cb39de2010-06-01 15:47:01 -0700340 public static Element U32(RenderScript rs) {
341 if(rs.mElement_U32 == null) {
342 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800343 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700344 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800345 }
346
Jason Sams8cb39de2010-06-01 15:47:01 -0700347 public static Element I32(RenderScript rs) {
348 if(rs.mElement_I32 == null) {
349 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800350 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700351 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800352 }
353
Stephen Hines52d83632010-10-11 16:10:42 -0700354 public static Element U64(RenderScript rs) {
355 if(rs.mElement_U64 == null) {
356 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
357 }
358 return rs.mElement_U64;
359 }
360
Stephen Hinesef1dac22010-10-01 15:39:33 -0700361 public static Element I64(RenderScript rs) {
362 if(rs.mElement_I64 == null) {
363 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
364 }
365 return rs.mElement_I64;
366 }
367
Jason Sams8cb39de2010-06-01 15:47:01 -0700368 public static Element F32(RenderScript rs) {
369 if(rs.mElement_F32 == null) {
370 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800371 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700372 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800373 }
374
Stephen Hines02f417052010-09-30 15:19:22 -0700375 public static Element F64(RenderScript rs) {
376 if(rs.mElement_F64 == null) {
377 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
378 }
379 return rs.mElement_F64;
380 }
381
Jason Sams8cb39de2010-06-01 15:47:01 -0700382 public static Element ELEMENT(RenderScript rs) {
383 if(rs.mElement_ELEMENT == null) {
384 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700385 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700386 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700387 }
388
Jason Sams8cb39de2010-06-01 15:47:01 -0700389 public static Element TYPE(RenderScript rs) {
390 if(rs.mElement_TYPE == null) {
391 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700392 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700393 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700394 }
395
Jason Sams8cb39de2010-06-01 15:47:01 -0700396 public static Element ALLOCATION(RenderScript rs) {
397 if(rs.mElement_ALLOCATION == null) {
398 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700399 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700400 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700401 }
402
Jason Sams8cb39de2010-06-01 15:47:01 -0700403 public static Element SAMPLER(RenderScript rs) {
404 if(rs.mElement_SAMPLER == null) {
405 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700406 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700407 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700408 }
409
Jason Sams8cb39de2010-06-01 15:47:01 -0700410 public static Element SCRIPT(RenderScript rs) {
411 if(rs.mElement_SCRIPT == null) {
412 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700413 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700414 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700415 }
416
Jason Sams8cb39de2010-06-01 15:47:01 -0700417 public static Element MESH(RenderScript rs) {
418 if(rs.mElement_MESH == null) {
419 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700420 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700421 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700422 }
423
Jason Sams8cb39de2010-06-01 15:47:01 -0700424 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
425 if(rs.mElement_PROGRAM_FRAGMENT == null) {
426 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700427 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700428 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700429 }
430
Jason Sams8cb39de2010-06-01 15:47:01 -0700431 public static Element PROGRAM_VERTEX(RenderScript rs) {
432 if(rs.mElement_PROGRAM_VERTEX == null) {
433 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700434 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700435 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700436 }
437
Jason Sams8cb39de2010-06-01 15:47:01 -0700438 public static Element PROGRAM_RASTER(RenderScript rs) {
439 if(rs.mElement_PROGRAM_RASTER == null) {
440 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700441 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700442 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700443 }
444
Jason Sams8cb39de2010-06-01 15:47:01 -0700445 public static Element PROGRAM_STORE(RenderScript rs) {
446 if(rs.mElement_PROGRAM_STORE == null) {
447 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700448 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700449 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700450 }
451
452
Jason Sams718cd1f2009-12-23 14:35:29 -0800453 public static Element A_8(RenderScript rs) {
454 if(rs.mElement_A_8 == null) {
455 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
456 }
457 return rs.mElement_A_8;
458 }
459
460 public static Element RGB_565(RenderScript rs) {
461 if(rs.mElement_RGB_565 == null) {
462 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
463 }
464 return rs.mElement_RGB_565;
465 }
466
467 public static Element RGB_888(RenderScript rs) {
468 if(rs.mElement_RGB_888 == null) {
469 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
470 }
471 return rs.mElement_RGB_888;
472 }
473
474 public static Element RGBA_5551(RenderScript rs) {
475 if(rs.mElement_RGBA_5551 == null) {
476 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
477 }
478 return rs.mElement_RGBA_5551;
479 }
480
481 public static Element RGBA_4444(RenderScript rs) {
482 if(rs.mElement_RGBA_4444 == null) {
483 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
484 }
485 return rs.mElement_RGBA_4444;
486 }
487
488 public static Element RGBA_8888(RenderScript rs) {
489 if(rs.mElement_RGBA_8888 == null) {
490 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
491 }
492 return rs.mElement_RGBA_8888;
493 }
494
Jason Sams8cb39de2010-06-01 15:47:01 -0700495 public static Element F32_2(RenderScript rs) {
496 if(rs.mElement_FLOAT_2 == null) {
497 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800498 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700499 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800500 }
501
Jason Sams8cb39de2010-06-01 15:47:01 -0700502 public static Element F32_3(RenderScript rs) {
503 if(rs.mElement_FLOAT_3 == null) {
504 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800505 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700506 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800507 }
508
Jason Sams8cb39de2010-06-01 15:47:01 -0700509 public static Element F32_4(RenderScript rs) {
510 if(rs.mElement_FLOAT_4 == null) {
511 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800512 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700513 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800514 }
515
Stephen Hines836c4a52011-06-01 14:38:10 -0700516 public static Element F64_2(RenderScript rs) {
517 if(rs.mElement_DOUBLE_2 == null) {
518 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
519 }
520 return rs.mElement_DOUBLE_2;
521 }
522
523 public static Element F64_3(RenderScript rs) {
524 if(rs.mElement_DOUBLE_3 == null) {
525 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
526 }
527 return rs.mElement_DOUBLE_3;
528 }
529
530 public static Element F64_4(RenderScript rs) {
531 if(rs.mElement_DOUBLE_4 == null) {
532 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
533 }
534 return rs.mElement_DOUBLE_4;
535 }
536
537 public static Element U8_2(RenderScript rs) {
538 if(rs.mElement_UCHAR_2 == null) {
539 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
540 }
541 return rs.mElement_UCHAR_2;
542 }
543
544 public static Element U8_3(RenderScript rs) {
545 if(rs.mElement_UCHAR_3 == null) {
546 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
547 }
548 return rs.mElement_UCHAR_3;
549 }
550
Jason Sams8cb39de2010-06-01 15:47:01 -0700551 public static Element U8_4(RenderScript rs) {
552 if(rs.mElement_UCHAR_4 == null) {
553 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800554 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700555 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800556 }
557
Stephen Hines836c4a52011-06-01 14:38:10 -0700558 public static Element I8_2(RenderScript rs) {
559 if(rs.mElement_CHAR_2 == null) {
560 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
561 }
562 return rs.mElement_CHAR_2;
563 }
564
565 public static Element I8_3(RenderScript rs) {
566 if(rs.mElement_CHAR_3 == null) {
567 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
568 }
569 return rs.mElement_CHAR_3;
570 }
571
572 public static Element I8_4(RenderScript rs) {
573 if(rs.mElement_CHAR_4 == null) {
574 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
575 }
576 return rs.mElement_CHAR_4;
577 }
578
579 public static Element U16_2(RenderScript rs) {
580 if(rs.mElement_USHORT_2 == null) {
581 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
582 }
583 return rs.mElement_USHORT_2;
584 }
585
586 public static Element U16_3(RenderScript rs) {
587 if(rs.mElement_USHORT_3 == null) {
588 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
589 }
590 return rs.mElement_USHORT_3;
591 }
592
593 public static Element U16_4(RenderScript rs) {
594 if(rs.mElement_USHORT_4 == null) {
595 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
596 }
597 return rs.mElement_USHORT_4;
598 }
599
600 public static Element I16_2(RenderScript rs) {
601 if(rs.mElement_SHORT_2 == null) {
602 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
603 }
604 return rs.mElement_SHORT_2;
605 }
606
607 public static Element I16_3(RenderScript rs) {
608 if(rs.mElement_SHORT_3 == null) {
609 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
610 }
611 return rs.mElement_SHORT_3;
612 }
613
614 public static Element I16_4(RenderScript rs) {
615 if(rs.mElement_SHORT_4 == null) {
616 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
617 }
618 return rs.mElement_SHORT_4;
619 }
620
621 public static Element U32_2(RenderScript rs) {
622 if(rs.mElement_UINT_2 == null) {
623 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
624 }
625 return rs.mElement_UINT_2;
626 }
627
628 public static Element U32_3(RenderScript rs) {
629 if(rs.mElement_UINT_3 == null) {
630 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
631 }
632 return rs.mElement_UINT_3;
633 }
634
635 public static Element U32_4(RenderScript rs) {
636 if(rs.mElement_UINT_4 == null) {
637 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
638 }
639 return rs.mElement_UINT_4;
640 }
641
642 public static Element I32_2(RenderScript rs) {
643 if(rs.mElement_INT_2 == null) {
644 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
645 }
646 return rs.mElement_INT_2;
647 }
648
649 public static Element I32_3(RenderScript rs) {
650 if(rs.mElement_INT_3 == null) {
651 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
652 }
653 return rs.mElement_INT_3;
654 }
655
656 public static Element I32_4(RenderScript rs) {
657 if(rs.mElement_INT_4 == null) {
658 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
659 }
660 return rs.mElement_INT_4;
661 }
662
663 public static Element U64_2(RenderScript rs) {
664 if(rs.mElement_ULONG_2 == null) {
665 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
666 }
667 return rs.mElement_ULONG_2;
668 }
669
670 public static Element U64_3(RenderScript rs) {
671 if(rs.mElement_ULONG_3 == null) {
672 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
673 }
674 return rs.mElement_ULONG_3;
675 }
676
677 public static Element U64_4(RenderScript rs) {
678 if(rs.mElement_ULONG_4 == null) {
679 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
680 }
681 return rs.mElement_ULONG_4;
682 }
683
684 public static Element I64_2(RenderScript rs) {
685 if(rs.mElement_LONG_2 == null) {
686 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
687 }
688 return rs.mElement_LONG_2;
689 }
690
691 public static Element I64_3(RenderScript rs) {
692 if(rs.mElement_LONG_3 == null) {
693 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
694 }
695 return rs.mElement_LONG_3;
696 }
697
698 public static Element I64_4(RenderScript rs) {
699 if(rs.mElement_LONG_4 == null) {
700 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
701 }
702 return rs.mElement_LONG_4;
703 }
704
Jason Sams1d45c472010-08-25 14:31:48 -0700705 public static Element MATRIX_4X4(RenderScript rs) {
706 if(rs.mElement_MATRIX_4X4 == null) {
707 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
708 }
709 return rs.mElement_MATRIX_4X4;
710 }
711 public static Element MATRIX4X4(RenderScript rs) {
712 return MATRIX_4X4(rs);
713 }
714
715 public static Element MATRIX_3X3(RenderScript rs) {
716 if(rs.mElement_MATRIX_3X3 == null) {
717 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
718 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800719 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700720 }
721
722 public static Element MATRIX_2X2(RenderScript rs) {
723 if(rs.mElement_MATRIX_2X2 == null) {
724 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
725 }
726 return rs.mElement_MATRIX_2X2;
727 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800728
Jason Sams70d4e502010-09-02 17:35:23 -0700729 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700730 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700731 mSize = 0;
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -0800732 mVectorSize = 1;
Jason Sams718cd1f2009-12-23 14:35:29 -0800733 mElements = e;
734 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700735 mArraySizes = as;
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800736 mType = DataType.NONE;
737 mKind = DataKind.USER;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700738 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800739 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700740 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700741 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800742 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800743 updateVisibleSubElements();
Jason Sams718cd1f2009-12-23 14:35:29 -0800744 }
745
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700746 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
747 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800748 if ((dt != DataType.UNSIGNED_5_6_5) &&
749 (dt != DataType.UNSIGNED_4_4_4_4) &&
750 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800751 if (size == 3) {
752 mSize = dt.mSize * 4;
753 } else {
754 mSize = dt.mSize * size;
755 }
Jason Sams252c0782011-01-11 17:42:52 -0800756 } else {
757 mSize = dt.mSize;
758 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800759 mType = dt;
760 mKind = dk;
761 mNormalized = norm;
762 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700763 }
764
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700765 Element(int id, RenderScript rs) {
766 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700767 }
768
769 @Override
770 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800771 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700772
773 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
774 int[] dataBuffer = new int[5];
Jason Sams06d69de2010-11-09 17:11:40 -0800775 mRS.nElementGetNativeData(getID(), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700776
777 mNormalized = dataBuffer[2] == 1 ? true : false;
778 mVectorSize = dataBuffer[3];
779 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700780 for (DataType dt: DataType.values()) {
781 if(dt.mID == dataBuffer[0]){
782 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700783 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700784 }
785 }
786 for (DataKind dk: DataKind.values()) {
787 if(dk.mID == dataBuffer[1]){
788 mKind = dk;
789 }
790 }
791
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700792 int numSubElements = dataBuffer[4];
793 if(numSubElements > 0) {
794 mElements = new Element[numSubElements];
795 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700796 mArraySizes = new int[numSubElements];
797 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700798
799 int[] subElementIds = new int[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700800 mRS.nElementGetSubElements(getID(), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700801 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700802 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700803 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700804 mOffsetInBytes[i] = mSize;
805 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700806 }
807 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800808 updateVisibleSubElements();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700809 }
810
Jason Samsa1b13ed2010-11-12 14:58:37 -0800811 /**
812 * Create a custom Element of the specified DataType. The DataKind will be
813 * set to USER and the vector size to 1 indicating non-vector.
814 *
815 * @param rs The context associated with the new Element.
816 * @param dt The DataType for the new element.
817 * @return Element
818 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800819 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700820 DataKind dk = DataKind.USER;
821 boolean norm = false;
822 int vecSize = 1;
823 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
824 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800825 }
826
Jason Samsa1b13ed2010-11-12 14:58:37 -0800827 /**
828 * Create a custom vector element of the specified DataType and vector size.
829 * DataKind will be set to USER.
830 *
831 * @param rs The context associated with the new Element.
832 * @param dt The DataType for the new element.
833 * @param size Vector size for the new Element. Range 2-4 inclusive
834 * supported.
835 *
836 * @return Element
837 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800838 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800839 if (size < 2 || size > 4) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800840 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700841 }
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700842 DataKind dk = DataKind.USER;
843 boolean norm = false;
844 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
845 return new Element(id, rs, dt, dk, norm, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700846 }
847
Jason Samsa1b13ed2010-11-12 14:58:37 -0800848 /**
849 * Create a new pixel Element type. A matching DataType and DataKind must
850 * be provided. The DataType and DataKind must contain the same number of
851 * components. Vector size will be set to 1.
852 *
853 * @param rs The context associated with the new Element.
854 * @param dt The DataType for the new element.
855 * @param dk The DataKind to specify the mapping of each component in the
856 * DataType.
857 *
858 * @return Element
859 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800860 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800861 if (!(dk == DataKind.PIXEL_L ||
862 dk == DataKind.PIXEL_A ||
863 dk == DataKind.PIXEL_LA ||
864 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700865 dk == DataKind.PIXEL_RGBA ||
866 dk == DataKind.PIXEL_DEPTH)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700867 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800868 }
869 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700870 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800871 dt == DataType.UNSIGNED_5_6_5 ||
872 dt == DataType.UNSIGNED_4_4_4_4 ||
873 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700874 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800875 }
876 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700877 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800878 }
879 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700880 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800881 }
882 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700883 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800884 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700885 if (dt == DataType.UNSIGNED_16 &&
886 dk != DataKind.PIXEL_DEPTH) {
887 throw new RSIllegalArgumentException("Bad kind and type combo");
888 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800889
890 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700891 switch (dk) {
892 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800893 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700894 break;
895 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800896 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700897 break;
898 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800899 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700900 break;
901 case PIXEL_DEPTH:
902 size = 2;
903 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800904 }
905
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700906 boolean norm = true;
907 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
908 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800909 }
Jason Sams36e612a2009-07-31 16:26:13 -0700910
Jason Samsa1b13ed2010-11-12 14:58:37 -0800911 /**
Stephen Hinesf257e512011-06-14 14:54:29 -0700912 * Check if the current Element is compatible with another Element.
913 * Primitive Elements are compatible if they share the same underlying
914 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
915 * must be equal in order to be compatible. This requires strict name
916 * equivalence for all sub-Elements (in addition to structural equivalence).
917 *
918 * @param e The Element to check compatibility with.
919 *
920 * @return boolean true if the Elements are compatible, otherwise false.
921 */
922 public boolean isCompatible(Element e) {
923 // Try strict BaseObj equality to start with.
924 if (this.equals(e)) {
925 return true;
926 }
927
928 // Ignore mKind because it is allowed to be different (user vs. pixel).
929 // We also ignore mNormalized because it can be different. The mType
930 // field must be non-null since we require name equivalence for
931 // user-created Elements.
932 return ((mSize == e.mSize) &&
933 (mType != null) &&
934 (mType == e.mType) &&
935 (mVectorSize == e.mVectorSize));
936 }
937
938 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800939 * Builder class for producing complex elements with matching field and name
940 * pairs. The builder starts empty. The order in which elements are added
941 * is retained for the layout in memory.
942 *
943 */
Jason Sams36e612a2009-07-31 16:26:13 -0700944 public static class Builder {
945 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800946 Element[] mElements;
947 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700948 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800949 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800950 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -0700951
Jason Samsa1b13ed2010-11-12 14:58:37 -0800952 /**
953 * Create a builder object.
954 *
955 * @param rs
956 */
Jason Sams22534172009-08-04 16:58:20 -0700957 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700958 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800959 mCount = 0;
960 mElements = new Element[8];
961 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -0700962 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700963 }
964
Jason Samsa1b13ed2010-11-12 14:58:37 -0800965 /**
966 * Add an array of elements to this element.
967 *
968 * @param element
969 * @param name
970 * @param arraySize
971 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800972 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -0700973 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -0700974 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -0700975 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800976
977 // Skip padding fields after a vector 3 type.
978 if (mSkipPadding != 0) {
979 if (name.startsWith("#padding_")) {
980 mSkipPadding = 0;
981 return this;
982 }
983 }
984
985 if (element.mVectorSize == 3) {
986 mSkipPadding = 1;
987 } else {
988 mSkipPadding = 0;
989 }
990
Jason Sams718cd1f2009-12-23 14:35:29 -0800991 if(mCount == mElements.length) {
992 Element[] e = new Element[mCount + 8];
993 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -0700994 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -0800995 System.arraycopy(mElements, 0, e, 0, mCount);
996 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700997 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -0800998 mElements = e;
999 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -07001000 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -07001001 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001002 mElements[mCount] = element;
1003 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -07001004 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -08001005 mCount++;
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001006 return this;
Jason Sams07ae4062009-08-27 20:23:34 -07001007 }
1008
Jason Samsa1b13ed2010-11-12 14:58:37 -08001009 /**
1010 * Add a single element to this Element.
1011 *
1012 * @param element
1013 * @param name
1014 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001015 public Builder add(Element element, String name) {
1016 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -07001017 }
1018
Jason Samsa1b13ed2010-11-12 14:58:37 -08001019 /**
1020 * Create the element from this builder.
1021 *
1022 *
1023 * @return Element
1024 */
Jason Sams22534172009-08-04 16:58:20 -07001025 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -08001026 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -08001027 Element[] ein = new Element[mCount];
1028 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -07001029 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -08001030 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1031 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001032 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001033
1034 int[] ids = new int[ein.length];
1035 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Sams06d69de2010-11-09 17:11:40 -08001036 ids[ct] = ein[ct].getID();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001037 }
Jason Sams70d4e502010-09-02 17:35:23 -07001038 int id = mRS.nElementCreate2(ids, sin, asin);
1039 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -07001040 }
1041 }
Jason Sams36e612a2009-07-31 16:26:13 -07001042}
1043