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