blob: 80c70bc2a26c4aad7ca814abf09f6b554a2323ca [file] [log] [blame]
Jason Sams36e612a2009-07-31 16:26:13 -07001/*
Stephen Hines3beb60e2012-02-14 20:38:20 -08002 * Copyright (C) 2008-2012 The Android Open Source Project
Jason Sams36e612a2009-07-31 16:26:13 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
Jason Sams43ee06852009-08-12 17:54:11 -070019import java.lang.reflect.Field;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070020import android.util.Log;
Jason Sams36e612a2009-07-31 16:26:13 -070021
22/**
Robert Ly11518ac2011-02-09 13:57:06 -080023 * <p>The most basic data type. An element represents one cell of a memory allocation.
Alex Sakhartchouk34769772011-02-28 16:01:28 -080024 * Element is the basic data type of Renderscript. An element can be of two forms: Basic elements or Complex forms.
Robert Ly11518ac2011-02-09 13:57:06 -080025 * Examples of basic elements are:</p>
26 * <ul>
27 * <li>Single float value</li>
28 * <li>4 element float vector</li>
29 * <li>single RGB-565 color</li>
30 * <li>single unsigned int 16</li>
31 * </ul>
Alex Sakhartchouk34769772011-02-28 16:01:28 -080032 * <p>Complex elements contain a list of sub-elements and names that
Robert Ly11518ac2011-02-09 13:57:06 -080033 * represents a structure of data. The fields can be accessed by name
34 * from a script or shader. The memory layout is defined and ordered. Data
Stephen Hinesf257e512011-06-14 14:54:29 -070035 * alignment is determined by the most basic primitive type. i.e. a float4
36 * vector will be aligned to sizeof(float) and not sizeof(float4). The
Jason Samsa1b13ed2010-11-12 14:58:37 -080037 * ordering of elements in memory will be the order in which they were added
Robert Ly11518ac2011-02-09 13:57:06 -080038 * with each component aligned as necessary. No re-ordering will be done.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080039 *
Robert Ly11518ac2011-02-09 13:57:06 -080040 * <p>The primary source of elements are from scripts. A script that exports a
41 * bind point for a data structure generates a Renderscript element to represent the
42 * data exported by the script. The other common source of elements is from bitmap formats.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080043 *
44 * <div class="special reference">
45 * <h3>Developer Guides</h3>
46 * <p>For more information about creating an application that uses Renderscript, read the
47 * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p>
48 * </div>
Jason Sams36e612a2009-07-31 16:26:13 -070049 **/
50public class Element extends BaseObj {
Jason Samsea84a7c2009-09-04 14:42:41 -070051 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080052 Element[] mElements;
53 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -070054 int[] mArraySizes;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070055 int[] mOffsetInBytes;
Jason Sams36e612a2009-07-31 16:26:13 -070056
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -080057 int[] mVisibleElementMap;
58
Jason Sams718cd1f2009-12-23 14:35:29 -080059 DataType mType;
60 DataKind mKind;
61 boolean mNormalized;
62 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070063
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -080064 private void updateVisibleSubElements() {
65 if (mElements == null) {
66 return;
67 }
68
69 int noPaddingFieldCount = 0;
70 int fieldCount = mElementNames.length;
71 // Find out how many elements are not padding
72 for (int ct = 0; ct < fieldCount; ct ++) {
73 if (mElementNames[ct].charAt(0) != '#') {
74 noPaddingFieldCount ++;
75 }
76 }
77 mVisibleElementMap = new int[noPaddingFieldCount];
78
79 // Make a map that points us at non-padding elements
80 for (int ct = 0, ctNoPadding = 0; ct < fieldCount; ct ++) {
81 if (mElementNames[ct].charAt(0) != '#') {
82 mVisibleElementMap[ctNoPadding ++] = ct;
83 }
84 }
85 }
86
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070087 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070088 * @return element size in bytes
89 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070090 public int getBytesSize() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070091
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -080092 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -070093 * Returns the number of vector components. 2 for float2, 4 for
94 * float4, etc.
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -080095 * @return element vector size
96 */
97 public int getVectorSize() {return mVectorSize;}
98
Jason Samsa1b13ed2010-11-12 14:58:37 -080099
100 /**
101 * DataType represents the basic type information for a basic element. The
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800102 * naming convention follows. For numeric types it is FLOAT,
103 * SIGNED, or UNSIGNED followed by the _BITS where BITS is the
104 * size of the data. BOOLEAN is a true / false (1,0)
105 * represented in an 8 bit container. The UNSIGNED variants
106 * with multiple bit definitions are for packed graphical data
107 * formats and represent vectors with per vector member sizes
108 * which are treated as a single unit for packing and alignment
109 * purposes.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800110 *
111 * MATRIX the three matrix types contain FLOAT_32 elements and are treated
112 * as 32 bits for alignment purposes.
113 *
114 * RS_* objects. 32 bit opaque handles.
115 */
Jason Sams36e612a2009-07-31 16:26:13 -0700116 public enum DataType {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800117 NONE (0, 0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800118 //FLOAT_16 (1, 2),
119 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -0700120 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800121 SIGNED_8 (4, 1),
122 SIGNED_16 (5, 2),
123 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -0700124 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800125 UNSIGNED_8 (8, 1),
126 UNSIGNED_16 (9, 2),
127 UNSIGNED_32 (10, 4),
Stephen Hines52d83632010-10-11 16:10:42 -0700128 UNSIGNED_64 (11, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -0800129
Jason Samsf110d4b2010-06-21 17:42:41 -0700130 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -0800131
Jason Samsf110d4b2010-06-21 17:42:41 -0700132 UNSIGNED_5_6_5 (13, 2),
133 UNSIGNED_5_5_5_1 (14, 2),
134 UNSIGNED_4_4_4_4 (15, 2),
135
Jason Sams1d45c472010-08-25 14:31:48 -0700136 MATRIX_4X4 (16, 64),
137 MATRIX_3X3 (17, 36),
138 MATRIX_2X2 (18, 16),
139
140 RS_ELEMENT (1000, 4),
141 RS_TYPE (1001, 4),
142 RS_ALLOCATION (1002, 4),
143 RS_SAMPLER (1003, 4),
144 RS_SCRIPT (1004, 4),
145 RS_MESH (1005, 4),
146 RS_PROGRAM_FRAGMENT (1006, 4),
147 RS_PROGRAM_VERTEX (1007, 4),
148 RS_PROGRAM_RASTER (1008, 4),
149 RS_PROGRAM_STORE (1009, 4);
Jason Sams36e612a2009-07-31 16:26:13 -0700150
151 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -0800152 int mSize;
153 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -0700154 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -0800155 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700156 }
157 }
158
Jason Samsa1b13ed2010-11-12 14:58:37 -0800159 /**
160 * The special interpretation of the data if required. This is primarly
161 * useful for graphical data. USER indicates no special interpretation is
162 * expected. PIXEL is used in conjunction with the standard data types for
163 * representing texture formats.
164 */
Jason Sams36e612a2009-07-31 16:26:13 -0700165 public enum DataKind {
166 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -0800167
168 PIXEL_L (7),
169 PIXEL_A (8),
170 PIXEL_LA (9),
171 PIXEL_RGB (10),
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700172 PIXEL_RGBA (11),
173 PIXEL_DEPTH (12);
Jason Sams36e612a2009-07-31 16:26:13 -0700174
175 int mID;
176 DataKind(int id) {
177 mID = id;
178 }
179 }
180
Jason Samsa1b13ed2010-11-12 14:58:37 -0800181 /**
182 * Return if a element is too complex for use as a data source for a Mesh or
183 * a Program.
184 *
185 * @return boolean
186 */
Jason Samsc1d62102010-11-04 14:32:19 -0700187 public boolean isComplex() {
188 if (mElements == null) {
189 return false;
190 }
191 for (int ct=0; ct < mElements.length; ct++) {
192 if (mElements[ct].mElements != null) {
193 return true;
194 }
195 }
196 return false;
197 }
198
Jason Samsa1b13ed2010-11-12 14:58:37 -0800199 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700200 * Elements could be simple, such as an int or a float, or a
201 * structure with multiple sub elements, such as a collection of
202 * floats, float2, float4. This function returns zero for simple
203 * elements or the number of sub-elements otherwise.
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700204 * @return number of sub-elements in this element
205 */
206 public int getSubElementCount() {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800207 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700208 return 0;
209 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800210 return mVisibleElementMap.length;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700211 }
212
213 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700214 * For complex elements, this function will return the
215 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700216 * @param index index of the sub-element to return
217 * @return sub-element in this element at given index
218 */
219 public Element getSubElement(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800220 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700221 throw new RSIllegalArgumentException("Element contains no sub-elements");
222 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800223 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700224 throw new RSIllegalArgumentException("Illegal sub-element index");
225 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800226 return mElements[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700227 }
228
229 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700230 * For complex elements, this function will return the
231 * sub-element name at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700232 * @param index index of the sub-element
233 * @return sub-element in this element at given index
234 */
235 public String getSubElementName(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 mElementNames[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700243 }
244
245 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700246 * For complex elements, some sub-elements could be statically
247 * sized arrays. This function will return the array size for
248 * sub-element at index
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700249 * @param index index of the sub-element
250 * @return array size of sub-element in this element at given index
251 */
252 public int getSubElementArraySize(int index) {
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800253 if (mVisibleElementMap == null) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700254 throw new RSIllegalArgumentException("Element contains no sub-elements");
255 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800256 if (index < 0 || index >= mVisibleElementMap.length) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700257 throw new RSIllegalArgumentException("Illegal sub-element index");
258 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800259 return mArraySizes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700260 }
261
262 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700263 * This function specifies the location of a sub-element within
264 * the element
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700265 * @param index index of the sub-element
266 * @return offset in bytes of sub-element in this element at given index
267 */
268 public int getSubElementOffsetBytes(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 mOffsetInBytes[mVisibleElementMap[index]];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700276 }
277
278 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800279 * @return element data type
280 */
281 public DataType getDataType() {
282 return mType;
283 }
284
285 /**
Alex Sakhartchoukf5d8ac72011-12-16 09:44:26 -0800286 * @return element data kind
287 */
288 public DataKind getDataKind() {
289 return mKind;
290 }
291
292 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800293 * Utility function for returning an Element containing a single Boolean.
294 *
295 * @param rs Context to which the element will belong.
296 *
297 * @return Element
298 */
Jason Samsf110d4b2010-06-21 17:42:41 -0700299 public static Element BOOLEAN(RenderScript rs) {
300 if(rs.mElement_BOOLEAN == null) {
301 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
302 }
303 return rs.mElement_BOOLEAN;
304 }
305
Jason Samsa1b13ed2010-11-12 14:58:37 -0800306 /**
307 * Utility function for returning an Element containing a single UNSIGNED_8.
308 *
309 * @param rs Context to which the element will belong.
310 *
311 * @return Element
312 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700313 public static Element U8(RenderScript rs) {
314 if(rs.mElement_U8 == null) {
315 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800316 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700317 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800318 }
319
Jason Samsa1b13ed2010-11-12 14:58:37 -0800320 /**
321 * Utility function for returning an Element containing a single SIGNED_8.
322 *
323 * @param rs Context to which the element will belong.
324 *
325 * @return Element
326 */
Jason Sams8cb39de2010-06-01 15:47:01 -0700327 public static Element I8(RenderScript rs) {
328 if(rs.mElement_I8 == null) {
329 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800330 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700331 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800332 }
333
Jason Samse29f3e72010-06-08 15:40:48 -0700334 public static Element U16(RenderScript rs) {
335 if(rs.mElement_U16 == null) {
336 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
337 }
338 return rs.mElement_U16;
339 }
340
341 public static Element I16(RenderScript rs) {
342 if(rs.mElement_I16 == null) {
343 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
344 }
345 return rs.mElement_I16;
346 }
347
Jason Sams8cb39de2010-06-01 15:47:01 -0700348 public static Element U32(RenderScript rs) {
349 if(rs.mElement_U32 == null) {
350 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800351 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700352 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800353 }
354
Jason Sams8cb39de2010-06-01 15:47:01 -0700355 public static Element I32(RenderScript rs) {
356 if(rs.mElement_I32 == null) {
357 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800358 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700359 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800360 }
361
Stephen Hines52d83632010-10-11 16:10:42 -0700362 public static Element U64(RenderScript rs) {
363 if(rs.mElement_U64 == null) {
364 rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
365 }
366 return rs.mElement_U64;
367 }
368
Stephen Hinesef1dac22010-10-01 15:39:33 -0700369 public static Element I64(RenderScript rs) {
370 if(rs.mElement_I64 == null) {
371 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
372 }
373 return rs.mElement_I64;
374 }
375
Jason Sams8cb39de2010-06-01 15:47:01 -0700376 public static Element F32(RenderScript rs) {
377 if(rs.mElement_F32 == null) {
378 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800379 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700380 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800381 }
382
Stephen Hines02f417052010-09-30 15:19:22 -0700383 public static Element F64(RenderScript rs) {
384 if(rs.mElement_F64 == null) {
385 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
386 }
387 return rs.mElement_F64;
388 }
389
Jason Sams8cb39de2010-06-01 15:47:01 -0700390 public static Element ELEMENT(RenderScript rs) {
391 if(rs.mElement_ELEMENT == null) {
392 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700393 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700394 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700395 }
396
Jason Sams8cb39de2010-06-01 15:47:01 -0700397 public static Element TYPE(RenderScript rs) {
398 if(rs.mElement_TYPE == null) {
399 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700400 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700401 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700402 }
403
Jason Sams8cb39de2010-06-01 15:47:01 -0700404 public static Element ALLOCATION(RenderScript rs) {
405 if(rs.mElement_ALLOCATION == null) {
406 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700407 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700408 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700409 }
410
Jason Sams8cb39de2010-06-01 15:47:01 -0700411 public static Element SAMPLER(RenderScript rs) {
412 if(rs.mElement_SAMPLER == null) {
413 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700414 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700415 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700416 }
417
Jason Sams8cb39de2010-06-01 15:47:01 -0700418 public static Element SCRIPT(RenderScript rs) {
419 if(rs.mElement_SCRIPT == null) {
420 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700421 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700422 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700423 }
424
Jason Sams8cb39de2010-06-01 15:47:01 -0700425 public static Element MESH(RenderScript rs) {
426 if(rs.mElement_MESH == null) {
427 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700428 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700429 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700430 }
431
Jason Sams8cb39de2010-06-01 15:47:01 -0700432 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
433 if(rs.mElement_PROGRAM_FRAGMENT == null) {
434 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700435 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700436 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700437 }
438
Jason Sams8cb39de2010-06-01 15:47:01 -0700439 public static Element PROGRAM_VERTEX(RenderScript rs) {
440 if(rs.mElement_PROGRAM_VERTEX == null) {
441 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700442 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700443 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700444 }
445
Jason Sams8cb39de2010-06-01 15:47:01 -0700446 public static Element PROGRAM_RASTER(RenderScript rs) {
447 if(rs.mElement_PROGRAM_RASTER == null) {
448 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700449 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700450 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700451 }
452
Jason Sams8cb39de2010-06-01 15:47:01 -0700453 public static Element PROGRAM_STORE(RenderScript rs) {
454 if(rs.mElement_PROGRAM_STORE == null) {
455 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700456 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700457 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700458 }
459
460
Jason Sams718cd1f2009-12-23 14:35:29 -0800461 public static Element A_8(RenderScript rs) {
462 if(rs.mElement_A_8 == null) {
463 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
464 }
465 return rs.mElement_A_8;
466 }
467
468 public static Element RGB_565(RenderScript rs) {
469 if(rs.mElement_RGB_565 == null) {
470 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
471 }
472 return rs.mElement_RGB_565;
473 }
474
475 public static Element RGB_888(RenderScript rs) {
476 if(rs.mElement_RGB_888 == null) {
477 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
478 }
479 return rs.mElement_RGB_888;
480 }
481
482 public static Element RGBA_5551(RenderScript rs) {
483 if(rs.mElement_RGBA_5551 == null) {
484 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
485 }
486 return rs.mElement_RGBA_5551;
487 }
488
489 public static Element RGBA_4444(RenderScript rs) {
490 if(rs.mElement_RGBA_4444 == null) {
491 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
492 }
493 return rs.mElement_RGBA_4444;
494 }
495
496 public static Element RGBA_8888(RenderScript rs) {
497 if(rs.mElement_RGBA_8888 == null) {
498 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
499 }
500 return rs.mElement_RGBA_8888;
501 }
502
Jason Sams8cb39de2010-06-01 15:47:01 -0700503 public static Element F32_2(RenderScript rs) {
504 if(rs.mElement_FLOAT_2 == null) {
505 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800506 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700507 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800508 }
509
Jason Sams8cb39de2010-06-01 15:47:01 -0700510 public static Element F32_3(RenderScript rs) {
511 if(rs.mElement_FLOAT_3 == null) {
512 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800513 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700514 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800515 }
516
Jason Sams8cb39de2010-06-01 15:47:01 -0700517 public static Element F32_4(RenderScript rs) {
518 if(rs.mElement_FLOAT_4 == null) {
519 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800520 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700521 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800522 }
523
Stephen Hines836c4a52011-06-01 14:38:10 -0700524 public static Element F64_2(RenderScript rs) {
525 if(rs.mElement_DOUBLE_2 == null) {
526 rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
527 }
528 return rs.mElement_DOUBLE_2;
529 }
530
531 public static Element F64_3(RenderScript rs) {
532 if(rs.mElement_DOUBLE_3 == null) {
533 rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
534 }
535 return rs.mElement_DOUBLE_3;
536 }
537
538 public static Element F64_4(RenderScript rs) {
539 if(rs.mElement_DOUBLE_4 == null) {
540 rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
541 }
542 return rs.mElement_DOUBLE_4;
543 }
544
545 public static Element U8_2(RenderScript rs) {
546 if(rs.mElement_UCHAR_2 == null) {
547 rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
548 }
549 return rs.mElement_UCHAR_2;
550 }
551
552 public static Element U8_3(RenderScript rs) {
553 if(rs.mElement_UCHAR_3 == null) {
554 rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
555 }
556 return rs.mElement_UCHAR_3;
557 }
558
Jason Sams8cb39de2010-06-01 15:47:01 -0700559 public static Element U8_4(RenderScript rs) {
560 if(rs.mElement_UCHAR_4 == null) {
561 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800562 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700563 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800564 }
565
Stephen Hines836c4a52011-06-01 14:38:10 -0700566 public static Element I8_2(RenderScript rs) {
567 if(rs.mElement_CHAR_2 == null) {
568 rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
569 }
570 return rs.mElement_CHAR_2;
571 }
572
573 public static Element I8_3(RenderScript rs) {
574 if(rs.mElement_CHAR_3 == null) {
575 rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
576 }
577 return rs.mElement_CHAR_3;
578 }
579
580 public static Element I8_4(RenderScript rs) {
581 if(rs.mElement_CHAR_4 == null) {
582 rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
583 }
584 return rs.mElement_CHAR_4;
585 }
586
587 public static Element U16_2(RenderScript rs) {
588 if(rs.mElement_USHORT_2 == null) {
589 rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
590 }
591 return rs.mElement_USHORT_2;
592 }
593
594 public static Element U16_3(RenderScript rs) {
595 if(rs.mElement_USHORT_3 == null) {
596 rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
597 }
598 return rs.mElement_USHORT_3;
599 }
600
601 public static Element U16_4(RenderScript rs) {
602 if(rs.mElement_USHORT_4 == null) {
603 rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
604 }
605 return rs.mElement_USHORT_4;
606 }
607
608 public static Element I16_2(RenderScript rs) {
609 if(rs.mElement_SHORT_2 == null) {
610 rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
611 }
612 return rs.mElement_SHORT_2;
613 }
614
615 public static Element I16_3(RenderScript rs) {
616 if(rs.mElement_SHORT_3 == null) {
617 rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
618 }
619 return rs.mElement_SHORT_3;
620 }
621
622 public static Element I16_4(RenderScript rs) {
623 if(rs.mElement_SHORT_4 == null) {
624 rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
625 }
626 return rs.mElement_SHORT_4;
627 }
628
629 public static Element U32_2(RenderScript rs) {
630 if(rs.mElement_UINT_2 == null) {
631 rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
632 }
633 return rs.mElement_UINT_2;
634 }
635
636 public static Element U32_3(RenderScript rs) {
637 if(rs.mElement_UINT_3 == null) {
638 rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
639 }
640 return rs.mElement_UINT_3;
641 }
642
643 public static Element U32_4(RenderScript rs) {
644 if(rs.mElement_UINT_4 == null) {
645 rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
646 }
647 return rs.mElement_UINT_4;
648 }
649
650 public static Element I32_2(RenderScript rs) {
651 if(rs.mElement_INT_2 == null) {
652 rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
653 }
654 return rs.mElement_INT_2;
655 }
656
657 public static Element I32_3(RenderScript rs) {
658 if(rs.mElement_INT_3 == null) {
659 rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
660 }
661 return rs.mElement_INT_3;
662 }
663
664 public static Element I32_4(RenderScript rs) {
665 if(rs.mElement_INT_4 == null) {
666 rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
667 }
668 return rs.mElement_INT_4;
669 }
670
671 public static Element U64_2(RenderScript rs) {
672 if(rs.mElement_ULONG_2 == null) {
673 rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
674 }
675 return rs.mElement_ULONG_2;
676 }
677
678 public static Element U64_3(RenderScript rs) {
679 if(rs.mElement_ULONG_3 == null) {
680 rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
681 }
682 return rs.mElement_ULONG_3;
683 }
684
685 public static Element U64_4(RenderScript rs) {
686 if(rs.mElement_ULONG_4 == null) {
687 rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
688 }
689 return rs.mElement_ULONG_4;
690 }
691
692 public static Element I64_2(RenderScript rs) {
693 if(rs.mElement_LONG_2 == null) {
694 rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
695 }
696 return rs.mElement_LONG_2;
697 }
698
699 public static Element I64_3(RenderScript rs) {
700 if(rs.mElement_LONG_3 == null) {
701 rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
702 }
703 return rs.mElement_LONG_3;
704 }
705
706 public static Element I64_4(RenderScript rs) {
707 if(rs.mElement_LONG_4 == null) {
708 rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
709 }
710 return rs.mElement_LONG_4;
711 }
712
Jason Sams1d45c472010-08-25 14:31:48 -0700713 public static Element MATRIX_4X4(RenderScript rs) {
714 if(rs.mElement_MATRIX_4X4 == null) {
715 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
716 }
717 return rs.mElement_MATRIX_4X4;
718 }
719 public static Element MATRIX4X4(RenderScript rs) {
720 return MATRIX_4X4(rs);
721 }
722
723 public static Element MATRIX_3X3(RenderScript rs) {
724 if(rs.mElement_MATRIX_3X3 == null) {
725 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
726 }
Alex Sakhartchouk34769772011-02-28 16:01:28 -0800727 return rs.mElement_MATRIX_3X3;
Jason Sams1d45c472010-08-25 14:31:48 -0700728 }
729
730 public static Element MATRIX_2X2(RenderScript rs) {
731 if(rs.mElement_MATRIX_2X2 == null) {
732 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
733 }
734 return rs.mElement_MATRIX_2X2;
735 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800736
Jason Sams70d4e502010-09-02 17:35:23 -0700737 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700738 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700739 mSize = 0;
Alex Sakhartchoukfd79e022011-12-22 14:30:55 -0800740 mVectorSize = 1;
Jason Sams718cd1f2009-12-23 14:35:29 -0800741 mElements = e;
742 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700743 mArraySizes = as;
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800744 mType = DataType.NONE;
745 mKind = DataKind.USER;
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700746 mOffsetInBytes = new int[mElements.length];
Jason Sams718cd1f2009-12-23 14:35:29 -0800747 for (int ct = 0; ct < mElements.length; ct++ ) {
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700748 mOffsetInBytes[ct] = mSize;
Alex Sakhartchouk9e401bc2010-10-13 14:22:02 -0700749 mSize += mElements[ct].mSize * mArraySizes[ct];
Jason Sams718cd1f2009-12-23 14:35:29 -0800750 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800751 updateVisibleSubElements();
Jason Sams718cd1f2009-12-23 14:35:29 -0800752 }
753
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700754 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
755 super(id, rs);
Jason Sams252c0782011-01-11 17:42:52 -0800756 if ((dt != DataType.UNSIGNED_5_6_5) &&
757 (dt != DataType.UNSIGNED_4_4_4_4) &&
758 (dt != DataType.UNSIGNED_5_5_5_1)) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800759 if (size == 3) {
760 mSize = dt.mSize * 4;
761 } else {
762 mSize = dt.mSize * size;
763 }
Jason Sams252c0782011-01-11 17:42:52 -0800764 } else {
765 mSize = dt.mSize;
766 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800767 mType = dt;
768 mKind = dk;
769 mNormalized = norm;
770 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700771 }
772
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700773 Element(int id, RenderScript rs) {
774 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700775 }
776
777 @Override
778 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800779 super.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700780
781 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
782 int[] dataBuffer = new int[5];
Jason Samse07694b2012-04-03 15:36:36 -0700783 mRS.nElementGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700784
785 mNormalized = dataBuffer[2] == 1 ? true : false;
786 mVectorSize = dataBuffer[3];
787 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700788 for (DataType dt: DataType.values()) {
789 if(dt.mID == dataBuffer[0]){
790 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700791 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700792 }
793 }
794 for (DataKind dk: DataKind.values()) {
795 if(dk.mID == dataBuffer[1]){
796 mKind = dk;
797 }
798 }
799
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700800 int numSubElements = dataBuffer[4];
801 if(numSubElements > 0) {
802 mElements = new Element[numSubElements];
803 mElementNames = new String[numSubElements];
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700804 mArraySizes = new int[numSubElements];
805 mOffsetInBytes = new int[numSubElements];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700806
807 int[] subElementIds = new int[numSubElements];
Jason Samse07694b2012-04-03 15:36:36 -0700808 mRS.nElementGetSubElements(getID(mRS), subElementIds, mElementNames, mArraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700809 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700810 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700811 mElements[i].updateFromNative();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700812 mOffsetInBytes[i] = mSize;
813 mSize += mElements[i].mSize * mArraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700814 }
815 }
Alex Sakhartchouk3aac0ab2011-12-22 13:11:48 -0800816 updateVisibleSubElements();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700817 }
818
Jason Samsa1b13ed2010-11-12 14:58:37 -0800819 /**
820 * Create a custom Element of the specified DataType. The DataKind will be
821 * set to USER and the vector size to 1 indicating non-vector.
822 *
823 * @param rs The context associated with the new Element.
824 * @param dt The DataType for the new element.
825 * @return Element
826 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800827 static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700828 DataKind dk = DataKind.USER;
829 boolean norm = false;
830 int vecSize = 1;
831 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
832 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800833 }
834
Jason Samsa1b13ed2010-11-12 14:58:37 -0800835 /**
836 * Create a custom vector element of the specified DataType and vector size.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800837 * DataKind will be set to USER. Only primitive types (FLOAT_32, FLOAT_64,
838 * SIGNED_8, SIGNED_16, SIGNED_32, SIGNED_64, UNSIGNED_8, UNSIGNED_16,
839 * UNSIGNED_32, UNSIGNED_64, BOOLEAN) are supported.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800840 *
841 * @param rs The context associated with the new Element.
Stephen Hines3beb60e2012-02-14 20:38:20 -0800842 * @param dt The DataType for the new Element.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800843 * @param size Vector size for the new Element. Range 2-4 inclusive
844 * supported.
845 *
846 * @return Element
847 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800848 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800849 if (size < 2 || size > 4) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800850 throw new RSIllegalArgumentException("Vector size out of range 2-4.");
Jason Samsea84a7c2009-09-04 14:42:41 -0700851 }
Stephen Hines3beb60e2012-02-14 20:38:20 -0800852
853 switch (dt) {
854 // Support only primitive integer/float/boolean types as vectors.
855 case FLOAT_32:
856 case FLOAT_64:
857 case SIGNED_8:
858 case SIGNED_16:
859 case SIGNED_32:
860 case SIGNED_64:
861 case UNSIGNED_8:
862 case UNSIGNED_16:
863 case UNSIGNED_32:
864 case UNSIGNED_64:
865 case BOOLEAN: {
866 DataKind dk = DataKind.USER;
867 boolean norm = false;
868 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
869 return new Element(id, rs, dt, dk, norm, size);
870 }
871
872 default: {
873 throw new RSIllegalArgumentException("Cannot create vector of " +
874 "non-primitive type.");
875 }
876 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700877 }
878
Jason Samsa1b13ed2010-11-12 14:58:37 -0800879 /**
880 * Create a new pixel Element type. A matching DataType and DataKind must
881 * be provided. The DataType and DataKind must contain the same number of
882 * components. Vector size will be set to 1.
883 *
884 * @param rs The context associated with the new Element.
885 * @param dt The DataType for the new element.
886 * @param dk The DataKind to specify the mapping of each component in the
887 * DataType.
888 *
889 * @return Element
890 */
Jason Sams718cd1f2009-12-23 14:35:29 -0800891 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800892 if (!(dk == DataKind.PIXEL_L ||
893 dk == DataKind.PIXEL_A ||
894 dk == DataKind.PIXEL_LA ||
895 dk == DataKind.PIXEL_RGB ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700896 dk == DataKind.PIXEL_RGBA ||
897 dk == DataKind.PIXEL_DEPTH)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700898 throw new RSIllegalArgumentException("Unsupported DataKind");
Jason Sams718cd1f2009-12-23 14:35:29 -0800899 }
900 if (!(dt == DataType.UNSIGNED_8 ||
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700901 dt == DataType.UNSIGNED_16 ||
Jason Sams718cd1f2009-12-23 14:35:29 -0800902 dt == DataType.UNSIGNED_5_6_5 ||
903 dt == DataType.UNSIGNED_4_4_4_4 ||
904 dt == DataType.UNSIGNED_5_5_5_1)) {
Jason Samsc1d62102010-11-04 14:32:19 -0700905 throw new RSIllegalArgumentException("Unsupported DataType");
Jason Sams718cd1f2009-12-23 14:35:29 -0800906 }
907 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
Jason Samsc1d62102010-11-04 14:32:19 -0700908 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800909 }
910 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700911 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800912 }
913 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
Jason Samsc1d62102010-11-04 14:32:19 -0700914 throw new RSIllegalArgumentException("Bad kind and type combo");
Jason Sams718cd1f2009-12-23 14:35:29 -0800915 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700916 if (dt == DataType.UNSIGNED_16 &&
917 dk != DataKind.PIXEL_DEPTH) {
918 throw new RSIllegalArgumentException("Bad kind and type combo");
919 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800920
921 int size = 1;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700922 switch (dk) {
923 case PIXEL_LA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800924 size = 2;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700925 break;
926 case PIXEL_RGB:
Jason Sams718cd1f2009-12-23 14:35:29 -0800927 size = 3;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700928 break;
929 case PIXEL_RGBA:
Jason Sams718cd1f2009-12-23 14:35:29 -0800930 size = 4;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700931 break;
932 case PIXEL_DEPTH:
933 size = 2;
934 break;
Jason Sams718cd1f2009-12-23 14:35:29 -0800935 }
936
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700937 boolean norm = true;
938 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
939 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800940 }
Jason Sams36e612a2009-07-31 16:26:13 -0700941
Jason Samsa1b13ed2010-11-12 14:58:37 -0800942 /**
Stephen Hinesf257e512011-06-14 14:54:29 -0700943 * Check if the current Element is compatible with another Element.
944 * Primitive Elements are compatible if they share the same underlying
945 * size and type (i.e. U8 is compatible with A_8). User-defined Elements
946 * must be equal in order to be compatible. This requires strict name
947 * equivalence for all sub-Elements (in addition to structural equivalence).
948 *
949 * @param e The Element to check compatibility with.
950 *
951 * @return boolean true if the Elements are compatible, otherwise false.
952 */
953 public boolean isCompatible(Element e) {
954 // Try strict BaseObj equality to start with.
955 if (this.equals(e)) {
956 return true;
957 }
958
959 // Ignore mKind because it is allowed to be different (user vs. pixel).
960 // We also ignore mNormalized because it can be different. The mType
Stephen Hines20948112012-02-14 19:42:42 -0800961 // field must not be NONE since we require name equivalence for
962 // all user-created Elements.
Stephen Hinesf257e512011-06-14 14:54:29 -0700963 return ((mSize == e.mSize) &&
Stephen Hines20948112012-02-14 19:42:42 -0800964 (mType != DataType.NONE) &&
Stephen Hinesf257e512011-06-14 14:54:29 -0700965 (mType == e.mType) &&
966 (mVectorSize == e.mVectorSize));
967 }
968
969 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800970 * Builder class for producing complex elements with matching field and name
971 * pairs. The builder starts empty. The order in which elements are added
972 * is retained for the layout in memory.
973 *
974 */
Jason Sams36e612a2009-07-31 16:26:13 -0700975 public static class Builder {
976 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800977 Element[] mElements;
978 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700979 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800980 int mCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800981 int mSkipPadding;
Jason Sams22534172009-08-04 16:58:20 -0700982
Jason Samsa1b13ed2010-11-12 14:58:37 -0800983 /**
984 * Create a builder object.
985 *
986 * @param rs
987 */
Jason Sams22534172009-08-04 16:58:20 -0700988 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700989 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800990 mCount = 0;
991 mElements = new Element[8];
992 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -0700993 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700994 }
995
Jason Samsa1b13ed2010-11-12 14:58:37 -0800996 /**
997 * Add an array of elements to this element.
998 *
999 * @param element
1000 * @param name
1001 * @param arraySize
1002 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001003 public Builder add(Element element, String name, int arraySize) {
Jason Sams70d4e502010-09-02 17:35:23 -07001004 if (arraySize < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -07001005 throw new RSIllegalArgumentException("Array size cannot be less than 1.");
Jason Sams70d4e502010-09-02 17:35:23 -07001006 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -08001007
1008 // Skip padding fields after a vector 3 type.
1009 if (mSkipPadding != 0) {
1010 if (name.startsWith("#padding_")) {
1011 mSkipPadding = 0;
1012 return this;
1013 }
1014 }
1015
1016 if (element.mVectorSize == 3) {
1017 mSkipPadding = 1;
1018 } else {
1019 mSkipPadding = 0;
1020 }
1021
Jason Sams718cd1f2009-12-23 14:35:29 -08001022 if(mCount == mElements.length) {
1023 Element[] e = new Element[mCount + 8];
1024 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -07001025 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -08001026 System.arraycopy(mElements, 0, e, 0, mCount);
1027 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001028 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -08001029 mElements = e;
1030 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -07001031 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -07001032 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001033 mElements[mCount] = element;
1034 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -07001035 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -08001036 mCount++;
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001037 return this;
Jason Sams07ae4062009-08-27 20:23:34 -07001038 }
1039
Jason Samsa1b13ed2010-11-12 14:58:37 -08001040 /**
1041 * Add a single element to this Element.
1042 *
1043 * @param element
1044 * @param name
1045 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001046 public Builder add(Element element, String name) {
1047 return add(element, name, 1);
Jason Sams70d4e502010-09-02 17:35:23 -07001048 }
1049
Jason Samsa1b13ed2010-11-12 14:58:37 -08001050 /**
1051 * Create the element from this builder.
1052 *
1053 *
1054 * @return Element
1055 */
Jason Sams22534172009-08-04 16:58:20 -07001056 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -08001057 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -08001058 Element[] ein = new Element[mCount];
1059 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -07001060 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -08001061 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1062 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -07001063 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001064
1065 int[] ids = new int[ein.length];
1066 for (int ct = 0; ct < ein.length; ct++ ) {
Jason Samse07694b2012-04-03 15:36:36 -07001067 ids[ct] = ein[ct].getID(mRS);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -07001068 }
Jason Sams70d4e502010-09-02 17:35:23 -07001069 int id = mRS.nElementCreate2(ids, sin, asin);
1070 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -07001071 }
1072 }
Jason Sams36e612a2009-07-31 16:26:13 -07001073}
1074