blob: 50226acc77b4f3e1f72794a3f55b48d32e2d13f2 [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 FLOAT_16 (1, 2),
Jason Sams718cd1f2009-12-23 14:35:29 -0800123 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -0700124 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800125 SIGNED_8 (4, 1),
126 SIGNED_16 (5, 2),
127 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -0700128 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800129 UNSIGNED_8 (8, 1),
130 UNSIGNED_16 (9, 2),
131 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -0700132 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800133
Jason Samsf110d4b2010-06-21 17:42:41 -0700134 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -0800135
Jason Samsf110d4b2010-06-21 17:42:41 -0700136 UNSIGNED_5_6_5 (13, 2),
137 UNSIGNED_5_5_5_1 (14, 2),
138 UNSIGNED_4_4_4_4 (15, 2),
139
Jason Sams1d45c472010-08-25 14:31:48 -0700140 MATRIX_4X4 (16, 64),
141 MATRIX_3X3 (17, 36),
142 MATRIX_2X2 (18, 16),
143
Jason Samsb49dfea2014-06-18 13:17:57 -0700144 RS_ELEMENT (1000),
145 RS_TYPE (1001),
146 RS_ALLOCATION (1002),
147 RS_SAMPLER (1003),
148 RS_SCRIPT (1004),
149 RS_MESH (1005),
150 RS_PROGRAM_FRAGMENT (1006),
151 RS_PROGRAM_VERTEX (1007),
152 RS_PROGRAM_RASTER (1008),
153 RS_PROGRAM_STORE (1009),
154 RS_FONT (1010);
Jason Sams36e612a2009-07-31 16:26:13 -0700155
156 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800157 int mSize;
158 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700159 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800160 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700161 }
Jason Samsb49dfea2014-06-18 13:17:57 -0700162
163 DataType(int id) {
164 mID = id;
165 mSize = 4;
166 if (RenderScript.sPointerSize == 8) {
167 mSize = 32;
168 }
169 }
Jason Sams36e612a2009-07-31 16:26:13 -0700170 }
171
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700172 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800173 * The special interpretation of the data if required. This is primarly
174 * useful for graphical data. USER indicates no special interpretation is
175 * expected. PIXEL is used in conjunction with the standard data types for
176 * representing texture formats.
177 */
Jason Sams36e612a2009-07-31 16:26:13 -0700178 public enum DataKind {
179 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800180
181 PIXEL_L (7),
182 PIXEL_A (8),
183 PIXEL_LA (9),
184 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700185 PIXEL_RGBA (11),
Jason Sams8140d7b2012-12-13 17:01:09 -0800186 PIXEL_DEPTH (12),
187 PIXEL_YUV(13);
Jason Sams36e612a2009-07-31 16:26:13 -0700188
189 int mID;
190 DataKind(int id) {
191 mID = id;
192 }
193 }
194
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700195 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800196 * Return if a element is too complex for use as a data source for a Mesh or
197 * a Program.
198 *
199 * @return boolean
200 */
Jason Samsc1d62102010-11-04 14:32:19 -0700201 public boolean isComplex() {
202 if (mElements == null) {
203 return false;
204 }
205 for (int ct=0; ct < mElements.length; ct++) {
206 if (mElements[ct].mElements != null) {
207 return true;
208 }
209 }
210 return false;
211 }
212
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700213 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700214 * Elements could be simple, such as an int or a float, or a
215 * structure with multiple sub elements, such as a collection of
216 * floats, float2, float4. This function returns zero for simple
217 * elements or the number of sub-elements otherwise.
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700218 * @return number of sub-elements in this element
219 */
220 public int getSubElementCount() {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800221 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700222 return 0;
223 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800224 return mVisibleElementMap.length;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700225 }
226
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700227 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700228 * For complex elements, this function will return the
229 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700230 * @param index index of the sub-element to return
231 * @return sub-element in this element at given index
232 */
233 public Element getSubElement(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800234 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700235 throw new RSIllegalArgumentException("Element contains no sub-elements");
236 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800237 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700238 throw new RSIllegalArgumentException("Illegal sub-element index");
239 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800240 return mElements[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700241 }
242
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700243 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700244 * For complex elements, this function will return the
245 * sub-element name at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700246 * @param index index of the sub-element
247 * @return sub-element in this element at given index
248 */
249 public String getSubElementName(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800250 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700251 throw new RSIllegalArgumentException("Element contains no sub-elements");
252 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800253 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700254 throw new RSIllegalArgumentException("Illegal sub-element index");
255 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800256 return mElementNames[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700257 }
258
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700259 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700260 * For complex elements, some sub-elements could be statically
261 * sized arrays. This function will return the array size for
262 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700263 * @param index index of the sub-element
264 * @return array size of sub-element in this element at given index
265 */
266 public int getSubElementArraySize(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800267 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700268 throw new RSIllegalArgumentException("Element contains no sub-elements");
269 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800270 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700271 throw new RSIllegalArgumentException("Illegal sub-element index");
272 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800273 return mArraySizes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700274 }
275
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700276 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700277 * This function specifies the location of a sub-element within
278 * the element
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700279 * @param index index of the sub-element
280 * @return offset in bytes of sub-element in this element at given index
281 */
282 public int getSubElementOffsetBytes(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800283 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700284 throw new RSIllegalArgumentException("Element contains no sub-elements");
285 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800286 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700287 throw new RSIllegalArgumentException("Illegal sub-element index");
288 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800289 return mOffsetInBytes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700290 }
291
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700292 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800293 * @return element data type
294 */
295 public DataType getDataType() {
296 return mType;
297 }
298
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700299 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800300 * @return element data kind
301 */
302 public DataKind getDataKind() {
303 return mKind;
304 }
305
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700306 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800307 * Utility function for returning an Element containing a single Boolean.
308 *
309 * @param rs Context to which the element will belong.
310 *
311 * @return Element
312 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700313 public static Element BOOLEAN(RenderScript rs) {
314 if(rs.mElement_BOOLEAN == null) {
315 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
316 }
317 return rs.mElement_BOOLEAN;
318 }
319
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700320 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800321 * Utility function for returning an Element containing a single UNSIGNED_8.
322 *
323 * @param rs Context to which the element will belong.
324 *
325 * @return Element
326 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700327 public static Element U8(RenderScript rs) {
328 if(rs.mElement_U8 == null) {
329 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800330 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700331 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800332 }
333
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700334 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800335 * Utility function for returning an Element containing a single SIGNED_8.
336 *
337 * @param rs Context to which the element will belong.
338 *
339 * @return Element
340 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700341 public static Element I8(RenderScript rs) {
342 if(rs.mElement_I8 == null) {
343 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800344 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700345 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800346 }
347
Jason Samse29f3e72010-06-08 15:40:48 -0700348 public static Element U16(RenderScript rs) {
349 if(rs.mElement_U16 == null) {
350 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
351 }
352 return rs.mElement_U16;
353 }
354
355 public static Element I16(RenderScript rs) {
356 if(rs.mElement_I16 == null) {
357 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
358 }
359 return rs.mElement_I16;
360 }
361
Jason Sams8cb39de2010-06-01 15:47:01 -0700362 public static Element U32(RenderScript rs) {
363 if(rs.mElement_U32 == null) {
364 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800365 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700366 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800367 }
368
Jason Sams8cb39de2010-06-01 15:47:01 -0700369 public static Element I32(RenderScript rs) {
370 if(rs.mElement_I32 == null) {
371 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800372 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700373 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800374 }
375
Stephen Hines52d83632010-10-11 16:10:42 -0700376 public static Element U64(RenderScript rs) {
377 if(rs.mElement_U64 == null) {
378 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
379 }
380 return rs.mElement_U64;
381 }
382
Stephen Hinesef1dac22010-10-01 15:39:33 -0700383 public static Element I64(RenderScript rs) {
384 if(rs.mElement_I64 == null) {
385 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
386 }
387 return rs.mElement_I64;
388 }
389
Jason Samsa5835a22014-11-05 15:16:26 -0800390 public static Element F16(RenderScript rs) {
391 if(rs.mElement_F16 == null) {
392 rs.mElement_F16 = createUser(rs, DataType.FLOAT_16);
393 }
394 return rs.mElement_F16;
395 }
396
Jason Sams8cb39de2010-06-01 15:47:01 -0700397 public static Element F32(RenderScript rs) {
398 if(rs.mElement_F32 == null) {
399 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800400 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700401 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800402 }
403
Stephen Hines02f417052010-09-30 15:19:22 -0700404 public static Element F64(RenderScript rs) {
405 if(rs.mElement_F64 == null) {
406 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
407 }
408 return rs.mElement_F64;
409 }
410
Jason Sams8cb39de2010-06-01 15:47:01 -0700411 public static Element ELEMENT(RenderScript rs) {
412 if(rs.mElement_ELEMENT == null) {
413 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700414 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700415 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700416 }
417
Jason Sams8cb39de2010-06-01 15:47:01 -0700418 public static Element TYPE(RenderScript rs) {
419 if(rs.mElement_TYPE == null) {
420 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700421 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700422 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700423 }
424
Jason Sams8cb39de2010-06-01 15:47:01 -0700425 public static Element ALLOCATION(RenderScript rs) {
426 if(rs.mElement_ALLOCATION == null) {
427 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700428 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700429 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700430 }
431
Jason Sams8cb39de2010-06-01 15:47:01 -0700432 public static Element SAMPLER(RenderScript rs) {
433 if(rs.mElement_SAMPLER == null) {
434 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700435 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700436 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700437 }
438
Jason Sams8cb39de2010-06-01 15:47:01 -0700439 public static Element SCRIPT(RenderScript rs) {
440 if(rs.mElement_SCRIPT == null) {
441 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700442 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700443 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700444 }
445
Jason Sams8cb39de2010-06-01 15:47:01 -0700446 public static Element MESH(RenderScript rs) {
447 if(rs.mElement_MESH == null) {
448 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700449 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700450 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700451 }
452
Jason Sams8cb39de2010-06-01 15:47:01 -0700453 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
454 if(rs.mElement_PROGRAM_FRAGMENT == null) {
455 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700456 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700457 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700458 }
459
Jason Sams8cb39de2010-06-01 15:47:01 -0700460 public static Element PROGRAM_VERTEX(RenderScript rs) {
461 if(rs.mElement_PROGRAM_VERTEX == null) {
462 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700463 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700464 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700465 }
466
Jason Sams8cb39de2010-06-01 15:47:01 -0700467 public static Element PROGRAM_RASTER(RenderScript rs) {
468 if(rs.mElement_PROGRAM_RASTER == null) {
469 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700470 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700471 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700472 }
473
Jason Sams8cb39de2010-06-01 15:47:01 -0700474 public static Element PROGRAM_STORE(RenderScript rs) {
475 if(rs.mElement_PROGRAM_STORE == null) {
476 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700477 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700478 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700479 }
480
Stephen Hines3a291412012-04-11 17:27:29 -0700481 public static Element FONT(RenderScript rs) {
482 if(rs.mElement_FONT == null) {
483 rs.mElement_FONT = createUser(rs, DataType.RS_FONT);
484 }
485 return rs.mElement_FONT;
486 }
487
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700488
Jason Sams718cd1f2009-12-23 14:35:29 -0800489 public static Element A_8(RenderScript rs) {
490 if(rs.mElement_A_8 == null) {
491 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
492 }
493 return rs.mElement_A_8;
494 }
495
496 public static Element RGB_565(RenderScript rs) {
497 if(rs.mElement_RGB_565 == null) {
498 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
499 }
500 return rs.mElement_RGB_565;
501 }
502
503 public static Element RGB_888(RenderScript rs) {
504 if(rs.mElement_RGB_888 == null) {
505 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
506 }
507 return rs.mElement_RGB_888;
508 }
509
510 public static Element RGBA_5551(RenderScript rs) {
511 if(rs.mElement_RGBA_5551 == null) {
512 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
513 }
514 return rs.mElement_RGBA_5551;
515 }
516
517 public static Element RGBA_4444(RenderScript rs) {
518 if(rs.mElement_RGBA_4444 == null) {
519 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
520 }
521 return rs.mElement_RGBA_4444;
522 }
523
524 public static Element RGBA_8888(RenderScript rs) {
525 if(rs.mElement_RGBA_8888 == null) {
526 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
527 }
528 return rs.mElement_RGBA_8888;
529 }
530
Jason Samsa5835a22014-11-05 15:16:26 -0800531 public static Element F16_2(RenderScript rs) {
532 if(rs.mElement_HALF_2 == null) {
533 rs.mElement_HALF_2 = createVector(rs, DataType.FLOAT_16, 2);
534 }
535 return rs.mElement_HALF_2;
536 }
537
Jason Samsa5835a22014-11-05 15:16:26 -0800538 public static Element F16_3(RenderScript rs) {
Jason Sams54371b42015-05-13 13:21:30 -0700539 if(rs.mElement_HALF_3 == null) {
540 rs.mElement_HALF_3 = createVector(rs, DataType.FLOAT_16, 3);
Jason Samsa5835a22014-11-05 15:16:26 -0800541 }
542 return rs.mElement_HALF_3;
543 }
544
Jason Samsa5835a22014-11-05 15:16:26 -0800545 public static Element F16_4(RenderScript rs) {
546 if(rs.mElement_HALF_4 == null) {
547 rs.mElement_HALF_4 = createVector(rs, DataType.FLOAT_16, 4);
548 }
549 return rs.mElement_HALF_4;
550 }
551
Jason Sams8cb39de2010-06-01 15:47:01 -0700552 public static Element F32_2(RenderScript rs) {
553 if(rs.mElement_FLOAT_2 == null) {
554 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800555 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700556 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800557 }
558
Jason Sams8cb39de2010-06-01 15:47:01 -0700559 public static Element F32_3(RenderScript rs) {
560 if(rs.mElement_FLOAT_3 == null) {
561 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800562 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700563 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800564 }
565
Jason Sams8cb39de2010-06-01 15:47:01 -0700566 public static Element F32_4(RenderScript rs) {
567 if(rs.mElement_FLOAT_4 == null) {
568 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800569 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700570 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800571 }
572
Stephen Hines836c4a52011-06-01 14:38:10 -0700573 public static Element F64_2(RenderScript rs) {
574 if(rs.mElement_DOUBLE_2 == null) {
575 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
576 }
577 return rs.mElement_DOUBLE_2;
578 }
579
580 public static Element F64_3(RenderScript rs) {
581 if(rs.mElement_DOUBLE_3 == null) {
582 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
583 }
584 return rs.mElement_DOUBLE_3;
585 }
586
587 public static Element F64_4(RenderScript rs) {
588 if(rs.mElement_DOUBLE_4 == null) {
589 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
590 }
591 return rs.mElement_DOUBLE_4;
592 }
593
594 public static Element U8_2(RenderScript rs) {
595 if(rs.mElement_UCHAR_2 == null) {
596 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
597 }
598 return rs.mElement_UCHAR_2;
599 }
600
601 public static Element U8_3(RenderScript rs) {
602 if(rs.mElement_UCHAR_3 == null) {
603 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
604 }
605 return rs.mElement_UCHAR_3;
606 }
607
Jason Sams8cb39de2010-06-01 15:47:01 -0700608 public static Element U8_4(RenderScript rs) {
609 if(rs.mElement_UCHAR_4 == null) {
610 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800611 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700612 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800613 }
614
Stephen Hines836c4a52011-06-01 14:38:10 -0700615 public static Element I8_2(RenderScript rs) {
616 if(rs.mElement_CHAR_2 == null) {
617 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
618 }
619 return rs.mElement_CHAR_2;
620 }
621
622 public static Element I8_3(RenderScript rs) {
623 if(rs.mElement_CHAR_3 == null) {
624 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
625 }
626 return rs.mElement_CHAR_3;
627 }
628
629 public static Element I8_4(RenderScript rs) {
630 if(rs.mElement_CHAR_4 == null) {
631 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
632 }
633 return rs.mElement_CHAR_4;
634 }
635
636 public static Element U16_2(RenderScript rs) {
637 if(rs.mElement_USHORT_2 == null) {
638 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
639 }
640 return rs.mElement_USHORT_2;
641 }
642
643 public static Element U16_3(RenderScript rs) {
644 if(rs.mElement_USHORT_3 == null) {
645 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
646 }
647 return rs.mElement_USHORT_3;
648 }
649
650 public static Element U16_4(RenderScript rs) {
651 if(rs.mElement_USHORT_4 == null) {
652 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
653 }
654 return rs.mElement_USHORT_4;
655 }
656
657 public static Element I16_2(RenderScript rs) {
658 if(rs.mElement_SHORT_2 == null) {
659 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
660 }
661 return rs.mElement_SHORT_2;
662 }
663
664 public static Element I16_3(RenderScript rs) {
665 if(rs.mElement_SHORT_3 == null) {
666 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
667 }
668 return rs.mElement_SHORT_3;
669 }
670
671 public static Element I16_4(RenderScript rs) {
672 if(rs.mElement_SHORT_4 == null) {
673 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
674 }
675 return rs.mElement_SHORT_4;
676 }
677
678 public static Element U32_2(RenderScript rs) {
679 if(rs.mElement_UINT_2 == null) {
680 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
681 }
682 return rs.mElement_UINT_2;
683 }
684
685 public static Element U32_3(RenderScript rs) {
686 if(rs.mElement_UINT_3 == null) {
687 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
688 }
689 return rs.mElement_UINT_3;
690 }
691
692 public static Element U32_4(RenderScript rs) {
693 if(rs.mElement_UINT_4 == null) {
694 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
695 }
696 return rs.mElement_UINT_4;
697 }
698
699 public static Element I32_2(RenderScript rs) {
700 if(rs.mElement_INT_2 == null) {
701 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
702 }
703 return rs.mElement_INT_2;
704 }
705
706 public static Element I32_3(RenderScript rs) {
707 if(rs.mElement_INT_3 == null) {
708 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
709 }
710 return rs.mElement_INT_3;
711 }
712
713 public static Element I32_4(RenderScript rs) {
714 if(rs.mElement_INT_4 == null) {
715 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
716 }
717 return rs.mElement_INT_4;
718 }
719
720 public static Element U64_2(RenderScript rs) {
721 if(rs.mElement_ULONG_2 == null) {
722 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
723 }
724 return rs.mElement_ULONG_2;
725 }
726
727 public static Element U64_3(RenderScript rs) {
728 if(rs.mElement_ULONG_3 == null) {
729 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
730 }
731 return rs.mElement_ULONG_3;
732 }
733
734 public static Element U64_4(RenderScript rs) {
735 if(rs.mElement_ULONG_4 == null) {
736 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
737 }
738 return rs.mElement_ULONG_4;
739 }
740
741 public static Element I64_2(RenderScript rs) {
742 if(rs.mElement_LONG_2 == null) {
743 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
744 }
745 return rs.mElement_LONG_2;
746 }
747
748 public static Element I64_3(RenderScript rs) {
749 if(rs.mElement_LONG_3 == null) {
750 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
751 }
752 return rs.mElement_LONG_3;
753 }
754
755 public static Element I64_4(RenderScript rs) {
756 if(rs.mElement_LONG_4 == null) {
757 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
758 }
759 return rs.mElement_LONG_4;
760 }
761
Tim Murray932e78e2013-09-03 11:42:26 -0700762 public static Element YUV(RenderScript rs) {
763 if (rs.mElement_YUV == null) {
764 rs.mElement_YUV = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_YUV);
765 }
766 return rs.mElement_YUV;
767 }
768
Jason Sams1d45c472010-08-25 14:31:48 -0700769 public static Element MATRIX_4X4(RenderScript rs) {
770 if(rs.mElement_MATRIX_4X4 == null) {
771 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
772 }
773 return rs.mElement_MATRIX_4X4;
774 }
Jason Sams65c80f82012-05-08 17:30:26 -0700775
776 /** @deprecated use MATRIX_4X4
777 */
Jason Sams1d45c472010-08-25 14:31:48 -0700778 public static Element MATRIX4X4(RenderScript rs) {
779 return MATRIX_4X4(rs);
780 }
781
782 public static Element MATRIX_3X3(RenderScript rs) {
783 if(rs.mElement_MATRIX_3X3 == null) {
784 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
785 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800786 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700787 }
788
789 public static Element MATRIX_2X2(RenderScript rs) {
790 if(rs.mElement_MATRIX_2X2 == null) {
791 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
792 }
793 return rs.mElement_MATRIX_2X2;
794 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800795
Tim Murray460a0492013-11-19 12:45:54 -0800796 Element(long id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700797 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700798 mSize = 0;
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -0800799 mVectorSize = 1;
Jason Sams718cd1f2009-12-23 14:35:29 -0800800 mElements = e;
801 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700802 mArraySizes = as;
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800803 mType = DataType.NONE;
804 mKind = DataKind.USER;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700805 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800806 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700807 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700808 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800809 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800810 updateVisibleSubElements();
Yang Ni6484b6b2016-03-24 09:40:32 -0700811 guard.open("destroy");
Jason Sams718cd1f2009-12-23 14:35:29 -0800812 }
813
Tim Murray460a0492013-11-19 12:45:54 -0800814 Element(long id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700815 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800816 if ((dt != DataType.UNSIGNED_5_6_5) &&
817 (dt != DataType.UNSIGNED_4_4_4_4) &&
818 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800819 if (size == 3) {
820 mSize = dt.mSize * 4;
821 } else {
822 mSize = dt.mSize * size;
823 }
Jason Sams252c0782011-01-11 17:42:52 -0800824 } else {
825 mSize = dt.mSize;
826 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800827 mType = dt;
828 mKind = dk;
829 mNormalized = norm;
830 mVectorSize = size;
Yang Ni6484b6b2016-03-24 09:40:32 -0700831 guard.open("destroy");
Jason Sams36e612a2009-07-31 16:26:13 -0700832 }
833
Tim Murray460a0492013-11-19 12:45:54 -0800834 Element(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700835 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700836 }
837
838 @Override
839 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800840 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700841
842 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
843 int[] dataBuffer = new int[5];
Jason Samse07694b2012-04-03 15:36:36 -0700844 mRS.nElementGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700845
846 mNormalized = dataBuffer[2] == 1 ? true : false;
847 mVectorSize = dataBuffer[3];
848 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700849 for (DataType dt: DataType.values()) {
850 if(dt.mID == dataBuffer[0]){
851 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700852 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700853 }
854 }
855 for (DataKind dk: DataKind.values()) {
856 if(dk.mID == dataBuffer[1]){
857 mKind = dk;
858 }
859 }
860
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700861 int numSubElements = dataBuffer[4];
862 if(numSubElements > 0) {
863 mElements = new Element[numSubElements];
864 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700865 mArraySizes = new int[numSubElements];
866 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700867
Ashok Bhat98071552014-02-12 09:54:43 +0000868 long[] subElementIds = new long[numSubElements];
Jason Samse07694b2012-04-03 15:36:36 -0700869 mRS.nElementGetSubElements(getID(mRS), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700870 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700871 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700872 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700873 mOffsetInBytes[i] = mSize;
874 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700875 }
876 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800877 updateVisibleSubElements();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700878 }
879
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700880 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800881 * Create a custom Element of the specified DataType. The DataKind will be
882 * set to USER and the vector size to 1 indicating non-vector.
883 *
884 * @param rs The context associated with the new Element.
885 * @param dt The DataType for the new element.
886 * @return Element
887 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800888 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700889 DataKind dk = DataKind.USER;
890 boolean norm = false;
891 int vecSize = 1;
Tim Murray460a0492013-11-19 12:45:54 -0800892 long id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700893 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800894 }
895
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700896 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800897 * Create a custom vector element of the specified DataType and vector size.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800898 * DataKind will be set to USER. Only primitive types (FLOAT_32, FLOAT_64,
899 * SIGNED_8, SIGNED_16, SIGNED_32, SIGNED_64, UNSIGNED_8, UNSIGNED_16,
900 * UNSIGNED_32, UNSIGNED_64, BOOLEAN) are supported.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800901 *
902 * @param rs The context associated with the new Element.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800903 * @param dt The DataType for the new Element.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800904 * @param size Vector size for the new Element. Range 2-4 inclusive
905 * supported.
906 *
907 * @return Element
908 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800909 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800910 if (size < 2 || size > 4) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800911 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700912 }
Stephen Hines3beb60e2012-02-14 20:38:20 -0800913
914 switch (dt) {
915 // Support only primitive integer/float/boolean types as vectors.
Jason Sams54371b42015-05-13 13:21:30 -0700916 case FLOAT_16:
Stephen Hines3beb60e2012-02-14 20:38:20 -0800917 case FLOAT_32:
918 case FLOAT_64:
919 case SIGNED_8:
920 case SIGNED_16:
921 case SIGNED_32:
922 case SIGNED_64:
923 case UNSIGNED_8:
924 case UNSIGNED_16:
925 case UNSIGNED_32:
926 case UNSIGNED_64:
927 case BOOLEAN: {
928 DataKind dk = DataKind.USER;
929 boolean norm = false;
Tim Murray460a0492013-11-19 12:45:54 -0800930 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Stephen Hines3beb60e2012-02-14 20:38:20 -0800931 return new Element(id, rs, dt, dk, norm, size);
932 }
933
934 default: {
935 throw new RSIllegalArgumentException("Cannot create vector of " +
936 "non-primitive type.");
937 }
938 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700939 }
940
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700941 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800942 * Create a new pixel Element type. A matching DataType and DataKind must
943 * be provided. The DataType and DataKind must contain the same number of
944 * components. Vector size will be set to 1.
945 *
946 * @param rs The context associated with the new Element.
947 * @param dt The DataType for the new element.
948 * @param dk The DataKind to specify the mapping of each component in the
949 * DataType.
950 *
951 * @return Element
952 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800953 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800954 if (!(dk == DataKind.PIXEL_L ||
955 dk == DataKind.PIXEL_A ||
956 dk == DataKind.PIXEL_LA ||
957 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700958 dk == DataKind.PIXEL_RGBA ||
Jason Samsdd6c8b32013-02-15 17:27:24 -0800959 dk == DataKind.PIXEL_DEPTH ||
960 dk == DataKind.PIXEL_YUV)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700961 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800962 }
963 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700964 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800965 dt == DataType.UNSIGNED_5_6_5 ||
966 dt == DataType.UNSIGNED_4_4_4_4 ||
967 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700968 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800969 }
970 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700971 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800972 }
973 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700974 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800975 }
976 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700977 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800978 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700979 if (dt == DataType.UNSIGNED_16 &&
980 dk != DataKind.PIXEL_DEPTH) {
981 throw new RSIllegalArgumentException("Bad kind and type combo");
982 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800983
984 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700985 switch (dk) {
986 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800987 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700988 break;
989 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800990 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700991 break;
992 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800993 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700994 break;
995 case PIXEL_DEPTH:
996 size = 2;
997 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800998 }
999
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001000 boolean norm = true;
Tim Murray460a0492013-11-19 12:45:54 -08001001 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001002 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -08001003 }
Jason Sams36e612a2009-07-31 16:26:13 -07001004
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001005 /**
Stephen Hinesf257e512011-06-14 14:54:29 -07001006 * Check if the current Element is compatible with another Element.
1007 * Primitive Elements are compatible if they share the same underlying
1008 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
1009 * must be equal in order to be compatible. This requires strict name
1010 * equivalence for all sub-Elements (in addition to structural equivalence).
1011 *
1012 * @param e The Element to check compatibility with.
1013 *
1014 * @return boolean true if the Elements are compatible, otherwise false.
1015 */
1016 public boolean isCompatible(Element e) {
1017 // Try strict BaseObj equality to start with.
1018 if (this.equals(e)) {
1019 return true;
1020 }
1021
1022 // Ignore mKind because it is allowed to be different (user vs. pixel).
1023 // We also ignore mNormalized because it can be different. The mType
Stephen Hines20948112012-02-14 19:42:42 -08001024 // field must not be NONE since we require name equivalence for
1025 // all user-created Elements.
Stephen Hinesf257e512011-06-14 14:54:29 -07001026 return ((mSize == e.mSize) &&
Stephen Hines20948112012-02-14 19:42:42 -08001027 (mType != DataType.NONE) &&
Stephen Hinesf257e512011-06-14 14:54:29 -07001028 (mType == e.mType) &&
1029 (mVectorSize == e.mVectorSize));
1030 }
1031
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001032 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001033 * Builder class for producing complex elements with matching field and name
1034 * pairs. The builder starts empty. The order in which elements are added
1035 * is retained for the layout in memory.
1036 *
1037 */
Jason Sams36e612a2009-07-31 16:26:13 -07001038 public static class Builder {
1039 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -08001040 Element[] mElements;
1041 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -07001042 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -08001043 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001044 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -07001045
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001046 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001047 * Create a builder object.
1048 *
1049 * @param rs
1050 */
Jason Sams22534172009-08-04 16:58:20 -07001051 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -07001052 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -08001053 mCount = 0;
1054 mElements = new Element[8];
1055 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -07001056 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -07001057 }
1058
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001059 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001060 * Add an array of elements to this element.
1061 *
1062 * @param element
1063 * @param name
1064 * @param arraySize
1065 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001066 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -07001067 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -07001068 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -07001069 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001070
1071 // Skip padding fields after a vector 3 type.
1072 if (mSkipPadding != 0) {
1073 if (name.startsWith("#padding_")) {
1074 mSkipPadding = 0;
1075 return this;
1076 }
1077 }
1078
1079 if (element.mVectorSize == 3) {
1080 mSkipPadding = 1;
1081 } else {
1082 mSkipPadding = 0;
1083 }
1084
Jason Sams718cd1f2009-12-23 14:35:29 -08001085 if(mCount == mElements.length) {
1086 Element[] e = new Element[mCount + 8];
1087 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -07001088 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -08001089 System.arraycopy(mElements, 0, e, 0, mCount);
1090 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001091 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -08001092 mElements = e;
1093 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -07001094 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -07001095 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001096 mElements[mCount] = element;
1097 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -07001098 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -08001099 mCount++;
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001100 return this;
Jason Sams07ae4062009-08-27 20:23:34 -07001101 }
1102
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001103 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001104 * Add a single element to this Element.
1105 *
1106 * @param element
1107 * @param name
1108 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001109 public Builder add(Element element, String name) {
1110 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -07001111 }
1112
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001113 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001114 * Create the element from this builder.
1115 *
1116 *
1117 * @return Element
1118 */
Jason Sams22534172009-08-04 16:58:20 -07001119 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -08001120 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -08001121 Element[] ein = new Element[mCount];
1122 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -07001123 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -08001124 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1125 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001126 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001127
Ashok Bhat98071552014-02-12 09:54:43 +00001128 long[] ids = new long[ein.length];
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001129 for (int ct = 0; ct < ein.length; ct++ ) {
Ashok Bhat98071552014-02-12 09:54:43 +00001130 ids[ct] = ein[ct].getID(mRS);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001131 }
Tim Murray460a0492013-11-19 12:45:54 -08001132 long id = mRS.nElementCreate2(ids, sin, asin);
Jason Sams70d4e502010-09-02 17:35:23 -07001133 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -07001134 }
1135 }
Jason Sams36e612a2009-07-31 16:26:13 -07001136}
1137