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