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