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