Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 19 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 20 | * Only intended for use by generated reflected code. |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 21 | * |
| 22 | **/ |
| 23 | public class AllocationAdapter extends Allocation { |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 24 | AllocationAdapter(long id, RenderScript rs, Allocation alloc) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 25 | super(id, rs, alloc.mType, alloc.mUsage); |
| 26 | mAdaptedAllocation = alloc; |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 27 | } |
| 28 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 29 | long getID(RenderScript rs) { |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 30 | throw new RSInvalidStateException( |
| 31 | "This operation is not supported with adapters at this time."); |
Jason Sams | ee2d809 | 2011-06-21 16:42:42 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 34 | /** |
| 35 | * @hide |
| 36 | */ |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 37 | public void subData(int xoff, FieldPacker fp) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 38 | super.setFromFieldPacker(xoff, fp); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 39 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 40 | /** |
| 41 | * @hide |
| 42 | */ |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 43 | public void subElementData(int xoff, int component_number, FieldPacker fp) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 44 | super.setFromFieldPacker(xoff, component_number, fp); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 45 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 46 | /** |
| 47 | * @hide |
| 48 | */ |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 49 | public void subData1D(int off, int count, int[] d) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 50 | super.copy1DRangeFrom(off, count, d); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 51 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 52 | /** |
| 53 | * @hide |
| 54 | */ |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 55 | public void subData1D(int off, int count, short[] d) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 56 | super.copy1DRangeFrom(off, count, d); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 57 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 58 | /** |
| 59 | * @hide |
| 60 | */ |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 61 | public void subData1D(int off, int count, byte[] d) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 62 | super.copy1DRangeFrom(off, count, d); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 63 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 64 | /** |
| 65 | * @hide |
| 66 | */ |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 67 | public void subData1D(int off, int count, float[] d) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 68 | super.copy1DRangeFrom(off, count, d); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 69 | } |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 70 | /** |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 71 | * @hide |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 72 | */ |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 73 | public void subData2D(int xoff, int yoff, int w, int h, int[] d) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 74 | super.copy2DRangeFrom(xoff, yoff, w, h, d); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 75 | } |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 76 | /** |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 77 | * @hide |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 78 | */ |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 79 | public void subData2D(int xoff, int yoff, int w, int h, float[] d) { |
| 80 | super.copy2DRangeFrom(xoff, yoff, w, h, d); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 81 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 82 | /** |
| 83 | * @hide |
| 84 | */ |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 85 | public void readData(int[] d) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 86 | super.copyTo(d); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 87 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 88 | /** |
| 89 | * @hide |
| 90 | */ |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 91 | public void readData(float[] d) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 92 | super.copyTo(d); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 95 | void initLOD(int lod) { |
Jason Sams | ee2d809 | 2011-06-21 16:42:42 -0700 | [diff] [blame] | 96 | if (lod < 0) { |
| 97 | throw new RSIllegalArgumentException("Attempting to set negative lod (" + lod + ")."); |
| 98 | } |
| 99 | |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 100 | int tx = mAdaptedAllocation.mType.getX(); |
| 101 | int ty = mAdaptedAllocation.mType.getY(); |
| 102 | int tz = mAdaptedAllocation.mType.getZ(); |
Jason Sams | ee2d809 | 2011-06-21 16:42:42 -0700 | [diff] [blame] | 103 | |
| 104 | for (int ct=0; ct < lod; ct++) { |
| 105 | if ((tx==1) && (ty == 1) && (tz == 1)) { |
| 106 | throw new RSIllegalArgumentException("Attempting to set lod (" + lod + ") out of range."); |
| 107 | } |
| 108 | |
| 109 | if (tx > 1) tx >>= 1; |
| 110 | if (ty > 1) ty >>= 1; |
| 111 | if (tz > 1) tz >>= 1; |
| 112 | } |
| 113 | |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 114 | mCurrentDimX = tx; |
| 115 | mCurrentDimY = ty; |
| 116 | mCurrentDimZ = tz; |
| 117 | mCurrentCount = mCurrentDimX; |
| 118 | if (mCurrentDimY > 1) { |
| 119 | mCurrentCount *= mCurrentDimY; |
Jason Sams | ee2d809 | 2011-06-21 16:42:42 -0700 | [diff] [blame] | 120 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 121 | if (mCurrentDimZ > 1) { |
| 122 | mCurrentCount *= mCurrentDimZ; |
Jason Sams | ee2d809 | 2011-06-21 16:42:42 -0700 | [diff] [blame] | 123 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 124 | mSelectedY = 0; |
| 125 | mSelectedZ = 0; |
Jason Sams | ee2d809 | 2011-06-21 16:42:42 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 128 | /** |
Jason Sams | ee2d809 | 2011-06-21 16:42:42 -0700 | [diff] [blame] | 129 | * Set the active LOD. The LOD must be within the range for the |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 130 | * type being adapted. The base allocation must have mipmaps. |
| 131 | * |
| 132 | * Because this changes the dimensions of the adapter the |
| 133 | * current Y and Z will be reset. |
Jason Sams | ee2d809 | 2011-06-21 16:42:42 -0700 | [diff] [blame] | 134 | * |
| 135 | * @param lod The LOD to make active. |
| 136 | */ |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 137 | public void setLOD(int lod) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 138 | if (!mAdaptedAllocation.getType().hasMipmaps()) { |
Jason Sams | ee2d809 | 2011-06-21 16:42:42 -0700 | [diff] [blame] | 139 | throw new RSInvalidStateException("Cannot set LOD when the allocation type does not include mipmaps."); |
| 140 | } |
| 141 | if (!mConstrainedLOD) { |
| 142 | throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps."); |
| 143 | } |
| 144 | |
| 145 | initLOD(lod); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 148 | /** |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 149 | * Set the active Face. The base allocation must be of a type |
| 150 | * that includes faces. |
| 151 | * |
| 152 | * @param cf The face to make active. |
| 153 | */ |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 154 | public void setFace(Type.CubemapFace cf) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 155 | if (!mAdaptedAllocation.getType().hasFaces()) { |
| 156 | throw new RSInvalidStateException("Cannot set Face when the allocation type does not include faces."); |
| 157 | } |
| 158 | if (!mConstrainedFace) { |
| 159 | throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps."); |
| 160 | } |
| 161 | if (cf == null) { |
| 162 | throw new RSIllegalArgumentException("Cannot set null face."); |
| 163 | } |
| 164 | |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 165 | mSelectedFace = cf; |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 168 | /** |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 169 | * Set the active Y. The y value must be within the range for |
| 170 | * the allocation being adapted. The base allocation must |
| 171 | * contain the Y dimension. |
| 172 | * |
| 173 | * @param y The y to make active. |
| 174 | */ |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 175 | public void setY(int y) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 176 | if (mAdaptedAllocation.getType().getY() == 0) { |
| 177 | throw new RSInvalidStateException("Cannot set Y when the allocation type does not include Y dim."); |
| 178 | } |
| 179 | if (mAdaptedAllocation.getType().getY() <= y) { |
| 180 | throw new RSInvalidStateException("Cannot set Y greater than dimension of allocation."); |
| 181 | } |
| 182 | if (!mConstrainedY) { |
| 183 | throw new RSInvalidStateException("Cannot set Y when the adapter includes Y."); |
| 184 | } |
| 185 | |
| 186 | mSelectedY = y; |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 189 | /** |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 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 | */ |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 196 | public void setZ(int z) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 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 (!mConstrainedZ) { |
| 204 | throw new RSInvalidStateException("Cannot set Z when the adapter includes Z."); |
| 205 | } |
| 206 | |
| 207 | mSelectedZ = z; |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 210 | static public AllocationAdapter create1D(RenderScript rs, Allocation a) { |
| 211 | rs.validate(); |
| 212 | AllocationAdapter aa = new AllocationAdapter(0, rs, a); |
| 213 | aa.mConstrainedLOD = true; |
| 214 | aa.mConstrainedFace = true; |
| 215 | aa.mConstrainedY = true; |
| 216 | aa.mConstrainedZ = true; |
| 217 | aa.initLOD(0); |
| 218 | return aa; |
| 219 | } |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 220 | |
| 221 | static public AllocationAdapter create2D(RenderScript rs, Allocation a) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 222 | android.util.Log.e("rs", "create2d " + a); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 223 | rs.validate(); |
| 224 | AllocationAdapter aa = new AllocationAdapter(0, rs, a); |
Jason Sams | ee2d809 | 2011-06-21 16:42:42 -0700 | [diff] [blame] | 225 | aa.mConstrainedLOD = true; |
| 226 | aa.mConstrainedFace = true; |
| 227 | aa.mConstrainedY = false; |
| 228 | aa.mConstrainedZ = true; |
| 229 | aa.initLOD(0); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 230 | return aa; |
| 231 | } |
| 232 | |
| 233 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 234 | /** |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 235 | * Override the Allocation resize. Resizing adapters is not |
| 236 | * allowed and will throw a RSInvalidStateException. |
| 237 | * |
| 238 | * @param dimX ignored. |
| 239 | */ |
| 240 | public synchronized void resize(int dimX) { |
| 241 | throw new RSInvalidStateException("Resize not allowed for Adapters."); |
| 242 | } |
| 243 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | |