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