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