blob: 287b3f127386435fbcb2fd6d17e2f6da2998b804 [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 Samsa5835a22014-11-05 15:16:26 -0800121 /**
122 * @hide
123 */
124 FLOAT_16 (1, 2),
Jason Sams718cd1f2009-12-23 14:35:29 -0800125 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -0700126 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800127 SIGNED_8 (4, 1),
128 SIGNED_16 (5, 2),
129 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -0700130 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800131 UNSIGNED_8 (8, 1),
132 UNSIGNED_16 (9, 2),
133 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -0700134 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800135
Jason Samsf110d4b2010-06-21 17:42:41 -0700136 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -0800137
Jason Samsf110d4b2010-06-21 17:42:41 -0700138 UNSIGNED_5_6_5 (13, 2),
139 UNSIGNED_5_5_5_1 (14, 2),
140 UNSIGNED_4_4_4_4 (15, 2),
141
Jason Sams1d45c472010-08-25 14:31:48 -0700142 MATRIX_4X4 (16, 64),
143 MATRIX_3X3 (17, 36),
144 MATRIX_2X2 (18, 16),
145
Jason Samsb49dfea2014-06-18 13:17:57 -0700146 RS_ELEMENT (1000),
147 RS_TYPE (1001),
148 RS_ALLOCATION (1002),
149 RS_SAMPLER (1003),
150 RS_SCRIPT (1004),
151 RS_MESH (1005),
152 RS_PROGRAM_FRAGMENT (1006),
153 RS_PROGRAM_VERTEX (1007),
154 RS_PROGRAM_RASTER (1008),
155 RS_PROGRAM_STORE (1009),
156 RS_FONT (1010);
Jason Sams36e612a2009-07-31 16:26:13 -0700157
158 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800159 int mSize;
160 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700161 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800162 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700163 }
Jason Samsb49dfea2014-06-18 13:17:57 -0700164
165 DataType(int id) {
166 mID = id;
167 mSize = 4;
168 if (RenderScript.sPointerSize == 8) {
169 mSize = 32;
170 }
171 }
Jason Sams36e612a2009-07-31 16:26:13 -0700172 }
173
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700174 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800175 * The special interpretation of the data if required. This is primarly
176 * useful for graphical data. USER indicates no special interpretation is
177 * expected. PIXEL is used in conjunction with the standard data types for
178 * representing texture formats.
179 */
Jason Sams36e612a2009-07-31 16:26:13 -0700180 public enum DataKind {
181 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800182
183 PIXEL_L (7),
184 PIXEL_A (8),
185 PIXEL_LA (9),
186 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700187 PIXEL_RGBA (11),
Jason Sams8140d7b2012-12-13 17:01:09 -0800188 PIXEL_DEPTH (12),
189 PIXEL_YUV(13);
Jason Sams36e612a2009-07-31 16:26:13 -0700190
191 int mID;
192 DataKind(int id) {
193 mID = id;
194 }
195 }
196
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700197 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800198 * Return if a element is too complex for use as a data source for a Mesh or
199 * a Program.
200 *
201 * @return boolean
202 */
Jason Samsc1d62102010-11-04 14:32:19 -0700203 public boolean isComplex() {
204 if (mElements == null) {
205 return false;
206 }
207 for (int ct=0; ct < mElements.length; ct++) {
208 if (mElements[ct].mElements != null) {
209 return true;
210 }
211 }
212 return false;
213 }
214
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700215 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700216 * Elements could be simple, such as an int or a float, or a
217 * structure with multiple sub elements, such as a collection of
218 * floats, float2, float4. This function returns zero for simple
219 * elements or the number of sub-elements otherwise.
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700220 * @return number of sub-elements in this element
221 */
222 public int getSubElementCount() {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800223 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700224 return 0;
225 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800226 return mVisibleElementMap.length;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700227 }
228
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700229 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700230 * For complex elements, this function will return the
231 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700232 * @param index index of the sub-element to return
233 * @return sub-element in this element at given index
234 */
235 public Element getSubElement(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800236 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700237 throw new RSIllegalArgumentException("Element contains no sub-elements");
238 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800239 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700240 throw new RSIllegalArgumentException("Illegal sub-element index");
241 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800242 return mElements[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700243 }
244
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700245 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700246 * For complex elements, this function will return the
247 * sub-element name at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700248 * @param index index of the sub-element
249 * @return sub-element in this element at given index
250 */
251 public String getSubElementName(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800252 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700253 throw new RSIllegalArgumentException("Element contains no sub-elements");
254 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800255 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700256 throw new RSIllegalArgumentException("Illegal sub-element index");
257 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800258 return mElementNames[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700259 }
260
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700261 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700262 * For complex elements, some sub-elements could be statically
263 * sized arrays. This function will return the array size for
264 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700265 * @param index index of the sub-element
266 * @return array size of sub-element in this element at given index
267 */
268 public int getSubElementArraySize(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800269 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700270 throw new RSIllegalArgumentException("Element contains no sub-elements");
271 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800272 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700273 throw new RSIllegalArgumentException("Illegal sub-element index");
274 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800275 return mArraySizes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700276 }
277
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700278 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700279 * This function specifies the location of a sub-element within
280 * the element
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700281 * @param index index of the sub-element
282 * @return offset in bytes of sub-element in this element at given index
283 */
284 public int getSubElementOffsetBytes(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800285 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700286 throw new RSIllegalArgumentException("Element contains no sub-elements");
287 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800288 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700289 throw new RSIllegalArgumentException("Illegal sub-element index");
290 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800291 return mOffsetInBytes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700292 }
293
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700294 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800295 * @return element data type
296 */
297 public DataType getDataType() {
298 return mType;
299 }
300
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700301 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800302 * @return element data kind
303 */
304 public DataKind getDataKind() {
305 return mKind;
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 Boolean.
310 *
311 * @param rs Context to which the element will belong.
312 *
313 * @return Element
314 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700315 public static Element BOOLEAN(RenderScript rs) {
316 if(rs.mElement_BOOLEAN == null) {
317 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
318 }
319 return rs.mElement_BOOLEAN;
320 }
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 UNSIGNED_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 U8(RenderScript rs) {
330 if(rs.mElement_U8 == null) {
331 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800332 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700333 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800334 }
335
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700336 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800337 * Utility function for returning an Element containing a single SIGNED_8.
338 *
339 * @param rs Context to which the element will belong.
340 *
341 * @return Element
342 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700343 public static Element I8(RenderScript rs) {
344 if(rs.mElement_I8 == null) {
345 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800346 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700347 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800348 }
349
Jason Samse29f3e72010-06-08 15:40:48 -0700350 public static Element U16(RenderScript rs) {
351 if(rs.mElement_U16 == null) {
352 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
353 }
354 return rs.mElement_U16;
355 }
356
357 public static Element I16(RenderScript rs) {
358 if(rs.mElement_I16 == null) {
359 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
360 }
361 return rs.mElement_I16;
362 }
363
Jason Sams8cb39de2010-06-01 15:47:01 -0700364 public static Element U32(RenderScript rs) {
365 if(rs.mElement_U32 == null) {
366 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800367 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700368 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800369 }
370
Jason Sams8cb39de2010-06-01 15:47:01 -0700371 public static Element I32(RenderScript rs) {
372 if(rs.mElement_I32 == null) {
373 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800374 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700375 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800376 }
377
Stephen Hines52d83632010-10-11 16:10:42 -0700378 public static Element U64(RenderScript rs) {
379 if(rs.mElement_U64 == null) {
380 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
381 }
382 return rs.mElement_U64;
383 }
384
Stephen Hinesef1dac22010-10-01 15:39:33 -0700385 public static Element I64(RenderScript rs) {
386 if(rs.mElement_I64 == null) {
387 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
388 }
389 return rs.mElement_I64;
390 }
391
Jason Samsa5835a22014-11-05 15:16:26 -0800392 /**
393 * @hide
394 */
395 public static Element F16(RenderScript rs) {
396 if(rs.mElement_F16 == null) {
397 rs.mElement_F16 = createUser(rs, DataType.FLOAT_16);
398 }
399 return rs.mElement_F16;
400 }
401
Jason Sams8cb39de2010-06-01 15:47:01 -0700402 public static Element F32(RenderScript rs) {
403 if(rs.mElement_F32 == null) {
404 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800405 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700406 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800407 }
408
Stephen Hines02f417052010-09-30 15:19:22 -0700409 public static Element F64(RenderScript rs) {
410 if(rs.mElement_F64 == null) {
411 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
412 }
413 return rs.mElement_F64;
414 }
415
Jason Sams8cb39de2010-06-01 15:47:01 -0700416 public static Element ELEMENT(RenderScript rs) {
417 if(rs.mElement_ELEMENT == null) {
418 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700419 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700420 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700421 }
422
Jason Sams8cb39de2010-06-01 15:47:01 -0700423 public static Element TYPE(RenderScript rs) {
424 if(rs.mElement_TYPE == null) {
425 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700426 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700427 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700428 }
429
Jason Sams8cb39de2010-06-01 15:47:01 -0700430 public static Element ALLOCATION(RenderScript rs) {
431 if(rs.mElement_ALLOCATION == null) {
432 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700433 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700434 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700435 }
436
Jason Sams8cb39de2010-06-01 15:47:01 -0700437 public static Element SAMPLER(RenderScript rs) {
438 if(rs.mElement_SAMPLER == null) {
439 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700440 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700441 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700442 }
443
Jason Sams8cb39de2010-06-01 15:47:01 -0700444 public static Element SCRIPT(RenderScript rs) {
445 if(rs.mElement_SCRIPT == null) {
446 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700447 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700448 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700449 }
450
Jason Sams8cb39de2010-06-01 15:47:01 -0700451 public static Element MESH(RenderScript rs) {
452 if(rs.mElement_MESH == null) {
453 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700454 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700455 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700456 }
457
Jason Sams8cb39de2010-06-01 15:47:01 -0700458 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
459 if(rs.mElement_PROGRAM_FRAGMENT == null) {
460 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700461 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700462 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700463 }
464
Jason Sams8cb39de2010-06-01 15:47:01 -0700465 public static Element PROGRAM_VERTEX(RenderScript rs) {
466 if(rs.mElement_PROGRAM_VERTEX == null) {
467 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700468 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700469 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700470 }
471
Jason Sams8cb39de2010-06-01 15:47:01 -0700472 public static Element PROGRAM_RASTER(RenderScript rs) {
473 if(rs.mElement_PROGRAM_RASTER == null) {
474 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700475 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700476 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700477 }
478
Jason Sams8cb39de2010-06-01 15:47:01 -0700479 public static Element PROGRAM_STORE(RenderScript rs) {
480 if(rs.mElement_PROGRAM_STORE == null) {
481 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700482 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700483 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700484 }
485
Stephen Hines3a291412012-04-11 17:27:29 -0700486 public static Element FONT(RenderScript rs) {
487 if(rs.mElement_FONT == null) {
488 rs.mElement_FONT = createUser(rs, DataType.RS_FONT);
489 }
490 return rs.mElement_FONT;
491 }
492
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700493
Jason Sams718cd1f2009-12-23 14:35:29 -0800494 public static Element A_8(RenderScript rs) {
495 if(rs.mElement_A_8 == null) {
496 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
497 }
498 return rs.mElement_A_8;
499 }
500
501 public static Element RGB_565(RenderScript rs) {
502 if(rs.mElement_RGB_565 == null) {
503 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
504 }
505 return rs.mElement_RGB_565;
506 }
507
508 public static Element RGB_888(RenderScript rs) {
509 if(rs.mElement_RGB_888 == null) {
510 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
511 }
512 return rs.mElement_RGB_888;
513 }
514
515 public static Element RGBA_5551(RenderScript rs) {
516 if(rs.mElement_RGBA_5551 == null) {
517 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
518 }
519 return rs.mElement_RGBA_5551;
520 }
521
522 public static Element RGBA_4444(RenderScript rs) {
523 if(rs.mElement_RGBA_4444 == null) {
524 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
525 }
526 return rs.mElement_RGBA_4444;
527 }
528
529 public static Element RGBA_8888(RenderScript rs) {
530 if(rs.mElement_RGBA_8888 == null) {
531 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
532 }
533 return rs.mElement_RGBA_8888;
534 }
535
Jason Samsa5835a22014-11-05 15:16:26 -0800536 /**
537 * @hide
538 */
539 public static Element F16_2(RenderScript rs) {
540 if(rs.mElement_HALF_2 == null) {
541 rs.mElement_HALF_2 = createVector(rs, DataType.FLOAT_16, 2);
542 }
543 return rs.mElement_HALF_2;
544 }
545
546 /**
547 * @hide
548 */
549 public static Element F16_3(RenderScript rs) {
550 if(rs.mElement_FLOAT_3 == null) {
551 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_16, 3);
552 }
553 return rs.mElement_HALF_3;
554 }
555
556 /**
557 * @hide
558 */
559 public static Element F16_4(RenderScript rs) {
560 if(rs.mElement_HALF_4 == null) {
561 rs.mElement_HALF_4 = createVector(rs, DataType.FLOAT_16, 4);
562 }
563 return rs.mElement_HALF_4;
564 }
565
Jason Sams8cb39de2010-06-01 15:47:01 -0700566 public static Element F32_2(RenderScript rs) {
567 if(rs.mElement_FLOAT_2 == null) {
568 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800569 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700570 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800571 }
572
Jason Sams8cb39de2010-06-01 15:47:01 -0700573 public static Element F32_3(RenderScript rs) {
574 if(rs.mElement_FLOAT_3 == null) {
575 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800576 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700577 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800578 }
579
Jason Sams8cb39de2010-06-01 15:47:01 -0700580 public static Element F32_4(RenderScript rs) {
581 if(rs.mElement_FLOAT_4 == null) {
582 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800583 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700584 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800585 }
586
Stephen Hines836c4a52011-06-01 14:38:10 -0700587 public static Element F64_2(RenderScript rs) {
588 if(rs.mElement_DOUBLE_2 == null) {
589 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
590 }
591 return rs.mElement_DOUBLE_2;
592 }
593
594 public static Element F64_3(RenderScript rs) {
595 if(rs.mElement_DOUBLE_3 == null) {
596 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
597 }
598 return rs.mElement_DOUBLE_3;
599 }
600
601 public static Element F64_4(RenderScript rs) {
602 if(rs.mElement_DOUBLE_4 == null) {
603 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
604 }
605 return rs.mElement_DOUBLE_4;
606 }
607
608 public static Element U8_2(RenderScript rs) {
609 if(rs.mElement_UCHAR_2 == null) {
610 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
611 }
612 return rs.mElement_UCHAR_2;
613 }
614
615 public static Element U8_3(RenderScript rs) {
616 if(rs.mElement_UCHAR_3 == null) {
617 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
618 }
619 return rs.mElement_UCHAR_3;
620 }
621
Jason Sams8cb39de2010-06-01 15:47:01 -0700622 public static Element U8_4(RenderScript rs) {
623 if(rs.mElement_UCHAR_4 == null) {
624 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800625 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700626 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800627 }
628
Stephen Hines836c4a52011-06-01 14:38:10 -0700629 public static Element I8_2(RenderScript rs) {
630 if(rs.mElement_CHAR_2 == null) {
631 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
632 }
633 return rs.mElement_CHAR_2;
634 }
635
636 public static Element I8_3(RenderScript rs) {
637 if(rs.mElement_CHAR_3 == null) {
638 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
639 }
640 return rs.mElement_CHAR_3;
641 }
642
643 public static Element I8_4(RenderScript rs) {
644 if(rs.mElement_CHAR_4 == null) {
645 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
646 }
647 return rs.mElement_CHAR_4;
648 }
649
650 public static Element U16_2(RenderScript rs) {
651 if(rs.mElement_USHORT_2 == null) {
652 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
653 }
654 return rs.mElement_USHORT_2;
655 }
656
657 public static Element U16_3(RenderScript rs) {
658 if(rs.mElement_USHORT_3 == null) {
659 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
660 }
661 return rs.mElement_USHORT_3;
662 }
663
664 public static Element U16_4(RenderScript rs) {
665 if(rs.mElement_USHORT_4 == null) {
666 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
667 }
668 return rs.mElement_USHORT_4;
669 }
670
671 public static Element I16_2(RenderScript rs) {
672 if(rs.mElement_SHORT_2 == null) {
673 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
674 }
675 return rs.mElement_SHORT_2;
676 }
677
678 public static Element I16_3(RenderScript rs) {
679 if(rs.mElement_SHORT_3 == null) {
680 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
681 }
682 return rs.mElement_SHORT_3;
683 }
684
685 public static Element I16_4(RenderScript rs) {
686 if(rs.mElement_SHORT_4 == null) {
687 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
688 }
689 return rs.mElement_SHORT_4;
690 }
691
692 public static Element U32_2(RenderScript rs) {
693 if(rs.mElement_UINT_2 == null) {
694 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
695 }
696 return rs.mElement_UINT_2;
697 }
698
699 public static Element U32_3(RenderScript rs) {
700 if(rs.mElement_UINT_3 == null) {
701 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
702 }
703 return rs.mElement_UINT_3;
704 }
705
706 public static Element U32_4(RenderScript rs) {
707 if(rs.mElement_UINT_4 == null) {
708 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
709 }
710 return rs.mElement_UINT_4;
711 }
712
713 public static Element I32_2(RenderScript rs) {
714 if(rs.mElement_INT_2 == null) {
715 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
716 }
717 return rs.mElement_INT_2;
718 }
719
720 public static Element I32_3(RenderScript rs) {
721 if(rs.mElement_INT_3 == null) {
722 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
723 }
724 return rs.mElement_INT_3;
725 }
726
727 public static Element I32_4(RenderScript rs) {
728 if(rs.mElement_INT_4 == null) {
729 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
730 }
731 return rs.mElement_INT_4;
732 }
733
734 public static Element U64_2(RenderScript rs) {
735 if(rs.mElement_ULONG_2 == null) {
736 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
737 }
738 return rs.mElement_ULONG_2;
739 }
740
741 public static Element U64_3(RenderScript rs) {
742 if(rs.mElement_ULONG_3 == null) {
743 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
744 }
745 return rs.mElement_ULONG_3;
746 }
747
748 public static Element U64_4(RenderScript rs) {
749 if(rs.mElement_ULONG_4 == null) {
750 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
751 }
752 return rs.mElement_ULONG_4;
753 }
754
755 public static Element I64_2(RenderScript rs) {
756 if(rs.mElement_LONG_2 == null) {
757 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
758 }
759 return rs.mElement_LONG_2;
760 }
761
762 public static Element I64_3(RenderScript rs) {
763 if(rs.mElement_LONG_3 == null) {
764 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
765 }
766 return rs.mElement_LONG_3;
767 }
768
769 public static Element I64_4(RenderScript rs) {
770 if(rs.mElement_LONG_4 == null) {
771 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
772 }
773 return rs.mElement_LONG_4;
774 }
775
Tim Murray932e78e2013-09-03 11:42:26 -0700776 public static Element YUV(RenderScript rs) {
777 if (rs.mElement_YUV == null) {
778 rs.mElement_YUV = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_YUV);
779 }
780 return rs.mElement_YUV;
781 }
782
Jason Sams1d45c472010-08-25 14:31:48 -0700783 public static Element MATRIX_4X4(RenderScript rs) {
784 if(rs.mElement_MATRIX_4X4 == null) {
785 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
786 }
787 return rs.mElement_MATRIX_4X4;
788 }
Jason Sams65c80f82012-05-08 17:30:26 -0700789
790 /** @deprecated use MATRIX_4X4
791 */
Jason Sams1d45c472010-08-25 14:31:48 -0700792 public static Element MATRIX4X4(RenderScript rs) {
793 return MATRIX_4X4(rs);
794 }
795
796 public static Element MATRIX_3X3(RenderScript rs) {
797 if(rs.mElement_MATRIX_3X3 == null) {
798 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
799 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800800 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700801 }
802
803 public static Element MATRIX_2X2(RenderScript rs) {
804 if(rs.mElement_MATRIX_2X2 == null) {
805 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
806 }
807 return rs.mElement_MATRIX_2X2;
808 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800809
Tim Murray460a0492013-11-19 12:45:54 -0800810 Element(long id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700811 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700812 mSize = 0;
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -0800813 mVectorSize = 1;
Jason Sams718cd1f2009-12-23 14:35:29 -0800814 mElements = e;
815 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700816 mArraySizes = as;
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800817 mType = DataType.NONE;
818 mKind = DataKind.USER;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700819 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800820 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700821 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700822 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800823 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800824 updateVisibleSubElements();
Jason Sams718cd1f2009-12-23 14:35:29 -0800825 }
826
Tim Murray460a0492013-11-19 12:45:54 -0800827 Element(long id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700828 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800829 if ((dt != DataType.UNSIGNED_5_6_5) &&
830 (dt != DataType.UNSIGNED_4_4_4_4) &&
831 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800832 if (size == 3) {
833 mSize = dt.mSize * 4;
834 } else {
835 mSize = dt.mSize * size;
836 }
Jason Sams252c0782011-01-11 17:42:52 -0800837 } else {
838 mSize = dt.mSize;
839 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800840 mType = dt;
841 mKind = dk;
842 mNormalized = norm;
843 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700844 }
845
Tim Murray460a0492013-11-19 12:45:54 -0800846 Element(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700847 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700848 }
849
850 @Override
851 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800852 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700853
854 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
855 int[] dataBuffer = new int[5];
Jason Samse07694b2012-04-03 15:36:36 -0700856 mRS.nElementGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700857
858 mNormalized = dataBuffer[2] == 1 ? true : false;
859 mVectorSize = dataBuffer[3];
860 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700861 for (DataType dt: DataType.values()) {
862 if(dt.mID == dataBuffer[0]){
863 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700864 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700865 }
866 }
867 for (DataKind dk: DataKind.values()) {
868 if(dk.mID == dataBuffer[1]){
869 mKind = dk;
870 }
871 }
872
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700873 int numSubElements = dataBuffer[4];
874 if(numSubElements > 0) {
875 mElements = new Element[numSubElements];
876 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700877 mArraySizes = new int[numSubElements];
878 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700879
Ashok Bhat98071552014-02-12 09:54:43 +0000880 long[] subElementIds = new long[numSubElements];
Jason Samse07694b2012-04-03 15:36:36 -0700881 mRS.nElementGetSubElements(getID(mRS), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700882 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700883 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700884 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700885 mOffsetInBytes[i] = mSize;
886 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700887 }
888 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800889 updateVisibleSubElements();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700890 }
891
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700892 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800893 * Create a custom Element of the specified DataType. The DataKind will be
894 * set to USER and the vector size to 1 indicating non-vector.
895 *
896 * @param rs The context associated with the new Element.
897 * @param dt The DataType for the new element.
898 * @return Element
899 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800900 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700901 DataKind dk = DataKind.USER;
902 boolean norm = false;
903 int vecSize = 1;
Tim Murray460a0492013-11-19 12:45:54 -0800904 long id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700905 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800906 }
907
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700908 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800909 * Create a custom vector element of the specified DataType and vector size.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800910 * DataKind will be set to USER. Only primitive types (FLOAT_32, FLOAT_64,
911 * SIGNED_8, SIGNED_16, SIGNED_32, SIGNED_64, UNSIGNED_8, UNSIGNED_16,
912 * UNSIGNED_32, UNSIGNED_64, BOOLEAN) are supported.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800913 *
914 * @param rs The context associated with the new Element.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800915 * @param dt The DataType for the new Element.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800916 * @param size Vector size for the new Element. Range 2-4 inclusive
917 * supported.
918 *
919 * @return Element
920 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800921 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800922 if (size < 2 || size > 4) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800923 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700924 }
Stephen Hines3beb60e2012-02-14 20:38:20 -0800925
926 switch (dt) {
927 // Support only primitive integer/float/boolean types as vectors.
928 case FLOAT_32:
929 case FLOAT_64:
930 case SIGNED_8:
931 case SIGNED_16:
932 case SIGNED_32:
933 case SIGNED_64:
934 case UNSIGNED_8:
935 case UNSIGNED_16:
936 case UNSIGNED_32:
937 case UNSIGNED_64:
938 case BOOLEAN: {
939 DataKind dk = DataKind.USER;
940 boolean norm = false;
Tim Murray460a0492013-11-19 12:45:54 -0800941 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Stephen Hines3beb60e2012-02-14 20:38:20 -0800942 return new Element(id, rs, dt, dk, norm, size);
943 }
944
945 default: {
946 throw new RSIllegalArgumentException("Cannot create vector of " +
947 "non-primitive type.");
948 }
949 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700950 }
951
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700952 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800953 * Create a new pixel Element type. A matching DataType and DataKind must
954 * be provided. The DataType and DataKind must contain the same number of
955 * components. Vector size will be set to 1.
956 *
957 * @param rs The context associated with the new Element.
958 * @param dt The DataType for the new element.
959 * @param dk The DataKind to specify the mapping of each component in the
960 * DataType.
961 *
962 * @return Element
963 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800964 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800965 if (!(dk == DataKind.PIXEL_L ||
966 dk == DataKind.PIXEL_A ||
967 dk == DataKind.PIXEL_LA ||
968 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700969 dk == DataKind.PIXEL_RGBA ||
Jason Samsdd6c8b32013-02-15 17:27:24 -0800970 dk == DataKind.PIXEL_DEPTH ||
971 dk == DataKind.PIXEL_YUV)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700972 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800973 }
974 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700975 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800976 dt == DataType.UNSIGNED_5_6_5 ||
977 dt == DataType.UNSIGNED_4_4_4_4 ||
978 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700979 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800980 }
981 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700982 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800983 }
984 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700985 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800986 }
987 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700988 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800989 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700990 if (dt == DataType.UNSIGNED_16 &&
991 dk != DataKind.PIXEL_DEPTH) {
992 throw new RSIllegalArgumentException("Bad kind and type combo");
993 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800994
995 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700996 switch (dk) {
997 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800998 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700999 break;
1000 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -08001001 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -07001002 break;
1003 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -08001004 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -07001005 break;
1006 case PIXEL_DEPTH:
1007 size = 2;
1008 break;
Jason Sams718cd1f2009-12-23 14:35:29 -08001009 }
1010
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001011 boolean norm = true;
Tim Murray460a0492013-11-19 12:45:54 -08001012 long id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001013 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -08001014 }
Jason Sams36e612a2009-07-31 16:26:13 -07001015
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001016 /**
Stephen Hinesf257e512011-06-14 14:54:29 -07001017 * Check if the current Element is compatible with another Element.
1018 * Primitive Elements are compatible if they share the same underlying
1019 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
1020 * must be equal in order to be compatible. This requires strict name
1021 * equivalence for all sub-Elements (in addition to structural equivalence).
1022 *
1023 * @param e The Element to check compatibility with.
1024 *
1025 * @return boolean true if the Elements are compatible, otherwise false.
1026 */
1027 public boolean isCompatible(Element e) {
1028 // Try strict BaseObj equality to start with.
1029 if (this.equals(e)) {
1030 return true;
1031 }
1032
1033 // Ignore mKind because it is allowed to be different (user vs. pixel).
1034 // We also ignore mNormalized because it can be different. The mType
Stephen Hines20948112012-02-14 19:42:42 -08001035 // field must not be NONE since we require name equivalence for
1036 // all user-created Elements.
Stephen Hinesf257e512011-06-14 14:54:29 -07001037 return ((mSize == e.mSize) &&
Stephen Hines20948112012-02-14 19:42:42 -08001038 (mType != DataType.NONE) &&
Stephen Hinesf257e512011-06-14 14:54:29 -07001039 (mType == e.mType) &&
1040 (mVectorSize == e.mVectorSize));
1041 }
1042
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001043 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001044 * Builder class for producing complex elements with matching field and name
1045 * pairs. The builder starts empty. The order in which elements are added
1046 * is retained for the layout in memory.
1047 *
1048 */
Jason Sams36e612a2009-07-31 16:26:13 -07001049 public static class Builder {
1050 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -08001051 Element[] mElements;
1052 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -07001053 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -08001054 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001055 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -07001056
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001057 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001058 * Create a builder object.
1059 *
1060 * @param rs
1061 */
Jason Sams22534172009-08-04 16:58:20 -07001062 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -07001063 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -08001064 mCount = 0;
1065 mElements = new Element[8];
1066 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -07001067 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -07001068 }
1069
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001070 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001071 * Add an array of elements to this element.
1072 *
1073 * @param element
1074 * @param name
1075 * @param arraySize
1076 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001077 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -07001078 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -07001079 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -07001080 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001081
1082 // Skip padding fields after a vector 3 type.
1083 if (mSkipPadding != 0) {
1084 if (name.startsWith("#padding_")) {
1085 mSkipPadding = 0;
1086 return this;
1087 }
1088 }
1089
1090 if (element.mVectorSize == 3) {
1091 mSkipPadding = 1;
1092 } else {
1093 mSkipPadding = 0;
1094 }
1095
Jason Sams718cd1f2009-12-23 14:35:29 -08001096 if(mCount == mElements.length) {
1097 Element[] e = new Element[mCount + 8];
1098 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -07001099 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -08001100 System.arraycopy(mElements, 0, e, 0, mCount);
1101 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001102 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -08001103 mElements = e;
1104 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -07001105 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -07001106 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001107 mElements[mCount] = element;
1108 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -07001109 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -08001110 mCount++;
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001111 return this;
Jason Sams07ae4062009-08-27 20:23:34 -07001112 }
1113
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001114 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001115 * Add a single element to this Element.
1116 *
1117 * @param element
1118 * @param name
1119 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001120 public Builder add(Element element, String name) {
1121 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -07001122 }
1123
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001124 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -08001125 * Create the element from this builder.
1126 *
1127 *
1128 * @return Element
1129 */
Jason Sams22534172009-08-04 16:58:20 -07001130 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -08001131 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -08001132 Element[] ein = new Element[mCount];
1133 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -07001134 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -08001135 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1136 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001137 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001138
Ashok Bhat98071552014-02-12 09:54:43 +00001139 long[] ids = new long[ein.length];
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001140 for (int ct = 0; ct < ein.length; ct++ ) {
Ashok Bhat98071552014-02-12 09:54:43 +00001141 ids[ct] = ein[ct].getID(mRS);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001142 }
Tim Murray460a0492013-11-19 12:45:54 -08001143 long id = mRS.nElementCreate2(ids, sin, asin);
Jason Sams70d4e502010-09-02 17:35:23 -07001144 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -07001145 }
1146 }
Jason Sams36e612a2009-07-31 16:26:13 -07001147}
1148