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