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