| 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; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
| Romain Guy | 650a3eb | 2009-08-31 14:06:43 -0700 | [diff] [blame] | 22 | import android.content.res.AssetManager; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
| 24 | import android.graphics.BitmapFactory; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 25 | import android.util.Log; |
| Romain Guy | 650a3eb | 2009-08-31 14:06:43 -0700 | [diff] [blame] | 26 | import android.util.TypedValue; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| Jason Sams | a23d4e7 | 2011-01-04 18:59:12 -0800 | [diff] [blame] | 29 | * Memory allocation class for renderscript. An allocation combines a Type with |
| 30 | * memory to provide storage for user data and objects. |
| 31 | * |
| 32 | * Allocations may exist in one or more memory spaces. Currently those are |
| 33 | * Script: accessable by RS scripts. |
| 34 | * Graphics Texture: accessable as a graphics texture. |
| 35 | * Graphics Vertex: accessable as graphical vertex data. |
| 36 | * Graphics Constants: Accessable as constants in user shaders |
| 37 | * |
| 38 | * By default java side updates are always applied to the script accessable |
| 39 | * memory. If this is not present they are then applied to the various HW |
| 40 | * memory types. A syncAll call is necessary after the script data is update to |
| 41 | * keep the other memory spaces in sync. |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 42 | * |
| 43 | **/ |
| 44 | public class Allocation extends BaseObj { |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 45 | Type mType; |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 46 | Bitmap mBitmap; |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 47 | int mUsage; |
| 48 | |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 49 | /** |
| 50 | * The usage of the allocation. These signal to renderscript |
| 51 | * where to place the allocation in memory. |
| 52 | * |
| 53 | * SCRIPT The allocation will be bound to and accessed by |
| 54 | * scripts. |
| 55 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 56 | public static final int USAGE_SCRIPT = 0x0001; |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * GRAPHICS_TEXTURE The allcation will be used as a texture |
| 60 | * source by one or more graphcics programs. |
| 61 | * |
| 62 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 63 | public static final int USAGE_GRAPHICS_TEXTURE = 0x0002; |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * GRAPHICS_VERTEX The allocation will be used as a graphics |
| 67 | * mesh. |
| 68 | * |
| 69 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 70 | public static final int USAGE_GRAPHICS_VERTEX = 0x0004; |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 71 | |
| 72 | |
| 73 | /** |
| 74 | * GRAPHICS_CONSTANTS The allocation will be used as the source |
| 75 | * of shader constants by one or more programs. |
| 76 | * |
| 77 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 78 | public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008; |
| 79 | |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 80 | |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 81 | /** |
| 82 | * Controls mipmap behavior when using the bitmap creation and |
| 83 | * update functions. |
| 84 | */ |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 85 | public enum MipmapControl { |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 86 | /** |
| 87 | * No mipmaps will be generated and the type generated from the |
| 88 | * incoming bitmap will not contain additional LODs. |
| 89 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 90 | MIPMAP_NONE(0), |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * A Full mipmap chain will be created in script memory. The |
| 94 | * type of the allocation will contain a full mipmap chain. On |
| 95 | * upload to graphics the full chain will be transfered. |
| 96 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 97 | MIPMAP_FULL(1), |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * The type of the allocation will be the same as MIPMAP_NONE. |
| 101 | * It will not contain mipmaps. On upload to graphics the |
| 102 | * graphics copy of the allocation data will contain a full |
| 103 | * mipmap chain generated from the top level in script memory. |
| 104 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 105 | MIPMAP_ON_SYNC_TO_TEXTURE(2); |
| 106 | |
| 107 | int mID; |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 108 | MipmapControl(int id) { |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 109 | mID = id; |
| 110 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 113 | Allocation(int id, RenderScript rs, Type t, int usage) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 114 | super(id, rs); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 115 | if ((usage & ~(USAGE_SCRIPT | |
| 116 | USAGE_GRAPHICS_TEXTURE | |
| 117 | USAGE_GRAPHICS_VERTEX | |
| 118 | USAGE_GRAPHICS_CONSTANTS)) != 0) { |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 119 | throw new RSIllegalArgumentException("Unknown usage specified."); |
| 120 | } |
| 121 | mType = t; |
| Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 124 | @Override |
| 125 | void updateFromNative() { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 126 | super.updateFromNative(); |
| 127 | int typeID = mRS.nAllocationGetType(getID()); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 128 | if(typeID != 0) { |
| 129 | mType = new Type(typeID, mRS); |
| 130 | mType.updateFromNative(); |
| 131 | } |
| 132 | } |
| 133 | |
| Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 134 | public Type getType() { |
| 135 | return mType; |
| 136 | } |
| 137 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 138 | public void syncAll(int srcLocation) { |
| 139 | switch (srcLocation) { |
| 140 | case USAGE_SCRIPT: |
| 141 | case USAGE_GRAPHICS_CONSTANTS: |
| 142 | case USAGE_GRAPHICS_TEXTURE: |
| 143 | case USAGE_GRAPHICS_VERTEX: |
| 144 | break; |
| 145 | default: |
| 146 | throw new RSIllegalArgumentException("Source must be exactly one usage type."); |
| 147 | } |
| 148 | mRS.validate(); |
| 149 | mRS.nAllocationSyncAll(getID(), srcLocation); |
| 150 | } |
| 151 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 152 | public void copyFrom(BaseObj[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 153 | mRS.validate(); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 154 | if (d.length != mType.getCount()) { |
| 155 | throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " + |
| 156 | mType.getCount() + ", array length = " + d.length); |
| 157 | } |
| 158 | int i[] = new int[d.length]; |
| 159 | for (int ct=0; ct < d.length; ct++) { |
| 160 | i[ct] = d[ct].getID(); |
| 161 | } |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 162 | copy1DRangeFrom(0, mType.getCount(), i); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 165 | private void validateBitmap(Bitmap b) { |
| 166 | mRS.validate(); |
| 167 | if(mType.getX() != b.getWidth() || |
| 168 | mType.getY() != b.getHeight()) { |
| 169 | throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch"); |
| 170 | } |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 171 | Bitmap.Config bc = b.getConfig(); |
| 172 | switch (bc) { |
| 173 | case ALPHA_8: |
| 174 | if (mType.getElement().mKind != Element.DataKind.PIXEL_A) { |
| 175 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 176 | mType.getElement().mKind + ", type " + |
| 177 | mType.getElement().mType + |
| 178 | " of " + mType.getElement().getSizeBytes() + |
| 179 | " bytes, passed bitmap was " + bc); |
| 180 | } |
| 181 | break; |
| 182 | case ARGB_8888: |
| 183 | if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) || |
| 184 | (mType.getElement().getSizeBytes() != 4)) { |
| 185 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 186 | mType.getElement().mKind + ", type " + |
| 187 | mType.getElement().mType + |
| 188 | " of " + mType.getElement().getSizeBytes() + |
| 189 | " bytes, passed bitmap was " + bc); |
| 190 | } |
| 191 | break; |
| 192 | case RGB_565: |
| 193 | if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) || |
| 194 | (mType.getElement().getSizeBytes() != 2)) { |
| 195 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 196 | mType.getElement().mKind + ", type " + |
| 197 | mType.getElement().mType + |
| 198 | " of " + mType.getElement().getSizeBytes() + |
| 199 | " bytes, passed bitmap was " + bc); |
| 200 | } |
| 201 | break; |
| 202 | case ARGB_4444: |
| 203 | if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) || |
| 204 | (mType.getElement().getSizeBytes() != 2)) { |
| 205 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 206 | mType.getElement().mKind + ", type " + |
| 207 | mType.getElement().mType + |
| 208 | " of " + mType.getElement().getSizeBytes() + |
| 209 | " bytes, passed bitmap was " + bc); |
| 210 | } |
| 211 | break; |
| 212 | |
| 213 | } |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 216 | public void copyFrom(int[] d) { |
| 217 | mRS.validate(); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 218 | copy1DRangeFrom(0, mType.getCount(), d); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 219 | } |
| 220 | public void copyFrom(short[] d) { |
| 221 | mRS.validate(); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 222 | copy1DRangeFrom(0, mType.getCount(), d); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 223 | } |
| 224 | public void copyFrom(byte[] d) { |
| 225 | mRS.validate(); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 226 | copy1DRangeFrom(0, mType.getCount(), d); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 227 | } |
| 228 | public void copyFrom(float[] d) { |
| 229 | mRS.validate(); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 230 | copy1DRangeFrom(0, mType.getCount(), d); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 231 | } |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 232 | public void copyFrom(Bitmap b) { |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 233 | validateBitmap(b); |
| 234 | mRS.nAllocationCopyFromBitmap(getID(), b); |
| Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 237 | /** |
| 238 | * @hide |
| 239 | * |
| 240 | * This is only intended to be used by auto-generate code reflected from the |
| 241 | * renderscript script files. |
| 242 | * |
| 243 | * @param xoff |
| 244 | * @param fp |
| 245 | */ |
| 246 | public void setOneElement(int xoff, FieldPacker fp) { |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 247 | int eSize = mType.mElement.getSizeBytes(); |
| 248 | final byte[] data = fp.getData(); |
| 249 | |
| 250 | int count = data.length / eSize; |
| 251 | if ((eSize * count) != data.length) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 252 | throw new RSIllegalArgumentException("Field packer length " + data.length + |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 253 | " not divisible by element size " + eSize + "."); |
| 254 | } |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 255 | data1DChecks(xoff, count, data.length, data.length); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 256 | mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length); |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 260 | /** |
| 261 | * @hide |
| 262 | * |
| 263 | * This is only intended to be used by auto-generate code reflected from the |
| 264 | * renderscript script files. |
| 265 | * |
| 266 | * @param xoff |
| 267 | * @param component_number |
| 268 | * @param fp |
| 269 | */ |
| 270 | public void setOneComponent(int xoff, int component_number, FieldPacker fp) { |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 271 | if (component_number >= mType.mElement.mElements.length) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 272 | throw new RSIllegalArgumentException("Component_number " + component_number + " out of range."); |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 273 | } |
| 274 | if(xoff < 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 275 | throw new RSIllegalArgumentException("Offset must be >= 0."); |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | final byte[] data = fp.getData(); |
| 279 | int eSize = mType.mElement.mElements[component_number].getSizeBytes(); |
| 280 | |
| 281 | if (data.length != eSize) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 282 | throw new RSIllegalArgumentException("Field packer sizelength " + data.length + |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 283 | " does not match component size " + eSize + "."); |
| 284 | } |
| 285 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 286 | mRS.nAllocationElementData1D(getID(), xoff, 0, component_number, data, data.length); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 289 | private void data1DChecks(int off, int count, int len, int dataSize) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 290 | mRS.validate(); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 291 | if(off < 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 292 | throw new RSIllegalArgumentException("Offset must be >= 0."); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 293 | } |
| 294 | if(count < 1) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 295 | throw new RSIllegalArgumentException("Count must be >= 1."); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 296 | } |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 297 | if((off + count) > mType.getCount()) { |
| 298 | throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() + |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 299 | ", got " + count + " at offset " + off + "."); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 300 | } |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 301 | if((len) < dataSize) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 302 | throw new RSIllegalArgumentException("Array too small for allocation type."); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 303 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 306 | /** |
| 307 | * Generate a mipmap chain. Requires the type of the allocation |
| 308 | * include mipmaps. |
| 309 | * |
| 310 | * This function will generate a complete set of mipmaps from |
| 311 | * the top level lod and place them into the script memoryspace. |
| 312 | * |
| 313 | * If the allocation is also using other memory spaces a |
| 314 | * followup sync will be required. |
| 315 | */ |
| 316 | public void generateMipmaps() { |
| 317 | mRS.nAllocationGenerateMipmaps(getID()); |
| 318 | } |
| 319 | |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 320 | public void copy1DRangeFrom(int off, int count, int[] d) { |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 321 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 322 | data1DChecks(off, count, d.length * 4, dataSize); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 323 | mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 324 | } |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 325 | public void copy1DRangeFrom(int off, int count, short[] d) { |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 326 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 327 | data1DChecks(off, count, d.length * 2, dataSize); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 328 | mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 329 | } |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 330 | public void copy1DRangeFrom(int off, int count, byte[] d) { |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 331 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 332 | data1DChecks(off, count, d.length, dataSize); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 333 | mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 334 | } |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 335 | public void copy1DRangeFrom(int off, int count, float[] d) { |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 336 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 337 | data1DChecks(off, count, d.length * 4, dataSize); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 338 | mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 341 | |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 342 | /** |
| 343 | * Copy a rectanglular region from the array into the |
| 344 | * allocation. The incoming array is assumed to be tightly |
| 345 | * packed. |
| 346 | * |
| 347 | * @param xoff X offset of the region to update |
| 348 | * @param yoff Y offset of the region to update |
| 349 | * @param w Width of the incoming region to update |
| 350 | * @param h Height of the incoming region to update |
| 351 | * @param data to be placed into the allocation |
| 352 | */ |
| 353 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) { |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 354 | mRS.validate(); |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 355 | mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 358 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) { |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 359 | mRS.validate(); |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 360 | mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 2); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 363 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 364 | mRS.validate(); |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 365 | mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 368 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 369 | mRS.validate(); |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 370 | mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 373 | /** |
| 374 | * Copy a bitmap into an allocation. The height and width of |
| 375 | * the update will use the height and width of the incoming |
| 376 | * bitmap. |
| 377 | * |
| 378 | * @param xoff X offset of the region to update |
| 379 | * @param yoff Y offset of the region to update |
| 380 | * @param data the bitmap to be copied |
| 381 | */ |
| 382 | public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) { |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 383 | mRS.validate(); |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 384 | mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, data); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | |
| 388 | public void copyTo(Bitmap b) { |
| 389 | validateBitmap(b); |
| 390 | mRS.nAllocationCopyToBitmap(getID(), b); |
| 391 | } |
| 392 | |
| 393 | public void copyTo(byte[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 394 | mRS.validate(); |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 395 | mRS.nAllocationRead(getID(), d); |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 398 | public void copyTo(short[] d) { |
| 399 | mRS.validate(); |
| 400 | mRS.nAllocationRead(getID(), d); |
| 401 | } |
| 402 | |
| 403 | public void copyTo(int[] d) { |
| 404 | mRS.validate(); |
| 405 | mRS.nAllocationRead(getID(), d); |
| 406 | } |
| 407 | |
| 408 | public void copyTo(float[] d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 409 | mRS.validate(); |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 410 | mRS.nAllocationRead(getID(), d); |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 413 | /** |
| 414 | * Resize a 1D allocation. The contents of the allocation are |
| 415 | * preserved. If new elements are allocated objects are created |
| 416 | * with null contents and the new region is otherwise undefined. |
| 417 | * |
| 418 | * If the new region is smaller the references of any objects |
| 419 | * outside the new region will be released. |
| 420 | * |
| 421 | * A new type will be created with the new dimension. |
| 422 | * |
| 423 | * @param dimX The new size of the allocation. |
| 424 | */ |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 425 | public synchronized void resize(int dimX) { |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 426 | if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 427 | throw new RSInvalidStateException("Resize only support for 1D allocations at this time."); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 428 | } |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 429 | mRS.nAllocationResize1D(getID(), dimX); |
| Jason Sams | d26297f | 2010-11-01 16:08:59 -0700 | [diff] [blame] | 430 | mRS.finish(); // Necessary because resize is fifoed and update is async. |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 431 | |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 432 | int typeID = mRS.nAllocationGetType(getID()); |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 433 | mType = new Type(typeID, mRS); |
| 434 | mType.updateFromNative(); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /* |
| 438 | public void resize(int dimX, int dimY) { |
| 439 | if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 440 | throw new RSIllegalStateException("Resize only support for 2D allocations at this time."); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 441 | } |
| 442 | if (mType.getY() == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 443 | throw new RSIllegalStateException("Resize only support for 2D allocations at this time."); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 444 | } |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 445 | mRS.nAllocationResize2D(getID(), dimX, dimY); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 446 | } |
| 447 | */ |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 448 | |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 449 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 450 | |
| 451 | // creation |
| 452 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 453 | static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options(); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 454 | static { |
| 455 | mBitmapOptions.inScaled = false; |
| 456 | } |
| 457 | |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 458 | static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mc, int usage) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 459 | rs.validate(); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 460 | if (type.getID() == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 461 | throw new RSInvalidStateException("Bad Type"); |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 462 | } |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 463 | int id = rs.nAllocationCreateTyped(type.getID(), mc.mID, usage); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 464 | if (id == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 465 | throw new RSRuntimeException("Allocation creation failed."); |
| 466 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 467 | return new Allocation(id, rs, type, usage); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| Jason Sams | e5d3712 | 2010-12-16 00:33:33 -0800 | [diff] [blame] | 470 | static public Allocation createTyped(RenderScript rs, Type type, int usage) { |
| 471 | return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage); |
| 472 | } |
| 473 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 474 | static public Allocation createTyped(RenderScript rs, Type type) { |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 475 | return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 476 | } |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 477 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 478 | static public Allocation createSized(RenderScript rs, Element e, |
| 479 | int count, int usage) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 480 | rs.validate(); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 481 | Type.Builder b = new Type.Builder(rs, e); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 482 | b.setX(count); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 483 | Type t = b.create(); |
| 484 | |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 485 | int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 486 | if (id == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 487 | throw new RSRuntimeException("Allocation creation failed."); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 488 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 489 | return new Allocation(id, rs, t, usage); |
| 490 | } |
| 491 | |
| 492 | static public Allocation createSized(RenderScript rs, Element e, int count) { |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 493 | return createSized(rs, e, count, USAGE_SCRIPT); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 496 | static Element elementFromBitmap(RenderScript rs, Bitmap b) { |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 497 | final Bitmap.Config bc = b.getConfig(); |
| 498 | if (bc == Bitmap.Config.ALPHA_8) { |
| 499 | return Element.A_8(rs); |
| 500 | } |
| 501 | if (bc == Bitmap.Config.ARGB_4444) { |
| 502 | return Element.RGBA_4444(rs); |
| 503 | } |
| 504 | if (bc == Bitmap.Config.ARGB_8888) { |
| 505 | return Element.RGBA_8888(rs); |
| 506 | } |
| 507 | if (bc == Bitmap.Config.RGB_565) { |
| 508 | return Element.RGB_565(rs); |
| 509 | } |
| Jeff Sharkey | 4bd1a3d | 2010-11-16 13:46:34 -0800 | [diff] [blame] | 510 | throw new RSInvalidStateException("Bad bitmap type: " + bc); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 513 | static Type typeFromBitmap(RenderScript rs, Bitmap b, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 514 | MipmapControl mip) { |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 515 | Element e = elementFromBitmap(rs, b); |
| 516 | Type.Builder tb = new Type.Builder(rs, e); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 517 | tb.setX(b.getWidth()); |
| 518 | tb.setY(b.getHeight()); |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 519 | tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 520 | return tb.create(); |
| 521 | } |
| 522 | |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 523 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 524 | MipmapControl mips, |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 525 | int usage) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 526 | rs.validate(); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 527 | Type t = typeFromBitmap(rs, b, mips); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 528 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 529 | int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage); |
| 530 | if (id == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 531 | throw new RSRuntimeException("Load failed."); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 532 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 533 | return new Allocation(id, rs, t, usage); |
| 534 | } |
| 535 | |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 536 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b) { |
| 537 | return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
| 538 | USAGE_GRAPHICS_TEXTURE); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 539 | } |
| 540 | |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 541 | /** |
| 542 | * Creates a cubemap allocation from a bitmap containing the |
| 543 | * horizontal list of cube faces. Each individual face must be |
| 544 | * the same size and power of 2 |
| 545 | * |
| 546 | * @param rs |
| 547 | * @param b bitmap with cubemap faces layed out in the following |
| 548 | * format: right, left, top, bottom, front, back |
| 549 | * @param mips specifies desired mipmap behaviour for the cubemap |
| 550 | * @param usage bitfield specifying how the cubemap is utilized |
| 551 | * |
| 552 | **/ |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 553 | static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 554 | MipmapControl mips, |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 555 | int usage) { |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 556 | rs.validate(); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 557 | |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 558 | int height = b.getHeight(); |
| 559 | int width = b.getWidth(); |
| 560 | |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 561 | if (width % 6 != 0) { |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 562 | throw new RSIllegalArgumentException("Cubemap height must be multiple of 6"); |
| 563 | } |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 564 | if (width / 6 != height) { |
| Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 565 | throw new RSIllegalArgumentException("Only square cube map faces supported"); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 566 | } |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 567 | boolean isPow2 = (height & (height - 1)) == 0; |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 568 | if (!isPow2) { |
| 569 | throw new RSIllegalArgumentException("Only power of 2 cube faces supported"); |
| 570 | } |
| 571 | |
| 572 | Element e = elementFromBitmap(rs, b); |
| 573 | Type.Builder tb = new Type.Builder(rs, e); |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 574 | tb.setX(height); |
| 575 | tb.setY(height); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 576 | tb.setFaces(true); |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 577 | tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 578 | Type t = tb.create(); |
| 579 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 580 | int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 581 | if(id == 0) { |
| 582 | throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e); |
| 583 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 584 | return new Allocation(id, rs, t, usage); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 587 | static public Allocation createCubemapFromBitmap(RenderScript rs, |
| 588 | Bitmap b) { |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 589 | return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 590 | USAGE_GRAPHICS_TEXTURE); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 593 | static public Allocation createCubemapFromCubeFaces(RenderScript rs, |
| 594 | Bitmap xpos, |
| 595 | Bitmap xneg, |
| 596 | Bitmap ypos, |
| 597 | Bitmap yneg, |
| 598 | Bitmap zpos, |
| 599 | Bitmap zneg, |
| 600 | MipmapControl mips, |
| 601 | int usage) { |
| 602 | int height = xpos.getHeight(); |
| 603 | if (xpos.getWidth() != height || |
| 604 | xneg.getWidth() != height || xneg.getHeight() != height || |
| 605 | ypos.getWidth() != height || ypos.getHeight() != height || |
| 606 | yneg.getWidth() != height || yneg.getHeight() != height || |
| 607 | zpos.getWidth() != height || zpos.getHeight() != height || |
| 608 | zneg.getWidth() != height || zneg.getHeight() != height) { |
| 609 | throw new RSIllegalArgumentException("Only square cube map faces supported"); |
| 610 | } |
| 611 | boolean isPow2 = (height & (height - 1)) == 0; |
| 612 | if (!isPow2) { |
| 613 | throw new RSIllegalArgumentException("Only power of 2 cube faces supported"); |
| 614 | } |
| 615 | |
| 616 | Element e = elementFromBitmap(rs, xpos); |
| 617 | Type.Builder tb = new Type.Builder(rs, e); |
| 618 | tb.setX(height); |
| 619 | tb.setY(height); |
| 620 | tb.setFaces(true); |
| 621 | tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL); |
| 622 | Type t = tb.create(); |
| 623 | Allocation cubemap = Allocation.createTyped(rs, t, mips, usage); |
| 624 | |
| 625 | AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap); |
| 626 | adapter.setFace(Type.CubemapFace.POSITVE_X); |
| 627 | adapter.copyFrom(xpos); |
| 628 | adapter.setFace(Type.CubemapFace.NEGATIVE_X); |
| 629 | adapter.copyFrom(xneg); |
| 630 | adapter.setFace(Type.CubemapFace.POSITVE_Y); |
| 631 | adapter.copyFrom(ypos); |
| 632 | adapter.setFace(Type.CubemapFace.NEGATIVE_Y); |
| 633 | adapter.copyFrom(yneg); |
| 634 | adapter.setFace(Type.CubemapFace.POSITVE_Z); |
| 635 | adapter.copyFrom(zpos); |
| 636 | adapter.setFace(Type.CubemapFace.NEGATIVE_Z); |
| 637 | adapter.copyFrom(zneg); |
| 638 | |
| 639 | return cubemap; |
| 640 | } |
| 641 | |
| 642 | static public Allocation createCubemapFromCubeFaces(RenderScript rs, |
| 643 | Bitmap xpos, |
| 644 | Bitmap xneg, |
| 645 | Bitmap ypos, |
| 646 | Bitmap yneg, |
| 647 | Bitmap zpos, |
| 648 | Bitmap zneg) { |
| 649 | return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg, |
| 650 | zpos, zneg, MipmapControl.MIPMAP_NONE, |
| 651 | USAGE_GRAPHICS_TEXTURE); |
| 652 | } |
| 653 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 654 | static public Allocation createFromBitmapResource(RenderScript rs, |
| 655 | Resources res, |
| 656 | int id, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 657 | MipmapControl mips, |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 658 | int usage) { |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 659 | |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 660 | rs.validate(); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 661 | Bitmap b = BitmapFactory.decodeResource(res, id); |
| 662 | Allocation alloc = createFromBitmap(rs, b, mips, usage); |
| 663 | b.recycle(); |
| 664 | return alloc; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 667 | static public Allocation createFromBitmapResource(RenderScript rs, |
| 668 | Resources res, |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 669 | int id) { |
| 670 | return createFromBitmapResource(rs, res, id, |
| 671 | MipmapControl.MIPMAP_NONE, |
| 672 | USAGE_GRAPHICS_TEXTURE); |
| 673 | } |
| 674 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 675 | static public Allocation createFromString(RenderScript rs, |
| 676 | String str, |
| 677 | int usage) { |
| 678 | rs.validate(); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 679 | byte[] allocArray = null; |
| 680 | try { |
| 681 | allocArray = str.getBytes("UTF-8"); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 682 | Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 683 | alloc.copyFrom(allocArray); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 684 | return alloc; |
| 685 | } |
| 686 | catch (Exception e) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 687 | throw new RSRuntimeException("Could not convert string to utf-8."); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 688 | } |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 689 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | |