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