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