| 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; |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 35 | Bitmap mBitmap; |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 36 | |
| 37 | Allocation(int id, RenderScript rs, Type t) { |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 38 | super(rs); |
| 39 | mID = id; |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 40 | mType = t; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 43 | public Type getType() { |
| 44 | return mType; |
| 45 | } |
| 46 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 47 | public void uploadToTexture(int baseMipLevel) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 48 | mRS.validate(); |
| Jason Sams | c2908e6 | 2010-02-23 17:44:28 -0800 | [diff] [blame] | 49 | mRS.nAllocationUploadToTexture(mID, false, baseMipLevel); |
| 50 | } |
| 51 | |
| 52 | public void uploadToTexture(boolean genMips, int baseMipLevel) { |
| 53 | mRS.validate(); |
| 54 | mRS.nAllocationUploadToTexture(mID, genMips, baseMipLevel); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 57 | public void uploadToBufferObject() { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 58 | mRS.validate(); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 59 | mRS.nAllocationUploadToBufferObject(mID); |
| 60 | } |
| 61 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 62 | public void data(int[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 63 | mRS.validate(); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 64 | subData1D(0, mType.getElementCount(), d); |
| 65 | } |
| 66 | public void data(short[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 67 | mRS.validate(); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 68 | subData1D(0, mType.getElementCount(), d); |
| 69 | } |
| 70 | public void data(byte[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 71 | mRS.validate(); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 72 | subData1D(0, mType.getElementCount(), d); |
| 73 | } |
| 74 | public void data(float[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 75 | mRS.validate(); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 76 | subData1D(0, mType.getElementCount(), d); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 79 | public void subData(int off, FieldPacker fp) { |
| 80 | int eSize = mType.mElement.getSizeBytes(); |
| 81 | final byte[] data = fp.getData(); |
| 82 | |
| 83 | int count = data.length / eSize; |
| 84 | if ((eSize * count) != data.length) { |
| 85 | throw new IllegalArgumentException("Field packer length " + data.length + |
| 86 | " not divisible by element size " + eSize + "."); |
| 87 | } |
| 88 | data1DChecks(off, count, data.length, data.length); |
| 89 | mRS.nAllocationSubData1D(mID, off, count, data, data.length); |
| 90 | } |
| 91 | |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 92 | private void data1DChecks(int off, int count, int len, int dataSize) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 93 | mRS.validate(); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 94 | if(off < 0) { |
| 95 | throw new IllegalArgumentException("Offset must be >= 0."); |
| 96 | } |
| 97 | if(count < 1) { |
| 98 | throw new IllegalArgumentException("Count must be >= 1."); |
| 99 | } |
| 100 | if((off + count) > mType.getElementCount()) { |
| 101 | throw new IllegalArgumentException("Overflow, Available count " + mType.getElementCount() + |
| 102 | ", got " + count + " at offset " + off + "."); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 103 | } |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 104 | if((len) < dataSize) { |
| 105 | throw new IllegalArgumentException("Array too small for allocation type."); |
| 106 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | public void subData1D(int off, int count, int[] d) { |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 110 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 111 | data1DChecks(off, count, d.length * 4, dataSize); |
| 112 | mRS.nAllocationSubData1D(mID, off, count, d, dataSize); |
| 113 | } |
| 114 | public void subData1D(int off, int count, short[] d) { |
| 115 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 116 | data1DChecks(off, count, d.length * 2, dataSize); |
| 117 | mRS.nAllocationSubData1D(mID, off, count, d, dataSize); |
| 118 | } |
| 119 | public void subData1D(int off, int count, byte[] d) { |
| 120 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 121 | data1DChecks(off, count, d.length, dataSize); |
| 122 | mRS.nAllocationSubData1D(mID, off, count, d, dataSize); |
| 123 | } |
| 124 | public void subData1D(int off, int count, float[] d) { |
| 125 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 126 | data1DChecks(off, count, d.length * 4, dataSize); |
| 127 | mRS.nAllocationSubData1D(mID, off, count, d, dataSize); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 130 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 131 | |
| 132 | public void subData2D(int xoff, int yoff, int w, int h, int[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 133 | mRS.validate(); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 134 | mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | public void subData2D(int xoff, int yoff, int w, int h, float[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 138 | mRS.validate(); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 139 | mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 142 | public void readData(int[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 143 | mRS.validate(); |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 144 | mRS.nAllocationRead(mID, d); |
| 145 | } |
| 146 | |
| 147 | public void readData(float[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 148 | mRS.validate(); |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 149 | mRS.nAllocationRead(mID, d); |
| 150 | } |
| 151 | |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 152 | public void data(Object o) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 153 | mRS.validate(); |
| Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 154 | mRS.nAllocationSubDataFromObject(mID, mType, 0, o); |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| Jason Sams | 5f43fd2 | 2009-09-15 12:39:22 -0700 | [diff] [blame] | 157 | public void read(Object o) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 158 | mRS.validate(); |
| Jason Sams | 5f43fd2 | 2009-09-15 12:39:22 -0700 | [diff] [blame] | 159 | mRS.nAllocationSubReadFromObject(mID, mType, 0, o); |
| 160 | } |
| 161 | |
| Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 162 | public void subData(int offset, Object o) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 163 | mRS.validate(); |
| Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 164 | mRS.nAllocationSubDataFromObject(mID, mType, offset, o); |
| 165 | } |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 166 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 167 | public class Adapter1D extends BaseObj { |
| 168 | Adapter1D(int id, RenderScript rs) { |
| 169 | super(rs); |
| 170 | mID = id; |
| 171 | } |
| 172 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 173 | public void setConstraint(Dimension dim, int value) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 174 | mRS.validate(); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 175 | mRS.nAdapter1DSetConstraint(mID, dim.mID, value); |
| 176 | } |
| 177 | |
| 178 | public void data(int[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 179 | mRS.validate(); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 180 | mRS.nAdapter1DData(mID, d); |
| 181 | } |
| 182 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 183 | public void data(float[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 184 | mRS.validate(); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 185 | mRS.nAdapter1DData(mID, d); |
| 186 | } |
| 187 | |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 188 | public void subData(int off, int count, int[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 189 | mRS.validate(); |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 190 | mRS.nAdapter1DSubData(mID, off, count, d); |
| 191 | } |
| 192 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 193 | public void subData(int off, int count, float[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 194 | mRS.validate(); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 195 | mRS.nAdapter1DSubData(mID, off, count, d); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | public Adapter1D createAdapter1D() { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 200 | mRS.validate(); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 201 | int id = mRS.nAdapter1DCreate(); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 202 | if(id == 0) { |
| 203 | throw new IllegalStateException("allocation failed."); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 204 | } |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 205 | mRS.nAdapter1DBindAllocation(id, mID); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 206 | return new Adapter1D(id, mRS); |
| 207 | } |
| 208 | |
| 209 | |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 210 | public class Adapter2D extends BaseObj { |
| 211 | Adapter2D(int id, RenderScript rs) { |
| 212 | super(rs); |
| 213 | mID = id; |
| 214 | } |
| 215 | |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 216 | public void setConstraint(Dimension dim, int value) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 217 | mRS.validate(); |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 218 | mRS.nAdapter2DSetConstraint(mID, dim.mID, value); |
| 219 | } |
| 220 | |
| 221 | public void data(int[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 222 | mRS.validate(); |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 223 | mRS.nAdapter2DData(mID, d); |
| 224 | } |
| 225 | |
| 226 | public void data(float[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 227 | mRS.validate(); |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 228 | mRS.nAdapter2DData(mID, d); |
| 229 | } |
| 230 | |
| 231 | public void subData(int xoff, int yoff, int w, int h, int[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 232 | mRS.validate(); |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 233 | mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d); |
| 234 | } |
| 235 | |
| 236 | public void subData(int xoff, int yoff, int w, int h, float[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 237 | mRS.validate(); |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 238 | mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | public Adapter2D createAdapter2D() { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 243 | mRS.validate(); |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 244 | int id = mRS.nAdapter2DCreate(); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 245 | if(id == 0) { |
| 246 | throw new IllegalStateException("allocation failed."); |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 247 | } |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 248 | mRS.nAdapter2DBindAllocation(id, mID); |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 249 | return new Adapter2D(id, mRS); |
| 250 | } |
| 251 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 252 | |
| 253 | // creation |
| 254 | |
| 255 | private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options(); |
| 256 | static { |
| 257 | mBitmapOptions.inScaled = false; |
| 258 | } |
| 259 | |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 260 | static public Allocation createTyped(RenderScript rs, Type type) |
| 261 | throws IllegalArgumentException { |
| 262 | |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 263 | rs.validate(); |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 264 | if(type.mID == 0) { |
| 265 | throw new IllegalStateException("Bad Type"); |
| 266 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 267 | int id = rs.nAllocationCreateTyped(type.mID); |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 268 | return new Allocation(id, rs, type); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 271 | static public Allocation createSized(RenderScript rs, Element e, int count) |
| 272 | throws IllegalArgumentException { |
| 273 | |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 274 | rs.validate(); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 275 | Type.Builder b = new Type.Builder(rs, e); |
| 276 | b.add(Dimension.X, count); |
| 277 | Type t = b.create(); |
| 278 | |
| 279 | int id = rs.nAllocationCreateTyped(t.mID); |
| Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 280 | if(id == 0) { |
| 281 | throw new IllegalStateException("Bad element."); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 282 | } |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 283 | return new Allocation(id, rs, t); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 286 | static private Element elementFromBitmap(RenderScript rs, Bitmap b) { |
| 287 | final Bitmap.Config bc = b.getConfig(); |
| 288 | if (bc == Bitmap.Config.ALPHA_8) { |
| 289 | return Element.A_8(rs); |
| 290 | } |
| 291 | if (bc == Bitmap.Config.ARGB_4444) { |
| 292 | return Element.RGBA_4444(rs); |
| 293 | } |
| 294 | if (bc == Bitmap.Config.ARGB_8888) { |
| 295 | return Element.RGBA_8888(rs); |
| 296 | } |
| 297 | if (bc == Bitmap.Config.RGB_565) { |
| 298 | return Element.RGB_565(rs); |
| 299 | } |
| 300 | throw new IllegalStateException("Bad bitmap type."); |
| 301 | } |
| 302 | |
| 303 | static private Type typeFromBitmap(RenderScript rs, Bitmap b) { |
| 304 | Element e = elementFromBitmap(rs, b); |
| 305 | Type.Builder tb = new Type.Builder(rs, e); |
| 306 | tb.add(Dimension.X, b.getWidth()); |
| 307 | tb.add(Dimension.Y, b.getHeight()); |
| 308 | return tb.create(); |
| 309 | } |
| 310 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 311 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) |
| 312 | throws IllegalArgumentException { |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 313 | |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 314 | rs.validate(); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 315 | Type t = typeFromBitmap(rs, b); |
| 316 | |
| Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 317 | int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 318 | if(id == 0) { |
| 319 | throw new IllegalStateException("Load failed."); |
| 320 | } |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 321 | return new Allocation(id, rs, t); |
| 322 | } |
| 323 | |
| 324 | static public Allocation createBitmapRef(RenderScript rs, Bitmap b) |
| 325 | throws IllegalArgumentException { |
| 326 | |
| 327 | rs.validate(); |
| 328 | Type t = typeFromBitmap(rs, b); |
| 329 | |
| 330 | int id = rs.nAllocationCreateBitmapRef(t.getID(), b); |
| 331 | if(id == 0) { |
| 332 | throw new IllegalStateException("Load failed."); |
| 333 | } |
| 334 | |
| 335 | Allocation a = new Allocation(id, rs, t); |
| 336 | a.mBitmap = b; |
| 337 | return a; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| Jason Sams | 74e02ef | 2010-01-06 15:10:29 -0800 | [diff] [blame] | 340 | static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 341 | throws IllegalArgumentException { |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 342 | |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 343 | rs.validate(); |
| Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 344 | int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 345 | if(id == 0) { |
| 346 | throw new IllegalStateException("Load failed."); |
| 347 | } |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 348 | return new Allocation(id, rs, null); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) |
| 352 | throws IllegalArgumentException { |
| 353 | |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 354 | rs.validate(); |
| Romain Guy | 650a3eb | 2009-08-31 14:06:43 -0700 | [diff] [blame] | 355 | InputStream is = null; |
| 356 | try { |
| 357 | final TypedValue value = new TypedValue(); |
| 358 | is = res.openRawResource(id, value); |
| 359 | |
| 360 | int asset = ((AssetManager.AssetInputStream) is).getAssetInt(); |
| Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 361 | int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips, |
| Romain Guy | 650a3eb | 2009-08-31 14:06:43 -0700 | [diff] [blame] | 362 | asset); |
| 363 | |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 364 | if(allocationId == 0) { |
| 365 | throw new IllegalStateException("Load failed."); |
| 366 | } |
| Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 367 | return new Allocation(allocationId, rs, null); |
| Romain Guy | 650a3eb | 2009-08-31 14:06:43 -0700 | [diff] [blame] | 368 | } catch (Exception e) { |
| 369 | // Ignore |
| 370 | } finally { |
| 371 | if (is != null) { |
| 372 | try { |
| 373 | is.close(); |
| 374 | } catch (IOException e) { |
| 375 | // Ignore |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | return null; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) |
| 384 | throws IllegalArgumentException { |
| 385 | |
| 386 | Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions); |
| 387 | return createFromBitmapBoxed(rs, b, dstFmt, genMips); |
| 388 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | |