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