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 | /** |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 29 | * <p> |
| 30 | * Memory allocation class for renderscript. An allocation combines a |
| 31 | * {@link android.renderscript.Type} with the memory to provide storage for user data and objects. |
| 32 | * This implies that all memory in Renderscript is typed. |
| 33 | * </p> |
Jason Sams | a23d4e7 | 2011-01-04 18:59:12 -0800 | [diff] [blame] | 34 | * |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 35 | * <p>Allocations are the primary way data moves into and out of scripts. Memory is user |
| 36 | * synchronized and it's possible for allocations to exist in multiple memory spaces |
| 37 | * concurrently. Currently those spaces are:</p> |
| 38 | * <ul> |
| 39 | * <li>Script: accessable by RS scripts.</li> |
| 40 | * <li>Graphics Texture: accessable as a graphics texture.</li> |
| 41 | * <li>Graphics Vertex: accessable as graphical vertex data.</li> |
| 42 | * <li>Graphics Constants: Accessable as constants in user shaders</li> |
| 43 | * </ul> |
| 44 | * </p> |
| 45 | * <p> |
| 46 | * For example, when creating a allocation for a texture, the user can |
| 47 | * specify its memory spaces as both script and textures. This means that it can both |
| 48 | * be used as script binding and as a GPU texture for rendering. To maintain |
| 49 | * synchronization if a script modifies an allocation used by other targets it must |
| 50 | * call a synchronizing function to push the updates to the memory, otherwise the results |
| 51 | * are undefined. |
| 52 | * </p> |
| 53 | * <p>By default, Android system side updates are always applied to the script accessable |
| 54 | * memory. If this is not present, they are then applied to the various HW |
| 55 | * memory types. A {@link android.renderscript.Allocation#syncAll syncAll()} |
| 56 | * call is necessary after the script data is updated to |
| 57 | * keep the other memory spaces in sync.</p> |
Jason Sams | a23d4e7 | 2011-01-04 18:59:12 -0800 | [diff] [blame] | 58 | * |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 59 | * <p>Allocation data is uploaded in one of two primary ways. For simple |
| 60 | * arrays there are copyFrom() functions that take an array from the control code and |
| 61 | * copy it to the slave memory store. Both type checked and unchecked copies are provided. |
| 62 | * The unchecked variants exist to allow apps to copy over arrays of structures from a |
| 63 | * control language that does not support structures.</p> |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 64 | * |
| 65 | **/ |
| 66 | public class Allocation extends BaseObj { |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 67 | Type mType; |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 68 | Bitmap mBitmap; |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 69 | int mUsage; |
| 70 | |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 71 | /** |
| 72 | * The usage of the allocation. These signal to renderscript |
| 73 | * where to place the allocation in memory. |
| 74 | * |
| 75 | * SCRIPT The allocation will be bound to and accessed by |
| 76 | * scripts. |
| 77 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 78 | public static final int USAGE_SCRIPT = 0x0001; |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * GRAPHICS_TEXTURE The allcation will be used as a texture |
| 82 | * source by one or more graphcics programs. |
| 83 | * |
| 84 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 85 | public static final int USAGE_GRAPHICS_TEXTURE = 0x0002; |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * GRAPHICS_VERTEX The allocation will be used as a graphics |
| 89 | * mesh. |
| 90 | * |
| 91 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 92 | public static final int USAGE_GRAPHICS_VERTEX = 0x0004; |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 93 | |
| 94 | |
| 95 | /** |
| 96 | * GRAPHICS_CONSTANTS The allocation will be used as the source |
| 97 | * of shader constants by one or more programs. |
| 98 | * |
| 99 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 100 | public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008; |
| 101 | |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 102 | |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 103 | /** |
| 104 | * Controls mipmap behavior when using the bitmap creation and |
| 105 | * update functions. |
| 106 | */ |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 107 | public enum MipmapControl { |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 108 | /** |
| 109 | * No mipmaps will be generated and the type generated from the |
| 110 | * incoming bitmap will not contain additional LODs. |
| 111 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 112 | MIPMAP_NONE(0), |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * A Full mipmap chain will be created in script memory. The |
| 116 | * type of the allocation will contain a full mipmap chain. On |
| 117 | * upload to graphics the full chain will be transfered. |
| 118 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 119 | MIPMAP_FULL(1), |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * The type of the allocation will be the same as MIPMAP_NONE. |
| 123 | * It will not contain mipmaps. On upload to graphics the |
| 124 | * graphics copy of the allocation data will contain a full |
| 125 | * mipmap chain generated from the top level in script memory. |
| 126 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 127 | MIPMAP_ON_SYNC_TO_TEXTURE(2); |
| 128 | |
| 129 | int mID; |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 130 | MipmapControl(int id) { |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 131 | mID = id; |
| 132 | } |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 135 | Allocation(int id, RenderScript rs, Type t, int usage) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 136 | super(id, rs); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 137 | if ((usage & ~(USAGE_SCRIPT | |
| 138 | USAGE_GRAPHICS_TEXTURE | |
| 139 | USAGE_GRAPHICS_VERTEX | |
| 140 | USAGE_GRAPHICS_CONSTANTS)) != 0) { |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 141 | throw new RSIllegalArgumentException("Unknown usage specified."); |
| 142 | } |
| 143 | mType = t; |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 146 | private void validateIsInt32() { |
| 147 | if ((mType.mElement.mType == Element.DataType.SIGNED_32) || |
| 148 | (mType.mElement.mType == Element.DataType.UNSIGNED_32)) { |
| 149 | return; |
| 150 | } |
| 151 | throw new RSIllegalArgumentException( |
| 152 | "32 bit integer source does not match allocation type " + mType.mElement.mType); |
| 153 | } |
| 154 | |
| 155 | private void validateIsInt16() { |
| 156 | if ((mType.mElement.mType == Element.DataType.SIGNED_16) || |
| 157 | (mType.mElement.mType == Element.DataType.UNSIGNED_16)) { |
| 158 | return; |
| 159 | } |
| 160 | throw new RSIllegalArgumentException( |
| 161 | "16 bit integer source does not match allocation type " + mType.mElement.mType); |
| 162 | } |
| 163 | |
| 164 | private void validateIsInt8() { |
| 165 | if ((mType.mElement.mType == Element.DataType.SIGNED_8) || |
| 166 | (mType.mElement.mType == Element.DataType.UNSIGNED_8)) { |
| 167 | return; |
| 168 | } |
| 169 | throw new RSIllegalArgumentException( |
| 170 | "8 bit integer source does not match allocation type " + mType.mElement.mType); |
| 171 | } |
| 172 | |
| 173 | private void validateIsFloat32() { |
| 174 | if (mType.mElement.mType == Element.DataType.FLOAT_32) { |
| 175 | return; |
| 176 | } |
| 177 | throw new RSIllegalArgumentException( |
| 178 | "32 bit float source does not match allocation type " + mType.mElement.mType); |
| 179 | } |
| 180 | |
| 181 | private void validateIsObject() { |
| 182 | if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) || |
| 183 | (mType.mElement.mType == Element.DataType.RS_TYPE) || |
| 184 | (mType.mElement.mType == Element.DataType.RS_ALLOCATION) || |
| 185 | (mType.mElement.mType == Element.DataType.RS_SAMPLER) || |
| 186 | (mType.mElement.mType == Element.DataType.RS_SCRIPT) || |
| 187 | (mType.mElement.mType == Element.DataType.RS_MESH) || |
| 188 | (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) || |
| 189 | (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) || |
| 190 | (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) || |
| 191 | (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) { |
| 192 | return; |
| 193 | } |
| 194 | throw new RSIllegalArgumentException( |
| 195 | "Object source does not match allocation type " + mType.mElement.mType); |
| 196 | } |
| 197 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 198 | @Override |
| 199 | void updateFromNative() { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 200 | super.updateFromNative(); |
| 201 | int typeID = mRS.nAllocationGetType(getID()); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 202 | if(typeID != 0) { |
| 203 | mType = new Type(typeID, mRS); |
| 204 | mType.updateFromNative(); |
| 205 | } |
| 206 | } |
| 207 | |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 208 | public Type getType() { |
| 209 | return mType; |
| 210 | } |
| 211 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 212 | public void syncAll(int srcLocation) { |
| 213 | switch (srcLocation) { |
| 214 | case USAGE_SCRIPT: |
| 215 | case USAGE_GRAPHICS_CONSTANTS: |
| 216 | case USAGE_GRAPHICS_TEXTURE: |
| 217 | case USAGE_GRAPHICS_VERTEX: |
| 218 | break; |
| 219 | default: |
| 220 | throw new RSIllegalArgumentException("Source must be exactly one usage type."); |
| 221 | } |
| 222 | mRS.validate(); |
| 223 | mRS.nAllocationSyncAll(getID(), srcLocation); |
| 224 | } |
| 225 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 226 | public void copyFrom(BaseObj[] d) { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 227 | mRS.validate(); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 228 | validateIsObject(); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 229 | if (d.length != mType.getCount()) { |
| 230 | throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " + |
| 231 | mType.getCount() + ", array length = " + d.length); |
| 232 | } |
| 233 | int i[] = new int[d.length]; |
| 234 | for (int ct=0; ct < d.length; ct++) { |
| 235 | i[ct] = d[ct].getID(); |
| 236 | } |
Jason Sams | ed5bab9 | 2011-01-21 13:08:02 -0800 | [diff] [blame] | 237 | copy1DRangeFromUnchecked(0, mType.getCount(), i); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 240 | private void validateBitmapFormat(Bitmap b) { |
Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 241 | Bitmap.Config bc = b.getConfig(); |
| 242 | switch (bc) { |
| 243 | case ALPHA_8: |
| 244 | if (mType.getElement().mKind != Element.DataKind.PIXEL_A) { |
| 245 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 246 | mType.getElement().mKind + ", type " + |
| 247 | mType.getElement().mType + |
| 248 | " of " + mType.getElement().getSizeBytes() + |
| 249 | " bytes, passed bitmap was " + bc); |
| 250 | } |
| 251 | break; |
| 252 | case ARGB_8888: |
| 253 | if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) || |
| 254 | (mType.getElement().getSizeBytes() != 4)) { |
| 255 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 256 | mType.getElement().mKind + ", type " + |
| 257 | mType.getElement().mType + |
| 258 | " of " + mType.getElement().getSizeBytes() + |
| 259 | " bytes, passed bitmap was " + bc); |
| 260 | } |
| 261 | break; |
| 262 | case RGB_565: |
| 263 | if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) || |
| 264 | (mType.getElement().getSizeBytes() != 2)) { |
| 265 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 266 | mType.getElement().mKind + ", type " + |
| 267 | mType.getElement().mType + |
| 268 | " of " + mType.getElement().getSizeBytes() + |
| 269 | " bytes, passed bitmap was " + bc); |
| 270 | } |
| 271 | break; |
| 272 | case ARGB_4444: |
| 273 | if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) || |
| 274 | (mType.getElement().getSizeBytes() != 2)) { |
| 275 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 276 | mType.getElement().mKind + ", type " + |
| 277 | mType.getElement().mType + |
| 278 | " of " + mType.getElement().getSizeBytes() + |
| 279 | " bytes, passed bitmap was " + bc); |
| 280 | } |
| 281 | break; |
| 282 | |
| 283 | } |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 286 | private void validateBitmapSize(Bitmap b) { |
| 287 | if(mType.getX() != b.getWidth() || |
| 288 | mType.getY() != b.getHeight()) { |
| 289 | throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch"); |
| 290 | } |
| 291 | } |
| 292 | |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 293 | /** |
| 294 | * Copy an allocation from an array. This variant is not type |
| 295 | * checked which allows an application to fill in structured |
| 296 | * data from an array. |
| 297 | * |
| 298 | * @param d the source data array |
| 299 | */ |
| 300 | public void copyFromUnchecked(int[] d) { |
| 301 | mRS.validate(); |
| 302 | copy1DRangeFromUnchecked(0, mType.getCount(), d); |
| 303 | } |
| 304 | /** |
| 305 | * Copy an allocation from an array. This variant is not type |
| 306 | * checked which allows an application to fill in structured |
| 307 | * data from an array. |
| 308 | * |
| 309 | * @param d the source data array |
| 310 | */ |
| 311 | public void copyFromUnchecked(short[] d) { |
| 312 | mRS.validate(); |
| 313 | copy1DRangeFromUnchecked(0, mType.getCount(), d); |
| 314 | } |
| 315 | /** |
| 316 | * Copy an allocation from an array. This variant is not type |
| 317 | * checked which allows an application to fill in structured |
| 318 | * data from an array. |
| 319 | * |
| 320 | * @param d the source data array |
| 321 | */ |
| 322 | public void copyFromUnchecked(byte[] d) { |
| 323 | mRS.validate(); |
| 324 | copy1DRangeFromUnchecked(0, mType.getCount(), d); |
| 325 | } |
| 326 | /** |
| 327 | * Copy an allocation from an array. This variant is not type |
| 328 | * checked which allows an application to fill in structured |
| 329 | * data from an array. |
| 330 | * |
| 331 | * @param d the source data array |
| 332 | */ |
| 333 | public void copyFromUnchecked(float[] d) { |
| 334 | mRS.validate(); |
| 335 | copy1DRangeFromUnchecked(0, mType.getCount(), d); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Copy an allocation from an array. This variant is type |
| 340 | * checked and will generate exceptions if the Allocation type |
| 341 | * is not a 32 bit integer type. |
| 342 | * |
| 343 | * @param d the source data array |
| 344 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 345 | public void copyFrom(int[] d) { |
| 346 | mRS.validate(); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 347 | copy1DRangeFrom(0, mType.getCount(), d); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 348 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 349 | |
| 350 | /** |
| 351 | * Copy an allocation from an array. This variant is type |
| 352 | * checked and will generate exceptions if the Allocation type |
| 353 | * is not a 16 bit integer type. |
| 354 | * |
| 355 | * @param d the source data array |
| 356 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 357 | public void copyFrom(short[] d) { |
| 358 | mRS.validate(); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 359 | copy1DRangeFrom(0, mType.getCount(), d); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 360 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 361 | |
| 362 | /** |
| 363 | * Copy an allocation from an array. This variant is type |
| 364 | * checked and will generate exceptions if the Allocation type |
| 365 | * is not a 8 bit integer type. |
| 366 | * |
| 367 | * @param d the source data array |
| 368 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 369 | public void copyFrom(byte[] d) { |
| 370 | mRS.validate(); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 371 | copy1DRangeFrom(0, mType.getCount(), d); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 372 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 373 | |
| 374 | /** |
| 375 | * Copy an allocation from an array. This variant is type |
| 376 | * checked and will generate exceptions if the Allocation type |
| 377 | * is not a 32 bit float type. |
| 378 | * |
| 379 | * @param d the source data array |
| 380 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 381 | public void copyFrom(float[] d) { |
| 382 | mRS.validate(); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 383 | copy1DRangeFrom(0, mType.getCount(), d); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 384 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 385 | |
| 386 | /** |
| 387 | * Copy an allocation from a bitmap. The height, width, and |
| 388 | * format of the bitmap must match the existing allocation. |
| 389 | * |
| 390 | * @param b the source bitmap |
| 391 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 392 | public void copyFrom(Bitmap b) { |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 393 | mRS.validate(); |
| 394 | validateBitmapSize(b); |
| 395 | validateBitmapFormat(b); |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 396 | mRS.nAllocationCopyFromBitmap(getID(), b); |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 399 | /** |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 400 | * This is only intended to be used by auto-generate code reflected from the |
| 401 | * renderscript script files. |
| 402 | * |
| 403 | * @param xoff |
| 404 | * @param fp |
| 405 | */ |
Jason Sams | 21b4103 | 2011-01-16 15:05:41 -0800 | [diff] [blame] | 406 | public void setFromFieldPacker(int xoff, FieldPacker fp) { |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 407 | int eSize = mType.mElement.getSizeBytes(); |
| 408 | final byte[] data = fp.getData(); |
| 409 | |
| 410 | int count = data.length / eSize; |
| 411 | if ((eSize * count) != data.length) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 412 | throw new RSIllegalArgumentException("Field packer length " + data.length + |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 413 | " not divisible by element size " + eSize + "."); |
| 414 | } |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 415 | data1DChecks(xoff, count, data.length, data.length); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 416 | mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 419 | /** |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 420 | * This is only intended to be used by auto-generate code reflected from the |
| 421 | * renderscript script files. |
| 422 | * |
| 423 | * @param xoff |
| 424 | * @param component_number |
| 425 | * @param fp |
| 426 | */ |
Jason Sams | 21b4103 | 2011-01-16 15:05:41 -0800 | [diff] [blame] | 427 | public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) { |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 428 | if (component_number >= mType.mElement.mElements.length) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 429 | throw new RSIllegalArgumentException("Component_number " + component_number + " out of range."); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 430 | } |
| 431 | if(xoff < 0) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 432 | throw new RSIllegalArgumentException("Offset must be >= 0."); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | final byte[] data = fp.getData(); |
| 436 | int eSize = mType.mElement.mElements[component_number].getSizeBytes(); |
| 437 | |
| 438 | if (data.length != eSize) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 439 | throw new RSIllegalArgumentException("Field packer sizelength " + data.length + |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 440 | " does not match component size " + eSize + "."); |
| 441 | } |
| 442 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 443 | mRS.nAllocationElementData1D(getID(), xoff, 0, component_number, data, data.length); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 446 | private void data1DChecks(int off, int count, int len, int dataSize) { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 447 | mRS.validate(); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 448 | if(off < 0) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 449 | throw new RSIllegalArgumentException("Offset must be >= 0."); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 450 | } |
| 451 | if(count < 1) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 452 | throw new RSIllegalArgumentException("Count must be >= 1."); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 453 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 454 | if((off + count) > mType.getCount()) { |
| 455 | throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() + |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 456 | ", got " + count + " at offset " + off + "."); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 457 | } |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 458 | if((len) < dataSize) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 459 | throw new RSIllegalArgumentException("Array too small for allocation type."); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 460 | } |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 463 | /** |
| 464 | * Generate a mipmap chain. Requires the type of the allocation |
| 465 | * include mipmaps. |
| 466 | * |
| 467 | * This function will generate a complete set of mipmaps from |
| 468 | * the top level lod and place them into the script memoryspace. |
| 469 | * |
| 470 | * If the allocation is also using other memory spaces a |
| 471 | * followup sync will be required. |
| 472 | */ |
| 473 | public void generateMipmaps() { |
| 474 | mRS.nAllocationGenerateMipmaps(getID()); |
| 475 | } |
| 476 | |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 477 | /** |
| 478 | * Copy part of an allocation from an array. This variant is |
| 479 | * not type checked which allows an application to fill in |
| 480 | * structured data from an array. |
| 481 | * |
| 482 | * @param off The offset of the first element to be copied. |
| 483 | * @param count The number of elements to be copied. |
| 484 | * @param d the source data array |
| 485 | */ |
| 486 | public void copy1DRangeFromUnchecked(int off, int count, int[] d) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 487 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 488 | data1DChecks(off, count, d.length * 4, dataSize); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 489 | mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 490 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 491 | /** |
| 492 | * Copy part of an allocation from an array. This variant is |
| 493 | * not type checked which allows an application to fill in |
| 494 | * structured data from an array. |
| 495 | * |
| 496 | * @param off The offset of the first element to be copied. |
| 497 | * @param count The number of elements to be copied. |
| 498 | * @param d the source data array |
| 499 | */ |
| 500 | public void copy1DRangeFromUnchecked(int off, int count, short[] d) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 501 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 502 | data1DChecks(off, count, d.length * 2, dataSize); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 503 | mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 504 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 505 | /** |
| 506 | * Copy part of an allocation from an array. This variant is |
| 507 | * not type checked which allows an application to fill in |
| 508 | * structured data from an array. |
| 509 | * |
| 510 | * @param off The offset of the first element to be copied. |
| 511 | * @param count The number of elements to be copied. |
| 512 | * @param d the source data array |
| 513 | */ |
| 514 | public void copy1DRangeFromUnchecked(int off, int count, byte[] d) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 515 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 516 | data1DChecks(off, count, d.length, dataSize); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 517 | mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 518 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 519 | /** |
| 520 | * Copy part of an allocation from an array. This variant is |
| 521 | * not type checked which allows an application to fill in |
| 522 | * structured data from an array. |
| 523 | * |
| 524 | * @param off The offset of the first element to be copied. |
| 525 | * @param count The number of elements to be copied. |
| 526 | * @param d the source data array |
| 527 | */ |
| 528 | public void copy1DRangeFromUnchecked(int off, int count, float[] d) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 529 | int dataSize = mType.mElement.getSizeBytes() * count; |
| 530 | data1DChecks(off, count, d.length * 4, dataSize); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 531 | mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 534 | /** |
| 535 | * Copy part of an allocation from an array. This variant is |
| 536 | * type checked and will generate exceptions if the Allocation |
| 537 | * type is not a 32 bit integer type. |
| 538 | * |
| 539 | * @param off The offset of the first element to be copied. |
| 540 | * @param count The number of elements to be copied. |
| 541 | * @param d the source data array |
| 542 | */ |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 543 | public void copy1DRangeFrom(int off, int count, int[] d) { |
| 544 | validateIsInt32(); |
| 545 | copy1DRangeFromUnchecked(off, count, d); |
| 546 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 547 | |
| 548 | /** |
| 549 | * Copy part of an allocation from an array. This variant is |
| 550 | * type checked and will generate exceptions if the Allocation |
| 551 | * type is not a 16 bit integer type. |
| 552 | * |
| 553 | * @param off The offset of the first element to be copied. |
| 554 | * @param count The number of elements to be copied. |
| 555 | * @param d the source data array |
| 556 | */ |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 557 | public void copy1DRangeFrom(int off, int count, short[] d) { |
| 558 | validateIsInt16(); |
| 559 | copy1DRangeFromUnchecked(off, count, d); |
| 560 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 561 | |
| 562 | /** |
| 563 | * Copy part of an allocation from an array. This variant is |
| 564 | * type checked and will generate exceptions if the Allocation |
| 565 | * type is not a 8 bit integer type. |
| 566 | * |
| 567 | * @param off The offset of the first element to be copied. |
| 568 | * @param count The number of elements to be copied. |
| 569 | * @param d the source data array |
| 570 | */ |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 571 | public void copy1DRangeFrom(int off, int count, byte[] d) { |
| 572 | validateIsInt8(); |
| 573 | copy1DRangeFromUnchecked(off, count, d); |
| 574 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 575 | |
| 576 | /** |
| 577 | * Copy part of an allocation from an array. This variant is |
| 578 | * type checked and will generate exceptions if the Allocation |
| 579 | * type is not a 32 bit float type. |
| 580 | * |
| 581 | * @param off The offset of the first element to be copied. |
| 582 | * @param count The number of elements to be copied. |
| 583 | * @param d the source data array |
| 584 | */ |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 585 | public void copy1DRangeFrom(int off, int count, float[] d) { |
| 586 | validateIsFloat32(); |
| 587 | copy1DRangeFromUnchecked(off, count, d); |
| 588 | } |
| 589 | |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 590 | private void validate2DRange(int xoff, int yoff, int w, int h) { |
| 591 | if (xoff < 0 || yoff < 0) { |
| 592 | throw new RSIllegalArgumentException("Offset cannot be negative."); |
| 593 | } |
| 594 | if (h < 0 || w < 0) { |
| 595 | throw new RSIllegalArgumentException("Height or width cannot be negative."); |
| 596 | } |
| 597 | if ((xoff + w) > mType.mDimX || |
| 598 | (yoff + h) > mType.mDimY) { |
| 599 | throw new RSIllegalArgumentException("Updated region larger than allocation."); |
| 600 | } |
| 601 | } |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 602 | |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 603 | /** |
| 604 | * Copy a rectanglular region from the array into the |
| 605 | * allocation. The incoming array is assumed to be tightly |
| 606 | * packed. |
| 607 | * |
| 608 | * @param xoff X offset of the region to update |
| 609 | * @param yoff Y offset of the region to update |
| 610 | * @param w Width of the incoming region to update |
| 611 | * @param h Height of the incoming region to update |
| 612 | * @param data to be placed into the allocation |
| 613 | */ |
| 614 | 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] | 615 | mRS.validate(); |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 616 | validate2DRange(xoff, yoff, w, h); |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 617 | mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 620 | 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] | 621 | mRS.validate(); |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 622 | validate2DRange(xoff, yoff, w, h); |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 623 | 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] | 624 | } |
| 625 | |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 626 | 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] | 627 | mRS.validate(); |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 628 | validate2DRange(xoff, yoff, w, h); |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 629 | 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] | 630 | } |
| 631 | |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 632 | 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] | 633 | mRS.validate(); |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 634 | validate2DRange(xoff, yoff, w, h); |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 635 | 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] | 636 | } |
| 637 | |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 638 | /** |
| 639 | * Copy a bitmap into an allocation. The height and width of |
| 640 | * the update will use the height and width of the incoming |
| 641 | * bitmap. |
| 642 | * |
| 643 | * @param xoff X offset of the region to update |
| 644 | * @param yoff Y offset of the region to update |
| 645 | * @param data the bitmap to be copied |
| 646 | */ |
| 647 | public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) { |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 648 | mRS.validate(); |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 649 | validateBitmapFormat(data); |
| 650 | validate2DRange(xoff, yoff, data.getWidth(), data.getHeight()); |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 651 | mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, data); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | |
| 655 | public void copyTo(Bitmap b) { |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 656 | mRS.validate(); |
| 657 | validateBitmapFormat(b); |
| 658 | validateBitmapSize(b); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 659 | mRS.nAllocationCopyToBitmap(getID(), b); |
| 660 | } |
| 661 | |
| 662 | public void copyTo(byte[] d) { |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 663 | validateIsInt8(); |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 664 | mRS.validate(); |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 665 | mRS.nAllocationRead(getID(), d); |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 668 | public void copyTo(short[] d) { |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 669 | validateIsInt16(); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 670 | mRS.validate(); |
| 671 | mRS.nAllocationRead(getID(), d); |
| 672 | } |
| 673 | |
| 674 | public void copyTo(int[] d) { |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 675 | validateIsInt32(); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 676 | mRS.validate(); |
| 677 | mRS.nAllocationRead(getID(), d); |
| 678 | } |
| 679 | |
| 680 | public void copyTo(float[] d) { |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 681 | validateIsFloat32(); |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 682 | mRS.validate(); |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 683 | mRS.nAllocationRead(getID(), d); |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 686 | /** |
| 687 | * Resize a 1D allocation. The contents of the allocation are |
| 688 | * preserved. If new elements are allocated objects are created |
| 689 | * with null contents and the new region is otherwise undefined. |
| 690 | * |
| 691 | * If the new region is smaller the references of any objects |
| 692 | * outside the new region will be released. |
| 693 | * |
| 694 | * A new type will be created with the new dimension. |
| 695 | * |
| 696 | * @param dimX The new size of the allocation. |
| 697 | */ |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 698 | public synchronized void resize(int dimX) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 699 | if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 700 | throw new RSInvalidStateException("Resize only support for 1D allocations at this time."); |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 701 | } |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 702 | mRS.nAllocationResize1D(getID(), dimX); |
Jason Sams | d26297f | 2010-11-01 16:08:59 -0700 | [diff] [blame] | 703 | mRS.finish(); // Necessary because resize is fifoed and update is async. |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 704 | |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 705 | int typeID = mRS.nAllocationGetType(getID()); |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 706 | mType = new Type(typeID, mRS); |
| 707 | mType.updateFromNative(); |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* |
| 711 | public void resize(int dimX, int dimY) { |
| 712 | if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 713 | throw new RSIllegalStateException("Resize only support for 2D allocations at this time."); |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 714 | } |
| 715 | if (mType.getY() == 0) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 716 | throw new RSIllegalStateException("Resize only support for 2D allocations at this time."); |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 717 | } |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 718 | mRS.nAllocationResize2D(getID(), dimX, dimY); |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 719 | } |
| 720 | */ |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 721 | |
Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 722 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 723 | |
| 724 | // creation |
| 725 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 726 | static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options(); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 727 | static { |
| 728 | mBitmapOptions.inScaled = false; |
| 729 | } |
| 730 | |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 731 | /** |
| 732 | * |
| 733 | * @param type renderscript type describing data layout |
| 734 | * @param mips specifies desired mipmap behaviour for the |
| 735 | * allocation |
| 736 | * @param usage bit field specifying how the allocation is |
| 737 | * utilized |
| 738 | */ |
| 739 | static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 740 | rs.validate(); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 741 | if (type.getID() == 0) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 742 | throw new RSInvalidStateException("Bad Type"); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 743 | } |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 744 | int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 745 | if (id == 0) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 746 | throw new RSRuntimeException("Allocation creation failed."); |
| 747 | } |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 748 | return new Allocation(id, rs, type, usage); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 751 | /** |
| 752 | * Creates a renderscript allocation with the size specified by |
| 753 | * the type and no mipmaps generated by default |
| 754 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 755 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 756 | * @param type renderscript type describing data layout |
| 757 | * @param usage bit field specifying how the allocation is |
| 758 | * utilized |
| 759 | * |
| 760 | * @return allocation |
| 761 | */ |
Jason Sams | e5d3712 | 2010-12-16 00:33:33 -0800 | [diff] [blame] | 762 | static public Allocation createTyped(RenderScript rs, Type type, int usage) { |
| 763 | return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage); |
| 764 | } |
| 765 | |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 766 | /** |
| 767 | * Creates a renderscript allocation for use by the script with |
| 768 | * the size specified by the type and no mipmaps generated by |
| 769 | * default |
| 770 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 771 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 772 | * @param type renderscript type describing data layout |
| 773 | * |
| 774 | * @return allocation |
| 775 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 776 | static public Allocation createTyped(RenderScript rs, Type type) { |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 777 | return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 778 | } |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 779 | |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 780 | /** |
| 781 | * Creates a renderscript allocation with a specified number of |
| 782 | * given elements |
| 783 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 784 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 785 | * @param e describes what each element of an allocation is |
| 786 | * @param count specifies the number of element in the allocation |
| 787 | * @param usage bit field specifying how the allocation is |
| 788 | * utilized |
| 789 | * |
| 790 | * @return allocation |
| 791 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 792 | static public Allocation createSized(RenderScript rs, Element e, |
| 793 | int count, int usage) { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 794 | rs.validate(); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 795 | Type.Builder b = new Type.Builder(rs, e); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 796 | b.setX(count); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 797 | Type t = b.create(); |
| 798 | |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 799 | int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 800 | if (id == 0) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 801 | throw new RSRuntimeException("Allocation creation failed."); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 802 | } |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 803 | return new Allocation(id, rs, t, usage); |
| 804 | } |
| 805 | |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 806 | /** |
| 807 | * Creates a renderscript allocation with a specified number of |
| 808 | * given elements |
| 809 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 810 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 811 | * @param e describes what each element of an allocation is |
| 812 | * @param count specifies the number of element in the allocation |
| 813 | * |
| 814 | * @return allocation |
| 815 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 816 | static public Allocation createSized(RenderScript rs, Element e, int count) { |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 817 | return createSized(rs, e, count, USAGE_SCRIPT); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 820 | static Element elementFromBitmap(RenderScript rs, Bitmap b) { |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 821 | final Bitmap.Config bc = b.getConfig(); |
| 822 | if (bc == Bitmap.Config.ALPHA_8) { |
| 823 | return Element.A_8(rs); |
| 824 | } |
| 825 | if (bc == Bitmap.Config.ARGB_4444) { |
| 826 | return Element.RGBA_4444(rs); |
| 827 | } |
| 828 | if (bc == Bitmap.Config.ARGB_8888) { |
| 829 | return Element.RGBA_8888(rs); |
| 830 | } |
| 831 | if (bc == Bitmap.Config.RGB_565) { |
| 832 | return Element.RGB_565(rs); |
| 833 | } |
Jeff Sharkey | 4bd1a3d | 2010-11-16 13:46:34 -0800 | [diff] [blame] | 834 | throw new RSInvalidStateException("Bad bitmap type: " + bc); |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 837 | static Type typeFromBitmap(RenderScript rs, Bitmap b, |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 838 | MipmapControl mip) { |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 839 | Element e = elementFromBitmap(rs, b); |
| 840 | Type.Builder tb = new Type.Builder(rs, e); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 841 | tb.setX(b.getWidth()); |
| 842 | tb.setY(b.getHeight()); |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 843 | tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL); |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 844 | return tb.create(); |
| 845 | } |
| 846 | |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 847 | /** |
| 848 | * Creates a renderscript allocation from a bitmap |
| 849 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 850 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 851 | * @param b bitmap source for the allocation data |
| 852 | * @param mips specifies desired mipmap behaviour for the |
| 853 | * allocation |
| 854 | * @param usage bit field specifying how the allocation is |
| 855 | * utilized |
| 856 | * |
| 857 | * @return renderscript allocation containing bitmap data |
| 858 | * |
| 859 | */ |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 860 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b, |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 861 | MipmapControl mips, |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 862 | int usage) { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 863 | rs.validate(); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 864 | Type t = typeFromBitmap(rs, b, mips); |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 865 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 866 | int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage); |
| 867 | if (id == 0) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 868 | throw new RSRuntimeException("Load failed."); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 869 | } |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 870 | return new Allocation(id, rs, t, usage); |
| 871 | } |
| 872 | |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 873 | /** |
| 874 | * Creates a non-mipmapped renderscript allocation to use as a |
| 875 | * graphics texture |
| 876 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 877 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 878 | * @param b bitmap source for the allocation data |
| 879 | * |
| 880 | * @return renderscript allocation containing bitmap data |
| 881 | * |
| 882 | */ |
Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 883 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b) { |
| 884 | return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
| 885 | USAGE_GRAPHICS_TEXTURE); |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 886 | } |
| 887 | |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 888 | /** |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 889 | * Creates a cubemap allocation from a bitmap containing the |
| 890 | * horizontal list of cube faces. Each individual face must be |
| 891 | * the same size and power of 2 |
| 892 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 893 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 894 | * @param b bitmap with cubemap faces layed out in the following |
| 895 | * format: right, left, top, bottom, front, back |
| 896 | * @param mips specifies desired mipmap behaviour for the cubemap |
| 897 | * @param usage bit field specifying how the cubemap is utilized |
| 898 | * |
| 899 | * @return allocation containing cubemap data |
| 900 | * |
| 901 | */ |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 902 | static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b, |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 903 | MipmapControl mips, |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 904 | int usage) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 905 | rs.validate(); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 906 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 907 | int height = b.getHeight(); |
| 908 | int width = b.getWidth(); |
| 909 | |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 910 | if (width % 6 != 0) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 911 | throw new RSIllegalArgumentException("Cubemap height must be multiple of 6"); |
| 912 | } |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 913 | if (width / 6 != height) { |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 914 | throw new RSIllegalArgumentException("Only square cube map faces supported"); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 915 | } |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 916 | boolean isPow2 = (height & (height - 1)) == 0; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 917 | if (!isPow2) { |
| 918 | throw new RSIllegalArgumentException("Only power of 2 cube faces supported"); |
| 919 | } |
| 920 | |
| 921 | Element e = elementFromBitmap(rs, b); |
| 922 | Type.Builder tb = new Type.Builder(rs, e); |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 923 | tb.setX(height); |
| 924 | tb.setY(height); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 925 | tb.setFaces(true); |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 926 | tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 927 | Type t = tb.create(); |
| 928 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 929 | int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 930 | if(id == 0) { |
| 931 | throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e); |
| 932 | } |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 933 | return new Allocation(id, rs, t, usage); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 934 | } |
| 935 | |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 936 | /** |
| 937 | * Creates a non-mipmapped cubemap allocation for use as a |
| 938 | * graphics texture from a bitmap containing the horizontal list |
| 939 | * of cube faces. Each individual face must be the same size and |
| 940 | * power of 2 |
| 941 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 942 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 943 | * @param b bitmap with cubemap faces layed out in the following |
| 944 | * format: right, left, top, bottom, front, back |
| 945 | * |
| 946 | * @return allocation containing cubemap data |
| 947 | * |
| 948 | */ |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 949 | static public Allocation createCubemapFromBitmap(RenderScript rs, |
| 950 | Bitmap b) { |
Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 951 | return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 952 | USAGE_GRAPHICS_TEXTURE); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 953 | } |
| 954 | |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 955 | /** |
| 956 | * Creates a cubemap allocation from 6 bitmaps containing |
| 957 | * the cube faces. All the faces must be the same size and |
| 958 | * power of 2 |
| 959 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 960 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 961 | * @param xpos cubemap face in the positive x direction |
| 962 | * @param xneg cubemap face in the negative x direction |
| 963 | * @param ypos cubemap face in the positive y direction |
| 964 | * @param yneg cubemap face in the negative y direction |
| 965 | * @param zpos cubemap face in the positive z direction |
| 966 | * @param zneg cubemap face in the negative z direction |
| 967 | * @param mips specifies desired mipmap behaviour for the cubemap |
| 968 | * @param usage bit field specifying how the cubemap is utilized |
| 969 | * |
| 970 | * @return allocation containing cubemap data |
| 971 | * |
| 972 | */ |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 973 | static public Allocation createCubemapFromCubeFaces(RenderScript rs, |
| 974 | Bitmap xpos, |
| 975 | Bitmap xneg, |
| 976 | Bitmap ypos, |
| 977 | Bitmap yneg, |
| 978 | Bitmap zpos, |
| 979 | Bitmap zneg, |
| 980 | MipmapControl mips, |
| 981 | int usage) { |
| 982 | int height = xpos.getHeight(); |
| 983 | if (xpos.getWidth() != height || |
| 984 | xneg.getWidth() != height || xneg.getHeight() != height || |
| 985 | ypos.getWidth() != height || ypos.getHeight() != height || |
| 986 | yneg.getWidth() != height || yneg.getHeight() != height || |
| 987 | zpos.getWidth() != height || zpos.getHeight() != height || |
| 988 | zneg.getWidth() != height || zneg.getHeight() != height) { |
| 989 | throw new RSIllegalArgumentException("Only square cube map faces supported"); |
| 990 | } |
| 991 | boolean isPow2 = (height & (height - 1)) == 0; |
| 992 | if (!isPow2) { |
| 993 | throw new RSIllegalArgumentException("Only power of 2 cube faces supported"); |
| 994 | } |
| 995 | |
| 996 | Element e = elementFromBitmap(rs, xpos); |
| 997 | Type.Builder tb = new Type.Builder(rs, e); |
| 998 | tb.setX(height); |
| 999 | tb.setY(height); |
| 1000 | tb.setFaces(true); |
| 1001 | tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL); |
| 1002 | Type t = tb.create(); |
| 1003 | Allocation cubemap = Allocation.createTyped(rs, t, mips, usage); |
| 1004 | |
| 1005 | AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap); |
| 1006 | adapter.setFace(Type.CubemapFace.POSITVE_X); |
| 1007 | adapter.copyFrom(xpos); |
| 1008 | adapter.setFace(Type.CubemapFace.NEGATIVE_X); |
| 1009 | adapter.copyFrom(xneg); |
| 1010 | adapter.setFace(Type.CubemapFace.POSITVE_Y); |
| 1011 | adapter.copyFrom(ypos); |
| 1012 | adapter.setFace(Type.CubemapFace.NEGATIVE_Y); |
| 1013 | adapter.copyFrom(yneg); |
| 1014 | adapter.setFace(Type.CubemapFace.POSITVE_Z); |
| 1015 | adapter.copyFrom(zpos); |
| 1016 | adapter.setFace(Type.CubemapFace.NEGATIVE_Z); |
| 1017 | adapter.copyFrom(zneg); |
| 1018 | |
| 1019 | return cubemap; |
| 1020 | } |
| 1021 | |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1022 | /** |
| 1023 | * Creates a non-mipmapped cubemap allocation for use as a |
| 1024 | * graphics texture from 6 bitmaps containing |
| 1025 | * the cube faces. All the faces must be the same size and |
| 1026 | * power of 2 |
| 1027 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1028 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1029 | * @param xpos cubemap face in the positive x direction |
| 1030 | * @param xneg cubemap face in the negative x direction |
| 1031 | * @param ypos cubemap face in the positive y direction |
| 1032 | * @param yneg cubemap face in the negative y direction |
| 1033 | * @param zpos cubemap face in the positive z direction |
| 1034 | * @param zneg cubemap face in the negative z direction |
| 1035 | * |
| 1036 | * @return allocation containing cubemap data |
| 1037 | * |
| 1038 | */ |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1039 | static public Allocation createCubemapFromCubeFaces(RenderScript rs, |
| 1040 | Bitmap xpos, |
| 1041 | Bitmap xneg, |
| 1042 | Bitmap ypos, |
| 1043 | Bitmap yneg, |
| 1044 | Bitmap zpos, |
| 1045 | Bitmap zneg) { |
| 1046 | return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg, |
| 1047 | zpos, zneg, MipmapControl.MIPMAP_NONE, |
| 1048 | USAGE_GRAPHICS_TEXTURE); |
| 1049 | } |
| 1050 | |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1051 | /** |
| 1052 | * Creates a renderscript allocation from the bitmap referenced |
| 1053 | * by resource id |
| 1054 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1055 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1056 | * @param res application resources |
| 1057 | * @param id resource id to load the data from |
| 1058 | * @param mips specifies desired mipmap behaviour for the |
| 1059 | * allocation |
| 1060 | * @param usage bit field specifying how the allocation is |
| 1061 | * utilized |
| 1062 | * |
| 1063 | * @return renderscript allocation containing resource data |
| 1064 | * |
| 1065 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1066 | static public Allocation createFromBitmapResource(RenderScript rs, |
| 1067 | Resources res, |
| 1068 | int id, |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 1069 | MipmapControl mips, |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1070 | int usage) { |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1071 | |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 1072 | rs.validate(); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1073 | Bitmap b = BitmapFactory.decodeResource(res, id); |
| 1074 | Allocation alloc = createFromBitmap(rs, b, mips, usage); |
| 1075 | b.recycle(); |
| 1076 | return alloc; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1079 | /** |
| 1080 | * Creates a non-mipmapped renderscript allocation to use as a |
| 1081 | * graphics texture from the bitmap referenced by resource id |
| 1082 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1083 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1084 | * @param res application resources |
| 1085 | * @param id resource id to load the data from |
| 1086 | * |
| 1087 | * @return renderscript allocation containing resource data |
| 1088 | * |
| 1089 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1090 | static public Allocation createFromBitmapResource(RenderScript rs, |
| 1091 | Resources res, |
Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 1092 | int id) { |
| 1093 | return createFromBitmapResource(rs, res, id, |
| 1094 | MipmapControl.MIPMAP_NONE, |
| 1095 | USAGE_GRAPHICS_TEXTURE); |
| 1096 | } |
| 1097 | |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1098 | /** |
| 1099 | * Creates a renderscript allocation containing string data |
| 1100 | * encoded in UTF-8 format |
| 1101 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1102 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1103 | * @param str string to create the allocation from |
| 1104 | * @param usage bit field specifying how the allocaiton is |
| 1105 | * utilized |
| 1106 | * |
| 1107 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1108 | static public Allocation createFromString(RenderScript rs, |
| 1109 | String str, |
| 1110 | int usage) { |
| 1111 | rs.validate(); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1112 | byte[] allocArray = null; |
| 1113 | try { |
| 1114 | allocArray = str.getBytes("UTF-8"); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1115 | Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1116 | alloc.copyFrom(allocArray); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1117 | return alloc; |
| 1118 | } |
| 1119 | catch (Exception e) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 1120 | throw new RSRuntimeException("Could not convert string to utf-8."); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1121 | } |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1122 | } |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | |