blob: c6b5b0d99ea2737daa49f053f3ed3021aa886d7c [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 *
117 * RS_* objects. 32 bit opaque handles.
118 */
Jason Sams36e612a2009-07-31 16:26:13 -0700119 public enum DataType {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800120 NONE (0, 0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800121 //FLOAT_16 (1, 2),
122 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -0700123 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800124 SIGNED_8 (4, 1),
125 SIGNED_16 (5, 2),
126 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -0700127 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800128 UNSIGNED_8 (8, 1),
129 UNSIGNED_16 (9, 2),
130 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -0700131 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800132
Jason Samsf110d4b2010-06-21 17:42:41 -0700133 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -0800134
Jason Samsf110d4b2010-06-21 17:42:41 -0700135 UNSIGNED_5_6_5 (13, 2),
136 UNSIGNED_5_5_5_1 (14, 2),
137 UNSIGNED_4_4_4_4 (15, 2),
138
Jason Sams1d45c472010-08-25 14:31:48 -0700139 MATRIX_4X4 (16, 64),
140 MATRIX_3X3 (17, 36),
141 MATRIX_2X2 (18, 16),
142
Jason Samsb49dfea2014-06-18 13:17:57 -0700143 RS_ELEMENT (1000),
144 RS_TYPE (1001),
145 RS_ALLOCATION (1002),
146 RS_SAMPLER (1003),
147 RS_SCRIPT (1004),
148 RS_MESH (1005),
149 RS_PROGRAM_FRAGMENT (1006),
150 RS_PROGRAM_VERTEX (1007),
151 RS_PROGRAM_RASTER (1008),
152 RS_PROGRAM_STORE (1009),
153 RS_FONT (1010);
Jason Sams36e612a2009-07-31 16:26:13 -0700154
155 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800156 int mSize;
157 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700158 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800159 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700160 }
Jason Samsb49dfea2014-06-18 13:17:57 -0700161
162 DataType(int id) {
163 mID = id;
164 mSize = 4;
165 if (RenderScript.sPointerSize == 8) {
166 mSize = 32;
167 }
168 }
Jason Sams36e612a2009-07-31 16:26:13 -0700169 }
170
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700171 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800172 * The special interpretation of the data if required. This is primarly
173 * useful for graphical data. USER indicates no special interpretation is
174 * expected. PIXEL is used in conjunction with the standard data types for
175 * representing texture formats.
176 */
Jason Sams36e612a2009-07-31 16:26:13 -0700177 public enum DataKind {
178 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800179
180 PIXEL_L (7),
181 PIXEL_A (8),
182 PIXEL_LA (9),
183 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700184 PIXEL_RGBA (11),
Jason Sams8140d7b2012-12-13 17:01:09 -0800185 PIXEL_DEPTH (12),
186 PIXEL_YUV(13);
Jason Sams36e612a2009-07-31 16:26:13 -0700187
188 int mID;
189 DataKind(int id) {
190 mID = id;
191 }
192 }
193
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700194 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800195 * Return if a element is too complex for use as a data source for a Mesh or
196 * a Program.
197 *
198 * @return boolean
199 */
Jason Samsc1d62102010-11-04 14:32:19 -0700200 public boolean isComplex() {
201 if (mElements == null) {
202 return false;
203 }
204 for (int ct=0; ct < mElements.length; ct++) {
205 if (mElements[ct].mElements != null) {
206 return true;
207 }
208 }
209 return false;
210 }
211
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700212 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700213 * Elements could be simple, such as an int or a float, or a
214 * structure with multiple sub elements, such as a collection of
215 * floats, float2, float4. This function returns zero for simple
216 * elements or the number of sub-elements otherwise.
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700217 * @return number of sub-elements in this element
218 */
219 public int getSubElementCount() {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800220 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700221 return 0;
222 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800223 return mVisibleElementMap.length;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700224 }
225
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700226 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700227 * For complex elements, this function will return the
228 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700229 * @param index index of the sub-element to return
230 * @return sub-element in this element at given index
231 */
232 public Element getSubElement(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800233 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700234 throw new RSIllegalArgumentException("Element contains no sub-elements");
235 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800236 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700237 throw new RSIllegalArgumentException("Illegal sub-element index");
238 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800239 return mElements[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700240 }
241
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700242 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700243 * For complex elements, this function will return the
244 * sub-element name at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700245 * @param index index of the sub-element
246 * @return sub-element in this element at given index
247 */
248 public String getSubElementName(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800249 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700250 throw new RSIllegalArgumentException("Element contains no sub-elements");
251 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800252 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700253 throw new RSIllegalArgumentException("Illegal sub-element index");
254 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800255 return mElementNames[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700256 }
257
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700258 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700259 * For complex elements, some sub-elements could be statically
260 * sized arrays. This function will return the array size for
261 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700262 * @param index index of the sub-element
263 * @return array size of sub-element in this element at given index
264 */
265 public int getSubElementArraySize(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800266 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700267 throw new RSIllegalArgumentException("Element contains no sub-elements");
268 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800269 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700270 throw new RSIllegalArgumentException("Illegal sub-element index");
271 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800272 return mArraySizes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700273 }
274
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700275 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700276 * This function specifies the location of a sub-element within
277 * the element
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700278 * @param index index of the sub-element
279 * @return offset in bytes of sub-element in this element at given index
280 */
281 public int getSubElementOffsetBytes(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800282 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700283 throw new RSIllegalArgumentException("Element contains no sub-elements");
284 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800285 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700286 throw new RSIllegalArgumentException("Illegal sub-element index");
287 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800288 return mOffsetInBytes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700289 }
290
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700291 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800292 * @return element data type
293 */
294 public DataType getDataType() {
295 return mType;
296 }
297
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700298 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800299 * @return element data kind
300 */
301 public DataKind getDataKind() {
302 return mKind;
303 }
304
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700305 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800306 * Utility function for returning an Element containing a single Boolean.
307 *
308 * @param rs Context to which the element will belong.
309 *
310 * @return Element
311 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700312 public static Element BOOLEAN(RenderScript rs) {
313 if(rs.mElement_BOOLEAN == null) {
314 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
315 }
316 return rs.mElement_BOOLEAN;
317 }
318
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700319 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800320 * Utility function for returning an Element containing a single UNSIGNED_8.
321 *
322 * @param rs Context to which the element will belong.
323 *
324 * @return Element
325 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700326 public static Element U8(RenderScript rs) {
327 if(rs.mElement_U8 == null) {
328 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800329 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700330 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800331 }
332
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700333 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800334 * Utility function for returning an Element containing a single SIGNED_8.
335 *
336 * @param rs Context to which the element will belong.
337 *
338 * @return Element
339 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700340 public static Element I8(RenderScript rs) {
341 if(rs.mElement_I8 == null) {
342 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800343 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700344 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800345 }
346
Jason Samse29f3e72010-06-08 15:40:48 -0700347 public static Element U16(RenderScript rs) {
348 if(rs.mElement_U16 == null) {
349 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
350 }
351 return rs.mElement_U16;
352 }
353
354 public static Element I16(RenderScript rs) {
355 if(rs.mElement_I16 == null) {
356 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
357 }
358 return rs.mElement_I16;
359 }
360
Jason Sams8cb39de2010-06-01 15:47:01 -0700361 public static Element U32(RenderScript rs) {
362 if(rs.mElement_U32 == null) {
363 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800364 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700365 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800366 }
367
Jason Sams8cb39de2010-06-01 15:47:01 -0700368 public static Element I32(RenderScript rs) {
369 if(rs.mElement_I32 == null) {
370 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800371 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700372 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800373 }
374
Stephen Hines52d83632010-10-11 16:10:42 -0700375 public static Element U64(RenderScript rs) {
376 if(rs.mElement_U64 == null) {
377 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
378 }
379 return rs.mElement_U64;
380 }
381
Stephen Hinesef1dac22010-10-01 15:39:33 -0700382 public static Element I64(RenderScript rs) {
383 if(rs.mElement_I64 == null) {
384 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
385 }
386 return rs.mElement_I64;
387 }
388
Jason Sams8cb39de2010-06-01 15:47:01 -0700389 public static Element F32(RenderScript rs) {
390 if(rs.mElement_F32 == null) {
391 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800392 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700393 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800394 }
395
Stephen Hines02f417052010-09-30 15:19:22 -0700396 public static Element F64(RenderScript rs) {
397 if(rs.mElement_F64 == null) {
398 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
399 }
400 return rs.mElement_F64;
401 }
402
Jason Sams8cb39de2010-06-01 15:47:01 -0700403 public static Element ELEMENT(RenderScript rs) {
404 if(rs.mElement_ELEMENT == null) {
405 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700406 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700407 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700408 }
409
Jason Sams8cb39de2010-06-01 15:47:01 -0700410 public static Element TYPE(RenderScript rs) {
411 if(rs.mElement_TYPE == null) {
412 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700413 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700414 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700415 }
416
Jason Sams8cb39de2010-06-01 15:47:01 -0700417 public static Element ALLOCATION(RenderScript rs) {
418 if(rs.mElement_ALLOCATION == null) {
419 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700420 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700421 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700422 }
423
Jason Sams8cb39de2010-06-01 15:47:01 -0700424 public static Element SAMPLER(RenderScript rs) {
425 if(rs.mElement_SAMPLER == null) {
426 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700427 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700428 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700429 }
430
Jason Sams8cb39de2010-06-01 15:47:01 -0700431 public static Element SCRIPT(RenderScript rs) {
432 if(rs.mElement_SCRIPT == null) {
433 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700434 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700435 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700436 }
437
Jason Sams8cb39de2010-06-01 15:47:01 -0700438 public static Element MESH(RenderScript rs) {
439 if(rs.mElement_MESH == null) {
440 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700441 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700442 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700443 }
444
Jason Sams8cb39de2010-06-01 15:47:01 -0700445 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
446 if(rs.mElement_PROGRAM_FRAGMENT == null) {
447 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700448 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700449 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700450 }
451
Jason Sams8cb39de2010-06-01 15:47:01 -0700452 public static Element PROGRAM_VERTEX(RenderScript rs) {
453 if(rs.mElement_PROGRAM_VERTEX == null) {
454 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700455 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700456 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700457 }
458
Jason Sams8cb39de2010-06-01 15:47:01 -0700459 public static Element PROGRAM_RASTER(RenderScript rs) {
460 if(rs.mElement_PROGRAM_RASTER == null) {
461 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700462 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700463 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700464 }
465
Jason Sams8cb39de2010-06-01 15:47:01 -0700466 public static Element PROGRAM_STORE(RenderScript rs) {
467 if(rs.mElement_PROGRAM_STORE == null) {
468 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700469 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700470 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700471 }
472
Stephen Hines3a291412012-04-11 17:27:29 -0700473 public static Element FONT(RenderScript rs) {
474 if(rs.mElement_FONT == null) {
475 rs.mElement_FONT = createUser(rs, DataType.RS_FONT);
476 }
477 return rs.mElement_FONT;
478 }
479
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700480
Jason Sams718cd1f2009-12-23 14:35:29 -0800481 public static Element A_8(RenderScript rs) {
482 if(rs.mElement_A_8 == null) {
483 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
484 }
485 return rs.mElement_A_8;
486 }
487
488 public static Element RGB_565(RenderScript rs) {
489 if(rs.mElement_RGB_565 == null) {
490 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
491 }
492 return rs.mElement_RGB_565;
493 }
494
495 public static Element RGB_888(RenderScript rs) {
496 if(rs.mElement_RGB_888 == null) {
497 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
498 }
499 return rs.mElement_RGB_888;
500 }
501
502 public static Element RGBA_5551(RenderScript rs) {
503 if(rs.mElement_RGBA_5551 == null) {
504 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
505 }
506 return rs.mElement_RGBA_5551;
507 }
508
509 public static Element RGBA_4444(RenderScript rs) {
510 if(rs.mElement_RGBA_4444 == null) {
511 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
512 }
513 return rs.mElement_RGBA_4444;
514 }
515
516 public static Element RGBA_8888(RenderScript rs) {
517 if(rs.mElement_RGBA_8888 == null) {
518 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
519 }
520 return rs.mElement_RGBA_8888;
521 }
522
Jason Sams8cb39de2010-06-01 15:47:01 -0700523 public static Element F32_2(RenderScript rs) {
524 if(rs.mElement_FLOAT_2 == null) {
525 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800526 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700527 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800528 }
529
Jason Sams8cb39de2010-06-01 15:47:01 -0700530 public static Element F32_3(RenderScript rs) {
531 if(rs.mElement_FLOAT_3 == null) {
532 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800533 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700534 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800535 }
536
Jason Sams8cb39de2010-06-01 15:47:01 -0700537 public static Element F32_4(RenderScript rs) {
538 if(rs.mElement_FLOAT_4 == null) {
539 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800540 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700541 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800542 }
543
Stephen Hines836c4a52011-06-01 14:38:10 -0700544 public static Element F64_2(RenderScript rs) {
545 if(rs.mElement_DOUBLE_2 == null) {
546 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
547 }
548 return rs.mElement_DOUBLE_2;
549 }
550
551 public static Element F64_3(RenderScript rs) {
552 if(rs.mElement_DOUBLE_3 == null) {
553 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
554 }
555 return rs.mElement_DOUBLE_3;
556 }
557
558 public static Element F64_4(RenderScript rs) {
559 if(rs.mElement_DOUBLE_4 == null) {
560 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
561 }
562 return rs.mElement_DOUBLE_4;
563 }
564
565 public static Element U8_2(RenderScript rs) {
566 if(rs.mElement_UCHAR_2 == null) {
567 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
568 }
569 return rs.mElement_UCHAR_2;
570 }
571
572 public static Element U8_3(RenderScript rs) {
573 if(rs.mElement_UCHAR_3 == null) {
574 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
575 }
576 return rs.mElement_UCHAR_3;
577 }
578
Jason Sams8cb39de2010-06-01 15:47:01 -0700579 public static Element U8_4(RenderScript rs) {
580 if(rs.mElement_UCHAR_4 == null) {
581 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800582 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700583 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800584 }
585
Stephen Hines836c4a52011-06-01 14:38:10 -0700586 public static Element I8_2(RenderScript rs) {
587 if(rs.mElement_CHAR_2 == null) {
588 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
589 }
590 return rs.mElement_CHAR_2;
591 }
592
593 public static Element I8_3(RenderScript rs) {
594 if(rs.mElement_CHAR_3 == null) {
595 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
596 }
597 return rs.mElement_CHAR_3;
598 }
599
600 public static Element I8_4(RenderScript rs) {
601 if(rs.mElement_CHAR_4 == null) {
602 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
603 }
604 return rs.mElement_CHAR_4;
605 }
606
607 public static Element U16_2(RenderScript rs) {
608 if(rs.mElement_USHORT_2 == null) {
609 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
610 }
611 return rs.mElement_USHORT_2;
612 }
613
614 public static Element U16_3(RenderScript rs) {
615 if(rs.mElement_USHORT_3 == null) {
616 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
617 }
618 return rs.mElement_USHORT_3;
619 }
620
621 public static Element U16_4(RenderScript rs) {
622 if(rs.mElement_USHORT_4 == null) {
623 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
624 }
625 return rs.mElement_USHORT_4;
626 }
627
628 public static Element I16_2(RenderScript rs) {
629 if(rs.mElement_SHORT_2 == null) {
630 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
631 }
632 return rs.mElement_SHORT_2;
633 }
634
635 public static Element I16_3(RenderScript rs) {
636 if(rs.mElement_SHORT_3 == null) {
637 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
638 }
639 return rs.mElement_SHORT_3;
640 }
641
642 public static Element I16_4(RenderScript rs) {
643 if(rs.mElement_SHORT_4 == null) {
644 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
645 }
646 return rs.mElement_SHORT_4;
647 }
648
649 public static Element U32_2(RenderScript rs) {
650 if(rs.mElement_UINT_2 == null) {
651 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
652 }
653 return rs.mElement_UINT_2;
654 }
655
656 public static Element U32_3(RenderScript rs) {
657 if(rs.mElement_UINT_3 == null) {
658 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
659 }
660 return rs.mElement_UINT_3;
661 }
662
663 public static Element U32_4(RenderScript rs) {
664 if(rs.mElement_UINT_4 == null) {
665 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
666 }
667 return rs.mElement_UINT_4;
668 }
669
670 public static Element I32_2(RenderScript rs) {
671 if(rs.mElement_INT_2 == null) {
672 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
673 }
674 return rs.mElement_INT_2;
675 }
676
677 public static Element I32_3(RenderScript rs) {
678 if(rs.mElement_INT_3 == null) {
679 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
680 }
681 return rs.mElement_INT_3;
682 }
683
684 public static Element I32_4(RenderScript rs) {
685 if(rs.mElement_INT_4 == null) {
686 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
687 }
688 return rs.mElement_INT_4;
689 }
690
691 public static Element U64_2(RenderScript rs) {
692 if(rs.mElement_ULONG_2 == null) {
693 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
694 }
695 return rs.mElement_ULONG_2;
696 }
697
698 public static Element U64_3(RenderScript rs) {
699 if(rs.mElement_ULONG_3 == null) {
700 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
701 }
702 return rs.mElement_ULONG_3;
703 }
704
705 public static Element U64_4(RenderScript rs) {
706 if(rs.mElement_ULONG_4 == null) {
707 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
708 }
709 return rs.mElement_ULONG_4;
710 }
711
712 public static Element I64_2(RenderScript rs) {
713 if(rs.mElement_LONG_2 == null) {
714 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
715 }
716 return rs.mElement_LONG_2;
717 }
718
719 public static Element I64_3(RenderScript rs) {
720 if(rs.mElement_LONG_3 == null) {
721 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
722 }
723 return rs.mElement_LONG_3;
724 }
725
726 public static Element I64_4(RenderScript rs) {
727 if(rs.mElement_LONG_4 == null) {
728 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
729 }
730 return rs.mElement_LONG_4;
731 }
732
Tim Murray932e78e2013-09-03 11:42:26 -0700733 public static Element YUV(RenderScript rs) {
734 if (rs.mElement_YUV == null) {
735 rs.mElement_YUV = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_YUV);
736 }
737 return rs.mElement_YUV;
738 }
739
Jason Sams1d45c472010-08-25 14:31:48 -0700740 public static Element MATRIX_4X4(RenderScript rs) {
741 if(rs.mElement_MATRIX_4X4 == null) {
742 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
743 }
744 return rs.mElement_MATRIX_4X4;
745 }
Jason Sams65c80f82012-05-08 17:30:26 -0700746
747 /** @deprecated use MATRIX_4X4
748 */
Jason Sams1d45c472010-08-25 14:31:48 -0700749 public static Element MATRIX4X4(RenderScript rs) {
750 return MATRIX_4X4(rs);
751 }
752
753 public static Element MATRIX_3X3(RenderScript rs) {
754 if(rs.mElement_MATRIX_3X3 == null) {
755 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
756 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800757 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700758 }
759
760 public static Element MATRIX_2X2(RenderScript rs) {
761 if(rs.mElement_MATRIX_2X2 == null) {
762 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
763 }
764 return rs.mElement_MATRIX_2X2;
765 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800766
Tim Murray460a0492013-11-19 12:45:54 -0800767 Element(long id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700768 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700769 mSize = 0;
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -0800770 mVectorSize = 1;
Jason Sams718cd1f2009-12-23 14:35:29 -0800771 mElements = e;
772 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700773 mArraySizes = as;
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800774 mType = DataType.NONE;
775 mKind = DataKind.USER;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700776 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800777 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700778 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700779 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800780 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800781 updateVisibleSubElements();
Jason Sams718cd1f2009-12-23 14:35:29 -0800782 }
783
Tim Murray460a0492013-11-19 12:45:54 -0800784 Element(long id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700785 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800786 if ((dt != DataType.UNSIGNED_5_6_5) &&
787 (dt != DataType.UNSIGNED_4_4_4_4) &&
788 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800789 if (size == 3) {
790 mSize = dt.mSize * 4;
791 } else {
792 mSize = dt.mSize * size;
793 }
Jason Sams252c0782011-01-11 17:42:52 -0800794 } else {
795 mSize = dt.mSize;
796 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800797 mType = dt;
798 mKind = dk;
799 mNormalized = norm;
800 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700801 }
802
Tim Murray460a0492013-11-19 12:45:54 -0800803 Element(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700804 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700805 }
806
807 @Override
808 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800809 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700810
811 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
812 int[] dataBuffer = new int[5];
Jason Samse07694b2012-04-03 15:36:36 -0700813 mRS.nElementGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700814
815 mNormalized = dataBuffer[2] == 1 ? true : false;
816 mVectorSize = dataBuffer[3];
817 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700818 for (DataType dt: DataType.values()) {
819 if(dt.mID == dataBuffer[0]){
820 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700821 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700822 }
823 }
824 for (DataKind dk: DataKind.values()) {
825 if(dk.mID == dataBuffer[1]){
826 mKind = dk;
827 }
828 }
829
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700830 int numSubElements = dataBuffer[4];
831 if(numSubElements > 0) {
832 mElements = new Element[numSubElements];
833 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700834 mArraySizes = new int[numSubElements];
835 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700836
Ashok Bhat98071552014-02-12 09:54:43 +0000837 long[] subElementIds = new long[numSubElements];
Jason Samse07694b2012-04-03 15:36:36 -0700838 mRS.nElementGetSubElements(getID(mRS), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700839 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700840 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700841 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700842 mOffsetInBytes[i] = mSize;
843 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700844 }
845 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800846 updateVisibleSubElements();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700847 }
848
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700849 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800850 * Create a custom Element of the specified DataType. The DataKind will be
851 * set to USER and the vector size to 1 indicating non-vector.
852 *
853 * @param rs The context associated with the new Element.
854 * @param dt The DataType for the new element.
855 * @return Element
856 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800857 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700858 DataKind dk = DataKind.USER;
859 boolean norm = false;
860 int vecSize = 1;
Tim Murray460a0492013-11-19 12:45:54 -0800861 long id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700862 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800863 }
864
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700865 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800866 * Create a custom vector element of the specified DataType and vector size.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800867 * DataKind will be set to USER. Only primitive types (FLOAT_32, FLOAT_64,
868 * SIGNED_8, SIGNED_16, SIGNED_32, SIGNED_64, UNSIGNED_8, UNSIGNED_16,
869 * UNSIGNED_32, UNSIGNED_64, BOOLEAN) are supported.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800870 *
871 * @param rs The context associated with the new Element.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800872 * @param dt The DataType for the new Element.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800873 * @param size Vector size for the new Element. Range 2-4 inclusive
874 * supported.
875 *
876 * @return Element
877 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800878 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800879 if (size < 2 || size > 4) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800880 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700881 }
Stephen Hines3beb60e2012-02-14 20:38:20 -0800882
883 switch (dt) {
884 // Support only primitive integer/float/boolean types as vectors.
885 case FLOAT_32:
886 case FLOAT_64:
887 case SIGNED_8:
888 case SIGNED_16:
889 case SIGNED_32:
890 case SIGNED_64:
891 case UNSIGNED_8:
892 case UNSIGNED_16:
893 case UNSIGNED_32:
894 case UNSIGNED_64:
895 case BOOLEAN: {
896 DataKind dk = DataKind.USER;
897 boolean norm = false;
Tim Murray460a0492013-11-19 12:45:54 -0800898 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Stephen Hines3beb60e2012-02-14 20:38:20 -0800899 return new Element(id, rs, dt, dk, norm, size);
900 }
901
902 default: {
903 throw new RSIllegalArgumentException("Cannot create vector of " +
904 "non-primitive type.");
905 }
906 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700907 }
908
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700909 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800910 * Create a new pixel Element type. A matching DataType and DataKind must
911 * be provided. The DataType and DataKind must contain the same number of
912 * components. Vector size will be set to 1.
913 *
914 * @param rs The context associated with the new Element.
915 * @param dt The DataType for the new element.
916 * @param dk The DataKind to specify the mapping of each component in the
917 * DataType.
918 *
919 * @return Element
920 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800921 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800922 if (!(dk == DataKind.PIXEL_L ||
923 dk == DataKind.PIXEL_A ||
924 dk == DataKind.PIXEL_LA ||
925 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700926 dk == DataKind.PIXEL_RGBA ||
Jason Samsdd6c8b32013-02-15 17:27:24 -0800927 dk == DataKind.PIXEL_DEPTH ||
928 dk == DataKind.PIXEL_YUV)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700929 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800930 }
931 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700932 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800933 dt == DataType.UNSIGNED_5_6_5 ||
934 dt == DataType.UNSIGNED_4_4_4_4 ||
935 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700936 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800937 }
938 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700939 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800940 }
941 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700942 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800943 }
944 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700945 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800946 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700947 if (dt == DataType.UNSIGNED_16 &&
948 dk != DataKind.PIXEL_DEPTH) {
949 throw new RSIllegalArgumentException("Bad kind and type combo");
950 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800951
952 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700953 switch (dk) {
954 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800955 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700956 break;
957 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800958 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700959 break;
960 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800961 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700962 break;
963 case PIXEL_DEPTH:
964 size = 2;
965 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800966 }
967
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700968 boolean norm = true;
Tim Murray460a0492013-11-19 12:45:54 -0800969 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700970 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800971 }
Jason Sams36e612a2009-07-31 16:26:13 -0700972
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700973 /**
Stephen Hinesf257e512011-06-14 14:54:29 -0700974 * Check if the current Element is compatible with another Element.
975 * Primitive Elements are compatible if they share the same underlying
976 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
977 * must be equal in order to be compatible. This requires strict name
978 * equivalence for all sub-Elements (in addition to structural equivalence).
979 *
980 * @param e The Element to check compatibility with.
981 *
982 * @return boolean true if the Elements are compatible, otherwise false.
983 */
984 public boolean isCompatible(Element e) {
985 // Try strict BaseObj equality to start with.
986 if (this.equals(e)) {
987 return true;
988 }
989
990 // Ignore mKind because it is allowed to be different (user vs. pixel).
991 // We also ignore mNormalized because it can be different. The mType
Stephen Hines20948112012-02-14 19:42:42 -0800992 // field must not be NONE since we require name equivalence for
993 // all user-created Elements.
Stephen Hinesf257e512011-06-14 14:54:29 -0700994 return ((mSize == e.mSize) &&
Stephen Hines20948112012-02-14 19:42:42 -0800995 (mType != DataType.NONE) &&
Stephen Hinesf257e512011-06-14 14:54:29 -0700996 (mType == e.mType) &&
997 (mVectorSize == e.mVectorSize));
998 }
999
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001000 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001001 * Builder class for producing complex elements with matching field and name
1002 * pairs. The builder starts empty. The order in which elements are added
1003 * is retained for the layout in memory.
1004 *
1005 */
Jason Sams36e612a2009-07-31 16:26:13 -07001006 public static class Builder {
1007 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -08001008 Element[] mElements;
1009 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -07001010 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -08001011 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001012 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -07001013
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001014 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001015 * Create a builder object.
1016 *
1017 * @param rs
1018 */
Jason Sams22534172009-08-04 16:58:20 -07001019 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -07001020 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -08001021 mCount = 0;
1022 mElements = new Element[8];
1023 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -07001024 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -07001025 }
1026
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001027 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001028 * Add an array of elements to this element.
1029 *
1030 * @param element
1031 * @param name
1032 * @param arraySize
1033 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001034 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -07001035 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -07001036 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -07001037 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001038
1039 // Skip padding fields after a vector 3 type.
1040 if (mSkipPadding != 0) {
1041 if (name.startsWith("#padding_")) {
1042 mSkipPadding = 0;
1043 return this;
1044 }
1045 }
1046
1047 if (element.mVectorSize == 3) {
1048 mSkipPadding = 1;
1049 } else {
1050 mSkipPadding = 0;
1051 }
1052
Jason Sams718cd1f2009-12-23 14:35:29 -08001053 if(mCount == mElements.length) {
1054 Element[] e = new Element[mCount + 8];
1055 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -07001056 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -08001057 System.arraycopy(mElements, 0, e, 0, mCount);
1058 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001059 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -08001060 mElements = e;
1061 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -07001062 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -07001063 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001064 mElements[mCount] = element;
1065 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -07001066 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -08001067 mCount++;
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001068 return this;
Jason Sams07ae4062009-08-27 20:23:34 -07001069 }
1070
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001071 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001072 * Add a single element to this Element.
1073 *
1074 * @param element
1075 * @param name
1076 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001077 public Builder add(Element element, String name) {
1078 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -07001079 }
1080
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001081 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001082 * Create the element from this builder.
1083 *
1084 *
1085 * @return Element
1086 */
Jason Sams22534172009-08-04 16:58:20 -07001087 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -08001088 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -08001089 Element[] ein = new Element[mCount];
1090 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -07001091 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -08001092 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1093 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001094 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001095
Ashok Bhat98071552014-02-12 09:54:43 +00001096 long[] ids = new long[ein.length];
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001097 for (int ct = 0; ct < ein.length; ct++ ) {
Ashok Bhat98071552014-02-12 09:54:43 +00001098 ids[ct] = ein[ct].getID(mRS);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001099 }
Tim Murray460a0492013-11-19 12:45:54 -08001100 long id = mRS.nElementCreate2(ids, sin, asin);
Jason Sams70d4e502010-09-02 17:35:23 -07001101 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -07001102 }
1103 }
Jason Sams36e612a2009-07-31 16:26:13 -07001104}
1105