Allocation.java revision 07ae40623737a6060b8a925fd2e6bba76780dcd4
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.lang.reflect.Field; 20import java.lang.reflect.Array; 21 22import java.io.IOException; 23import java.io.InputStream; 24 25import android.content.res.Resources; 26import android.graphics.Bitmap; 27import android.graphics.BitmapFactory; 28import android.os.Bundle; 29import android.renderscript.Type; 30import android.util.Config; 31import android.util.Log; 32 33/** 34 * @hide 35 * 36 **/ 37public class Allocation extends BaseObj { 38 Type mType; 39 40 Allocation(int id, RenderScript rs, Type t) { 41 super(rs); 42 mID = id; 43 mType = t; 44 } 45 46 public void uploadToTexture(int baseMipLevel) { 47 mRS.nAllocationUploadToTexture(mID, baseMipLevel); 48 } 49 50 public void uploadToBufferObject() { 51 mRS.nAllocationUploadToBufferObject(mID); 52 } 53 54 public void data(int[] d) { 55 int size = 0; 56 if(mType != null && mType.mElement != null) { 57 size = mType.mElement.mSize; 58 for(int ct=0; ct < mType.mValues.length; ct++) { 59 if(mType.mValues[ct] != 0) { 60 size *= mType.mValues[ct]; 61 } 62 } 63 if((d.length * 4) < size) { 64 throw new IllegalArgumentException("Array too small for allocation type."); 65 } 66 Log.e("rs", "Alloc data size=" + size); 67 mRS.nAllocationData(mID, d, size); 68 return; 69 } 70 mRS.nAllocationData(mID, d, d.length * 4); 71 } 72 73 public void data(float[] d) { 74 int size = 0; 75 if(mType != null && mType.mElement != null) { 76 size = mType.mElement.mSize; 77 for(int ct=0; ct < mType.mValues.length; ct++) { 78 if(mType.mValues[ct] != 0) { 79 size *= mType.mValues[ct]; 80 } 81 } 82 if((d.length * 4) < size) { 83 throw new IllegalArgumentException("Array too small for allocation type."); 84 } 85 Log.e("rs", "Alloc data size=" + size); 86 mRS.nAllocationData(mID, d, size); 87 return; 88 } 89 mRS.nAllocationData(mID, d, d.length * 4); 90 } 91 92 public void subData1D(int off, int count, int[] d) { 93 mRS.nAllocationSubData1D(mID, off, count, d, count * 4); 94 } 95 96 public void subData1D(int off, int count, float[] d) { 97 mRS.nAllocationSubData1D(mID, off, count, d, d.length * 4); 98 } 99 100 public void subData2D(int xoff, int yoff, int w, int h, int[] d) { 101 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4); 102 } 103 104 public void subData2D(int xoff, int yoff, int w, int h, float[] d) { 105 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4); 106 } 107 108 public void readData(int[] d) { 109 mRS.nAllocationRead(mID, d); 110 } 111 112 public void readData(float[] d) { 113 mRS.nAllocationRead(mID, d); 114 } 115 116 public void data(Object o) { 117 mRS.nAllocationDataFromObject(mID, mType, o); 118 } 119 120 121 public class Adapter1D extends BaseObj { 122 Adapter1D(int id, RenderScript rs) { 123 super(rs); 124 mID = id; 125 } 126 127 public void setConstraint(Dimension dim, int value) { 128 mRS.nAdapter1DSetConstraint(mID, dim.mID, value); 129 } 130 131 public void data(int[] d) { 132 mRS.nAdapter1DData(mID, d); 133 } 134 135 public void data(float[] d) { 136 mRS.nAdapter1DData(mID, d); 137 } 138 139 public void subData(int off, int count, int[] d) { 140 mRS.nAdapter1DSubData(mID, off, count, d); 141 } 142 143 public void subData(int off, int count, float[] d) { 144 mRS.nAdapter1DSubData(mID, off, count, d); 145 } 146 } 147 148 public Adapter1D createAdapter1D() { 149 int id = mRS.nAdapter1DCreate(); 150 if (id != 0) { 151 mRS.nAdapter1DBindAllocation(id, mID); 152 } 153 return new Adapter1D(id, mRS); 154 } 155 156 157 public class Adapter2D extends BaseObj { 158 Adapter2D(int id, RenderScript rs) { 159 super(rs); 160 mID = id; 161 } 162 163 public void setConstraint(Dimension dim, int value) { 164 mRS.nAdapter2DSetConstraint(mID, dim.mID, value); 165 } 166 167 public void data(int[] d) { 168 mRS.nAdapter2DData(mID, d); 169 } 170 171 public void data(float[] d) { 172 mRS.nAdapter2DData(mID, d); 173 } 174 175 public void subData(int xoff, int yoff, int w, int h, int[] d) { 176 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d); 177 } 178 179 public void subData(int xoff, int yoff, int w, int h, float[] d) { 180 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d); 181 } 182 } 183 184 public Adapter2D createAdapter2D() { 185 int id = mRS.nAdapter2DCreate(); 186 if (id != 0) { 187 mRS.nAdapter2DBindAllocation(id, mID); 188 } 189 return new Adapter2D(id, mRS); 190 } 191 192 193 // creation 194 195 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options(); 196 static { 197 mBitmapOptions.inScaled = false; 198 } 199 200 static public Allocation createTyped(RenderScript rs, Type type) 201 throws IllegalArgumentException { 202 203 if(type.mID == 0) { 204 throw new IllegalStateException("Bad Type"); 205 } 206 int id = rs.nAllocationCreateTyped(type.mID); 207 return new Allocation(id, rs, type); 208 } 209 210 static public Allocation createSized(RenderScript rs, Element e, int count) 211 throws IllegalArgumentException { 212 213 int id; 214 if(e.mIsPredefined) { 215 id = rs.nAllocationCreatePredefSized(e.mPredefinedID, count); 216 } else { 217 id = rs.nAllocationCreateSized(e.mID, count); 218 if(id == 0) { 219 throw new IllegalStateException("Bad element."); 220 } 221 } 222 return new Allocation(id, rs, null); 223 } 224 225 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) 226 throws IllegalArgumentException { 227 if(!dstFmt.mIsPredefined) { 228 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element."); 229 } 230 231 int id = rs.nAllocationCreateFromBitmap(dstFmt.mPredefinedID, genMips, b); 232 return new Allocation(id, rs, null); 233 } 234 235 static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) 236 throws IllegalArgumentException { 237 if(!dstFmt.mIsPredefined) { 238 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element."); 239 } 240 241 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mPredefinedID, genMips, b); 242 return new Allocation(id, rs, null); 243 } 244 245 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) 246 throws IllegalArgumentException { 247 248 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions); 249 return createFromBitmap(rs, b, dstFmt, genMips); 250 } 251 252 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) 253 throws IllegalArgumentException { 254 255 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions); 256 return createFromBitmapBoxed(rs, b, dstFmt, genMips); 257 } 258} 259 260 261