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