Allocation.java revision c2908e60c9b021fb4bb69acff8d49981dd4dade8
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 17package android.renderscript; 18 19import java.io.IOException; 20import java.io.InputStream; 21 22import android.content.res.Resources; 23import android.content.res.AssetManager; 24import android.graphics.Bitmap; 25import android.graphics.BitmapFactory; 26import android.util.Log; 27import android.util.TypedValue; 28 29/** 30 * @hide 31 * 32 **/ 33public class Allocation extends BaseObj { 34 Type mType; 35 36 Allocation(int id, RenderScript rs, Type t) { 37 super(rs); 38 mID = id; 39 mType = t; 40 } 41 42 public Type getType() { 43 return mType; 44 } 45 46 public void uploadToTexture(int baseMipLevel) { 47 mRS.validate(); 48 mRS.nAllocationUploadToTexture(mID, false, baseMipLevel); 49 } 50 51 public void uploadToTexture(boolean genMips, int baseMipLevel) { 52 mRS.validate(); 53 mRS.nAllocationUploadToTexture(mID, genMips, baseMipLevel); 54 } 55 56 public void uploadToBufferObject() { 57 mRS.validate(); 58 mRS.nAllocationUploadToBufferObject(mID); 59 } 60 61 public void data(int[] d) { 62 mRS.validate(); 63 subData1D(0, mType.getElementCount(), d); 64 } 65 public void data(short[] d) { 66 mRS.validate(); 67 subData1D(0, mType.getElementCount(), d); 68 } 69 public void data(byte[] d) { 70 mRS.validate(); 71 subData1D(0, mType.getElementCount(), d); 72 } 73 public void data(float[] d) { 74 mRS.validate(); 75 subData1D(0, mType.getElementCount(), d); 76 } 77 78 private void data1DChecks(int off, int count, int len, int dataSize) { 79 mRS.validate(); 80 if((off < 0) || (count < 1) || ((off + count) > mType.getElementCount())) { 81 throw new IllegalArgumentException("Offset or Count out of bounds."); 82 } 83 if((len) < dataSize) { 84 throw new IllegalArgumentException("Array too small for allocation type."); 85 } 86 } 87 88 public void subData1D(int off, int count, int[] d) { 89 int dataSize = mType.mElement.getSizeBytes() * count; 90 data1DChecks(off, count, d.length * 4, dataSize); 91 mRS.nAllocationSubData1D(mID, off, count, d, dataSize); 92 } 93 public void subData1D(int off, int count, short[] d) { 94 int dataSize = mType.mElement.getSizeBytes() * count; 95 data1DChecks(off, count, d.length * 2, dataSize); 96 mRS.nAllocationSubData1D(mID, off, count, d, dataSize); 97 } 98 public void subData1D(int off, int count, byte[] d) { 99 int dataSize = mType.mElement.getSizeBytes() * count; 100 data1DChecks(off, count, d.length, dataSize); 101 mRS.nAllocationSubData1D(mID, off, count, d, dataSize); 102 } 103 public void subData1D(int off, int count, float[] d) { 104 int dataSize = mType.mElement.getSizeBytes() * count; 105 data1DChecks(off, count, d.length * 4, dataSize); 106 mRS.nAllocationSubData1D(mID, off, count, d, dataSize); 107 } 108 109 110 111 public void subData2D(int xoff, int yoff, int w, int h, int[] d) { 112 mRS.validate(); 113 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4); 114 } 115 116 public void subData2D(int xoff, int yoff, int w, int h, float[] d) { 117 mRS.validate(); 118 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4); 119 } 120 121 public void readData(int[] d) { 122 mRS.validate(); 123 mRS.nAllocationRead(mID, d); 124 } 125 126 public void readData(float[] d) { 127 mRS.validate(); 128 mRS.nAllocationRead(mID, d); 129 } 130 131 public void data(Object o) { 132 mRS.validate(); 133 mRS.nAllocationSubDataFromObject(mID, mType, 0, o); 134 } 135 136 public void read(Object o) { 137 mRS.validate(); 138 mRS.nAllocationSubReadFromObject(mID, mType, 0, o); 139 } 140 141 public void subData(int offset, Object o) { 142 mRS.validate(); 143 mRS.nAllocationSubDataFromObject(mID, mType, offset, o); 144 } 145 146 public class Adapter1D extends BaseObj { 147 Adapter1D(int id, RenderScript rs) { 148 super(rs); 149 mID = id; 150 } 151 152 public void setConstraint(Dimension dim, int value) { 153 mRS.validate(); 154 mRS.nAdapter1DSetConstraint(mID, dim.mID, value); 155 } 156 157 public void data(int[] d) { 158 mRS.validate(); 159 mRS.nAdapter1DData(mID, d); 160 } 161 162 public void data(float[] d) { 163 mRS.validate(); 164 mRS.nAdapter1DData(mID, d); 165 } 166 167 public void subData(int off, int count, int[] d) { 168 mRS.validate(); 169 mRS.nAdapter1DSubData(mID, off, count, d); 170 } 171 172 public void subData(int off, int count, float[] d) { 173 mRS.validate(); 174 mRS.nAdapter1DSubData(mID, off, count, d); 175 } 176 } 177 178 public Adapter1D createAdapter1D() { 179 mRS.validate(); 180 int id = mRS.nAdapter1DCreate(); 181 if(id == 0) { 182 throw new IllegalStateException("allocation failed."); 183 } 184 mRS.nAdapter1DBindAllocation(id, mID); 185 return new Adapter1D(id, mRS); 186 } 187 188 189 public class Adapter2D extends BaseObj { 190 Adapter2D(int id, RenderScript rs) { 191 super(rs); 192 mID = id; 193 } 194 195 public void setConstraint(Dimension dim, int value) { 196 mRS.validate(); 197 mRS.nAdapter2DSetConstraint(mID, dim.mID, value); 198 } 199 200 public void data(int[] d) { 201 mRS.validate(); 202 mRS.nAdapter2DData(mID, d); 203 } 204 205 public void data(float[] d) { 206 mRS.validate(); 207 mRS.nAdapter2DData(mID, d); 208 } 209 210 public void subData(int xoff, int yoff, int w, int h, int[] d) { 211 mRS.validate(); 212 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d); 213 } 214 215 public void subData(int xoff, int yoff, int w, int h, float[] d) { 216 mRS.validate(); 217 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d); 218 } 219 } 220 221 public Adapter2D createAdapter2D() { 222 mRS.validate(); 223 int id = mRS.nAdapter2DCreate(); 224 if(id == 0) { 225 throw new IllegalStateException("allocation failed."); 226 } 227 mRS.nAdapter2DBindAllocation(id, mID); 228 return new Adapter2D(id, mRS); 229 } 230 231 232 // creation 233 234 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options(); 235 static { 236 mBitmapOptions.inScaled = false; 237 } 238 239 static public Allocation createTyped(RenderScript rs, Type type) 240 throws IllegalArgumentException { 241 242 rs.validate(); 243 if(type.mID == 0) { 244 throw new IllegalStateException("Bad Type"); 245 } 246 int id = rs.nAllocationCreateTyped(type.mID); 247 return new Allocation(id, rs, type); 248 } 249 250 static public Allocation createSized(RenderScript rs, Element e, int count) 251 throws IllegalArgumentException { 252 253 rs.validate(); 254 Type.Builder b = new Type.Builder(rs, e); 255 b.add(Dimension.X, count); 256 Type t = b.create(); 257 258 int id = rs.nAllocationCreateTyped(t.mID); 259 if(id == 0) { 260 throw new IllegalStateException("Bad element."); 261 } 262 return new Allocation(id, rs, t); 263 } 264 265 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) 266 throws IllegalArgumentException { 267 268 rs.validate(); 269 int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b); 270 if(id == 0) { 271 throw new IllegalStateException("Load failed."); 272 } 273 return new Allocation(id, rs, null); 274 } 275 276 static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) 277 throws IllegalArgumentException { 278 279 rs.validate(); 280 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b); 281 if(id == 0) { 282 throw new IllegalStateException("Load failed."); 283 } 284 return new Allocation(id, rs, null); 285 } 286 287 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) 288 throws IllegalArgumentException { 289 290 rs.validate(); 291 InputStream is = null; 292 try { 293 final TypedValue value = new TypedValue(); 294 is = res.openRawResource(id, value); 295 296 int asset = ((AssetManager.AssetInputStream) is).getAssetInt(); 297 int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips, 298 asset); 299 300 if(allocationId == 0) { 301 throw new IllegalStateException("Load failed."); 302 } 303 return new Allocation(allocationId, rs, null); 304 } catch (Exception e) { 305 // Ignore 306 } finally { 307 if (is != null) { 308 try { 309 is.close(); 310 } catch (IOException e) { 311 // Ignore 312 } 313 } 314 } 315 316 return null; 317 } 318 319 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) 320 throws IllegalArgumentException { 321 322 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions); 323 return createFromBitmapBoxed(rs, b, dstFmt, genMips); 324 } 325} 326 327 328