Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -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 | b8c5a84 | 2009-07-31 20:40:47 -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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 19 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 20 | * <p>A Type describes the {@link android.renderscript.Element} and dimensions used for an {@link |
| 21 | * android.renderscript.Allocation} or a parallel operation. Types are created through {@link |
| 22 | * android.renderscript.Type.Builder}.</p> |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 23 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 24 | * <p>A Type always includes an {@link android.renderscript.Element} and an X |
| 25 | * dimension. A Type may be multidimensional, up to three dimensions. A nonzero |
| 26 | * value in the Y or Z dimensions indicates that the dimension is present. Note |
| 27 | * that a Type with only a given X dimension and a Type with the same X |
| 28 | * dimension but Y = 1 are not equivalent.</p> |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 29 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 30 | * <p>A Type also supports inclusion of level of detail (LOD) or cube map |
| 31 | * faces. LOD and cube map faces are booleans to indicate present or not |
| 32 | * present. </p> |
| 33 | * |
Eino-Ville Talvala | ccadaf1 | 2013-08-14 10:36:30 -0700 | [diff] [blame] | 34 | * <p>A Type also supports YUV format information to support an |
| 35 | * {@link android.renderscript.Allocation} in a YUV format. The YUV formats |
| 36 | * supported are {@link android.graphics.ImageFormat#YV12}, |
| 37 | * {@link android.graphics.ImageFormat#NV21}, and |
| 38 | * {@link android.graphics.ImageFormat#YUV_420_888}</p> |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 39 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 40 | * <div class="special reference"> |
| 41 | * <h3>Developer Guides</h3> |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 42 | * <p>For more information about creating an application that uses RenderScript, read the |
| 43 | * <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] | 44 | * </div> |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 45 | **/ |
| 46 | public class Type extends BaseObj { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 47 | int mDimX; |
| 48 | int mDimY; |
| 49 | int mDimZ; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 50 | boolean mDimMipmaps; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 51 | boolean mDimFaces; |
Jason Sams | 8140d7b | 2012-12-13 17:01:09 -0800 | [diff] [blame] | 52 | int mDimYuv; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 53 | int mElementCount; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 54 | Element mElement; |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame] | 55 | int mArrays[]; |
| 56 | |
| 57 | static final int mMaxArrays = 4; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 58 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 59 | public enum CubemapFace { |
Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 60 | POSITIVE_X (0), |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 61 | NEGATIVE_X (1), |
Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 62 | POSITIVE_Y (2), |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 63 | NEGATIVE_Y (3), |
Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 64 | POSITIVE_Z (4), |
| 65 | NEGATIVE_Z (5), |
| 66 | @Deprecated |
| 67 | POSITVE_X (0), |
| 68 | @Deprecated |
| 69 | POSITVE_Y (2), |
| 70 | @Deprecated |
| 71 | POSITVE_Z (4); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 72 | |
| 73 | int mID; |
| 74 | CubemapFace(int id) { |
| 75 | mID = id; |
| 76 | } |
| 77 | } |
| 78 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 79 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 80 | * Return the element associated with this Type. |
| 81 | * |
| 82 | * @return Element |
| 83 | */ |
Jason Sams | e17964e | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 84 | public Element getElement() { |
| 85 | return mElement; |
| 86 | } |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 87 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 88 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 89 | * Return the value of the X dimension. |
| 90 | * |
| 91 | * @return int |
| 92 | */ |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 93 | public int getX() { |
| 94 | return mDimX; |
| 95 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 96 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 97 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 98 | * Return the value of the Y dimension or 0 for a 1D allocation. |
| 99 | * |
| 100 | * @return int |
| 101 | */ |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 102 | public int getY() { |
| 103 | return mDimY; |
| 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 | * Return the value of the Z dimension or 0 for a 1D or 2D allocation. |
| 108 | * |
| 109 | * @return int |
| 110 | */ |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 111 | public int getZ() { |
| 112 | return mDimZ; |
| 113 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 114 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 115 | /** |
Jason Sams | 5a722cf | 2013-03-26 13:27:37 -0700 | [diff] [blame] | 116 | * Get the YUV format |
| 117 | * |
Jason Sams | 02d56d9 | 2013-04-12 16:40:50 -0700 | [diff] [blame] | 118 | * |
Jason Sams | 5a722cf | 2013-03-26 13:27:37 -0700 | [diff] [blame] | 119 | * @return int |
| 120 | */ |
| 121 | public int getYuv() { |
| 122 | return mDimYuv; |
| 123 | } |
| 124 | |
| 125 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 126 | * Return if the Type has a mipmap chain. |
| 127 | * |
| 128 | * @return boolean |
| 129 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 130 | public boolean hasMipmaps() { |
| 131 | return mDimMipmaps; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 132 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 133 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 134 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 135 | * Return if the Type is a cube map. |
| 136 | * |
| 137 | * @return boolean |
| 138 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 139 | public boolean hasFaces() { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 140 | return mDimFaces; |
| 141 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 142 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 143 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 144 | * Return the total number of accessable cells in the Type. |
| 145 | * |
| 146 | * @return int |
| 147 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 148 | public int getCount() { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 149 | return mElementCount; |
| 150 | } |
| 151 | |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame] | 152 | /** |
Jason Sams | d016266 | 2015-04-15 17:18:10 -0700 | [diff] [blame] | 153 | * @hide |
Jason Sams | 6a420b5 | 2015-03-30 15:31:26 -0700 | [diff] [blame] | 154 | * Return the dimension of the specified array. |
| 155 | * |
| 156 | * @param arrayNum The array dimension to query |
| 157 | * @return int |
| 158 | */ |
| 159 | public int getArray(int arrayNum) { |
| 160 | if ((arrayNum < 0) || (arrayNum >= mMaxArrays)) { |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame] | 161 | throw new RSIllegalArgumentException("Array dimension out of range."); |
| 162 | } |
| 163 | |
Jason Sams | 6a420b5 | 2015-03-30 15:31:26 -0700 | [diff] [blame] | 164 | if (mArrays == null || arrayNum >= mArrays.length) { |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame] | 165 | // Dimension in range but no array for that dimension allocated |
| 166 | return 0; |
| 167 | } |
| 168 | |
Jason Sams | 6a420b5 | 2015-03-30 15:31:26 -0700 | [diff] [blame] | 169 | return mArrays[arrayNum]; |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
Jason Sams | d016266 | 2015-04-15 17:18:10 -0700 | [diff] [blame] | 173 | * @hide |
Jason Sams | 6a420b5 | 2015-03-30 15:31:26 -0700 | [diff] [blame] | 174 | * Return the number of array dimensions. |
| 175 | * |
| 176 | * @return int |
| 177 | */ |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame] | 178 | public int getArrayCount() { |
| 179 | if (mArrays != null) return mArrays.length; |
| 180 | return 0; |
| 181 | } |
| 182 | |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 183 | void calcElementCount() { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 184 | boolean hasLod = hasMipmaps(); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 185 | int x = getX(); |
| 186 | int y = getY(); |
| 187 | int z = getZ(); |
| 188 | int faces = 1; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 189 | if (hasFaces()) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 190 | faces = 6; |
| 191 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 192 | if (x == 0) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 193 | x = 1; |
| 194 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 195 | if (y == 0) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 196 | y = 1; |
| 197 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 198 | if (z == 0) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 199 | z = 1; |
| 200 | } |
| 201 | |
| 202 | int count = x * y * z * faces; |
Alex Sakhartchouk | 9ea30a6 | 2011-03-02 12:33:50 -0800 | [diff] [blame] | 203 | |
| 204 | while (hasLod && ((x > 1) || (y > 1) || (z > 1))) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 205 | if(x > 1) { |
| 206 | x >>= 1; |
| 207 | } |
| 208 | if(y > 1) { |
| 209 | y >>= 1; |
| 210 | } |
| 211 | if(z > 1) { |
| 212 | z >>= 1; |
| 213 | } |
| 214 | |
| 215 | count += x * y * z * faces; |
| 216 | } |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame] | 217 | |
| 218 | if (mArrays != null) { |
| 219 | for (int ct = 0; ct < mArrays.length; ct++) { |
| 220 | count *= mArrays[ct]; |
| 221 | } |
| 222 | } |
| 223 | |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 224 | mElementCount = count; |
| 225 | } |
| 226 | |
| 227 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 228 | Type(long id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 229 | super(id, rs); |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 232 | @Override |
| 233 | void updateFromNative() { |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 234 | // We have 6 integer/long to obtain mDimX; mDimY; mDimZ; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 235 | // mDimLOD; mDimFaces; mElement; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 236 | long[] dataBuffer = new long[6]; |
| 237 | mRS.nTypeGetNativeData(getID(mRS), dataBuffer); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 238 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 239 | mDimX = (int)dataBuffer[0]; |
| 240 | mDimY = (int)dataBuffer[1]; |
| 241 | mDimZ = (int)dataBuffer[2]; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 242 | mDimMipmaps = dataBuffer[3] == 1 ? true : false; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 243 | mDimFaces = dataBuffer[4] == 1 ? true : false; |
| 244 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 245 | long elementID = dataBuffer[5]; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 246 | if(elementID != 0) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 247 | mElement = new Element(elementID, mRS); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 248 | mElement.updateFromNative(); |
| 249 | } |
| 250 | calcElementCount(); |
| 251 | } |
| 252 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 253 | /** |
Jason Sams | ec44e5d | 2013-10-09 17:15:36 -0700 | [diff] [blame] | 254 | * Utility function for creating basic 1D types. The type is |
| 255 | * created without mipmaps enabled. |
| 256 | * |
| 257 | * @param rs The RenderScript context |
| 258 | * @param e The Element for the Type |
| 259 | * @param dimX The X dimension, must be > 0 |
| 260 | * |
| 261 | * @return Type |
| 262 | */ |
| 263 | static public Type createX(RenderScript rs, Element e, int dimX) { |
| 264 | if (dimX < 1) { |
| 265 | throw new RSInvalidStateException("Dimension must be >= 1."); |
| 266 | } |
| 267 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 268 | long id = rs.nTypeCreate(e.getID(rs), dimX, 0, 0, false, false, 0); |
Jason Sams | ec44e5d | 2013-10-09 17:15:36 -0700 | [diff] [blame] | 269 | Type t = new Type(id, rs); |
| 270 | t.mElement = e; |
| 271 | t.mDimX = dimX; |
| 272 | t.calcElementCount(); |
| 273 | return t; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Utility function for creating basic 2D types. The type is |
| 278 | * created without mipmaps or cubemaps. |
| 279 | * |
| 280 | * @param rs The RenderScript context |
| 281 | * @param e The Element for the Type |
| 282 | * @param dimX The X dimension, must be > 0 |
| 283 | * @param dimY The Y dimension, must be > 0 |
| 284 | * |
| 285 | * @return Type |
| 286 | */ |
| 287 | static public Type createXY(RenderScript rs, Element e, int dimX, int dimY) { |
| 288 | if ((dimX < 1) || (dimY < 1)) { |
| 289 | throw new RSInvalidStateException("Dimension must be >= 1."); |
| 290 | } |
| 291 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 292 | long id = rs.nTypeCreate(e.getID(rs), dimX, dimY, 0, false, false, 0); |
Jason Sams | ec44e5d | 2013-10-09 17:15:36 -0700 | [diff] [blame] | 293 | Type t = new Type(id, rs); |
| 294 | t.mElement = e; |
| 295 | t.mDimX = dimX; |
| 296 | t.mDimY = dimY; |
| 297 | t.calcElementCount(); |
| 298 | return t; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Utility function for creating basic 3D types. The type is |
| 303 | * created without mipmaps. |
| 304 | * |
| 305 | * @param rs The RenderScript context |
| 306 | * @param e The Element for the Type |
| 307 | * @param dimX The X dimension, must be > 0 |
| 308 | * @param dimY The Y dimension, must be > 0 |
| 309 | * @param dimZ The Z dimension, must be > 0 |
| 310 | * |
| 311 | * @return Type |
| 312 | */ |
| 313 | static public Type createXYZ(RenderScript rs, Element e, int dimX, int dimY, int dimZ) { |
| 314 | if ((dimX < 1) || (dimY < 1) || (dimZ < 1)) { |
| 315 | throw new RSInvalidStateException("Dimension must be >= 1."); |
| 316 | } |
| 317 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 318 | long id = rs.nTypeCreate(e.getID(rs), dimX, dimY, dimZ, false, false, 0); |
Jason Sams | ec44e5d | 2013-10-09 17:15:36 -0700 | [diff] [blame] | 319 | Type t = new Type(id, rs); |
| 320 | t.mElement = e; |
| 321 | t.mDimX = dimX; |
| 322 | t.mDimY = dimY; |
| 323 | t.mDimZ = dimZ; |
| 324 | t.calcElementCount(); |
| 325 | return t; |
| 326 | } |
| 327 | |
| 328 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 329 | * Builder class for Type. |
| 330 | * |
| 331 | */ |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 332 | public static class Builder { |
| 333 | RenderScript mRS; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 334 | int mDimX = 1; |
| 335 | int mDimY; |
| 336 | int mDimZ; |
| 337 | boolean mDimMipmaps; |
| 338 | boolean mDimFaces; |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 339 | int mYuv; |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame] | 340 | int[] mArray = new int[mMaxArrays]; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 341 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 342 | Element mElement; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 343 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 344 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 345 | * Create a new builder object. |
| 346 | * |
| 347 | * @param rs |
| 348 | * @param e The element for the type to be created. |
| 349 | */ |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 350 | public Builder(RenderScript rs, Element e) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 351 | e.checkValid(); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 352 | mRS = rs; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 353 | mElement = e; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 356 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 357 | * Add a dimension to the Type. |
| 358 | * |
| 359 | * |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 360 | * @param value |
| 361 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 362 | public Builder setX(int value) { |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 363 | if(value < 1) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 364 | throw new RSIllegalArgumentException("Values of less than 1 for Dimension X are not valid."); |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 365 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 366 | mDimX = value; |
| 367 | return this; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 370 | public Builder setY(int value) { |
| 371 | if(value < 1) { |
| 372 | throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid."); |
| 373 | } |
| 374 | mDimY = value; |
| 375 | return this; |
| 376 | } |
| 377 | |
Jason Sams | d1c306a | 2012-12-27 20:26:41 -0800 | [diff] [blame] | 378 | public Builder setZ(int value) { |
| 379 | if(value < 1) { |
| 380 | throw new RSIllegalArgumentException("Values of less than 1 for Dimension Z are not valid."); |
| 381 | } |
| 382 | mDimZ = value; |
| 383 | return this; |
| 384 | } |
| 385 | |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame] | 386 | /** |
Jason Sams | d016266 | 2015-04-15 17:18:10 -0700 | [diff] [blame] | 387 | * @hide |
Jason Sams | 6a420b5 | 2015-03-30 15:31:26 -0700 | [diff] [blame] | 388 | * Adds an array dimension to the builder |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame] | 389 | * |
| 390 | * @param dim |
| 391 | * @param value |
| 392 | * |
| 393 | * @return Builder |
| 394 | */ |
| 395 | public Builder setArray(int dim, int value) { |
| 396 | if(dim < 0 || dim >= mMaxArrays) { |
| 397 | throw new RSIllegalArgumentException("Array dimension out of range."); |
| 398 | } |
| 399 | mArray[dim] = value; |
| 400 | return this; |
| 401 | } |
| 402 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 403 | public Builder setMipmaps(boolean value) { |
| 404 | mDimMipmaps = value; |
| 405 | return this; |
| 406 | } |
| 407 | |
| 408 | public Builder setFaces(boolean value) { |
| 409 | mDimFaces = value; |
| 410 | return this; |
| 411 | } |
| 412 | |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 413 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 414 | * Set the YUV layout for a Type. |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 415 | * |
Eino-Ville Talvala | ccadaf1 | 2013-08-14 10:36:30 -0700 | [diff] [blame] | 416 | * @param yuvFormat {@link android.graphics.ImageFormat#YV12}, {@link android.graphics.ImageFormat#NV21}, or |
| 417 | * {@link android.graphics.ImageFormat#YUV_420_888}. |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 418 | */ |
| 419 | public Builder setYuvFormat(int yuvFormat) { |
| 420 | switch (yuvFormat) { |
| 421 | case android.graphics.ImageFormat.NV21: |
| 422 | case android.graphics.ImageFormat.YV12: |
Eino-Ville Talvala | ccadaf1 | 2013-08-14 10:36:30 -0700 | [diff] [blame] | 423 | case android.graphics.ImageFormat.YUV_420_888: |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 424 | break; |
| 425 | |
| 426 | default: |
Eino-Ville Talvala | ccadaf1 | 2013-08-14 10:36:30 -0700 | [diff] [blame] | 427 | throw new RSIllegalArgumentException( |
| 428 | "Only ImageFormat.NV21, .YV12, and .YUV_420_888 are supported.."); |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | mYuv = yuvFormat; |
| 432 | return this; |
| 433 | } |
| 434 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 435 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 436 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 437 | * Validate structure and create a new Type. |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 438 | * |
| 439 | * @return Type |
| 440 | */ |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 441 | public Type create() { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 442 | if (mDimZ > 0) { |
| 443 | if ((mDimX < 1) || (mDimY < 1)) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 444 | throw new RSInvalidStateException("Both X and Y dimension required when Z is present."); |
| 445 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 446 | if (mDimFaces) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 447 | throw new RSInvalidStateException("Cube maps not supported with 3D types."); |
| 448 | } |
| 449 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 450 | if (mDimY > 0) { |
| 451 | if (mDimX < 1) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 452 | throw new RSInvalidStateException("X dimension required when Y is present."); |
| 453 | } |
| 454 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 455 | if (mDimFaces) { |
| 456 | if (mDimY < 1) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 457 | throw new RSInvalidStateException("Cube maps require 2D Types."); |
| 458 | } |
| 459 | } |
| 460 | |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 461 | if (mYuv != 0) { |
| 462 | if ((mDimZ != 0) || mDimFaces || mDimMipmaps) { |
| 463 | throw new RSInvalidStateException("YUV only supports basic 2D."); |
| 464 | } |
| 465 | } |
| 466 | |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame] | 467 | int[] arrays = null; |
| 468 | for (int ct = mMaxArrays - 1; ct >= 0; ct--) { |
| 469 | if (mArray[ct] != 0 && arrays == null) { |
| 470 | arrays = new int[ct]; |
| 471 | } |
| 472 | if ((mArray[ct] == 0) && (arrays != null)) { |
| 473 | throw new RSInvalidStateException("Array dimensions must be contigous from 0."); |
| 474 | } |
| 475 | } |
| 476 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 477 | long id = mRS.nTypeCreate(mElement.getID(mRS), |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 478 | mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces, mYuv); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 479 | Type t = new Type(id, mRS); |
| 480 | t.mElement = mElement; |
| 481 | t.mDimX = mDimX; |
| 482 | t.mDimY = mDimY; |
| 483 | t.mDimZ = mDimZ; |
| 484 | t.mDimMipmaps = mDimMipmaps; |
| 485 | t.mDimFaces = mDimFaces; |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 486 | t.mDimYuv = mYuv; |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame] | 487 | t.mArrays = arrays; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 488 | |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 489 | t.calcElementCount(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 490 | return t; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
| 494 | } |