| 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 | /** |
| Jason Sams | a23d4e7 | 2011-01-04 18:59:12 -0800 | [diff] [blame] | 30 | * Memory allocation class for renderscript. An allocation combines a Type with |
| 31 | * memory to provide storage for user data and objects. |
| 32 | * |
| 33 | * Allocations may exist in one or more memory spaces. Currently those are |
| 34 | * Script: accessable by RS scripts. |
| 35 | * Graphics Texture: accessable as a graphics texture. |
| 36 | * Graphics Vertex: accessable as graphical vertex data. |
| 37 | * Graphics Constants: Accessable as constants in user shaders |
| 38 | * |
| 39 | * By default java side updates are always applied to the script accessable |
| 40 | * memory. If this is not present they are then applied to the various HW |
| 41 | * memory types. A syncAll call is necessary after the script data is update to |
| 42 | * keep the other memory spaces in sync. |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 43 | * |
| 44 | **/ |
| 45 | public class Allocation extends BaseObj { |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 46 | Type mType; |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 47 | Bitmap mBitmap; |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 48 | int mUsage; |
| 49 | |
| 50 | public static final int USAGE_SCRIPT = 0x0001; |
| 51 | public static final int USAGE_GRAPHICS_TEXTURE = 0x0002; |
| 52 | public static final int USAGE_GRAPHICS_VERTEX = 0x0004; |
| 53 | public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008; |
| 54 | |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 55 | |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 56 | public enum MipmapControl { |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 57 | MIPMAP_NONE(0), |
| 58 | MIPMAP_FULL(1), |
| 59 | MIPMAP_ON_SYNC_TO_TEXTURE(2); |
| 60 | |
| 61 | int mID; |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 62 | MipmapControl(int id) { |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 63 | mID = id; |
| 64 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 67 | Allocation(int id, RenderScript rs, Type t, int usage) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 68 | super(id, rs); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 69 | if ((usage & ~(USAGE_SCRIPT | |
| 70 | USAGE_GRAPHICS_TEXTURE | |
| 71 | USAGE_GRAPHICS_VERTEX | |
| 72 | USAGE_GRAPHICS_CONSTANTS)) != 0) { |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 73 | throw new RSIllegalArgumentException("Unknown usage specified."); |
| 74 | } |
| 75 | mType = t; |
| Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 78 | @Override |
| 79 | void updateFromNative() { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 80 | super.updateFromNative(); |
| 81 | int typeID = mRS.nAllocationGetType(getID()); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 82 | if(typeID != 0) { |
| 83 | mType = new Type(typeID, mRS); |
| 84 | mType.updateFromNative(); |
| 85 | } |
| 86 | } |
| 87 | |
| Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 88 | public Type getType() { |
| 89 | return mType; |
| 90 | } |
| 91 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 92 | public void syncAll(int srcLocation) { |
| 93 | switch (srcLocation) { |
| 94 | case USAGE_SCRIPT: |
| 95 | case USAGE_GRAPHICS_CONSTANTS: |
| 96 | case USAGE_GRAPHICS_TEXTURE: |
| 97 | case USAGE_GRAPHICS_VERTEX: |
| 98 | break; |
| 99 | default: |
| 100 | throw new RSIllegalArgumentException("Source must be exactly one usage type."); |
| 101 | } |
| 102 | mRS.validate(); |
| 103 | mRS.nAllocationSyncAll(getID(), srcLocation); |
| 104 | } |
| 105 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 106 | public void copyFrom(BaseObj[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 107 | mRS.validate(); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 108 | if (d.length != mType.getCount()) { |
| 109 | throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " + |
| 110 | mType.getCount() + ", array length = " + d.length); |
| 111 | } |
| 112 | int i[] = new int[d.length]; |
| 113 | for (int ct=0; ct < d.length; ct++) { |
| 114 | i[ct] = d[ct].getID(); |
| 115 | } |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 116 | copy1DRangeFrom(0, mType.getCount(), i); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 119 | private void validateBitmap(Bitmap b) { |
| 120 | mRS.validate(); |
| 121 | if(mType.getX() != b.getWidth() || |
| 122 | mType.getY() != b.getHeight()) { |
| 123 | throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch"); |
| 124 | } |
| 125 | } |
| 126 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 127 | public void copyFrom(int[] d) { |
| 128 | mRS.validate(); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 129 | copy1DRangeFrom(0, mType.getCount(), d); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 130 | } |
| 131 | public void copyFrom(short[] d) { |
| 132 | mRS.validate(); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 133 | copy1DRangeFrom(0, mType.getCount(), d); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 134 | } |
| 135 | public void copyFrom(byte[] d) { |
| 136 | mRS.validate(); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 137 | copy1DRangeFrom(0, mType.getCount(), d); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 138 | } |
| 139 | public void copyFrom(float[] d) { |
| 140 | mRS.validate(); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 141 | copy1DRangeFrom(0, mType.getCount(), d); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 142 | } |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 143 | public void copyFrom(Bitmap b) { |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 144 | validateBitmap(b); |
| 145 | mRS.nAllocationCopyFromBitmap(getID(), b); |
| Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 148 | /** |
| 149 | * @hide |
| 150 | * |
| 151 | * This is only intended to be used by auto-generate code reflected from the |
| 152 | * renderscript script files. |
| 153 | * |
| 154 | * @param xoff |
| 155 | * @param fp |
| 156 | */ |
| 157 | public void setOneElement(int xoff, FieldPacker fp) { |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 158 | int eSize = mType.mElement.getSizeBytes(); |
| 159 | final byte[] data = fp.getData(); |
| 160 | |
| 161 | int count = data.length / eSize; |
| 162 | if ((eSize * count) != data.length) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 163 | throw new RSIllegalArgumentException("Field packer length " + data.length + |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 164 | " not divisible by element size " + eSize + "."); |
| 165 | } |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 166 | data1DChecks(xoff, count, data.length, data.length); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 167 | mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length); |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 171 | /** |
| 172 | * @hide |
| 173 | * |
| 174 | * This is only intended to be used by auto-generate code reflected from the |
| 175 | * renderscript script files. |
| 176 | * |
| 177 | * @param xoff |
| 178 | * @param component_number |
| 179 | * @param fp |
| 180 | */ |
| 181 | public void setOneComponent(int xoff, int component_number, FieldPacker fp) { |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 182 | if (component_number >= mType.mElement.mElements.length) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 183 | throw new RSIllegalArgumentException("Component_number " + component_number + " out of range."); |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 184 | } |
| 185 | if(xoff < 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 186 | throw new RSIllegalArgumentException("Offset must be >= 0."); |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | final byte[] data = fp.getData(); |
| 190 | int eSize = mType.mElement.mElements[component_number].getSizeBytes(); |
| 191 | |
| 192 | if (data.length != eSize) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 193 | throw new RSIllegalArgumentException("Field packer sizelength " + data.length + |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 194 | " does not match component size " + eSize + "."); |
| 195 | } |
| 196 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 197 | mRS.nAllocationElementData1D(getID(), xoff, 0, component_number, data, data.length); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 200 | private void data1DChecks(int off, int count, int len, int dataSize) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 201 | mRS.validate(); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 202 | if(off < 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 203 | throw new RSIllegalArgumentException("Offset must be >= 0."); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 204 | } |
| 205 | if(count < 1) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 206 | throw new RSIllegalArgumentException("Count must be >= 1."); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 207 | } |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 208 | if((off + count) > mType.getCount()) { |
| 209 | throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() + |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 210 | ", got " + count + " at offset " + off + "."); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 211 | } |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 212 | if((len) < dataSize) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 213 | throw new RSIllegalArgumentException("Array too small for allocation type."); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 214 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 217 | public void copy1DRangeFrom(int off, int count, int[] d) { |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 218 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 219 | data1DChecks(off, count, d.length * 4, dataSize); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 220 | mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 221 | } |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 222 | public void copy1DRangeFrom(int off, int count, short[] d) { |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 223 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 224 | data1DChecks(off, count, d.length * 2, dataSize); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 225 | mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 226 | } |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 227 | public void copy1DRangeFrom(int off, int count, byte[] d) { |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 228 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 229 | data1DChecks(off, count, d.length, dataSize); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 230 | mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 231 | } |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 232 | public void copy1DRangeFrom(int off, int count, float[] d) { |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 233 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 234 | data1DChecks(off, count, d.length * 4, dataSize); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 235 | mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 238 | |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 239 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] d) { |
| 240 | mRS.validate(); |
| 241 | mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length); |
| 242 | } |
| 243 | |
| 244 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] d) { |
| 245 | mRS.validate(); |
| 246 | mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 2); |
| 247 | } |
| 248 | |
| 249 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 250 | mRS.validate(); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 251 | mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 4); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 254 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 255 | mRS.validate(); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 256 | mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 4); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 259 | public void copy2DRangeFrom(int xoff, int yoff, Bitmap b) { |
| 260 | mRS.validate(); |
| 261 | mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, b); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | public void copyTo(Bitmap b) { |
| 266 | validateBitmap(b); |
| 267 | mRS.nAllocationCopyToBitmap(getID(), b); |
| 268 | } |
| 269 | |
| 270 | public void copyTo(byte[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 271 | mRS.validate(); |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 272 | mRS.nAllocationRead(getID(), d); |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 275 | public void copyTo(short[] d) { |
| 276 | mRS.validate(); |
| 277 | mRS.nAllocationRead(getID(), d); |
| 278 | } |
| 279 | |
| 280 | public void copyTo(int[] d) { |
| 281 | mRS.validate(); |
| 282 | mRS.nAllocationRead(getID(), d); |
| 283 | } |
| 284 | |
| 285 | public void copyTo(float[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 286 | mRS.validate(); |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 287 | mRS.nAllocationRead(getID(), d); |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 290 | public synchronized void resize(int dimX) { |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 291 | if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 292 | throw new RSInvalidStateException("Resize only support for 1D allocations at this time."); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 293 | } |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 294 | mRS.nAllocationResize1D(getID(), dimX); |
| Jason Sams | d26297f | 2010-11-01 16:08:59 -0700 | [diff] [blame] | 295 | mRS.finish(); // Necessary because resize is fifoed and update is async. |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 296 | |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 297 | int typeID = mRS.nAllocationGetType(getID()); |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 298 | mType = new Type(typeID, mRS); |
| 299 | mType.updateFromNative(); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* |
| 303 | public void resize(int dimX, int dimY) { |
| 304 | if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 305 | throw new RSIllegalStateException("Resize only support for 2D allocations at this time."); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 306 | } |
| 307 | if (mType.getY() == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 308 | throw new RSIllegalStateException("Resize only support for 2D allocations at this time."); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 309 | } |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 310 | mRS.nAllocationResize2D(getID(), dimX, dimY); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 311 | } |
| 312 | */ |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 313 | |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 314 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 315 | |
| 316 | // creation |
| 317 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 318 | static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options(); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 319 | static { |
| 320 | mBitmapOptions.inScaled = false; |
| 321 | } |
| 322 | |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 323 | static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mc, int usage) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 324 | rs.validate(); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 325 | if (type.getID() == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 326 | throw new RSInvalidStateException("Bad Type"); |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 327 | } |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 328 | int id = rs.nAllocationCreateTyped(type.getID(), mc.mID, usage); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 329 | if (id == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 330 | throw new RSRuntimeException("Allocation creation failed."); |
| 331 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 332 | return new Allocation(id, rs, type, usage); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| Jason Sams | e5d3712 | 2010-12-16 00:33:33 -0800 | [diff] [blame] | 335 | static public Allocation createTyped(RenderScript rs, Type type, int usage) { |
| 336 | return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage); |
| 337 | } |
| 338 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 339 | static public Allocation createTyped(RenderScript rs, Type type) { |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 340 | return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 341 | } |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 342 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 343 | static public Allocation createSized(RenderScript rs, Element e, |
| 344 | int count, int usage) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 345 | rs.validate(); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 346 | Type.Builder b = new Type.Builder(rs, e); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 347 | b.setX(count); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 348 | Type t = b.create(); |
| 349 | |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 350 | int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 351 | if (id == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 352 | throw new RSRuntimeException("Allocation creation failed."); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 353 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 354 | return new Allocation(id, rs, t, usage); |
| 355 | } |
| 356 | |
| 357 | static public Allocation createSized(RenderScript rs, Element e, int count) { |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 358 | return createSized(rs, e, count, USAGE_SCRIPT); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 361 | static Element elementFromBitmap(RenderScript rs, Bitmap b) { |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 362 | final Bitmap.Config bc = b.getConfig(); |
| 363 | if (bc == Bitmap.Config.ALPHA_8) { |
| 364 | return Element.A_8(rs); |
| 365 | } |
| 366 | if (bc == Bitmap.Config.ARGB_4444) { |
| 367 | return Element.RGBA_4444(rs); |
| 368 | } |
| 369 | if (bc == Bitmap.Config.ARGB_8888) { |
| 370 | return Element.RGBA_8888(rs); |
| 371 | } |
| 372 | if (bc == Bitmap.Config.RGB_565) { |
| 373 | return Element.RGB_565(rs); |
| 374 | } |
| Jeff Sharkey | 4bd1a3d | 2010-11-16 13:46:34 -0800 | [diff] [blame] | 375 | throw new RSInvalidStateException("Bad bitmap type: " + bc); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 378 | static Type typeFromBitmap(RenderScript rs, Bitmap b, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 379 | MipmapControl mip) { |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 380 | Element e = elementFromBitmap(rs, b); |
| 381 | Type.Builder tb = new Type.Builder(rs, e); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 382 | tb.setX(b.getWidth()); |
| 383 | tb.setY(b.getHeight()); |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 384 | tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 385 | return tb.create(); |
| 386 | } |
| 387 | |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 388 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 389 | MipmapControl mips, |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 390 | int usage) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 391 | rs.validate(); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 392 | Type t = typeFromBitmap(rs, b, mips); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 393 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 394 | int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage); |
| 395 | if (id == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 396 | throw new RSRuntimeException("Load failed."); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 397 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 398 | return new Allocation(id, rs, t, usage); |
| 399 | } |
| 400 | |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 401 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b) { |
| 402 | return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
| 403 | USAGE_GRAPHICS_TEXTURE); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 406 | /** |
| 407 | * Creates a cubemap allocation from a bitmap containing the |
| 408 | * horizontal list of cube faces. Each individual face must be |
| 409 | * the same size and power of 2 |
| 410 | * |
| 411 | * @param rs |
| 412 | * @param b bitmap with cubemap faces layed out in the following |
| 413 | * format: right, left, top, bottom, front, back |
| 414 | * @param mips specifies desired mipmap behaviour for the cubemap |
| 415 | * @param usage bitfield specifying how the cubemap is utilized |
| 416 | * |
| 417 | **/ |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 418 | static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 419 | MipmapControl mips, |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 420 | int usage) { |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 421 | rs.validate(); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 422 | |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 423 | int height = b.getHeight(); |
| 424 | int width = b.getWidth(); |
| 425 | |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 426 | if (width % 6 != 0) { |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 427 | throw new RSIllegalArgumentException("Cubemap height must be multiple of 6"); |
| 428 | } |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 429 | if (width / 6 != height) { |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 430 | throw new RSIllegalArgumentException("Only square cobe map faces supported"); |
| 431 | } |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 432 | boolean isPow2 = (height & (height - 1)) == 0; |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 433 | if (!isPow2) { |
| 434 | throw new RSIllegalArgumentException("Only power of 2 cube faces supported"); |
| 435 | } |
| 436 | |
| 437 | Element e = elementFromBitmap(rs, b); |
| 438 | Type.Builder tb = new Type.Builder(rs, e); |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 439 | tb.setX(height); |
| 440 | tb.setY(height); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 441 | tb.setFaces(true); |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 442 | tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 443 | Type t = tb.create(); |
| 444 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 445 | int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 446 | if(id == 0) { |
| 447 | throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e); |
| 448 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 449 | return new Allocation(id, rs, t, usage); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 452 | static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b) { |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 453 | return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 454 | USAGE_GRAPHICS_TEXTURE); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 457 | static public Allocation createFromBitmapResource(RenderScript rs, |
| 458 | Resources res, |
| 459 | int id, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 460 | MipmapControl mips, |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 461 | int usage) { |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 462 | |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 463 | rs.validate(); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 464 | Bitmap b = BitmapFactory.decodeResource(res, id); |
| 465 | Allocation alloc = createFromBitmap(rs, b, mips, usage); |
| 466 | b.recycle(); |
| 467 | return alloc; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 470 | static public Allocation createFromBitmapResource(RenderScript rs, |
| 471 | Resources res, |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 472 | int id) { |
| 473 | return createFromBitmapResource(rs, res, id, |
| 474 | MipmapControl.MIPMAP_NONE, |
| 475 | USAGE_GRAPHICS_TEXTURE); |
| 476 | } |
| 477 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 478 | static public Allocation createFromString(RenderScript rs, |
| 479 | String str, |
| 480 | int usage) { |
| 481 | rs.validate(); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 482 | byte[] allocArray = null; |
| 483 | try { |
| 484 | allocArray = str.getBytes("UTF-8"); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 485 | Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 486 | alloc.copyFrom(allocArray); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 487 | return alloc; |
| 488 | } |
| 489 | catch (Exception e) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 490 | throw new RSRuntimeException("Could not convert string to utf-8."); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 491 | } |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 492 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | |