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