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