blob: 238bf0f5a335af6426f3ffc47df03466f8acf56e [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
Stephen Hines9069ee82012-02-13 18:25:54 -08002 * Copyright (C) 2008-2012 The Android Open Source Project
Jason Samsb8c5a842009-07-31 20:40:47 -07003 *
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
17package android.renderscript;
18
Miao Wang0facf022015-11-25 11:21:13 -080019import java.nio.ByteBuffer;
Jason Sams739c8262013-04-11 18:07:52 -070020import java.util.HashMap;
Miao Wang0facf022015-11-25 11:21:13 -080021
Jason Samsb8c5a842009-07-31 20:40:47 -070022import android.content.res.Resources;
23import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
Tim Murrayabd5db92013-02-28 11:45:22 -080025import android.graphics.Canvas;
Tim Murray6d7a53c2013-05-23 16:59:23 -070026import android.os.Trace;
Miao Wang0facf022015-11-25 11:21:13 -080027import android.util.Log;
28import android.view.Surface;
Jason Samsb8c5a842009-07-31 20:40:47 -070029
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070030/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070031 * <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 Samsa23d4e72011-01-04 18:59:12 -080034 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070035 * <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 Samsa23d4e72011-01-04 18:59:12 -080045 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070046 * <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 Samsb8c5a842009-07-31 20:40:47 -070049 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080050 * <div class="special reference">
51 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070052 * <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 Fernandez3aef8e1d2011-12-20 10:38:34 -080054 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070055 **/
Chris Craik06d29842015-06-02 17:19:24 -070056
Jason Samsb8c5a842009-07-31 20:40:47 -070057public class Allocation extends BaseObj {
Miao Wang8c150922015-10-26 17:44:10 -070058 private static final int MAX_NUMBER_IO_INPUT_ALLOC = 16;
59
Jason Sams43ee06852009-08-12 17:54:11 -070060 Type mType;
Yang Nie1798e42016-04-07 11:17:59 -070061 boolean mOwningType = false;
Jason Sams8a647432010-03-01 15:31:04 -080062 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080063 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070064 Allocation mAdaptedAllocation;
Tim Murray2f2472c2013-08-22 14:55:26 -070065 int mSize;
Miao Wang8c150922015-10-26 17:44:10 -070066 MipmapControl mMipmapControl;
Jason Samsba862d12011-07-07 15:24:42 -070067
Miao Wang8c150922015-10-26 17:44:10 -070068 long mTimeStamp = -1;
Jason Sams615e7ce2012-01-13 14:01:20 -080069 boolean mReadAllowed = true;
70 boolean mWriteAllowed = true;
Miao Wang87e908d2015-03-02 15:15:15 -080071 boolean mAutoPadding = false;
Jason Sams46ba27e32015-02-06 17:45:15 -080072 int mSelectedX;
Jason Samsba862d12011-07-07 15:24:42 -070073 int mSelectedY;
74 int mSelectedZ;
75 int mSelectedLOD;
Jason Sams46ba27e32015-02-06 17:45:15 -080076 int mSelectedArray[];
Jason Samsba862d12011-07-07 15:24:42 -070077 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
78
79 int mCurrentDimX;
80 int mCurrentDimY;
81 int mCurrentDimZ;
82 int mCurrentCount;
Tim Murray460a0492013-11-19 12:45:54 -080083 static HashMap<Long, Allocation> mAllocationMap =
84 new HashMap<Long, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070085 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070086
Jason Sams1e68bac2015-03-17 16:36:55 -070087 private Surface mGetSurfaceSurface = null;
Miao Wang0facf022015-11-25 11:21:13 -080088 private ByteBuffer mByteBuffer = null;
89 private long mByteBufferStride = -1;
Jason Sams1e68bac2015-03-17 16:36:55 -070090
Jason Sams3042d262013-11-25 18:28:33 -080091 private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
92 final Class c = d.getClass();
93 if (!c.isArray()) {
94 throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
95 }
96 final Class cmp = c.getComponentType();
97 if (!cmp.isPrimitive()) {
98 throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
99 }
100
101 if (cmp == Long.TYPE) {
102 if (checkType) {
103 validateIsInt64();
104 return mType.mElement.mType;
105 }
106 return Element.DataType.SIGNED_64;
107 }
108
109 if (cmp == Integer.TYPE) {
110 if (checkType) {
111 validateIsInt32();
112 return mType.mElement.mType;
113 }
114 return Element.DataType.SIGNED_32;
115 }
116
117 if (cmp == Short.TYPE) {
118 if (checkType) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800119 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -0800120 return mType.mElement.mType;
121 }
122 return Element.DataType.SIGNED_16;
123 }
124
125 if (cmp == Byte.TYPE) {
126 if (checkType) {
127 validateIsInt8();
128 return mType.mElement.mType;
129 }
130 return Element.DataType.SIGNED_8;
131 }
132
133 if (cmp == Float.TYPE) {
134 if (checkType) {
135 validateIsFloat32();
136 }
137 return Element.DataType.FLOAT_32;
138 }
139
140 if (cmp == Double.TYPE) {
141 if (checkType) {
142 validateIsFloat64();
143 }
144 return Element.DataType.FLOAT_64;
145 }
Pirama Arumuga Nainar3934dad2016-03-28 12:00:00 -0700146
147 throw new RSIllegalArgumentException("Parameter of type " + cmp.getSimpleName() +
148 "[] is not compatible with data type " + mType.mElement.mType.name() +
149 " of allocation");
Jason Sams3042d262013-11-25 18:28:33 -0800150 }
151
152
Tim Murrayc11e25c2013-04-09 11:01:01 -0700153 /**
154 * The usage of the Allocation. These signal to RenderScript where to place
155 * the Allocation in memory.
156 *
157 */
Jason Sams5476b452010-12-08 16:14:36 -0800158
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700159 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700160 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -0800161 */
Jason Sams5476b452010-12-08 16:14:36 -0800162 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800163
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700164 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700165 * The Allocation will be used as a texture source by one or more graphics
166 * programs.
Jason Samsf7086092011-01-12 13:28:37 -0800167 *
168 */
Jason Sams5476b452010-12-08 16:14:36 -0800169 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800170
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700171 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700172 * The Allocation will be used as a graphics mesh.
173 *
174 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800175 *
176 */
Jason Sams5476b452010-12-08 16:14:36 -0800177 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800178
179
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700180 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700181 * The Allocation will be used as the source of shader constants by one or
182 * more programs.
183 *
184 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800185 *
186 */
Jason Sams5476b452010-12-08 16:14:36 -0800187 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
188
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700189 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700190 * The Allocation will be used as a target for offscreen rendering
191 *
192 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700193 *
194 */
195 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
196
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700197 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700198 * The Allocation will be used as a {@link android.view.Surface}
199 * consumer. This usage will cause the Allocation to be created
200 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800201 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800202 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700203 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800204
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700205 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700206 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700207 * producer. The dimensions and format of the {@link
Jason Sams3a1b8e42013-09-24 15:18:52 -0700208 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700209 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800210 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800211 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700212 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700213
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700214 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700215 * The Allocation's backing store will be inherited from another object
216 * (usually a {@link android.graphics.Bitmap}); copying to or from the
217 * original source Bitmap will cause a synchronization rather than a full
218 * copy. {@link #syncAll} may also be used to synchronize the Allocation
219 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800220 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700221 * <p>This is set by default for allocations created with {@link
222 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800223 *
224 */
225 public static final int USAGE_SHARED = 0x0080;
226
227 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700228 * Controls mipmap behavior when using the bitmap creation and update
229 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800230 */
Jason Sams4ef66502010-12-10 16:03:15 -0800231 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700232 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700233 * No mipmaps will be generated and the type generated from the incoming
234 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800235 */
Jason Sams5476b452010-12-08 16:14:36 -0800236 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800237
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700238 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700239 * A full mipmap chain will be created in script memory. The Type of
240 * the Allocation will contain a full mipmap chain. On upload, the full
241 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800242 */
Jason Sams5476b452010-12-08 16:14:36 -0800243 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800244
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700245 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700246 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
247 * not contain mipmaps. On upload, the allocation data will contain a
248 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800249 */
Jason Sams5476b452010-12-08 16:14:36 -0800250 MIPMAP_ON_SYNC_TO_TEXTURE(2);
251
252 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800253 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800254 mID = id;
255 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700256 }
257
Jason Sams48fe5342011-07-08 13:52:30 -0700258
Tim Murray460a0492013-11-19 12:45:54 -0800259 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700260 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700261 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700262 }
Jason Samse07694b2012-04-03 15:36:36 -0700263 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700264 }
265
Jason Sams03d2d002012-03-23 13:51:56 -0700266
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700267 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700268 * Get the {@link android.renderscript.Element} of the {@link
269 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700270 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700271 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700272 *
273 */
274 public Element getElement() {
275 return mType.getElement();
276 }
277
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700278 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700279 * Get the usage flags of the Allocation.
280 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700281 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700282 *
283 */
284 public int getUsage() {
285 return mUsage;
286 }
287
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700288 /**
Miao Wang8c150922015-10-26 17:44:10 -0700289 * @hide
290 * Get the Mipmap control flag of the Allocation.
291 *
292 * @return the Mipmap control flag of the Allocation
293 *
294 */
295 public MipmapControl getMipmap() {
296 return mMipmapControl;
297 }
298
299 /**
Miao Wang3231e8e2016-04-01 15:10:47 -0700300 * Specifies the mapping between the Allocation's cells and an array's elements
301 * when data is copied from the Allocation to the array, or vice-versa.
Miao Wang9ee76072016-03-29 15:56:55 -0700302 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700303 * Only applies to an Allocation whose Element is a vector of length 3 (such as
304 * {@link Element#U8_3} or {@link Element#RGB_888}). Enabling this feature may make
305 * copying data from the Allocation to an array or vice-versa less efficient.
306 *
307 * <p> Vec3 Element cells are stored in an Allocation as Vec4 Element cells with
308 * the same {@link android.renderscript.Element.DataType}, with the fourth vector
309 * component treated as padding. When this feature is enabled, only the data components,
310 * i.e. the first 3 vector components of each cell, will be mapped between the array
311 * and the Allocation. When disabled, explicit mapping of the padding components
312 * is required, as described in the following example.
313 *
314 * <p> For example, when copying an integer array to an Allocation of two {@link
315 * Element#I32_3} cells using {@link #copyFrom(int[])}:
316 * <p> When disabled:
317 * The array must have at least 8 integers, with the first 4 integers copied
318 * to the first cell of the Allocation, and the next 4 integers copied to
319 * the second cell. The 4th and 8th integers are mapped as the padding components.
320 *
321 * <p> When enabled:
322 * The array just needs to have at least 6 integers, with the first 3 integers
323 * copied to the the first cell as data components, and the next 3 copied to
324 * the second cell. There is no mapping for the padding components.
325 *
326 * <p> Similarly, when copying a byte array to an Allocation of two {@link
327 * Element#I32_3} cells, using {@link #copyFromUnchecked(int[])}:
328 * <p> When disabled:
329 * The array must have at least 32 bytes, with the first 16 bytes copied
330 * to the first cell of the Allocation, and the next 16 bytes copied to
331 * the second cell. The 13th-16th and 29th-32nd bytes are mapped as padding
332 * components.
333 *
334 * <p> When enabled:
335 * The array just needs to have at least 24 bytes, with the first 12 bytes copied
336 * to the first cell of the Allocation, and the next 12 bytes copied to
337 * the second cell. There is no mapping for the padding components.
338 *
339 * <p> Similar to copying data to an Allocation from an array, when copying data from an
340 * Allocation to an array, the padding components for Vec3 Element cells will not be
341 * copied/mapped to the array if AutoPadding is enabled.
342 *
343 * <p> Default: Disabled.
Miao Wang87e908d2015-03-02 15:15:15 -0800344 *
Miao Wang179e8b52015-04-15 17:44:32 -0700345 * @param useAutoPadding True: enable AutoPadding; False: disable AutoPadding
Miao Wang87e908d2015-03-02 15:15:15 -0800346 *
347 */
348 public void setAutoPadding(boolean useAutoPadding) {
349 mAutoPadding = useAutoPadding;
350 }
351
352 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700353 * Get the size of the Allocation in bytes.
354 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700355 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700356 *
357 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700358 public int getBytesSize() {
Tim Murray04f0d6e2013-12-17 17:15:25 -0800359 if (mType.mDimYuv != 0) {
360 return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
361 }
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700362 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700363 }
364
Jason Sams452a7662011-07-07 16:05:18 -0700365 private void updateCacheInfo(Type t) {
366 mCurrentDimX = t.getX();
367 mCurrentDimY = t.getY();
368 mCurrentDimZ = t.getZ();
369 mCurrentCount = mCurrentDimX;
370 if (mCurrentDimY > 1) {
371 mCurrentCount *= mCurrentDimY;
372 }
373 if (mCurrentDimZ > 1) {
374 mCurrentCount *= mCurrentDimZ;
375 }
376 }
Jason Samsba862d12011-07-07 15:24:42 -0700377
Tim Murraya3145512012-12-04 17:59:29 -0800378 private void setBitmap(Bitmap b) {
379 mBitmap = b;
380 }
381
Tim Murray460a0492013-11-19 12:45:54 -0800382 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700383 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800384 if ((usage & ~(USAGE_SCRIPT |
385 USAGE_GRAPHICS_TEXTURE |
386 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700387 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800388 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800389 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800390 USAGE_IO_OUTPUT |
391 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800392 throw new RSIllegalArgumentException("Unknown usage specified.");
393 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800394
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700395 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800396 mWriteAllowed = false;
397
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700398 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800399 USAGE_GRAPHICS_TEXTURE |
400 USAGE_SCRIPT)) != 0) {
401 throw new RSIllegalArgumentException("Invalid usage combination.");
402 }
403 }
Jason Sams9bf18922013-04-13 19:48:36 -0700404
Jason Sams5476b452010-12-08 16:14:36 -0800405 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800406 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700407
Jason Sams452a7662011-07-07 16:05:18 -0700408 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700409 // TODO: A3D doesn't have Type info during creation, so we can't
410 // calculate the size ahead of time. We can possibly add a method
411 // to update the size in the future if it seems reasonable.
412 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700413 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700414 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700415 try {
416 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
417 } catch (Exception e) {
418 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
419 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
420 }
Yang Ni6484b6b2016-03-24 09:40:32 -0700421 guard.open("destroy");
Tim Murray2f2472c2013-08-22 14:55:26 -0700422 }
423
Yang Nie1798e42016-04-07 11:17:59 -0700424 Allocation(long id, RenderScript rs, Type t, boolean owningType, int usage, MipmapControl mips) {
Miao Wang8c150922015-10-26 17:44:10 -0700425 this(id, rs, t, usage);
Yang Nie1798e42016-04-07 11:17:59 -0700426 mOwningType = owningType;
Miao Wang8c150922015-10-26 17:44:10 -0700427 mMipmapControl = mips;
428 }
429
Tim Murray2f2472c2013-08-22 14:55:26 -0700430 protected void finalize() throws Throwable {
431 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
432 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700433 }
434
Jason Sams3042d262013-11-25 18:28:33 -0800435 private void validateIsInt64() {
436 if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
437 (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
438 return;
439 }
440 throw new RSIllegalArgumentException(
441 "64 bit integer source does not match allocation type " + mType.mElement.mType);
442 }
443
Jason Samsb97b2512011-01-16 15:04:08 -0800444 private void validateIsInt32() {
445 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
446 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
447 return;
448 }
449 throw new RSIllegalArgumentException(
450 "32 bit integer source does not match allocation type " + mType.mElement.mType);
451 }
452
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800453 private void validateIsInt16OrFloat16() {
Jason Samsb97b2512011-01-16 15:04:08 -0800454 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800455 (mType.mElement.mType == Element.DataType.UNSIGNED_16) ||
456 (mType.mElement.mType == Element.DataType.FLOAT_16)) {
Jason Samsb97b2512011-01-16 15:04:08 -0800457 return;
458 }
459 throw new RSIllegalArgumentException(
460 "16 bit integer source does not match allocation type " + mType.mElement.mType);
461 }
462
463 private void validateIsInt8() {
464 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
465 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
466 return;
467 }
468 throw new RSIllegalArgumentException(
469 "8 bit integer source does not match allocation type " + mType.mElement.mType);
470 }
471
472 private void validateIsFloat32() {
473 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
474 return;
475 }
476 throw new RSIllegalArgumentException(
477 "32 bit float source does not match allocation type " + mType.mElement.mType);
478 }
479
Jason Sams3042d262013-11-25 18:28:33 -0800480 private void validateIsFloat64() {
481 if (mType.mElement.mType == Element.DataType.FLOAT_64) {
482 return;
483 }
484 throw new RSIllegalArgumentException(
485 "64 bit float source does not match allocation type " + mType.mElement.mType);
486 }
487
Jason Samsb97b2512011-01-16 15:04:08 -0800488 private void validateIsObject() {
489 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
490 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
491 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
492 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
493 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
494 (mType.mElement.mType == Element.DataType.RS_MESH) ||
495 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
496 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
497 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
498 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
499 return;
500 }
501 throw new RSIllegalArgumentException(
502 "Object source does not match allocation type " + mType.mElement.mType);
503 }
504
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700505 @Override
506 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800507 super.updateFromNative();
Tim Murray460a0492013-11-19 12:45:54 -0800508 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700509 if(typeID != 0) {
510 mType = new Type(typeID, mRS);
511 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700512 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700513 }
514 }
515
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700516 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700517 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700518 *
519 * @return Type
520 *
521 */
Jason Samsea87e962010-01-12 12:12:28 -0800522 public Type getType() {
523 return mType;
524 }
525
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700526 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700527 * Propagate changes from one usage of the Allocation to the
528 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700529 *
530 */
Jason Sams5476b452010-12-08 16:14:36 -0800531 public void syncAll(int srcLocation) {
Chris Craik06d29842015-06-02 17:19:24 -0700532 try {
533 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
534 switch (srcLocation) {
535 case USAGE_GRAPHICS_TEXTURE:
536 case USAGE_SCRIPT:
537 if ((mUsage & USAGE_SHARED) != 0) {
538 copyFrom(mBitmap);
539 }
540 break;
541 case USAGE_GRAPHICS_CONSTANTS:
542 case USAGE_GRAPHICS_VERTEX:
543 break;
544 case USAGE_SHARED:
545 if ((mUsage & USAGE_SHARED) != 0) {
546 copyTo(mBitmap);
547 }
548 break;
549 default:
550 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
Tim Murray78e64942013-04-09 17:28:56 -0700551 }
Chris Craik06d29842015-06-02 17:19:24 -0700552 mRS.validate();
553 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
554 } finally {
555 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800556 }
Jason Sams5476b452010-12-08 16:14:36 -0800557 }
558
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700559 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700560 * Send a buffer to the output stream. The contents of the Allocation will
561 * be undefined after this operation. This operation is only valid if {@link
562 * #USAGE_IO_OUTPUT} is set on the Allocation.
563 *
Jason Sams163766c2012-02-15 12:04:24 -0800564 *
Jason Sams163766c2012-02-15 12:04:24 -0800565 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700566 public void ioSend() {
Chris Craik06d29842015-06-02 17:19:24 -0700567 try {
568 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
569 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
570 throw new RSIllegalArgumentException(
571 "Can only send buffer if IO_OUTPUT usage specified.");
572 }
573 mRS.validate();
574 mRS.nAllocationIoSend(getID(mRS));
575 } finally {
576 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800577 }
Jason Sams163766c2012-02-15 12:04:24 -0800578 }
579
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700580 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700581 * Receive the latest input into the Allocation. This operation
582 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800583 *
Jason Sams163766c2012-02-15 12:04:24 -0800584 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700585 public void ioReceive() {
Chris Craik06d29842015-06-02 17:19:24 -0700586 try {
587 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
588 if ((mUsage & USAGE_IO_INPUT) == 0) {
589 throw new RSIllegalArgumentException(
590 "Can only receive if IO_INPUT usage specified.");
591 }
592 mRS.validate();
Miao Wang8c150922015-10-26 17:44:10 -0700593 mTimeStamp = mRS.nAllocationIoReceive(getID(mRS));
Chris Craik06d29842015-06-02 17:19:24 -0700594 } finally {
595 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800596 }
Jason Sams163766c2012-02-15 12:04:24 -0800597 }
598
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700599 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700600 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700601 *
602 * @param d Source array.
603 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800604 public void copyFrom(BaseObj[] d) {
Chris Craik06d29842015-06-02 17:19:24 -0700605 try {
606 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
607 mRS.validate();
608 validateIsObject();
609 if (d.length != mCurrentCount) {
610 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
611 mCurrentCount + ", array length = " + d.length);
612 }
Tim Murray460a0492013-11-19 12:45:54 -0800613
Chris Craik06d29842015-06-02 17:19:24 -0700614 if (RenderScript.sPointerSize == 8) {
615 long i[] = new long[d.length * 4];
616 for (int ct=0; ct < d.length; ct++) {
617 i[ct * 4] = d[ct].getID(mRS);
618 }
619 copy1DRangeFromUnchecked(0, mCurrentCount, i);
620 } else {
621 int i[] = new int[d.length];
622 for (int ct=0; ct < d.length; ct++) {
623 i[ct] = (int) d[ct].getID(mRS);
624 }
625 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray3de3dc72014-07-01 16:56:18 -0700626 }
Chris Craik06d29842015-06-02 17:19:24 -0700627 } finally {
628 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800629 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700630 }
631
Jason Samsfb9f82c2011-01-12 14:53:25 -0800632 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800633 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800634 if (bc == null) {
635 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
636 }
Jason Sams252c0782011-01-11 17:42:52 -0800637 switch (bc) {
638 case ALPHA_8:
639 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
640 throw new RSIllegalArgumentException("Allocation kind is " +
641 mType.getElement().mKind + ", type " +
642 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700643 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800644 " bytes, passed bitmap was " + bc);
645 }
646 break;
647 case ARGB_8888:
648 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700649 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800650 throw new RSIllegalArgumentException("Allocation kind is " +
651 mType.getElement().mKind + ", type " +
652 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700653 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800654 " bytes, passed bitmap was " + bc);
655 }
656 break;
657 case RGB_565:
658 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700659 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800660 throw new RSIllegalArgumentException("Allocation kind is " +
661 mType.getElement().mKind + ", type " +
662 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700663 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800664 " bytes, passed bitmap was " + bc);
665 }
666 break;
667 case ARGB_4444:
668 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700669 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800670 throw new RSIllegalArgumentException("Allocation kind is " +
671 mType.getElement().mKind + ", type " +
672 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700673 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800674 " bytes, passed bitmap was " + bc);
675 }
676 break;
677
678 }
Jason Sams4ef66502010-12-10 16:03:15 -0800679 }
680
Jason Samsfb9f82c2011-01-12 14:53:25 -0800681 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700682 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800683 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
684 }
685 }
686
Jason Sams3042d262013-11-25 18:28:33 -0800687 private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -0700688 try {
689 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
690 mRS.validate();
691 if (mCurrentDimZ > 0) {
692 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
693 } else if (mCurrentDimY > 0) {
694 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
695 } else {
696 copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
697 }
698 } finally {
699 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams3042d262013-11-25 18:28:33 -0800700 }
Jason Sams3042d262013-11-25 18:28:33 -0800701 }
702
Miao Wang3231e8e2016-04-01 15:10:47 -0700703
Jason Sams3042d262013-11-25 18:28:33 -0800704 /**
705 * Copy into this Allocation from an array. This method does not guarantee
706 * that the Allocation is compatible with the input buffer; it copies memory
707 * without reinterpretation.
708 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700709 * <p> If the Allocation does not have Vec3 Elements, then the size of the
710 * array in bytes must be at least the size of the Allocation {@link
711 * #getBytesSize getBytesSize()}.
712 *
713 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
714 * is disabled, then the size of the array in bytes must be at least the size
715 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
716 * the cells must be part of the array.
717 *
718 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
719 * is enabled, then the size of the array in bytes must be at least 3/4 the size
720 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
721 * the cells must not be part of the array.
722 *
723 * @param array The source array
Jason Sams3042d262013-11-25 18:28:33 -0800724 */
725 public void copyFromUnchecked(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700726 try {
727 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
728 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
729 java.lang.reflect.Array.getLength(array));
730 } finally {
731 Trace.traceEnd(RenderScript.TRACE_TAG);
732 }
Jason Sams3042d262013-11-25 18:28:33 -0800733 }
734
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700735 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700736 * Copy into this Allocation from an array. This method does not guarantee
737 * that the Allocation is compatible with the input buffer; it copies memory
738 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800739 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700740 * <p> If the Allocation does not have Vec3 Elements, then the size of the
741 * array in bytes must be at least the size of the Allocation {@link
742 * #getBytesSize getBytesSize()}.
743 *
744 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
745 * is disabled, then the size of the array in bytes must be at least the size
746 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
747 * the cells must be part of the array.
748 *
749 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
750 * is enabled, then the size of the array in bytes must be at least 3/4 the size
751 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
752 * the cells must not be part of the array.
753 *
754 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800755 */
756 public void copyFromUnchecked(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800757 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800758 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700759
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700760 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700761 * Copy into this Allocation from an array. This method does not guarantee
762 * that the Allocation is compatible with the input buffer; it copies memory
763 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800764 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700765 * <p> If the Allocation does not have Vec3 Elements, then the size of the
766 * array in bytes must be at least the size of the Allocation {@link
767 * #getBytesSize getBytesSize()}.
768 *
769 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
770 * is disabled, then the size of the array in bytes must be at least the size
771 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
772 * the cells must be part of the array.
773 *
774 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
775 * is enabled, then the size of the array in bytes must be at least 3/4 the size
776 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
777 * the cells must not be part of the array.
778 *
779 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800780 */
781 public void copyFromUnchecked(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800782 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800783 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700784
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700785 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700786 * Copy into this Allocation from an array. This method does not guarantee
787 * that the Allocation is compatible with the input buffer; it copies memory
788 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800789 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700790 * <p> If the Allocation does not have Vec3 Elements, then the size of the
791 * array in bytes must be at least the size of the Allocation {@link
792 * #getBytesSize getBytesSize()}.
793 *
794 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
795 * is disabled, then the size of the array in bytes must be at least the size
796 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
797 * the cells must be part of the array.
798 *
799 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
800 * is enabled, then the size of the array in bytes must be at least 3/4 the size
801 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
802 * the cells must not be part of the array.
803 *
804 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800805 */
806 public void copyFromUnchecked(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800807 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800808 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700809
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700810 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700811 * Copy into this Allocation from an array. This method does not guarantee
812 * that the Allocation is compatible with the input buffer; it copies memory
813 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800814 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700815 * <p> If the Allocation does not have Vec3 Elements, then the size of the
816 * array in bytes must be at least the size of the Allocation {@link
817 * #getBytesSize getBytesSize()}.
818 *
819 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
820 * is disabled, then the size of the array in bytes must be at least the size
821 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
822 * the cells must be part of the array.
823 *
824 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
825 * is enabled, then the size of the array in bytes must be at least 3/4 the size
826 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
827 * the cells must not be part of the array.
828 *
829 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800830 */
831 public void copyFromUnchecked(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800832 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800833 }
834
Tim Murray6d7a53c2013-05-23 16:59:23 -0700835
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700836 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700837 * Copy into this Allocation from an array. This variant is type checked
838 * and will generate exceptions if the Allocation's {@link
Jason Sams3042d262013-11-25 18:28:33 -0800839 * android.renderscript.Element} does not match the array's
840 * primitive type.
841 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700842 * <p> If the Allocation does not have Vec3 Elements, then the size of the
843 * array in bytes must be at least the size of the Allocation {@link
844 * #getBytesSize getBytesSize()}.
845 *
846 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
847 * is disabled, then the size of the array in bytes must be at least the size
848 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
849 * the cells must be part of the array.
850 *
851 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
852 * is enabled, then the size of the array in bytes must be at least 3/4 the size
853 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
854 * the cells must not be part of the array.
855 *
856 * @param array The source array
Jason Sams3042d262013-11-25 18:28:33 -0800857 */
858 public void copyFrom(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700859 try {
860 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
861 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
862 java.lang.reflect.Array.getLength(array));
863 } finally {
864 Trace.traceEnd(RenderScript.TRACE_TAG);
865 }
Jason Sams3042d262013-11-25 18:28:33 -0800866 }
867
868 /**
869 * Copy into this Allocation from an array. This variant is type checked
870 * and will generate exceptions if the Allocation's {@link
Miao Wang3231e8e2016-04-01 15:10:47 -0700871 * android.renderscript.Element} is not a 32 bit integer nor a vector of 32 bit
872 * integers {@link android.renderscript.Element.DataType}.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800873 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700874 * <p> If the Allocation does not have Vec3 Elements, then the size of the
875 * array in bytes must be at least the size of the Allocation {@link
876 * #getBytesSize getBytesSize()}.
877 *
878 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
879 * is disabled, then the size of the array in bytes must be at least the size
880 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
881 * the cells must be part of the array.
882 *
883 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
884 * is enabled, then the size of the array in bytes must be at least 3/4 the size
885 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
886 * the cells must not be part of the array.
887 *
888 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800889 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800890 public void copyFrom(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800891 validateIsInt32();
892 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800893 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800894
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700895 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700896 * Copy into this Allocation from an array. This variant is type checked
897 * and will generate exceptions if the Allocation's {@link
Miao Wang3231e8e2016-04-01 15:10:47 -0700898 * android.renderscript.Element} is not a 16 bit integer nor a vector of 16 bit
899 * integers {@link android.renderscript.Element.DataType}.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800900 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700901 * <p> If the Allocation does not have Vec3 Elements, then the size of the
902 * array in bytes must be at least the size of the Allocation {@link
903 * #getBytesSize getBytesSize()}.
904 *
905 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
906 * is disabled, then the size of the array in bytes must be at least the size
907 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
908 * the cells must be part of the array.
909 *
910 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
911 * is enabled, then the size of the array in bytes must be at least 3/4 the size
912 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
913 * the cells must not be part of the array.
914 *
915 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800916 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800917 public void copyFrom(short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800918 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -0800919 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800920 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800921
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700922 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700923 * Copy into this Allocation from an array. This variant is type checked
924 * and will generate exceptions if the Allocation's {@link
Miao Wang3231e8e2016-04-01 15:10:47 -0700925 * android.renderscript.Element} is not an 8 bit integer nor a vector of 8 bit
926 * integers {@link android.renderscript.Element.DataType}.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800927 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700928 * <p> If the Allocation does not have Vec3 Elements, then the size of the
929 * array in bytes must be at least the size of the Allocation {@link
930 * #getBytesSize getBytesSize()}.
931 *
932 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
933 * is disabled, then the size of the array in bytes must be at least the size
934 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
935 * the cells must be part of the array.
936 *
937 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
938 * is enabled, then the size of the array in bytes must be at least 3/4 the size
939 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
940 * the cells must not be part of the array.
941 *
942 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800943 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800944 public void copyFrom(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800945 validateIsInt8();
946 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800947 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800948
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700949 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700950 * Copy into this Allocation from an array. This variant is type checked
951 * and will generate exceptions if the Allocation's {@link
Miao Wang3231e8e2016-04-01 15:10:47 -0700952 * android.renderscript.Element} is neither a 32 bit float nor a vector of
953 * 32 bit floats {@link android.renderscript.Element.DataType}.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800954 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700955 * <p> If the Allocation does not have Vec3 Elements, then the size of the
956 * array in bytes must be at least the size of the Allocation {@link
957 * #getBytesSize getBytesSize()}.
958 *
959 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
960 * is disabled, then the size of the array in bytes must be at least the size
961 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
962 * the cells must be part of the array.
963 *
964 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
965 * is enabled, then the size of the array in bytes must be at least 3/4 the size
966 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
967 * the cells must not be part of the array.
968 *
969 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800970 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800971 public void copyFrom(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800972 validateIsFloat32();
973 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800974 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800975
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700976 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700977 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
978 * height, width, and format of the bitmap must match the existing
979 * allocation.
980 *
981 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
982 * android.graphics.Bitmap} used to create the Allocation with {@link
983 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
984 * this will synchronize the Allocation with the latest data from the {@link
985 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800986 *
987 * @param b the source bitmap
988 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800989 public void copyFrom(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -0700990 try {
991 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
992 mRS.validate();
993 if (b.getConfig() == null) {
994 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
995 Canvas c = new Canvas(newBitmap);
996 c.drawBitmap(b, 0, 0, null);
997 copyFrom(newBitmap);
998 return;
999 }
1000 validateBitmapSize(b);
1001 validateBitmapFormat(b);
1002 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
1003 } finally {
1004 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -08001005 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001006 }
1007
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001008 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001009 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -07001010 * must be identical.
1011 *
1012 * @param a the source allocation
1013 */
1014 public void copyFrom(Allocation a) {
Chris Craik06d29842015-06-02 17:19:24 -07001015 try {
1016 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
1017 mRS.validate();
1018 if (!mType.equals(a.getType())) {
1019 throw new RSIllegalArgumentException("Types of allocations must match.");
1020 }
1021 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
1022 } finally {
1023 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -07001024 }
Tim Murrayf671fb02012-10-03 13:50:05 -07001025 }
1026
Tim Murrayf671fb02012-10-03 13:50:05 -07001027 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001028 * This is only intended to be used by auto-generated code reflected from
1029 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -08001030 *
1031 * @param xoff
1032 * @param fp
1033 */
Jason Sams21b41032011-01-16 15:05:41 -08001034 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -08001035 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001036 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -07001037 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -07001038 int data_length = fp.getPos();
Jason Samsa70f4162010-03-26 15:33:42 -07001039
Stephen Hinesfa1275a2014-06-17 17:25:04 -07001040 int count = data_length / eSize;
1041 if ((eSize * count) != data_length) {
1042 throw new RSIllegalArgumentException("Field packer length " + data_length +
Jason Samsa70f4162010-03-26 15:33:42 -07001043 " not divisible by element size " + eSize + ".");
1044 }
Jason Samsba862d12011-07-07 15:24:42 -07001045 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -07001046 }
1047
Miao Wang45cec0a2015-03-04 16:40:21 -08001048
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001049 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001050 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -08001051 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -08001052 *
1053 * @param xoff
1054 * @param component_number
1055 * @param fp
1056 */
Jason Sams21b41032011-01-16 15:05:41 -08001057 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001058 setFromFieldPacker(xoff, 0, 0, component_number, fp);
1059 }
1060
1061 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001062 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -08001063 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -08001064 *
1065 * @param xoff
1066 * @param yoff
Miao Wangc8e237e2015-02-20 18:36:32 -08001067 * @param zoff
1068 * @param component_number
1069 * @param fp
1070 */
1071 public void setFromFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -08001072 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -07001073 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -08001074 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -07001075 }
1076 if(xoff < 0) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001077 throw new RSIllegalArgumentException("Offset x must be >= 0.");
1078 }
1079 if(yoff < 0) {
1080 throw new RSIllegalArgumentException("Offset y must be >= 0.");
1081 }
1082 if(zoff < 0) {
1083 throw new RSIllegalArgumentException("Offset z must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -07001084 }
1085
1086 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -07001087 int data_length = fp.getPos();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001088 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -08001089 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -07001090
Stephen Hinesfa1275a2014-06-17 17:25:04 -07001091 if (data_length != eSize) {
1092 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
Jason Sams49bdaf02010-08-31 13:50:42 -07001093 " does not match component size " + eSize + ".");
1094 }
1095
Miao Wangc8e237e2015-02-20 18:36:32 -08001096 mRS.nAllocationElementData(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1097 component_number, data, data_length);
Jason Samsa70f4162010-03-26 15:33:42 -07001098 }
1099
Miao Wang87e908d2015-03-02 15:15:15 -08001100 private void data1DChecks(int off, int count, int len, int dataSize, boolean usePadding) {
Jason Sams771bebb2009-12-07 12:40:12 -08001101 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -07001102 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001103 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -07001104 }
1105 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -08001106 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -07001107 }
Jason Samsba862d12011-07-07 15:24:42 -07001108 if((off + count) > mCurrentCount) {
1109 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -07001110 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -07001111 }
Miao Wang87e908d2015-03-02 15:15:15 -08001112 if(usePadding) {
1113 if(len < dataSize / 4 * 3) {
1114 throw new RSIllegalArgumentException("Array too small for allocation type.");
1115 }
1116 } else {
1117 if(len < dataSize) {
1118 throw new RSIllegalArgumentException("Array too small for allocation type.");
1119 }
Jason Sams768bc022009-09-21 19:41:04 -07001120 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001121 }
1122
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001123 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001124 * Generate a mipmap chain. This is only valid if the Type of the Allocation
1125 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -08001126 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001127 * <p>This function will generate a complete set of mipmaps from the top
1128 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001129 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001130 * <p>If the Allocation is also using other memory spaces, a call to {@link
1131 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001132 */
1133 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -07001134 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -08001135 }
1136
Jason Sams3042d262013-11-25 18:28:33 -08001137 private void copy1DRangeFromUnchecked(int off, int count, Object array,
1138 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001139 try {
1140 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
1141 final int dataSize = mType.mElement.getBytesSize() * count;
1142 // AutoPadding for Vec3 Element
1143 boolean usePadding = false;
1144 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1145 usePadding = true;
1146 }
1147 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
1148 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
1149 mType.mElement.mType.mSize, usePadding);
1150 } finally {
1151 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001152 }
Jason Sams3042d262013-11-25 18:28:33 -08001153 }
1154
Miao Wang3231e8e2016-04-01 15:10:47 -07001155
Jason Sams3042d262013-11-25 18:28:33 -08001156 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001157 * Copy an array into a 1D region of this Allocation. This method does not
Jason Sams3042d262013-11-25 18:28:33 -08001158 * guarantee that the Allocation is compatible with the input buffer.
1159 *
Miao Wang3231e8e2016-04-01 15:10:47 -07001160 * <p> The size of the region is: count * {@link #getElement}.{@link
1161 * Element#getBytesSize}.
1162 *
1163 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1164 * array in bytes must be at least the size of the region.
1165 *
1166 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1167 * is disabled, then the size of the array in bytes must be at least the size
1168 * of the region. The padding bytes for the cells must be part of the array.
1169 *
1170 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1171 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1172 * of the region. The padding bytes for the cells must not be part of the array.
1173 *
Jason Sams3042d262013-11-25 18:28:33 -08001174 * @param off The offset of the first element to be copied.
1175 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001176 * @param array The source array
Jason Sams3042d262013-11-25 18:28:33 -08001177 */
1178 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
1179 copy1DRangeFromUnchecked(off, count, array,
1180 validateObjectIsPrimitiveArray(array, false),
1181 java.lang.reflect.Array.getLength(array));
1182 }
1183
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001184 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001185 * Copy an array into a 1D region of this Allocation. This method does not
Tim Murrayc11e25c2013-04-09 11:01:01 -07001186 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001187 *
Miao Wang3231e8e2016-04-01 15:10:47 -07001188 * <p> The size of the region is: count * {@link #getElement}.{@link
1189 * Element#getBytesSize}.
1190 *
1191 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1192 * array in bytes must be at least the size of the region.
1193 *
1194 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1195 * is disabled, then the size of the array in bytes must be at least the size
1196 * of the region. The padding bytes for the cells must be part of the array.
1197 *
1198 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1199 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1200 * of the region. The padding bytes for the cells must not be part of the array.
1201 *
Jason Sams4fa3eed2011-01-19 15:44:38 -08001202 * @param off The offset of the first element to be copied.
1203 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001204 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001205 */
1206 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001207 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -07001208 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001209
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001210 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001211 * Copy an array into a 1D region of this Allocation. This method does not
Tim Murrayc11e25c2013-04-09 11:01:01 -07001212 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001213 *
Miao Wang3231e8e2016-04-01 15:10:47 -07001214 * <p> The size of the region is: count * {@link #getElement}.{@link
1215 * Element#getBytesSize}.
1216 *
1217 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1218 * array in bytes must be at least the size of the region.
1219 *
1220 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1221 * is disabled, then the size of the array in bytes must be at least the size
1222 * of the region. The padding bytes for the cells must be part of the array.
1223 *
1224 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1225 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1226 * of the region. The padding bytes for the cells must not be part of the array.
1227 *
Jason Sams4fa3eed2011-01-19 15:44:38 -08001228 * @param off The offset of the first element to be copied.
1229 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001230 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001231 */
1232 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001233 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -07001234 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001235
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001236 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001237 * Copy an array into a 1D region of this Allocation. This method does not
Tim Murrayc11e25c2013-04-09 11:01:01 -07001238 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001239 *
Miao Wang3231e8e2016-04-01 15:10:47 -07001240 * <p> The size of the region is: count * {@link #getElement}.{@link
1241 * Element#getBytesSize}.
1242 *
1243 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1244 * array in bytes must be at least the size of the region.
1245 *
1246 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1247 * is disabled, then the size of the array in bytes must be at least the size
1248 * of the region. The padding bytes for the cells must be part of the array.
1249 *
1250 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1251 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1252 * of the region. The padding bytes for the cells must not be part of the array.
1253 *
Jason Sams4fa3eed2011-01-19 15:44:38 -08001254 * @param off The offset of the first element to be copied.
1255 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001256 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001257 */
1258 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001259 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -07001260 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001261
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001262 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001263 * Copy an array into a 1D region of this Allocation. This method does not
Tim Murrayc11e25c2013-04-09 11:01:01 -07001264 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001265 *
Miao Wang3231e8e2016-04-01 15:10:47 -07001266 * <p> The size of the region is: count * {@link #getElement}.{@link
1267 * Element#getBytesSize}.
1268 *
1269 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1270 * array in bytes must be at least the size of the region.
1271 *
1272 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1273 * is disabled, then the size of the array in bytes must be at least the size
1274 * of the region. The padding bytes for the cells must be part of the array.
1275 *
1276 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1277 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1278 * of the region. The padding bytes for the cells must not be part of the array.
1279 *
Jason Sams4fa3eed2011-01-19 15:44:38 -08001280 * @param off The offset of the first element to be copied.
1281 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001282 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001283 */
1284 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001285 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
1286 }
1287
Jason Sams3042d262013-11-25 18:28:33 -08001288 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001289 * Copy an array into a 1D region of this Allocation. This variant is type checked
1290 * and will generate exceptions if the Allocation's {@link
1291 * android.renderscript.Element} does not match the component type
1292 * of the array passed in.
1293 *
1294 * <p> The size of the region is: count * {@link #getElement}.{@link
1295 * Element#getBytesSize}.
1296 *
1297 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1298 * array in bytes must be at least the size of the region.
1299 *
1300 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1301 * is disabled, then the size of the array in bytes must be at least the size
1302 * of the region. The padding bytes for the cells must be part of the array.
1303 *
1304 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1305 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1306 * of the region. The padding bytes for the cells must not be part of the array.
Jason Sams3042d262013-11-25 18:28:33 -08001307 *
1308 * @param off The offset of the first element to be copied.
1309 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001310 * @param array The source array.
Jason Sams3042d262013-11-25 18:28:33 -08001311 */
1312 public void copy1DRangeFrom(int off, int count, Object array) {
1313 copy1DRangeFromUnchecked(off, count, array,
1314 validateObjectIsPrimitiveArray(array, true),
1315 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -07001316 }
1317
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001318 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001319 * Copy an array into a 1D region of this Allocation. This variant is type checked
1320 * and will generate exceptions if the Allocation's {@link
1321 * android.renderscript.Element} is not an 32 bit integer nor a vector of 32 bit
1322 * integers {@link android.renderscript.Element.DataType}.
1323 *
1324 * <p> The size of the region is: count * {@link #getElement}.{@link
1325 * Element#getBytesSize}.
1326 *
1327 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1328 * array in bytes must be at least the size of the region.
1329 *
1330 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1331 * is disabled, then the size of the array in bytes must be at least the size
1332 * of the region. The padding bytes for the cells must be part of the array.
1333 *
1334 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1335 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1336 * of the region. The padding bytes for the cells must not be part of the array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001337 *
1338 * @param off The offset of the first element to be copied.
1339 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001340 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001341 */
Jason Samsb97b2512011-01-16 15:04:08 -08001342 public void copy1DRangeFrom(int off, int count, int[] d) {
1343 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001344 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001345 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001346
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001347 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001348 * Copy an array into a 1D region of this Allocation. This variant is type checked
1349 * and will generate exceptions if the Allocation's {@link
1350 * android.renderscript.Element} is not an 16 bit integer nor a vector of 16 bit
1351 * integers {@link android.renderscript.Element.DataType}.
1352 *
1353 * <p> The size of the region is: count * {@link #getElement}.{@link
1354 * Element#getBytesSize}.
1355 *
1356 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1357 * array in bytes must be at least the size of the region.
1358 *
1359 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1360 * is disabled, then the size of the array in bytes must be at least the size
1361 * of the region. The padding bytes for the cells must be part of the array.
1362 *
1363 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1364 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1365 * of the region. The padding bytes for the cells must not be part of the array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001366 *
1367 * @param off The offset of the first element to be copied.
1368 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001369 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001370 */
Jason Samsb97b2512011-01-16 15:04:08 -08001371 public void copy1DRangeFrom(int off, int count, short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001372 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -08001373 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001374 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001375
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001376 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001377 * Copy an array into a 1D region of this Allocation. This variant is type checked
1378 * and will generate exceptions if the Allocation's {@link
1379 * android.renderscript.Element} is not an 8 bit integer nor a vector of 8 bit
1380 * integers {@link android.renderscript.Element.DataType}.
1381 *
1382 * <p> The size of the region is: count * {@link #getElement}.{@link
1383 * Element#getBytesSize}.
1384 *
1385 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1386 * array in bytes must be at least the size of the region.
1387 *
1388 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1389 * is disabled, then the size of the array in bytes must be at least the size
1390 * of the region. The padding bytes for the cells must be part of the array.
1391 *
1392 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1393 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1394 * of the region. The padding bytes for the cells must not be part of the array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001395 *
1396 * @param off The offset of the first element to be copied.
1397 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001398 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001399 */
Jason Samsb97b2512011-01-16 15:04:08 -08001400 public void copy1DRangeFrom(int off, int count, byte[] d) {
1401 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001402 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001403 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001404
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001405 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001406 * Copy an array into a 1D region of this Allocation. This variant is type checked
1407 * and will generate exceptions if the Allocation's {@link
1408 * android.renderscript.Element} is neither a 32 bit float nor a vector of
1409 * 32 bit floats {@link android.renderscript.Element.DataType}.
1410 *
1411 * <p> The size of the region is: count * {@link #getElement}.{@link
1412 * Element#getBytesSize}.
1413 *
1414 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1415 * array in bytes must be at least the size of the region.
1416 *
1417 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1418 * is disabled, then the size of the array in bytes must be at least the size
1419 * of the region. The padding bytes for the cells must be part of the array.
1420 *
1421 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1422 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1423 * of the region. The padding bytes for the cells must not be part of the array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001424 *
1425 * @param off The offset of the first element to be copied.
1426 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001427 * @param d the source array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001428 */
Jason Samsb97b2512011-01-16 15:04:08 -08001429 public void copy1DRangeFrom(int off, int count, float[] d) {
1430 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001431 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001432 }
Jason Sams3042d262013-11-25 18:28:33 -08001433
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001434 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001435 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001436 *
1437 * @param off The offset of the first element to be copied.
1438 * @param count The number of elements to be copied.
1439 * @param data the source data allocation.
1440 * @param dataOff off The offset of the first element in data to
1441 * be copied.
1442 */
1443 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001444 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -07001445 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001446 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001447 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001448 data.mSelectedLOD, data.mSelectedFace.mID);
Chris Craik5c705d62015-06-01 10:39:36 -07001449 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001450 }
1451
Jason Samsfb9f82c2011-01-12 14:53:25 -08001452 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001453 if (mAdaptedAllocation != null) {
1454
1455 } else {
1456
1457 if (xoff < 0 || yoff < 0) {
1458 throw new RSIllegalArgumentException("Offset cannot be negative.");
1459 }
1460 if (h < 0 || w < 0) {
1461 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1462 }
1463 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1464 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1465 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001466 }
1467 }
Jason Sams768bc022009-09-21 19:41:04 -07001468
Jason Sams3042d262013-11-25 18:28:33 -08001469 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1470 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001471 try {
1472 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
1473 mRS.validate();
1474 validate2DRange(xoff, yoff, w, h);
1475 final int dataSize = mType.mElement.getBytesSize() * w * h;
1476 // AutoPadding for Vec3 Element
1477 boolean usePadding = false;
1478 int sizeBytes = arrayLen * dt.mSize;
1479 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1480 if (dataSize / 4 * 3 > sizeBytes) {
1481 throw new RSIllegalArgumentException("Array too small for allocation type.");
1482 }
1483 usePadding = true;
1484 sizeBytes = dataSize;
1485 } else {
1486 if (dataSize > sizeBytes) {
1487 throw new RSIllegalArgumentException("Array too small for allocation type.");
1488 }
Miao Wang87e908d2015-03-02 15:15:15 -08001489 }
Chris Craik06d29842015-06-02 17:19:24 -07001490 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1491 array, sizeBytes, dt,
1492 mType.mElement.mType.mSize, usePadding);
1493 } finally {
1494 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001495 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001496 }
1497
Jason Sams3042d262013-11-25 18:28:33 -08001498 /**
1499 * Copy from an array into a rectangular region in this Allocation. The
Miao Wang3231e8e2016-04-01 15:10:47 -07001500 * array is assumed to be tightly packed. This variant is type checked
1501 * and will generate exceptions if the Allocation's {@link
1502 * android.renderscript.Element} does not match the input data type.
1503 *
1504 * <p> The size of the region is: w * h * {@link #getElement}.{@link
1505 * Element#getBytesSize}.
1506 *
1507 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1508 * array in bytes must be at least the size of the region.
1509 *
1510 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1511 * is disabled, then the size of the array in bytes must be at least the size
1512 * of the region. The padding bytes for the cells must be part of the array.
1513 *
1514 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1515 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1516 * of the region. The padding bytes for the cells must not be part of the array.
Jason Sams3042d262013-11-25 18:28:33 -08001517 *
1518 * @param xoff X offset of the region to update in this Allocation
1519 * @param yoff Y offset of the region to update in this Allocation
1520 * @param w Width of the region to update
1521 * @param h Height of the region to update
Ying Wang16229812013-11-26 15:45:12 -08001522 * @param array Data to be placed into the Allocation
Jason Sams3042d262013-11-25 18:28:33 -08001523 */
1524 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001525 try {
1526 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1527 copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
1528 validateObjectIsPrimitiveArray(array, true),
1529 java.lang.reflect.Array.getLength(array));
1530 } finally {
1531 Trace.traceEnd(RenderScript.TRACE_TAG);
1532 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001533 }
1534
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001535 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001536 * Copy from an array into a rectangular region in this Allocation. The
Miao Wang3231e8e2016-04-01 15:10:47 -07001537 * array is assumed to be tightly packed. This variant is type checked
1538 * and will generate exceptions if the Allocation's {@link
1539 * android.renderscript.Element} is not an 8 bit integer nor a vector of 8 bit
1540 * integers {@link android.renderscript.Element.DataType}.
1541 *
1542 * <p> The size of the region is: w * h * {@link #getElement}.{@link
1543 * Element#getBytesSize}.
1544 *
1545 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1546 * array in bytes must be at least the size of the region.
1547 *
1548 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1549 * is disabled, then the size of the array in bytes must be at least the size
1550 * of the region. The padding bytes for the cells must be part of the array.
1551 *
1552 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1553 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1554 * of the region. The padding bytes for the cells must not be part of the array.
Jason Samsf7086092011-01-12 13:28:37 -08001555 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001556 * @param xoff X offset of the region to update in this Allocation
1557 * @param yoff Y offset of the region to update in this Allocation
1558 * @param w Width of the region to update
1559 * @param h Height of the region to update
1560 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001561 */
1562 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001563 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001564 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1565 Element.DataType.SIGNED_8, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001566 }
1567
Tim Murrayc11e25c2013-04-09 11:01:01 -07001568 /**
1569 * Copy from an array into a rectangular region in this Allocation. The
Miao Wang3231e8e2016-04-01 15:10:47 -07001570 * array is assumed to be tightly packed. This variant is type checked
1571 * and will generate exceptions if the Allocation's {@link
1572 * android.renderscript.Element} is not a 16 bit integer nor a vector of 16 bit
1573 * integers {@link android.renderscript.Element.DataType}.
1574 *
1575 * <p> The size of the region is: w * h * {@link #getElement}.{@link
1576 * Element#getBytesSize}.
1577 *
1578 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1579 * array in bytes must be at least the size of the region.
1580 *
1581 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1582 * is disabled, then the size of the array in bytes must be at least the size
1583 * of the region. The padding bytes for the cells must be part of the array.
1584 *
1585 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1586 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1587 * of the region. The padding bytes for the cells must not be part of the array.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001588 *
1589 * @param xoff X offset of the region to update in this Allocation
1590 * @param yoff Y offset of the region to update in this Allocation
1591 * @param w Width of the region to update
1592 * @param h Height of the region to update
1593 * @param data to be placed into the Allocation
1594 */
Jason Samsf7086092011-01-12 13:28:37 -08001595 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001596 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -08001597 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1598 Element.DataType.SIGNED_16, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001599 }
1600
Tim Murrayc11e25c2013-04-09 11:01:01 -07001601 /**
1602 * Copy from an array into a rectangular region in this Allocation. The
Miao Wang3231e8e2016-04-01 15:10:47 -07001603 * array is assumed to be tightly packed. This variant is type checked
1604 * and will generate exceptions if the Allocation's {@link
1605 * android.renderscript.Element} is not a 32 bit integer nor a vector of 32 bit
1606 * integers {@link android.renderscript.Element.DataType}.
1607 *
1608 * <p> The size of the region is: w * h * {@link #getElement}.{@link
1609 * Element#getBytesSize}.
1610 *
1611 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1612 * array in bytes must be at least the size of the region.
1613 *
1614 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1615 * is disabled, then the size of the array in bytes must be at least the size
1616 * of the region. The padding bytes for the cells must be part of the array.
1617 *
1618 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1619 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1620 * of the region. The padding bytes for the cells must not be part of the array.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001621 *
1622 * @param xoff X offset of the region to update in this Allocation
1623 * @param yoff Y offset of the region to update in this Allocation
1624 * @param w Width of the region to update
1625 * @param h Height of the region to update
1626 * @param data to be placed into the Allocation
1627 */
Jason Samsf7086092011-01-12 13:28:37 -08001628 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001629 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001630 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1631 Element.DataType.SIGNED_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001632 }
1633
Tim Murrayc11e25c2013-04-09 11:01:01 -07001634 /**
1635 * Copy from an array into a rectangular region in this Allocation. The
Miao Wang3231e8e2016-04-01 15:10:47 -07001636 * array is assumed to be tightly packed. This variant is type checked
1637 * and will generate exceptions if the Allocation's {@link
1638 * android.renderscript.Element} is neither a 32 bit float nor a vector of
1639 * 32 bit floats {@link android.renderscript.Element.DataType}.
1640 *
1641 * <p> The size of the region is: w * h * {@link #getElement}.{@link
1642 * Element#getBytesSize}.
1643 *
1644 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1645 * array in bytes must be at least the size of the region.
1646 *
1647 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1648 * is disabled, then the size of the array in bytes must be at least the size
1649 * of the region. The padding bytes for the cells must be part of the array.
1650 *
1651 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1652 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1653 * of the region. The padding bytes for the cells must not be part of the array.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001654 *
1655 * @param xoff X offset of the region to update in this Allocation
1656 * @param yoff Y offset of the region to update in this Allocation
1657 * @param w Width of the region to update
1658 * @param h Height of the region to update
1659 * @param data to be placed into the Allocation
1660 */
Jason Samsf7086092011-01-12 13:28:37 -08001661 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001662 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001663 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1664 Element.DataType.FLOAT_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001665 }
1666
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001667 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001668 * Copy a rectangular region from an Allocation into a rectangular region in
1669 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001670 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001671 * @param xoff X offset of the region in this Allocation
1672 * @param yoff Y offset of the region in this Allocation
1673 * @param w Width of the region to update.
1674 * @param h Height of the region to update.
1675 * @param data source Allocation.
1676 * @param dataXoff X offset in source Allocation
1677 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001678 */
1679 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1680 Allocation data, int dataXoff, int dataYoff) {
Chris Craik06d29842015-06-02 17:19:24 -07001681 try {
1682 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1683 mRS.validate();
1684 validate2DRange(xoff, yoff, w, h);
1685 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
1686 mSelectedLOD, mSelectedFace.mID,
1687 w, h, data.getID(mRS), dataXoff, dataYoff,
1688 data.mSelectedLOD, data.mSelectedFace.mID);
1689 } finally {
1690 Trace.traceEnd(RenderScript.TRACE_TAG);
1691 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001692 }
1693
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001694 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001695 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1696 * and width of the update will use the height and width of the {@link
1697 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001698 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001699 * @param xoff X offset of the region to update in this Allocation
1700 * @param yoff Y offset of the region to update in this Allocation
1701 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001702 */
1703 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Chris Craik5c705d62015-06-01 10:39:36 -07001704 try {
1705 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1706 mRS.validate();
1707 if (data.getConfig() == null) {
1708 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1709 Canvas c = new Canvas(newBitmap);
1710 c.drawBitmap(data, 0, 0, null);
1711 copy2DRangeFrom(xoff, yoff, newBitmap);
1712 return;
1713 }
1714 validateBitmapFormat(data);
1715 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
1716 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
1717 } finally {
1718 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -08001719 }
Jason Samsfa445b92011-01-07 17:00:07 -08001720 }
1721
Jason Samsb05d6892013-04-09 15:59:24 -07001722 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1723 if (mAdaptedAllocation != null) {
1724
1725 } else {
1726
1727 if (xoff < 0 || yoff < 0 || zoff < 0) {
1728 throw new RSIllegalArgumentException("Offset cannot be negative.");
1729 }
1730 if (h < 0 || w < 0 || d < 0) {
1731 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1732 }
1733 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1734 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1735 }
1736 }
1737 }
1738
1739 /**
Miao Wang258db502015-03-03 14:05:36 -08001740 * Copy a rectangular region from the array into the allocation.
1741 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001742 *
Miao Wang258db502015-03-03 14:05:36 -08001743 * The data type of the array is not required to be the same as
1744 * the element data type.
Jason Samsb05d6892013-04-09 15:59:24 -07001745 */
Jason Sams3042d262013-11-25 18:28:33 -08001746 private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1747 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001748 try {
1749 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
1750 mRS.validate();
1751 validate3DRange(xoff, yoff, zoff, w, h, d);
1752 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1753 // AutoPadding for Vec3 Element
1754 boolean usePadding = false;
1755 int sizeBytes = arrayLen * dt.mSize;
1756 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1757 if (dataSize / 4 * 3 > sizeBytes) {
1758 throw new RSIllegalArgumentException("Array too small for allocation type.");
1759 }
1760 usePadding = true;
1761 sizeBytes = dataSize;
1762 } else {
1763 if (dataSize > sizeBytes) {
1764 throw new RSIllegalArgumentException("Array too small for allocation type.");
1765 }
Miao Wang87e908d2015-03-02 15:15:15 -08001766 }
Chris Craik06d29842015-06-02 17:19:24 -07001767 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1768 array, sizeBytes, dt,
1769 mType.mElement.mType.mSize, usePadding);
1770 } finally {
1771 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001772 }
Jason Samsb05d6892013-04-09 15:59:24 -07001773 }
1774
1775 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001776 * Copy from an array into a 3D region in this Allocation. The
1777 * array is assumed to be tightly packed. This variant is type checked
1778 * and will generate exceptions if the Allocation's {@link
1779 * android.renderscript.Element} does not match the input data type.
1780 *
1781 * <p> The size of the region is: w * h * d * {@link #getElement}.{@link
1782 * Element#getBytesSize}.
1783 *
1784 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1785 * array in bytes must be at least the size of the region.
1786 *
1787 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1788 * is disabled, then the size of the array in bytes must be at least the size
1789 * of the region. The padding bytes for the cells must be part of the array.
1790 *
1791 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1792 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1793 * of the region. The padding bytes for the cells must not be part of the array.
Jason Samsb05d6892013-04-09 15:59:24 -07001794 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001795 * @param xoff X offset of the region to update in this Allocation
1796 * @param yoff Y offset of the region to update in this Allocation
1797 * @param zoff Z offset of the region to update in this Allocation
1798 * @param w Width of the region to update
1799 * @param h Height of the region to update
1800 * @param d Depth of the region to update
Miao Wang87e908d2015-03-02 15:15:15 -08001801 * @param array to be placed into the allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001802 */
Jason Sams3042d262013-11-25 18:28:33 -08001803 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001804 try {
1805 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
1806 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
1807 validateObjectIsPrimitiveArray(array, true),
1808 java.lang.reflect.Array.getLength(array));
1809 } finally {
1810 Trace.traceEnd(RenderScript.TRACE_TAG);
1811 }
Jason Samsb05d6892013-04-09 15:59:24 -07001812 }
1813
1814 /**
Jason Samsb05d6892013-04-09 15:59:24 -07001815 * Copy a rectangular region into the allocation from another
1816 * allocation.
1817 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001818 * @param xoff X offset of the region to update in this Allocation
1819 * @param yoff Y offset of the region to update in this Allocation
1820 * @param zoff Z offset of the region to update in this Allocation
1821 * @param w Width of the region to update.
1822 * @param h Height of the region to update.
1823 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001824 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001825 * @param dataXoff X offset of the region in the source Allocation
1826 * @param dataYoff Y offset of the region in the source Allocation
1827 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001828 */
1829 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1830 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1831 mRS.validate();
1832 validate3DRange(xoff, yoff, zoff, w, h, d);
1833 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1834 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1835 data.mSelectedLOD);
1836 }
1837
Jason Samsfa445b92011-01-07 17:00:07 -08001838
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001839 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001840 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1841 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001842 *
1843 * @param b The bitmap to be set from the Allocation.
1844 */
Jason Samsfa445b92011-01-07 17:00:07 -08001845 public void copyTo(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -07001846 try {
1847 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1848 mRS.validate();
1849 validateBitmapFormat(b);
1850 validateBitmapSize(b);
1851 mRS.nAllocationCopyToBitmap(getID(mRS), b);
1852 } finally {
1853 Trace.traceEnd(RenderScript.TRACE_TAG);
1854 }
Jason Samsfa445b92011-01-07 17:00:07 -08001855 }
1856
Jason Sams3042d262013-11-25 18:28:33 -08001857 private void copyTo(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001858 try {
1859 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1860 mRS.validate();
1861 boolean usePadding = false;
1862 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1863 usePadding = true;
Miao Wangd9b63282015-04-03 09:15:39 -07001864 }
Chris Craik06d29842015-06-02 17:19:24 -07001865 if (usePadding) {
1866 if (dt.mSize * arrayLen < mSize / 4 * 3) {
1867 throw new RSIllegalArgumentException(
1868 "Size of output array cannot be smaller than size of allocation.");
1869 }
1870 } else {
1871 if (dt.mSize * arrayLen < mSize) {
1872 throw new RSIllegalArgumentException(
1873 "Size of output array cannot be smaller than size of allocation.");
1874 }
Miao Wangd9b63282015-04-03 09:15:39 -07001875 }
Chris Craik06d29842015-06-02 17:19:24 -07001876 mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
1877 } finally {
1878 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wangd9b63282015-04-03 09:15:39 -07001879 }
Jason Sams3042d262013-11-25 18:28:33 -08001880 }
1881
1882 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001883 * Copy from the Allocation into an array. The method is type checked
1884 * and will generate exceptions if the Allocation's {@link
1885 * android.renderscript.Element} does not match the input data type.
1886 *
1887 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1888 * array in bytes must be at least the size of the Allocation {@link
1889 * #getBytesSize getBytesSize()}.
1890 *
1891 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1892 * is disabled, then the size of the array in bytes must be at least the size
1893 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1894 * the cells will be part of the array.
1895 *
1896 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1897 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1898 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1899 * the cells must not be part of the array.
Jason Sams3042d262013-11-25 18:28:33 -08001900 *
1901 * @param array The array to be set from the Allocation.
1902 */
1903 public void copyTo(Object array) {
1904 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1905 java.lang.reflect.Array.getLength(array));
1906 }
1907
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001908 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001909 * Copy from the Allocation into a byte array. This variant is type checked
1910 * and will generate exceptions if the Allocation's {@link
1911 * android.renderscript.Element} is neither an 8 bit integer nor a vector of 8 bit
1912 * integers {@link android.renderscript.Element.DataType}.
1913 *
1914 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1915 * array in bytes must be at least the size of the Allocation {@link
1916 * #getBytesSize getBytesSize()}.
1917 *
1918 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1919 * is disabled, then the size of the array in bytes must be at least the size
1920 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1921 * the cells will be part of the array.
1922 *
1923 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1924 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1925 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1926 * the cells must not be part of the array.
Jason Sams48fe5342011-07-08 13:52:30 -07001927 *
1928 * @param d The array to be set from the Allocation.
1929 */
Jason Samsfa445b92011-01-07 17:00:07 -08001930 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001931 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001932 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001933 }
1934
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001935 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001936 * Copy from the Allocation into a short array. This variant is type checked
1937 * and will generate exceptions if the Allocation's {@link
1938 * android.renderscript.Element} is not a 16 bit integer nor a vector of 16 bit
1939 * integers {@link android.renderscript.Element.DataType}.
1940 *
1941 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1942 * array in bytes must be at least the size of the Allocation {@link
1943 * #getBytesSize getBytesSize()}.
1944 *
1945 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1946 * is disabled, then the size of the array in bytes must be at least the size
1947 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1948 * the cells will be part of the array.
1949 *
1950 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1951 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1952 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1953 * the cells must not be part of the array.
Jason Sams48fe5342011-07-08 13:52:30 -07001954 *
1955 * @param d The array to be set from the Allocation.
1956 */
Jason Samsfa445b92011-01-07 17:00:07 -08001957 public void copyTo(short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001958 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -08001959 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001960 }
1961
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001962 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001963 * Copy from the Allocation into a int array. This variant is type checked
1964 * and will generate exceptions if the Allocation's {@link
1965 * android.renderscript.Element} is not a 32 bit integer nor a vector of 32 bit
1966 * integers {@link android.renderscript.Element.DataType}.
1967 *
1968 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1969 * array in bytes must be at least the size of the Allocation {@link
1970 * #getBytesSize getBytesSize()}.
1971 *
1972 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1973 * is disabled, then the size of the array in bytes must be at least the size
1974 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1975 * the cells will be part of the array.
1976 *
1977 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1978 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1979 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1980 * the cells must not be part of the array.
Jason Sams48fe5342011-07-08 13:52:30 -07001981 *
1982 * @param d The array to be set from the Allocation.
1983 */
Jason Samsfa445b92011-01-07 17:00:07 -08001984 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001985 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001986 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001987 }
1988
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001989 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001990 * Copy from the Allocation into a float array. This variant is type checked
1991 * and will generate exceptions if the Allocation's {@link
1992 * android.renderscript.Element} is neither a 32 bit float nor a vector of
1993 * 32 bit floats {@link android.renderscript.Element.DataType}.
1994 *
1995 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1996 * array in bytes must be at least the size of the Allocation {@link
1997 * #getBytesSize getBytesSize()}.
1998 *
1999 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2000 * is disabled, then the size of the array in bytes must be at least the size
2001 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
2002 * the cells will be part of the array.
2003 *
2004 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2005 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2006 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
2007 * the cells must not be part of the array.
Jason Sams48fe5342011-07-08 13:52:30 -07002008 *
2009 * @param d The array to be set from the Allocation.
2010 */
Jason Samsfa445b92011-01-07 17:00:07 -08002011 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08002012 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08002013 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07002014 }
2015
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002016 /**
Miao Wang3c613272015-05-11 11:41:55 -07002017 * @hide
2018 *
Miao Wang45cec0a2015-03-04 16:40:21 -08002019 * This is only intended to be used by auto-generated code reflected from
2020 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -08002021 *
2022 * @param xoff
2023 * @param yoff
2024 * @param zoff
2025 * @param component_number
Miao Wang258db502015-03-03 14:05:36 -08002026 * @param fp
Miao Wangc8e237e2015-02-20 18:36:32 -08002027 */
Miao Wang45cec0a2015-03-04 16:40:21 -08002028 public void copyToFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -08002029 mRS.validate();
2030 if (component_number >= mType.mElement.mElements.length) {
2031 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
2032 }
2033 if(xoff < 0) {
2034 throw new RSIllegalArgumentException("Offset x must be >= 0.");
2035 }
2036 if(yoff < 0) {
2037 throw new RSIllegalArgumentException("Offset y must be >= 0.");
2038 }
2039 if(zoff < 0) {
2040 throw new RSIllegalArgumentException("Offset z must be >= 0.");
2041 }
2042
Miao Wang45cec0a2015-03-04 16:40:21 -08002043 final byte[] data = fp.getData();
Miao Wangbfa5e652015-05-04 15:29:25 -07002044 int data_length = data.length;
Miao Wangc8e237e2015-02-20 18:36:32 -08002045 int eSize = mType.mElement.mElements[component_number].getBytesSize();
2046 eSize *= mType.mElement.mArraySizes[component_number];
2047
Miao Wang45cec0a2015-03-04 16:40:21 -08002048 if (data_length != eSize) {
2049 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
2050 " does not match component size " + eSize + ".");
Miao Wangc8e237e2015-02-20 18:36:32 -08002051 }
2052
2053 mRS.nAllocationElementRead(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Miao Wang45cec0a2015-03-04 16:40:21 -08002054 component_number, data, data_length);
Miao Wangc8e237e2015-02-20 18:36:32 -08002055 }
2056 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002057 * Resize a 1D allocation. The contents of the allocation are preserved.
2058 * If new elements are allocated objects are created with null contents and
2059 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08002060 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002061 * <p>If the new region is smaller the references of any objects outside the
2062 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08002063 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002064 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08002065 *
2066 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07002067 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002068 * @deprecated RenderScript objects should be immutable once created. The
Tim Murraycd38b762014-08-13 13:20:25 -07002069 * replacement is to create a new allocation and copy the contents. This
2070 * function will throw an exception if API 21 or higher is used.
Jason Samsf7086092011-01-12 13:28:37 -08002071 */
Jason Sams31a7e422010-10-26 13:09:17 -07002072 public synchronized void resize(int dimX) {
Tim Murraycd38b762014-08-13 13:20:25 -07002073 if (mRS.getApplicationContext().getApplicationInfo().targetSdkVersion >= 21) {
2074 throw new RSRuntimeException("Resize is not allowed in API 21+.");
2075 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -08002076 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08002077 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07002078 }
Jason Samse07694b2012-04-03 15:36:36 -07002079 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07002080 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07002081
Tim Murray460a0492013-11-19 12:45:54 -08002082 long typeID = mRS.nAllocationGetType(getID(mRS));
Yang Nie1798e42016-04-07 11:17:59 -07002083 // Sets zero the mID so that the finalizer of the old mType value won't
2084 // destroy the native object that is being reused.
2085 mType.setID(0);
Jason Sams31a7e422010-10-26 13:09:17 -07002086 mType = new Type(typeID, mRS);
2087 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07002088 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07002089 }
2090
Miao Wangc8e237e2015-02-20 18:36:32 -08002091 private void copy1DRangeToUnchecked(int off, int count, Object array,
2092 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07002093 try {
2094 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked");
2095 final int dataSize = mType.mElement.getBytesSize() * count;
2096 // AutoPadding for Vec3 Element
2097 boolean usePadding = false;
2098 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
2099 usePadding = true;
2100 }
2101 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
2102 mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
2103 mType.mElement.mType.mSize, usePadding);
2104 } finally {
2105 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08002106 }
Miao Wangc8e237e2015-02-20 18:36:32 -08002107 }
2108
2109 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002110 * Copy a 1D region of this Allocation into an array. This method does not
Miao Wangc8e237e2015-02-20 18:36:32 -08002111 * guarantee that the Allocation is compatible with the input buffer.
2112 *
Miao Wang3231e8e2016-04-01 15:10:47 -07002113 * <p> The size of the region is: count * {@link #getElement}.{@link
2114 * Element#getBytesSize}.
2115 *
2116 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2117 * array in bytes must be at least the size of the region.
2118 *
2119 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2120 * is disabled, then the size of the array in bytes must be at least the size
2121 * of the region. The padding bytes for the cells must be part of the array.
2122 *
2123 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2124 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2125 * of the region. The padding bytes for the cells must not be part of the array.
2126 *
Miao Wangc8e237e2015-02-20 18:36:32 -08002127 * @param off The offset of the first element to be copied.
2128 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002129 * @param array The dest array
Miao Wangc8e237e2015-02-20 18:36:32 -08002130 */
2131 public void copy1DRangeToUnchecked(int off, int count, Object array) {
2132 copy1DRangeToUnchecked(off, count, array,
2133 validateObjectIsPrimitiveArray(array, false),
2134 java.lang.reflect.Array.getLength(array));
2135 }
2136
2137 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002138 * Copy a 1D region of this Allocation into an array. This method does not
Miao Wangc8e237e2015-02-20 18:36:32 -08002139 * guarantee that the Allocation is compatible with the input buffer.
2140 *
Miao Wang3231e8e2016-04-01 15:10:47 -07002141 * <p> The size of the region is: count * {@link #getElement}.{@link
2142 * Element#getBytesSize}.
2143 *
2144 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2145 * array in bytes must be at least the size of the region.
2146 *
2147 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2148 * is disabled, then the size of the array in bytes must be at least the size
2149 * of the region. The padding bytes for the cells must be part of the array.
2150 *
2151 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2152 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2153 * of the region. The padding bytes for the cells must not be part of the array.
2154 *
Miao Wangc8e237e2015-02-20 18:36:32 -08002155 * @param off The offset of the first element to be copied.
2156 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002157 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002158 */
2159 public void copy1DRangeToUnchecked(int off, int count, int[] d) {
2160 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
2161 }
2162
2163 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002164 * Copy a 1D region of this Allocation into an array. This method does not
Miao Wangc8e237e2015-02-20 18:36:32 -08002165 * guarantee that the Allocation is compatible with the input buffer.
2166 *
Miao Wang3231e8e2016-04-01 15:10:47 -07002167 * <p> The size of the region is: count * {@link #getElement}.{@link
2168 * Element#getBytesSize}.
2169 *
2170 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2171 * array in bytes must be at least the size of the region.
2172 *
2173 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2174 * is disabled, then the size of the array in bytes must be at least the size
2175 * of the region. The padding bytes for the cells must be part of the array.
2176 *
2177 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2178 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2179 * of the region. The padding bytes for the cells must not be part of the array.
2180 *
Miao Wangc8e237e2015-02-20 18:36:32 -08002181 * @param off The offset of the first element to be copied.
2182 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002183 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002184 */
2185 public void copy1DRangeToUnchecked(int off, int count, short[] d) {
2186 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
2187 }
2188
2189 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002190 * Copy a 1D region of this Allocation into an array. This method does not
Miao Wangc8e237e2015-02-20 18:36:32 -08002191 * guarantee that the Allocation is compatible with the input buffer.
2192 *
Miao Wang3231e8e2016-04-01 15:10:47 -07002193 * <p> The size of the region is: count * {@link #getElement}.{@link
2194 * Element#getBytesSize}.
2195 *
2196 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2197 * array in bytes must be at least the size of the region.
2198 *
2199 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2200 * is disabled, then the size of the array in bytes must be at least the size
2201 * of the region. The padding bytes for the cells must be part of the array.
2202 *
2203 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2204 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2205 * of the region. The padding bytes for the cells must not be part of the array.
2206 *
Miao Wangc8e237e2015-02-20 18:36:32 -08002207 * @param off The offset of the first element to be copied.
2208 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002209 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002210 */
2211 public void copy1DRangeToUnchecked(int off, int count, byte[] d) {
2212 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
2213 }
2214
2215 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002216 * Copy a 1D region of this Allocation into an array. This method does not
Miao Wangc8e237e2015-02-20 18:36:32 -08002217 * guarantee that the Allocation is compatible with the input buffer.
2218 *
Miao Wang3231e8e2016-04-01 15:10:47 -07002219 * <p> The size of the region is: count * {@link #getElement}.{@link
2220 * Element#getBytesSize}.
2221 *
2222 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2223 * array in bytes must be at least the size of the region.
2224 *
2225 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2226 * is disabled, then the size of the array in bytes must be at least the size
2227 * of the region. The padding bytes for the cells must be part of the array.
2228 *
2229 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2230 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2231 * of the region. The padding bytes for the cells must not be part of the array.
2232 *
Miao Wangc8e237e2015-02-20 18:36:32 -08002233 * @param off The offset of the first element to be copied.
2234 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002235 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002236 */
2237 public void copy1DRangeToUnchecked(int off, int count, float[] d) {
2238 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
2239 }
2240
Miao Wangc8e237e2015-02-20 18:36:32 -08002241 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002242 * Copy a 1D region of this Allocation into an array. This method is type checked
2243 * and will generate exceptions if the Allocation's {@link
2244 * android.renderscript.Element} does not match the component type
2245 * of the array passed in.
2246 *
2247 * <p> The size of the region is: count * {@link #getElement}.{@link
2248 * Element#getBytesSize}.
2249 *
2250 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2251 * array in bytes must be at least the size of the region.
2252 *
2253 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2254 * is disabled, then the size of the array in bytes must be at least the size
2255 * of the region. The padding bytes for the cells must be part of the array.
2256 *
2257 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2258 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2259 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002260 *
2261 * @param off The offset of the first element to be copied.
2262 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002263 * @param array The source array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002264 */
2265 public void copy1DRangeTo(int off, int count, Object array) {
2266 copy1DRangeToUnchecked(off, count, array,
2267 validateObjectIsPrimitiveArray(array, true),
2268 java.lang.reflect.Array.getLength(array));
2269 }
2270
2271 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002272 * Copy a 1D region of this Allocation into an array. This variant is type checked
2273 * and will generate exceptions if the Allocation's {@link
2274 * android.renderscript.Element} is neither a 32 bit integer nor a vector of 32 bit
2275 * integers {@link android.renderscript.Element.DataType}.
2276 *
2277 * <p> The size of the region is: count * {@link #getElement}.{@link
2278 * Element#getBytesSize}.
2279 *
2280 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2281 * array in bytes must be at least the size of the region.
2282 *
2283 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2284 * is disabled, then the size of the array in bytes must be at least the size
2285 * of the region. The padding bytes for the cells must be part of the array.
2286 *
2287 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2288 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2289 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002290 *
2291 * @param off The offset of the first element to be copied.
2292 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002293 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002294 */
2295 public void copy1DRangeTo(int off, int count, int[] d) {
2296 validateIsInt32();
2297 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
2298 }
2299
2300 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002301 * Copy a 1D region of this Allocation into an array. This variant is type checked
2302 * and will generate exceptions if the Allocation's {@link
2303 * android.renderscript.Element} is neither a 16 bit integer nor a vector of 16 bit
2304 * integers {@link android.renderscript.Element.DataType}.
2305 *
2306 * <p> The size of the region is: count * {@link #getElement}.{@link
2307 * Element#getBytesSize}.
2308 *
2309 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2310 * array in bytes must be at least the size of the region.
2311 *
2312 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2313 * is disabled, then the size of the array in bytes must be at least the size
2314 * of the region. The padding bytes for the cells must be part of the array.
2315 *
2316 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2317 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2318 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002319 *
2320 * @param off The offset of the first element to be copied.
2321 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002322 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002323 */
2324 public void copy1DRangeTo(int off, int count, short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08002325 validateIsInt16OrFloat16();
Miao Wangc8e237e2015-02-20 18:36:32 -08002326 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
2327 }
2328
2329 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002330 * Copy a 1D region of this Allocation into an array. This variant is type checked
2331 * and will generate exceptions if the Allocation's {@link
2332 * android.renderscript.Element} is neither an 8 bit integer nor a vector of 8 bit
2333 * integers {@link android.renderscript.Element.DataType}.
2334 *
2335 * <p> The size of the region is: count * {@link #getElement}.{@link
2336 * Element#getBytesSize}.
2337 *
2338 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2339 * array in bytes must be at least the size of the region.
2340 *
2341 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2342 * is disabled, then the size of the array in bytes must be at least the size
2343 * of the region. The padding bytes for the cells must be part of the array.
2344 *
2345 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2346 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2347 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002348 *
2349 * @param off The offset of the first element to be copied.
2350 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002351 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002352 */
2353 public void copy1DRangeTo(int off, int count, byte[] d) {
2354 validateIsInt8();
2355 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
2356 }
2357
2358 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002359 * Copy a 1D region of this Allocation into an array. This variant is type checked
2360 * and will generate exceptions if the Allocation's {@link
2361 * android.renderscript.Element} is neither a 32 bit float nor a vector of
2362 * 32 bit floats {@link android.renderscript.Element.DataType}.
2363 *
2364 * <p> The size of the region is: count * {@link #getElement}.{@link
2365 * Element#getBytesSize}.
2366 *
2367 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2368 * array in bytes must be at least the size of the region.
2369 *
2370 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2371 * is disabled, then the size of the array in bytes must be at least the size
2372 * of the region. The padding bytes for the cells must be part of the array.
2373 *
2374 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2375 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2376 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002377 *
2378 * @param off The offset of the first element to be copied.
2379 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002380 * @param d the source array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002381 */
2382 public void copy1DRangeTo(int off, int count, float[] d) {
2383 validateIsFloat32();
2384 copy1DRangeToUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
2385 }
2386
2387
2388 void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
2389 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07002390 try {
2391 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked");
2392 mRS.validate();
2393 validate2DRange(xoff, yoff, w, h);
2394 final int dataSize = mType.mElement.getBytesSize() * w * h;
2395 // AutoPadding for Vec3 Element
2396 boolean usePadding = false;
2397 int sizeBytes = arrayLen * dt.mSize;
2398 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
2399 if (dataSize / 4 * 3 > sizeBytes) {
2400 throw new RSIllegalArgumentException("Array too small for allocation type.");
2401 }
2402 usePadding = true;
2403 sizeBytes = dataSize;
2404 } else {
2405 if (dataSize > sizeBytes) {
2406 throw new RSIllegalArgumentException("Array too small for allocation type.");
2407 }
Miao Wang87e908d2015-03-02 15:15:15 -08002408 }
Chris Craik06d29842015-06-02 17:19:24 -07002409 mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
2410 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
2411 } finally {
2412 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08002413 }
Miao Wangc8e237e2015-02-20 18:36:32 -08002414 }
2415
2416 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002417 * Copy from a rectangular region in this Allocation into an array. This
2418 * method is type checked and will generate exceptions if the Allocation's
2419 * {@link android.renderscript.Element} does not match the component type
2420 * of the array passed in.
2421 *
2422 * <p> The size of the region is: w * h * {@link #getElement}.{@link
2423 * Element#getBytesSize}.
2424 *
2425 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2426 * array in bytes must be at least the size of the region.
2427 *
2428 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2429 * is disabled, then the size of the array in bytes must be at least the size
2430 * of the region. The padding bytes for the cells must be part of the array.
2431 *
2432 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2433 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2434 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002435 *
2436 * @param xoff X offset of the region to copy in this Allocation
2437 * @param yoff Y offset of the region to copy in this Allocation
2438 * @param w Width of the region to copy
2439 * @param h Height of the region to copy
2440 * @param array Dest Array to be copied into
2441 */
2442 public void copy2DRangeTo(int xoff, int yoff, int w, int h, Object array) {
2443 copy2DRangeToUnchecked(xoff, yoff, w, h, array,
2444 validateObjectIsPrimitiveArray(array, true),
2445 java.lang.reflect.Array.getLength(array));
2446 }
2447
2448 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002449 * Copy from a rectangular region in this Allocation into an array. This
2450 * variant is type checked and will generate exceptions if the Allocation's
2451 * {@link android.renderscript.Element} is neither an 8 bit integer nor a vector
2452 * of 8 bit integers {@link android.renderscript.Element.DataType}.
2453 *
2454 * <p> The size of the region is: w * h * {@link #getElement}.{@link
2455 * Element#getBytesSize}.
2456 *
2457 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2458 * array in bytes must be at least the size of the region.
2459 *
2460 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2461 * is disabled, then the size of the array in bytes must be at least the size
2462 * of the region. The padding bytes for the cells must be part of the array.
2463 *
2464 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2465 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2466 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002467 *
2468 * @param xoff X offset of the region to copy in this Allocation
2469 * @param yoff Y offset of the region to copy in this Allocation
2470 * @param w Width of the region to copy
2471 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08002472 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08002473 */
2474 public void copy2DRangeTo(int xoff, int yoff, int w, int h, byte[] data) {
2475 validateIsInt8();
2476 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
2477 Element.DataType.SIGNED_8, data.length);
2478 }
2479
2480 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002481 * Copy from a rectangular region in this Allocation into an array. This
2482 * variant is type checked and will generate exceptions if the Allocation's
2483 * {@link android.renderscript.Element} is neither a 16 bit integer nor a vector
2484 * of 16 bit integers {@link android.renderscript.Element.DataType}.
2485 *
2486 * <p> The size of the region is: w * h * {@link #getElement}.{@link
2487 * Element#getBytesSize}.
2488 *
2489 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2490 * array in bytes must be at least the size of the region.
2491 *
2492 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2493 * is disabled, then the size of the array in bytes must be at least the size
2494 * of the region. The padding bytes for the cells must be part of the array.
2495 *
2496 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2497 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2498 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002499 *
2500 * @param xoff X offset of the region to copy in this Allocation
2501 * @param yoff Y offset of the region to copy in this Allocation
2502 * @param w Width of the region to copy
2503 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08002504 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08002505 */
2506 public void copy2DRangeTo(int xoff, int yoff, int w, int h, short[] data) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08002507 validateIsInt16OrFloat16();
Miao Wangc8e237e2015-02-20 18:36:32 -08002508 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
2509 Element.DataType.SIGNED_16, data.length);
2510 }
2511
2512 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002513 * Copy from a rectangular region in this Allocation into an array. This
2514 * variant is type checked and will generate exceptions if the Allocation's
2515 * {@link android.renderscript.Element} is neither a 32 bit integer nor a vector
2516 * of 32 bit integers {@link android.renderscript.Element.DataType}.
2517 *
2518 * <p> The size of the region is: w * h * {@link #getElement}.{@link
2519 * Element#getBytesSize}.
2520 *
2521 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2522 * array in bytes must be at least the size of the region.
2523 *
2524 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2525 * is disabled, then the size of the array in bytes must be at least the size
2526 * of the region. The padding bytes for the cells must be part of the array.
2527 *
2528 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2529 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2530 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002531 *
2532 * @param xoff X offset of the region to copy in this Allocation
2533 * @param yoff Y offset of the region to copy in this Allocation
2534 * @param w Width of the region to copy
2535 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08002536 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08002537 */
2538 public void copy2DRangeTo(int xoff, int yoff, int w, int h, int[] data) {
2539 validateIsInt32();
2540 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
2541 Element.DataType.SIGNED_32, data.length);
2542 }
2543
2544 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002545 * Copy from a rectangular region in this Allocation into an array. This
2546 * variant is type checked and will generate exceptions if the Allocation's
2547 * {@link android.renderscript.Element} is neither a 32 bit float nor a vector
2548 * of 32 bit floats {@link android.renderscript.Element.DataType}.
2549 *
2550 * <p> The size of the region is: w * h * {@link #getElement}.{@link
2551 * Element#getBytesSize}.
2552 *
2553 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2554 * array in bytes must be at least the size of the region.
2555 *
2556 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2557 * is disabled, then the size of the array in bytes must be at least the size
2558 * of the region. The padding bytes for the cells must be part of the array.
2559 *
2560 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2561 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2562 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002563 *
2564 * @param xoff X offset of the region to copy in this Allocation
2565 * @param yoff Y offset of the region to copy in this Allocation
2566 * @param w Width of the region to copy
2567 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08002568 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08002569 */
2570 public void copy2DRangeTo(int xoff, int yoff, int w, int h, float[] data) {
2571 validateIsFloat32();
2572 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
2573 Element.DataType.FLOAT_32, data.length);
2574 }
2575
2576
2577 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002578 * Copy from a 3D region in this Allocation into an array. This method does
2579 * not guarantee that the Allocation is compatible with the input buffer.
Miao Wang258db502015-03-03 14:05:36 -08002580 * The array is assumed to be tightly packed.
Miao Wangc8e237e2015-02-20 18:36:32 -08002581 *
Miao Wang258db502015-03-03 14:05:36 -08002582 * The data type of the array is not required to be the same as
2583 * the element data type.
Miao Wangc8e237e2015-02-20 18:36:32 -08002584 */
2585 private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
2586 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07002587 try {
2588 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked");
2589 mRS.validate();
2590 validate3DRange(xoff, yoff, zoff, w, h, d);
2591 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
2592 // AutoPadding for Vec3 Element
2593 boolean usePadding = false;
2594 int sizeBytes = arrayLen * dt.mSize;
2595 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
2596 if (dataSize / 4 * 3 > sizeBytes) {
2597 throw new RSIllegalArgumentException("Array too small for allocation type.");
2598 }
2599 usePadding = true;
2600 sizeBytes = dataSize;
2601 } else {
2602 if (dataSize > sizeBytes) {
2603 throw new RSIllegalArgumentException("Array too small for allocation type.");
2604 }
Miao Wang87e908d2015-03-02 15:15:15 -08002605 }
Chris Craik06d29842015-06-02 17:19:24 -07002606 mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
2607 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
2608 } finally {
2609 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08002610 }
Miao Wangc8e237e2015-02-20 18:36:32 -08002611 }
2612
Miao Wang258db502015-03-03 14:05:36 -08002613 /*
Miao Wang3231e8e2016-04-01 15:10:47 -07002614 * Copy from a 3D region in this Allocation into an array. This
2615 * method is type checked and will generate exceptions if the Allocation's
2616 * {@link android.renderscript.Element} does not match the component type
2617 * of the array passed in.
2618 *
2619 * <p> The size of the region is: w * h * d * {@link #getElement}.{@link
2620 * Element#getBytesSize}.
2621 *
2622 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2623 * array in bytes must be at least the size of the region.
2624 *
2625 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2626 * is disabled, then the size of the array in bytes must be at least the size
2627 * of the region. The padding bytes for the cells must be part of the array.
2628 *
2629 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2630 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2631 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002632 *
2633 * @param xoff X offset of the region to copy in this Allocation
2634 * @param yoff Y offset of the region to copy in this Allocation
2635 * @param zoff Z offset of the region to copy in this Allocation
2636 * @param w Width of the region to copy
2637 * @param h Height of the region to copy
2638 * @param d Depth of the region to copy
2639 * @param array Dest Array to be copied into
2640 */
2641 public void copy3DRangeTo(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
2642 copy3DRangeToUnchecked(xoff, yoff, zoff, w, h, d, array,
2643 validateObjectIsPrimitiveArray(array, true),
2644 java.lang.reflect.Array.getLength(array));
2645 }
Jason Samsb8c5a842009-07-31 20:40:47 -07002646
2647 // creation
2648
Jason Sams49a05d72010-12-29 14:31:29 -08002649 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07002650 static {
2651 mBitmapOptions.inScaled = false;
2652 }
2653
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002654 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002655 * Creates a new Allocation with the given {@link
2656 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002657 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002658 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002659 * @param mips specifies desired mipmap behaviour for the
2660 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07002661 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002662 * utilized
2663 */
2664 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07002665 try {
2666 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
2667 rs.validate();
2668 if (type.getID(rs) == 0) {
2669 throw new RSInvalidStateException("Bad Type");
2670 }
Yang Ni6484b6b2016-03-24 09:40:32 -07002671 // TODO: What if there is an exception after this? The native allocation would leak.
Chris Craik06d29842015-06-02 17:19:24 -07002672 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
2673 if (id == 0) {
2674 throw new RSRuntimeException("Allocation creation failed.");
2675 }
Yang Nie1798e42016-04-07 11:17:59 -07002676 return new Allocation(id, rs, type, false, usage, mips);
Chris Craik06d29842015-06-02 17:19:24 -07002677 } finally {
2678 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams1bada8c2009-08-09 17:01:55 -07002679 }
Jason Sams857d0c72011-11-23 15:02:15 -08002680 }
2681
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002682 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002683 * Creates an Allocation with the size specified by the type and no mipmaps
2684 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002685 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002686 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002687 * @param type renderscript type describing data layout
2688 * @param usage bit field specifying how the allocation is
2689 * utilized
2690 *
2691 * @return allocation
2692 */
Jason Samse5d37122010-12-16 00:33:33 -08002693 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
2694 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
2695 }
2696
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002697 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002698 * Creates an Allocation for use by scripts with a given {@link
2699 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002700 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002701 * @param rs Context to which the Allocation will belong.
2702 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002703 *
2704 * @return allocation
2705 */
Jason Sams5476b452010-12-08 16:14:36 -08002706 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08002707 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08002708 }
Jason Sams1bada8c2009-08-09 17:01:55 -07002709
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002710 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002711 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002712 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002713 * @param rs Context to which the Allocation will belong.
2714 * @param e Element to use in the Allocation
2715 * @param count the number of Elements in the Allocation
2716 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002717 * utilized
2718 *
2719 * @return allocation
2720 */
Jason Sams5476b452010-12-08 16:14:36 -08002721 static public Allocation createSized(RenderScript rs, Element e,
2722 int count, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07002723 try {
2724 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
2725 rs.validate();
2726 Type.Builder b = new Type.Builder(rs, e);
2727 b.setX(count);
2728 Type t = b.create();
Jason Sams768bc022009-09-21 19:41:04 -07002729
Chris Craik06d29842015-06-02 17:19:24 -07002730 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
2731 if (id == 0) {
2732 throw new RSRuntimeException("Allocation creation failed.");
2733 }
Yang Nie1798e42016-04-07 11:17:59 -07002734 return new Allocation(id, rs, t, true, usage, MipmapControl.MIPMAP_NONE);
Chris Craik06d29842015-06-02 17:19:24 -07002735 } finally {
2736 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07002737 }
Jason Sams5476b452010-12-08 16:14:36 -08002738 }
2739
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002740 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002741 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002742 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002743 * @param rs Context to which the Allocation will belong.
2744 * @param e Element to use in the Allocation
2745 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002746 *
2747 * @return allocation
2748 */
Jason Sams5476b452010-12-08 16:14:36 -08002749 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08002750 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07002751 }
2752
Jason Sams49a05d72010-12-29 14:31:29 -08002753 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08002754 final Bitmap.Config bc = b.getConfig();
2755 if (bc == Bitmap.Config.ALPHA_8) {
2756 return Element.A_8(rs);
2757 }
2758 if (bc == Bitmap.Config.ARGB_4444) {
2759 return Element.RGBA_4444(rs);
2760 }
2761 if (bc == Bitmap.Config.ARGB_8888) {
2762 return Element.RGBA_8888(rs);
2763 }
2764 if (bc == Bitmap.Config.RGB_565) {
2765 return Element.RGB_565(rs);
2766 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08002767 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08002768 }
2769
Jason Sams49a05d72010-12-29 14:31:29 -08002770 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002771 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08002772 Element e = elementFromBitmap(rs, b);
2773 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08002774 tb.setX(b.getWidth());
2775 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08002776 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08002777 return tb.create();
2778 }
2779
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002780 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002781 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002782 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002783 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07002784 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002785 * @param mips specifies desired mipmap behaviour for the
2786 * allocation
2787 * @param usage bit field specifying how the allocation is
2788 * utilized
2789 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002790 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002791 *
2792 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002793 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002794 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002795 int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07002796 try {
2797 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
2798 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08002799
Chris Craik06d29842015-06-02 17:19:24 -07002800 // WAR undocumented color formats
2801 if (b.getConfig() == null) {
2802 if ((usage & USAGE_SHARED) != 0) {
2803 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
2804 }
2805 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
2806 Canvas c = new Canvas(newBitmap);
2807 c.drawBitmap(b, 0, 0, null);
2808 return createFromBitmap(rs, newBitmap, mips, usage);
Tim Murrayabd5db92013-02-28 11:45:22 -08002809 }
Tim Murrayabd5db92013-02-28 11:45:22 -08002810
Chris Craik06d29842015-06-02 17:19:24 -07002811 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08002812
Chris Craik06d29842015-06-02 17:19:24 -07002813 // enable optimized bitmap path only with no mipmap and script-only usage
2814 if (mips == MipmapControl.MIPMAP_NONE &&
2815 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
2816 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
2817 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
2818 if (id == 0) {
2819 throw new RSRuntimeException("Load failed.");
2820 }
2821
2822 // keep a reference to the Bitmap around to prevent GC
Yang Nie1798e42016-04-07 11:17:59 -07002823 Allocation alloc = new Allocation(id, rs, t, true, usage, mips);
Chris Craik06d29842015-06-02 17:19:24 -07002824 alloc.setBitmap(b);
2825 return alloc;
2826 }
2827
2828
2829 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08002830 if (id == 0) {
2831 throw new RSRuntimeException("Load failed.");
2832 }
Yang Nie1798e42016-04-07 11:17:59 -07002833 return new Allocation(id, rs, t, true, usage, mips);
Chris Craik06d29842015-06-02 17:19:24 -07002834 } finally {
2835 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murraya3145512012-12-04 17:59:29 -08002836 }
Jason Sams5476b452010-12-08 16:14:36 -08002837 }
2838
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002839 /**
Miao Wang0facf022015-11-25 11:21:13 -08002840 * Gets or creates a ByteBuffer that contains the raw data of the current Allocation.
Miao Wang3231e8e2016-04-01 15:10:47 -07002841 * <p> If the Allocation is created with USAGE_IO_INPUT, the returned ByteBuffer
Miao Wang0facf022015-11-25 11:21:13 -08002842 * would contain the up-to-date data as READ ONLY.
2843 * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
2844 * the Allocation has certain alignment. The size of each row including padding,
2845 * called stride, can be queried using the {@link #getStride()} method.
2846 *
2847 * Note: Operating on the ByteBuffer of a destroyed Allocation will triger errors.
2848 *
2849 * @return ByteBuffer The ByteBuffer associated with raw data pointer of the Allocation.
2850 */
2851 public ByteBuffer getByteBuffer() {
2852 // Create a new ByteBuffer if it is not initialized or using IO_INPUT.
2853 if (mType.hasFaces()) {
2854 throw new RSInvalidStateException("Cubemap is not supported for getByteBuffer().");
2855 }
2856 if (mType.getYuv() == android.graphics.ImageFormat.NV21 ||
2857 mType.getYuv() == android.graphics.ImageFormat.YV12 ||
2858 mType.getYuv() == android.graphics.ImageFormat.YUV_420_888 ) {
2859 throw new RSInvalidStateException("YUV format is not supported for getByteBuffer().");
2860 }
2861 if (mByteBuffer == null || (mUsage & USAGE_IO_INPUT) != 0) {
2862 int xBytesSize = mType.getX() * mType.getElement().getBytesSize();
2863 long[] stride = new long[1];
2864 mByteBuffer = mRS.nAllocationGetByteBuffer(getID(mRS), stride, xBytesSize, mType.getY(), mType.getZ());
2865 mByteBufferStride = stride[0];
2866 }
2867 if ((mUsage & USAGE_IO_INPUT) != 0) {
2868 return mByteBuffer.asReadOnlyBuffer();
2869 }
2870 return mByteBuffer;
2871 }
2872
2873 /**
Miao Wang8c150922015-10-26 17:44:10 -07002874 * Creates a new Allocation Array with the given {@link
2875 * android.renderscript.Type}, and usage flags.
2876 * Note: If the input allocation is of usage: USAGE_IO_INPUT,
2877 * the created Allocation will be sharing the same BufferQueue.
2878 *
2879 * @param rs RenderScript context
2880 * @param t RenderScript type describing data layout
2881 * @param usage bit field specifying how the Allocation is
2882 * utilized
2883 * @param numAlloc Number of Allocations in the array.
2884 * @return Allocation[]
2885 */
2886 public static Allocation[] createAllocations(RenderScript rs, Type t, int usage, int numAlloc) {
2887 try {
2888 Trace.traceBegin(RenderScript.TRACE_TAG, "createAllocations");
2889 rs.validate();
2890 if (t.getID(rs) == 0) {
2891 throw new RSInvalidStateException("Bad Type");
2892 }
2893
2894 Allocation[] mAllocationArray = new Allocation[numAlloc];
2895 mAllocationArray[0] = createTyped(rs, t, usage);
2896 if ((usage & USAGE_IO_INPUT) != 0) {
2897 if (numAlloc > MAX_NUMBER_IO_INPUT_ALLOC) {
Yang Nic48a09c2017-04-28 08:49:01 -07002898 mAllocationArray[0].destroy();
Miao Wang8c150922015-10-26 17:44:10 -07002899 throw new RSIllegalArgumentException("Exceeds the max number of Allocations allowed: " +
2900 MAX_NUMBER_IO_INPUT_ALLOC);
2901 }
2902 mAllocationArray[0].setupBufferQueue(numAlloc);;
2903 }
2904
2905 for (int i=1; i<numAlloc; i++) {
Yang Nie1798e42016-04-07 11:17:59 -07002906 mAllocationArray[i] = createFromAllocation(rs, mAllocationArray[0]);
Miao Wang8c150922015-10-26 17:44:10 -07002907 }
2908 return mAllocationArray;
2909 } finally {
2910 Trace.traceEnd(RenderScript.TRACE_TAG);
2911 }
2912 }
2913
2914 /**
2915 * Creates a new Allocation with the given {@link
2916 * android.renderscript.Allocation}. The same data layout of
2917 * the input Allocation will be applied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002918 * <p> If the input allocation is of usage: USAGE_IO_INPUT, the created
Miao Wang8c150922015-10-26 17:44:10 -07002919 * Allocation will be sharing the same BufferQueue.
2920 *
2921 * @param rs Context to which the allocation will belong.
2922 * @param alloc RenderScript Allocation describing data layout.
2923 * @return Allocation sharing the same data structure.
2924 */
Yang Nie1798e42016-04-07 11:17:59 -07002925 static Allocation createFromAllocation(RenderScript rs, Allocation alloc) {
Miao Wang8c150922015-10-26 17:44:10 -07002926 try {
2927 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromAllcation");
2928 rs.validate();
2929 if (alloc.getID(rs) == 0) {
2930 throw new RSInvalidStateException("Bad input Allocation");
2931 }
2932
2933 Type type = alloc.getType();
2934 int usage = alloc.getUsage();
2935 MipmapControl mips = alloc.getMipmap();
2936 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
2937 if (id == 0) {
2938 throw new RSRuntimeException("Allocation creation failed.");
2939 }
Yang Nie1798e42016-04-07 11:17:59 -07002940 Allocation outAlloc = new Allocation(id, rs, type, false, usage, mips);
Miao Wang8c150922015-10-26 17:44:10 -07002941 if ((usage & USAGE_IO_INPUT) != 0) {
2942 outAlloc.shareBufferQueue(alloc);
2943 }
2944 return outAlloc;
2945 } finally {
2946 Trace.traceEnd(RenderScript.TRACE_TAG);
2947 }
2948 }
2949
2950 /**
2951 * Initialize BufferQueue with specified max number of buffers.
2952 */
2953 void setupBufferQueue(int numAlloc) {
2954 mRS.validate();
2955 if ((mUsage & USAGE_IO_INPUT) == 0) {
2956 throw new RSInvalidStateException("Allocation is not USAGE_IO_INPUT.");
2957 }
2958 mRS.nAllocationSetupBufferQueue(getID(mRS), numAlloc);
2959 }
2960
2961 /**
2962 * Share the BufferQueue with another {@link #USAGE_IO_INPUT} Allocation.
2963 *
2964 * @param alloc Allocation to associate with allocation
2965 */
2966 void shareBufferQueue(Allocation alloc) {
2967 mRS.validate();
2968 if ((mUsage & USAGE_IO_INPUT) == 0) {
2969 throw new RSInvalidStateException("Allocation is not USAGE_IO_INPUT.");
2970 }
2971 mGetSurfaceSurface = alloc.getSurface();
2972 mRS.nAllocationShareBufferQueue(getID(mRS), alloc.getID(mRS));
2973 }
2974
2975 /**
Miao Wang0facf022015-11-25 11:21:13 -08002976 * Gets the stride of the Allocation.
2977 * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
2978 * the Allocation has certain alignment. The size of each row including such
2979 * padding is called stride.
2980 *
2981 * @return the stride. For 1D Allocation, the stride will be the number of
2982 * bytes of this Allocation. For 2D and 3D Allocations, the stride
2983 * will be the stride in X dimension measuring in bytes.
2984 */
2985 public long getStride() {
2986 if (mByteBufferStride == -1) {
2987 getByteBuffer();
2988 }
2989 return mByteBufferStride;
2990 }
2991
2992 /**
Miao Wang8c150922015-10-26 17:44:10 -07002993 * Get the timestamp for the most recent buffer held by this Allocation.
2994 * The timestamp is guaranteed to be unique and monotonically increasing.
2995 * Default value: -1. The timestamp will be updated after each {@link
2996 * #ioReceive ioReceive()} call.
2997 *
2998 * It can be used to identify the images by comparing the unique timestamps
2999 * when used with {@link android.hardware.camera2} APIs.
3000 * Example steps:
3001 * 1. Save {@link android.hardware.camera2.TotalCaptureResult} when the
3002 * capture is completed.
3003 * 2. Get the timestamp after {@link #ioReceive ioReceive()} call.
3004 * 3. Comparing totalCaptureResult.get(CaptureResult.SENSOR_TIMESTAMP) with
3005 * alloc.getTimeStamp().
3006 * @return long Timestamp associated with the buffer held by the Allocation.
3007 */
3008 public long getTimeStamp() {
3009 return mTimeStamp;
3010 }
3011
3012 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003013 * Returns the handle to a raw buffer that is being managed by the screen
3014 * compositor. This operation is only valid for Allocations with {@link
3015 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07003016 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07003017 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07003018 *
3019 */
3020 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08003021 if ((mUsage & USAGE_IO_INPUT) == 0) {
3022 throw new RSInvalidStateException("Allocation is not a surface texture.");
3023 }
Jason Sams1e68bac2015-03-17 16:36:55 -07003024
3025 if (mGetSurfaceSurface == null) {
3026 mGetSurfaceSurface = mRS.nAllocationGetSurface(getID(mRS));
3027 }
3028
3029 return mGetSurfaceSurface;
Jason Samsfb9aa9f2012-03-28 15:30:07 -07003030 }
3031
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003032 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003033 * Associate a {@link android.view.Surface} with this Allocation. This
3034 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07003035 *
3036 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08003037 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07003038 public void setSurface(Surface sur) {
3039 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08003040 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
3041 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
3042 }
3043
Jason Samse07694b2012-04-03 15:36:36 -07003044 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08003045 }
3046
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003047 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003048 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08003049 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07003050 * <p>With target API version 18 or greater, this Allocation will be created
3051 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
3052 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
3053 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003054 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003055 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003056 * @param b bitmap source for the allocation data
3057 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07003058 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003059 *
3060 */
Jason Sams6d8eb262010-12-15 01:41:00 -08003061 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08003062 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
3063 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07003064 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08003065 }
Jason Sams6d8eb262010-12-15 01:41:00 -08003066 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
3067 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08003068 }
3069
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003070 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003071 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
3072 * containing the horizontal list of cube faces. Each face must be a square,
3073 * have the same size as all other faces, and have a width that is a power
3074 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003075 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003076 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07003077 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003078 * format: right, left, top, bottom, front, back
3079 * @param mips specifies desired mipmap behaviour for the cubemap
3080 * @param usage bit field specifying how the cubemap is utilized
3081 *
3082 * @return allocation containing cubemap data
3083 *
3084 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003085 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08003086 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08003087 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003088 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08003089
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003090 int height = b.getHeight();
3091 int width = b.getWidth();
3092
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08003093 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003094 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
3095 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08003096 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003097 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003098 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08003099 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003100 if (!isPow2) {
3101 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
3102 }
3103
3104 Element e = elementFromBitmap(rs, b);
3105 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08003106 tb.setX(height);
3107 tb.setY(height);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08003108 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08003109 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003110 Type t = tb.create();
3111
Tim Murray460a0492013-11-19 12:45:54 -08003112 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003113 if(id == 0) {
3114 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
3115 }
Yang Nie1798e42016-04-07 11:17:59 -07003116 return new Allocation(id, rs, t, true, usage, mips);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003117 }
3118
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003119 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003120 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
3121 * from a {@link android.graphics.Bitmap} containing the horizontal list of
3122 * cube faces. Each face must be a square, have the same size as all other
3123 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003124 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003125 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003126 * @param b bitmap with cubemap faces layed out in the following
3127 * format: right, left, top, bottom, front, back
3128 *
3129 * @return allocation containing cubemap data
3130 *
3131 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003132 static public Allocation createCubemapFromBitmap(RenderScript rs,
3133 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08003134 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08003135 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08003136 }
3137
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003138 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003139 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
3140 * objects containing the cube faces. Each face must be a square, have the
3141 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003142 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003143 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003144 * @param xpos cubemap face in the positive x direction
3145 * @param xneg cubemap face in the negative x direction
3146 * @param ypos cubemap face in the positive y direction
3147 * @param yneg cubemap face in the negative y direction
3148 * @param zpos cubemap face in the positive z direction
3149 * @param zneg cubemap face in the negative z direction
3150 * @param mips specifies desired mipmap behaviour for the cubemap
3151 * @param usage bit field specifying how the cubemap is utilized
3152 *
3153 * @return allocation containing cubemap data
3154 *
3155 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003156 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
3157 Bitmap xpos,
3158 Bitmap xneg,
3159 Bitmap ypos,
3160 Bitmap yneg,
3161 Bitmap zpos,
3162 Bitmap zneg,
3163 MipmapControl mips,
3164 int usage) {
3165 int height = xpos.getHeight();
3166 if (xpos.getWidth() != height ||
3167 xneg.getWidth() != height || xneg.getHeight() != height ||
3168 ypos.getWidth() != height || ypos.getHeight() != height ||
3169 yneg.getWidth() != height || yneg.getHeight() != height ||
3170 zpos.getWidth() != height || zpos.getHeight() != height ||
3171 zneg.getWidth() != height || zneg.getHeight() != height) {
3172 throw new RSIllegalArgumentException("Only square cube map faces supported");
3173 }
3174 boolean isPow2 = (height & (height - 1)) == 0;
3175 if (!isPow2) {
3176 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
3177 }
3178
3179 Element e = elementFromBitmap(rs, xpos);
3180 Type.Builder tb = new Type.Builder(rs, e);
3181 tb.setX(height);
3182 tb.setY(height);
3183 tb.setFaces(true);
3184 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
3185 Type t = tb.create();
3186 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
3187
3188 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07003189 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003190 adapter.copyFrom(xpos);
3191 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
3192 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07003193 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003194 adapter.copyFrom(ypos);
3195 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
3196 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07003197 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003198 adapter.copyFrom(zpos);
3199 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
3200 adapter.copyFrom(zneg);
3201
3202 return cubemap;
3203 }
3204
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003205 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003206 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
3207 * from 6 {@link android.graphics.Bitmap} objects containing the cube
3208 * faces. Each face must be a square, have the same size as all other faces,
3209 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003210 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003211 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003212 * @param xpos cubemap face in the positive x direction
3213 * @param xneg cubemap face in the negative x direction
3214 * @param ypos cubemap face in the positive y direction
3215 * @param yneg cubemap face in the negative y direction
3216 * @param zpos cubemap face in the positive z direction
3217 * @param zneg cubemap face in the negative z direction
3218 *
3219 * @return allocation containing cubemap data
3220 *
3221 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003222 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
3223 Bitmap xpos,
3224 Bitmap xneg,
3225 Bitmap ypos,
3226 Bitmap yneg,
3227 Bitmap zpos,
3228 Bitmap zneg) {
3229 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
3230 zpos, zneg, MipmapControl.MIPMAP_NONE,
3231 USAGE_GRAPHICS_TEXTURE);
3232 }
3233
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003234 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003235 * Creates an Allocation from the Bitmap referenced
3236 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003237 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003238 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003239 * @param res application resources
3240 * @param id resource id to load the data from
3241 * @param mips specifies desired mipmap behaviour for the
3242 * allocation
3243 * @param usage bit field specifying how the allocation is
3244 * utilized
3245 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07003246 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003247 *
3248 */
Jason Sams5476b452010-12-08 16:14:36 -08003249 static public Allocation createFromBitmapResource(RenderScript rs,
3250 Resources res,
3251 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08003252 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08003253 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07003254
Jason Sams771bebb2009-12-07 12:40:12 -08003255 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07003256 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
3257 throw new RSIllegalArgumentException("Unsupported usage specified.");
3258 }
Jason Sams5476b452010-12-08 16:14:36 -08003259 Bitmap b = BitmapFactory.decodeResource(res, id);
3260 Allocation alloc = createFromBitmap(rs, b, mips, usage);
3261 b.recycle();
3262 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07003263 }
3264
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003265 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003266 * Creates a non-mipmapped Allocation to use as a graphics texture from the
3267 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003268 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07003269 * <p>With target API version 18 or greater, this allocation will be created
3270 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
3271 * target API version 17 or lower, this allocation will be created with
3272 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08003273 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003274 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003275 * @param res application resources
3276 * @param id resource id to load the data from
3277 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07003278 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003279 *
3280 */
Jason Sams5476b452010-12-08 16:14:36 -08003281 static public Allocation createFromBitmapResource(RenderScript rs,
3282 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08003283 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08003284 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
3285 return createFromBitmapResource(rs, res, id,
3286 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07003287 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08003288 }
Jason Sams6d8eb262010-12-15 01:41:00 -08003289 return createFromBitmapResource(rs, res, id,
3290 MipmapControl.MIPMAP_NONE,
3291 USAGE_GRAPHICS_TEXTURE);
3292 }
3293
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003294 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003295 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003296 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003297 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003298 * @param str string to create the allocation from
3299 * @param usage bit field specifying how the allocaiton is
3300 * utilized
3301 *
3302 */
Jason Sams5476b452010-12-08 16:14:36 -08003303 static public Allocation createFromString(RenderScript rs,
3304 String str,
3305 int usage) {
3306 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07003307 byte[] allocArray = null;
3308 try {
3309 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08003310 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08003311 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07003312 return alloc;
3313 }
3314 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08003315 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07003316 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07003317 }
Jason Sams739c8262013-04-11 18:07:52 -07003318
3319 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003320 * Interface to handle notification when new buffers are available via
3321 * {@link #USAGE_IO_INPUT}. An application will receive one notification
3322 * when a buffer is available. Additional buffers will not trigger new
3323 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07003324 */
Jason Sams42ef2382013-08-29 13:30:59 -07003325 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07003326 public void onBufferAvailable(Allocation a);
3327 }
3328
3329 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003330 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07003331 *
Jason Sams42ef2382013-08-29 13:30:59 -07003332 * @param callback instance of the OnBufferAvailableListener
3333 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07003334 */
Jason Sams42ef2382013-08-29 13:30:59 -07003335 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07003336 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08003337 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07003338 mBufferNotifier = callback;
3339 }
3340 }
3341
Tim Murrayb730d862014-08-18 16:14:24 -07003342 static void sendBufferNotification(long id) {
Jason Sams739c8262013-04-11 18:07:52 -07003343 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08003344 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07003345
3346 if ((a != null) && (a.mBufferNotifier != null)) {
3347 a.mBufferNotifier.onBufferAvailable(a);
3348 }
3349 }
3350 }
3351
Miao Wangf0f6e802015-02-03 17:16:43 -08003352 /**
3353 * For USAGE_IO_OUTPUT, destroy() implies setSurface(null).
3354 *
3355 */
3356 @Override
3357 public void destroy() {
3358 if((mUsage & USAGE_IO_OUTPUT) != 0) {
3359 setSurface(null);
3360 }
Yang Nie1798e42016-04-07 11:17:59 -07003361
3362 if (mType != null && mOwningType) {
3363 mType.destroy();
Yang Nie1798e42016-04-07 11:17:59 -07003364 }
3365
Miao Wangf0f6e802015-02-03 17:16:43 -08003366 super.destroy();
3367 }
Yang Nie1798e42016-04-07 11:17:59 -07003368
Jason Samsb8c5a842009-07-31 20:40:47 -07003369}