| 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 | |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 56 | public enum CubemapLayout { |
| 57 | VERTICAL_FACE_LIST (0), |
| 58 | HORIZONTAL_FACE_LIST (1), |
| 59 | VERTICAL_CROSS (2), |
| 60 | HORIZONTAL_CROSS (3); |
| 61 | |
| 62 | int mID; |
| 63 | CubemapLayout(int id) { |
| 64 | mID = id; |
| 65 | } |
| 66 | } |
| 67 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 68 | |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 69 | public enum MipmapControl { |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 70 | MIPMAP_NONE(0), |
| 71 | MIPMAP_FULL(1), |
| 72 | MIPMAP_ON_SYNC_TO_TEXTURE(2); |
| 73 | |
| 74 | int mID; |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 75 | MipmapControl(int id) { |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 76 | mID = id; |
| 77 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 80 | Allocation(int id, RenderScript rs, Type t, int usage) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 81 | super(id, rs); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 82 | if ((usage & ~(USAGE_SCRIPT | |
| 83 | USAGE_GRAPHICS_TEXTURE | |
| 84 | USAGE_GRAPHICS_VERTEX | |
| 85 | USAGE_GRAPHICS_CONSTANTS)) != 0) { |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 86 | throw new RSIllegalArgumentException("Unknown usage specified."); |
| 87 | } |
| 88 | mType = t; |
| Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 91 | @Override |
| 92 | void updateFromNative() { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 93 | super.updateFromNative(); |
| 94 | int typeID = mRS.nAllocationGetType(getID()); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 95 | if(typeID != 0) { |
| 96 | mType = new Type(typeID, mRS); |
| 97 | mType.updateFromNative(); |
| 98 | } |
| 99 | } |
| 100 | |
| Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 101 | public Type getType() { |
| 102 | return mType; |
| 103 | } |
| 104 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 105 | public void syncAll(int srcLocation) { |
| 106 | switch (srcLocation) { |
| 107 | case USAGE_SCRIPT: |
| 108 | case USAGE_GRAPHICS_CONSTANTS: |
| 109 | case USAGE_GRAPHICS_TEXTURE: |
| 110 | case USAGE_GRAPHICS_VERTEX: |
| 111 | break; |
| 112 | default: |
| 113 | throw new RSIllegalArgumentException("Source must be exactly one usage type."); |
| 114 | } |
| 115 | mRS.validate(); |
| 116 | mRS.nAllocationSyncAll(getID(), srcLocation); |
| 117 | } |
| 118 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 119 | public void copyFrom(BaseObj[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 120 | mRS.validate(); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 121 | if (d.length != mType.getCount()) { |
| 122 | throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " + |
| 123 | mType.getCount() + ", array length = " + d.length); |
| 124 | } |
| 125 | int i[] = new int[d.length]; |
| 126 | for (int ct=0; ct < d.length; ct++) { |
| 127 | i[ct] = d[ct].getID(); |
| 128 | } |
| 129 | subData1D(0, mType.getCount(), i); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 132 | private void validateBitmap(Bitmap b) { |
| 133 | mRS.validate(); |
| 134 | if(mType.getX() != b.getWidth() || |
| 135 | mType.getY() != b.getHeight()) { |
| 136 | throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch"); |
| 137 | } |
| 138 | } |
| 139 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 140 | public void copyFrom(int[] d) { |
| 141 | mRS.validate(); |
| 142 | subData1D(0, mType.getCount(), d); |
| 143 | } |
| 144 | public void copyFrom(short[] d) { |
| 145 | mRS.validate(); |
| 146 | subData1D(0, mType.getCount(), d); |
| 147 | } |
| 148 | public void copyFrom(byte[] d) { |
| 149 | mRS.validate(); |
| 150 | subData1D(0, mType.getCount(), d); |
| 151 | } |
| 152 | public void copyFrom(float[] d) { |
| 153 | mRS.validate(); |
| 154 | subData1D(0, mType.getCount(), d); |
| 155 | } |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 156 | public void copyFrom(Bitmap b) { |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 157 | validateBitmap(b); |
| 158 | mRS.nAllocationCopyFromBitmap(getID(), b); |
| Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 161 | public void copyTo(Bitmap b) { |
| 162 | validateBitmap(b); |
| 163 | mRS.nAllocationCopyToBitmap(getID(), b); |
| 164 | } |
| 165 | |
| 166 | |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 167 | public void subData(int xoff, FieldPacker fp) { |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 168 | int eSize = mType.mElement.getSizeBytes(); |
| 169 | final byte[] data = fp.getData(); |
| 170 | |
| 171 | int count = data.length / eSize; |
| 172 | if ((eSize * count) != data.length) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 173 | throw new RSIllegalArgumentException("Field packer length " + data.length + |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 174 | " not divisible by element size " + eSize + "."); |
| 175 | } |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 176 | data1DChecks(xoff, count, data.length, data.length); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 177 | mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length); |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
| 181 | public void subElementData(int xoff, int component_number, FieldPacker fp) { |
| 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 | |
| 217 | public void subData1D(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 | } |
| 222 | public void subData1D(int off, int count, short[] d) { |
| 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 | } |
| 227 | public void subData1D(int off, int count, byte[] d) { |
| 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 | } |
| 232 | public void subData1D(int off, int count, float[] d) { |
| 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 | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 239 | 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] | 240 | mRS.validate(); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 241 | 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] | 242 | } |
| 243 | |
| 244 | 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] | 245 | mRS.validate(); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 246 | 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] | 247 | } |
| 248 | |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 249 | public void readData(int[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 250 | mRS.validate(); |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 251 | mRS.nAllocationRead(getID(), d); |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | public void readData(float[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 255 | mRS.validate(); |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 256 | mRS.nAllocationRead(getID(), d); |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 259 | public synchronized void resize(int dimX) { |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 260 | if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 261 | throw new RSInvalidStateException("Resize only support for 1D allocations at this time."); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 262 | } |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 263 | mRS.nAllocationResize1D(getID(), dimX); |
| Jason Sams | d26297f | 2010-11-01 16:08:59 -0700 | [diff] [blame] | 264 | mRS.finish(); // Necessary because resize is fifoed and update is async. |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 265 | |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 266 | int typeID = mRS.nAllocationGetType(getID()); |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 267 | mType = new Type(typeID, mRS); |
| 268 | mType.updateFromNative(); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
| 272 | public void resize(int dimX, int dimY) { |
| 273 | if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 274 | throw new RSIllegalStateException("Resize only support for 2D allocations at this time."); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 275 | } |
| 276 | if (mType.getY() == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 277 | throw new RSIllegalStateException("Resize only support for 2D allocations at this time."); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 278 | } |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 279 | mRS.nAllocationResize2D(getID(), dimX, dimY); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 280 | } |
| 281 | */ |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 282 | |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 283 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 284 | |
| 285 | // creation |
| 286 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 287 | static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options(); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 288 | static { |
| 289 | mBitmapOptions.inScaled = false; |
| 290 | } |
| 291 | |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 292 | static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mc, int usage) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 293 | rs.validate(); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 294 | if (type.getID() == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 295 | throw new RSInvalidStateException("Bad Type"); |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 296 | } |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 297 | int id = rs.nAllocationCreateTyped(type.getID(), mc.mID, usage); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 298 | if (id == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 299 | throw new RSRuntimeException("Allocation creation failed."); |
| 300 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 301 | return new Allocation(id, rs, type, usage); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| Jason Sams | e5d3712 | 2010-12-16 00:33:33 -0800 | [diff] [blame] | 304 | static public Allocation createTyped(RenderScript rs, Type type, int usage) { |
| 305 | return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage); |
| 306 | } |
| 307 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 308 | static public Allocation createTyped(RenderScript rs, Type type) { |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 309 | return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 310 | } |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 311 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 312 | static public Allocation createSized(RenderScript rs, Element e, |
| 313 | int count, int usage) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 314 | rs.validate(); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 315 | Type.Builder b = new Type.Builder(rs, e); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 316 | b.setX(count); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 317 | Type t = b.create(); |
| 318 | |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 319 | int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 320 | if (id == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 321 | throw new RSRuntimeException("Allocation creation failed."); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 322 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 323 | return new Allocation(id, rs, t, usage); |
| 324 | } |
| 325 | |
| 326 | static public Allocation createSized(RenderScript rs, Element e, int count) { |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 327 | return createSized(rs, e, count, USAGE_SCRIPT); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 330 | static Element elementFromBitmap(RenderScript rs, Bitmap b) { |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 331 | final Bitmap.Config bc = b.getConfig(); |
| 332 | if (bc == Bitmap.Config.ALPHA_8) { |
| 333 | return Element.A_8(rs); |
| 334 | } |
| 335 | if (bc == Bitmap.Config.ARGB_4444) { |
| 336 | return Element.RGBA_4444(rs); |
| 337 | } |
| 338 | if (bc == Bitmap.Config.ARGB_8888) { |
| 339 | return Element.RGBA_8888(rs); |
| 340 | } |
| 341 | if (bc == Bitmap.Config.RGB_565) { |
| 342 | return Element.RGB_565(rs); |
| 343 | } |
| Jeff Sharkey | 4bd1a3d | 2010-11-16 13:46:34 -0800 | [diff] [blame] | 344 | throw new RSInvalidStateException("Bad bitmap type: " + bc); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 347 | static Type typeFromBitmap(RenderScript rs, Bitmap b, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 348 | MipmapControl mip) { |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 349 | Element e = elementFromBitmap(rs, b); |
| 350 | Type.Builder tb = new Type.Builder(rs, e); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 351 | tb.setX(b.getWidth()); |
| 352 | tb.setY(b.getHeight()); |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 353 | tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 354 | return tb.create(); |
| 355 | } |
| 356 | |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 357 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 358 | MipmapControl mips, |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 359 | int usage) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 360 | rs.validate(); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 361 | Type t = typeFromBitmap(rs, b, mips); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 362 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 363 | int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage); |
| 364 | if (id == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 365 | throw new RSRuntimeException("Load failed."); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 366 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 367 | return new Allocation(id, rs, t, usage); |
| 368 | } |
| 369 | |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 370 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b) { |
| 371 | return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
| 372 | USAGE_GRAPHICS_TEXTURE); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 375 | static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 376 | MipmapControl mips, |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 377 | CubemapLayout layout, |
| 378 | int usage) { |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 379 | rs.validate(); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 380 | |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 381 | int height = b.getHeight(); |
| 382 | int width = b.getWidth(); |
| 383 | |
| 384 | if (layout != CubemapLayout.VERTICAL_FACE_LIST) { |
| 385 | throw new RSIllegalArgumentException("Only vertical face list supported"); |
| 386 | } |
| 387 | if (height % 6 != 0) { |
| 388 | throw new RSIllegalArgumentException("Cubemap height must be multiple of 6"); |
| 389 | } |
| 390 | if (height / 6 != width) { |
| 391 | throw new RSIllegalArgumentException("Only square cobe map faces supported"); |
| 392 | } |
| 393 | boolean isPow2 = (width & (width - 1)) == 0; |
| 394 | if (!isPow2) { |
| 395 | throw new RSIllegalArgumentException("Only power of 2 cube faces supported"); |
| 396 | } |
| 397 | |
| 398 | Element e = elementFromBitmap(rs, b); |
| 399 | Type.Builder tb = new Type.Builder(rs, e); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 400 | tb.setX(width); |
| 401 | tb.setY(width); |
| 402 | tb.setFaces(true); |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 403 | tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 404 | Type t = tb.create(); |
| 405 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 406 | int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 407 | if(id == 0) { |
| 408 | throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e); |
| 409 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 410 | return new Allocation(id, rs, t, usage); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 413 | static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b, |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 414 | CubemapLayout layout) { |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 415 | return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
| 416 | layout, USAGE_GRAPHICS_TEXTURE); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 419 | static public Allocation createFromBitmapResource(RenderScript rs, |
| 420 | Resources res, |
| 421 | int id, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 422 | MipmapControl mips, |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 423 | int usage) { |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 424 | |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 425 | rs.validate(); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 426 | Bitmap b = BitmapFactory.decodeResource(res, id); |
| 427 | Allocation alloc = createFromBitmap(rs, b, mips, usage); |
| 428 | b.recycle(); |
| 429 | return alloc; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 432 | static public Allocation createFromBitmapResource(RenderScript rs, |
| 433 | Resources res, |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 434 | int id) { |
| 435 | return createFromBitmapResource(rs, res, id, |
| 436 | MipmapControl.MIPMAP_NONE, |
| 437 | USAGE_GRAPHICS_TEXTURE); |
| 438 | } |
| 439 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 440 | static public Allocation createFromString(RenderScript rs, |
| 441 | String str, |
| 442 | int usage) { |
| 443 | rs.validate(); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 444 | byte[] allocArray = null; |
| 445 | try { |
| 446 | allocArray = str.getBytes("UTF-8"); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 447 | Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 448 | alloc.copyFrom(allocArray); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 449 | return alloc; |
| 450 | } |
| 451 | catch (Exception e) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 452 | throw new RSRuntimeException("Could not convert string to utf-8."); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 453 | } |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 454 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | |