| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 1 | /* |
| Jason Sams | dd6c8b3 | 2013-02-15 17:27:24 -0800 | [diff] [blame] | 2 | * Copyright (C) 2013 The Android Open Source Project |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package android.renderscript; |
| 18 | |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 19 | import java.lang.reflect.Field; |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 20 | import android.util.Log; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 21 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 22 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 23 | * <p>An Element represents one item within an {@link |
| 24 | * android.renderscript.Allocation}. An Element is roughly equivalent to a C |
| 25 | * type in a RenderScript kernel. Elements may be basic or complex. Some basic |
| 26 | * elements are</p> <ul> <li>A single float value (equivalent to a float in a |
| 27 | * kernel)</li> <li>A four-element float vector (equivalent to a float4 in a |
| 28 | * kernel)</li> <li>An unsigned 32-bit integer (equivalent to an unsigned int in |
| 29 | * a kernel)</li> <li>A single signed 8-bit integer (equivalent to a char in a |
| 30 | * kernel)</li> </ul> <p>A complex element is roughly equivalent to a C struct |
| 31 | * and contains a number of basic or complex Elements. From Java code, a complex |
| 32 | * element contains a list of sub-elements and names that represents a |
| 33 | * particular data structure. Structs used in RS scripts are available to Java |
| 34 | * code by using the {@code ScriptField_structname} class that is reflected from |
| 35 | * a particular script.</p> |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 36 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 37 | * <p>Basic Elements are comprised of a {@link |
| 38 | * android.renderscript.Element.DataType} and a {@link |
| 39 | * android.renderscript.Element.DataKind}. The DataType encodes C type |
| 40 | * information of an Element, while the DataKind encodes how that Element should |
| 41 | * be interpreted by a {@link android.renderscript.Sampler}. Note that {@link |
| 42 | * android.renderscript.Allocation} objects with DataKind {@link |
| 43 | * android.renderscript.Element.DataKind#USER} cannot be used as input for a |
| 44 | * {@link android.renderscript.Sampler}. In general, {@link |
| 45 | * android.renderscript.Allocation} objects that are intended for use with a |
| 46 | * {@link android.renderscript.Sampler} should use bitmap-derived Elements such |
| 47 | * as {@link android.renderscript.Element#RGBA_8888} or {@link |
| 48 | * android.renderscript#Element.A_8}.</p> |
| Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 49 | * |
| 50 | * <div class="special reference"> |
| 51 | * <h3>Developer Guides</h3> |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 52 | * <p>For more information about creating an application that uses RenderScript, read the |
| 53 | * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p> |
| Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 54 | * </div> |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 55 | **/ |
| 56 | public class Element extends BaseObj { |
| Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 57 | int mSize; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 58 | Element[] mElements; |
| 59 | String[] mElementNames; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 60 | int[] mArraySizes; |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 61 | int[] mOffsetInBytes; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 62 | |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 63 | int[] mVisibleElementMap; |
| 64 | |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 65 | DataType mType; |
| 66 | DataKind mKind; |
| 67 | boolean mNormalized; |
| 68 | int mVectorSize; |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 69 | |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 70 | private void updateVisibleSubElements() { |
| 71 | if (mElements == null) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | int noPaddingFieldCount = 0; |
| 76 | int fieldCount = mElementNames.length; |
| 77 | // Find out how many elements are not padding |
| 78 | for (int ct = 0; ct < fieldCount; ct ++) { |
| 79 | if (mElementNames[ct].charAt(0) != '#') { |
| 80 | noPaddingFieldCount ++; |
| 81 | } |
| 82 | } |
| 83 | mVisibleElementMap = new int[noPaddingFieldCount]; |
| 84 | |
| 85 | // Make a map that points us at non-padding elements |
| 86 | for (int ct = 0, ctNoPadding = 0; ct < fieldCount; ct ++) { |
| 87 | if (mElementNames[ct].charAt(0) != '#') { |
| 88 | mVisibleElementMap[ctNoPadding ++] = ct; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 93 | /** |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 94 | * @return element size in bytes |
| 95 | */ |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 96 | public int getBytesSize() {return mSize;} |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 97 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 98 | /** |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 99 | * Returns the number of vector components. 2 for float2, 4 for |
| 100 | * float4, etc. |
| Alex Sakhartchouk | fd79e02 | 2011-12-22 14:30:55 -0800 | [diff] [blame] | 101 | * @return element vector size |
| 102 | */ |
| 103 | public int getVectorSize() {return mVectorSize;} |
| 104 | |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 105 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 106 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 107 | * DataType represents the basic type information for a basic element. The |
| Alex Sakhartchouk | f5d8ac7 | 2011-12-16 09:44:26 -0800 | [diff] [blame] | 108 | * naming convention follows. For numeric types it is FLOAT, |
| 109 | * SIGNED, or UNSIGNED followed by the _BITS where BITS is the |
| 110 | * size of the data. BOOLEAN is a true / false (1,0) |
| 111 | * represented in an 8 bit container. The UNSIGNED variants |
| 112 | * with multiple bit definitions are for packed graphical data |
| 113 | * formats and represent vectors with per vector member sizes |
| 114 | * which are treated as a single unit for packing and alignment |
| 115 | * purposes. |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 116 | * |
| 117 | * MATRIX the three matrix types contain FLOAT_32 elements and are treated |
| 118 | * as 32 bits for alignment purposes. |
| 119 | * |
| 120 | * RS_* objects. 32 bit opaque handles. |
| 121 | */ |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 122 | public enum DataType { |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 123 | NONE (0, 0), |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 124 | //FLOAT_16 (1, 2), |
| 125 | FLOAT_32 (2, 4), |
| Stephen Hines | 02f41705 | 2010-09-30 15:19:22 -0700 | [diff] [blame] | 126 | FLOAT_64 (3, 8), |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 127 | SIGNED_8 (4, 1), |
| 128 | SIGNED_16 (5, 2), |
| 129 | SIGNED_32 (6, 4), |
| Stephen Hines | ef1dac2 | 2010-10-01 15:39:33 -0700 | [diff] [blame] | 130 | SIGNED_64 (7, 8), |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 131 | UNSIGNED_8 (8, 1), |
| 132 | UNSIGNED_16 (9, 2), |
| 133 | UNSIGNED_32 (10, 4), |
| Stephen Hines | 52d8363 | 2010-10-11 16:10:42 -0700 | [diff] [blame] | 134 | UNSIGNED_64 (11, 8), |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 135 | |
| Jason Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 136 | BOOLEAN(12, 1), |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 137 | |
| Jason Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 138 | UNSIGNED_5_6_5 (13, 2), |
| 139 | UNSIGNED_5_5_5_1 (14, 2), |
| 140 | UNSIGNED_4_4_4_4 (15, 2), |
| 141 | |
| Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 142 | MATRIX_4X4 (16, 64), |
| 143 | MATRIX_3X3 (17, 36), |
| 144 | MATRIX_2X2 (18, 16), |
| 145 | |
| Jason Sams | b49dfea | 2014-06-18 13:17:57 -0700 | [diff] [blame^] | 146 | 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 Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 157 | |
| 158 | int mID; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 159 | int mSize; |
| 160 | DataType(int id, int size) { |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 161 | mID = id; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 162 | mSize = size; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 163 | } |
| Jason Sams | b49dfea | 2014-06-18 13:17:57 -0700 | [diff] [blame^] | 164 | |
| 165 | DataType(int id) { |
| 166 | mID = id; |
| 167 | mSize = 4; |
| 168 | if (RenderScript.sPointerSize == 8) { |
| 169 | mSize = 32; |
| 170 | } |
| 171 | } |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 174 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 175 | * 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 Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 180 | public enum DataKind { |
| 181 | USER (0), |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 182 | |
| 183 | PIXEL_L (7), |
| 184 | PIXEL_A (8), |
| 185 | PIXEL_LA (9), |
| 186 | PIXEL_RGB (10), |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 187 | PIXEL_RGBA (11), |
| Jason Sams | 8140d7b | 2012-12-13 17:01:09 -0800 | [diff] [blame] | 188 | PIXEL_DEPTH (12), |
| 189 | PIXEL_YUV(13); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 190 | |
| 191 | int mID; |
| 192 | DataKind(int id) { |
| 193 | mID = id; |
| 194 | } |
| 195 | } |
| 196 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 197 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 198 | * 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 Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 203 | 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 Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 215 | /** |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 216 | * 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 Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 220 | * @return number of sub-elements in this element |
| 221 | */ |
| 222 | public int getSubElementCount() { |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 223 | if (mVisibleElementMap == null) { |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 226 | return mVisibleElementMap.length; |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 229 | /** |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 230 | * For complex elements, this function will return the |
| 231 | * sub-element at index |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 232 | * @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 Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 236 | if (mVisibleElementMap == null) { |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 237 | throw new RSIllegalArgumentException("Element contains no sub-elements"); |
| 238 | } |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 239 | if (index < 0 || index >= mVisibleElementMap.length) { |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 240 | throw new RSIllegalArgumentException("Illegal sub-element index"); |
| 241 | } |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 242 | return mElements[mVisibleElementMap[index]]; |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 245 | /** |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 246 | * For complex elements, this function will return the |
| 247 | * sub-element name at index |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 248 | * @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 Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 252 | if (mVisibleElementMap == null) { |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 253 | throw new RSIllegalArgumentException("Element contains no sub-elements"); |
| 254 | } |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 255 | if (index < 0 || index >= mVisibleElementMap.length) { |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 256 | throw new RSIllegalArgumentException("Illegal sub-element index"); |
| 257 | } |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 258 | return mElementNames[mVisibleElementMap[index]]; |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 261 | /** |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 262 | * 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 Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 265 | * @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 Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 269 | if (mVisibleElementMap == null) { |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 270 | throw new RSIllegalArgumentException("Element contains no sub-elements"); |
| 271 | } |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 272 | if (index < 0 || index >= mVisibleElementMap.length) { |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 273 | throw new RSIllegalArgumentException("Illegal sub-element index"); |
| 274 | } |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 275 | return mArraySizes[mVisibleElementMap[index]]; |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 278 | /** |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 279 | * This function specifies the location of a sub-element within |
| 280 | * the element |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 281 | * @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 Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 285 | if (mVisibleElementMap == null) { |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 286 | throw new RSIllegalArgumentException("Element contains no sub-elements"); |
| 287 | } |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 288 | if (index < 0 || index >= mVisibleElementMap.length) { |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 289 | throw new RSIllegalArgumentException("Illegal sub-element index"); |
| 290 | } |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 291 | return mOffsetInBytes[mVisibleElementMap[index]]; |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 294 | /** |
| Alex Sakhartchouk | f5d8ac7 | 2011-12-16 09:44:26 -0800 | [diff] [blame] | 295 | * @return element data type |
| 296 | */ |
| 297 | public DataType getDataType() { |
| 298 | return mType; |
| 299 | } |
| 300 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 301 | /** |
| Alex Sakhartchouk | f5d8ac7 | 2011-12-16 09:44:26 -0800 | [diff] [blame] | 302 | * @return element data kind |
| 303 | */ |
| 304 | public DataKind getDataKind() { |
| 305 | return mKind; |
| 306 | } |
| 307 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 308 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 309 | * 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 Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 315 | 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 Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 322 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 323 | * 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 Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 329 | public static Element U8(RenderScript rs) { |
| 330 | if(rs.mElement_U8 == null) { |
| 331 | rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 332 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 333 | return rs.mElement_U8; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 336 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 337 | * 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 Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 343 | public static Element I8(RenderScript rs) { |
| 344 | if(rs.mElement_I8 == null) { |
| 345 | rs.mElement_I8 = createUser(rs, DataType.SIGNED_8); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 346 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 347 | return rs.mElement_I8; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| Jason Sams | e29f3e7 | 2010-06-08 15:40:48 -0700 | [diff] [blame] | 350 | 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 Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 364 | public static Element U32(RenderScript rs) { |
| 365 | if(rs.mElement_U32 == null) { |
| 366 | rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 367 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 368 | return rs.mElement_U32; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 371 | public static Element I32(RenderScript rs) { |
| 372 | if(rs.mElement_I32 == null) { |
| 373 | rs.mElement_I32 = createUser(rs, DataType.SIGNED_32); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 374 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 375 | return rs.mElement_I32; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| Stephen Hines | 52d8363 | 2010-10-11 16:10:42 -0700 | [diff] [blame] | 378 | 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 Hines | ef1dac2 | 2010-10-01 15:39:33 -0700 | [diff] [blame] | 385 | 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 Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 392 | public static Element F32(RenderScript rs) { |
| 393 | if(rs.mElement_F32 == null) { |
| 394 | rs.mElement_F32 = createUser(rs, DataType.FLOAT_32); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 395 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 396 | return rs.mElement_F32; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| Stephen Hines | 02f41705 | 2010-09-30 15:19:22 -0700 | [diff] [blame] | 399 | public static Element F64(RenderScript rs) { |
| 400 | if(rs.mElement_F64 == null) { |
| 401 | rs.mElement_F64 = createUser(rs, DataType.FLOAT_64); |
| 402 | } |
| 403 | return rs.mElement_F64; |
| 404 | } |
| 405 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 406 | public static Element ELEMENT(RenderScript rs) { |
| 407 | if(rs.mElement_ELEMENT == null) { |
| 408 | rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 409 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 410 | return rs.mElement_ELEMENT; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 413 | public static Element TYPE(RenderScript rs) { |
| 414 | if(rs.mElement_TYPE == null) { |
| 415 | rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 416 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 417 | return rs.mElement_TYPE; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 420 | public static Element ALLOCATION(RenderScript rs) { |
| 421 | if(rs.mElement_ALLOCATION == null) { |
| 422 | rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 423 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 424 | return rs.mElement_ALLOCATION; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 427 | public static Element SAMPLER(RenderScript rs) { |
| 428 | if(rs.mElement_SAMPLER == null) { |
| 429 | rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 430 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 431 | return rs.mElement_SAMPLER; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 434 | public static Element SCRIPT(RenderScript rs) { |
| 435 | if(rs.mElement_SCRIPT == null) { |
| 436 | rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 437 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 438 | return rs.mElement_SCRIPT; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 441 | public static Element MESH(RenderScript rs) { |
| 442 | if(rs.mElement_MESH == null) { |
| 443 | rs.mElement_MESH = createUser(rs, DataType.RS_MESH); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 444 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 445 | return rs.mElement_MESH; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 448 | public static Element PROGRAM_FRAGMENT(RenderScript rs) { |
| 449 | if(rs.mElement_PROGRAM_FRAGMENT == null) { |
| 450 | rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 451 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 452 | return rs.mElement_PROGRAM_FRAGMENT; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 455 | public static Element PROGRAM_VERTEX(RenderScript rs) { |
| 456 | if(rs.mElement_PROGRAM_VERTEX == null) { |
| 457 | rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 458 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 459 | return rs.mElement_PROGRAM_VERTEX; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 462 | public static Element PROGRAM_RASTER(RenderScript rs) { |
| 463 | if(rs.mElement_PROGRAM_RASTER == null) { |
| 464 | rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 465 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 466 | return rs.mElement_PROGRAM_RASTER; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 469 | public static Element PROGRAM_STORE(RenderScript rs) { |
| 470 | if(rs.mElement_PROGRAM_STORE == null) { |
| 471 | rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 472 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 473 | return rs.mElement_PROGRAM_STORE; |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| Stephen Hines | 3a29141 | 2012-04-11 17:27:29 -0700 | [diff] [blame] | 476 | public static Element FONT(RenderScript rs) { |
| 477 | if(rs.mElement_FONT == null) { |
| 478 | rs.mElement_FONT = createUser(rs, DataType.RS_FONT); |
| 479 | } |
| 480 | return rs.mElement_FONT; |
| 481 | } |
| 482 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 483 | |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 484 | public static Element A_8(RenderScript rs) { |
| 485 | if(rs.mElement_A_8 == null) { |
| 486 | rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A); |
| 487 | } |
| 488 | return rs.mElement_A_8; |
| 489 | } |
| 490 | |
| 491 | public static Element RGB_565(RenderScript rs) { |
| 492 | if(rs.mElement_RGB_565 == null) { |
| 493 | rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB); |
| 494 | } |
| 495 | return rs.mElement_RGB_565; |
| 496 | } |
| 497 | |
| 498 | public static Element RGB_888(RenderScript rs) { |
| 499 | if(rs.mElement_RGB_888 == null) { |
| 500 | rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB); |
| 501 | } |
| 502 | return rs.mElement_RGB_888; |
| 503 | } |
| 504 | |
| 505 | public static Element RGBA_5551(RenderScript rs) { |
| 506 | if(rs.mElement_RGBA_5551 == null) { |
| 507 | rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA); |
| 508 | } |
| 509 | return rs.mElement_RGBA_5551; |
| 510 | } |
| 511 | |
| 512 | public static Element RGBA_4444(RenderScript rs) { |
| 513 | if(rs.mElement_RGBA_4444 == null) { |
| 514 | rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA); |
| 515 | } |
| 516 | return rs.mElement_RGBA_4444; |
| 517 | } |
| 518 | |
| 519 | public static Element RGBA_8888(RenderScript rs) { |
| 520 | if(rs.mElement_RGBA_8888 == null) { |
| 521 | rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA); |
| 522 | } |
| 523 | return rs.mElement_RGBA_8888; |
| 524 | } |
| 525 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 526 | public static Element F32_2(RenderScript rs) { |
| 527 | if(rs.mElement_FLOAT_2 == null) { |
| 528 | rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 529 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 530 | return rs.mElement_FLOAT_2; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 533 | public static Element F32_3(RenderScript rs) { |
| 534 | if(rs.mElement_FLOAT_3 == null) { |
| 535 | rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 536 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 537 | return rs.mElement_FLOAT_3; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 540 | public static Element F32_4(RenderScript rs) { |
| 541 | if(rs.mElement_FLOAT_4 == null) { |
| 542 | rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 543 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 544 | return rs.mElement_FLOAT_4; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| Stephen Hines | 836c4a5 | 2011-06-01 14:38:10 -0700 | [diff] [blame] | 547 | public static Element F64_2(RenderScript rs) { |
| 548 | if(rs.mElement_DOUBLE_2 == null) { |
| 549 | rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2); |
| 550 | } |
| 551 | return rs.mElement_DOUBLE_2; |
| 552 | } |
| 553 | |
| 554 | public static Element F64_3(RenderScript rs) { |
| 555 | if(rs.mElement_DOUBLE_3 == null) { |
| 556 | rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3); |
| 557 | } |
| 558 | return rs.mElement_DOUBLE_3; |
| 559 | } |
| 560 | |
| 561 | public static Element F64_4(RenderScript rs) { |
| 562 | if(rs.mElement_DOUBLE_4 == null) { |
| 563 | rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4); |
| 564 | } |
| 565 | return rs.mElement_DOUBLE_4; |
| 566 | } |
| 567 | |
| 568 | public static Element U8_2(RenderScript rs) { |
| 569 | if(rs.mElement_UCHAR_2 == null) { |
| 570 | rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2); |
| 571 | } |
| 572 | return rs.mElement_UCHAR_2; |
| 573 | } |
| 574 | |
| 575 | public static Element U8_3(RenderScript rs) { |
| 576 | if(rs.mElement_UCHAR_3 == null) { |
| 577 | rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3); |
| 578 | } |
| 579 | return rs.mElement_UCHAR_3; |
| 580 | } |
| 581 | |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 582 | public static Element U8_4(RenderScript rs) { |
| 583 | if(rs.mElement_UCHAR_4 == null) { |
| 584 | rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 585 | } |
| Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 586 | return rs.mElement_UCHAR_4; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| Stephen Hines | 836c4a5 | 2011-06-01 14:38:10 -0700 | [diff] [blame] | 589 | public static Element I8_2(RenderScript rs) { |
| 590 | if(rs.mElement_CHAR_2 == null) { |
| 591 | rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2); |
| 592 | } |
| 593 | return rs.mElement_CHAR_2; |
| 594 | } |
| 595 | |
| 596 | public static Element I8_3(RenderScript rs) { |
| 597 | if(rs.mElement_CHAR_3 == null) { |
| 598 | rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3); |
| 599 | } |
| 600 | return rs.mElement_CHAR_3; |
| 601 | } |
| 602 | |
| 603 | public static Element I8_4(RenderScript rs) { |
| 604 | if(rs.mElement_CHAR_4 == null) { |
| 605 | rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4); |
| 606 | } |
| 607 | return rs.mElement_CHAR_4; |
| 608 | } |
| 609 | |
| 610 | public static Element U16_2(RenderScript rs) { |
| 611 | if(rs.mElement_USHORT_2 == null) { |
| 612 | rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2); |
| 613 | } |
| 614 | return rs.mElement_USHORT_2; |
| 615 | } |
| 616 | |
| 617 | public static Element U16_3(RenderScript rs) { |
| 618 | if(rs.mElement_USHORT_3 == null) { |
| 619 | rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3); |
| 620 | } |
| 621 | return rs.mElement_USHORT_3; |
| 622 | } |
| 623 | |
| 624 | public static Element U16_4(RenderScript rs) { |
| 625 | if(rs.mElement_USHORT_4 == null) { |
| 626 | rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4); |
| 627 | } |
| 628 | return rs.mElement_USHORT_4; |
| 629 | } |
| 630 | |
| 631 | public static Element I16_2(RenderScript rs) { |
| 632 | if(rs.mElement_SHORT_2 == null) { |
| 633 | rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2); |
| 634 | } |
| 635 | return rs.mElement_SHORT_2; |
| 636 | } |
| 637 | |
| 638 | public static Element I16_3(RenderScript rs) { |
| 639 | if(rs.mElement_SHORT_3 == null) { |
| 640 | rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3); |
| 641 | } |
| 642 | return rs.mElement_SHORT_3; |
| 643 | } |
| 644 | |
| 645 | public static Element I16_4(RenderScript rs) { |
| 646 | if(rs.mElement_SHORT_4 == null) { |
| 647 | rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4); |
| 648 | } |
| 649 | return rs.mElement_SHORT_4; |
| 650 | } |
| 651 | |
| 652 | public static Element U32_2(RenderScript rs) { |
| 653 | if(rs.mElement_UINT_2 == null) { |
| 654 | rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2); |
| 655 | } |
| 656 | return rs.mElement_UINT_2; |
| 657 | } |
| 658 | |
| 659 | public static Element U32_3(RenderScript rs) { |
| 660 | if(rs.mElement_UINT_3 == null) { |
| 661 | rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3); |
| 662 | } |
| 663 | return rs.mElement_UINT_3; |
| 664 | } |
| 665 | |
| 666 | public static Element U32_4(RenderScript rs) { |
| 667 | if(rs.mElement_UINT_4 == null) { |
| 668 | rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4); |
| 669 | } |
| 670 | return rs.mElement_UINT_4; |
| 671 | } |
| 672 | |
| 673 | public static Element I32_2(RenderScript rs) { |
| 674 | if(rs.mElement_INT_2 == null) { |
| 675 | rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2); |
| 676 | } |
| 677 | return rs.mElement_INT_2; |
| 678 | } |
| 679 | |
| 680 | public static Element I32_3(RenderScript rs) { |
| 681 | if(rs.mElement_INT_3 == null) { |
| 682 | rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3); |
| 683 | } |
| 684 | return rs.mElement_INT_3; |
| 685 | } |
| 686 | |
| 687 | public static Element I32_4(RenderScript rs) { |
| 688 | if(rs.mElement_INT_4 == null) { |
| 689 | rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4); |
| 690 | } |
| 691 | return rs.mElement_INT_4; |
| 692 | } |
| 693 | |
| 694 | public static Element U64_2(RenderScript rs) { |
| 695 | if(rs.mElement_ULONG_2 == null) { |
| 696 | rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2); |
| 697 | } |
| 698 | return rs.mElement_ULONG_2; |
| 699 | } |
| 700 | |
| 701 | public static Element U64_3(RenderScript rs) { |
| 702 | if(rs.mElement_ULONG_3 == null) { |
| 703 | rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3); |
| 704 | } |
| 705 | return rs.mElement_ULONG_3; |
| 706 | } |
| 707 | |
| 708 | public static Element U64_4(RenderScript rs) { |
| 709 | if(rs.mElement_ULONG_4 == null) { |
| 710 | rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4); |
| 711 | } |
| 712 | return rs.mElement_ULONG_4; |
| 713 | } |
| 714 | |
| 715 | public static Element I64_2(RenderScript rs) { |
| 716 | if(rs.mElement_LONG_2 == null) { |
| 717 | rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2); |
| 718 | } |
| 719 | return rs.mElement_LONG_2; |
| 720 | } |
| 721 | |
| 722 | public static Element I64_3(RenderScript rs) { |
| 723 | if(rs.mElement_LONG_3 == null) { |
| 724 | rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3); |
| 725 | } |
| 726 | return rs.mElement_LONG_3; |
| 727 | } |
| 728 | |
| 729 | public static Element I64_4(RenderScript rs) { |
| 730 | if(rs.mElement_LONG_4 == null) { |
| 731 | rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4); |
| 732 | } |
| 733 | return rs.mElement_LONG_4; |
| 734 | } |
| 735 | |
| Tim Murray | 932e78e | 2013-09-03 11:42:26 -0700 | [diff] [blame] | 736 | public static Element YUV(RenderScript rs) { |
| 737 | if (rs.mElement_YUV == null) { |
| 738 | rs.mElement_YUV = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_YUV); |
| 739 | } |
| 740 | return rs.mElement_YUV; |
| 741 | } |
| 742 | |
| Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 743 | public static Element MATRIX_4X4(RenderScript rs) { |
| 744 | if(rs.mElement_MATRIX_4X4 == null) { |
| 745 | rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4); |
| 746 | } |
| 747 | return rs.mElement_MATRIX_4X4; |
| 748 | } |
| Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 749 | |
| 750 | /** @deprecated use MATRIX_4X4 |
| 751 | */ |
| Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 752 | public static Element MATRIX4X4(RenderScript rs) { |
| 753 | return MATRIX_4X4(rs); |
| 754 | } |
| 755 | |
| 756 | public static Element MATRIX_3X3(RenderScript rs) { |
| 757 | if(rs.mElement_MATRIX_3X3 == null) { |
| 758 | rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3); |
| 759 | } |
| Alex Sakhartchouk | 3476977 | 2011-02-28 16:01:28 -0800 | [diff] [blame] | 760 | return rs.mElement_MATRIX_3X3; |
| Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | public static Element MATRIX_2X2(RenderScript rs) { |
| 764 | if(rs.mElement_MATRIX_2X2 == null) { |
| 765 | rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2); |
| 766 | } |
| 767 | return rs.mElement_MATRIX_2X2; |
| 768 | } |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 769 | |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 770 | Element(long id, RenderScript rs, Element[] e, String[] n, int[] as) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 771 | super(id, rs); |
| Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 772 | mSize = 0; |
| Alex Sakhartchouk | fd79e02 | 2011-12-22 14:30:55 -0800 | [diff] [blame] | 773 | mVectorSize = 1; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 774 | mElements = e; |
| 775 | mElementNames = n; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 776 | mArraySizes = as; |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 777 | mType = DataType.NONE; |
| 778 | mKind = DataKind.USER; |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 779 | mOffsetInBytes = new int[mElements.length]; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 780 | for (int ct = 0; ct < mElements.length; ct++ ) { |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 781 | mOffsetInBytes[ct] = mSize; |
| Alex Sakhartchouk | 9e401bc | 2010-10-13 14:22:02 -0700 | [diff] [blame] | 782 | mSize += mElements[ct].mSize * mArraySizes[ct]; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 783 | } |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 784 | updateVisibleSubElements(); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 785 | } |
| 786 | |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 787 | Element(long id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 788 | super(id, rs); |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 789 | if ((dt != DataType.UNSIGNED_5_6_5) && |
| 790 | (dt != DataType.UNSIGNED_4_4_4_4) && |
| 791 | (dt != DataType.UNSIGNED_5_5_5_1)) { |
| Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 792 | if (size == 3) { |
| 793 | mSize = dt.mSize * 4; |
| 794 | } else { |
| 795 | mSize = dt.mSize * size; |
| 796 | } |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 797 | } else { |
| 798 | mSize = dt.mSize; |
| 799 | } |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 800 | mType = dt; |
| 801 | mKind = dk; |
| 802 | mNormalized = norm; |
| 803 | mVectorSize = size; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 806 | Element(long id, RenderScript rs) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 807 | super(id, rs); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | @Override |
| 811 | void updateFromNative() { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 812 | super.updateFromNative(); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 813 | |
| 814 | // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements |
| 815 | int[] dataBuffer = new int[5]; |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 816 | mRS.nElementGetNativeData(getID(mRS), dataBuffer); |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 817 | |
| 818 | mNormalized = dataBuffer[2] == 1 ? true : false; |
| 819 | mVectorSize = dataBuffer[3]; |
| 820 | mSize = 0; |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 821 | for (DataType dt: DataType.values()) { |
| 822 | if(dt.mID == dataBuffer[0]){ |
| 823 | mType = dt; |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 824 | mSize = mType.mSize * mVectorSize; |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | for (DataKind dk: DataKind.values()) { |
| 828 | if(dk.mID == dataBuffer[1]){ |
| 829 | mKind = dk; |
| 830 | } |
| 831 | } |
| 832 | |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 833 | int numSubElements = dataBuffer[4]; |
| 834 | if(numSubElements > 0) { |
| 835 | mElements = new Element[numSubElements]; |
| 836 | mElementNames = new String[numSubElements]; |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 837 | mArraySizes = new int[numSubElements]; |
| 838 | mOffsetInBytes = new int[numSubElements]; |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 839 | |
| Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 840 | long[] subElementIds = new long[numSubElements]; |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 841 | mRS.nElementGetSubElements(getID(mRS), subElementIds, mElementNames, mArraySizes); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 842 | for(int i = 0; i < numSubElements; i ++) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 843 | mElements[i] = new Element(subElementIds[i], mRS); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 844 | mElements[i].updateFromNative(); |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 845 | mOffsetInBytes[i] = mSize; |
| 846 | mSize += mElements[i].mSize * mArraySizes[i]; |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 847 | } |
| 848 | } |
| Alex Sakhartchouk | 3aac0ab | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 849 | updateVisibleSubElements(); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 852 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 853 | * Create a custom Element of the specified DataType. The DataKind will be |
| 854 | * set to USER and the vector size to 1 indicating non-vector. |
| 855 | * |
| 856 | * @param rs The context associated with the new Element. |
| 857 | * @param dt The DataType for the new element. |
| 858 | * @return Element |
| 859 | */ |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 860 | static Element createUser(RenderScript rs, DataType dt) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 861 | DataKind dk = DataKind.USER; |
| 862 | boolean norm = false; |
| 863 | int vecSize = 1; |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 864 | long id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize); |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 865 | return new Element(id, rs, dt, dk, norm, vecSize); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 866 | } |
| 867 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 868 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 869 | * Create a custom vector element of the specified DataType and vector size. |
| Stephen Hines | 3beb60e | 2012-02-14 20:38:20 -0800 | [diff] [blame] | 870 | * DataKind will be set to USER. Only primitive types (FLOAT_32, FLOAT_64, |
| 871 | * SIGNED_8, SIGNED_16, SIGNED_32, SIGNED_64, UNSIGNED_8, UNSIGNED_16, |
| 872 | * UNSIGNED_32, UNSIGNED_64, BOOLEAN) are supported. |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 873 | * |
| 874 | * @param rs The context associated with the new Element. |
| Stephen Hines | 3beb60e | 2012-02-14 20:38:20 -0800 | [diff] [blame] | 875 | * @param dt The DataType for the new Element. |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 876 | * @param size Vector size for the new Element. Range 2-4 inclusive |
| 877 | * supported. |
| 878 | * |
| 879 | * @return Element |
| 880 | */ |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 881 | public static Element createVector(RenderScript rs, DataType dt, int size) { |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 882 | if (size < 2 || size > 4) { |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 883 | throw new RSIllegalArgumentException("Vector size out of range 2-4."); |
| Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 884 | } |
| Stephen Hines | 3beb60e | 2012-02-14 20:38:20 -0800 | [diff] [blame] | 885 | |
| 886 | switch (dt) { |
| 887 | // Support only primitive integer/float/boolean types as vectors. |
| 888 | case FLOAT_32: |
| 889 | case FLOAT_64: |
| 890 | case SIGNED_8: |
| 891 | case SIGNED_16: |
| 892 | case SIGNED_32: |
| 893 | case SIGNED_64: |
| 894 | case UNSIGNED_8: |
| 895 | case UNSIGNED_16: |
| 896 | case UNSIGNED_32: |
| 897 | case UNSIGNED_64: |
| 898 | case BOOLEAN: { |
| 899 | DataKind dk = DataKind.USER; |
| 900 | boolean norm = false; |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 901 | long id = rs.nElementCreate(dt.mID, dk.mID, norm, size); |
| Stephen Hines | 3beb60e | 2012-02-14 20:38:20 -0800 | [diff] [blame] | 902 | return new Element(id, rs, dt, dk, norm, size); |
| 903 | } |
| 904 | |
| 905 | default: { |
| 906 | throw new RSIllegalArgumentException("Cannot create vector of " + |
| 907 | "non-primitive type."); |
| 908 | } |
| 909 | } |
| Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 910 | } |
| 911 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 912 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 913 | * Create a new pixel Element type. A matching DataType and DataKind must |
| 914 | * be provided. The DataType and DataKind must contain the same number of |
| 915 | * components. Vector size will be set to 1. |
| 916 | * |
| 917 | * @param rs The context associated with the new Element. |
| 918 | * @param dt The DataType for the new element. |
| 919 | * @param dk The DataKind to specify the mapping of each component in the |
| 920 | * DataType. |
| 921 | * |
| 922 | * @return Element |
| 923 | */ |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 924 | public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) { |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 925 | if (!(dk == DataKind.PIXEL_L || |
| 926 | dk == DataKind.PIXEL_A || |
| 927 | dk == DataKind.PIXEL_LA || |
| 928 | dk == DataKind.PIXEL_RGB || |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 929 | dk == DataKind.PIXEL_RGBA || |
| Jason Sams | dd6c8b3 | 2013-02-15 17:27:24 -0800 | [diff] [blame] | 930 | dk == DataKind.PIXEL_DEPTH || |
| 931 | dk == DataKind.PIXEL_YUV)) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 932 | throw new RSIllegalArgumentException("Unsupported DataKind"); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 933 | } |
| 934 | if (!(dt == DataType.UNSIGNED_8 || |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 935 | dt == DataType.UNSIGNED_16 || |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 936 | dt == DataType.UNSIGNED_5_6_5 || |
| 937 | dt == DataType.UNSIGNED_4_4_4_4 || |
| 938 | dt == DataType.UNSIGNED_5_5_5_1)) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 939 | throw new RSIllegalArgumentException("Unsupported DataType"); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 940 | } |
| 941 | if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 942 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 943 | } |
| 944 | if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 945 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 946 | } |
| 947 | if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 948 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 949 | } |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 950 | if (dt == DataType.UNSIGNED_16 && |
| 951 | dk != DataKind.PIXEL_DEPTH) { |
| 952 | throw new RSIllegalArgumentException("Bad kind and type combo"); |
| 953 | } |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 954 | |
| 955 | int size = 1; |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 956 | switch (dk) { |
| 957 | case PIXEL_LA: |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 958 | size = 2; |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 959 | break; |
| 960 | case PIXEL_RGB: |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 961 | size = 3; |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 962 | break; |
| 963 | case PIXEL_RGBA: |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 964 | size = 4; |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 965 | break; |
| 966 | case PIXEL_DEPTH: |
| 967 | size = 2; |
| 968 | break; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 969 | } |
| 970 | |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 971 | boolean norm = true; |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 972 | long id = rs.nElementCreate(dt.mID, dk.mID, norm, size); |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 973 | return new Element(id, rs, dt, dk, norm, size); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 974 | } |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 975 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 976 | /** |
| Stephen Hines | f257e51 | 2011-06-14 14:54:29 -0700 | [diff] [blame] | 977 | * Check if the current Element is compatible with another Element. |
| 978 | * Primitive Elements are compatible if they share the same underlying |
| 979 | * size and type (i.e. U8 is compatible with A_8). User-defined Elements |
| 980 | * must be equal in order to be compatible. This requires strict name |
| 981 | * equivalence for all sub-Elements (in addition to structural equivalence). |
| 982 | * |
| 983 | * @param e The Element to check compatibility with. |
| 984 | * |
| 985 | * @return boolean true if the Elements are compatible, otherwise false. |
| 986 | */ |
| 987 | public boolean isCompatible(Element e) { |
| 988 | // Try strict BaseObj equality to start with. |
| 989 | if (this.equals(e)) { |
| 990 | return true; |
| 991 | } |
| 992 | |
| 993 | // Ignore mKind because it is allowed to be different (user vs. pixel). |
| 994 | // We also ignore mNormalized because it can be different. The mType |
| Stephen Hines | 2094811 | 2012-02-14 19:42:42 -0800 | [diff] [blame] | 995 | // field must not be NONE since we require name equivalence for |
| 996 | // all user-created Elements. |
| Stephen Hines | f257e51 | 2011-06-14 14:54:29 -0700 | [diff] [blame] | 997 | return ((mSize == e.mSize) && |
| Stephen Hines | 2094811 | 2012-02-14 19:42:42 -0800 | [diff] [blame] | 998 | (mType != DataType.NONE) && |
| Stephen Hines | f257e51 | 2011-06-14 14:54:29 -0700 | [diff] [blame] | 999 | (mType == e.mType) && |
| 1000 | (mVectorSize == e.mVectorSize)); |
| 1001 | } |
| 1002 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1003 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 1004 | * Builder class for producing complex elements with matching field and name |
| 1005 | * pairs. The builder starts empty. The order in which elements are added |
| 1006 | * is retained for the layout in memory. |
| 1007 | * |
| 1008 | */ |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 1009 | public static class Builder { |
| 1010 | RenderScript mRS; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 1011 | Element[] mElements; |
| 1012 | String[] mElementNames; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 1013 | int[] mArraySizes; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 1014 | int mCount; |
| Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 1015 | int mSkipPadding; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 1016 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1017 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 1018 | * Create a builder object. |
| 1019 | * |
| 1020 | * @param rs |
| 1021 | */ |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 1022 | public Builder(RenderScript rs) { |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 1023 | mRS = rs; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 1024 | mCount = 0; |
| 1025 | mElements = new Element[8]; |
| 1026 | mElementNames = new String[8]; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 1027 | mArraySizes = new int[8]; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1030 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 1031 | * Add an array of elements to this element. |
| 1032 | * |
| 1033 | * @param element |
| 1034 | * @param name |
| 1035 | * @param arraySize |
| 1036 | */ |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1037 | public Builder add(Element element, String name, int arraySize) { |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 1038 | if (arraySize < 1) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 1039 | throw new RSIllegalArgumentException("Array size cannot be less than 1."); |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 1040 | } |
| Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 1041 | |
| 1042 | // Skip padding fields after a vector 3 type. |
| 1043 | if (mSkipPadding != 0) { |
| 1044 | if (name.startsWith("#padding_")) { |
| 1045 | mSkipPadding = 0; |
| 1046 | return this; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | if (element.mVectorSize == 3) { |
| 1051 | mSkipPadding = 1; |
| 1052 | } else { |
| 1053 | mSkipPadding = 0; |
| 1054 | } |
| 1055 | |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 1056 | if(mCount == mElements.length) { |
| 1057 | Element[] e = new Element[mCount + 8]; |
| 1058 | String[] s = new String[mCount + 8]; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 1059 | int[] as = new int[mCount + 8]; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 1060 | System.arraycopy(mElements, 0, e, 0, mCount); |
| 1061 | System.arraycopy(mElementNames, 0, s, 0, mCount); |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 1062 | System.arraycopy(mArraySizes, 0, as, 0, mCount); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 1063 | mElements = e; |
| 1064 | mElementNames = s; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 1065 | mArraySizes = as; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 1066 | } |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 1067 | mElements[mCount] = element; |
| 1068 | mElementNames[mCount] = name; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 1069 | mArraySizes[mCount] = arraySize; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 1070 | mCount++; |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1071 | return this; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1074 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 1075 | * Add a single element to this Element. |
| 1076 | * |
| 1077 | * @param element |
| 1078 | * @param name |
| 1079 | */ |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1080 | public Builder add(Element element, String name) { |
| 1081 | return add(element, name, 1); |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1084 | /** |
| Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 1085 | * Create the element from this builder. |
| 1086 | * |
| 1087 | * |
| 1088 | * @return Element |
| 1089 | */ |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 1090 | public Element create() { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 1091 | mRS.validate(); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 1092 | Element[] ein = new Element[mCount]; |
| 1093 | String[] sin = new String[mCount]; |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 1094 | int[] asin = new int[mCount]; |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 1095 | java.lang.System.arraycopy(mElements, 0, ein, 0, mCount); |
| 1096 | java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount); |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 1097 | java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount); |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 1098 | |
| Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 1099 | long[] ids = new long[ein.length]; |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 1100 | for (int ct = 0; ct < ein.length; ct++ ) { |
| Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 1101 | ids[ct] = ein[ct].getID(mRS); |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 1102 | } |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 1103 | long id = mRS.nElementCreate2(ids, sin, asin); |
| Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 1104 | return new Element(id, mRS, ein, sin, asin); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 1105 | } |
| 1106 | } |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |