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