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 | /** |
| 153 | * @hide |
| 154 | */ |
| 155 | public int getArray(int dim) { |
| 156 | if ((dim < 0) || (dim >= mMaxArrays)) { |
| 157 | throw new RSIllegalArgumentException("Array dimension out of range."); |
| 158 | } |
| 159 | |
| 160 | if (mArrays == null || dim >= mArrays.length) { |
| 161 | // Dimension in range but no array for that dimension allocated |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | return mArrays[dim]; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @hide |
| 170 | */ |
| 171 | public int getArrayCount() { |
| 172 | if (mArrays != null) return mArrays.length; |
| 173 | return 0; |
| 174 | } |
| 175 | |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 176 | void calcElementCount() { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 177 | boolean hasLod = hasMipmaps(); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 178 | int x = getX(); |
| 179 | int y = getY(); |
| 180 | int z = getZ(); |
| 181 | int faces = 1; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 182 | if (hasFaces()) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 183 | faces = 6; |
| 184 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 185 | if (x == 0) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 186 | x = 1; |
| 187 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 188 | if (y == 0) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 189 | y = 1; |
| 190 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 191 | if (z == 0) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 192 | z = 1; |
| 193 | } |
| 194 | |
| 195 | int count = x * y * z * faces; |
Alex Sakhartchouk | 9ea30a6 | 2011-03-02 12:33:50 -0800 | [diff] [blame] | 196 | |
| 197 | while (hasLod && ((x > 1) || (y > 1) || (z > 1))) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 198 | if(x > 1) { |
| 199 | x >>= 1; |
| 200 | } |
| 201 | if(y > 1) { |
| 202 | y >>= 1; |
| 203 | } |
| 204 | if(z > 1) { |
| 205 | z >>= 1; |
| 206 | } |
| 207 | |
| 208 | count += x * y * z * faces; |
| 209 | } |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame^] | 210 | |
| 211 | if (mArrays != null) { |
| 212 | for (int ct = 0; ct < mArrays.length; ct++) { |
| 213 | count *= mArrays[ct]; |
| 214 | } |
| 215 | } |
| 216 | |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 217 | mElementCount = count; |
| 218 | } |
| 219 | |
| 220 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 221 | Type(long id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 222 | super(id, rs); |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 225 | @Override |
| 226 | void updateFromNative() { |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 227 | // We have 6 integer/long to obtain mDimX; mDimY; mDimZ; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 228 | // mDimLOD; mDimFaces; mElement; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 229 | long[] dataBuffer = new long[6]; |
| 230 | mRS.nTypeGetNativeData(getID(mRS), dataBuffer); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 231 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 232 | mDimX = (int)dataBuffer[0]; |
| 233 | mDimY = (int)dataBuffer[1]; |
| 234 | mDimZ = (int)dataBuffer[2]; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 235 | mDimMipmaps = dataBuffer[3] == 1 ? true : false; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 236 | mDimFaces = dataBuffer[4] == 1 ? true : false; |
| 237 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 238 | long elementID = dataBuffer[5]; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 239 | if(elementID != 0) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 240 | mElement = new Element(elementID, mRS); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 241 | mElement.updateFromNative(); |
| 242 | } |
| 243 | calcElementCount(); |
| 244 | } |
| 245 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 246 | /** |
Jason Sams | ec44e5d | 2013-10-09 17:15:36 -0700 | [diff] [blame] | 247 | * Utility function for creating basic 1D types. The type is |
| 248 | * created without mipmaps enabled. |
| 249 | * |
| 250 | * @param rs The RenderScript context |
| 251 | * @param e The Element for the Type |
| 252 | * @param dimX The X dimension, must be > 0 |
| 253 | * |
| 254 | * @return Type |
| 255 | */ |
| 256 | static public Type createX(RenderScript rs, Element e, int dimX) { |
| 257 | if (dimX < 1) { |
| 258 | throw new RSInvalidStateException("Dimension must be >= 1."); |
| 259 | } |
| 260 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 261 | 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] | 262 | Type t = new Type(id, rs); |
| 263 | t.mElement = e; |
| 264 | t.mDimX = dimX; |
| 265 | t.calcElementCount(); |
| 266 | return t; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Utility function for creating basic 2D types. The type is |
| 271 | * created without mipmaps or cubemaps. |
| 272 | * |
| 273 | * @param rs The RenderScript context |
| 274 | * @param e The Element for the Type |
| 275 | * @param dimX The X dimension, must be > 0 |
| 276 | * @param dimY The Y dimension, must be > 0 |
| 277 | * |
| 278 | * @return Type |
| 279 | */ |
| 280 | static public Type createXY(RenderScript rs, Element e, int dimX, int dimY) { |
| 281 | if ((dimX < 1) || (dimY < 1)) { |
| 282 | throw new RSInvalidStateException("Dimension must be >= 1."); |
| 283 | } |
| 284 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 285 | 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] | 286 | Type t = new Type(id, rs); |
| 287 | t.mElement = e; |
| 288 | t.mDimX = dimX; |
| 289 | t.mDimY = dimY; |
| 290 | t.calcElementCount(); |
| 291 | return t; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Utility function for creating basic 3D types. The type is |
| 296 | * created without mipmaps. |
| 297 | * |
| 298 | * @param rs The RenderScript context |
| 299 | * @param e The Element for the Type |
| 300 | * @param dimX The X dimension, must be > 0 |
| 301 | * @param dimY The Y dimension, must be > 0 |
| 302 | * @param dimZ The Z dimension, must be > 0 |
| 303 | * |
| 304 | * @return Type |
| 305 | */ |
| 306 | static public Type createXYZ(RenderScript rs, Element e, int dimX, int dimY, int dimZ) { |
| 307 | if ((dimX < 1) || (dimY < 1) || (dimZ < 1)) { |
| 308 | throw new RSInvalidStateException("Dimension must be >= 1."); |
| 309 | } |
| 310 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 311 | 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] | 312 | Type t = new Type(id, rs); |
| 313 | t.mElement = e; |
| 314 | t.mDimX = dimX; |
| 315 | t.mDimY = dimY; |
| 316 | t.mDimZ = dimZ; |
| 317 | t.calcElementCount(); |
| 318 | return t; |
| 319 | } |
| 320 | |
| 321 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 322 | * Builder class for Type. |
| 323 | * |
| 324 | */ |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 325 | public static class Builder { |
| 326 | RenderScript mRS; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 327 | int mDimX = 1; |
| 328 | int mDimY; |
| 329 | int mDimZ; |
| 330 | boolean mDimMipmaps; |
| 331 | boolean mDimFaces; |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 332 | int mYuv; |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame^] | 333 | int[] mArray = new int[mMaxArrays]; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 334 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 335 | Element mElement; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 336 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 337 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 338 | * Create a new builder object. |
| 339 | * |
| 340 | * @param rs |
| 341 | * @param e The element for the type to be created. |
| 342 | */ |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 343 | public Builder(RenderScript rs, Element e) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 344 | e.checkValid(); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 345 | mRS = rs; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 346 | mElement = e; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 349 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 350 | * Add a dimension to the Type. |
| 351 | * |
| 352 | * |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 353 | * @param value |
| 354 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 355 | public Builder setX(int value) { |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 356 | if(value < 1) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 357 | 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] | 358 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 359 | mDimX = value; |
| 360 | return this; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 363 | public Builder setY(int value) { |
| 364 | if(value < 1) { |
| 365 | throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid."); |
| 366 | } |
| 367 | mDimY = value; |
| 368 | return this; |
| 369 | } |
| 370 | |
Jason Sams | d1c306a | 2012-12-27 20:26:41 -0800 | [diff] [blame] | 371 | public Builder setZ(int value) { |
| 372 | if(value < 1) { |
| 373 | throw new RSIllegalArgumentException("Values of less than 1 for Dimension Z are not valid."); |
| 374 | } |
| 375 | mDimZ = value; |
| 376 | return this; |
| 377 | } |
| 378 | |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame^] | 379 | /** |
| 380 | * @hide |
| 381 | * |
| 382 | * @param dim |
| 383 | * @param value |
| 384 | * |
| 385 | * @return Builder |
| 386 | */ |
| 387 | public Builder setArray(int dim, int value) { |
| 388 | if(dim < 0 || dim >= mMaxArrays) { |
| 389 | throw new RSIllegalArgumentException("Array dimension out of range."); |
| 390 | } |
| 391 | mArray[dim] = value; |
| 392 | return this; |
| 393 | } |
| 394 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 395 | public Builder setMipmaps(boolean value) { |
| 396 | mDimMipmaps = value; |
| 397 | return this; |
| 398 | } |
| 399 | |
| 400 | public Builder setFaces(boolean value) { |
| 401 | mDimFaces = value; |
| 402 | return this; |
| 403 | } |
| 404 | |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 405 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 406 | * Set the YUV layout for a Type. |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 407 | * |
Eino-Ville Talvala | ccadaf1 | 2013-08-14 10:36:30 -0700 | [diff] [blame] | 408 | * @param yuvFormat {@link android.graphics.ImageFormat#YV12}, {@link android.graphics.ImageFormat#NV21}, or |
| 409 | * {@link android.graphics.ImageFormat#YUV_420_888}. |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 410 | */ |
| 411 | public Builder setYuvFormat(int yuvFormat) { |
| 412 | switch (yuvFormat) { |
| 413 | case android.graphics.ImageFormat.NV21: |
| 414 | case android.graphics.ImageFormat.YV12: |
Eino-Ville Talvala | ccadaf1 | 2013-08-14 10:36:30 -0700 | [diff] [blame] | 415 | case android.graphics.ImageFormat.YUV_420_888: |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 416 | break; |
| 417 | |
| 418 | default: |
Eino-Ville Talvala | ccadaf1 | 2013-08-14 10:36:30 -0700 | [diff] [blame] | 419 | throw new RSIllegalArgumentException( |
| 420 | "Only ImageFormat.NV21, .YV12, and .YUV_420_888 are supported.."); |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | mYuv = yuvFormat; |
| 424 | return this; |
| 425 | } |
| 426 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 427 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 428 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 429 | * Validate structure and create a new Type. |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 430 | * |
| 431 | * @return Type |
| 432 | */ |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 433 | public Type create() { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 434 | if (mDimZ > 0) { |
| 435 | if ((mDimX < 1) || (mDimY < 1)) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 436 | throw new RSInvalidStateException("Both X and Y dimension required when Z is present."); |
| 437 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 438 | if (mDimFaces) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 439 | throw new RSInvalidStateException("Cube maps not supported with 3D types."); |
| 440 | } |
| 441 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 442 | if (mDimY > 0) { |
| 443 | if (mDimX < 1) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 444 | throw new RSInvalidStateException("X dimension required when Y is present."); |
| 445 | } |
| 446 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 447 | if (mDimFaces) { |
| 448 | if (mDimY < 1) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 449 | throw new RSInvalidStateException("Cube maps require 2D Types."); |
| 450 | } |
| 451 | } |
| 452 | |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 453 | if (mYuv != 0) { |
| 454 | if ((mDimZ != 0) || mDimFaces || mDimMipmaps) { |
| 455 | throw new RSInvalidStateException("YUV only supports basic 2D."); |
| 456 | } |
| 457 | } |
| 458 | |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame^] | 459 | int[] arrays = null; |
| 460 | for (int ct = mMaxArrays - 1; ct >= 0; ct--) { |
| 461 | if (mArray[ct] != 0 && arrays == null) { |
| 462 | arrays = new int[ct]; |
| 463 | } |
| 464 | if ((mArray[ct] == 0) && (arrays != null)) { |
| 465 | throw new RSInvalidStateException("Array dimensions must be contigous from 0."); |
| 466 | } |
| 467 | } |
| 468 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 469 | long id = mRS.nTypeCreate(mElement.getID(mRS), |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 470 | mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces, mYuv); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 471 | Type t = new Type(id, mRS); |
| 472 | t.mElement = mElement; |
| 473 | t.mDimX = mDimX; |
| 474 | t.mDimY = mDimY; |
| 475 | t.mDimZ = mDimZ; |
| 476 | t.mDimMipmaps = mDimMipmaps; |
| 477 | t.mDimFaces = mDimFaces; |
Jason Sams | b109cc7 | 2013-01-07 18:20:12 -0800 | [diff] [blame] | 478 | t.mDimYuv = mYuv; |
Jason Sams | 46ba27e3 | 2015-02-06 17:45:15 -0800 | [diff] [blame^] | 479 | t.mArrays = arrays; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 480 | |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 481 | t.calcElementCount(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 482 | return t; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
| 486 | } |