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