blob: 93e839e237d724bd606a36722a94c89d7f421530 [file] [log] [blame]
Jason Sams36e612a2009-07-31 16:26:13 -07001/*
Jason Samsdd6c8b32013-02-15 17:27:24 -08002 * Copyright (C) 2013 The Android Open Source Project
Jason Sams36e612a2009-07-31 16:26:13 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070019/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070020 * <p>An Element represents one item within an {@link
21 * android.renderscript.Allocation}. An Element is roughly equivalent to a C
22 * type in a RenderScript kernel. Elements may be basic or complex. Some basic
23 * elements are</p> <ul> <li>A single float value (equivalent to a float in a
24 * kernel)</li> <li>A four-element float vector (equivalent to a float4 in a
25 * kernel)</li> <li>An unsigned 32-bit integer (equivalent to an unsigned int in
26 * a kernel)</li> <li>A single signed 8-bit integer (equivalent to a char in a
27 * kernel)</li> </ul> <p>A complex element is roughly equivalent to a C struct
28 * and contains a number of basic or complex Elements. From Java code, a complex
29 * element contains a list of sub-elements and names that represents a
30 * particular data structure. Structs used in RS scripts are available to Java
31 * code by using the {@code ScriptField_structname} class that is reflected from
32 * a particular script.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080033 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070034 * <p>Basic Elements are comprised of a {@link
35 * android.renderscript.Element.DataType} and a {@link
36 * android.renderscript.Element.DataKind}. The DataType encodes C type
37 * information of an Element, while the DataKind encodes how that Element should
38 * be interpreted by a {@link android.renderscript.Sampler}. Note that {@link
39 * android.renderscript.Allocation} objects with DataKind {@link
40 * android.renderscript.Element.DataKind#USER} cannot be used as input for a
41 * {@link android.renderscript.Sampler}. In general, {@link
42 * android.renderscript.Allocation} objects that are intended for use with a
43 * {@link android.renderscript.Sampler} should use bitmap-derived Elements such
44 * as {@link android.renderscript.Element#RGBA_8888} or {@link
45 * android.renderscript#Element.A_8}.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080046 *
47 * <div class="special reference">
48 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070049 * <p>For more information about creating an application that uses RenderScript, read the
50 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080051 * </div>
Jason Sams36e612a2009-07-31 16:26:13 -070052 **/
53public class Element extends BaseObj {
Jason Samsea84a7c2009-09-04 14:42:41 -070054 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080055 Element[] mElements;
56 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -070057 int[] mArraySizes;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070058 int[] mOffsetInBytes;
Jason Sams36e612a2009-07-31 16:26:13 -070059
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -080060 int[] mVisibleElementMap;
61
Jason Sams718cd1f2009-12-23 14:35:29 -080062 DataType mType;
63 DataKind mKind;
64 boolean mNormalized;
65 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070066
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -080067 private void updateVisibleSubElements() {
68 if (mElements == null) {
69 return;
70 }
71
72 int noPaddingFieldCount = 0;
73 int fieldCount = mElementNames.length;
74 // Find out how many elements are not padding
75 for (int ct = 0; ct < fieldCount; ct ++) {
76 if (mElementNames[ct].charAt(0) != '#') {
77 noPaddingFieldCount ++;
78 }
79 }
80 mVisibleElementMap = new int[noPaddingFieldCount];
81
82 // Make a map that points us at non-padding elements
83 for (int ct = 0, ctNoPadding = 0; ct < fieldCount; ct ++) {
84 if (mElementNames[ct].charAt(0) != '#') {
85 mVisibleElementMap[ctNoPadding ++] = ct;
86 }
87 }
88 }
89
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070090 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070091 * @return element size in bytes
92 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070093 public int getBytesSize() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070094
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070095 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070096 * Returns the number of vector components. 2 for float2, 4 for
97 * float4, etc.
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -080098 * @return element vector size
99 */
100 public int getVectorSize() {return mVectorSize;}
101
Jason Samsa1b13ed2010-11-12 14:58:37 -0800102
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700103 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800104 * DataType represents the basic type information for a basic element. The
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800105 * naming convention follows. For numeric types it is FLOAT,
106 * SIGNED, or UNSIGNED followed by the _BITS where BITS is the
107 * size of the data. BOOLEAN is a true / false (1,0)
108 * represented in an 8 bit container. The UNSIGNED variants
109 * with multiple bit definitions are for packed graphical data
110 * formats and represent vectors with per vector member sizes
111 * which are treated as a single unit for packing and alignment
112 * purposes.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800113 *
114 * MATRIX the three matrix types contain FLOAT_32 elements and are treated
115 * as 32 bits for alignment purposes.
116 *
117 * RS_* objects. 32 bit opaque handles.
118 */
Jason Sams36e612a2009-07-31 16:26:13 -0700119 public enum DataType {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800120 NONE (0, 0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800121 //FLOAT_16 (1, 2),
122 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -0700123 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800124 SIGNED_8 (4, 1),
125 SIGNED_16 (5, 2),
126 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -0700127 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800128 UNSIGNED_8 (8, 1),
129 UNSIGNED_16 (9, 2),
130 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -0700131 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800132
Jason Samsf110d4b2010-06-21 17:42:41 -0700133 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -0800134
Jason Samsf110d4b2010-06-21 17:42:41 -0700135 UNSIGNED_5_6_5 (13, 2),
136 UNSIGNED_5_5_5_1 (14, 2),
137 UNSIGNED_4_4_4_4 (15, 2),
138
Jason Sams1d45c472010-08-25 14:31:48 -0700139 MATRIX_4X4 (16, 64),
140 MATRIX_3X3 (17, 36),
141 MATRIX_2X2 (18, 16),
142
143 RS_ELEMENT (1000, 4),
144 RS_TYPE (1001, 4),
145 RS_ALLOCATION (1002, 4),
146 RS_SAMPLER (1003, 4),
147 RS_SCRIPT (1004, 4),
148 RS_MESH (1005, 4),
149 RS_PROGRAM_FRAGMENT (1006, 4),
150 RS_PROGRAM_VERTEX (1007, 4),
151 RS_PROGRAM_RASTER (1008, 4),
Stephen Hines3a291412012-04-11 17:27:29 -0700152 RS_PROGRAM_STORE (1009, 4),
153 RS_FONT (1010, 4);
Jason Sams36e612a2009-07-31 16:26:13 -0700154
155 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800156 int mSize;
157 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700158 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800159 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700160 }
161 }
162
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700163 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800164 * The special interpretation of the data if required. This is primarly
165 * useful for graphical data. USER indicates no special interpretation is
166 * expected. PIXEL is used in conjunction with the standard data types for
167 * representing texture formats.
168 */
Jason Sams36e612a2009-07-31 16:26:13 -0700169 public enum DataKind {
170 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800171
172 PIXEL_L (7),
173 PIXEL_A (8),
174 PIXEL_LA (9),
175 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700176 PIXEL_RGBA (11),
Jason Sams8140d7b2012-12-13 17:01:09 -0800177 PIXEL_DEPTH (12),
178 PIXEL_YUV(13);
Jason Sams36e612a2009-07-31 16:26:13 -0700179
180 int mID;
181 DataKind(int id) {
182 mID = id;
183 }
184 }
185
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700186 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800187 * Return if a element is too complex for use as a data source for a Mesh or
188 * a Program.
189 *
190 * @return boolean
191 */
Jason Samsc1d62102010-11-04 14:32:19 -0700192 public boolean isComplex() {
193 if (mElements == null) {
194 return false;
195 }
196 for (int ct=0; ct < mElements.length; ct++) {
197 if (mElements[ct].mElements != null) {
198 return true;
199 }
200 }
201 return false;
202 }
203
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700204 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700205 * Elements could be simple, such as an int or a float, or a
206 * structure with multiple sub elements, such as a collection of
207 * floats, float2, float4. This function returns zero for simple
208 * elements or the number of sub-elements otherwise.
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700209 * @return number of sub-elements in this element
210 */
211 public int getSubElementCount() {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800212 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700213 return 0;
214 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800215 return mVisibleElementMap.length;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700216 }
217
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700218 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700219 * For complex elements, this function will return the
220 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700221 * @param index index of the sub-element to return
222 * @return sub-element in this element at given index
223 */
224 public Element getSubElement(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800225 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700226 throw new RSIllegalArgumentException("Element contains no sub-elements");
227 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800228 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700229 throw new RSIllegalArgumentException("Illegal sub-element index");
230 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800231 return mElements[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700232 }
233
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700234 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700235 * For complex elements, this function will return the
236 * sub-element name at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700237 * @param index index of the sub-element
238 * @return sub-element in this element at given index
239 */
240 public String getSubElementName(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800241 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700242 throw new RSIllegalArgumentException("Element contains no sub-elements");
243 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800244 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700245 throw new RSIllegalArgumentException("Illegal sub-element index");
246 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800247 return mElementNames[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700248 }
249
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700250 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700251 * For complex elements, some sub-elements could be statically
252 * sized arrays. This function will return the array size for
253 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700254 * @param index index of the sub-element
255 * @return array size of sub-element in this element at given index
256 */
257 public int getSubElementArraySize(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800258 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700259 throw new RSIllegalArgumentException("Element contains no sub-elements");
260 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800261 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700262 throw new RSIllegalArgumentException("Illegal sub-element index");
263 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800264 return mArraySizes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700265 }
266
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700267 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700268 * This function specifies the location of a sub-element within
269 * the element
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700270 * @param index index of the sub-element
271 * @return offset in bytes of sub-element in this element at given index
272 */
273 public int getSubElementOffsetBytes(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800274 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700275 throw new RSIllegalArgumentException("Element contains no sub-elements");
276 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800277 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700278 throw new RSIllegalArgumentException("Illegal sub-element index");
279 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800280 return mOffsetInBytes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700281 }
282
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700283 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800284 * @return element data type
285 */
286 public DataType getDataType() {
287 return mType;
288 }
289
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700290 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800291 * @return element data kind
292 */
293 public DataKind getDataKind() {
294 return mKind;
295 }
296
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700297 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800298 * Utility function for returning an Element containing a single Boolean.
299 *
300 * @param rs Context to which the element will belong.
301 *
302 * @return Element
303 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700304 public static Element BOOLEAN(RenderScript rs) {
305 if(rs.mElement_BOOLEAN == null) {
306 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
307 }
308 return rs.mElement_BOOLEAN;
309 }
310
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700311 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800312 * Utility function for returning an Element containing a single UNSIGNED_8.
313 *
314 * @param rs Context to which the element will belong.
315 *
316 * @return Element
317 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700318 public static Element U8(RenderScript rs) {
319 if(rs.mElement_U8 == null) {
320 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800321 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700322 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800323 }
324
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700325 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800326 * Utility function for returning an Element containing a single SIGNED_8.
327 *
328 * @param rs Context to which the element will belong.
329 *
330 * @return Element
331 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700332 public static Element I8(RenderScript rs) {
333 if(rs.mElement_I8 == null) {
334 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800335 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700336 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800337 }
338
Jason Samse29f3e72010-06-08 15:40:48 -0700339 public static Element U16(RenderScript rs) {
340 if(rs.mElement_U16 == null) {
341 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
342 }
343 return rs.mElement_U16;
344 }
345
346 public static Element I16(RenderScript rs) {
347 if(rs.mElement_I16 == null) {
348 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
349 }
350 return rs.mElement_I16;
351 }
352
Jason Sams8cb39de2010-06-01 15:47:01 -0700353 public static Element U32(RenderScript rs) {
354 if(rs.mElement_U32 == null) {
355 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800356 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700357 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800358 }
359
Jason Sams8cb39de2010-06-01 15:47:01 -0700360 public static Element I32(RenderScript rs) {
361 if(rs.mElement_I32 == null) {
362 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800363 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700364 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800365 }
366
Stephen Hines52d83632010-10-11 16:10:42 -0700367 public static Element U64(RenderScript rs) {
368 if(rs.mElement_U64 == null) {
369 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
370 }
371 return rs.mElement_U64;
372 }
373
Stephen Hinesef1dac22010-10-01 15:39:33 -0700374 public static Element I64(RenderScript rs) {
375 if(rs.mElement_I64 == null) {
376 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
377 }
378 return rs.mElement_I64;
379 }
380
Jason Sams8cb39de2010-06-01 15:47:01 -0700381 public static Element F32(RenderScript rs) {
382 if(rs.mElement_F32 == null) {
383 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800384 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700385 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800386 }
387
Stephen Hines02f417052010-09-30 15:19:22 -0700388 public static Element F64(RenderScript rs) {
389 if(rs.mElement_F64 == null) {
390 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
391 }
392 return rs.mElement_F64;
393 }
394
Jason Sams8cb39de2010-06-01 15:47:01 -0700395 public static Element ELEMENT(RenderScript rs) {
396 if(rs.mElement_ELEMENT == null) {
397 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700398 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700399 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700400 }
401
Jason Sams8cb39de2010-06-01 15:47:01 -0700402 public static Element TYPE(RenderScript rs) {
403 if(rs.mElement_TYPE == null) {
404 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700405 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700406 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700407 }
408
Jason Sams8cb39de2010-06-01 15:47:01 -0700409 public static Element ALLOCATION(RenderScript rs) {
410 if(rs.mElement_ALLOCATION == null) {
411 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700412 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700413 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700414 }
415
Jason Sams8cb39de2010-06-01 15:47:01 -0700416 public static Element SAMPLER(RenderScript rs) {
417 if(rs.mElement_SAMPLER == null) {
418 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700419 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700420 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700421 }
422
Jason Sams8cb39de2010-06-01 15:47:01 -0700423 public static Element SCRIPT(RenderScript rs) {
424 if(rs.mElement_SCRIPT == null) {
425 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700426 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700427 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700428 }
429
Jason Sams8cb39de2010-06-01 15:47:01 -0700430 public static Element MESH(RenderScript rs) {
431 if(rs.mElement_MESH == null) {
432 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700433 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700434 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700435 }
436
Jason Sams8cb39de2010-06-01 15:47:01 -0700437 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
438 if(rs.mElement_PROGRAM_FRAGMENT == null) {
439 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700440 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700441 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700442 }
443
Jason Sams8cb39de2010-06-01 15:47:01 -0700444 public static Element PROGRAM_VERTEX(RenderScript rs) {
445 if(rs.mElement_PROGRAM_VERTEX == null) {
446 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700447 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700448 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700449 }
450
Jason Sams8cb39de2010-06-01 15:47:01 -0700451 public static Element PROGRAM_RASTER(RenderScript rs) {
452 if(rs.mElement_PROGRAM_RASTER == null) {
453 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700454 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700455 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700456 }
457
Jason Sams8cb39de2010-06-01 15:47:01 -0700458 public static Element PROGRAM_STORE(RenderScript rs) {
459 if(rs.mElement_PROGRAM_STORE == null) {
460 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700461 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700462 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700463 }
464
Stephen Hines3a291412012-04-11 17:27:29 -0700465 public static Element FONT(RenderScript rs) {
466 if(rs.mElement_FONT == null) {
467 rs.mElement_FONT = createUser(rs, DataType.RS_FONT);
468 }
469 return rs.mElement_FONT;
470 }
471
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700472
Jason Sams718cd1f2009-12-23 14:35:29 -0800473 public static Element A_8(RenderScript rs) {
474 if(rs.mElement_A_8 == null) {
475 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
476 }
477 return rs.mElement_A_8;
478 }
479
480 public static Element RGB_565(RenderScript rs) {
481 if(rs.mElement_RGB_565 == null) {
482 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
483 }
484 return rs.mElement_RGB_565;
485 }
486
487 public static Element RGB_888(RenderScript rs) {
488 if(rs.mElement_RGB_888 == null) {
489 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
490 }
491 return rs.mElement_RGB_888;
492 }
493
494 public static Element RGBA_5551(RenderScript rs) {
495 if(rs.mElement_RGBA_5551 == null) {
496 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
497 }
498 return rs.mElement_RGBA_5551;
499 }
500
501 public static Element RGBA_4444(RenderScript rs) {
502 if(rs.mElement_RGBA_4444 == null) {
503 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
504 }
505 return rs.mElement_RGBA_4444;
506 }
507
508 public static Element RGBA_8888(RenderScript rs) {
509 if(rs.mElement_RGBA_8888 == null) {
510 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
511 }
512 return rs.mElement_RGBA_8888;
513 }
514
Jason Sams8cb39de2010-06-01 15:47:01 -0700515 public static Element F32_2(RenderScript rs) {
516 if(rs.mElement_FLOAT_2 == null) {
517 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800518 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700519 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800520 }
521
Jason Sams8cb39de2010-06-01 15:47:01 -0700522 public static Element F32_3(RenderScript rs) {
523 if(rs.mElement_FLOAT_3 == null) {
524 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800525 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700526 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800527 }
528
Jason Sams8cb39de2010-06-01 15:47:01 -0700529 public static Element F32_4(RenderScript rs) {
530 if(rs.mElement_FLOAT_4 == null) {
531 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800532 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700533 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800534 }
535
Stephen Hines836c4a52011-06-01 14:38:10 -0700536 public static Element F64_2(RenderScript rs) {
537 if(rs.mElement_DOUBLE_2 == null) {
538 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
539 }
540 return rs.mElement_DOUBLE_2;
541 }
542
543 public static Element F64_3(RenderScript rs) {
544 if(rs.mElement_DOUBLE_3 == null) {
545 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
546 }
547 return rs.mElement_DOUBLE_3;
548 }
549
550 public static Element F64_4(RenderScript rs) {
551 if(rs.mElement_DOUBLE_4 == null) {
552 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
553 }
554 return rs.mElement_DOUBLE_4;
555 }
556
557 public static Element U8_2(RenderScript rs) {
558 if(rs.mElement_UCHAR_2 == null) {
559 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
560 }
561 return rs.mElement_UCHAR_2;
562 }
563
564 public static Element U8_3(RenderScript rs) {
565 if(rs.mElement_UCHAR_3 == null) {
566 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
567 }
568 return rs.mElement_UCHAR_3;
569 }
570
Jason Sams8cb39de2010-06-01 15:47:01 -0700571 public static Element U8_4(RenderScript rs) {
572 if(rs.mElement_UCHAR_4 == null) {
573 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800574 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700575 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800576 }
577
Stephen Hines836c4a52011-06-01 14:38:10 -0700578 public static Element I8_2(RenderScript rs) {
579 if(rs.mElement_CHAR_2 == null) {
580 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
581 }
582 return rs.mElement_CHAR_2;
583 }
584
585 public static Element I8_3(RenderScript rs) {
586 if(rs.mElement_CHAR_3 == null) {
587 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
588 }
589 return rs.mElement_CHAR_3;
590 }
591
592 public static Element I8_4(RenderScript rs) {
593 if(rs.mElement_CHAR_4 == null) {
594 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
595 }
596 return rs.mElement_CHAR_4;
597 }
598
599 public static Element U16_2(RenderScript rs) {
600 if(rs.mElement_USHORT_2 == null) {
601 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
602 }
603 return rs.mElement_USHORT_2;
604 }
605
606 public static Element U16_3(RenderScript rs) {
607 if(rs.mElement_USHORT_3 == null) {
608 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
609 }
610 return rs.mElement_USHORT_3;
611 }
612
613 public static Element U16_4(RenderScript rs) {
614 if(rs.mElement_USHORT_4 == null) {
615 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
616 }
617 return rs.mElement_USHORT_4;
618 }
619
620 public static Element I16_2(RenderScript rs) {
621 if(rs.mElement_SHORT_2 == null) {
622 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
623 }
624 return rs.mElement_SHORT_2;
625 }
626
627 public static Element I16_3(RenderScript rs) {
628 if(rs.mElement_SHORT_3 == null) {
629 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
630 }
631 return rs.mElement_SHORT_3;
632 }
633
634 public static Element I16_4(RenderScript rs) {
635 if(rs.mElement_SHORT_4 == null) {
636 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
637 }
638 return rs.mElement_SHORT_4;
639 }
640
641 public static Element U32_2(RenderScript rs) {
642 if(rs.mElement_UINT_2 == null) {
643 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
644 }
645 return rs.mElement_UINT_2;
646 }
647
648 public static Element U32_3(RenderScript rs) {
649 if(rs.mElement_UINT_3 == null) {
650 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
651 }
652 return rs.mElement_UINT_3;
653 }
654
655 public static Element U32_4(RenderScript rs) {
656 if(rs.mElement_UINT_4 == null) {
657 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
658 }
659 return rs.mElement_UINT_4;
660 }
661
662 public static Element I32_2(RenderScript rs) {
663 if(rs.mElement_INT_2 == null) {
664 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
665 }
666 return rs.mElement_INT_2;
667 }
668
669 public static Element I32_3(RenderScript rs) {
670 if(rs.mElement_INT_3 == null) {
671 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
672 }
673 return rs.mElement_INT_3;
674 }
675
676 public static Element I32_4(RenderScript rs) {
677 if(rs.mElement_INT_4 == null) {
678 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
679 }
680 return rs.mElement_INT_4;
681 }
682
683 public static Element U64_2(RenderScript rs) {
684 if(rs.mElement_ULONG_2 == null) {
685 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
686 }
687 return rs.mElement_ULONG_2;
688 }
689
690 public static Element U64_3(RenderScript rs) {
691 if(rs.mElement_ULONG_3 == null) {
692 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
693 }
694 return rs.mElement_ULONG_3;
695 }
696
697 public static Element U64_4(RenderScript rs) {
698 if(rs.mElement_ULONG_4 == null) {
699 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
700 }
701 return rs.mElement_ULONG_4;
702 }
703
704 public static Element I64_2(RenderScript rs) {
705 if(rs.mElement_LONG_2 == null) {
706 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
707 }
708 return rs.mElement_LONG_2;
709 }
710
711 public static Element I64_3(RenderScript rs) {
712 if(rs.mElement_LONG_3 == null) {
713 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
714 }
715 return rs.mElement_LONG_3;
716 }
717
718 public static Element I64_4(RenderScript rs) {
719 if(rs.mElement_LONG_4 == null) {
720 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
721 }
722 return rs.mElement_LONG_4;
723 }
724
Tim Murray932e78e2013-09-03 11:42:26 -0700725 public static Element YUV(RenderScript rs) {
726 if (rs.mElement_YUV == null) {
727 rs.mElement_YUV = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_YUV);
728 }
729 return rs.mElement_YUV;
730 }
731
Jason Sams1d45c472010-08-25 14:31:48 -0700732 public static Element MATRIX_4X4(RenderScript rs) {
733 if(rs.mElement_MATRIX_4X4 == null) {
734 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
735 }
736 return rs.mElement_MATRIX_4X4;
737 }
Jason Sams65c80f82012-05-08 17:30:26 -0700738
739 /** @deprecated use MATRIX_4X4
740 */
Jason Sams1d45c472010-08-25 14:31:48 -0700741 public static Element MATRIX4X4(RenderScript rs) {
742 return MATRIX_4X4(rs);
743 }
744
745 public static Element MATRIX_3X3(RenderScript rs) {
746 if(rs.mElement_MATRIX_3X3 == null) {
747 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
748 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800749 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700750 }
751
752 public static Element MATRIX_2X2(RenderScript rs) {
753 if(rs.mElement_MATRIX_2X2 == null) {
754 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
755 }
756 return rs.mElement_MATRIX_2X2;
757 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800758
Tim Murray460a0492013-11-19 12:45:54 -0800759 Element(long id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700760 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700761 mSize = 0;
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -0800762 mVectorSize = 1;
Jason Sams718cd1f2009-12-23 14:35:29 -0800763 mElements = e;
764 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700765 mArraySizes = as;
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800766 mType = DataType.NONE;
767 mKind = DataKind.USER;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700768 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800769 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700770 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700771 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800772 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800773 updateVisibleSubElements();
Jason Sams718cd1f2009-12-23 14:35:29 -0800774 }
775
Tim Murray460a0492013-11-19 12:45:54 -0800776 Element(long id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700777 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800778 if ((dt != DataType.UNSIGNED_5_6_5) &&
779 (dt != DataType.UNSIGNED_4_4_4_4) &&
780 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800781 if (size == 3) {
782 mSize = dt.mSize * 4;
783 } else {
784 mSize = dt.mSize * size;
785 }
Jason Sams252c0782011-01-11 17:42:52 -0800786 } else {
787 mSize = dt.mSize;
788 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800789 mType = dt;
790 mKind = dk;
791 mNormalized = norm;
792 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700793 }
794
Tim Murray460a0492013-11-19 12:45:54 -0800795 Element(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700796 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700797 }
798
799 @Override
800 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800801 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700802
Tim Murray460a0492013-11-19 12:45:54 -0800803 // FIXME: updateFromNative is broken in JNI for 64-bit
804
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700805 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
806 int[] dataBuffer = new int[5];
Jason Samse07694b2012-04-03 15:36:36 -0700807 mRS.nElementGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700808
809 mNormalized = dataBuffer[2] == 1 ? true : false;
810 mVectorSize = dataBuffer[3];
811 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700812 for (DataType dt: DataType.values()) {
813 if(dt.mID == dataBuffer[0]){
814 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700815 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700816 }
817 }
818 for (DataKind dk: DataKind.values()) {
819 if(dk.mID == dataBuffer[1]){
820 mKind = dk;
821 }
822 }
823
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700824 int numSubElements = dataBuffer[4];
825 if(numSubElements > 0) {
826 mElements = new Element[numSubElements];
827 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700828 mArraySizes = new int[numSubElements];
829 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700830
831 int[] subElementIds = new int[numSubElements];
Jason Samse07694b2012-04-03 15:36:36 -0700832 mRS.nElementGetSubElements(getID(mRS), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700833 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700834 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700835 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700836 mOffsetInBytes[i] = mSize;
837 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700838 }
839 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800840 updateVisibleSubElements();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700841 }
842
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700843 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800844 * Create a custom Element of the specified DataType. The DataKind will be
845 * set to USER and the vector size to 1 indicating non-vector.
846 *
847 * @param rs The context associated with the new Element.
848 * @param dt The DataType for the new element.
849 * @return Element
850 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800851 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700852 DataKind dk = DataKind.USER;
853 boolean norm = false;
854 int vecSize = 1;
Tim Murray460a0492013-11-19 12:45:54 -0800855 long id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700856 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800857 }
858
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700859 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800860 * Create a custom vector element of the specified DataType and vector size.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800861 * DataKind will be set to USER. Only primitive types (FLOAT_32, FLOAT_64,
862 * SIGNED_8, SIGNED_16, SIGNED_32, SIGNED_64, UNSIGNED_8, UNSIGNED_16,
863 * UNSIGNED_32, UNSIGNED_64, BOOLEAN) are supported.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800864 *
865 * @param rs The context associated with the new Element.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800866 * @param dt The DataType for the new Element.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800867 * @param size Vector size for the new Element. Range 2-4 inclusive
868 * supported.
869 *
870 * @return Element
871 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800872 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800873 if (size < 2 || size > 4) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800874 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700875 }
Stephen Hines3beb60e2012-02-14 20:38:20 -0800876
877 switch (dt) {
878 // Support only primitive integer/float/boolean types as vectors.
879 case FLOAT_32:
880 case FLOAT_64:
881 case SIGNED_8:
882 case SIGNED_16:
883 case SIGNED_32:
884 case SIGNED_64:
885 case UNSIGNED_8:
886 case UNSIGNED_16:
887 case UNSIGNED_32:
888 case UNSIGNED_64:
889 case BOOLEAN: {
890 DataKind dk = DataKind.USER;
891 boolean norm = false;
Tim Murray460a0492013-11-19 12:45:54 -0800892 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Stephen Hines3beb60e2012-02-14 20:38:20 -0800893 return new Element(id, rs, dt, dk, norm, size);
894 }
895
896 default: {
897 throw new RSIllegalArgumentException("Cannot create vector of " +
898 "non-primitive type.");
899 }
900 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700901 }
902
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700903 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800904 * Create a new pixel Element type. A matching DataType and DataKind must
905 * be provided. The DataType and DataKind must contain the same number of
906 * components. Vector size will be set to 1.
907 *
908 * @param rs The context associated with the new Element.
909 * @param dt The DataType for the new element.
910 * @param dk The DataKind to specify the mapping of each component in the
911 * DataType.
912 *
913 * @return Element
914 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800915 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800916 if (!(dk == DataKind.PIXEL_L ||
917 dk == DataKind.PIXEL_A ||
918 dk == DataKind.PIXEL_LA ||
919 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700920 dk == DataKind.PIXEL_RGBA ||
Jason Samsdd6c8b32013-02-15 17:27:24 -0800921 dk == DataKind.PIXEL_DEPTH ||
922 dk == DataKind.PIXEL_YUV)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700923 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800924 }
925 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700926 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800927 dt == DataType.UNSIGNED_5_6_5 ||
928 dt == DataType.UNSIGNED_4_4_4_4 ||
929 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700930 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800931 }
932 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700933 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800934 }
935 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700936 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800937 }
938 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700939 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800940 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700941 if (dt == DataType.UNSIGNED_16 &&
942 dk != DataKind.PIXEL_DEPTH) {
943 throw new RSIllegalArgumentException("Bad kind and type combo");
944 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800945
946 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700947 switch (dk) {
948 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800949 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700950 break;
951 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800952 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700953 break;
954 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800955 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700956 break;
957 case PIXEL_DEPTH:
958 size = 2;
959 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800960 }
961
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700962 boolean norm = true;
Tim Murray460a0492013-11-19 12:45:54 -0800963 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700964 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800965 }
Jason Sams36e612a2009-07-31 16:26:13 -0700966
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700967 /**
Stephen Hinesf257e512011-06-14 14:54:29 -0700968 * Check if the current Element is compatible with another Element.
969 * Primitive Elements are compatible if they share the same underlying
970 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
971 * must be equal in order to be compatible. This requires strict name
972 * equivalence for all sub-Elements (in addition to structural equivalence).
973 *
974 * @param e The Element to check compatibility with.
975 *
976 * @return boolean true if the Elements are compatible, otherwise false.
977 */
978 public boolean isCompatible(Element e) {
979 // Try strict BaseObj equality to start with.
980 if (this.equals(e)) {
981 return true;
982 }
983
984 // Ignore mKind because it is allowed to be different (user vs. pixel).
985 // We also ignore mNormalized because it can be different. The mType
Stephen Hines20948112012-02-14 19:42:42 -0800986 // field must not be NONE since we require name equivalence for
987 // all user-created Elements.
Stephen Hinesf257e512011-06-14 14:54:29 -0700988 return ((mSize == e.mSize) &&
Stephen Hines20948112012-02-14 19:42:42 -0800989 (mType != DataType.NONE) &&
Stephen Hinesf257e512011-06-14 14:54:29 -0700990 (mType == e.mType) &&
991 (mVectorSize == e.mVectorSize));
992 }
993
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700994 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800995 * Builder class for producing complex elements with matching field and name
996 * pairs. The builder starts empty. The order in which elements are added
997 * is retained for the layout in memory.
998 *
999 */
Jason Sams36e612a2009-07-31 16:26:13 -07001000 public static class Builder {
1001 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -08001002 Element[] mElements;
1003 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -07001004 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -08001005 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001006 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -07001007
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001008 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001009 * Create a builder object.
1010 *
1011 * @param rs
1012 */
Jason Sams22534172009-08-04 16:58:20 -07001013 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -07001014 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -08001015 mCount = 0;
1016 mElements = new Element[8];
1017 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -07001018 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -07001019 }
1020
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001021 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001022 * Add an array of elements to this element.
1023 *
1024 * @param element
1025 * @param name
1026 * @param arraySize
1027 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001028 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -07001029 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -07001030 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -07001031 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001032
1033 // Skip padding fields after a vector 3 type.
1034 if (mSkipPadding != 0) {
1035 if (name.startsWith("#padding_")) {
1036 mSkipPadding = 0;
1037 return this;
1038 }
1039 }
1040
1041 if (element.mVectorSize == 3) {
1042 mSkipPadding = 1;
1043 } else {
1044 mSkipPadding = 0;
1045 }
1046
Jason Sams718cd1f2009-12-23 14:35:29 -08001047 if(mCount == mElements.length) {
1048 Element[] e = new Element[mCount + 8];
1049 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -07001050 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -08001051 System.arraycopy(mElements, 0, e, 0, mCount);
1052 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001053 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -08001054 mElements = e;
1055 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -07001056 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -07001057 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001058 mElements[mCount] = element;
1059 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -07001060 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -08001061 mCount++;
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001062 return this;
Jason Sams07ae4062009-08-27 20:23:34 -07001063 }
1064
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001065 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001066 * Add a single element to this Element.
1067 *
1068 * @param element
1069 * @param name
1070 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001071 public Builder add(Element element, String name) {
1072 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -07001073 }
1074
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001075 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001076 * Create the element from this builder.
1077 *
1078 *
1079 * @return Element
1080 */
Jason Sams22534172009-08-04 16:58:20 -07001081 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -08001082 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -08001083 Element[] ein = new Element[mCount];
1084 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -07001085 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -08001086 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1087 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001088 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001089
Tim Murray460a0492013-11-19 12:45:54 -08001090 // FIXME: broken for 64-bit
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001091 int[] ids = new int[ein.length];
1092 for (int ct = 0; ct < ein.length; ct++ ) {
Tim Murray460a0492013-11-19 12:45:54 -08001093 ids[ct] = (int)ein[ct].getID(mRS);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001094 }
Tim Murray460a0492013-11-19 12:45:54 -08001095 long id = mRS.nElementCreate2(ids, sin, asin);
Jason Sams70d4e502010-09-02 17:35:23 -07001096 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -07001097 }
1098 }
Jason Sams36e612a2009-07-31 16:26:13 -07001099}
1100