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