blob: 60ff996d0505a6d5f3dc88f8a819dffbb84fa667 [file] [log] [blame]
Jason Sams36e612a2009-07-31 16:26:13 -07001/*
Jason Samsdd6c8b32013-02-15 17:27:24 -08002 * Copyright (C) 2013 The Android Open Source Project
Jason Sams36e612a2009-07-31 16:26:13 -07003 *
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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070019/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070020 * <p>An Element represents one item within an {@link
21 * android.renderscript.Allocation}. An Element is roughly equivalent to a C
22 * type in a RenderScript kernel. Elements may be basic or complex. Some basic
23 * elements are</p> <ul> <li>A single float value (equivalent to a float in a
24 * kernel)</li> <li>A four-element float vector (equivalent to a float4 in a
25 * kernel)</li> <li>An unsigned 32-bit integer (equivalent to an unsigned int in
26 * a kernel)</li> <li>A single signed 8-bit integer (equivalent to a char in a
27 * kernel)</li> </ul> <p>A complex element is roughly equivalent to a C struct
28 * and contains a number of basic or complex Elements. From Java code, a complex
29 * element contains a list of sub-elements and names that represents a
30 * particular data structure. Structs used in RS scripts are available to Java
31 * code by using the {@code ScriptField_structname} class that is reflected from
32 * a particular script.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080033 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070034 * <p>Basic Elements are comprised of a {@link
35 * android.renderscript.Element.DataType} and a {@link
36 * android.renderscript.Element.DataKind}. The DataType encodes C type
37 * information of an Element, while the DataKind encodes how that Element should
38 * be interpreted by a {@link android.renderscript.Sampler}. Note that {@link
39 * android.renderscript.Allocation} objects with DataKind {@link
40 * android.renderscript.Element.DataKind#USER} cannot be used as input for a
41 * {@link android.renderscript.Sampler}. In general, {@link
42 * android.renderscript.Allocation} objects that are intended for use with a
43 * {@link android.renderscript.Sampler} should use bitmap-derived Elements such
44 * as {@link android.renderscript.Element#RGBA_8888} or {@link
45 * android.renderscript#Element.A_8}.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080046 *
47 * <div class="special reference">
48 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070049 * <p>For more information about creating an application that uses RenderScript, read the
50 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080051 * </div>
Jason Sams36e612a2009-07-31 16:26:13 -070052 **/
53public class Element extends BaseObj {
Jason Samsea84a7c2009-09-04 14:42:41 -070054 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080055 Element[] mElements;
56 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -070057 int[] mArraySizes;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070058 int[] mOffsetInBytes;
Jason Sams36e612a2009-07-31 16:26:13 -070059
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -080060 int[] mVisibleElementMap;
61
Jason Sams718cd1f2009-12-23 14:35:29 -080062 DataType mType;
63 DataKind mKind;
64 boolean mNormalized;
65 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070066
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -080067 private void updateVisibleSubElements() {
68 if (mElements == null) {
69 return;
70 }
71
72 int noPaddingFieldCount = 0;
73 int fieldCount = mElementNames.length;
74 // Find out how many elements are not padding
75 for (int ct = 0; ct < fieldCount; ct ++) {
76 if (mElementNames[ct].charAt(0) != '#') {
77 noPaddingFieldCount ++;
78 }
79 }
80 mVisibleElementMap = new int[noPaddingFieldCount];
81
82 // Make a map that points us at non-padding elements
83 for (int ct = 0, ctNoPadding = 0; ct < fieldCount; ct ++) {
84 if (mElementNames[ct].charAt(0) != '#') {
85 mVisibleElementMap[ctNoPadding ++] = ct;
86 }
87 }
88 }
89
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070090 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070091 * @return element size in bytes
92 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070093 public int getBytesSize() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070094
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070095 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070096 * Returns the number of vector components. 2 for float2, 4 for
97 * float4, etc.
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -080098 * @return element vector size
99 */
100 public int getVectorSize() {return mVectorSize;}
101
Jason Samsa1b13ed2010-11-12 14:58:37 -0800102
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700103 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800104 * DataType represents the basic type information for a basic element. The
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800105 * naming convention follows. For numeric types it is FLOAT,
106 * SIGNED, or UNSIGNED followed by the _BITS where BITS is the
107 * size of the data. BOOLEAN is a true / false (1,0)
108 * represented in an 8 bit container. The UNSIGNED variants
109 * with multiple bit definitions are for packed graphical data
110 * formats and represent vectors with per vector member sizes
111 * which are treated as a single unit for packing and alignment
112 * purposes.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800113 *
114 * MATRIX the three matrix types contain FLOAT_32 elements and are treated
115 * as 32 bits for alignment purposes.
116 *
Jason Samsfb4f5cf2015-03-26 17:39:34 -0700117 * RS_* objects: opaque handles with implementation dependent
118 * sizes.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800119 */
Jason Sams36e612a2009-07-31 16:26:13 -0700120 public enum DataType {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800121 NONE (0, 0),
Jason Samsa5835a22014-11-05 15:16:26 -0800122 /**
123 * @hide
124 */
125 FLOAT_16 (1, 2),
Jason Sams718cd1f2009-12-23 14:35:29 -0800126 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -0700127 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800128 SIGNED_8 (4, 1),
129 SIGNED_16 (5, 2),
130 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -0700131 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800132 UNSIGNED_8 (8, 1),
133 UNSIGNED_16 (9, 2),
134 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -0700135 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800136
Jason Samsf110d4b2010-06-21 17:42:41 -0700137 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -0800138
Jason Samsf110d4b2010-06-21 17:42:41 -0700139 UNSIGNED_5_6_5 (13, 2),
140 UNSIGNED_5_5_5_1 (14, 2),
141 UNSIGNED_4_4_4_4 (15, 2),
142
Jason Sams1d45c472010-08-25 14:31:48 -0700143 MATRIX_4X4 (16, 64),
144 MATRIX_3X3 (17, 36),
145 MATRIX_2X2 (18, 16),
146
Jason Samsb49dfea2014-06-18 13:17:57 -0700147 RS_ELEMENT (1000),
148 RS_TYPE (1001),
149 RS_ALLOCATION (1002),
150 RS_SAMPLER (1003),
151 RS_SCRIPT (1004),
152 RS_MESH (1005),
153 RS_PROGRAM_FRAGMENT (1006),
154 RS_PROGRAM_VERTEX (1007),
155 RS_PROGRAM_RASTER (1008),
156 RS_PROGRAM_STORE (1009),
157 RS_FONT (1010);
Jason Sams36e612a2009-07-31 16:26:13 -0700158
159 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800160 int mSize;
161 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700162 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800163 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700164 }
Jason Samsb49dfea2014-06-18 13:17:57 -0700165
166 DataType(int id) {
167 mID = id;
168 mSize = 4;
169 if (RenderScript.sPointerSize == 8) {
170 mSize = 32;
171 }
172 }
Jason Sams36e612a2009-07-31 16:26:13 -0700173 }
174
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700175 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800176 * The special interpretation of the data if required. This is primarly
177 * useful for graphical data. USER indicates no special interpretation is
178 * expected. PIXEL is used in conjunction with the standard data types for
179 * representing texture formats.
180 */
Jason Sams36e612a2009-07-31 16:26:13 -0700181 public enum DataKind {
182 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800183
184 PIXEL_L (7),
185 PIXEL_A (8),
186 PIXEL_LA (9),
187 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700188 PIXEL_RGBA (11),
Jason Sams8140d7b2012-12-13 17:01:09 -0800189 PIXEL_DEPTH (12),
190 PIXEL_YUV(13);
Jason Sams36e612a2009-07-31 16:26:13 -0700191
192 int mID;
193 DataKind(int id) {
194 mID = id;
195 }
196 }
197
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700198 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800199 * Return if a element is too complex for use as a data source for a Mesh or
200 * a Program.
201 *
202 * @return boolean
203 */
Jason Samsc1d62102010-11-04 14:32:19 -0700204 public boolean isComplex() {
205 if (mElements == null) {
206 return false;
207 }
208 for (int ct=0; ct < mElements.length; ct++) {
209 if (mElements[ct].mElements != null) {
210 return true;
211 }
212 }
213 return false;
214 }
215
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700216 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700217 * Elements could be simple, such as an int or a float, or a
218 * structure with multiple sub elements, such as a collection of
219 * floats, float2, float4. This function returns zero for simple
220 * elements or the number of sub-elements otherwise.
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700221 * @return number of sub-elements in this element
222 */
223 public int getSubElementCount() {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800224 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700225 return 0;
226 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800227 return mVisibleElementMap.length;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700228 }
229
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700230 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700231 * For complex elements, this function will return the
232 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700233 * @param index index of the sub-element to return
234 * @return sub-element in this element at given index
235 */
236 public Element getSubElement(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800237 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700238 throw new RSIllegalArgumentException("Element contains no sub-elements");
239 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800240 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700241 throw new RSIllegalArgumentException("Illegal sub-element index");
242 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800243 return mElements[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700244 }
245
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700246 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700247 * For complex elements, this function will return the
248 * sub-element name at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700249 * @param index index of the sub-element
250 * @return sub-element in this element at given index
251 */
252 public String getSubElementName(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 mElementNames[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700260 }
261
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700262 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700263 * For complex elements, some sub-elements could be statically
264 * sized arrays. This function will return the array size for
265 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700266 * @param index index of the sub-element
267 * @return array size of sub-element in this element at given index
268 */
269 public int getSubElementArraySize(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800270 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700271 throw new RSIllegalArgumentException("Element contains no sub-elements");
272 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800273 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700274 throw new RSIllegalArgumentException("Illegal sub-element index");
275 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800276 return mArraySizes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700277 }
278
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700279 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700280 * This function specifies the location of a sub-element within
281 * the element
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700282 * @param index index of the sub-element
283 * @return offset in bytes of sub-element in this element at given index
284 */
285 public int getSubElementOffsetBytes(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800286 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700287 throw new RSIllegalArgumentException("Element contains no sub-elements");
288 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800289 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700290 throw new RSIllegalArgumentException("Illegal sub-element index");
291 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800292 return mOffsetInBytes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700293 }
294
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700295 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800296 * @return element data type
297 */
298 public DataType getDataType() {
299 return mType;
300 }
301
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700302 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800303 * @return element data kind
304 */
305 public DataKind getDataKind() {
306 return mKind;
307 }
308
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700309 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800310 * Utility function for returning an Element containing a single Boolean.
311 *
312 * @param rs Context to which the element will belong.
313 *
314 * @return Element
315 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700316 public static Element BOOLEAN(RenderScript rs) {
317 if(rs.mElement_BOOLEAN == null) {
318 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
319 }
320 return rs.mElement_BOOLEAN;
321 }
322
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700323 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800324 * Utility function for returning an Element containing a single UNSIGNED_8.
325 *
326 * @param rs Context to which the element will belong.
327 *
328 * @return Element
329 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700330 public static Element U8(RenderScript rs) {
331 if(rs.mElement_U8 == null) {
332 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800333 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700334 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800335 }
336
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700337 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800338 * Utility function for returning an Element containing a single SIGNED_8.
339 *
340 * @param rs Context to which the element will belong.
341 *
342 * @return Element
343 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700344 public static Element I8(RenderScript rs) {
345 if(rs.mElement_I8 == null) {
346 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800347 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700348 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800349 }
350
Jason Samse29f3e72010-06-08 15:40:48 -0700351 public static Element U16(RenderScript rs) {
352 if(rs.mElement_U16 == null) {
353 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
354 }
355 return rs.mElement_U16;
356 }
357
358 public static Element I16(RenderScript rs) {
359 if(rs.mElement_I16 == null) {
360 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
361 }
362 return rs.mElement_I16;
363 }
364
Jason Sams8cb39de2010-06-01 15:47:01 -0700365 public static Element U32(RenderScript rs) {
366 if(rs.mElement_U32 == null) {
367 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800368 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700369 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800370 }
371
Jason Sams8cb39de2010-06-01 15:47:01 -0700372 public static Element I32(RenderScript rs) {
373 if(rs.mElement_I32 == null) {
374 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800375 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700376 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800377 }
378
Stephen Hines52d83632010-10-11 16:10:42 -0700379 public static Element U64(RenderScript rs) {
380 if(rs.mElement_U64 == null) {
381 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
382 }
383 return rs.mElement_U64;
384 }
385
Stephen Hinesef1dac22010-10-01 15:39:33 -0700386 public static Element I64(RenderScript rs) {
387 if(rs.mElement_I64 == null) {
388 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
389 }
390 return rs.mElement_I64;
391 }
392
Jason Samsa5835a22014-11-05 15:16:26 -0800393 /**
394 * @hide
395 */
396 public static Element F16(RenderScript rs) {
397 if(rs.mElement_F16 == null) {
398 rs.mElement_F16 = createUser(rs, DataType.FLOAT_16);
399 }
400 return rs.mElement_F16;
401 }
402
Jason Sams8cb39de2010-06-01 15:47:01 -0700403 public static Element F32(RenderScript rs) {
404 if(rs.mElement_F32 == null) {
405 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800406 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700407 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800408 }
409
Stephen Hines02f417052010-09-30 15:19:22 -0700410 public static Element F64(RenderScript rs) {
411 if(rs.mElement_F64 == null) {
412 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
413 }
414 return rs.mElement_F64;
415 }
416
Jason Sams8cb39de2010-06-01 15:47:01 -0700417 public static Element ELEMENT(RenderScript rs) {
418 if(rs.mElement_ELEMENT == null) {
419 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700420 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700421 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700422 }
423
Jason Sams8cb39de2010-06-01 15:47:01 -0700424 public static Element TYPE(RenderScript rs) {
425 if(rs.mElement_TYPE == null) {
426 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700427 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700428 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700429 }
430
Jason Sams8cb39de2010-06-01 15:47:01 -0700431 public static Element ALLOCATION(RenderScript rs) {
432 if(rs.mElement_ALLOCATION == null) {
433 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700434 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700435 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700436 }
437
Jason Sams8cb39de2010-06-01 15:47:01 -0700438 public static Element SAMPLER(RenderScript rs) {
439 if(rs.mElement_SAMPLER == null) {
440 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700441 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700442 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700443 }
444
Jason Sams8cb39de2010-06-01 15:47:01 -0700445 public static Element SCRIPT(RenderScript rs) {
446 if(rs.mElement_SCRIPT == null) {
447 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700448 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700449 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700450 }
451
Jason Sams8cb39de2010-06-01 15:47:01 -0700452 public static Element MESH(RenderScript rs) {
453 if(rs.mElement_MESH == null) {
454 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700455 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700456 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700457 }
458
Jason Sams8cb39de2010-06-01 15:47:01 -0700459 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
460 if(rs.mElement_PROGRAM_FRAGMENT == null) {
461 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700462 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700463 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700464 }
465
Jason Sams8cb39de2010-06-01 15:47:01 -0700466 public static Element PROGRAM_VERTEX(RenderScript rs) {
467 if(rs.mElement_PROGRAM_VERTEX == null) {
468 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700469 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700470 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700471 }
472
Jason Sams8cb39de2010-06-01 15:47:01 -0700473 public static Element PROGRAM_RASTER(RenderScript rs) {
474 if(rs.mElement_PROGRAM_RASTER == null) {
475 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700476 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700477 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700478 }
479
Jason Sams8cb39de2010-06-01 15:47:01 -0700480 public static Element PROGRAM_STORE(RenderScript rs) {
481 if(rs.mElement_PROGRAM_STORE == null) {
482 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700483 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700484 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700485 }
486
Stephen Hines3a291412012-04-11 17:27:29 -0700487 public static Element FONT(RenderScript rs) {
488 if(rs.mElement_FONT == null) {
489 rs.mElement_FONT = createUser(rs, DataType.RS_FONT);
490 }
491 return rs.mElement_FONT;
492 }
493
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700494
Jason Sams718cd1f2009-12-23 14:35:29 -0800495 public static Element A_8(RenderScript rs) {
496 if(rs.mElement_A_8 == null) {
497 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
498 }
499 return rs.mElement_A_8;
500 }
501
502 public static Element RGB_565(RenderScript rs) {
503 if(rs.mElement_RGB_565 == null) {
504 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
505 }
506 return rs.mElement_RGB_565;
507 }
508
509 public static Element RGB_888(RenderScript rs) {
510 if(rs.mElement_RGB_888 == null) {
511 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
512 }
513 return rs.mElement_RGB_888;
514 }
515
516 public static Element RGBA_5551(RenderScript rs) {
517 if(rs.mElement_RGBA_5551 == null) {
518 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
519 }
520 return rs.mElement_RGBA_5551;
521 }
522
523 public static Element RGBA_4444(RenderScript rs) {
524 if(rs.mElement_RGBA_4444 == null) {
525 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
526 }
527 return rs.mElement_RGBA_4444;
528 }
529
530 public static Element RGBA_8888(RenderScript rs) {
531 if(rs.mElement_RGBA_8888 == null) {
532 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
533 }
534 return rs.mElement_RGBA_8888;
535 }
536
Jason Samsa5835a22014-11-05 15:16:26 -0800537 /**
538 * @hide
539 */
540 public static Element F16_2(RenderScript rs) {
541 if(rs.mElement_HALF_2 == null) {
542 rs.mElement_HALF_2 = createVector(rs, DataType.FLOAT_16, 2);
543 }
544 return rs.mElement_HALF_2;
545 }
546
547 /**
548 * @hide
549 */
550 public static Element F16_3(RenderScript rs) {
551 if(rs.mElement_FLOAT_3 == null) {
552 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_16, 3);
553 }
554 return rs.mElement_HALF_3;
555 }
556
557 /**
558 * @hide
559 */
560 public static Element F16_4(RenderScript rs) {
561 if(rs.mElement_HALF_4 == null) {
562 rs.mElement_HALF_4 = createVector(rs, DataType.FLOAT_16, 4);
563 }
564 return rs.mElement_HALF_4;
565 }
566
Jason Sams8cb39de2010-06-01 15:47:01 -0700567 public static Element F32_2(RenderScript rs) {
568 if(rs.mElement_FLOAT_2 == null) {
569 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800570 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700571 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800572 }
573
Jason Sams8cb39de2010-06-01 15:47:01 -0700574 public static Element F32_3(RenderScript rs) {
575 if(rs.mElement_FLOAT_3 == null) {
576 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800577 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700578 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800579 }
580
Jason Sams8cb39de2010-06-01 15:47:01 -0700581 public static Element F32_4(RenderScript rs) {
582 if(rs.mElement_FLOAT_4 == null) {
583 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800584 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700585 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800586 }
587
Stephen Hines836c4a52011-06-01 14:38:10 -0700588 public static Element F64_2(RenderScript rs) {
589 if(rs.mElement_DOUBLE_2 == null) {
590 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
591 }
592 return rs.mElement_DOUBLE_2;
593 }
594
595 public static Element F64_3(RenderScript rs) {
596 if(rs.mElement_DOUBLE_3 == null) {
597 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
598 }
599 return rs.mElement_DOUBLE_3;
600 }
601
602 public static Element F64_4(RenderScript rs) {
603 if(rs.mElement_DOUBLE_4 == null) {
604 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
605 }
606 return rs.mElement_DOUBLE_4;
607 }
608
609 public static Element U8_2(RenderScript rs) {
610 if(rs.mElement_UCHAR_2 == null) {
611 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
612 }
613 return rs.mElement_UCHAR_2;
614 }
615
616 public static Element U8_3(RenderScript rs) {
617 if(rs.mElement_UCHAR_3 == null) {
618 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
619 }
620 return rs.mElement_UCHAR_3;
621 }
622
Jason Sams8cb39de2010-06-01 15:47:01 -0700623 public static Element U8_4(RenderScript rs) {
624 if(rs.mElement_UCHAR_4 == null) {
625 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800626 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700627 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800628 }
629
Stephen Hines836c4a52011-06-01 14:38:10 -0700630 public static Element I8_2(RenderScript rs) {
631 if(rs.mElement_CHAR_2 == null) {
632 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
633 }
634 return rs.mElement_CHAR_2;
635 }
636
637 public static Element I8_3(RenderScript rs) {
638 if(rs.mElement_CHAR_3 == null) {
639 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
640 }
641 return rs.mElement_CHAR_3;
642 }
643
644 public static Element I8_4(RenderScript rs) {
645 if(rs.mElement_CHAR_4 == null) {
646 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
647 }
648 return rs.mElement_CHAR_4;
649 }
650
651 public static Element U16_2(RenderScript rs) {
652 if(rs.mElement_USHORT_2 == null) {
653 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
654 }
655 return rs.mElement_USHORT_2;
656 }
657
658 public static Element U16_3(RenderScript rs) {
659 if(rs.mElement_USHORT_3 == null) {
660 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
661 }
662 return rs.mElement_USHORT_3;
663 }
664
665 public static Element U16_4(RenderScript rs) {
666 if(rs.mElement_USHORT_4 == null) {
667 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
668 }
669 return rs.mElement_USHORT_4;
670 }
671
672 public static Element I16_2(RenderScript rs) {
673 if(rs.mElement_SHORT_2 == null) {
674 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
675 }
676 return rs.mElement_SHORT_2;
677 }
678
679 public static Element I16_3(RenderScript rs) {
680 if(rs.mElement_SHORT_3 == null) {
681 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
682 }
683 return rs.mElement_SHORT_3;
684 }
685
686 public static Element I16_4(RenderScript rs) {
687 if(rs.mElement_SHORT_4 == null) {
688 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
689 }
690 return rs.mElement_SHORT_4;
691 }
692
693 public static Element U32_2(RenderScript rs) {
694 if(rs.mElement_UINT_2 == null) {
695 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
696 }
697 return rs.mElement_UINT_2;
698 }
699
700 public static Element U32_3(RenderScript rs) {
701 if(rs.mElement_UINT_3 == null) {
702 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
703 }
704 return rs.mElement_UINT_3;
705 }
706
707 public static Element U32_4(RenderScript rs) {
708 if(rs.mElement_UINT_4 == null) {
709 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
710 }
711 return rs.mElement_UINT_4;
712 }
713
714 public static Element I32_2(RenderScript rs) {
715 if(rs.mElement_INT_2 == null) {
716 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
717 }
718 return rs.mElement_INT_2;
719 }
720
721 public static Element I32_3(RenderScript rs) {
722 if(rs.mElement_INT_3 == null) {
723 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
724 }
725 return rs.mElement_INT_3;
726 }
727
728 public static Element I32_4(RenderScript rs) {
729 if(rs.mElement_INT_4 == null) {
730 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
731 }
732 return rs.mElement_INT_4;
733 }
734
735 public static Element U64_2(RenderScript rs) {
736 if(rs.mElement_ULONG_2 == null) {
737 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
738 }
739 return rs.mElement_ULONG_2;
740 }
741
742 public static Element U64_3(RenderScript rs) {
743 if(rs.mElement_ULONG_3 == null) {
744 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
745 }
746 return rs.mElement_ULONG_3;
747 }
748
749 public static Element U64_4(RenderScript rs) {
750 if(rs.mElement_ULONG_4 == null) {
751 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
752 }
753 return rs.mElement_ULONG_4;
754 }
755
756 public static Element I64_2(RenderScript rs) {
757 if(rs.mElement_LONG_2 == null) {
758 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
759 }
760 return rs.mElement_LONG_2;
761 }
762
763 public static Element I64_3(RenderScript rs) {
764 if(rs.mElement_LONG_3 == null) {
765 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
766 }
767 return rs.mElement_LONG_3;
768 }
769
770 public static Element I64_4(RenderScript rs) {
771 if(rs.mElement_LONG_4 == null) {
772 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
773 }
774 return rs.mElement_LONG_4;
775 }
776
Tim Murray932e78e2013-09-03 11:42:26 -0700777 public static Element YUV(RenderScript rs) {
778 if (rs.mElement_YUV == null) {
779 rs.mElement_YUV = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_YUV);
780 }
781 return rs.mElement_YUV;
782 }
783
Jason Sams1d45c472010-08-25 14:31:48 -0700784 public static Element MATRIX_4X4(RenderScript rs) {
785 if(rs.mElement_MATRIX_4X4 == null) {
786 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
787 }
788 return rs.mElement_MATRIX_4X4;
789 }
Jason Sams65c80f82012-05-08 17:30:26 -0700790
791 /** @deprecated use MATRIX_4X4
792 */
Jason Sams1d45c472010-08-25 14:31:48 -0700793 public static Element MATRIX4X4(RenderScript rs) {
794 return MATRIX_4X4(rs);
795 }
796
797 public static Element MATRIX_3X3(RenderScript rs) {
798 if(rs.mElement_MATRIX_3X3 == null) {
799 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
800 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800801 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700802 }
803
804 public static Element MATRIX_2X2(RenderScript rs) {
805 if(rs.mElement_MATRIX_2X2 == null) {
806 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
807 }
808 return rs.mElement_MATRIX_2X2;
809 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800810
Tim Murray460a0492013-11-19 12:45:54 -0800811 Element(long id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700812 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700813 mSize = 0;
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -0800814 mVectorSize = 1;
Jason Sams718cd1f2009-12-23 14:35:29 -0800815 mElements = e;
816 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700817 mArraySizes = as;
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800818 mType = DataType.NONE;
819 mKind = DataKind.USER;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700820 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800821 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700822 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700823 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800824 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800825 updateVisibleSubElements();
Jason Sams718cd1f2009-12-23 14:35:29 -0800826 }
827
Tim Murray460a0492013-11-19 12:45:54 -0800828 Element(long id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700829 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800830 if ((dt != DataType.UNSIGNED_5_6_5) &&
831 (dt != DataType.UNSIGNED_4_4_4_4) &&
832 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800833 if (size == 3) {
834 mSize = dt.mSize * 4;
835 } else {
836 mSize = dt.mSize * size;
837 }
Jason Sams252c0782011-01-11 17:42:52 -0800838 } else {
839 mSize = dt.mSize;
840 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800841 mType = dt;
842 mKind = dk;
843 mNormalized = norm;
844 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700845 }
846
Tim Murray460a0492013-11-19 12:45:54 -0800847 Element(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700848 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700849 }
850
851 @Override
852 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800853 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700854
855 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
856 int[] dataBuffer = new int[5];
Jason Samse07694b2012-04-03 15:36:36 -0700857 mRS.nElementGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700858
859 mNormalized = dataBuffer[2] == 1 ? true : false;
860 mVectorSize = dataBuffer[3];
861 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700862 for (DataType dt: DataType.values()) {
863 if(dt.mID == dataBuffer[0]){
864 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700865 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700866 }
867 }
868 for (DataKind dk: DataKind.values()) {
869 if(dk.mID == dataBuffer[1]){
870 mKind = dk;
871 }
872 }
873
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700874 int numSubElements = dataBuffer[4];
875 if(numSubElements > 0) {
876 mElements = new Element[numSubElements];
877 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700878 mArraySizes = new int[numSubElements];
879 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700880
Ashok Bhat98071552014-02-12 09:54:43 +0000881 long[] subElementIds = new long[numSubElements];
Jason Samse07694b2012-04-03 15:36:36 -0700882 mRS.nElementGetSubElements(getID(mRS), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700883 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700884 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700885 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700886 mOffsetInBytes[i] = mSize;
887 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700888 }
889 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800890 updateVisibleSubElements();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700891 }
892
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700893 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800894 * Create a custom Element of the specified DataType. The DataKind will be
895 * set to USER and the vector size to 1 indicating non-vector.
896 *
897 * @param rs The context associated with the new Element.
898 * @param dt The DataType for the new element.
899 * @return Element
900 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800901 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700902 DataKind dk = DataKind.USER;
903 boolean norm = false;
904 int vecSize = 1;
Tim Murray460a0492013-11-19 12:45:54 -0800905 long id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700906 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800907 }
908
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700909 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800910 * Create a custom vector element of the specified DataType and vector size.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800911 * DataKind will be set to USER. Only primitive types (FLOAT_32, FLOAT_64,
912 * SIGNED_8, SIGNED_16, SIGNED_32, SIGNED_64, UNSIGNED_8, UNSIGNED_16,
913 * UNSIGNED_32, UNSIGNED_64, BOOLEAN) are supported.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800914 *
915 * @param rs The context associated with the new Element.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800916 * @param dt The DataType for the new Element.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800917 * @param size Vector size for the new Element. Range 2-4 inclusive
918 * supported.
919 *
920 * @return Element
921 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800922 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800923 if (size < 2 || size > 4) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800924 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700925 }
Stephen Hines3beb60e2012-02-14 20:38:20 -0800926
927 switch (dt) {
928 // Support only primitive integer/float/boolean types as vectors.
929 case FLOAT_32:
930 case FLOAT_64:
931 case SIGNED_8:
932 case SIGNED_16:
933 case SIGNED_32:
934 case SIGNED_64:
935 case UNSIGNED_8:
936 case UNSIGNED_16:
937 case UNSIGNED_32:
938 case UNSIGNED_64:
939 case BOOLEAN: {
940 DataKind dk = DataKind.USER;
941 boolean norm = false;
Tim Murray460a0492013-11-19 12:45:54 -0800942 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Stephen Hines3beb60e2012-02-14 20:38:20 -0800943 return new Element(id, rs, dt, dk, norm, size);
944 }
945
946 default: {
947 throw new RSIllegalArgumentException("Cannot create vector of " +
948 "non-primitive type.");
949 }
950 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700951 }
952
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700953 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800954 * Create a new pixel Element type. A matching DataType and DataKind must
955 * be provided. The DataType and DataKind must contain the same number of
956 * components. Vector size will be set to 1.
957 *
958 * @param rs The context associated with the new Element.
959 * @param dt The DataType for the new element.
960 * @param dk The DataKind to specify the mapping of each component in the
961 * DataType.
962 *
963 * @return Element
964 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800965 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800966 if (!(dk == DataKind.PIXEL_L ||
967 dk == DataKind.PIXEL_A ||
968 dk == DataKind.PIXEL_LA ||
969 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700970 dk == DataKind.PIXEL_RGBA ||
Jason Samsdd6c8b32013-02-15 17:27:24 -0800971 dk == DataKind.PIXEL_DEPTH ||
972 dk == DataKind.PIXEL_YUV)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700973 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800974 }
975 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700976 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800977 dt == DataType.UNSIGNED_5_6_5 ||
978 dt == DataType.UNSIGNED_4_4_4_4 ||
979 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700980 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800981 }
982 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700983 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800984 }
985 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700986 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800987 }
988 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700989 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800990 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700991 if (dt == DataType.UNSIGNED_16 &&
992 dk != DataKind.PIXEL_DEPTH) {
993 throw new RSIllegalArgumentException("Bad kind and type combo");
994 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800995
996 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700997 switch (dk) {
998 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800999 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -07001000 break;
1001 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -08001002 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -07001003 break;
1004 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -08001005 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -07001006 break;
1007 case PIXEL_DEPTH:
1008 size = 2;
1009 break;
Jason Sams718cd1f2009-12-23 14:35:29 -08001010 }
1011
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001012 boolean norm = true;
Tim Murray460a0492013-11-19 12:45:54 -08001013 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001014 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -08001015 }
Jason Sams36e612a2009-07-31 16:26:13 -07001016
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001017 /**
Stephen Hinesf257e512011-06-14 14:54:29 -07001018 * Check if the current Element is compatible with another Element.
1019 * Primitive Elements are compatible if they share the same underlying
1020 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
1021 * must be equal in order to be compatible. This requires strict name
1022 * equivalence for all sub-Elements (in addition to structural equivalence).
1023 *
1024 * @param e The Element to check compatibility with.
1025 *
1026 * @return boolean true if the Elements are compatible, otherwise false.
1027 */
1028 public boolean isCompatible(Element e) {
1029 // Try strict BaseObj equality to start with.
1030 if (this.equals(e)) {
1031 return true;
1032 }
1033
1034 // Ignore mKind because it is allowed to be different (user vs. pixel).
1035 // We also ignore mNormalized because it can be different. The mType
Stephen Hines20948112012-02-14 19:42:42 -08001036 // field must not be NONE since we require name equivalence for
1037 // all user-created Elements.
Stephen Hinesf257e512011-06-14 14:54:29 -07001038 return ((mSize == e.mSize) &&
Stephen Hines20948112012-02-14 19:42:42 -08001039 (mType != DataType.NONE) &&
Stephen Hinesf257e512011-06-14 14:54:29 -07001040 (mType == e.mType) &&
1041 (mVectorSize == e.mVectorSize));
1042 }
1043
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001044 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001045 * Builder class for producing complex elements with matching field and name
1046 * pairs. The builder starts empty. The order in which elements are added
1047 * is retained for the layout in memory.
1048 *
1049 */
Jason Sams36e612a2009-07-31 16:26:13 -07001050 public static class Builder {
1051 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -08001052 Element[] mElements;
1053 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -07001054 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -08001055 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001056 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -07001057
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001058 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001059 * Create a builder object.
1060 *
1061 * @param rs
1062 */
Jason Sams22534172009-08-04 16:58:20 -07001063 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -07001064 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -08001065 mCount = 0;
1066 mElements = new Element[8];
1067 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -07001068 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -07001069 }
1070
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001071 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001072 * Add an array of elements to this element.
1073 *
1074 * @param element
1075 * @param name
1076 * @param arraySize
1077 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001078 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -07001079 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -07001080 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -07001081 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001082
1083 // Skip padding fields after a vector 3 type.
1084 if (mSkipPadding != 0) {
1085 if (name.startsWith("#padding_")) {
1086 mSkipPadding = 0;
1087 return this;
1088 }
1089 }
1090
1091 if (element.mVectorSize == 3) {
1092 mSkipPadding = 1;
1093 } else {
1094 mSkipPadding = 0;
1095 }
1096
Jason Sams718cd1f2009-12-23 14:35:29 -08001097 if(mCount == mElements.length) {
1098 Element[] e = new Element[mCount + 8];
1099 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -07001100 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -08001101 System.arraycopy(mElements, 0, e, 0, mCount);
1102 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001103 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -08001104 mElements = e;
1105 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -07001106 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -07001107 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001108 mElements[mCount] = element;
1109 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -07001110 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -08001111 mCount++;
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001112 return this;
Jason Sams07ae4062009-08-27 20:23:34 -07001113 }
1114
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001115 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001116 * Add a single element to this Element.
1117 *
1118 * @param element
1119 * @param name
1120 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001121 public Builder add(Element element, String name) {
1122 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -07001123 }
1124
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001125 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001126 * Create the element from this builder.
1127 *
1128 *
1129 * @return Element
1130 */
Jason Sams22534172009-08-04 16:58:20 -07001131 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -08001132 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -08001133 Element[] ein = new Element[mCount];
1134 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -07001135 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -08001136 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1137 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001138 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001139
Ashok Bhat98071552014-02-12 09:54:43 +00001140 long[] ids = new long[ein.length];
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001141 for (int ct = 0; ct < ein.length; ct++ ) {
Ashok Bhat98071552014-02-12 09:54:43 +00001142 ids[ct] = ein[ct].getID(mRS);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001143 }
Tim Murray460a0492013-11-19 12:45:54 -08001144 long id = mRS.nElementCreate2(ids, sin, asin);
Jason Sams70d4e502010-09-02 17:35:23 -07001145 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -07001146 }
1147 }
Jason Sams36e612a2009-07-31 16:26:13 -07001148}
1149