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