| 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 | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 21 | import java.util.HashMap; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
| Romain Guy | 650a3eb | 2009-08-31 14:06:43 -0700 | [diff] [blame] | 23 | import android.content.res.AssetManager; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.BitmapFactory; |
| Jason Sams | fb9aa9f | 2012-03-28 15:30:07 -0700 | [diff] [blame] | 26 | import android.view.Surface; |
| 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; |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 30 | import android.os.Trace; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 31 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 32 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 33 | * <p> This class provides the primary method through which data is passed to |
| 34 | * and from RenderScript kernels. An Allocation provides the backing store for |
| 35 | * a given {@link android.renderscript.Type}. </p> |
| Jason Sams | a23d4e7 | 2011-01-04 18:59:12 -0800 | [diff] [blame] | 36 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 37 | * <p>An Allocation also contains a set of usage flags that denote how the |
| 38 | * Allocation could be used. For example, an Allocation may have usage flags |
| 39 | * specifying that it can be used from a script as well as input to a {@link |
| 40 | * android.renderscript.Sampler}. A developer must synchronize across these |
| 41 | * different usages using {@link android.renderscript.Allocation#syncAll} in |
| 42 | * order to ensure that different users of the Allocation have a consistent view |
| 43 | * of memory. For example, in the case where an Allocation is used as the output |
| 44 | * of one kernel and as Sampler input in a later kernel, a developer must call |
| 45 | * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the |
| 46 | * second kernel to ensure correctness. |
| Jason Sams | a23d4e7 | 2011-01-04 18:59:12 -0800 | [diff] [blame] | 47 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 48 | * <p>An Allocation can be populated with the {@link #copyFrom} routines. For |
| 49 | * more complex Element types, the {@link #copyFromUnchecked} methods can be |
| 50 | * used to copy from byte arrays or similar constructs.</p> |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 51 | * |
| Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 52 | * <div class="special reference"> |
| 53 | * <h3>Developer Guides</h3> |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 54 | * <p>For more information about creating an application that uses RenderScript, read the |
| 55 | * <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] | 56 | * </div> |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 57 | **/ |
| 58 | public class Allocation extends BaseObj { |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 59 | Type mType; |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 60 | Bitmap mBitmap; |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 61 | int mUsage; |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 62 | Allocation mAdaptedAllocation; |
| Tim Murray | 2f2472c | 2013-08-22 14:55:26 -0700 | [diff] [blame] | 63 | int mSize; |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 64 | |
| 65 | boolean mConstrainedLOD; |
| 66 | boolean mConstrainedFace; |
| 67 | boolean mConstrainedY; |
| 68 | boolean mConstrainedZ; |
| Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 69 | boolean mReadAllowed = true; |
| 70 | boolean mWriteAllowed = true; |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 71 | int mSelectedY; |
| 72 | int mSelectedZ; |
| 73 | int mSelectedLOD; |
| 74 | Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X; |
| 75 | |
| 76 | int mCurrentDimX; |
| 77 | int mCurrentDimY; |
| 78 | int mCurrentDimZ; |
| 79 | int mCurrentCount; |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 80 | static HashMap<Long, Allocation> mAllocationMap = |
| 81 | new HashMap<Long, Allocation>(); |
| Jason Sams | 42ef238 | 2013-08-29 13:30:59 -0700 | [diff] [blame] | 82 | OnBufferAvailableListener mBufferNotifier; |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 83 | |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 84 | private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) { |
| 85 | final Class c = d.getClass(); |
| 86 | if (!c.isArray()) { |
| 87 | throw new RSIllegalArgumentException("Object passed is not an array of primitives."); |
| 88 | } |
| 89 | final Class cmp = c.getComponentType(); |
| 90 | if (!cmp.isPrimitive()) { |
| 91 | throw new RSIllegalArgumentException("Object passed is not an Array of primitives."); |
| 92 | } |
| 93 | |
| 94 | if (cmp == Long.TYPE) { |
| 95 | if (checkType) { |
| 96 | validateIsInt64(); |
| 97 | return mType.mElement.mType; |
| 98 | } |
| 99 | return Element.DataType.SIGNED_64; |
| 100 | } |
| 101 | |
| 102 | if (cmp == Integer.TYPE) { |
| 103 | if (checkType) { |
| 104 | validateIsInt32(); |
| 105 | return mType.mElement.mType; |
| 106 | } |
| 107 | return Element.DataType.SIGNED_32; |
| 108 | } |
| 109 | |
| 110 | if (cmp == Short.TYPE) { |
| 111 | if (checkType) { |
| 112 | validateIsInt16(); |
| 113 | return mType.mElement.mType; |
| 114 | } |
| 115 | return Element.DataType.SIGNED_16; |
| 116 | } |
| 117 | |
| 118 | if (cmp == Byte.TYPE) { |
| 119 | if (checkType) { |
| 120 | validateIsInt8(); |
| 121 | return mType.mElement.mType; |
| 122 | } |
| 123 | return Element.DataType.SIGNED_8; |
| 124 | } |
| 125 | |
| 126 | if (cmp == Float.TYPE) { |
| 127 | if (checkType) { |
| 128 | validateIsFloat32(); |
| 129 | } |
| 130 | return Element.DataType.FLOAT_32; |
| 131 | } |
| 132 | |
| 133 | if (cmp == Double.TYPE) { |
| 134 | if (checkType) { |
| 135 | validateIsFloat64(); |
| 136 | } |
| 137 | return Element.DataType.FLOAT_64; |
| 138 | } |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 143 | /** |
| 144 | * The usage of the Allocation. These signal to RenderScript where to place |
| 145 | * the Allocation in memory. |
| 146 | * |
| 147 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 148 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 149 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 150 | * The Allocation will be bound to and accessed by scripts. |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 151 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 152 | public static final int USAGE_SCRIPT = 0x0001; |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 153 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 154 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 155 | * The Allocation will be used as a texture source by one or more graphics |
| 156 | * programs. |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 157 | * |
| 158 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 159 | public static final int USAGE_GRAPHICS_TEXTURE = 0x0002; |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 160 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 161 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 162 | * The Allocation will be used as a graphics mesh. |
| 163 | * |
| 164 | * This was deprecated in API level 16. |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 165 | * |
| 166 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 167 | public static final int USAGE_GRAPHICS_VERTEX = 0x0004; |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 168 | |
| 169 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 170 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 171 | * The Allocation will be used as the source of shader constants by one or |
| 172 | * more programs. |
| 173 | * |
| 174 | * This was deprecated in API level 16. |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 175 | * |
| 176 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 177 | public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008; |
| 178 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 179 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 180 | * The Allocation will be used as a target for offscreen rendering |
| 181 | * |
| 182 | * This was deprecated in API level 16. |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 183 | * |
| 184 | */ |
| 185 | public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010; |
| 186 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 187 | /** |
| Jason Sams | 1887d52 | 2013-09-24 15:18:52 -0700 | [diff] [blame] | 188 | * The Allocation will be used as a {@link android.view.Surface} |
| 189 | * consumer. This usage will cause the Allocation to be created |
| 190 | * as read-only. |
| Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 191 | * |
| Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 192 | */ |
| Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 193 | public static final int USAGE_IO_INPUT = 0x0020; |
| Stephen Hines | 9069ee8 | 2012-02-13 18:25:54 -0800 | [diff] [blame] | 194 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 195 | /** |
| Jason Sams | 1887d52 | 2013-09-24 15:18:52 -0700 | [diff] [blame] | 196 | * The Allocation will be used as a {@link android.view.Surface} |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 197 | * producer. The dimensions and format of the {@link |
| Jason Sams | 1887d52 | 2013-09-24 15:18:52 -0700 | [diff] [blame] | 198 | * android.view.Surface} will be forced to those of the |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 199 | * Allocation. |
| Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 200 | * |
| Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 201 | */ |
| Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 202 | public static final int USAGE_IO_OUTPUT = 0x0040; |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 203 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 204 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 205 | * The Allocation's backing store will be inherited from another object |
| 206 | * (usually a {@link android.graphics.Bitmap}); copying to or from the |
| 207 | * original source Bitmap will cause a synchronization rather than a full |
| 208 | * copy. {@link #syncAll} may also be used to synchronize the Allocation |
| 209 | * and the source Bitmap. |
| Tim Murray | 00bb454 | 2012-12-17 16:35:06 -0800 | [diff] [blame] | 210 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 211 | * <p>This is set by default for allocations created with {@link |
| 212 | * #createFromBitmap} in API version 18 and higher.</p> |
| Tim Murray | 00bb454 | 2012-12-17 16:35:06 -0800 | [diff] [blame] | 213 | * |
| 214 | */ |
| 215 | public static final int USAGE_SHARED = 0x0080; |
| 216 | |
| 217 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 218 | * Controls mipmap behavior when using the bitmap creation and update |
| 219 | * functions. |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 220 | */ |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 221 | public enum MipmapControl { |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 222 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 223 | * No mipmaps will be generated and the type generated from the incoming |
| 224 | * bitmap will not contain additional LODs. |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 225 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 226 | MIPMAP_NONE(0), |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 227 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 228 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 229 | * A full mipmap chain will be created in script memory. The Type of |
| 230 | * the Allocation will contain a full mipmap chain. On upload, the full |
| 231 | * chain will be transferred. |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 232 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 233 | MIPMAP_FULL(1), |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 234 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 235 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 236 | * The Type of the Allocation will be the same as MIPMAP_NONE. It will |
| 237 | * not contain mipmaps. On upload, the allocation data will contain a |
| 238 | * full mipmap chain generated from the top level in script memory. |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 239 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 240 | MIPMAP_ON_SYNC_TO_TEXTURE(2); |
| 241 | |
| 242 | int mID; |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 243 | MipmapControl(int id) { |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 244 | mID = id; |
| 245 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 248 | |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 249 | private long getIDSafe() { |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 250 | if (mAdaptedAllocation != null) { |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 251 | return mAdaptedAllocation.getID(mRS); |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 252 | } |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 253 | return getID(mRS); |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 256 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 257 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 258 | * Get the {@link android.renderscript.Element} of the {@link |
| 259 | * android.renderscript.Type} of the Allocation. |
| Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 260 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 261 | * @return Element |
| Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 262 | * |
| 263 | */ |
| 264 | public Element getElement() { |
| 265 | return mType.getElement(); |
| 266 | } |
| 267 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 268 | /** |
| Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 269 | * Get the usage flags of the Allocation. |
| 270 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 271 | * @return usage this Allocation's set of the USAGE_* flags OR'd together |
| Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 272 | * |
| 273 | */ |
| 274 | public int getUsage() { |
| 275 | return mUsage; |
| 276 | } |
| 277 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 278 | /** |
| Jason Sams | 36c0f64 | 2012-03-23 15:48:37 -0700 | [diff] [blame] | 279 | * Get the size of the Allocation in bytes. |
| 280 | * |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 281 | * @return size of the Allocation in bytes. |
| Jason Sams | 36c0f64 | 2012-03-23 15:48:37 -0700 | [diff] [blame] | 282 | * |
| 283 | */ |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 284 | public int getBytesSize() { |
| Tim Murray | e6eaaf6 | 2013-12-17 17:15:25 -0800 | [diff] [blame] | 285 | if (mType.mDimYuv != 0) { |
| 286 | return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5); |
| 287 | } |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 288 | return mType.getCount() * mType.getElement().getBytesSize(); |
| Jason Sams | 36c0f64 | 2012-03-23 15:48:37 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| Jason Sams | 452a766 | 2011-07-07 16:05:18 -0700 | [diff] [blame] | 291 | private void updateCacheInfo(Type t) { |
| 292 | mCurrentDimX = t.getX(); |
| 293 | mCurrentDimY = t.getY(); |
| 294 | mCurrentDimZ = t.getZ(); |
| 295 | mCurrentCount = mCurrentDimX; |
| 296 | if (mCurrentDimY > 1) { |
| 297 | mCurrentCount *= mCurrentDimY; |
| 298 | } |
| 299 | if (mCurrentDimZ > 1) { |
| 300 | mCurrentCount *= mCurrentDimZ; |
| 301 | } |
| 302 | } |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 303 | |
| Tim Murray | a314551 | 2012-12-04 17:59:29 -0800 | [diff] [blame] | 304 | private void setBitmap(Bitmap b) { |
| 305 | mBitmap = b; |
| 306 | } |
| 307 | |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 308 | Allocation(long id, RenderScript rs, Type t, int usage) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 309 | super(id, rs); |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 310 | if ((usage & ~(USAGE_SCRIPT | |
| 311 | USAGE_GRAPHICS_TEXTURE | |
| 312 | USAGE_GRAPHICS_VERTEX | |
| Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 313 | USAGE_GRAPHICS_CONSTANTS | |
| Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 314 | USAGE_GRAPHICS_RENDER_TARGET | |
| Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 315 | USAGE_IO_INPUT | |
| Tim Murray | 00bb454 | 2012-12-17 16:35:06 -0800 | [diff] [blame] | 316 | USAGE_IO_OUTPUT | |
| 317 | USAGE_SHARED)) != 0) { |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 318 | throw new RSIllegalArgumentException("Unknown usage specified."); |
| 319 | } |
| Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 320 | |
| Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 321 | if ((usage & USAGE_IO_INPUT) != 0) { |
| Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 322 | mWriteAllowed = false; |
| 323 | |
| Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 324 | if ((usage & ~(USAGE_IO_INPUT | |
| Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 325 | USAGE_GRAPHICS_TEXTURE | |
| 326 | USAGE_SCRIPT)) != 0) { |
| 327 | throw new RSIllegalArgumentException("Invalid usage combination."); |
| 328 | } |
| 329 | } |
| Jason Sams | 9bf1892 | 2013-04-13 19:48:36 -0700 | [diff] [blame] | 330 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 331 | mType = t; |
| Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 332 | mUsage = usage; |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 333 | |
| Jason Sams | 452a766 | 2011-07-07 16:05:18 -0700 | [diff] [blame] | 334 | if (t != null) { |
| Stephen Hines | 88990da | 2013-09-09 17:56:07 -0700 | [diff] [blame] | 335 | // TODO: A3D doesn't have Type info during creation, so we can't |
| 336 | // calculate the size ahead of time. We can possibly add a method |
| 337 | // to update the size in the future if it seems reasonable. |
| 338 | mSize = mType.getCount() * mType.getElement().getBytesSize(); |
| Jason Sams | 452a766 | 2011-07-07 16:05:18 -0700 | [diff] [blame] | 339 | updateCacheInfo(t); |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 340 | } |
| Tim Murray | 2f2472c | 2013-08-22 14:55:26 -0700 | [diff] [blame] | 341 | try { |
| 342 | RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize); |
| 343 | } catch (Exception e) { |
| 344 | Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e); |
| 345 | throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | protected void finalize() throws Throwable { |
| 350 | RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize); |
| 351 | super.finalize(); |
| Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 354 | private void validateIsInt64() { |
| 355 | if ((mType.mElement.mType == Element.DataType.SIGNED_64) || |
| 356 | (mType.mElement.mType == Element.DataType.UNSIGNED_64)) { |
| 357 | return; |
| 358 | } |
| 359 | throw new RSIllegalArgumentException( |
| 360 | "64 bit integer source does not match allocation type " + mType.mElement.mType); |
| 361 | } |
| 362 | |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 363 | private void validateIsInt32() { |
| 364 | if ((mType.mElement.mType == Element.DataType.SIGNED_32) || |
| 365 | (mType.mElement.mType == Element.DataType.UNSIGNED_32)) { |
| 366 | return; |
| 367 | } |
| 368 | throw new RSIllegalArgumentException( |
| 369 | "32 bit integer source does not match allocation type " + mType.mElement.mType); |
| 370 | } |
| 371 | |
| 372 | private void validateIsInt16() { |
| 373 | if ((mType.mElement.mType == Element.DataType.SIGNED_16) || |
| 374 | (mType.mElement.mType == Element.DataType.UNSIGNED_16)) { |
| 375 | return; |
| 376 | } |
| 377 | throw new RSIllegalArgumentException( |
| 378 | "16 bit integer source does not match allocation type " + mType.mElement.mType); |
| 379 | } |
| 380 | |
| 381 | private void validateIsInt8() { |
| 382 | if ((mType.mElement.mType == Element.DataType.SIGNED_8) || |
| 383 | (mType.mElement.mType == Element.DataType.UNSIGNED_8)) { |
| 384 | return; |
| 385 | } |
| 386 | throw new RSIllegalArgumentException( |
| 387 | "8 bit integer source does not match allocation type " + mType.mElement.mType); |
| 388 | } |
| 389 | |
| 390 | private void validateIsFloat32() { |
| 391 | if (mType.mElement.mType == Element.DataType.FLOAT_32) { |
| 392 | return; |
| 393 | } |
| 394 | throw new RSIllegalArgumentException( |
| 395 | "32 bit float source does not match allocation type " + mType.mElement.mType); |
| 396 | } |
| 397 | |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 398 | private void validateIsFloat64() { |
| 399 | if (mType.mElement.mType == Element.DataType.FLOAT_64) { |
| 400 | return; |
| 401 | } |
| 402 | throw new RSIllegalArgumentException( |
| 403 | "64 bit float source does not match allocation type " + mType.mElement.mType); |
| 404 | } |
| 405 | |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 406 | private void validateIsObject() { |
| 407 | if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) || |
| 408 | (mType.mElement.mType == Element.DataType.RS_TYPE) || |
| 409 | (mType.mElement.mType == Element.DataType.RS_ALLOCATION) || |
| 410 | (mType.mElement.mType == Element.DataType.RS_SAMPLER) || |
| 411 | (mType.mElement.mType == Element.DataType.RS_SCRIPT) || |
| 412 | (mType.mElement.mType == Element.DataType.RS_MESH) || |
| 413 | (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) || |
| 414 | (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) || |
| 415 | (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) || |
| 416 | (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) { |
| 417 | return; |
| 418 | } |
| 419 | throw new RSIllegalArgumentException( |
| 420 | "Object source does not match allocation type " + mType.mElement.mType); |
| 421 | } |
| 422 | |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 423 | @Override |
| 424 | void updateFromNative() { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 425 | super.updateFromNative(); |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 426 | long typeID = mRS.nAllocationGetType(getID(mRS)); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 427 | if(typeID != 0) { |
| 428 | mType = new Type(typeID, mRS); |
| 429 | mType.updateFromNative(); |
| Jason Sams | ad37cb2 | 2011-07-07 16:17:36 -0700 | [diff] [blame] | 430 | updateCacheInfo(mType); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 434 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 435 | * Get the {@link android.renderscript.Type} of the Allocation. |
| Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 436 | * |
| 437 | * @return Type |
| 438 | * |
| 439 | */ |
| Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 440 | public Type getType() { |
| 441 | return mType; |
| 442 | } |
| 443 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 444 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 445 | * Propagate changes from one usage of the Allocation to the |
| 446 | * other usages of the Allocation. |
| Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 447 | * |
| 448 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 449 | public void syncAll(int srcLocation) { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 450 | Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll"); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 451 | switch (srcLocation) { |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 452 | case USAGE_GRAPHICS_TEXTURE: |
| Tim Murray | 78e6494 | 2013-04-09 17:28:56 -0700 | [diff] [blame] | 453 | case USAGE_SCRIPT: |
| 454 | if ((mUsage & USAGE_SHARED) != 0) { |
| 455 | copyFrom(mBitmap); |
| 456 | } |
| 457 | break; |
| 458 | case USAGE_GRAPHICS_CONSTANTS: |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 459 | case USAGE_GRAPHICS_VERTEX: |
| 460 | break; |
| Tim Murray | 78e6494 | 2013-04-09 17:28:56 -0700 | [diff] [blame] | 461 | case USAGE_SHARED: |
| 462 | if ((mUsage & USAGE_SHARED) != 0) { |
| 463 | copyTo(mBitmap); |
| 464 | } |
| 465 | break; |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 466 | default: |
| 467 | throw new RSIllegalArgumentException("Source must be exactly one usage type."); |
| 468 | } |
| 469 | mRS.validate(); |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 470 | mRS.nAllocationSyncAll(getIDSafe(), srcLocation); |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 471 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 474 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 475 | * Send a buffer to the output stream. The contents of the Allocation will |
| 476 | * be undefined after this operation. This operation is only valid if {@link |
| 477 | * #USAGE_IO_OUTPUT} is set on the Allocation. |
| 478 | * |
| Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 479 | * |
| Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 480 | */ |
| Jason Sams | c5f519c | 2012-03-29 17:58:15 -0700 | [diff] [blame] | 481 | public void ioSend() { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 482 | Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend"); |
| Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 483 | if ((mUsage & USAGE_IO_OUTPUT) == 0) { |
| 484 | throw new RSIllegalArgumentException( |
| 485 | "Can only send buffer if IO_OUTPUT usage specified."); |
| 486 | } |
| 487 | mRS.validate(); |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 488 | mRS.nAllocationIoSend(getID(mRS)); |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 489 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 492 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 493 | * Receive the latest input into the Allocation. This operation |
| 494 | * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation. |
| Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 495 | * |
| Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 496 | */ |
| Jason Sams | c5f519c | 2012-03-29 17:58:15 -0700 | [diff] [blame] | 497 | public void ioReceive() { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 498 | Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive"); |
| Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 499 | if ((mUsage & USAGE_IO_INPUT) == 0) { |
| 500 | throw new RSIllegalArgumentException( |
| Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 501 | "Can only receive if IO_INPUT usage specified."); |
| Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 502 | } |
| 503 | mRS.validate(); |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 504 | mRS.nAllocationIoReceive(getID(mRS)); |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 505 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 508 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 509 | * Copy an array of RS objects to the Allocation. |
| Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 510 | * |
| 511 | * @param d Source array. |
| 512 | */ |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 513 | public void copyFrom(BaseObj[] d) { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 514 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 515 | mRS.validate(); |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 516 | validateIsObject(); |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 517 | if (d.length != mCurrentCount) { |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 518 | throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " + |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 519 | mCurrentCount + ", array length = " + d.length); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 520 | } |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 521 | // FIXME: requires 64-bit path |
| 522 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 523 | int i[] = new int[d.length]; |
| 524 | for (int ct=0; ct < d.length; ct++) { |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 525 | i[ct] = (int)d[ct].getID(mRS); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 526 | } |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 527 | copy1DRangeFromUnchecked(0, mCurrentCount, i); |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 528 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 531 | private void validateBitmapFormat(Bitmap b) { |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 532 | Bitmap.Config bc = b.getConfig(); |
| Tim Murray | abd5db9 | 2013-02-28 11:45:22 -0800 | [diff] [blame] | 533 | if (bc == null) { |
| 534 | throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation"); |
| 535 | } |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 536 | switch (bc) { |
| 537 | case ALPHA_8: |
| 538 | if (mType.getElement().mKind != Element.DataKind.PIXEL_A) { |
| 539 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 540 | mType.getElement().mKind + ", type " + |
| 541 | mType.getElement().mType + |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 542 | " of " + mType.getElement().getBytesSize() + |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 543 | " bytes, passed bitmap was " + bc); |
| 544 | } |
| 545 | break; |
| 546 | case ARGB_8888: |
| 547 | if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) || |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 548 | (mType.getElement().getBytesSize() != 4)) { |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 549 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 550 | mType.getElement().mKind + ", type " + |
| 551 | mType.getElement().mType + |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 552 | " of " + mType.getElement().getBytesSize() + |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 553 | " bytes, passed bitmap was " + bc); |
| 554 | } |
| 555 | break; |
| 556 | case RGB_565: |
| 557 | if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) || |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 558 | (mType.getElement().getBytesSize() != 2)) { |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 559 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 560 | mType.getElement().mKind + ", type " + |
| 561 | mType.getElement().mType + |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 562 | " of " + mType.getElement().getBytesSize() + |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 563 | " bytes, passed bitmap was " + bc); |
| 564 | } |
| 565 | break; |
| 566 | case ARGB_4444: |
| 567 | if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) || |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 568 | (mType.getElement().getBytesSize() != 2)) { |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 569 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 570 | mType.getElement().mKind + ", type " + |
| 571 | mType.getElement().mType + |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 572 | " of " + mType.getElement().getBytesSize() + |
| Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 573 | " bytes, passed bitmap was " + bc); |
| 574 | } |
| 575 | break; |
| 576 | |
| 577 | } |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 580 | private void validateBitmapSize(Bitmap b) { |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 581 | if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) { |
| Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 582 | throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch"); |
| 583 | } |
| 584 | } |
| 585 | |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 586 | private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) { |
| 587 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked"); |
| 588 | mRS.validate(); |
| 589 | if (mCurrentDimZ > 0) { |
| 590 | copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen); |
| 591 | } else if (mCurrentDimY > 0) { |
| 592 | copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen); |
| 593 | } else { |
| 594 | copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen); |
| 595 | } |
| 596 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Copy into this Allocation from an array. This method does not guarantee |
| 601 | * that the Allocation is compatible with the input buffer; it copies memory |
| 602 | * without reinterpretation. |
| 603 | * |
| 604 | * @param array The source data array |
| 605 | * @hide |
| 606 | */ |
| 607 | public void copyFromUnchecked(Object array) { |
| 608 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked"); |
| 609 | copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false), |
| 610 | java.lang.reflect.Array.getLength(array)); |
| 611 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| 612 | } |
| 613 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 614 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 615 | * Copy into this Allocation from an array. This method does not guarantee |
| 616 | * that the Allocation is compatible with the input buffer; it copies memory |
| 617 | * without reinterpretation. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 618 | * |
| 619 | * @param d the source data array |
| 620 | */ |
| 621 | public void copyFromUnchecked(int[] d) { |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 622 | copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length); |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 623 | } |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 624 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 625 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 626 | * Copy into this Allocation from an array. This method does not guarantee |
| 627 | * that the Allocation is compatible with the input buffer; it copies memory |
| 628 | * without reinterpretation. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 629 | * |
| 630 | * @param d the source data array |
| 631 | */ |
| 632 | public void copyFromUnchecked(short[] d) { |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 633 | copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length); |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 634 | } |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 635 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 636 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 637 | * Copy into this Allocation from an array. This method does not guarantee |
| 638 | * that the Allocation is compatible with the input buffer; it copies memory |
| 639 | * without reinterpretation. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 640 | * |
| 641 | * @param d the source data array |
| 642 | */ |
| 643 | public void copyFromUnchecked(byte[] d) { |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 644 | copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length); |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 645 | } |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 646 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 647 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 648 | * Copy into this Allocation from an array. This method does not guarantee |
| 649 | * that the Allocation is compatible with the input buffer; it copies memory |
| 650 | * without reinterpretation. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 651 | * |
| 652 | * @param d the source data array |
| 653 | */ |
| 654 | public void copyFromUnchecked(float[] d) { |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 655 | copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length); |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 656 | } |
| 657 | |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 658 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 659 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 660 | * Copy into this Allocation from an array. This variant is type checked |
| 661 | * and will generate exceptions if the Allocation's {@link |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 662 | * android.renderscript.Element} does not match the array's |
| 663 | * primitive type. |
| 664 | * |
| 665 | * @param d the source data array |
| 666 | * @hide |
| 667 | */ |
| 668 | public void copyFrom(Object array) { |
| 669 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); |
| 670 | copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true), |
| 671 | java.lang.reflect.Array.getLength(array)); |
| 672 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Copy into this Allocation from an array. This variant is type checked |
| 677 | * and will generate exceptions if the Allocation's {@link |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 678 | * android.renderscript.Element} is not a 32 bit integer type. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 679 | * |
| 680 | * @param d the source data array |
| 681 | */ |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 682 | public void copyFrom(int[] d) { |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 683 | validateIsInt32(); |
| 684 | copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 685 | } |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 686 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 687 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 688 | * Copy into this Allocation from an array. This variant is type checked |
| 689 | * and will generate exceptions if the Allocation's {@link |
| 690 | * android.renderscript.Element} is not a 16 bit integer type. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 691 | * |
| 692 | * @param d the source data array |
| 693 | */ |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 694 | public void copyFrom(short[] d) { |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 695 | validateIsInt16(); |
| 696 | copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 697 | } |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 698 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 699 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 700 | * Copy into this Allocation from an array. This variant is type checked |
| 701 | * and will generate exceptions if the Allocation's {@link |
| 702 | * android.renderscript.Element} is not an 8 bit integer type. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 703 | * |
| 704 | * @param d the source data array |
| 705 | */ |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 706 | public void copyFrom(byte[] d) { |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 707 | validateIsInt8(); |
| 708 | copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 709 | } |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 710 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 711 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 712 | * Copy into this Allocation from an array. This variant is type checked |
| 713 | * and will generate exceptions if the Allocation's {@link |
| 714 | * android.renderscript.Element} is not a 32 bit float type. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 715 | * |
| 716 | * @param d the source data array |
| 717 | */ |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 718 | public void copyFrom(float[] d) { |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 719 | validateIsFloat32(); |
| 720 | copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 721 | } |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 722 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 723 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 724 | * Copy into an Allocation from a {@link android.graphics.Bitmap}. The |
| 725 | * height, width, and format of the bitmap must match the existing |
| 726 | * allocation. |
| 727 | * |
| 728 | * <p>If the {@link android.graphics.Bitmap} is the same as the {@link |
| 729 | * android.graphics.Bitmap} used to create the Allocation with {@link |
| 730 | * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation, |
| 731 | * this will synchronize the Allocation with the latest data from the {@link |
| 732 | * android.graphics.Bitmap}, potentially avoiding the actual copy.</p> |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 733 | * |
| 734 | * @param b the source bitmap |
| 735 | */ |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 736 | public void copyFrom(Bitmap b) { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 737 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); |
| Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 738 | mRS.validate(); |
| Tim Murray | abd5db9 | 2013-02-28 11:45:22 -0800 | [diff] [blame] | 739 | if (b.getConfig() == null) { |
| 740 | Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888); |
| 741 | Canvas c = new Canvas(newBitmap); |
| 742 | c.drawBitmap(b, 0, 0, null); |
| 743 | copyFrom(newBitmap); |
| 744 | return; |
| 745 | } |
| Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 746 | validateBitmapSize(b); |
| 747 | validateBitmapFormat(b); |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 748 | mRS.nAllocationCopyFromBitmap(getID(mRS), b); |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 749 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 752 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 753 | * Copy an Allocation from an Allocation. The types of both allocations |
| Tim Murray | f671fb0 | 2012-10-03 13:50:05 -0700 | [diff] [blame] | 754 | * must be identical. |
| 755 | * |
| 756 | * @param a the source allocation |
| 757 | */ |
| 758 | public void copyFrom(Allocation a) { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 759 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); |
| Tim Murray | f671fb0 | 2012-10-03 13:50:05 -0700 | [diff] [blame] | 760 | mRS.validate(); |
| 761 | if (!mType.equals(a.getType())) { |
| 762 | throw new RSIllegalArgumentException("Types of allocations must match."); |
| 763 | } |
| 764 | copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0); |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 765 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Tim Murray | f671fb0 | 2012-10-03 13:50:05 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| Tim Murray | f671fb0 | 2012-10-03 13:50:05 -0700 | [diff] [blame] | 768 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 769 | * This is only intended to be used by auto-generated code reflected from |
| 770 | * the RenderScript script files and should not be used by developers. |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 771 | * |
| 772 | * @param xoff |
| 773 | * @param fp |
| 774 | */ |
| Jason Sams | 21b4103 | 2011-01-16 15:05:41 -0800 | [diff] [blame] | 775 | public void setFromFieldPacker(int xoff, FieldPacker fp) { |
| Jason Sams | f70b0fc8 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 776 | mRS.validate(); |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 777 | int eSize = mType.mElement.getBytesSize(); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 778 | final byte[] data = fp.getData(); |
| Stephen Hines | fa1275a | 2014-06-17 17:25:04 -0700 | [diff] [blame^] | 779 | int data_length = fp.getPos(); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 780 | |
| Stephen Hines | fa1275a | 2014-06-17 17:25:04 -0700 | [diff] [blame^] | 781 | int count = data_length / eSize; |
| 782 | if ((eSize * count) != data_length) { |
| 783 | throw new RSIllegalArgumentException("Field packer length " + data_length + |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 784 | " not divisible by element size " + eSize + "."); |
| 785 | } |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 786 | copy1DRangeFromUnchecked(xoff, count, data); |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 789 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 790 | * This is only intended to be used by auto-generated code reflected from |
| 791 | * the RenderScript script files. |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 792 | * |
| 793 | * @param xoff |
| 794 | * @param component_number |
| 795 | * @param fp |
| 796 | */ |
| Jason Sams | 21b4103 | 2011-01-16 15:05:41 -0800 | [diff] [blame] | 797 | public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) { |
| Jason Sams | f70b0fc8 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 798 | mRS.validate(); |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 799 | if (component_number >= mType.mElement.mElements.length) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 800 | throw new RSIllegalArgumentException("Component_number " + component_number + " out of range."); |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 801 | } |
| 802 | if(xoff < 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 803 | throw new RSIllegalArgumentException("Offset must be >= 0."); |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | final byte[] data = fp.getData(); |
| Stephen Hines | fa1275a | 2014-06-17 17:25:04 -0700 | [diff] [blame^] | 807 | int data_length = fp.getPos(); |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 808 | int eSize = mType.mElement.mElements[component_number].getBytesSize(); |
| Alex Sakhartchouk | bf3c3f2 | 2012-02-02 09:47:26 -0800 | [diff] [blame] | 809 | eSize *= mType.mElement.mArraySizes[component_number]; |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 810 | |
| Stephen Hines | fa1275a | 2014-06-17 17:25:04 -0700 | [diff] [blame^] | 811 | if (data_length != eSize) { |
| 812 | throw new RSIllegalArgumentException("Field packer sizelength " + data_length + |
| Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 813 | " does not match component size " + eSize + "."); |
| 814 | } |
| 815 | |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 816 | mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD, |
| Stephen Hines | fa1275a | 2014-06-17 17:25:04 -0700 | [diff] [blame^] | 817 | component_number, data, data_length); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 820 | private void data1DChecks(int off, int count, int len, int dataSize) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 821 | mRS.validate(); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 822 | if(off < 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 823 | throw new RSIllegalArgumentException("Offset must be >= 0."); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 824 | } |
| 825 | if(count < 1) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 826 | throw new RSIllegalArgumentException("Count must be >= 1."); |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 827 | } |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 828 | if((off + count) > mCurrentCount) { |
| 829 | throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount + |
| Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 830 | ", got " + count + " at offset " + off + "."); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 831 | } |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 832 | if(len < dataSize) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 833 | throw new RSIllegalArgumentException("Array too small for allocation type."); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 834 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 837 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 838 | * Generate a mipmap chain. This is only valid if the Type of the Allocation |
| 839 | * includes mipmaps. |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 840 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 841 | * <p>This function will generate a complete set of mipmaps from the top |
| 842 | * level LOD and place them into the script memory space.</p> |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 843 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 844 | * <p>If the Allocation is also using other memory spaces, a call to {@link |
| 845 | * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p> |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 846 | */ |
| 847 | public void generateMipmaps() { |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 848 | mRS.nAllocationGenerateMipmaps(getID(mRS)); |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 849 | } |
| 850 | |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 851 | private void copy1DRangeFromUnchecked(int off, int count, Object array, |
| 852 | Element.DataType dt, int arrayLen) { |
| 853 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked"); |
| 854 | final int dataSize = mType.mElement.getBytesSize() * count; |
| 855 | data1DChecks(off, count, arrayLen * dt.mSize, dataSize); |
| 856 | mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt); |
| 857 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * Copy an array into part of this Allocation. This method does not |
| 862 | * guarantee that the Allocation is compatible with the input buffer. |
| 863 | * |
| 864 | * @param off The offset of the first element to be copied. |
| 865 | * @param count The number of elements to be copied. |
| 866 | * @param array The source data array |
| 867 | * @hide |
| 868 | */ |
| 869 | public void copy1DRangeFromUnchecked(int off, int count, Object array) { |
| 870 | copy1DRangeFromUnchecked(off, count, array, |
| 871 | validateObjectIsPrimitiveArray(array, false), |
| 872 | java.lang.reflect.Array.getLength(array)); |
| 873 | } |
| 874 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 875 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 876 | * Copy an array into part of this Allocation. This method does not |
| 877 | * guarantee that the Allocation is compatible with the input buffer. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 878 | * |
| 879 | * @param off The offset of the first element to be copied. |
| 880 | * @param count The number of elements to be copied. |
| 881 | * @param d the source data array |
| 882 | */ |
| 883 | public void copy1DRangeFromUnchecked(int off, int count, int[] d) { |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 884 | copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 885 | } |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 886 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 887 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 888 | * Copy an array into part of this Allocation. This method does not |
| 889 | * guarantee that the Allocation is compatible with the input buffer. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 890 | * |
| 891 | * @param off The offset of the first element to be copied. |
| 892 | * @param count The number of elements to be copied. |
| 893 | * @param d the source data array |
| 894 | */ |
| 895 | public void copy1DRangeFromUnchecked(int off, int count, short[] d) { |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 896 | copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 897 | } |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 898 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 899 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 900 | * Copy an array into part of this Allocation. This method does not |
| 901 | * guarantee that the Allocation is compatible with the input buffer. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 902 | * |
| 903 | * @param off The offset of the first element to be copied. |
| 904 | * @param count The number of elements to be copied. |
| 905 | * @param d the source data array |
| 906 | */ |
| 907 | public void copy1DRangeFromUnchecked(int off, int count, byte[] d) { |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 908 | copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 909 | } |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 910 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 911 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 912 | * Copy an array into part of this Allocation. This method does not |
| 913 | * guarantee that the Allocation is compatible with the input buffer. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 914 | * |
| 915 | * @param off The offset of the first element to be copied. |
| 916 | * @param count The number of elements to be copied. |
| 917 | * @param d the source data array |
| 918 | */ |
| 919 | public void copy1DRangeFromUnchecked(int off, int count, float[] d) { |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 920 | copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length); |
| 921 | } |
| 922 | |
| 923 | |
| 924 | /** |
| 925 | * Copy an array into part of this Allocation. This variant is type checked |
| 926 | * and will generate exceptions if the Allocation type does not |
| 927 | * match the component type of the array passed in. |
| 928 | * |
| 929 | * @param off The offset of the first element to be copied. |
| 930 | * @param count The number of elements to be copied. |
| 931 | * @param array The source data array. |
| 932 | * @hide |
| 933 | */ |
| 934 | public void copy1DRangeFrom(int off, int count, Object array) { |
| 935 | copy1DRangeFromUnchecked(off, count, array, |
| 936 | validateObjectIsPrimitiveArray(array, true), |
| 937 | java.lang.reflect.Array.getLength(array)); |
| 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 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 941 | * Copy an array into part of this Allocation. This variant is type checked |
| 942 | * and will generate exceptions if the Allocation type is not a 32 bit |
| 943 | * integer type. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 944 | * |
| 945 | * @param off The offset of the first element to be copied. |
| 946 | * @param count The number of elements to be copied. |
| 947 | * @param d the source data array |
| 948 | */ |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 949 | public void copy1DRangeFrom(int off, int count, int[] d) { |
| 950 | validateIsInt32(); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 951 | copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length); |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 952 | } |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 953 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 954 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 955 | * Copy an array into part of this Allocation. This variant is type checked |
| 956 | * and will generate exceptions if the Allocation type is not a 16 bit |
| 957 | * integer type. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 958 | * |
| 959 | * @param off The offset of the first element to be copied. |
| 960 | * @param count The number of elements to be copied. |
| 961 | * @param d the source data array |
| 962 | */ |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 963 | public void copy1DRangeFrom(int off, int count, short[] d) { |
| 964 | validateIsInt16(); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 965 | copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length); |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 966 | } |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 967 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 968 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 969 | * Copy an array into part of this Allocation. This variant is type checked |
| 970 | * and will generate exceptions if the Allocation type is not an 8 bit |
| 971 | * integer type. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 972 | * |
| 973 | * @param off The offset of the first element to be copied. |
| 974 | * @param count The number of elements to be copied. |
| 975 | * @param d the source data array |
| 976 | */ |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 977 | public void copy1DRangeFrom(int off, int count, byte[] d) { |
| 978 | validateIsInt8(); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 979 | copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length); |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 980 | } |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 981 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 982 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 983 | * Copy an array into part of this Allocation. This variant is type checked |
| 984 | * and will generate exceptions if the Allocation type is not a 32 bit float |
| 985 | * type. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 986 | * |
| 987 | * @param off The offset of the first element to be copied. |
| 988 | * @param count The number of elements to be copied. |
| Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 989 | * @param d the source data array. |
| Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 990 | */ |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 991 | public void copy1DRangeFrom(int off, int count, float[] d) { |
| 992 | validateIsFloat32(); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 993 | copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length); |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 994 | } |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 995 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 996 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 997 | * Copy part of an Allocation into this Allocation. |
| Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 998 | * |
| 999 | * @param off The offset of the first element to be copied. |
| 1000 | * @param count The number of elements to be copied. |
| 1001 | * @param data the source data allocation. |
| 1002 | * @param dataOff off The offset of the first element in data to |
| 1003 | * be copied. |
| 1004 | */ |
| 1005 | public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1006 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom"); |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1007 | mRS.nAllocationData2D(getIDSafe(), off, 0, |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 1008 | mSelectedLOD, mSelectedFace.mID, |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1009 | count, 1, data.getID(mRS), dataOff, 0, |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 1010 | data.mSelectedLOD, data.mSelectedFace.mID); |
| Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
| Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 1013 | private void validate2DRange(int xoff, int yoff, int w, int h) { |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 1014 | if (mAdaptedAllocation != null) { |
| 1015 | |
| 1016 | } else { |
| 1017 | |
| 1018 | if (xoff < 0 || yoff < 0) { |
| 1019 | throw new RSIllegalArgumentException("Offset cannot be negative."); |
| 1020 | } |
| 1021 | if (h < 0 || w < 0) { |
| 1022 | throw new RSIllegalArgumentException("Height or width cannot be negative."); |
| 1023 | } |
| 1024 | if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) { |
| 1025 | throw new RSIllegalArgumentException("Updated region larger than allocation."); |
| 1026 | } |
| Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 1027 | } |
| 1028 | } |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 1029 | |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1030 | void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array, |
| 1031 | Element.DataType dt, int arrayLen) { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1032 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked"); |
| Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 1033 | mRS.validate(); |
| 1034 | validate2DRange(xoff, yoff, w, h); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1035 | mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h, |
| 1036 | array, arrayLen * dt.mSize, dt); |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1037 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1040 | /** |
| 1041 | * Copy from an array into a rectangular region in this Allocation. The |
| 1042 | * array is assumed to be tightly packed. |
| 1043 | * |
| 1044 | * @param xoff X offset of the region to update in this Allocation |
| 1045 | * @param yoff Y offset of the region to update in this Allocation |
| 1046 | * @param w Width of the region to update |
| 1047 | * @param h Height of the region to update |
| 1048 | * @param data to be placed into the Allocation |
| 1049 | * @hide |
| 1050 | */ |
| 1051 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) { |
| 1052 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom"); |
| 1053 | copy2DRangeFromUnchecked(xoff, yoff, w, h, array, |
| 1054 | validateObjectIsPrimitiveArray(array, true), |
| 1055 | java.lang.reflect.Array.getLength(array)); |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1056 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 1057 | } |
| 1058 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1059 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1060 | * Copy from an array into a rectangular region in this Allocation. The |
| 1061 | * array is assumed to be tightly packed. |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1062 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1063 | * @param xoff X offset of the region to update in this Allocation |
| 1064 | * @param yoff Y offset of the region to update in this Allocation |
| 1065 | * @param w Width of the region to update |
| 1066 | * @param h Height of the region to update |
| 1067 | * @param data to be placed into the Allocation |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1068 | */ |
| 1069 | 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] | 1070 | validateIsInt8(); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1071 | copy2DRangeFromUnchecked(xoff, yoff, w, h, data, |
| 1072 | Element.DataType.SIGNED_8, data.length); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1073 | } |
| 1074 | |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1075 | /** |
| 1076 | * Copy from an array into a rectangular region in this Allocation. The |
| 1077 | * array is assumed to be tightly packed. |
| 1078 | * |
| 1079 | * @param xoff X offset of the region to update in this Allocation |
| 1080 | * @param yoff Y offset of the region to update in this Allocation |
| 1081 | * @param w Width of the region to update |
| 1082 | * @param h Height of the region to update |
| 1083 | * @param data to be placed into the Allocation |
| 1084 | */ |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1085 | 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] | 1086 | validateIsInt16(); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1087 | copy2DRangeFromUnchecked(xoff, yoff, w, h, data, |
| 1088 | Element.DataType.SIGNED_16, data.length); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1089 | } |
| 1090 | |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1091 | /** |
| 1092 | * Copy from an array into a rectangular region in this Allocation. The |
| 1093 | * array is assumed to be tightly packed. |
| 1094 | * |
| 1095 | * @param xoff X offset of the region to update in this Allocation |
| 1096 | * @param yoff Y offset of the region to update in this Allocation |
| 1097 | * @param w Width of the region to update |
| 1098 | * @param h Height of the region to update |
| 1099 | * @param data to be placed into the Allocation |
| 1100 | */ |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1101 | 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] | 1102 | validateIsInt32(); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1103 | copy2DRangeFromUnchecked(xoff, yoff, w, h, data, |
| 1104 | Element.DataType.SIGNED_32, data.length); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1107 | /** |
| 1108 | * Copy from an array into a rectangular region in this Allocation. The |
| 1109 | * array is assumed to be tightly packed. |
| 1110 | * |
| 1111 | * @param xoff X offset of the region to update in this Allocation |
| 1112 | * @param yoff Y offset of the region to update in this Allocation |
| 1113 | * @param w Width of the region to update |
| 1114 | * @param h Height of the region to update |
| 1115 | * @param data to be placed into the Allocation |
| 1116 | */ |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1117 | 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] | 1118 | validateIsFloat32(); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1119 | copy2DRangeFromUnchecked(xoff, yoff, w, h, data, |
| 1120 | Element.DataType.FLOAT_32, data.length); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1123 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1124 | * Copy a rectangular region from an Allocation into a rectangular region in |
| 1125 | * this Allocation. |
| Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1126 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1127 | * @param xoff X offset of the region in this Allocation |
| 1128 | * @param yoff Y offset of the region in this Allocation |
| 1129 | * @param w Width of the region to update. |
| 1130 | * @param h Height of the region to update. |
| 1131 | * @param data source Allocation. |
| 1132 | * @param dataXoff X offset in source Allocation |
| 1133 | * @param dataYoff Y offset in source Allocation |
| Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1134 | */ |
| 1135 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, |
| 1136 | Allocation data, int dataXoff, int dataYoff) { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1137 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom"); |
| Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1138 | mRS.validate(); |
| 1139 | validate2DRange(xoff, yoff, w, h); |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1140 | mRS.nAllocationData2D(getIDSafe(), xoff, yoff, |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 1141 | mSelectedLOD, mSelectedFace.mID, |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1142 | w, h, data.getID(mRS), dataXoff, dataYoff, |
| Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 1143 | data.mSelectedLOD, data.mSelectedFace.mID); |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1144 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1147 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1148 | * Copy a {@link android.graphics.Bitmap} into an Allocation. The height |
| 1149 | * and width of the update will use the height and width of the {@link |
| 1150 | * android.graphics.Bitmap}. |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1151 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1152 | * @param xoff X offset of the region to update in this Allocation |
| 1153 | * @param yoff Y offset of the region to update in this Allocation |
| 1154 | * @param data the Bitmap to be copied |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1155 | */ |
| 1156 | public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1157 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom"); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1158 | mRS.validate(); |
| Tim Murray | abd5db9 | 2013-02-28 11:45:22 -0800 | [diff] [blame] | 1159 | if (data.getConfig() == null) { |
| 1160 | Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888); |
| 1161 | Canvas c = new Canvas(newBitmap); |
| 1162 | c.drawBitmap(data, 0, 0, null); |
| 1163 | copy2DRangeFrom(xoff, yoff, newBitmap); |
| Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1164 | return; |
| Tim Murray | abd5db9 | 2013-02-28 11:45:22 -0800 | [diff] [blame] | 1165 | } |
| Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 1166 | validateBitmapFormat(data); |
| 1167 | validate2DRange(xoff, yoff, data.getWidth(), data.getHeight()); |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1168 | mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data); |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1169 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
| Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1172 | private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) { |
| 1173 | if (mAdaptedAllocation != null) { |
| 1174 | |
| 1175 | } else { |
| 1176 | |
| 1177 | if (xoff < 0 || yoff < 0 || zoff < 0) { |
| 1178 | throw new RSIllegalArgumentException("Offset cannot be negative."); |
| 1179 | } |
| 1180 | if (h < 0 || w < 0 || d < 0) { |
| 1181 | throw new RSIllegalArgumentException("Height or width cannot be negative."); |
| 1182 | } |
| 1183 | if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) { |
| 1184 | throw new RSIllegalArgumentException("Updated region larger than allocation."); |
| 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * @hide |
| 1191 | * |
| 1192 | */ |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1193 | private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, |
| 1194 | Object array, Element.DataType dt, int arrayLen) { |
| 1195 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked"); |
| Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1196 | mRS.validate(); |
| 1197 | validate3DRange(xoff, yoff, zoff, w, h, d); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1198 | mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d, |
| 1199 | array, arrayLen * dt.mSize, dt); |
| 1200 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | /** |
| 1204 | * @hide |
| Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1205 | * Copy a rectangular region from the array into the allocation. |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1206 | * The array is assumed to be tightly packed. |
| Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1207 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1208 | * @param xoff X offset of the region to update in this Allocation |
| 1209 | * @param yoff Y offset of the region to update in this Allocation |
| 1210 | * @param zoff Z offset of the region to update in this Allocation |
| 1211 | * @param w Width of the region to update |
| 1212 | * @param h Height of the region to update |
| 1213 | * @param d Depth of the region to update |
| Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1214 | * @param data to be placed into the allocation |
| 1215 | */ |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1216 | public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) { |
| 1217 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom"); |
| 1218 | copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array, |
| 1219 | validateObjectIsPrimitiveArray(array, true), |
| 1220 | java.lang.reflect.Array.getLength(array)); |
| 1221 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * @hide |
| 1226 | * Copy a rectangular region into the allocation from another |
| 1227 | * allocation. |
| 1228 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1229 | * @param xoff X offset of the region to update in this Allocation |
| 1230 | * @param yoff Y offset of the region to update in this Allocation |
| 1231 | * @param zoff Z offset of the region to update in this Allocation |
| 1232 | * @param w Width of the region to update. |
| 1233 | * @param h Height of the region to update. |
| 1234 | * @param d Depth of the region to update. |
| Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1235 | * @param data source allocation. |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1236 | * @param dataXoff X offset of the region in the source Allocation |
| 1237 | * @param dataYoff Y offset of the region in the source Allocation |
| 1238 | * @param dataZoff Z offset of the region in the source Allocation |
| Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1239 | */ |
| 1240 | public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, |
| 1241 | Allocation data, int dataXoff, int dataYoff, int dataZoff) { |
| 1242 | mRS.validate(); |
| 1243 | validate3DRange(xoff, yoff, zoff, w, h, d); |
| 1244 | mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, |
| 1245 | w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff, |
| 1246 | data.mSelectedLOD); |
| 1247 | } |
| 1248 | |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1249 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1250 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1251 | * Copy from the Allocation into a {@link android.graphics.Bitmap}. The |
| 1252 | * bitmap must match the dimensions of the Allocation. |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1253 | * |
| 1254 | * @param b The bitmap to be set from the Allocation. |
| 1255 | */ |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1256 | public void copyTo(Bitmap b) { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1257 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo"); |
| Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 1258 | mRS.validate(); |
| 1259 | validateBitmapFormat(b); |
| 1260 | validateBitmapSize(b); |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1261 | mRS.nAllocationCopyToBitmap(getID(mRS), b); |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1262 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1265 | private void copyTo(Object array, Element.DataType dt, int arrayLen) { |
| 1266 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo"); |
| 1267 | mRS.validate(); |
| 1268 | mRS.nAllocationRead(getID(mRS), array, dt); |
| 1269 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| 1270 | } |
| 1271 | |
| 1272 | /** |
| 1273 | * Copy from the Allocation into an array. The array must be at |
| 1274 | * least as large as the Allocation. The |
| 1275 | * {@link android.renderscript.Element} must match the component |
| 1276 | * type of the array passed in. |
| 1277 | * |
| 1278 | * @param array The array to be set from the Allocation. |
| 1279 | * @hide |
| 1280 | */ |
| 1281 | public void copyTo(Object array) { |
| 1282 | copyTo(array, validateObjectIsPrimitiveArray(array, true), |
| 1283 | java.lang.reflect.Array.getLength(array)); |
| 1284 | } |
| 1285 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1286 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1287 | * Copy from the Allocation into a byte array. The array must be at least |
| 1288 | * as large as the Allocation. The allocation must be of an 8 bit integer |
| 1289 | * {@link android.renderscript.Element} type. |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1290 | * |
| 1291 | * @param d The array to be set from the Allocation. |
| 1292 | */ |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1293 | public void copyTo(byte[] d) { |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 1294 | validateIsInt8(); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1295 | copyTo(d, Element.DataType.SIGNED_8, d.length); |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1298 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1299 | * Copy from the Allocation into a short array. The array must be at least |
| 1300 | * as large as the Allocation. The allocation must be of an 16 bit integer |
| 1301 | * {@link android.renderscript.Element} type. |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1302 | * |
| 1303 | * @param d The array to be set from the Allocation. |
| 1304 | */ |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1305 | public void copyTo(short[] d) { |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 1306 | validateIsInt16(); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1307 | copyTo(d, Element.DataType.SIGNED_16, d.length); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1308 | } |
| 1309 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1310 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1311 | * Copy from the Allocation into a int array. The array must be at least as |
| 1312 | * large as the Allocation. The allocation must be of an 32 bit integer |
| 1313 | * {@link android.renderscript.Element} type. |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1314 | * |
| 1315 | * @param d The array to be set from the Allocation. |
| 1316 | */ |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1317 | public void copyTo(int[] d) { |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 1318 | validateIsInt32(); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1319 | copyTo(d, Element.DataType.SIGNED_32, d.length); |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1320 | } |
| 1321 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1322 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1323 | * Copy from the Allocation into a float array. The array must be at least |
| 1324 | * as large as the Allocation. The allocation must be of an 32 bit float |
| 1325 | * {@link android.renderscript.Element} type. |
| Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1326 | * |
| 1327 | * @param d The array to be set from the Allocation. |
| 1328 | */ |
| Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1329 | public void copyTo(float[] d) { |
| Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 1330 | validateIsFloat32(); |
| Jason Sams | 1136bb9 | 2013-11-25 18:28:33 -0800 | [diff] [blame] | 1331 | copyTo(d, Element.DataType.FLOAT_32, d.length); |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1334 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1335 | * Resize a 1D allocation. The contents of the allocation are preserved. |
| 1336 | * If new elements are allocated objects are created with null contents and |
| 1337 | * the new region is otherwise undefined. |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1338 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1339 | * <p>If the new region is smaller the references of any objects outside the |
| 1340 | * new region will be released.</p> |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1341 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1342 | * <p>A new type will be created with the new dimension.</p> |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1343 | * |
| 1344 | * @param dimX The new size of the allocation. |
| Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1345 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1346 | * @deprecated RenderScript objects should be immutable once created. The |
| 1347 | * replacement is to create a new allocation and copy the contents. |
| Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1348 | */ |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 1349 | public synchronized void resize(int dimX) { |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1350 | if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 1351 | throw new RSInvalidStateException("Resize only support for 1D allocations at this time."); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 1352 | } |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1353 | mRS.nAllocationResize1D(getID(mRS), dimX); |
| Jason Sams | d26297f | 2010-11-01 16:08:59 -0700 | [diff] [blame] | 1354 | mRS.finish(); // Necessary because resize is fifoed and update is async. |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 1355 | |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 1356 | long typeID = mRS.nAllocationGetType(getID(mRS)); |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 1357 | mType = new Type(typeID, mRS); |
| 1358 | mType.updateFromNative(); |
| Jason Sams | 452a766 | 2011-07-07 16:05:18 -0700 | [diff] [blame] | 1359 | updateCacheInfo(mType); |
| Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1362 | |
| 1363 | // creation |
| 1364 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 1365 | static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options(); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1366 | static { |
| 1367 | mBitmapOptions.inScaled = false; |
| 1368 | } |
| 1369 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1370 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1371 | * Creates a new Allocation with the given {@link |
| 1372 | * android.renderscript.Type}, mipmap flag, and usage flags. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1373 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1374 | * @param type RenderScript type describing data layout |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1375 | * @param mips specifies desired mipmap behaviour for the |
| 1376 | * allocation |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1377 | * @param usage bit field specifying how the Allocation is |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1378 | * utilized |
| 1379 | */ |
| 1380 | static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1381 | Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped"); |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 1382 | rs.validate(); |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1383 | if (type.getID(rs) == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 1384 | throw new RSInvalidStateException("Bad Type"); |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 1385 | } |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 1386 | long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0); |
| Jason Sams | 857d0c7 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 1387 | if (id == 0) { |
| 1388 | throw new RSRuntimeException("Allocation creation failed."); |
| 1389 | } |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1390 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Jason Sams | 857d0c7 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 1391 | return new Allocation(id, rs, type, usage); |
| 1392 | } |
| 1393 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1394 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1395 | * Creates an Allocation with the size specified by the type and no mipmaps |
| 1396 | * generated by default |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1397 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1398 | * @param rs Context to which the allocation will belong. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1399 | * @param type renderscript type describing data layout |
| 1400 | * @param usage bit field specifying how the allocation is |
| 1401 | * utilized |
| 1402 | * |
| 1403 | * @return allocation |
| 1404 | */ |
| Jason Sams | e5d3712 | 2010-12-16 00:33:33 -0800 | [diff] [blame] | 1405 | static public Allocation createTyped(RenderScript rs, Type type, int usage) { |
| 1406 | return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage); |
| 1407 | } |
| 1408 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1409 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1410 | * Creates an Allocation for use by scripts with a given {@link |
| 1411 | * android.renderscript.Type} and no mipmaps |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1412 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1413 | * @param rs Context to which the Allocation will belong. |
| 1414 | * @param type RenderScript Type describing data layout |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1415 | * |
| 1416 | * @return allocation |
| 1417 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1418 | static public Allocation createTyped(RenderScript rs, Type type) { |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 1419 | return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1420 | } |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 1421 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1422 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1423 | * Creates an Allocation with a specified number of given elements |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1424 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1425 | * @param rs Context to which the Allocation will belong. |
| 1426 | * @param e Element to use in the Allocation |
| 1427 | * @param count the number of Elements in the Allocation |
| 1428 | * @param usage bit field specifying how the Allocation is |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1429 | * utilized |
| 1430 | * |
| 1431 | * @return allocation |
| 1432 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1433 | static public Allocation createSized(RenderScript rs, Element e, |
| 1434 | int count, int usage) { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1435 | Trace.traceBegin(RenderScript.TRACE_TAG, "createSized"); |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 1436 | rs.validate(); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 1437 | Type.Builder b = new Type.Builder(rs, e); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1438 | b.setX(count); |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 1439 | Type t = b.create(); |
| 1440 | |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 1441 | long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1442 | if (id == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 1443 | throw new RSRuntimeException("Allocation creation failed."); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1444 | } |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1445 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1446 | return new Allocation(id, rs, t, usage); |
| 1447 | } |
| 1448 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1449 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1450 | * Creates an Allocation with a specified number of given elements |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1451 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1452 | * @param rs Context to which the Allocation will belong. |
| 1453 | * @param e Element to use in the Allocation |
| 1454 | * @param count the number of Elements in the Allocation |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1455 | * |
| 1456 | * @return allocation |
| 1457 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1458 | static public Allocation createSized(RenderScript rs, Element e, int count) { |
| Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 1459 | return createSized(rs, e, count, USAGE_SCRIPT); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 1462 | static Element elementFromBitmap(RenderScript rs, Bitmap b) { |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 1463 | final Bitmap.Config bc = b.getConfig(); |
| 1464 | if (bc == Bitmap.Config.ALPHA_8) { |
| 1465 | return Element.A_8(rs); |
| 1466 | } |
| 1467 | if (bc == Bitmap.Config.ARGB_4444) { |
| 1468 | return Element.RGBA_4444(rs); |
| 1469 | } |
| 1470 | if (bc == Bitmap.Config.ARGB_8888) { |
| 1471 | return Element.RGBA_8888(rs); |
| 1472 | } |
| 1473 | if (bc == Bitmap.Config.RGB_565) { |
| 1474 | return Element.RGB_565(rs); |
| 1475 | } |
| Jeff Sharkey | 4bd1a3d | 2010-11-16 13:46:34 -0800 | [diff] [blame] | 1476 | throw new RSInvalidStateException("Bad bitmap type: " + bc); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 1477 | } |
| 1478 | |
| Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 1479 | static Type typeFromBitmap(RenderScript rs, Bitmap b, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 1480 | MipmapControl mip) { |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 1481 | Element e = elementFromBitmap(rs, b); |
| 1482 | Type.Builder tb = new Type.Builder(rs, e); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1483 | tb.setX(b.getWidth()); |
| 1484 | tb.setY(b.getHeight()); |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 1485 | tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 1486 | return tb.create(); |
| 1487 | } |
| 1488 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1489 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1490 | * Creates an Allocation from a {@link android.graphics.Bitmap}. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1491 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1492 | * @param rs Context to which the allocation will belong. |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1493 | * @param b Bitmap source for the allocation data |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1494 | * @param mips specifies desired mipmap behaviour for the |
| 1495 | * allocation |
| 1496 | * @param usage bit field specifying how the allocation is |
| 1497 | * utilized |
| 1498 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1499 | * @return Allocation containing bitmap data |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1500 | * |
| 1501 | */ |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1502 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 1503 | MipmapControl mips, |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1504 | int usage) { |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1505 | Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap"); |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 1506 | rs.validate(); |
| Tim Murray | abd5db9 | 2013-02-28 11:45:22 -0800 | [diff] [blame] | 1507 | |
| 1508 | // WAR undocumented color formats |
| 1509 | if (b.getConfig() == null) { |
| 1510 | if ((usage & USAGE_SHARED) != 0) { |
| 1511 | throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config."); |
| 1512 | } |
| 1513 | Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888); |
| 1514 | Canvas c = new Canvas(newBitmap); |
| 1515 | c.drawBitmap(b, 0, 0, null); |
| 1516 | return createFromBitmap(rs, newBitmap, mips, usage); |
| 1517 | } |
| 1518 | |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1519 | Type t = typeFromBitmap(rs, b, mips); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 1520 | |
| Tim Murray | a314551 | 2012-12-04 17:59:29 -0800 | [diff] [blame] | 1521 | // enable optimized bitmap path only with no mipmap and script-only usage |
| 1522 | if (mips == MipmapControl.MIPMAP_NONE && |
| 1523 | t.getElement().isCompatible(Element.RGBA_8888(rs)) && |
| Tim Murray | 78e6494 | 2013-04-09 17:28:56 -0700 | [diff] [blame] | 1524 | usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) { |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 1525 | long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage); |
| Tim Murray | a314551 | 2012-12-04 17:59:29 -0800 | [diff] [blame] | 1526 | if (id == 0) { |
| 1527 | throw new RSRuntimeException("Load failed."); |
| 1528 | } |
| 1529 | |
| 1530 | // keep a reference to the Bitmap around to prevent GC |
| 1531 | Allocation alloc = new Allocation(id, rs, t, usage); |
| 1532 | alloc.setBitmap(b); |
| 1533 | return alloc; |
| 1534 | } |
| 1535 | |
| Jason Sams | 9bf1892 | 2013-04-13 19:48:36 -0700 | [diff] [blame] | 1536 | |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 1537 | long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1538 | if (id == 0) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 1539 | throw new RSRuntimeException("Load failed."); |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 1540 | } |
| Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1541 | Trace.traceEnd(RenderScript.TRACE_TAG); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1542 | return new Allocation(id, rs, t, usage); |
| 1543 | } |
| 1544 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1545 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1546 | * Returns the handle to a raw buffer that is being managed by the screen |
| 1547 | * compositor. This operation is only valid for Allocations with {@link |
| 1548 | * #USAGE_IO_INPUT}. |
| Jason Sams | fb9aa9f | 2012-03-28 15:30:07 -0700 | [diff] [blame] | 1549 | * |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 1550 | * @return Surface object associated with allocation |
| Jason Sams | fb9aa9f | 2012-03-28 15:30:07 -0700 | [diff] [blame] | 1551 | * |
| 1552 | */ |
| 1553 | public Surface getSurface() { |
| Jason Sams | 72226e0 | 2013-02-22 12:45:54 -0800 | [diff] [blame] | 1554 | if ((mUsage & USAGE_IO_INPUT) == 0) { |
| 1555 | throw new RSInvalidStateException("Allocation is not a surface texture."); |
| 1556 | } |
| 1557 | return mRS.nAllocationGetSurface(getID(mRS)); |
| Jason Sams | fb9aa9f | 2012-03-28 15:30:07 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1560 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1561 | * Associate a {@link android.view.Surface} with this Allocation. This |
| 1562 | * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}. |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 1563 | * |
| 1564 | * @param sur Surface to associate with allocation |
| Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 1565 | */ |
| Jason Sams | fb9aa9f | 2012-03-28 15:30:07 -0700 | [diff] [blame] | 1566 | public void setSurface(Surface sur) { |
| 1567 | mRS.validate(); |
| Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 1568 | if ((mUsage & USAGE_IO_OUTPUT) == 0) { |
| 1569 | throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT."); |
| 1570 | } |
| 1571 | |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1572 | mRS.nAllocationSetSurface(getID(mRS), sur); |
| Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 1573 | } |
| 1574 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1575 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1576 | * Creates an Allocation from a {@link android.graphics.Bitmap}. |
| Tim Murray | 00bb454 | 2012-12-17 16:35:06 -0800 | [diff] [blame] | 1577 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1578 | * <p>With target API version 18 or greater, this Allocation will be created |
| 1579 | * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link |
| 1580 | * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this |
| 1581 | * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p> |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1582 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1583 | * @param rs Context to which the allocation will belong. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1584 | * @param b bitmap source for the allocation data |
| 1585 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1586 | * @return Allocation containing bitmap data |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1587 | * |
| 1588 | */ |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 1589 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b) { |
| Tim Murray | 00bb454 | 2012-12-17 16:35:06 -0800 | [diff] [blame] | 1590 | if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) { |
| 1591 | return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
| Tim Murray | 78e6494 | 2013-04-09 17:28:56 -0700 | [diff] [blame] | 1592 | USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE); |
| Tim Murray | 00bb454 | 2012-12-17 16:35:06 -0800 | [diff] [blame] | 1593 | } |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 1594 | return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
| 1595 | USAGE_GRAPHICS_TEXTURE); |
| Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 1596 | } |
| 1597 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1598 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1599 | * Creates a cubemap Allocation from a {@link android.graphics.Bitmap} |
| 1600 | * containing the horizontal list of cube faces. Each face must be a square, |
| 1601 | * have the same size as all other faces, and have a width that is a power |
| 1602 | * of 2. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1603 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1604 | * @param rs Context to which the allocation will belong. |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1605 | * @param b Bitmap with cubemap faces layed out in the following |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1606 | * format: right, left, top, bottom, front, back |
| 1607 | * @param mips specifies desired mipmap behaviour for the cubemap |
| 1608 | * @param usage bit field specifying how the cubemap is utilized |
| 1609 | * |
| 1610 | * @return allocation containing cubemap data |
| 1611 | * |
| 1612 | */ |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1613 | static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 1614 | MipmapControl mips, |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1615 | int usage) { |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1616 | rs.validate(); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1617 | |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1618 | int height = b.getHeight(); |
| 1619 | int width = b.getWidth(); |
| 1620 | |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 1621 | if (width % 6 != 0) { |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1622 | throw new RSIllegalArgumentException("Cubemap height must be multiple of 6"); |
| 1623 | } |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 1624 | if (width / 6 != height) { |
| Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1625 | throw new RSIllegalArgumentException("Only square cube map faces supported"); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1626 | } |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 1627 | boolean isPow2 = (height & (height - 1)) == 0; |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1628 | if (!isPow2) { |
| 1629 | throw new RSIllegalArgumentException("Only power of 2 cube faces supported"); |
| 1630 | } |
| 1631 | |
| 1632 | Element e = elementFromBitmap(rs, b); |
| 1633 | Type.Builder tb = new Type.Builder(rs, e); |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 1634 | tb.setX(height); |
| 1635 | tb.setY(height); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1636 | tb.setFaces(true); |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 1637 | tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1638 | Type t = tb.create(); |
| 1639 | |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 1640 | long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1641 | if(id == 0) { |
| 1642 | throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e); |
| 1643 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1644 | return new Allocation(id, rs, t, usage); |
| Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1645 | } |
| 1646 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1647 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1648 | * Creates a non-mipmapped cubemap Allocation for use as a graphics texture |
| 1649 | * from a {@link android.graphics.Bitmap} containing the horizontal list of |
| 1650 | * cube faces. Each face must be a square, have the same size as all other |
| 1651 | * faces, and have a width that is a power of 2. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1652 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1653 | * @param rs Context to which the allocation will belong. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1654 | * @param b bitmap with cubemap faces layed out in the following |
| 1655 | * format: right, left, top, bottom, front, back |
| 1656 | * |
| 1657 | * @return allocation containing cubemap data |
| 1658 | * |
| 1659 | */ |
| Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1660 | static public Allocation createCubemapFromBitmap(RenderScript rs, |
| 1661 | Bitmap b) { |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 1662 | return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
| Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 1663 | USAGE_GRAPHICS_TEXTURE); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1666 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1667 | * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap} |
| 1668 | * objects containing the cube faces. Each face must be a square, have the |
| 1669 | * same size as all other faces, and have a width that is a power of 2. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1670 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1671 | * @param rs Context to which the allocation will belong. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1672 | * @param xpos cubemap face in the positive x direction |
| 1673 | * @param xneg cubemap face in the negative x direction |
| 1674 | * @param ypos cubemap face in the positive y direction |
| 1675 | * @param yneg cubemap face in the negative y direction |
| 1676 | * @param zpos cubemap face in the positive z direction |
| 1677 | * @param zneg cubemap face in the negative z direction |
| 1678 | * @param mips specifies desired mipmap behaviour for the cubemap |
| 1679 | * @param usage bit field specifying how the cubemap is utilized |
| 1680 | * |
| 1681 | * @return allocation containing cubemap data |
| 1682 | * |
| 1683 | */ |
| Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1684 | static public Allocation createCubemapFromCubeFaces(RenderScript rs, |
| 1685 | Bitmap xpos, |
| 1686 | Bitmap xneg, |
| 1687 | Bitmap ypos, |
| 1688 | Bitmap yneg, |
| 1689 | Bitmap zpos, |
| 1690 | Bitmap zneg, |
| 1691 | MipmapControl mips, |
| 1692 | int usage) { |
| 1693 | int height = xpos.getHeight(); |
| 1694 | if (xpos.getWidth() != height || |
| 1695 | xneg.getWidth() != height || xneg.getHeight() != height || |
| 1696 | ypos.getWidth() != height || ypos.getHeight() != height || |
| 1697 | yneg.getWidth() != height || yneg.getHeight() != height || |
| 1698 | zpos.getWidth() != height || zpos.getHeight() != height || |
| 1699 | zneg.getWidth() != height || zneg.getHeight() != height) { |
| 1700 | throw new RSIllegalArgumentException("Only square cube map faces supported"); |
| 1701 | } |
| 1702 | boolean isPow2 = (height & (height - 1)) == 0; |
| 1703 | if (!isPow2) { |
| 1704 | throw new RSIllegalArgumentException("Only power of 2 cube faces supported"); |
| 1705 | } |
| 1706 | |
| 1707 | Element e = elementFromBitmap(rs, xpos); |
| 1708 | Type.Builder tb = new Type.Builder(rs, e); |
| 1709 | tb.setX(height); |
| 1710 | tb.setY(height); |
| 1711 | tb.setFaces(true); |
| 1712 | tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL); |
| 1713 | Type t = tb.create(); |
| 1714 | Allocation cubemap = Allocation.createTyped(rs, t, mips, usage); |
| 1715 | |
| 1716 | AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap); |
| Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 1717 | adapter.setFace(Type.CubemapFace.POSITIVE_X); |
| Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1718 | adapter.copyFrom(xpos); |
| 1719 | adapter.setFace(Type.CubemapFace.NEGATIVE_X); |
| 1720 | adapter.copyFrom(xneg); |
| Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 1721 | adapter.setFace(Type.CubemapFace.POSITIVE_Y); |
| Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1722 | adapter.copyFrom(ypos); |
| 1723 | adapter.setFace(Type.CubemapFace.NEGATIVE_Y); |
| 1724 | adapter.copyFrom(yneg); |
| Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 1725 | adapter.setFace(Type.CubemapFace.POSITIVE_Z); |
| Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1726 | adapter.copyFrom(zpos); |
| 1727 | adapter.setFace(Type.CubemapFace.NEGATIVE_Z); |
| 1728 | adapter.copyFrom(zneg); |
| 1729 | |
| 1730 | return cubemap; |
| 1731 | } |
| 1732 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1733 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1734 | * Creates a non-mipmapped cubemap Allocation for use as a sampler input |
| 1735 | * from 6 {@link android.graphics.Bitmap} objects containing the cube |
| 1736 | * faces. Each face must be a square, have the same size as all other faces, |
| 1737 | * and have a width that is a power of 2. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1738 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1739 | * @param rs Context to which the allocation will belong. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1740 | * @param xpos cubemap face in the positive x direction |
| 1741 | * @param xneg cubemap face in the negative x direction |
| 1742 | * @param ypos cubemap face in the positive y direction |
| 1743 | * @param yneg cubemap face in the negative y direction |
| 1744 | * @param zpos cubemap face in the positive z direction |
| 1745 | * @param zneg cubemap face in the negative z direction |
| 1746 | * |
| 1747 | * @return allocation containing cubemap data |
| 1748 | * |
| 1749 | */ |
| Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1750 | static public Allocation createCubemapFromCubeFaces(RenderScript rs, |
| 1751 | Bitmap xpos, |
| 1752 | Bitmap xneg, |
| 1753 | Bitmap ypos, |
| 1754 | Bitmap yneg, |
| 1755 | Bitmap zpos, |
| 1756 | Bitmap zneg) { |
| 1757 | return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg, |
| 1758 | zpos, zneg, MipmapControl.MIPMAP_NONE, |
| 1759 | USAGE_GRAPHICS_TEXTURE); |
| 1760 | } |
| 1761 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1762 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1763 | * Creates an Allocation from the Bitmap referenced |
| 1764 | * by resource ID. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1765 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1766 | * @param rs Context to which the allocation will belong. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1767 | * @param res application resources |
| 1768 | * @param id resource id to load the data from |
| 1769 | * @param mips specifies desired mipmap behaviour for the |
| 1770 | * allocation |
| 1771 | * @param usage bit field specifying how the allocation is |
| 1772 | * utilized |
| 1773 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1774 | * @return Allocation containing resource data |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1775 | * |
| 1776 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1777 | static public Allocation createFromBitmapResource(RenderScript rs, |
| 1778 | Resources res, |
| 1779 | int id, |
| Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 1780 | MipmapControl mips, |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1781 | int usage) { |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1782 | |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 1783 | rs.validate(); |
| Jason Sams | 3ece2f3 | 2013-05-31 14:00:46 -0700 | [diff] [blame] | 1784 | if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) { |
| 1785 | throw new RSIllegalArgumentException("Unsupported usage specified."); |
| 1786 | } |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1787 | Bitmap b = BitmapFactory.decodeResource(res, id); |
| 1788 | Allocation alloc = createFromBitmap(rs, b, mips, usage); |
| 1789 | b.recycle(); |
| 1790 | return alloc; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1791 | } |
| 1792 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1793 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1794 | * Creates a non-mipmapped Allocation to use as a graphics texture from the |
| 1795 | * {@link android.graphics.Bitmap} referenced by resource ID. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1796 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1797 | * <p>With target API version 18 or greater, this allocation will be created |
| 1798 | * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With |
| 1799 | * target API version 17 or lower, this allocation will be created with |
| 1800 | * {@link #USAGE_GRAPHICS_TEXTURE}.</p> |
| Jason Sams | 455d644 | 2013-02-05 19:20:18 -0800 | [diff] [blame] | 1801 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1802 | * @param rs Context to which the allocation will belong. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1803 | * @param res application resources |
| 1804 | * @param id resource id to load the data from |
| 1805 | * |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1806 | * @return Allocation containing resource data |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1807 | * |
| 1808 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1809 | static public Allocation createFromBitmapResource(RenderScript rs, |
| 1810 | Resources res, |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 1811 | int id) { |
| Jason Sams | 455d644 | 2013-02-05 19:20:18 -0800 | [diff] [blame] | 1812 | if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) { |
| 1813 | return createFromBitmapResource(rs, res, id, |
| 1814 | MipmapControl.MIPMAP_NONE, |
| Jason Sams | 3ece2f3 | 2013-05-31 14:00:46 -0700 | [diff] [blame] | 1815 | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE); |
| Jason Sams | 455d644 | 2013-02-05 19:20:18 -0800 | [diff] [blame] | 1816 | } |
| Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 1817 | return createFromBitmapResource(rs, res, id, |
| 1818 | MipmapControl.MIPMAP_NONE, |
| 1819 | USAGE_GRAPHICS_TEXTURE); |
| 1820 | } |
| 1821 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1822 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1823 | * Creates an Allocation containing string data encoded in UTF-8 format. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1824 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1825 | * @param rs Context to which the allocation will belong. |
| Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1826 | * @param str string to create the allocation from |
| 1827 | * @param usage bit field specifying how the allocaiton is |
| 1828 | * utilized |
| 1829 | * |
| 1830 | */ |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1831 | static public Allocation createFromString(RenderScript rs, |
| 1832 | String str, |
| 1833 | int usage) { |
| 1834 | rs.validate(); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1835 | byte[] allocArray = null; |
| 1836 | try { |
| 1837 | allocArray = str.getBytes("UTF-8"); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1838 | Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1839 | alloc.copyFrom(allocArray); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1840 | return alloc; |
| 1841 | } |
| 1842 | catch (Exception e) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 1843 | throw new RSRuntimeException("Could not convert string to utf-8."); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1844 | } |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1845 | } |
| Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 1846 | |
| 1847 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1848 | * Interface to handle notification when new buffers are available via |
| 1849 | * {@link #USAGE_IO_INPUT}. An application will receive one notification |
| 1850 | * when a buffer is available. Additional buffers will not trigger new |
| 1851 | * notifications until a buffer is processed. |
| Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 1852 | */ |
| Jason Sams | 42ef238 | 2013-08-29 13:30:59 -0700 | [diff] [blame] | 1853 | public interface OnBufferAvailableListener { |
| Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 1854 | public void onBufferAvailable(Allocation a); |
| 1855 | } |
| 1856 | |
| 1857 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1858 | * Set a notification handler for {@link #USAGE_IO_INPUT}. |
| Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 1859 | * |
| Jason Sams | 42ef238 | 2013-08-29 13:30:59 -0700 | [diff] [blame] | 1860 | * @param callback instance of the OnBufferAvailableListener |
| 1861 | * class to be called when buffer arrive. |
| Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 1862 | */ |
| Jason Sams | 42ef238 | 2013-08-29 13:30:59 -0700 | [diff] [blame] | 1863 | public void setOnBufferAvailableListener(OnBufferAvailableListener callback) { |
| Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 1864 | synchronized(mAllocationMap) { |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 1865 | mAllocationMap.put(new Long(getID(mRS)), this); |
| Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 1866 | mBufferNotifier = callback; |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | static void sendBufferNotification(int id) { |
| 1871 | synchronized(mAllocationMap) { |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 1872 | Allocation a = mAllocationMap.get(new Long(id)); |
| Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 1873 | |
| 1874 | if ((a != null) && (a.mBufferNotifier != null)) { |
| 1875 | a.mBufferNotifier.onBufferAvailable(a); |
| 1876 | } |
| 1877 | } |
| 1878 | } |
| 1879 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |