blob: aa5d687960c5d77284ceb695daf986f14a30b27b [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
Jason Sams43ee06852009-08-12 17:54:11 -070019import java.lang.reflect.Field;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070020import android.util.Log;
Jason Sams36e612a2009-07-31 16:26:13 -070021
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070022/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070023 * <p>An Element represents one item within an {@link
24 * android.renderscript.Allocation}. An Element is roughly equivalent to a C
25 * type in a RenderScript kernel. Elements may be basic or complex. Some basic
26 * elements are</p> <ul> <li>A single float value (equivalent to a float in a
27 * kernel)</li> <li>A four-element float vector (equivalent to a float4 in a
28 * kernel)</li> <li>An unsigned 32-bit integer (equivalent to an unsigned int in
29 * a kernel)</li> <li>A single signed 8-bit integer (equivalent to a char in a
30 * kernel)</li> </ul> <p>A complex element is roughly equivalent to a C struct
31 * and contains a number of basic or complex Elements. From Java code, a complex
32 * element contains a list of sub-elements and names that represents a
33 * particular data structure. Structs used in RS scripts are available to Java
34 * code by using the {@code ScriptField_structname} class that is reflected from
35 * a particular script.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080036 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070037 * <p>Basic Elements are comprised of a {@link
38 * android.renderscript.Element.DataType} and a {@link
39 * android.renderscript.Element.DataKind}. The DataType encodes C type
40 * information of an Element, while the DataKind encodes how that Element should
41 * be interpreted by a {@link android.renderscript.Sampler}. Note that {@link
42 * android.renderscript.Allocation} objects with DataKind {@link
43 * android.renderscript.Element.DataKind#USER} cannot be used as input for a
44 * {@link android.renderscript.Sampler}. In general, {@link
45 * android.renderscript.Allocation} objects that are intended for use with a
46 * {@link android.renderscript.Sampler} should use bitmap-derived Elements such
47 * as {@link android.renderscript.Element#RGBA_8888} or {@link
48 * android.renderscript#Element.A_8}.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080049 *
50 * <div class="special reference">
51 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070052 * <p>For more information about creating an application that uses RenderScript, read the
53 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080054 * </div>
Jason Sams36e612a2009-07-31 16:26:13 -070055 **/
56public class Element extends BaseObj {
Jason Samsea84a7c2009-09-04 14:42:41 -070057 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080058 Element[] mElements;
59 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -070060 int[] mArraySizes;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070061 int[] mOffsetInBytes;
Jason Sams36e612a2009-07-31 16:26:13 -070062
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -080063 int[] mVisibleElementMap;
64
Jason Sams718cd1f2009-12-23 14:35:29 -080065 DataType mType;
66 DataKind mKind;
67 boolean mNormalized;
68 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070069
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -080070 private void updateVisibleSubElements() {
71 if (mElements == null) {
72 return;
73 }
74
75 int noPaddingFieldCount = 0;
76 int fieldCount = mElementNames.length;
77 // Find out how many elements are not padding
78 for (int ct = 0; ct < fieldCount; ct ++) {
79 if (mElementNames[ct].charAt(0) != '#') {
80 noPaddingFieldCount ++;
81 }
82 }
83 mVisibleElementMap = new int[noPaddingFieldCount];
84
85 // Make a map that points us at non-padding elements
86 for (int ct = 0, ctNoPadding = 0; ct < fieldCount; ct ++) {
87 if (mElementNames[ct].charAt(0) != '#') {
88 mVisibleElementMap[ctNoPadding ++] = ct;
89 }
90 }
91 }
92
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070093 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070094 * @return element size in bytes
95 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070096 public int getBytesSize() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070097
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070098 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070099 * Returns the number of vector components. 2 for float2, 4 for
100 * float4, etc.
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -0800101 * @return element vector size
102 */
103 public int getVectorSize() {return mVectorSize;}
104
Jason Samsa1b13ed2010-11-12 14:58:37 -0800105
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700106 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800107 * DataType represents the basic type information for a basic element. The
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800108 * naming convention follows. For numeric types it is FLOAT,
109 * SIGNED, or UNSIGNED followed by the _BITS where BITS is the
110 * size of the data. BOOLEAN is a true / false (1,0)
111 * represented in an 8 bit container. The UNSIGNED variants
112 * with multiple bit definitions are for packed graphical data
113 * formats and represent vectors with per vector member sizes
114 * which are treated as a single unit for packing and alignment
115 * purposes.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800116 *
117 * MATRIX the three matrix types contain FLOAT_32 elements and are treated
118 * as 32 bits for alignment purposes.
119 *
120 * RS_* objects. 32 bit opaque handles.
121 */
Jason Sams36e612a2009-07-31 16:26:13 -0700122 public enum DataType {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800123 NONE (0, 0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800124 //FLOAT_16 (1, 2),
125 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -0700126 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800127 SIGNED_8 (4, 1),
128 SIGNED_16 (5, 2),
129 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -0700130 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800131 UNSIGNED_8 (8, 1),
132 UNSIGNED_16 (9, 2),
133 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -0700134 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800135
Jason Samsf110d4b2010-06-21 17:42:41 -0700136 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -0800137
Jason Samsf110d4b2010-06-21 17:42:41 -0700138 UNSIGNED_5_6_5 (13, 2),
139 UNSIGNED_5_5_5_1 (14, 2),
140 UNSIGNED_4_4_4_4 (15, 2),
141
Jason Sams1d45c472010-08-25 14:31:48 -0700142 MATRIX_4X4 (16, 64),
143 MATRIX_3X3 (17, 36),
144 MATRIX_2X2 (18, 16),
145
146 RS_ELEMENT (1000, 4),
147 RS_TYPE (1001, 4),
148 RS_ALLOCATION (1002, 4),
149 RS_SAMPLER (1003, 4),
150 RS_SCRIPT (1004, 4),
151 RS_MESH (1005, 4),
152 RS_PROGRAM_FRAGMENT (1006, 4),
153 RS_PROGRAM_VERTEX (1007, 4),
154 RS_PROGRAM_RASTER (1008, 4),
Stephen Hines3a291412012-04-11 17:27:29 -0700155 RS_PROGRAM_STORE (1009, 4),
156 RS_FONT (1010, 4);
Jason Sams36e612a2009-07-31 16:26:13 -0700157
158 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800159 int mSize;
160 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700161 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800162 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700163 }
164 }
165
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700166 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800167 * The special interpretation of the data if required. This is primarly
168 * useful for graphical data. USER indicates no special interpretation is
169 * expected. PIXEL is used in conjunction with the standard data types for
170 * representing texture formats.
171 */
Jason Sams36e612a2009-07-31 16:26:13 -0700172 public enum DataKind {
173 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800174
175 PIXEL_L (7),
176 PIXEL_A (8),
177 PIXEL_LA (9),
178 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700179 PIXEL_RGBA (11),
Jason Sams8140d7b2012-12-13 17:01:09 -0800180 PIXEL_DEPTH (12),
181 PIXEL_YUV(13);
Jason Sams36e612a2009-07-31 16:26:13 -0700182
183 int mID;
184 DataKind(int id) {
185 mID = id;
186 }
187 }
188
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700189 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800190 * Return if a element is too complex for use as a data source for a Mesh or
191 * a Program.
192 *
193 * @return boolean
194 */
Jason Samsc1d62102010-11-04 14:32:19 -0700195 public boolean isComplex() {
196 if (mElements == null) {
197 return false;
198 }
199 for (int ct=0; ct < mElements.length; ct++) {
200 if (mElements[ct].mElements != null) {
201 return true;
202 }
203 }
204 return false;
205 }
206
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700207 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700208 * Elements could be simple, such as an int or a float, or a
209 * structure with multiple sub elements, such as a collection of
210 * floats, float2, float4. This function returns zero for simple
211 * elements or the number of sub-elements otherwise.
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700212 * @return number of sub-elements in this element
213 */
214 public int getSubElementCount() {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800215 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700216 return 0;
217 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800218 return mVisibleElementMap.length;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700219 }
220
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700221 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700222 * For complex elements, this function will return the
223 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700224 * @param index index of the sub-element to return
225 * @return sub-element in this element at given index
226 */
227 public Element getSubElement(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800228 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700229 throw new RSIllegalArgumentException("Element contains no sub-elements");
230 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800231 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700232 throw new RSIllegalArgumentException("Illegal sub-element index");
233 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800234 return mElements[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700235 }
236
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700237 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700238 * For complex elements, this function will return the
239 * sub-element name at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700240 * @param index index of the sub-element
241 * @return sub-element in this element at given index
242 */
243 public String getSubElementName(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800244 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700245 throw new RSIllegalArgumentException("Element contains no sub-elements");
246 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800247 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700248 throw new RSIllegalArgumentException("Illegal sub-element index");
249 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800250 return mElementNames[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700251 }
252
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700253 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700254 * For complex elements, some sub-elements could be statically
255 * sized arrays. This function will return the array size for
256 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700257 * @param index index of the sub-element
258 * @return array size of sub-element in this element at given index
259 */
260 public int getSubElementArraySize(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800261 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700262 throw new RSIllegalArgumentException("Element contains no sub-elements");
263 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800264 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700265 throw new RSIllegalArgumentException("Illegal sub-element index");
266 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800267 return mArraySizes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700268 }
269
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700270 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700271 * This function specifies the location of a sub-element within
272 * the element
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700273 * @param index index of the sub-element
274 * @return offset in bytes of sub-element in this element at given index
275 */
276 public int getSubElementOffsetBytes(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800277 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700278 throw new RSIllegalArgumentException("Element contains no sub-elements");
279 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800280 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700281 throw new RSIllegalArgumentException("Illegal sub-element index");
282 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800283 return mOffsetInBytes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700284 }
285
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700286 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800287 * @return element data type
288 */
289 public DataType getDataType() {
290 return mType;
291 }
292
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700293 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800294 * @return element data kind
295 */
296 public DataKind getDataKind() {
297 return mKind;
298 }
299
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700300 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800301 * Utility function for returning an Element containing a single Boolean.
302 *
303 * @param rs Context to which the element will belong.
304 *
305 * @return Element
306 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700307 public static Element BOOLEAN(RenderScript rs) {
308 if(rs.mElement_BOOLEAN == null) {
309 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
310 }
311 return rs.mElement_BOOLEAN;
312 }
313
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700314 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800315 * Utility function for returning an Element containing a single UNSIGNED_8.
316 *
317 * @param rs Context to which the element will belong.
318 *
319 * @return Element
320 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700321 public static Element U8(RenderScript rs) {
322 if(rs.mElement_U8 == null) {
323 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800324 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700325 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800326 }
327
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700328 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800329 * Utility function for returning an Element containing a single SIGNED_8.
330 *
331 * @param rs Context to which the element will belong.
332 *
333 * @return Element
334 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700335 public static Element I8(RenderScript rs) {
336 if(rs.mElement_I8 == null) {
337 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800338 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700339 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800340 }
341
Jason Samse29f3e72010-06-08 15:40:48 -0700342 public static Element U16(RenderScript rs) {
343 if(rs.mElement_U16 == null) {
344 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
345 }
346 return rs.mElement_U16;
347 }
348
349 public static Element I16(RenderScript rs) {
350 if(rs.mElement_I16 == null) {
351 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
352 }
353 return rs.mElement_I16;
354 }
355
Jason Sams8cb39de2010-06-01 15:47:01 -0700356 public static Element U32(RenderScript rs) {
357 if(rs.mElement_U32 == null) {
358 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800359 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700360 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800361 }
362
Jason Sams8cb39de2010-06-01 15:47:01 -0700363 public static Element I32(RenderScript rs) {
364 if(rs.mElement_I32 == null) {
365 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800366 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700367 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800368 }
369
Stephen Hines52d83632010-10-11 16:10:42 -0700370 public static Element U64(RenderScript rs) {
371 if(rs.mElement_U64 == null) {
372 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
373 }
374 return rs.mElement_U64;
375 }
376
Stephen Hinesef1dac22010-10-01 15:39:33 -0700377 public static Element I64(RenderScript rs) {
378 if(rs.mElement_I64 == null) {
379 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
380 }
381 return rs.mElement_I64;
382 }
383
Jason Sams8cb39de2010-06-01 15:47:01 -0700384 public static Element F32(RenderScript rs) {
385 if(rs.mElement_F32 == null) {
386 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800387 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700388 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800389 }
390
Stephen Hines02f417052010-09-30 15:19:22 -0700391 public static Element F64(RenderScript rs) {
392 if(rs.mElement_F64 == null) {
393 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
394 }
395 return rs.mElement_F64;
396 }
397
Jason Sams8cb39de2010-06-01 15:47:01 -0700398 public static Element ELEMENT(RenderScript rs) {
399 if(rs.mElement_ELEMENT == null) {
400 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700401 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700402 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700403 }
404
Jason Sams8cb39de2010-06-01 15:47:01 -0700405 public static Element TYPE(RenderScript rs) {
406 if(rs.mElement_TYPE == null) {
407 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700408 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700409 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700410 }
411
Jason Sams8cb39de2010-06-01 15:47:01 -0700412 public static Element ALLOCATION(RenderScript rs) {
413 if(rs.mElement_ALLOCATION == null) {
414 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700415 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700416 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700417 }
418
Jason Sams8cb39de2010-06-01 15:47:01 -0700419 public static Element SAMPLER(RenderScript rs) {
420 if(rs.mElement_SAMPLER == null) {
421 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700422 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700423 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700424 }
425
Jason Sams8cb39de2010-06-01 15:47:01 -0700426 public static Element SCRIPT(RenderScript rs) {
427 if(rs.mElement_SCRIPT == null) {
428 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700429 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700430 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700431 }
432
Jason Sams8cb39de2010-06-01 15:47:01 -0700433 public static Element MESH(RenderScript rs) {
434 if(rs.mElement_MESH == null) {
435 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700436 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700437 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700438 }
439
Jason Sams8cb39de2010-06-01 15:47:01 -0700440 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
441 if(rs.mElement_PROGRAM_FRAGMENT == null) {
442 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700443 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700444 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700445 }
446
Jason Sams8cb39de2010-06-01 15:47:01 -0700447 public static Element PROGRAM_VERTEX(RenderScript rs) {
448 if(rs.mElement_PROGRAM_VERTEX == null) {
449 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700450 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700451 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700452 }
453
Jason Sams8cb39de2010-06-01 15:47:01 -0700454 public static Element PROGRAM_RASTER(RenderScript rs) {
455 if(rs.mElement_PROGRAM_RASTER == null) {
456 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700457 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700458 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700459 }
460
Jason Sams8cb39de2010-06-01 15:47:01 -0700461 public static Element PROGRAM_STORE(RenderScript rs) {
462 if(rs.mElement_PROGRAM_STORE == null) {
463 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700464 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700465 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700466 }
467
Stephen Hines3a291412012-04-11 17:27:29 -0700468 public static Element FONT(RenderScript rs) {
469 if(rs.mElement_FONT == null) {
470 rs.mElement_FONT = createUser(rs, DataType.RS_FONT);
471 }
472 return rs.mElement_FONT;
473 }
474
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700475
Jason Sams718cd1f2009-12-23 14:35:29 -0800476 public static Element A_8(RenderScript rs) {
477 if(rs.mElement_A_8 == null) {
478 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
479 }
480 return rs.mElement_A_8;
481 }
482
483 public static Element RGB_565(RenderScript rs) {
484 if(rs.mElement_RGB_565 == null) {
485 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
486 }
487 return rs.mElement_RGB_565;
488 }
489
490 public static Element RGB_888(RenderScript rs) {
491 if(rs.mElement_RGB_888 == null) {
492 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
493 }
494 return rs.mElement_RGB_888;
495 }
496
497 public static Element RGBA_5551(RenderScript rs) {
498 if(rs.mElement_RGBA_5551 == null) {
499 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
500 }
501 return rs.mElement_RGBA_5551;
502 }
503
504 public static Element RGBA_4444(RenderScript rs) {
505 if(rs.mElement_RGBA_4444 == null) {
506 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
507 }
508 return rs.mElement_RGBA_4444;
509 }
510
511 public static Element RGBA_8888(RenderScript rs) {
512 if(rs.mElement_RGBA_8888 == null) {
513 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
514 }
515 return rs.mElement_RGBA_8888;
516 }
517
Jason Sams8cb39de2010-06-01 15:47:01 -0700518 public static Element F32_2(RenderScript rs) {
519 if(rs.mElement_FLOAT_2 == null) {
520 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800521 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700522 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800523 }
524
Jason Sams8cb39de2010-06-01 15:47:01 -0700525 public static Element F32_3(RenderScript rs) {
526 if(rs.mElement_FLOAT_3 == null) {
527 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800528 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700529 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800530 }
531
Jason Sams8cb39de2010-06-01 15:47:01 -0700532 public static Element F32_4(RenderScript rs) {
533 if(rs.mElement_FLOAT_4 == null) {
534 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800535 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700536 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800537 }
538
Stephen Hines836c4a52011-06-01 14:38:10 -0700539 public static Element F64_2(RenderScript rs) {
540 if(rs.mElement_DOUBLE_2 == null) {
541 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
542 }
543 return rs.mElement_DOUBLE_2;
544 }
545
546 public static Element F64_3(RenderScript rs) {
547 if(rs.mElement_DOUBLE_3 == null) {
548 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
549 }
550 return rs.mElement_DOUBLE_3;
551 }
552
553 public static Element F64_4(RenderScript rs) {
554 if(rs.mElement_DOUBLE_4 == null) {
555 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
556 }
557 return rs.mElement_DOUBLE_4;
558 }
559
560 public static Element U8_2(RenderScript rs) {
561 if(rs.mElement_UCHAR_2 == null) {
562 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
563 }
564 return rs.mElement_UCHAR_2;
565 }
566
567 public static Element U8_3(RenderScript rs) {
568 if(rs.mElement_UCHAR_3 == null) {
569 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
570 }
571 return rs.mElement_UCHAR_3;
572 }
573
Jason Sams8cb39de2010-06-01 15:47:01 -0700574 public static Element U8_4(RenderScript rs) {
575 if(rs.mElement_UCHAR_4 == null) {
576 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800577 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700578 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800579 }
580
Stephen Hines836c4a52011-06-01 14:38:10 -0700581 public static Element I8_2(RenderScript rs) {
582 if(rs.mElement_CHAR_2 == null) {
583 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
584 }
585 return rs.mElement_CHAR_2;
586 }
587
588 public static Element I8_3(RenderScript rs) {
589 if(rs.mElement_CHAR_3 == null) {
590 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
591 }
592 return rs.mElement_CHAR_3;
593 }
594
595 public static Element I8_4(RenderScript rs) {
596 if(rs.mElement_CHAR_4 == null) {
597 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
598 }
599 return rs.mElement_CHAR_4;
600 }
601
602 public static Element U16_2(RenderScript rs) {
603 if(rs.mElement_USHORT_2 == null) {
604 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
605 }
606 return rs.mElement_USHORT_2;
607 }
608
609 public static Element U16_3(RenderScript rs) {
610 if(rs.mElement_USHORT_3 == null) {
611 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
612 }
613 return rs.mElement_USHORT_3;
614 }
615
616 public static Element U16_4(RenderScript rs) {
617 if(rs.mElement_USHORT_4 == null) {
618 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
619 }
620 return rs.mElement_USHORT_4;
621 }
622
623 public static Element I16_2(RenderScript rs) {
624 if(rs.mElement_SHORT_2 == null) {
625 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
626 }
627 return rs.mElement_SHORT_2;
628 }
629
630 public static Element I16_3(RenderScript rs) {
631 if(rs.mElement_SHORT_3 == null) {
632 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
633 }
634 return rs.mElement_SHORT_3;
635 }
636
637 public static Element I16_4(RenderScript rs) {
638 if(rs.mElement_SHORT_4 == null) {
639 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
640 }
641 return rs.mElement_SHORT_4;
642 }
643
644 public static Element U32_2(RenderScript rs) {
645 if(rs.mElement_UINT_2 == null) {
646 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
647 }
648 return rs.mElement_UINT_2;
649 }
650
651 public static Element U32_3(RenderScript rs) {
652 if(rs.mElement_UINT_3 == null) {
653 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
654 }
655 return rs.mElement_UINT_3;
656 }
657
658 public static Element U32_4(RenderScript rs) {
659 if(rs.mElement_UINT_4 == null) {
660 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
661 }
662 return rs.mElement_UINT_4;
663 }
664
665 public static Element I32_2(RenderScript rs) {
666 if(rs.mElement_INT_2 == null) {
667 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
668 }
669 return rs.mElement_INT_2;
670 }
671
672 public static Element I32_3(RenderScript rs) {
673 if(rs.mElement_INT_3 == null) {
674 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
675 }
676 return rs.mElement_INT_3;
677 }
678
679 public static Element I32_4(RenderScript rs) {
680 if(rs.mElement_INT_4 == null) {
681 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
682 }
683 return rs.mElement_INT_4;
684 }
685
686 public static Element U64_2(RenderScript rs) {
687 if(rs.mElement_ULONG_2 == null) {
688 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
689 }
690 return rs.mElement_ULONG_2;
691 }
692
693 public static Element U64_3(RenderScript rs) {
694 if(rs.mElement_ULONG_3 == null) {
695 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
696 }
697 return rs.mElement_ULONG_3;
698 }
699
700 public static Element U64_4(RenderScript rs) {
701 if(rs.mElement_ULONG_4 == null) {
702 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
703 }
704 return rs.mElement_ULONG_4;
705 }
706
707 public static Element I64_2(RenderScript rs) {
708 if(rs.mElement_LONG_2 == null) {
709 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
710 }
711 return rs.mElement_LONG_2;
712 }
713
714 public static Element I64_3(RenderScript rs) {
715 if(rs.mElement_LONG_3 == null) {
716 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
717 }
718 return rs.mElement_LONG_3;
719 }
720
721 public static Element I64_4(RenderScript rs) {
722 if(rs.mElement_LONG_4 == null) {
723 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
724 }
725 return rs.mElement_LONG_4;
726 }
727
Tim Murray932e78e2013-09-03 11:42:26 -0700728 public static Element YUV(RenderScript rs) {
729 if (rs.mElement_YUV == null) {
730 rs.mElement_YUV = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_YUV);
731 }
732 return rs.mElement_YUV;
733 }
734
Jason Sams1d45c472010-08-25 14:31:48 -0700735 public static Element MATRIX_4X4(RenderScript rs) {
736 if(rs.mElement_MATRIX_4X4 == null) {
737 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
738 }
739 return rs.mElement_MATRIX_4X4;
740 }
Jason Sams65c80f82012-05-08 17:30:26 -0700741
742 /** @deprecated use MATRIX_4X4
743 */
Jason Sams1d45c472010-08-25 14:31:48 -0700744 public static Element MATRIX4X4(RenderScript rs) {
745 return MATRIX_4X4(rs);
746 }
747
748 public static Element MATRIX_3X3(RenderScript rs) {
749 if(rs.mElement_MATRIX_3X3 == null) {
750 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
751 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800752 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700753 }
754
755 public static Element MATRIX_2X2(RenderScript rs) {
756 if(rs.mElement_MATRIX_2X2 == null) {
757 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
758 }
759 return rs.mElement_MATRIX_2X2;
760 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800761
Tim Murray7a629fa2013-11-19 12:45:54 -0800762 Element(long id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700763 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700764 mSize = 0;
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -0800765 mVectorSize = 1;
Jason Sams718cd1f2009-12-23 14:35:29 -0800766 mElements = e;
767 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700768 mArraySizes = as;
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800769 mType = DataType.NONE;
770 mKind = DataKind.USER;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700771 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800772 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700773 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700774 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800775 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800776 updateVisibleSubElements();
Jason Sams718cd1f2009-12-23 14:35:29 -0800777 }
778
Tim Murray7a629fa2013-11-19 12:45:54 -0800779 Element(long id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700780 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800781 if ((dt != DataType.UNSIGNED_5_6_5) &&
782 (dt != DataType.UNSIGNED_4_4_4_4) &&
783 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800784 if (size == 3) {
785 mSize = dt.mSize * 4;
786 } else {
787 mSize = dt.mSize * size;
788 }
Jason Sams252c0782011-01-11 17:42:52 -0800789 } else {
790 mSize = dt.mSize;
791 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800792 mType = dt;
793 mKind = dk;
794 mNormalized = norm;
795 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700796 }
797
Tim Murray7a629fa2013-11-19 12:45:54 -0800798 Element(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700799 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700800 }
801
802 @Override
803 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800804 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700805
806 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
807 int[] dataBuffer = new int[5];
Jason Samse07694b2012-04-03 15:36:36 -0700808 mRS.nElementGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700809
810 mNormalized = dataBuffer[2] == 1 ? true : false;
811 mVectorSize = dataBuffer[3];
812 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700813 for (DataType dt: DataType.values()) {
814 if(dt.mID == dataBuffer[0]){
815 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700816 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700817 }
818 }
819 for (DataKind dk: DataKind.values()) {
820 if(dk.mID == dataBuffer[1]){
821 mKind = dk;
822 }
823 }
824
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700825 int numSubElements = dataBuffer[4];
826 if(numSubElements > 0) {
827 mElements = new Element[numSubElements];
828 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700829 mArraySizes = new int[numSubElements];
830 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700831
Ashok Bhat98071552014-02-12 09:54:43 +0000832 long[] subElementIds = new long[numSubElements];
Jason Samse07694b2012-04-03 15:36:36 -0700833 mRS.nElementGetSubElements(getID(mRS), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700834 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700835 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700836 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700837 mOffsetInBytes[i] = mSize;
838 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700839 }
840 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800841 updateVisibleSubElements();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700842 }
843
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700844 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800845 * Create a custom Element of the specified DataType. The DataKind will be
846 * set to USER and the vector size to 1 indicating non-vector.
847 *
848 * @param rs The context associated with the new Element.
849 * @param dt The DataType for the new element.
850 * @return Element
851 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800852 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700853 DataKind dk = DataKind.USER;
854 boolean norm = false;
855 int vecSize = 1;
Tim Murray7a629fa2013-11-19 12:45:54 -0800856 long id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700857 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800858 }
859
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700860 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800861 * Create a custom vector element of the specified DataType and vector size.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800862 * DataKind will be set to USER. Only primitive types (FLOAT_32, FLOAT_64,
863 * SIGNED_8, SIGNED_16, SIGNED_32, SIGNED_64, UNSIGNED_8, UNSIGNED_16,
864 * UNSIGNED_32, UNSIGNED_64, BOOLEAN) are supported.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800865 *
866 * @param rs The context associated with the new Element.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800867 * @param dt The DataType for the new Element.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800868 * @param size Vector size for the new Element. Range 2-4 inclusive
869 * supported.
870 *
871 * @return Element
872 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800873 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800874 if (size < 2 || size > 4) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800875 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700876 }
Stephen Hines3beb60e2012-02-14 20:38:20 -0800877
878 switch (dt) {
879 // Support only primitive integer/float/boolean types as vectors.
880 case FLOAT_32:
881 case FLOAT_64:
882 case SIGNED_8:
883 case SIGNED_16:
884 case SIGNED_32:
885 case SIGNED_64:
886 case UNSIGNED_8:
887 case UNSIGNED_16:
888 case UNSIGNED_32:
889 case UNSIGNED_64:
890 case BOOLEAN: {
891 DataKind dk = DataKind.USER;
892 boolean norm = false;
Tim Murray7a629fa2013-11-19 12:45:54 -0800893 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Stephen Hines3beb60e2012-02-14 20:38:20 -0800894 return new Element(id, rs, dt, dk, norm, size);
895 }
896
897 default: {
898 throw new RSIllegalArgumentException("Cannot create vector of " +
899 "non-primitive type.");
900 }
901 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700902 }
903
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700904 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800905 * Create a new pixel Element type. A matching DataType and DataKind must
906 * be provided. The DataType and DataKind must contain the same number of
907 * components. Vector size will be set to 1.
908 *
909 * @param rs The context associated with the new Element.
910 * @param dt The DataType for the new element.
911 * @param dk The DataKind to specify the mapping of each component in the
912 * DataType.
913 *
914 * @return Element
915 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800916 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800917 if (!(dk == DataKind.PIXEL_L ||
918 dk == DataKind.PIXEL_A ||
919 dk == DataKind.PIXEL_LA ||
920 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700921 dk == DataKind.PIXEL_RGBA ||
Jason Samsdd6c8b32013-02-15 17:27:24 -0800922 dk == DataKind.PIXEL_DEPTH ||
923 dk == DataKind.PIXEL_YUV)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700924 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800925 }
926 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700927 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800928 dt == DataType.UNSIGNED_5_6_5 ||
929 dt == DataType.UNSIGNED_4_4_4_4 ||
930 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700931 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800932 }
933 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700934 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800935 }
936 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700937 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800938 }
939 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700940 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800941 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700942 if (dt == DataType.UNSIGNED_16 &&
943 dk != DataKind.PIXEL_DEPTH) {
944 throw new RSIllegalArgumentException("Bad kind and type combo");
945 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800946
947 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700948 switch (dk) {
949 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800950 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700951 break;
952 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800953 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700954 break;
955 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800956 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700957 break;
958 case PIXEL_DEPTH:
959 size = 2;
960 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800961 }
962
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700963 boolean norm = true;
Tim Murray7a629fa2013-11-19 12:45:54 -0800964 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700965 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800966 }
Jason Sams36e612a2009-07-31 16:26:13 -0700967
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700968 /**
Stephen Hinesf257e512011-06-14 14:54:29 -0700969 * Check if the current Element is compatible with another Element.
970 * Primitive Elements are compatible if they share the same underlying
971 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
972 * must be equal in order to be compatible. This requires strict name
973 * equivalence for all sub-Elements (in addition to structural equivalence).
974 *
975 * @param e The Element to check compatibility with.
976 *
977 * @return boolean true if the Elements are compatible, otherwise false.
978 */
979 public boolean isCompatible(Element e) {
980 // Try strict BaseObj equality to start with.
981 if (this.equals(e)) {
982 return true;
983 }
984
985 // Ignore mKind because it is allowed to be different (user vs. pixel).
986 // We also ignore mNormalized because it can be different. The mType
Stephen Hines20948112012-02-14 19:42:42 -0800987 // field must not be NONE since we require name equivalence for
988 // all user-created Elements.
Stephen Hinesf257e512011-06-14 14:54:29 -0700989 return ((mSize == e.mSize) &&
Stephen Hines20948112012-02-14 19:42:42 -0800990 (mType != DataType.NONE) &&
Stephen Hinesf257e512011-06-14 14:54:29 -0700991 (mType == e.mType) &&
992 (mVectorSize == e.mVectorSize));
993 }
994
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700995 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800996 * Builder class for producing complex elements with matching field and name
997 * pairs. The builder starts empty. The order in which elements are added
998 * is retained for the layout in memory.
999 *
1000 */
Jason Sams36e612a2009-07-31 16:26:13 -07001001 public static class Builder {
1002 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -08001003 Element[] mElements;
1004 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -07001005 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -08001006 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001007 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -07001008
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001009 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001010 * Create a builder object.
1011 *
1012 * @param rs
1013 */
Jason Sams22534172009-08-04 16:58:20 -07001014 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -07001015 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -08001016 mCount = 0;
1017 mElements = new Element[8];
1018 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -07001019 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -07001020 }
1021
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001022 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001023 * Add an array of elements to this element.
1024 *
1025 * @param element
1026 * @param name
1027 * @param arraySize
1028 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001029 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -07001030 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -07001031 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -07001032 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001033
1034 // Skip padding fields after a vector 3 type.
1035 if (mSkipPadding != 0) {
1036 if (name.startsWith("#padding_")) {
1037 mSkipPadding = 0;
1038 return this;
1039 }
1040 }
1041
1042 if (element.mVectorSize == 3) {
1043 mSkipPadding = 1;
1044 } else {
1045 mSkipPadding = 0;
1046 }
1047
Jason Sams718cd1f2009-12-23 14:35:29 -08001048 if(mCount == mElements.length) {
1049 Element[] e = new Element[mCount + 8];
1050 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -07001051 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -08001052 System.arraycopy(mElements, 0, e, 0, mCount);
1053 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001054 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -08001055 mElements = e;
1056 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -07001057 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -07001058 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001059 mElements[mCount] = element;
1060 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -07001061 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -08001062 mCount++;
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001063 return this;
Jason Sams07ae4062009-08-27 20:23:34 -07001064 }
1065
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001066 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001067 * Add a single element to this Element.
1068 *
1069 * @param element
1070 * @param name
1071 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001072 public Builder add(Element element, String name) {
1073 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -07001074 }
1075
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001076 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001077 * Create the element from this builder.
1078 *
1079 *
1080 * @return Element
1081 */
Jason Sams22534172009-08-04 16:58:20 -07001082 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -08001083 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -08001084 Element[] ein = new Element[mCount];
1085 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -07001086 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -08001087 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1088 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001089 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001090
Ashok Bhat98071552014-02-12 09:54:43 +00001091 long[] ids = new long[ein.length];
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001092 for (int ct = 0; ct < ein.length; ct++ ) {
Ashok Bhat98071552014-02-12 09:54:43 +00001093 ids[ct] = ein[ct].getID(mRS);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001094 }
Tim Murray7a629fa2013-11-19 12:45:54 -08001095 long id = mRS.nElementCreate2(ids, sin, asin);
Jason Sams70d4e502010-09-02 17:35:23 -07001096 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -07001097 }
1098 }
Jason Sams36e612a2009-07-31 16:26:13 -07001099}
1100