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