blob: 20e716cb95464460ee3f2d1838041bcc09fc70fc [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
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
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),
Jason Sams8140d7b2012-12-13 17:01:09 -0800174 PIXEL_DEPTH (12),
175 PIXEL_YUV(13);
Jason Sams36e612a2009-07-31 16:26:13 -0700176
177 int mID;
178 DataKind(int id) {
179 mID = id;
180 }
181 }
182
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700183 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800184 * Return if a element is too complex for use as a data source for a Mesh or
185 * a Program.
186 *
187 * @return boolean
188 */
Jason Samsc1d62102010-11-04 14:32:19 -0700189 public boolean isComplex() {
190 if (mElements == null) {
191 return false;
192 }
193 for (int ct=0; ct < mElements.length; ct++) {
194 if (mElements[ct].mElements != null) {
195 return true;
196 }
197 }
198 return false;
199 }
200
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700201 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700202 * Elements could be simple, such as an int or a float, or a
203 * structure with multiple sub elements, such as a collection of
204 * floats, float2, float4. This function returns zero for simple
205 * elements or the number of sub-elements otherwise.
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700206 * @return number of sub-elements in this element
207 */
208 public int getSubElementCount() {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800209 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700210 return 0;
211 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800212 return mVisibleElementMap.length;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700213 }
214
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700215 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700216 * For complex elements, this function will return the
217 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700218 * @param index index of the sub-element to return
219 * @return sub-element in this element at given index
220 */
221 public Element getSubElement(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800222 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700223 throw new RSIllegalArgumentException("Element contains no sub-elements");
224 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800225 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700226 throw new RSIllegalArgumentException("Illegal sub-element index");
227 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800228 return mElements[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700229 }
230
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700231 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700232 * For complex elements, this function will return the
233 * sub-element name at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700234 * @param index index of the sub-element
235 * @return sub-element in this element at given index
236 */
237 public String getSubElementName(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800238 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700239 throw new RSIllegalArgumentException("Element contains no sub-elements");
240 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800241 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700242 throw new RSIllegalArgumentException("Illegal sub-element index");
243 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800244 return mElementNames[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700245 }
246
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700247 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700248 * For complex elements, some sub-elements could be statically
249 * sized arrays. This function will return the array size for
250 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700251 * @param index index of the sub-element
252 * @return array size of sub-element in this element at given index
253 */
254 public int getSubElementArraySize(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800255 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700256 throw new RSIllegalArgumentException("Element contains no sub-elements");
257 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800258 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700259 throw new RSIllegalArgumentException("Illegal sub-element index");
260 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800261 return mArraySizes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700262 }
263
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700264 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700265 * This function specifies the location of a sub-element within
266 * the element
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700267 * @param index index of the sub-element
268 * @return offset in bytes of sub-element in this element at given index
269 */
270 public int getSubElementOffsetBytes(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800271 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700272 throw new RSIllegalArgumentException("Element contains no sub-elements");
273 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800274 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700275 throw new RSIllegalArgumentException("Illegal sub-element index");
276 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800277 return mOffsetInBytes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700278 }
279
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700280 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800281 * @return element data type
282 */
283 public DataType getDataType() {
284 return mType;
285 }
286
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700287 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800288 * @return element data kind
289 */
290 public DataKind getDataKind() {
291 return mKind;
292 }
293
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700294 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800295 * Utility function for returning an Element containing a single Boolean.
296 *
297 * @param rs Context to which the element will belong.
298 *
299 * @return Element
300 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700301 public static Element BOOLEAN(RenderScript rs) {
302 if(rs.mElement_BOOLEAN == null) {
303 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
304 }
305 return rs.mElement_BOOLEAN;
306 }
307
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700308 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800309 * Utility function for returning an Element containing a single UNSIGNED_8.
310 *
311 * @param rs Context to which the element will belong.
312 *
313 * @return Element
314 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700315 public static Element U8(RenderScript rs) {
316 if(rs.mElement_U8 == null) {
317 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800318 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700319 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800320 }
321
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700322 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800323 * Utility function for returning an Element containing a single SIGNED_8.
324 *
325 * @param rs Context to which the element will belong.
326 *
327 * @return Element
328 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700329 public static Element I8(RenderScript rs) {
330 if(rs.mElement_I8 == null) {
331 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800332 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700333 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800334 }
335
Jason Samse29f3e72010-06-08 15:40:48 -0700336 public static Element U16(RenderScript rs) {
337 if(rs.mElement_U16 == null) {
338 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
339 }
340 return rs.mElement_U16;
341 }
342
343 public static Element I16(RenderScript rs) {
344 if(rs.mElement_I16 == null) {
345 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
346 }
347 return rs.mElement_I16;
348 }
349
Jason Sams8cb39de2010-06-01 15:47:01 -0700350 public static Element U32(RenderScript rs) {
351 if(rs.mElement_U32 == null) {
352 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800353 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700354 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800355 }
356
Jason Sams8cb39de2010-06-01 15:47:01 -0700357 public static Element I32(RenderScript rs) {
358 if(rs.mElement_I32 == null) {
359 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800360 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700361 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800362 }
363
Stephen Hines52d83632010-10-11 16:10:42 -0700364 public static Element U64(RenderScript rs) {
365 if(rs.mElement_U64 == null) {
366 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
367 }
368 return rs.mElement_U64;
369 }
370
Stephen Hinesef1dac22010-10-01 15:39:33 -0700371 public static Element I64(RenderScript rs) {
372 if(rs.mElement_I64 == null) {
373 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
374 }
375 return rs.mElement_I64;
376 }
377
Jason Sams8cb39de2010-06-01 15:47:01 -0700378 public static Element F32(RenderScript rs) {
379 if(rs.mElement_F32 == null) {
380 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800381 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700382 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800383 }
384
Stephen Hines02f417052010-09-30 15:19:22 -0700385 public static Element F64(RenderScript rs) {
386 if(rs.mElement_F64 == null) {
387 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
388 }
389 return rs.mElement_F64;
390 }
391
Jason Sams8cb39de2010-06-01 15:47:01 -0700392 public static Element ELEMENT(RenderScript rs) {
393 if(rs.mElement_ELEMENT == null) {
394 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700395 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700396 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700397 }
398
Jason Sams8cb39de2010-06-01 15:47:01 -0700399 public static Element TYPE(RenderScript rs) {
400 if(rs.mElement_TYPE == null) {
401 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700402 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700403 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700404 }
405
Jason Sams8cb39de2010-06-01 15:47:01 -0700406 public static Element ALLOCATION(RenderScript rs) {
407 if(rs.mElement_ALLOCATION == null) {
408 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700409 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700410 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700411 }
412
Jason Sams8cb39de2010-06-01 15:47:01 -0700413 public static Element SAMPLER(RenderScript rs) {
414 if(rs.mElement_SAMPLER == null) {
415 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700416 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700417 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700418 }
419
Jason Sams8cb39de2010-06-01 15:47:01 -0700420 public static Element SCRIPT(RenderScript rs) {
421 if(rs.mElement_SCRIPT == null) {
422 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700423 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700424 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700425 }
426
Jason Sams8cb39de2010-06-01 15:47:01 -0700427 public static Element MESH(RenderScript rs) {
428 if(rs.mElement_MESH == null) {
429 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700430 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700431 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700432 }
433
Jason Sams8cb39de2010-06-01 15:47:01 -0700434 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
435 if(rs.mElement_PROGRAM_FRAGMENT == null) {
436 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700437 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700438 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700439 }
440
Jason Sams8cb39de2010-06-01 15:47:01 -0700441 public static Element PROGRAM_VERTEX(RenderScript rs) {
442 if(rs.mElement_PROGRAM_VERTEX == null) {
443 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700444 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700445 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700446 }
447
Jason Sams8cb39de2010-06-01 15:47:01 -0700448 public static Element PROGRAM_RASTER(RenderScript rs) {
449 if(rs.mElement_PROGRAM_RASTER == null) {
450 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700451 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700452 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700453 }
454
Jason Sams8cb39de2010-06-01 15:47:01 -0700455 public static Element PROGRAM_STORE(RenderScript rs) {
456 if(rs.mElement_PROGRAM_STORE == null) {
457 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700458 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700459 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700460 }
461
Stephen Hines3a291412012-04-11 17:27:29 -0700462 public static Element FONT(RenderScript rs) {
463 if(rs.mElement_FONT == null) {
464 rs.mElement_FONT = createUser(rs, DataType.RS_FONT);
465 }
466 return rs.mElement_FONT;
467 }
468
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700469
Jason Sams718cd1f2009-12-23 14:35:29 -0800470 public static Element A_8(RenderScript rs) {
471 if(rs.mElement_A_8 == null) {
472 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
473 }
474 return rs.mElement_A_8;
475 }
476
477 public static Element RGB_565(RenderScript rs) {
478 if(rs.mElement_RGB_565 == null) {
479 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
480 }
481 return rs.mElement_RGB_565;
482 }
483
484 public static Element RGB_888(RenderScript rs) {
485 if(rs.mElement_RGB_888 == null) {
486 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
487 }
488 return rs.mElement_RGB_888;
489 }
490
491 public static Element RGBA_5551(RenderScript rs) {
492 if(rs.mElement_RGBA_5551 == null) {
493 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
494 }
495 return rs.mElement_RGBA_5551;
496 }
497
498 public static Element RGBA_4444(RenderScript rs) {
499 if(rs.mElement_RGBA_4444 == null) {
500 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
501 }
502 return rs.mElement_RGBA_4444;
503 }
504
505 public static Element RGBA_8888(RenderScript rs) {
506 if(rs.mElement_RGBA_8888 == null) {
507 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
508 }
509 return rs.mElement_RGBA_8888;
510 }
511
Jason Sams8cb39de2010-06-01 15:47:01 -0700512 public static Element F32_2(RenderScript rs) {
513 if(rs.mElement_FLOAT_2 == null) {
514 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800515 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700516 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800517 }
518
Jason Sams8cb39de2010-06-01 15:47:01 -0700519 public static Element F32_3(RenderScript rs) {
520 if(rs.mElement_FLOAT_3 == null) {
521 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800522 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700523 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800524 }
525
Jason Sams8cb39de2010-06-01 15:47:01 -0700526 public static Element F32_4(RenderScript rs) {
527 if(rs.mElement_FLOAT_4 == null) {
528 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800529 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700530 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800531 }
532
Stephen Hines836c4a52011-06-01 14:38:10 -0700533 public static Element F64_2(RenderScript rs) {
534 if(rs.mElement_DOUBLE_2 == null) {
535 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
536 }
537 return rs.mElement_DOUBLE_2;
538 }
539
540 public static Element F64_3(RenderScript rs) {
541 if(rs.mElement_DOUBLE_3 == null) {
542 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
543 }
544 return rs.mElement_DOUBLE_3;
545 }
546
547 public static Element F64_4(RenderScript rs) {
548 if(rs.mElement_DOUBLE_4 == null) {
549 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
550 }
551 return rs.mElement_DOUBLE_4;
552 }
553
554 public static Element U8_2(RenderScript rs) {
555 if(rs.mElement_UCHAR_2 == null) {
556 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
557 }
558 return rs.mElement_UCHAR_2;
559 }
560
561 public static Element U8_3(RenderScript rs) {
562 if(rs.mElement_UCHAR_3 == null) {
563 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
564 }
565 return rs.mElement_UCHAR_3;
566 }
567
Jason Sams8cb39de2010-06-01 15:47:01 -0700568 public static Element U8_4(RenderScript rs) {
569 if(rs.mElement_UCHAR_4 == null) {
570 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800571 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700572 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800573 }
574
Stephen Hines836c4a52011-06-01 14:38:10 -0700575 public static Element I8_2(RenderScript rs) {
576 if(rs.mElement_CHAR_2 == null) {
577 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
578 }
579 return rs.mElement_CHAR_2;
580 }
581
582 public static Element I8_3(RenderScript rs) {
583 if(rs.mElement_CHAR_3 == null) {
584 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
585 }
586 return rs.mElement_CHAR_3;
587 }
588
589 public static Element I8_4(RenderScript rs) {
590 if(rs.mElement_CHAR_4 == null) {
591 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
592 }
593 return rs.mElement_CHAR_4;
594 }
595
596 public static Element U16_2(RenderScript rs) {
597 if(rs.mElement_USHORT_2 == null) {
598 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
599 }
600 return rs.mElement_USHORT_2;
601 }
602
603 public static Element U16_3(RenderScript rs) {
604 if(rs.mElement_USHORT_3 == null) {
605 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
606 }
607 return rs.mElement_USHORT_3;
608 }
609
610 public static Element U16_4(RenderScript rs) {
611 if(rs.mElement_USHORT_4 == null) {
612 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
613 }
614 return rs.mElement_USHORT_4;
615 }
616
617 public static Element I16_2(RenderScript rs) {
618 if(rs.mElement_SHORT_2 == null) {
619 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
620 }
621 return rs.mElement_SHORT_2;
622 }
623
624 public static Element I16_3(RenderScript rs) {
625 if(rs.mElement_SHORT_3 == null) {
626 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
627 }
628 return rs.mElement_SHORT_3;
629 }
630
631 public static Element I16_4(RenderScript rs) {
632 if(rs.mElement_SHORT_4 == null) {
633 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
634 }
635 return rs.mElement_SHORT_4;
636 }
637
638 public static Element U32_2(RenderScript rs) {
639 if(rs.mElement_UINT_2 == null) {
640 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
641 }
642 return rs.mElement_UINT_2;
643 }
644
645 public static Element U32_3(RenderScript rs) {
646 if(rs.mElement_UINT_3 == null) {
647 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
648 }
649 return rs.mElement_UINT_3;
650 }
651
652 public static Element U32_4(RenderScript rs) {
653 if(rs.mElement_UINT_4 == null) {
654 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
655 }
656 return rs.mElement_UINT_4;
657 }
658
659 public static Element I32_2(RenderScript rs) {
660 if(rs.mElement_INT_2 == null) {
661 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
662 }
663 return rs.mElement_INT_2;
664 }
665
666 public static Element I32_3(RenderScript rs) {
667 if(rs.mElement_INT_3 == null) {
668 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
669 }
670 return rs.mElement_INT_3;
671 }
672
673 public static Element I32_4(RenderScript rs) {
674 if(rs.mElement_INT_4 == null) {
675 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
676 }
677 return rs.mElement_INT_4;
678 }
679
680 public static Element U64_2(RenderScript rs) {
681 if(rs.mElement_ULONG_2 == null) {
682 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
683 }
684 return rs.mElement_ULONG_2;
685 }
686
687 public static Element U64_3(RenderScript rs) {
688 if(rs.mElement_ULONG_3 == null) {
689 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
690 }
691 return rs.mElement_ULONG_3;
692 }
693
694 public static Element U64_4(RenderScript rs) {
695 if(rs.mElement_ULONG_4 == null) {
696 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
697 }
698 return rs.mElement_ULONG_4;
699 }
700
701 public static Element I64_2(RenderScript rs) {
702 if(rs.mElement_LONG_2 == null) {
703 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
704 }
705 return rs.mElement_LONG_2;
706 }
707
708 public static Element I64_3(RenderScript rs) {
709 if(rs.mElement_LONG_3 == null) {
710 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
711 }
712 return rs.mElement_LONG_3;
713 }
714
715 public static Element I64_4(RenderScript rs) {
716 if(rs.mElement_LONG_4 == null) {
717 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
718 }
719 return rs.mElement_LONG_4;
720 }
721
Jason Sams1d45c472010-08-25 14:31:48 -0700722 public static Element MATRIX_4X4(RenderScript rs) {
723 if(rs.mElement_MATRIX_4X4 == null) {
724 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
725 }
726 return rs.mElement_MATRIX_4X4;
727 }
Jason Sams65c80f82012-05-08 17:30:26 -0700728
729 /** @deprecated use MATRIX_4X4
730 */
Jason Sams1d45c472010-08-25 14:31:48 -0700731 public static Element MATRIX4X4(RenderScript rs) {
732 return MATRIX_4X4(rs);
733 }
734
735 public static Element MATRIX_3X3(RenderScript rs) {
736 if(rs.mElement_MATRIX_3X3 == null) {
737 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
738 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800739 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700740 }
741
742 public static Element MATRIX_2X2(RenderScript rs) {
743 if(rs.mElement_MATRIX_2X2 == null) {
744 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
745 }
746 return rs.mElement_MATRIX_2X2;
747 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800748
Jason Sams70d4e502010-09-02 17:35:23 -0700749 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700750 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700751 mSize = 0;
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -0800752 mVectorSize = 1;
Jason Sams718cd1f2009-12-23 14:35:29 -0800753 mElements = e;
754 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700755 mArraySizes = as;
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800756 mType = DataType.NONE;
757 mKind = DataKind.USER;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700758 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800759 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700760 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700761 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800762 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800763 updateVisibleSubElements();
Jason Sams718cd1f2009-12-23 14:35:29 -0800764 }
765
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700766 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
767 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800768 if ((dt != DataType.UNSIGNED_5_6_5) &&
769 (dt != DataType.UNSIGNED_4_4_4_4) &&
770 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800771 if (size == 3) {
772 mSize = dt.mSize * 4;
773 } else {
774 mSize = dt.mSize * size;
775 }
Jason Sams252c0782011-01-11 17:42:52 -0800776 } else {
777 mSize = dt.mSize;
778 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800779 mType = dt;
780 mKind = dk;
781 mNormalized = norm;
782 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700783 }
784
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700785 Element(int id, RenderScript rs) {
786 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700787 }
788
789 @Override
790 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800791 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700792
793 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
794 int[] dataBuffer = new int[5];
Jason Samse07694b2012-04-03 15:36:36 -0700795 mRS.nElementGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700796
797 mNormalized = dataBuffer[2] == 1 ? true : false;
798 mVectorSize = dataBuffer[3];
799 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700800 for (DataType dt: DataType.values()) {
801 if(dt.mID == dataBuffer[0]){
802 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700803 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700804 }
805 }
806 for (DataKind dk: DataKind.values()) {
807 if(dk.mID == dataBuffer[1]){
808 mKind = dk;
809 }
810 }
811
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700812 int numSubElements = dataBuffer[4];
813 if(numSubElements > 0) {
814 mElements = new Element[numSubElements];
815 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700816 mArraySizes = new int[numSubElements];
817 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700818
819 int[] subElementIds = new int[numSubElements];
Jason Samse07694b2012-04-03 15:36:36 -0700820 mRS.nElementGetSubElements(getID(mRS), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700821 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700822 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700823 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700824 mOffsetInBytes[i] = mSize;
825 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700826 }
827 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800828 updateVisibleSubElements();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700829 }
830
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700831 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800832 * Create a custom Element of the specified DataType. The DataKind will be
833 * set to USER and the vector size to 1 indicating non-vector.
834 *
835 * @param rs The context associated with the new Element.
836 * @param dt The DataType for the new element.
837 * @return Element
838 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800839 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700840 DataKind dk = DataKind.USER;
841 boolean norm = false;
842 int vecSize = 1;
843 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
844 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800845 }
846
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700847 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800848 * Create a custom vector element of the specified DataType and vector size.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800849 * DataKind will be set to USER. Only primitive types (FLOAT_32, FLOAT_64,
850 * SIGNED_8, SIGNED_16, SIGNED_32, SIGNED_64, UNSIGNED_8, UNSIGNED_16,
851 * UNSIGNED_32, UNSIGNED_64, BOOLEAN) are supported.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800852 *
853 * @param rs The context associated with the new Element.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800854 * @param dt The DataType for the new Element.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800855 * @param size Vector size for the new Element. Range 2-4 inclusive
856 * supported.
857 *
858 * @return Element
859 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800860 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800861 if (size < 2 || size > 4) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800862 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700863 }
Stephen Hines3beb60e2012-02-14 20:38:20 -0800864
865 switch (dt) {
866 // Support only primitive integer/float/boolean types as vectors.
867 case FLOAT_32:
868 case FLOAT_64:
869 case SIGNED_8:
870 case SIGNED_16:
871 case SIGNED_32:
872 case SIGNED_64:
873 case UNSIGNED_8:
874 case UNSIGNED_16:
875 case UNSIGNED_32:
876 case UNSIGNED_64:
877 case BOOLEAN: {
878 DataKind dk = DataKind.USER;
879 boolean norm = false;
880 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
881 return new Element(id, rs, dt, dk, norm, size);
882 }
883
884 default: {
885 throw new RSIllegalArgumentException("Cannot create vector of " +
886 "non-primitive type.");
887 }
888 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700889 }
890
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700891 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800892 * Create a new pixel Element type. A matching DataType and DataKind must
893 * be provided. The DataType and DataKind must contain the same number of
894 * components. Vector size will be set to 1.
895 *
896 * @param rs The context associated with the new Element.
897 * @param dt The DataType for the new element.
898 * @param dk The DataKind to specify the mapping of each component in the
899 * DataType.
900 *
901 * @return Element
902 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800903 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800904 if (!(dk == DataKind.PIXEL_L ||
905 dk == DataKind.PIXEL_A ||
906 dk == DataKind.PIXEL_LA ||
907 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700908 dk == DataKind.PIXEL_RGBA ||
909 dk == DataKind.PIXEL_DEPTH)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700910 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800911 }
912 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700913 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800914 dt == DataType.UNSIGNED_5_6_5 ||
915 dt == DataType.UNSIGNED_4_4_4_4 ||
916 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700917 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800918 }
919 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700920 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800921 }
922 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700923 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800924 }
925 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700926 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800927 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700928 if (dt == DataType.UNSIGNED_16 &&
929 dk != DataKind.PIXEL_DEPTH) {
930 throw new RSIllegalArgumentException("Bad kind and type combo");
931 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800932
933 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700934 switch (dk) {
935 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800936 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700937 break;
938 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800939 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700940 break;
941 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800942 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700943 break;
944 case PIXEL_DEPTH:
945 size = 2;
946 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800947 }
948
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700949 boolean norm = true;
950 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
951 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800952 }
Jason Sams36e612a2009-07-31 16:26:13 -0700953
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700954 /**
Stephen Hinesf257e512011-06-14 14:54:29 -0700955 * Check if the current Element is compatible with another Element.
956 * Primitive Elements are compatible if they share the same underlying
957 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
958 * must be equal in order to be compatible. This requires strict name
959 * equivalence for all sub-Elements (in addition to structural equivalence).
960 *
961 * @param e The Element to check compatibility with.
962 *
963 * @return boolean true if the Elements are compatible, otherwise false.
964 */
965 public boolean isCompatible(Element e) {
966 // Try strict BaseObj equality to start with.
967 if (this.equals(e)) {
968 return true;
969 }
970
971 // Ignore mKind because it is allowed to be different (user vs. pixel).
972 // We also ignore mNormalized because it can be different. The mType
Stephen Hines20948112012-02-14 19:42:42 -0800973 // field must not be NONE since we require name equivalence for
974 // all user-created Elements.
Stephen Hinesf257e512011-06-14 14:54:29 -0700975 return ((mSize == e.mSize) &&
Stephen Hines20948112012-02-14 19:42:42 -0800976 (mType != DataType.NONE) &&
Stephen Hinesf257e512011-06-14 14:54:29 -0700977 (mType == e.mType) &&
978 (mVectorSize == e.mVectorSize));
979 }
980
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700981 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800982 * Builder class for producing complex elements with matching field and name
983 * pairs. The builder starts empty. The order in which elements are added
984 * is retained for the layout in memory.
985 *
986 */
Jason Sams36e612a2009-07-31 16:26:13 -0700987 public static class Builder {
988 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800989 Element[] mElements;
990 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700991 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800992 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800993 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -0700994
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700995 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800996 * Create a builder object.
997 *
998 * @param rs
999 */
Jason Sams22534172009-08-04 16:58:20 -07001000 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -07001001 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -08001002 mCount = 0;
1003 mElements = new Element[8];
1004 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -07001005 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -07001006 }
1007
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001008 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001009 * Add an array of elements to this element.
1010 *
1011 * @param element
1012 * @param name
1013 * @param arraySize
1014 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001015 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -07001016 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -07001017 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -07001018 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001019
1020 // Skip padding fields after a vector 3 type.
1021 if (mSkipPadding != 0) {
1022 if (name.startsWith("#padding_")) {
1023 mSkipPadding = 0;
1024 return this;
1025 }
1026 }
1027
1028 if (element.mVectorSize == 3) {
1029 mSkipPadding = 1;
1030 } else {
1031 mSkipPadding = 0;
1032 }
1033
Jason Sams718cd1f2009-12-23 14:35:29 -08001034 if(mCount == mElements.length) {
1035 Element[] e = new Element[mCount + 8];
1036 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -07001037 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -08001038 System.arraycopy(mElements, 0, e, 0, mCount);
1039 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001040 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -08001041 mElements = e;
1042 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -07001043 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -07001044 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001045 mElements[mCount] = element;
1046 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -07001047 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -08001048 mCount++;
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001049 return this;
Jason Sams07ae4062009-08-27 20:23:34 -07001050 }
1051
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001052 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001053 * Add a single element to this Element.
1054 *
1055 * @param element
1056 * @param name
1057 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001058 public Builder add(Element element, String name) {
1059 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -07001060 }
1061
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001062 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001063 * Create the element from this builder.
1064 *
1065 *
1066 * @return Element
1067 */
Jason Sams22534172009-08-04 16:58:20 -07001068 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -08001069 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -08001070 Element[] ein = new Element[mCount];
1071 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -07001072 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -08001073 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1074 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001075 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001076
1077 int[] ids = new int[ein.length];
1078 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Samse07694b2012-04-03 15:36:36 -07001079 ids[ct] = ein[ct].getID(mRS);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001080 }
Jason Sams70d4e502010-09-02 17:35:23 -07001081 int id = mRS.nElementCreate2(ids, sin, asin);
1082 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -07001083 }
1084 }
Jason Sams36e612a2009-07-31 16:26:13 -07001085}
1086