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