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