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