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