Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 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; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 21 | import android.util.Log; |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 22 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 23 | /** |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 24 | * <p>Type is an allocation template. It consists of an Element and one or more |
| 25 | * dimensions. It describes only the layout of memory but does not allocate any |
| 26 | * storage for the data that is described.</p> |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 27 | * |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 28 | * <p>A Type consists of several dimensions. Those are X, Y, Z, LOD (level of |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 29 | * detail), Faces (faces of a cube map). The X,Y,Z dimensions can be assigned |
| 30 | * any positive integral value within the constraints of available memory. A |
| 31 | * single dimension allocation would have an X dimension of greater than zero |
| 32 | * while the Y and Z dimensions would be zero to indicate not present. In this |
| 33 | * regard an allocation of x=10, y=1 would be considered 2 dimensionsal while |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 34 | * x=10, y=0 would be considered 1 dimensional.</p> |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 35 | * |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 36 | * <p>The LOD and Faces dimensions are booleans to indicate present or not present.</p> |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 37 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 38 | * <div class="special reference"> |
| 39 | * <h3>Developer Guides</h3> |
| 40 | * <p>For more information about creating an application that uses Renderscript, read the |
| 41 | * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p> |
| 42 | * </div> |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 43 | **/ |
| 44 | public class Type extends BaseObj { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 45 | int mDimX; |
| 46 | int mDimY; |
| 47 | int mDimZ; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 48 | boolean mDimMipmaps; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 49 | boolean mDimFaces; |
Jason Sams | 8140d7b | 2012-12-13 17:01:09 -0800 | [diff] [blame^] | 50 | int mDimYuv; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 51 | int mElementCount; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 52 | Element mElement; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 53 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 54 | public enum CubemapFace { |
Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 55 | POSITIVE_X (0), |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 56 | NEGATIVE_X (1), |
Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 57 | POSITIVE_Y (2), |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 58 | NEGATIVE_Y (3), |
Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 59 | POSITIVE_Z (4), |
| 60 | NEGATIVE_Z (5), |
| 61 | @Deprecated |
| 62 | POSITVE_X (0), |
| 63 | @Deprecated |
| 64 | POSITVE_Y (2), |
| 65 | @Deprecated |
| 66 | POSITVE_Z (4); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 67 | |
| 68 | int mID; |
| 69 | CubemapFace(int id) { |
| 70 | mID = id; |
| 71 | } |
| 72 | } |
| 73 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 74 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 75 | * Return the element associated with this Type. |
| 76 | * |
| 77 | * @return Element |
| 78 | */ |
Jason Sams | e17964e | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 79 | public Element getElement() { |
| 80 | return mElement; |
| 81 | } |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 82 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 83 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 84 | * Return the value of the X dimension. |
| 85 | * |
| 86 | * @return int |
| 87 | */ |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 88 | public int getX() { |
| 89 | return mDimX; |
| 90 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 91 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 92 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 93 | * Return the value of the Y dimension or 0 for a 1D allocation. |
| 94 | * |
| 95 | * @return int |
| 96 | */ |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 97 | public int getY() { |
| 98 | return mDimY; |
| 99 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 100 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 101 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 102 | * Return the value of the Z dimension or 0 for a 1D or 2D allocation. |
| 103 | * |
| 104 | * @return int |
| 105 | */ |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 106 | public int getZ() { |
| 107 | return mDimZ; |
| 108 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 109 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 110 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 111 | * Return if the Type has a mipmap chain. |
| 112 | * |
| 113 | * @return boolean |
| 114 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 115 | public boolean hasMipmaps() { |
| 116 | return mDimMipmaps; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 117 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 118 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 119 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 120 | * Return if the Type is a cube map. |
| 121 | * |
| 122 | * @return boolean |
| 123 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 124 | public boolean hasFaces() { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 125 | return mDimFaces; |
| 126 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 127 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 128 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 129 | * Return the total number of accessable cells in the Type. |
| 130 | * |
| 131 | * @return int |
| 132 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 133 | public int getCount() { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 134 | return mElementCount; |
| 135 | } |
| 136 | |
| 137 | void calcElementCount() { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 138 | boolean hasLod = hasMipmaps(); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 139 | int x = getX(); |
| 140 | int y = getY(); |
| 141 | int z = getZ(); |
| 142 | int faces = 1; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 143 | if (hasFaces()) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 144 | faces = 6; |
| 145 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 146 | if (x == 0) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 147 | x = 1; |
| 148 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 149 | if (y == 0) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 150 | y = 1; |
| 151 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 152 | if (z == 0) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 153 | z = 1; |
| 154 | } |
| 155 | |
| 156 | int count = x * y * z * faces; |
Alex Sakhartchouk | 9ea30a6 | 2011-03-02 12:33:50 -0800 | [diff] [blame] | 157 | |
| 158 | while (hasLod && ((x > 1) || (y > 1) || (z > 1))) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 159 | if(x > 1) { |
| 160 | x >>= 1; |
| 161 | } |
| 162 | if(y > 1) { |
| 163 | y >>= 1; |
| 164 | } |
| 165 | if(z > 1) { |
| 166 | z >>= 1; |
| 167 | } |
| 168 | |
| 169 | count += x * y * z * faces; |
| 170 | } |
| 171 | mElementCount = count; |
| 172 | } |
| 173 | |
| 174 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 175 | Type(int id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 176 | super(id, rs); |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 179 | @Override |
| 180 | void updateFromNative() { |
| 181 | // We have 6 integer to obtain mDimX; mDimY; mDimZ; |
| 182 | // mDimLOD; mDimFaces; mElement; |
| 183 | int[] dataBuffer = new int[6]; |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 184 | mRS.nTypeGetNativeData(getID(mRS), dataBuffer); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 185 | |
| 186 | mDimX = dataBuffer[0]; |
| 187 | mDimY = dataBuffer[1]; |
| 188 | mDimZ = dataBuffer[2]; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 189 | mDimMipmaps = dataBuffer[3] == 1 ? true : false; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 190 | mDimFaces = dataBuffer[4] == 1 ? true : false; |
| 191 | |
| 192 | int elementID = dataBuffer[5]; |
| 193 | if(elementID != 0) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 194 | mElement = new Element(elementID, mRS); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 195 | mElement.updateFromNative(); |
| 196 | } |
| 197 | calcElementCount(); |
| 198 | } |
| 199 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 200 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 201 | * Builder class for Type. |
| 202 | * |
| 203 | */ |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 204 | public static class Builder { |
| 205 | RenderScript mRS; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 206 | int mDimX = 1; |
| 207 | int mDimY; |
| 208 | int mDimZ; |
| 209 | boolean mDimMipmaps; |
| 210 | boolean mDimFaces; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 211 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 212 | Element mElement; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 213 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 214 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 215 | * Create a new builder object. |
| 216 | * |
| 217 | * @param rs |
| 218 | * @param e The element for the type to be created. |
| 219 | */ |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 220 | public Builder(RenderScript rs, Element e) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 221 | e.checkValid(); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 222 | mRS = rs; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 223 | mElement = e; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 226 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 227 | * Add a dimension to the Type. |
| 228 | * |
| 229 | * |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 230 | * @param value |
| 231 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 232 | public Builder setX(int value) { |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 233 | if(value < 1) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 234 | 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] | 235 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 236 | mDimX = value; |
| 237 | return this; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 240 | public Builder setY(int value) { |
| 241 | if(value < 1) { |
| 242 | throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid."); |
| 243 | } |
| 244 | mDimY = value; |
| 245 | return this; |
| 246 | } |
| 247 | |
Jason Sams | d1c306a | 2012-12-27 20:26:41 -0800 | [diff] [blame] | 248 | public Builder setZ(int value) { |
| 249 | if(value < 1) { |
| 250 | throw new RSIllegalArgumentException("Values of less than 1 for Dimension Z are not valid."); |
| 251 | } |
| 252 | mDimZ = value; |
| 253 | return this; |
| 254 | } |
| 255 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 256 | public Builder setMipmaps(boolean value) { |
| 257 | mDimMipmaps = value; |
| 258 | return this; |
| 259 | } |
| 260 | |
| 261 | public Builder setFaces(boolean value) { |
| 262 | mDimFaces = value; |
| 263 | return this; |
| 264 | } |
| 265 | |
| 266 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 267 | /** |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 268 | * Validate structure and create a new type. |
| 269 | * |
| 270 | * @return Type |
| 271 | */ |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 272 | public Type create() { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 273 | if (mDimZ > 0) { |
| 274 | if ((mDimX < 1) || (mDimY < 1)) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 275 | throw new RSInvalidStateException("Both X and Y dimension required when Z is present."); |
| 276 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 277 | if (mDimFaces) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 278 | throw new RSInvalidStateException("Cube maps not supported with 3D types."); |
| 279 | } |
| 280 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 281 | if (mDimY > 0) { |
| 282 | if (mDimX < 1) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 283 | throw new RSInvalidStateException("X dimension required when Y is present."); |
| 284 | } |
| 285 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 286 | if (mDimFaces) { |
| 287 | if (mDimY < 1) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 288 | throw new RSInvalidStateException("Cube maps require 2D Types."); |
| 289 | } |
| 290 | } |
| 291 | |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 292 | int id = mRS.nTypeCreate(mElement.getID(mRS), |
| 293 | mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 294 | Type t = new Type(id, mRS); |
| 295 | t.mElement = mElement; |
| 296 | t.mDimX = mDimX; |
| 297 | t.mDimY = mDimY; |
| 298 | t.mDimZ = mDimZ; |
| 299 | t.mDimMipmaps = mDimMipmaps; |
| 300 | t.mDimFaces = mDimFaces; |
| 301 | |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 302 | t.calcElementCount(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 303 | return t; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | } |