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