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 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -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; |
| 50 | int mElementCount; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 51 | Element mElement; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 52 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 53 | public enum CubemapFace { |
Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 54 | POSITIVE_X (0), |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 55 | NEGATIVE_X (1), |
Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 56 | POSITIVE_Y (2), |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 57 | NEGATIVE_Y (3), |
Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 58 | POSITIVE_Z (4), |
| 59 | NEGATIVE_Z (5), |
| 60 | @Deprecated |
| 61 | POSITVE_X (0), |
| 62 | @Deprecated |
| 63 | POSITVE_Y (2), |
| 64 | @Deprecated |
| 65 | POSITVE_Z (4); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 66 | |
| 67 | int mID; |
| 68 | CubemapFace(int id) { |
| 69 | mID = id; |
| 70 | } |
| 71 | } |
| 72 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 73 | /** |
| 74 | * Return the element associated with this Type. |
| 75 | * |
| 76 | * @return Element |
| 77 | */ |
Jason Sams | e17964e | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 78 | public Element getElement() { |
| 79 | return mElement; |
| 80 | } |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 81 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Return the value of the X dimension. |
| 84 | * |
| 85 | * @return int |
| 86 | */ |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 87 | public int getX() { |
| 88 | return mDimX; |
| 89 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Return the value of the Y dimension or 0 for a 1D allocation. |
| 93 | * |
| 94 | * @return int |
| 95 | */ |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 96 | public int getY() { |
| 97 | return mDimY; |
| 98 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Return the value of the Z dimension or 0 for a 1D or 2D allocation. |
| 102 | * |
| 103 | * @return int |
| 104 | */ |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 105 | public int getZ() { |
| 106 | return mDimZ; |
| 107 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Return if the Type has a mipmap chain. |
| 111 | * |
| 112 | * @return boolean |
| 113 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 114 | public boolean hasMipmaps() { |
| 115 | return mDimMipmaps; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 116 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Return if the Type is a cube map. |
| 120 | * |
| 121 | * @return boolean |
| 122 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 123 | public boolean hasFaces() { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 124 | return mDimFaces; |
| 125 | } |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * Return the total number of accessable cells in the Type. |
| 129 | * |
| 130 | * @return int |
| 131 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 132 | public int getCount() { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 133 | return mElementCount; |
| 134 | } |
| 135 | |
| 136 | void calcElementCount() { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 137 | boolean hasLod = hasMipmaps(); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 138 | int x = getX(); |
| 139 | int y = getY(); |
| 140 | int z = getZ(); |
| 141 | int faces = 1; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 142 | if (hasFaces()) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 143 | faces = 6; |
| 144 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 145 | if (x == 0) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 146 | x = 1; |
| 147 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 148 | if (y == 0) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 149 | y = 1; |
| 150 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 151 | if (z == 0) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 152 | z = 1; |
| 153 | } |
| 154 | |
| 155 | int count = x * y * z * faces; |
Alex Sakhartchouk | 9ea30a6 | 2011-03-02 12:33:50 -0800 | [diff] [blame] | 156 | |
| 157 | while (hasLod && ((x > 1) || (y > 1) || (z > 1))) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 158 | if(x > 1) { |
| 159 | x >>= 1; |
| 160 | } |
| 161 | if(y > 1) { |
| 162 | y >>= 1; |
| 163 | } |
| 164 | if(z > 1) { |
| 165 | z >>= 1; |
| 166 | } |
| 167 | |
| 168 | count += x * y * z * faces; |
| 169 | } |
| 170 | mElementCount = count; |
| 171 | } |
| 172 | |
| 173 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 174 | Type(int id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 175 | super(id, rs); |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 178 | @Override |
| 179 | void updateFromNative() { |
| 180 | // We have 6 integer to obtain mDimX; mDimY; mDimZ; |
| 181 | // mDimLOD; mDimFaces; mElement; |
| 182 | int[] dataBuffer = new int[6]; |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 183 | mRS.nTypeGetNativeData(getID(), dataBuffer); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 184 | |
| 185 | mDimX = dataBuffer[0]; |
| 186 | mDimY = dataBuffer[1]; |
| 187 | mDimZ = dataBuffer[2]; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 188 | mDimMipmaps = dataBuffer[3] == 1 ? true : false; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 189 | mDimFaces = dataBuffer[4] == 1 ? true : false; |
| 190 | |
| 191 | int elementID = dataBuffer[5]; |
| 192 | if(elementID != 0) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 193 | mElement = new Element(elementID, mRS); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 194 | mElement.updateFromNative(); |
| 195 | } |
| 196 | calcElementCount(); |
| 197 | } |
| 198 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 199 | /** |
| 200 | * Builder class for Type. |
| 201 | * |
| 202 | */ |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 203 | public static class Builder { |
| 204 | RenderScript mRS; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 205 | int mDimX = 1; |
| 206 | int mDimY; |
| 207 | int mDimZ; |
| 208 | boolean mDimMipmaps; |
| 209 | boolean mDimFaces; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 210 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 211 | Element mElement; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 212 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 213 | /** |
| 214 | * Create a new builder object. |
| 215 | * |
| 216 | * @param rs |
| 217 | * @param e The element for the type to be created. |
| 218 | */ |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 219 | public Builder(RenderScript rs, Element e) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 220 | e.checkValid(); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 221 | mRS = rs; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 222 | mElement = e; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 225 | /** |
| 226 | * Add a dimension to the Type. |
| 227 | * |
| 228 | * |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 229 | * @param value |
| 230 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 231 | public Builder setX(int value) { |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 232 | if(value < 1) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 233 | 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] | 234 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 235 | mDimX = value; |
| 236 | return this; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 239 | public Builder setY(int value) { |
| 240 | if(value < 1) { |
| 241 | throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid."); |
| 242 | } |
| 243 | mDimY = value; |
| 244 | return this; |
| 245 | } |
| 246 | |
| 247 | public Builder setMipmaps(boolean value) { |
| 248 | mDimMipmaps = value; |
| 249 | return this; |
| 250 | } |
| 251 | |
| 252 | public Builder setFaces(boolean value) { |
| 253 | mDimFaces = value; |
| 254 | return this; |
| 255 | } |
| 256 | |
| 257 | |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 258 | /** |
| 259 | * Validate structure and create a new type. |
| 260 | * |
| 261 | * @return Type |
| 262 | */ |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 263 | public Type create() { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 264 | if (mDimZ > 0) { |
| 265 | if ((mDimX < 1) || (mDimY < 1)) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 266 | throw new RSInvalidStateException("Both X and Y dimension required when Z is present."); |
| 267 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 268 | if (mDimFaces) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 269 | throw new RSInvalidStateException("Cube maps not supported with 3D types."); |
| 270 | } |
| 271 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 272 | if (mDimY > 0) { |
| 273 | if (mDimX < 1) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 274 | throw new RSInvalidStateException("X dimension required when Y is present."); |
| 275 | } |
| 276 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 277 | if (mDimFaces) { |
| 278 | if (mDimY < 1) { |
Jason Sams | a1b13ed | 2010-11-12 14:58:37 -0800 | [diff] [blame] | 279 | throw new RSInvalidStateException("Cube maps require 2D Types."); |
| 280 | } |
| 281 | } |
| 282 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 283 | int id = mRS.nTypeCreate(mElement.getID(), mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces); |
| 284 | Type t = new Type(id, mRS); |
| 285 | t.mElement = mElement; |
| 286 | t.mDimX = mDimX; |
| 287 | t.mDimY = mDimY; |
| 288 | t.mDimZ = mDimZ; |
| 289 | t.mDimMipmaps = mDimMipmaps; |
| 290 | t.mDimFaces = mDimFaces; |
| 291 | |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 292 | t.calcElementCount(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 293 | return t; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | } |