blob: 93cabd6e35bc69977dabdb9fc12368db73d086e5 [file] [log] [blame]
Jason Sams36e612a2009-07-31 16:26:13 -07001/*
Stephen Hines3beb60e2012-02-14 20:38:20 -08002 * Copyright (C) 2008-2012 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/**
Robert Ly11518ac2011-02-09 13:57:06 -080023 * <p>The most basic data type. An element represents one cell of a memory allocation.
Alex Sakhartchouk34769772011-02-28 16:01:28 -080024 * Element is the basic data type of Renderscript. An element can be of two forms: Basic elements or Complex forms.
Robert Ly11518ac2011-02-09 13:57:06 -080025 * Examples of basic elements are:</p>
26 * <ul>
27 * <li>Single float value</li>
28 * <li>4 element float vector</li>
29 * <li>single RGB-565 color</li>
30 * <li>single unsigned int 16</li>
31 * </ul>
Alex Sakhartchouk34769772011-02-28 16:01:28 -080032 * <p>Complex elements contain a list of sub-elements and names that
Robert Ly11518ac2011-02-09 13:57:06 -080033 * represents a structure of data. The fields can be accessed by name
34 * from a script or shader. The memory layout is defined and ordered. Data
Stephen Hinesf257e512011-06-14 14:54:29 -070035 * alignment is determined by the most basic primitive type. i.e. a float4
36 * vector will be aligned to sizeof(float) and not sizeof(float4). The
Jason Samsa1b13ed2010-11-12 14:58:37 -080037 * ordering of elements in memory will be the order in which they were added
Robert Ly11518ac2011-02-09 13:57:06 -080038 * with each component aligned as necessary. No re-ordering will be done.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080039 *
Robert Ly11518ac2011-02-09 13:57:06 -080040 * <p>The primary source of elements are from scripts. A script that exports a
41 * bind point for a data structure generates a Renderscript element to represent the
42 * data exported by the script. The other common source of elements is from bitmap formats.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080043 *
44 * <div class="special reference">
45 * <h3>Developer Guides</h3>
46 * <p>For more information about creating an application that uses Renderscript, read the
Scott Mainb47fa162013-02-05 14:23:13 -080047 * <a href="{@docRoot}guide/topics/renderscript/index.html">Renderscript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080048 * </div>
Jason Sams36e612a2009-07-31 16:26:13 -070049 **/
50public class Element extends BaseObj {
Jason Samsea84a7c2009-09-04 14:42:41 -070051 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080052 Element[] mElements;
53 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -070054 int[] mArraySizes;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070055 int[] mOffsetInBytes;
Jason Sams36e612a2009-07-31 16:26:13 -070056
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -080057 int[] mVisibleElementMap;
58
Jason Sams718cd1f2009-12-23 14:35:29 -080059 DataType mType;
60 DataKind mKind;
61 boolean mNormalized;
62 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070063
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -080064 private void updateVisibleSubElements() {
65 if (mElements == null) {
66 return;
67 }
68
69 int noPaddingFieldCount = 0;
70 int fieldCount = mElementNames.length;
71 // Find out how many elements are not padding
72 for (int ct = 0; ct < fieldCount; ct ++) {
73 if (mElementNames[ct].charAt(0) != '#') {
74 noPaddingFieldCount ++;
75 }
76 }
77 mVisibleElementMap = new int[noPaddingFieldCount];
78
79 // Make a map that points us at non-padding elements
80 for (int ct = 0, ctNoPadding = 0; ct < fieldCount; ct ++) {
81 if (mElementNames[ct].charAt(0) != '#') {
82 mVisibleElementMap[ctNoPadding ++] = ct;
83 }
84 }
85 }
86
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070087 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070088 * @return element size in bytes
89 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070090 public int getBytesSize() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070091
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070092 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070093 * Returns the number of vector components. 2 for float2, 4 for
94 * float4, etc.
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -080095 * @return element vector size
96 */
97 public int getVectorSize() {return mVectorSize;}
98
Jason Samsa1b13ed2010-11-12 14:58:37 -080099
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700100 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800101 * DataType represents the basic type information for a basic element. The
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800102 * naming convention follows. For numeric types it is FLOAT,
103 * SIGNED, or UNSIGNED followed by the _BITS where BITS is the
104 * size of the data. BOOLEAN is a true / false (1,0)
105 * represented in an 8 bit container. The UNSIGNED variants
106 * with multiple bit definitions are for packed graphical data
107 * formats and represent vectors with per vector member sizes
108 * which are treated as a single unit for packing and alignment
109 * purposes.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800110 *
111 * MATRIX the three matrix types contain FLOAT_32 elements and are treated
112 * as 32 bits for alignment purposes.
113 *
114 * RS_* objects. 32 bit opaque handles.
115 */
Jason Sams36e612a2009-07-31 16:26:13 -0700116 public enum DataType {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800117 NONE (0, 0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800118 //FLOAT_16 (1, 2),
119 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -0700120 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800121 SIGNED_8 (4, 1),
122 SIGNED_16 (5, 2),
123 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -0700124 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800125 UNSIGNED_8 (8, 1),
126 UNSIGNED_16 (9, 2),
127 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -0700128 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800129
Jason Samsf110d4b2010-06-21 17:42:41 -0700130 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -0800131
Jason Samsf110d4b2010-06-21 17:42:41 -0700132 UNSIGNED_5_6_5 (13, 2),
133 UNSIGNED_5_5_5_1 (14, 2),
134 UNSIGNED_4_4_4_4 (15, 2),
135
Jason Sams1d45c472010-08-25 14:31:48 -0700136 MATRIX_4X4 (16, 64),
137 MATRIX_3X3 (17, 36),
138 MATRIX_2X2 (18, 16),
139
140 RS_ELEMENT (1000, 4),
141 RS_TYPE (1001, 4),
142 RS_ALLOCATION (1002, 4),
143 RS_SAMPLER (1003, 4),
144 RS_SCRIPT (1004, 4),
145 RS_MESH (1005, 4),
146 RS_PROGRAM_FRAGMENT (1006, 4),
147 RS_PROGRAM_VERTEX (1007, 4),
148 RS_PROGRAM_RASTER (1008, 4),
Stephen Hines3a291412012-04-11 17:27:29 -0700149 RS_PROGRAM_STORE (1009, 4),
150 RS_FONT (1010, 4);
Jason Sams36e612a2009-07-31 16:26:13 -0700151
152 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800153 int mSize;
154 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700155 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800156 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700157 }
158 }
159
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700160 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800161 * The special interpretation of the data if required. This is primarly
162 * useful for graphical data. USER indicates no special interpretation is
163 * expected. PIXEL is used in conjunction with the standard data types for
164 * representing texture formats.
165 */
Jason Sams36e612a2009-07-31 16:26:13 -0700166 public enum DataKind {
167 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800168
169 PIXEL_L (7),
170 PIXEL_A (8),
171 PIXEL_LA (9),
172 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700173 PIXEL_RGBA (11),
174 PIXEL_DEPTH (12);
Jason Sams36e612a2009-07-31 16:26:13 -0700175
176 int mID;
177 DataKind(int id) {
178 mID = id;
179 }
180 }
181
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700182 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800183 * Return if a element is too complex for use as a data source for a Mesh or
184 * a Program.
185 *
186 * @return boolean
187 */
Jason Samsc1d62102010-11-04 14:32:19 -0700188 public boolean isComplex() {
189 if (mElements == null) {
190 return false;
191 }
192 for (int ct=0; ct < mElements.length; ct++) {
193 if (mElements[ct].mElements != null) {
194 return true;
195 }
196 }
197 return false;
198 }
199
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700200 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700201 * Elements could be simple, such as an int or a float, or a
202 * structure with multiple sub elements, such as a collection of
203 * floats, float2, float4. This function returns zero for simple
204 * elements or the number of sub-elements otherwise.
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700205 * @return number of sub-elements in this element
206 */
207 public int getSubElementCount() {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800208 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700209 return 0;
210 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800211 return mVisibleElementMap.length;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700212 }
213
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700214 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700215 * For complex elements, this function will return the
216 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700217 * @param index index of the sub-element to return
218 * @return sub-element in this element at given index
219 */
220 public Element getSubElement(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800221 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700222 throw new RSIllegalArgumentException("Element contains no sub-elements");
223 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800224 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700225 throw new RSIllegalArgumentException("Illegal sub-element index");
226 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800227 return mElements[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700228 }
229
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700230 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700231 * For complex elements, this function will return the
232 * sub-element name at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700233 * @param index index of the sub-element
234 * @return sub-element in this element at given index
235 */
236 public String getSubElementName(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800237 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700238 throw new RSIllegalArgumentException("Element contains no sub-elements");
239 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800240 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700241 throw new RSIllegalArgumentException("Illegal sub-element index");
242 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800243 return mElementNames[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700244 }
245
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700246 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700247 * For complex elements, some sub-elements could be statically
248 * sized arrays. This function will return the array size for
249 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700250 * @param index index of the sub-element
251 * @return array size of sub-element in this element at given index
252 */
253 public int getSubElementArraySize(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800254 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700255 throw new RSIllegalArgumentException("Element contains no sub-elements");
256 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800257 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700258 throw new RSIllegalArgumentException("Illegal sub-element index");
259 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800260 return mArraySizes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700261 }
262
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700263 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700264 * This function specifies the location of a sub-element within
265 * the element
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700266 * @param index index of the sub-element
267 * @return offset in bytes of sub-element in this element at given index
268 */
269 public int getSubElementOffsetBytes(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800270 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700271 throw new RSIllegalArgumentException("Element contains no sub-elements");
272 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800273 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700274 throw new RSIllegalArgumentException("Illegal sub-element index");
275 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800276 return mOffsetInBytes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700277 }
278
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700279 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800280 * @return element data type
281 */
282 public DataType getDataType() {
283 return mType;
284 }
285
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700286 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800287 * @return element data kind
288 */
289 public DataKind getDataKind() {
290 return mKind;
291 }
292
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700293 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800294 * Utility function for returning an Element containing a single Boolean.
295 *
296 * @param rs Context to which the element will belong.
297 *
298 * @return Element
299 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700300 public static Element BOOLEAN(RenderScript rs) {
301 if(rs.mElement_BOOLEAN == null) {
302 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
303 }
304 return rs.mElement_BOOLEAN;
305 }
306
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700307 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800308 * Utility function for returning an Element containing a single UNSIGNED_8.
309 *
310 * @param rs Context to which the element will belong.
311 *
312 * @return Element
313 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700314 public static Element U8(RenderScript rs) {
315 if(rs.mElement_U8 == null) {
316 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800317 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700318 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800319 }
320
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700321 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800322 * Utility function for returning an Element containing a single SIGNED_8.
323 *
324 * @param rs Context to which the element will belong.
325 *
326 * @return Element
327 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700328 public static Element I8(RenderScript rs) {
329 if(rs.mElement_I8 == null) {
330 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800331 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700332 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800333 }
334
Jason Samse29f3e72010-06-08 15:40:48 -0700335 public static Element U16(RenderScript rs) {
336 if(rs.mElement_U16 == null) {
337 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
338 }
339 return rs.mElement_U16;
340 }
341
342 public static Element I16(RenderScript rs) {
343 if(rs.mElement_I16 == null) {
344 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
345 }
346 return rs.mElement_I16;
347 }
348
Jason Sams8cb39de2010-06-01 15:47:01 -0700349 public static Element U32(RenderScript rs) {
350 if(rs.mElement_U32 == null) {
351 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800352 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700353 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800354 }
355
Jason Sams8cb39de2010-06-01 15:47:01 -0700356 public static Element I32(RenderScript rs) {
357 if(rs.mElement_I32 == null) {
358 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800359 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700360 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800361 }
362
Stephen Hines52d83632010-10-11 16:10:42 -0700363 public static Element U64(RenderScript rs) {
364 if(rs.mElement_U64 == null) {
365 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
366 }
367 return rs.mElement_U64;
368 }
369
Stephen Hinesef1dac22010-10-01 15:39:33 -0700370 public static Element I64(RenderScript rs) {
371 if(rs.mElement_I64 == null) {
372 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
373 }
374 return rs.mElement_I64;
375 }
376
Jason Sams8cb39de2010-06-01 15:47:01 -0700377 public static Element F32(RenderScript rs) {
378 if(rs.mElement_F32 == null) {
379 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800380 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700381 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800382 }
383
Stephen Hines02f417052010-09-30 15:19:22 -0700384 public static Element F64(RenderScript rs) {
385 if(rs.mElement_F64 == null) {
386 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
387 }
388 return rs.mElement_F64;
389 }
390
Jason Sams8cb39de2010-06-01 15:47:01 -0700391 public static Element ELEMENT(RenderScript rs) {
392 if(rs.mElement_ELEMENT == null) {
393 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700394 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700395 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700396 }
397
Jason Sams8cb39de2010-06-01 15:47:01 -0700398 public static Element TYPE(RenderScript rs) {
399 if(rs.mElement_TYPE == null) {
400 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700401 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700402 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700403 }
404
Jason Sams8cb39de2010-06-01 15:47:01 -0700405 public static Element ALLOCATION(RenderScript rs) {
406 if(rs.mElement_ALLOCATION == null) {
407 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700408 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700409 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700410 }
411
Jason Sams8cb39de2010-06-01 15:47:01 -0700412 public static Element SAMPLER(RenderScript rs) {
413 if(rs.mElement_SAMPLER == null) {
414 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700415 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700416 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700417 }
418
Jason Sams8cb39de2010-06-01 15:47:01 -0700419 public static Element SCRIPT(RenderScript rs) {
420 if(rs.mElement_SCRIPT == null) {
421 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700422 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700423 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700424 }
425
Jason Sams8cb39de2010-06-01 15:47:01 -0700426 public static Element MESH(RenderScript rs) {
427 if(rs.mElement_MESH == null) {
428 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700429 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700430 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700431 }
432
Jason Sams8cb39de2010-06-01 15:47:01 -0700433 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
434 if(rs.mElement_PROGRAM_FRAGMENT == null) {
435 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700436 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700437 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700438 }
439
Jason Sams8cb39de2010-06-01 15:47:01 -0700440 public static Element PROGRAM_VERTEX(RenderScript rs) {
441 if(rs.mElement_PROGRAM_VERTEX == null) {
442 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700443 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700444 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700445 }
446
Jason Sams8cb39de2010-06-01 15:47:01 -0700447 public static Element PROGRAM_RASTER(RenderScript rs) {
448 if(rs.mElement_PROGRAM_RASTER == null) {
449 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700450 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700451 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700452 }
453
Jason Sams8cb39de2010-06-01 15:47:01 -0700454 public static Element PROGRAM_STORE(RenderScript rs) {
455 if(rs.mElement_PROGRAM_STORE == null) {
456 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700457 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700458 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700459 }
460
Stephen Hines3a291412012-04-11 17:27:29 -0700461 public static Element FONT(RenderScript rs) {
462 if(rs.mElement_FONT == null) {
463 rs.mElement_FONT = createUser(rs, DataType.RS_FONT);
464 }
465 return rs.mElement_FONT;
466 }
467
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700468
Jason Sams718cd1f2009-12-23 14:35:29 -0800469 public static Element A_8(RenderScript rs) {
470 if(rs.mElement_A_8 == null) {
471 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
472 }
473 return rs.mElement_A_8;
474 }
475
476 public static Element RGB_565(RenderScript rs) {
477 if(rs.mElement_RGB_565 == null) {
478 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
479 }
480 return rs.mElement_RGB_565;
481 }
482
483 public static Element RGB_888(RenderScript rs) {
484 if(rs.mElement_RGB_888 == null) {
485 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
486 }
487 return rs.mElement_RGB_888;
488 }
489
490 public static Element RGBA_5551(RenderScript rs) {
491 if(rs.mElement_RGBA_5551 == null) {
492 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
493 }
494 return rs.mElement_RGBA_5551;
495 }
496
497 public static Element RGBA_4444(RenderScript rs) {
498 if(rs.mElement_RGBA_4444 == null) {
499 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
500 }
501 return rs.mElement_RGBA_4444;
502 }
503
504 public static Element RGBA_8888(RenderScript rs) {
505 if(rs.mElement_RGBA_8888 == null) {
506 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
507 }
508 return rs.mElement_RGBA_8888;
509 }
510
Jason Sams8cb39de2010-06-01 15:47:01 -0700511 public static Element F32_2(RenderScript rs) {
512 if(rs.mElement_FLOAT_2 == null) {
513 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800514 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700515 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800516 }
517
Jason Sams8cb39de2010-06-01 15:47:01 -0700518 public static Element F32_3(RenderScript rs) {
519 if(rs.mElement_FLOAT_3 == null) {
520 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800521 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700522 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800523 }
524
Jason Sams8cb39de2010-06-01 15:47:01 -0700525 public static Element F32_4(RenderScript rs) {
526 if(rs.mElement_FLOAT_4 == null) {
527 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800528 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700529 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800530 }
531
Stephen Hines836c4a52011-06-01 14:38:10 -0700532 public static Element F64_2(RenderScript rs) {
533 if(rs.mElement_DOUBLE_2 == null) {
534 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
535 }
536 return rs.mElement_DOUBLE_2;
537 }
538
539 public static Element F64_3(RenderScript rs) {
540 if(rs.mElement_DOUBLE_3 == null) {
541 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
542 }
543 return rs.mElement_DOUBLE_3;
544 }
545
546 public static Element F64_4(RenderScript rs) {
547 if(rs.mElement_DOUBLE_4 == null) {
548 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
549 }
550 return rs.mElement_DOUBLE_4;
551 }
552
553 public static Element U8_2(RenderScript rs) {
554 if(rs.mElement_UCHAR_2 == null) {
555 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
556 }
557 return rs.mElement_UCHAR_2;
558 }
559
560 public static Element U8_3(RenderScript rs) {
561 if(rs.mElement_UCHAR_3 == null) {
562 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
563 }
564 return rs.mElement_UCHAR_3;
565 }
566
Jason Sams8cb39de2010-06-01 15:47:01 -0700567 public static Element U8_4(RenderScript rs) {
568 if(rs.mElement_UCHAR_4 == null) {
569 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800570 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700571 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800572 }
573
Stephen Hines836c4a52011-06-01 14:38:10 -0700574 public static Element I8_2(RenderScript rs) {
575 if(rs.mElement_CHAR_2 == null) {
576 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
577 }
578 return rs.mElement_CHAR_2;
579 }
580
581 public static Element I8_3(RenderScript rs) {
582 if(rs.mElement_CHAR_3 == null) {
583 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
584 }
585 return rs.mElement_CHAR_3;
586 }
587
588 public static Element I8_4(RenderScript rs) {
589 if(rs.mElement_CHAR_4 == null) {
590 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
591 }
592 return rs.mElement_CHAR_4;
593 }
594
595 public static Element U16_2(RenderScript rs) {
596 if(rs.mElement_USHORT_2 == null) {
597 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
598 }
599 return rs.mElement_USHORT_2;
600 }
601
602 public static Element U16_3(RenderScript rs) {
603 if(rs.mElement_USHORT_3 == null) {
604 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
605 }
606 return rs.mElement_USHORT_3;
607 }
608
609 public static Element U16_4(RenderScript rs) {
610 if(rs.mElement_USHORT_4 == null) {
611 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
612 }
613 return rs.mElement_USHORT_4;
614 }
615
616 public static Element I16_2(RenderScript rs) {
617 if(rs.mElement_SHORT_2 == null) {
618 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
619 }
620 return rs.mElement_SHORT_2;
621 }
622
623 public static Element I16_3(RenderScript rs) {
624 if(rs.mElement_SHORT_3 == null) {
625 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
626 }
627 return rs.mElement_SHORT_3;
628 }
629
630 public static Element I16_4(RenderScript rs) {
631 if(rs.mElement_SHORT_4 == null) {
632 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
633 }
634 return rs.mElement_SHORT_4;
635 }
636
637 public static Element U32_2(RenderScript rs) {
638 if(rs.mElement_UINT_2 == null) {
639 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
640 }
641 return rs.mElement_UINT_2;
642 }
643
644 public static Element U32_3(RenderScript rs) {
645 if(rs.mElement_UINT_3 == null) {
646 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
647 }
648 return rs.mElement_UINT_3;
649 }
650
651 public static Element U32_4(RenderScript rs) {
652 if(rs.mElement_UINT_4 == null) {
653 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
654 }
655 return rs.mElement_UINT_4;
656 }
657
658 public static Element I32_2(RenderScript rs) {
659 if(rs.mElement_INT_2 == null) {
660 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
661 }
662 return rs.mElement_INT_2;
663 }
664
665 public static Element I32_3(RenderScript rs) {
666 if(rs.mElement_INT_3 == null) {
667 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
668 }
669 return rs.mElement_INT_3;
670 }
671
672 public static Element I32_4(RenderScript rs) {
673 if(rs.mElement_INT_4 == null) {
674 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
675 }
676 return rs.mElement_INT_4;
677 }
678
679 public static Element U64_2(RenderScript rs) {
680 if(rs.mElement_ULONG_2 == null) {
681 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
682 }
683 return rs.mElement_ULONG_2;
684 }
685
686 public static Element U64_3(RenderScript rs) {
687 if(rs.mElement_ULONG_3 == null) {
688 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
689 }
690 return rs.mElement_ULONG_3;
691 }
692
693 public static Element U64_4(RenderScript rs) {
694 if(rs.mElement_ULONG_4 == null) {
695 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
696 }
697 return rs.mElement_ULONG_4;
698 }
699
700 public static Element I64_2(RenderScript rs) {
701 if(rs.mElement_LONG_2 == null) {
702 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
703 }
704 return rs.mElement_LONG_2;
705 }
706
707 public static Element I64_3(RenderScript rs) {
708 if(rs.mElement_LONG_3 == null) {
709 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
710 }
711 return rs.mElement_LONG_3;
712 }
713
714 public static Element I64_4(RenderScript rs) {
715 if(rs.mElement_LONG_4 == null) {
716 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
717 }
718 return rs.mElement_LONG_4;
719 }
720
Jason Sams1d45c472010-08-25 14:31:48 -0700721 public static Element MATRIX_4X4(RenderScript rs) {
722 if(rs.mElement_MATRIX_4X4 == null) {
723 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
724 }
725 return rs.mElement_MATRIX_4X4;
726 }
Jason Sams65c80f82012-05-08 17:30:26 -0700727
728 /** @deprecated use MATRIX_4X4
729 */
Jason Sams1d45c472010-08-25 14:31:48 -0700730 public static Element MATRIX4X4(RenderScript rs) {
731 return MATRIX_4X4(rs);
732 }
733
734 public static Element MATRIX_3X3(RenderScript rs) {
735 if(rs.mElement_MATRIX_3X3 == null) {
736 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
737 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800738 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700739 }
740
741 public static Element MATRIX_2X2(RenderScript rs) {
742 if(rs.mElement_MATRIX_2X2 == null) {
743 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
744 }
745 return rs.mElement_MATRIX_2X2;
746 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800747
Jason Sams70d4e502010-09-02 17:35:23 -0700748 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700749 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700750 mSize = 0;
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -0800751 mVectorSize = 1;
Jason Sams718cd1f2009-12-23 14:35:29 -0800752 mElements = e;
753 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700754 mArraySizes = as;
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800755 mType = DataType.NONE;
756 mKind = DataKind.USER;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700757 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800758 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700759 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700760 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800761 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800762 updateVisibleSubElements();
Jason Sams718cd1f2009-12-23 14:35:29 -0800763 }
764
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700765 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
766 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800767 if ((dt != DataType.UNSIGNED_5_6_5) &&
768 (dt != DataType.UNSIGNED_4_4_4_4) &&
769 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800770 if (size == 3) {
771 mSize = dt.mSize * 4;
772 } else {
773 mSize = dt.mSize * size;
774 }
Jason Sams252c0782011-01-11 17:42:52 -0800775 } else {
776 mSize = dt.mSize;
777 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800778 mType = dt;
779 mKind = dk;
780 mNormalized = norm;
781 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700782 }
783
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700784 Element(int id, RenderScript rs) {
785 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700786 }
787
788 @Override
789 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800790 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700791
792 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
793 int[] dataBuffer = new int[5];
Jason Samse07694b2012-04-03 15:36:36 -0700794 mRS.nElementGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700795
796 mNormalized = dataBuffer[2] == 1 ? true : false;
797 mVectorSize = dataBuffer[3];
798 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700799 for (DataType dt: DataType.values()) {
800 if(dt.mID == dataBuffer[0]){
801 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700802 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700803 }
804 }
805 for (DataKind dk: DataKind.values()) {
806 if(dk.mID == dataBuffer[1]){
807 mKind = dk;
808 }
809 }
810
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700811 int numSubElements = dataBuffer[4];
812 if(numSubElements > 0) {
813 mElements = new Element[numSubElements];
814 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700815 mArraySizes = new int[numSubElements];
816 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700817
818 int[] subElementIds = new int[numSubElements];
Jason Samse07694b2012-04-03 15:36:36 -0700819 mRS.nElementGetSubElements(getID(mRS), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700820 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700821 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700822 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700823 mOffsetInBytes[i] = mSize;
824 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700825 }
826 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800827 updateVisibleSubElements();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700828 }
829
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700830 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800831 * Create a custom Element of the specified DataType. The DataKind will be
832 * set to USER and the vector size to 1 indicating non-vector.
833 *
834 * @param rs The context associated with the new Element.
835 * @param dt The DataType for the new element.
836 * @return Element
837 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800838 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700839 DataKind dk = DataKind.USER;
840 boolean norm = false;
841 int vecSize = 1;
842 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
843 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800844 }
845
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700846 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800847 * Create a custom vector element of the specified DataType and vector size.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800848 * DataKind will be set to USER. Only primitive types (FLOAT_32, FLOAT_64,
849 * SIGNED_8, SIGNED_16, SIGNED_32, SIGNED_64, UNSIGNED_8, UNSIGNED_16,
850 * UNSIGNED_32, UNSIGNED_64, BOOLEAN) are supported.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800851 *
852 * @param rs The context associated with the new Element.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800853 * @param dt The DataType for the new Element.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800854 * @param size Vector size for the new Element. Range 2-4 inclusive
855 * supported.
856 *
857 * @return Element
858 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800859 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800860 if (size < 2 || size > 4) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800861 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700862 }
Stephen Hines3beb60e2012-02-14 20:38:20 -0800863
864 switch (dt) {
865 // Support only primitive integer/float/boolean types as vectors.
866 case FLOAT_32:
867 case FLOAT_64:
868 case SIGNED_8:
869 case SIGNED_16:
870 case SIGNED_32:
871 case SIGNED_64:
872 case UNSIGNED_8:
873 case UNSIGNED_16:
874 case UNSIGNED_32:
875 case UNSIGNED_64:
876 case BOOLEAN: {
877 DataKind dk = DataKind.USER;
878 boolean norm = false;
879 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
880 return new Element(id, rs, dt, dk, norm, size);
881 }
882
883 default: {
884 throw new RSIllegalArgumentException("Cannot create vector of " +
885 "non-primitive type.");
886 }
887 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700888 }
889
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700890 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800891 * Create a new pixel Element type. A matching DataType and DataKind must
892 * be provided. The DataType and DataKind must contain the same number of
893 * components. Vector size will be set to 1.
894 *
895 * @param rs The context associated with the new Element.
896 * @param dt The DataType for the new element.
897 * @param dk The DataKind to specify the mapping of each component in the
898 * DataType.
899 *
900 * @return Element
901 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800902 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800903 if (!(dk == DataKind.PIXEL_L ||
904 dk == DataKind.PIXEL_A ||
905 dk == DataKind.PIXEL_LA ||
906 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700907 dk == DataKind.PIXEL_RGBA ||
908 dk == DataKind.PIXEL_DEPTH)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700909 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800910 }
911 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700912 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800913 dt == DataType.UNSIGNED_5_6_5 ||
914 dt == DataType.UNSIGNED_4_4_4_4 ||
915 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700916 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800917 }
918 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700919 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800920 }
921 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700922 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800923 }
924 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700925 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800926 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700927 if (dt == DataType.UNSIGNED_16 &&
928 dk != DataKind.PIXEL_DEPTH) {
929 throw new RSIllegalArgumentException("Bad kind and type combo");
930 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800931
932 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700933 switch (dk) {
934 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800935 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700936 break;
937 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800938 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700939 break;
940 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800941 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700942 break;
943 case PIXEL_DEPTH:
944 size = 2;
945 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800946 }
947
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700948 boolean norm = true;
949 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
950 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800951 }
Jason Sams36e612a2009-07-31 16:26:13 -0700952
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700953 /**
Stephen Hinesf257e512011-06-14 14:54:29 -0700954 * Check if the current Element is compatible with another Element.
955 * Primitive Elements are compatible if they share the same underlying
956 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
957 * must be equal in order to be compatible. This requires strict name
958 * equivalence for all sub-Elements (in addition to structural equivalence).
959 *
960 * @param e The Element to check compatibility with.
961 *
962 * @return boolean true if the Elements are compatible, otherwise false.
963 */
964 public boolean isCompatible(Element e) {
965 // Try strict BaseObj equality to start with.
966 if (this.equals(e)) {
967 return true;
968 }
969
970 // Ignore mKind because it is allowed to be different (user vs. pixel).
971 // We also ignore mNormalized because it can be different. The mType
Stephen Hines20948112012-02-14 19:42:42 -0800972 // field must not be NONE since we require name equivalence for
973 // all user-created Elements.
Stephen Hinesf257e512011-06-14 14:54:29 -0700974 return ((mSize == e.mSize) &&
Stephen Hines20948112012-02-14 19:42:42 -0800975 (mType != DataType.NONE) &&
Stephen Hinesf257e512011-06-14 14:54:29 -0700976 (mType == e.mType) &&
977 (mVectorSize == e.mVectorSize));
978 }
979
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700980 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800981 * Builder class for producing complex elements with matching field and name
982 * pairs. The builder starts empty. The order in which elements are added
983 * is retained for the layout in memory.
984 *
985 */
Jason Sams36e612a2009-07-31 16:26:13 -0700986 public static class Builder {
987 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800988 Element[] mElements;
989 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700990 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800991 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800992 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -0700993
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700994 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800995 * Create a builder object.
996 *
997 * @param rs
998 */
Jason Sams22534172009-08-04 16:58:20 -0700999 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -07001000 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -08001001 mCount = 0;
1002 mElements = new Element[8];
1003 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -07001004 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -07001005 }
1006
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001007 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001008 * Add an array of elements to this element.
1009 *
1010 * @param element
1011 * @param name
1012 * @param arraySize
1013 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001014 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -07001015 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -07001016 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -07001017 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001018
1019 // Skip padding fields after a vector 3 type.
1020 if (mSkipPadding != 0) {
1021 if (name.startsWith("#padding_")) {
1022 mSkipPadding = 0;
1023 return this;
1024 }
1025 }
1026
1027 if (element.mVectorSize == 3) {
1028 mSkipPadding = 1;
1029 } else {
1030 mSkipPadding = 0;
1031 }
1032
Jason Sams718cd1f2009-12-23 14:35:29 -08001033 if(mCount == mElements.length) {
1034 Element[] e = new Element[mCount + 8];
1035 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -07001036 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -08001037 System.arraycopy(mElements, 0, e, 0, mCount);
1038 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001039 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -08001040 mElements = e;
1041 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -07001042 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -07001043 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001044 mElements[mCount] = element;
1045 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -07001046 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -08001047 mCount++;
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001048 return this;
Jason Sams07ae4062009-08-27 20:23:34 -07001049 }
1050
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001051 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001052 * Add a single element to this Element.
1053 *
1054 * @param element
1055 * @param name
1056 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001057 public Builder add(Element element, String name) {
1058 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -07001059 }
1060
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001061 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001062 * Create the element from this builder.
1063 *
1064 *
1065 * @return Element
1066 */
Jason Sams22534172009-08-04 16:58:20 -07001067 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -08001068 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -08001069 Element[] ein = new Element[mCount];
1070 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -07001071 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -08001072 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1073 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001074 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001075
1076 int[] ids = new int[ein.length];
1077 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Samse07694b2012-04-03 15:36:36 -07001078 ids[ct] = ein[ct].getID(mRS);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001079 }
Jason Sams70d4e502010-09-02 17:35:23 -07001080 int id = mRS.nElementCreate2(ids, sin, asin);
1081 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -07001082 }
1083 }
Jason Sams36e612a2009-07-31 16:26:13 -07001084}
1085