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