blob: 28914ce1a3bf9695b5fa8246eef83a19b553e563 [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
22/**
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
47 * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p>
48 * </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
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -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
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -080092 /**
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
100 /**
101 * 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
Jason Samsa1b13ed2010-11-12 14:58:37 -0800160 /**
161 * 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
Jason Samsa1b13ed2010-11-12 14:58:37 -0800182 /**
183 * 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
Jason Samsa1b13ed2010-11-12 14:58:37 -0800200 /**
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
214 /**
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
230 /**
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
246 /**
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
263 /**
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
279 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800280 * @return element data type
281 */
282 public DataType getDataType() {
283 return mType;
284 }
285
286 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800287 * @return element data kind
288 */
289 public DataKind getDataKind() {
290 return mKind;
291 }
292
293 /**
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
Jason Samsa1b13ed2010-11-12 14:58:37 -0800307 /**
308 * 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
Jason Samsa1b13ed2010-11-12 14:58:37 -0800321 /**
322 * 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
Jason Samsa70f4162010-03-26 15:33:42 -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 }
727 public static Element MATRIX4X4(RenderScript rs) {
728 return MATRIX_4X4(rs);
729 }
730
731 public static Element MATRIX_3X3(RenderScript rs) {
732 if(rs.mElement_MATRIX_3X3 == null) {
733 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
734 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800735 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700736 }
737
738 public static Element MATRIX_2X2(RenderScript rs) {
739 if(rs.mElement_MATRIX_2X2 == null) {
740 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
741 }
742 return rs.mElement_MATRIX_2X2;
743 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800744
Jason Sams70d4e502010-09-02 17:35:23 -0700745 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700746 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700747 mSize = 0;
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -0800748 mVectorSize = 1;
Jason Sams718cd1f2009-12-23 14:35:29 -0800749 mElements = e;
750 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700751 mArraySizes = as;
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800752 mType = DataType.NONE;
753 mKind = DataKind.USER;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700754 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800755 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700756 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700757 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800758 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800759 updateVisibleSubElements();
Jason Sams718cd1f2009-12-23 14:35:29 -0800760 }
761
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700762 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
763 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800764 if ((dt != DataType.UNSIGNED_5_6_5) &&
765 (dt != DataType.UNSIGNED_4_4_4_4) &&
766 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800767 if (size == 3) {
768 mSize = dt.mSize * 4;
769 } else {
770 mSize = dt.mSize * size;
771 }
Jason Sams252c0782011-01-11 17:42:52 -0800772 } else {
773 mSize = dt.mSize;
774 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800775 mType = dt;
776 mKind = dk;
777 mNormalized = norm;
778 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700779 }
780
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700781 Element(int id, RenderScript rs) {
782 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700783 }
784
785 @Override
786 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800787 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700788
789 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
790 int[] dataBuffer = new int[5];
Jason Samse07694b2012-04-03 15:36:36 -0700791 mRS.nElementGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700792
793 mNormalized = dataBuffer[2] == 1 ? true : false;
794 mVectorSize = dataBuffer[3];
795 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700796 for (DataType dt: DataType.values()) {
797 if(dt.mID == dataBuffer[0]){
798 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700799 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700800 }
801 }
802 for (DataKind dk: DataKind.values()) {
803 if(dk.mID == dataBuffer[1]){
804 mKind = dk;
805 }
806 }
807
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700808 int numSubElements = dataBuffer[4];
809 if(numSubElements > 0) {
810 mElements = new Element[numSubElements];
811 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700812 mArraySizes = new int[numSubElements];
813 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700814
815 int[] subElementIds = new int[numSubElements];
Jason Samse07694b2012-04-03 15:36:36 -0700816 mRS.nElementGetSubElements(getID(mRS), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700817 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700818 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700819 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700820 mOffsetInBytes[i] = mSize;
821 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700822 }
823 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800824 updateVisibleSubElements();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700825 }
826
Jason Samsa1b13ed2010-11-12 14:58:37 -0800827 /**
828 * Create a custom Element of the specified DataType. The DataKind will be
829 * set to USER and the vector size to 1 indicating non-vector.
830 *
831 * @param rs The context associated with the new Element.
832 * @param dt The DataType for the new element.
833 * @return Element
834 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800835 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700836 DataKind dk = DataKind.USER;
837 boolean norm = false;
838 int vecSize = 1;
839 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
840 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800841 }
842
Jason Samsa1b13ed2010-11-12 14:58:37 -0800843 /**
844 * Create a custom vector element of the specified DataType and vector size.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800845 * DataKind will be set to USER. Only primitive types (FLOAT_32, FLOAT_64,
846 * SIGNED_8, SIGNED_16, SIGNED_32, SIGNED_64, UNSIGNED_8, UNSIGNED_16,
847 * UNSIGNED_32, UNSIGNED_64, BOOLEAN) are supported.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800848 *
849 * @param rs The context associated with the new Element.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800850 * @param dt The DataType for the new Element.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800851 * @param size Vector size for the new Element. Range 2-4 inclusive
852 * supported.
853 *
854 * @return Element
855 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800856 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800857 if (size < 2 || size > 4) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800858 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700859 }
Stephen Hines3beb60e2012-02-14 20:38:20 -0800860
861 switch (dt) {
862 // Support only primitive integer/float/boolean types as vectors.
863 case FLOAT_32:
864 case FLOAT_64:
865 case SIGNED_8:
866 case SIGNED_16:
867 case SIGNED_32:
868 case SIGNED_64:
869 case UNSIGNED_8:
870 case UNSIGNED_16:
871 case UNSIGNED_32:
872 case UNSIGNED_64:
873 case BOOLEAN: {
874 DataKind dk = DataKind.USER;
875 boolean norm = false;
876 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
877 return new Element(id, rs, dt, dk, norm, size);
878 }
879
880 default: {
881 throw new RSIllegalArgumentException("Cannot create vector of " +
882 "non-primitive type.");
883 }
884 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700885 }
886
Jason Samsa1b13ed2010-11-12 14:58:37 -0800887 /**
888 * Create a new pixel Element type. A matching DataType and DataKind must
889 * be provided. The DataType and DataKind must contain the same number of
890 * components. Vector size will be set to 1.
891 *
892 * @param rs The context associated with the new Element.
893 * @param dt The DataType for the new element.
894 * @param dk The DataKind to specify the mapping of each component in the
895 * DataType.
896 *
897 * @return Element
898 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800899 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800900 if (!(dk == DataKind.PIXEL_L ||
901 dk == DataKind.PIXEL_A ||
902 dk == DataKind.PIXEL_LA ||
903 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700904 dk == DataKind.PIXEL_RGBA ||
905 dk == DataKind.PIXEL_DEPTH)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700906 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800907 }
908 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700909 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800910 dt == DataType.UNSIGNED_5_6_5 ||
911 dt == DataType.UNSIGNED_4_4_4_4 ||
912 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700913 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800914 }
915 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700916 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800917 }
918 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
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_4_4_4_4 && 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 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700924 if (dt == DataType.UNSIGNED_16 &&
925 dk != DataKind.PIXEL_DEPTH) {
926 throw new RSIllegalArgumentException("Bad kind and type combo");
927 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800928
929 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700930 switch (dk) {
931 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800932 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700933 break;
934 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800935 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700936 break;
937 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800938 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700939 break;
940 case PIXEL_DEPTH:
941 size = 2;
942 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800943 }
944
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700945 boolean norm = true;
946 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
947 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800948 }
Jason Sams36e612a2009-07-31 16:26:13 -0700949
Jason Samsa1b13ed2010-11-12 14:58:37 -0800950 /**
Stephen Hinesf257e512011-06-14 14:54:29 -0700951 * Check if the current Element is compatible with another Element.
952 * Primitive Elements are compatible if they share the same underlying
953 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
954 * must be equal in order to be compatible. This requires strict name
955 * equivalence for all sub-Elements (in addition to structural equivalence).
956 *
957 * @param e The Element to check compatibility with.
958 *
959 * @return boolean true if the Elements are compatible, otherwise false.
960 */
961 public boolean isCompatible(Element e) {
962 // Try strict BaseObj equality to start with.
963 if (this.equals(e)) {
964 return true;
965 }
966
967 // Ignore mKind because it is allowed to be different (user vs. pixel).
968 // We also ignore mNormalized because it can be different. The mType
Stephen Hines20948112012-02-14 19:42:42 -0800969 // field must not be NONE since we require name equivalence for
970 // all user-created Elements.
Stephen Hinesf257e512011-06-14 14:54:29 -0700971 return ((mSize == e.mSize) &&
Stephen Hines20948112012-02-14 19:42:42 -0800972 (mType != DataType.NONE) &&
Stephen Hinesf257e512011-06-14 14:54:29 -0700973 (mType == e.mType) &&
974 (mVectorSize == e.mVectorSize));
975 }
976
977 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800978 * Builder class for producing complex elements with matching field and name
979 * pairs. The builder starts empty. The order in which elements are added
980 * is retained for the layout in memory.
981 *
982 */
Jason Sams36e612a2009-07-31 16:26:13 -0700983 public static class Builder {
984 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800985 Element[] mElements;
986 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700987 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800988 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800989 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -0700990
Jason Samsa1b13ed2010-11-12 14:58:37 -0800991 /**
992 * Create a builder object.
993 *
994 * @param rs
995 */
Jason Sams22534172009-08-04 16:58:20 -0700996 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700997 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800998 mCount = 0;
999 mElements = new Element[8];
1000 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -07001001 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -07001002 }
1003
Jason Samsa1b13ed2010-11-12 14:58:37 -08001004 /**
1005 * Add an array of elements to this element.
1006 *
1007 * @param element
1008 * @param name
1009 * @param arraySize
1010 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001011 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -07001012 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -07001013 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -07001014 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001015
1016 // Skip padding fields after a vector 3 type.
1017 if (mSkipPadding != 0) {
1018 if (name.startsWith("#padding_")) {
1019 mSkipPadding = 0;
1020 return this;
1021 }
1022 }
1023
1024 if (element.mVectorSize == 3) {
1025 mSkipPadding = 1;
1026 } else {
1027 mSkipPadding = 0;
1028 }
1029
Jason Sams718cd1f2009-12-23 14:35:29 -08001030 if(mCount == mElements.length) {
1031 Element[] e = new Element[mCount + 8];
1032 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -07001033 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -08001034 System.arraycopy(mElements, 0, e, 0, mCount);
1035 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001036 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -08001037 mElements = e;
1038 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -07001039 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -07001040 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001041 mElements[mCount] = element;
1042 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -07001043 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -08001044 mCount++;
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001045 return this;
Jason Sams07ae4062009-08-27 20:23:34 -07001046 }
1047
Jason Samsa1b13ed2010-11-12 14:58:37 -08001048 /**
1049 * Add a single element to this Element.
1050 *
1051 * @param element
1052 * @param name
1053 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001054 public Builder add(Element element, String name) {
1055 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -07001056 }
1057
Jason Samsa1b13ed2010-11-12 14:58:37 -08001058 /**
1059 * Create the element from this builder.
1060 *
1061 *
1062 * @return Element
1063 */
Jason Sams22534172009-08-04 16:58:20 -07001064 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -08001065 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -08001066 Element[] ein = new Element[mCount];
1067 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -07001068 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -08001069 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1070 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001071 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001072
1073 int[] ids = new int[ein.length];
1074 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Samse07694b2012-04-03 15:36:36 -07001075 ids[ct] = ein[ct].getID(mRS);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001076 }
Jason Sams70d4e502010-09-02 17:35:23 -07001077 int id = mRS.nElementCreate2(ids, sin, asin);
1078 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -07001079 }
1080 }
Jason Sams36e612a2009-07-31 16:26:13 -07001081}
1082