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