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 19 /** 20 * Only intended for use by generated reflected code. 21 * 22 * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a 23 * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration 24 * guide</a> for the proposed alternatives. 25 **/ 26 @Deprecated 27 public class AllocationAdapter extends Allocation { 28 Type mWindow; 29 AllocationAdapter(long id, RenderScript rs, Allocation alloc, Type t)30 AllocationAdapter(long id, RenderScript rs, Allocation alloc, Type t) { 31 super(id, rs, alloc.mType, alloc.mUsage); 32 mAdaptedAllocation = alloc; 33 mWindow = t; 34 } 35 36 /* 37 long getID(RenderScript rs) { 38 throw new RSInvalidStateException( 39 "This operation is not supported with adapters at this time."); 40 } 41 */ 42 initLOD(int lod)43 void initLOD(int lod) { 44 if (lod < 0) { 45 throw new RSIllegalArgumentException("Attempting to set negative lod (" + lod + ")."); 46 } 47 48 int tx = mAdaptedAllocation.mType.getX(); 49 int ty = mAdaptedAllocation.mType.getY(); 50 int tz = mAdaptedAllocation.mType.getZ(); 51 52 for (int ct=0; ct < lod; ct++) { 53 if ((tx==1) && (ty == 1) && (tz == 1)) { 54 throw new RSIllegalArgumentException("Attempting to set lod (" + lod + ") out of range."); 55 } 56 57 if (tx > 1) tx >>= 1; 58 if (ty > 1) ty >>= 1; 59 if (tz > 1) tz >>= 1; 60 } 61 62 mCurrentDimX = tx; 63 mCurrentDimY = ty; 64 mCurrentDimZ = tz; 65 mCurrentCount = mCurrentDimX; 66 if (mCurrentDimY > 1) { 67 mCurrentCount *= mCurrentDimY; 68 } 69 if (mCurrentDimZ > 1) { 70 mCurrentCount *= mCurrentDimZ; 71 } 72 mSelectedY = 0; 73 mSelectedZ = 0; 74 } 75 updateOffsets()76 private void updateOffsets() { 77 int a1 = 0, a2 = 0, a3 = 0, a4 = 0; 78 79 if (mSelectedArray != null) { 80 if (mSelectedArray.length > 0) { 81 a1 = mSelectedArray[0]; 82 } 83 if (mSelectedArray.length > 1) { 84 a2 = mSelectedArray[2]; 85 } 86 if (mSelectedArray.length > 2) { 87 a3 = mSelectedArray[2]; 88 } 89 if (mSelectedArray.length > 3) { 90 a4 = mSelectedArray[3]; 91 } 92 } 93 mRS.nAllocationAdapterOffset(getID(mRS), mSelectedX, mSelectedY, mSelectedZ, 94 mSelectedLOD, mSelectedFace.mID, a1, a2, a3, a4); 95 96 } 97 98 /** 99 * Set the active LOD. The LOD must be within the range for the 100 * type being adapted. The base allocation must have mipmaps. 101 * 102 * Because this changes the dimensions of the adapter the 103 * current Y and Z will be reset. 104 * 105 * @param lod The LOD to make active. 106 */ setLOD(int lod)107 public void setLOD(int lod) { 108 if (!mAdaptedAllocation.getType().hasMipmaps()) { 109 throw new RSInvalidStateException("Cannot set LOD when the allocation type does not include mipmaps."); 110 } 111 if (mWindow.hasMipmaps()) { 112 throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps."); 113 } 114 115 initLOD(lod); 116 mSelectedLOD = lod; 117 updateOffsets(); 118 } 119 120 /** 121 * Set the active Face. The base allocation must be of a type 122 * that includes faces. 123 * 124 * @param cf The face to make active. 125 */ setFace(Type.CubemapFace cf)126 public void setFace(Type.CubemapFace cf) { 127 if (!mAdaptedAllocation.getType().hasFaces()) { 128 throw new RSInvalidStateException("Cannot set Face when the allocation type does not include faces."); 129 } 130 if (mWindow.hasFaces()) { 131 throw new RSInvalidStateException("Cannot set face when the adapter includes faces."); 132 } 133 if (cf == null) { 134 throw new RSIllegalArgumentException("Cannot set null face."); 135 } 136 137 mSelectedFace = cf; 138 updateOffsets(); 139 } 140 141 142 /** 143 * 144 * Set the active X. The x value must be within the range for 145 * the allocation being adapted. 146 * 147 * @param x The x to make active. 148 */ setX(int x)149 public void setX(int x) { 150 if (mAdaptedAllocation.getType().getX() <= x) { 151 throw new RSInvalidStateException("Cannot set X greater than dimension of allocation."); 152 } 153 if (mWindow.getX() == mAdaptedAllocation.getType().getX()) { 154 throw new RSInvalidStateException("Cannot set X when the adapter includes X."); 155 } 156 if ((mWindow.getX() + x) >= mAdaptedAllocation.getType().getX()) { 157 throw new RSInvalidStateException("Cannot set (X + window) which would be larger than dimension of allocation."); 158 } 159 160 mSelectedX = x; 161 updateOffsets(); 162 } 163 164 /** 165 * Set the active Y. The y value must be within the range for 166 * the allocation being adapted. The base allocation must 167 * contain the Y dimension. 168 * 169 * @param y The y to make active. 170 */ setY(int y)171 public void setY(int y) { 172 if (mAdaptedAllocation.getType().getY() == 0) { 173 throw new RSInvalidStateException("Cannot set Y when the allocation type does not include Y dim."); 174 } 175 if (mAdaptedAllocation.getType().getY() <= y) { 176 throw new RSInvalidStateException("Cannot set Y greater than dimension of allocation."); 177 } 178 if (mWindow.getY() == mAdaptedAllocation.getType().getY()) { 179 throw new RSInvalidStateException("Cannot set Y when the adapter includes Y."); 180 } 181 if ((mWindow.getY() + y) >= mAdaptedAllocation.getType().getY()) { 182 throw new RSInvalidStateException("Cannot set (Y + window) which would be larger than dimension of allocation."); 183 } 184 185 mSelectedY = y; 186 updateOffsets(); 187 } 188 189 /** 190 * Set the active Z. The z value must be within the range for 191 * the allocation being adapted. The base allocation must 192 * contain the Z dimension. 193 * 194 * @param z The z to make active. 195 */ setZ(int z)196 public void setZ(int z) { 197 if (mAdaptedAllocation.getType().getZ() == 0) { 198 throw new RSInvalidStateException("Cannot set Z when the allocation type does not include Z dim."); 199 } 200 if (mAdaptedAllocation.getType().getZ() <= z) { 201 throw new RSInvalidStateException("Cannot set Z greater than dimension of allocation."); 202 } 203 if (mWindow.getZ() == mAdaptedAllocation.getType().getZ()) { 204 throw new RSInvalidStateException("Cannot set Z when the adapter includes Z."); 205 } 206 if ((mWindow.getZ() + z) >= mAdaptedAllocation.getType().getZ()) { 207 throw new RSInvalidStateException("Cannot set (Z + window) which would be larger than dimension of allocation."); 208 } 209 210 mSelectedZ = z; 211 updateOffsets(); 212 } 213 214 /** 215 * @hide 216 */ setArray(int arrayNum, int arrayVal)217 public void setArray(int arrayNum, int arrayVal) { 218 if (mAdaptedAllocation.getType().getArray(arrayNum) == 0) { 219 throw new RSInvalidStateException("Cannot set arrayNum when the allocation type does not include arrayNum dim."); 220 } 221 if (mAdaptedAllocation.getType().getArray(arrayNum) <= arrayVal) { 222 throw new RSInvalidStateException("Cannot set arrayNum greater than dimension of allocation."); 223 } 224 if (mWindow.getArray(arrayNum) == mAdaptedAllocation.getType().getArray(arrayNum)) { 225 throw new RSInvalidStateException("Cannot set arrayNum when the adapter includes arrayNum."); 226 } 227 if ((mWindow.getArray(arrayNum) + arrayVal) >= mAdaptedAllocation.getType().getArray(arrayNum)) { 228 throw new RSInvalidStateException("Cannot set (arrayNum + window) which would be larger than dimension of allocation."); 229 } 230 231 mSelectedArray[arrayNum] = arrayVal; 232 updateOffsets(); 233 } 234 create1D(RenderScript rs, Allocation a)235 static public AllocationAdapter create1D(RenderScript rs, Allocation a) { 236 rs.validate(); 237 Type t = Type.createX(rs, a.getElement(), a.getType().getX()); 238 return createTyped(rs, a, t); 239 } 240 241 create2D(RenderScript rs, Allocation a)242 static public AllocationAdapter create2D(RenderScript rs, Allocation a) { 243 rs.validate(); 244 Type t = Type.createXY(rs, a.getElement(), a.getType().getX(), a.getType().getY()); 245 return createTyped(rs, a, t); 246 } 247 248 /** 249 * 250 * 251 * Create an arbitrary window into the base allocation. 252 * The type describes the shape of the window. 253 * 254 * Any dimensions present in the type must be equal or smaller 255 * to the dimensions in the source allocation. A dimension 256 * present in the allocation that is not present in the type 257 * will be constrained away with the selectors. 258 * 259 * If a dimension is present in both the type and allocation, one of 260 * two things will happen. 261 * 262 * If the type is smaller than the allocation, a window will be 263 * created, the selected value in the adapter for that dimension 264 * will act as the base address, and the type will describe the 265 * size of the view starting at that point. 266 * 267 * If the type and allocation dimension are of the same size, 268 * then setting the selector for the dimension will be an error. 269 */ createTyped(RenderScript rs, Allocation a, Type t)270 static public AllocationAdapter createTyped(RenderScript rs, Allocation a, Type t) { 271 rs.validate(); 272 273 if (a.mAdaptedAllocation != null) { 274 throw new RSInvalidStateException("Adapters cannot be nested."); 275 } 276 277 if (!a.getType().getElement().equals(t.getElement())) { 278 throw new RSInvalidStateException("Element must match Allocation type."); 279 } 280 281 if (t.hasFaces() || t.hasMipmaps()) { 282 throw new RSInvalidStateException("Adapters do not support window types with Mipmaps or Faces."); 283 } 284 285 Type at = a.getType(); 286 if ((t.getX() > at.getX()) || 287 (t.getY() > at.getY()) || 288 (t.getZ() > at.getZ()) || 289 (t.getArrayCount() > at.getArrayCount())) { 290 291 throw new RSInvalidStateException("Type cannot have dimension larger than the source allocation."); 292 } 293 294 if (t.getArrayCount() > 0) { 295 for (int i = 0; i < t.getArray(i); i++) { 296 if (t.getArray(i) > at.getArray(i)) { 297 throw new RSInvalidStateException("Type cannot have dimension larger than the source allocation."); 298 } 299 } 300 } 301 302 // Create the object 303 long id = rs.nAllocationAdapterCreate(a.getID(rs), t.getID(rs)); 304 if (id == 0) { 305 throw new RSRuntimeException("AllocationAdapter creation failed."); 306 } 307 return new AllocationAdapter(id, rs, a, t); 308 } 309 310 /** 311 * Override the Allocation resize. Resizing adapters is not 312 * allowed and will throw a RSInvalidStateException. 313 * 314 * @param dimX ignored. 315 */ resize(int dimX)316 public synchronized void resize(int dimX) { 317 throw new RSInvalidStateException("Resize not allowed for Adapters."); 318 } 319 320 } 321 322 323