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