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