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