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