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