blob: d420bd4bdb665fb2c3e1425c6a3736e8dfc521ee [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>
Xusong Wang8b4548c2021-01-05 10:09:52 -080055 *
56 * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
57 * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
58 * guide</a> for the proposed alternatives.
Jason Samsb8c5a842009-07-31 20:40:47 -070059 **/
Xusong Wang8b4548c2021-01-05 10:09:52 -080060@Deprecated
Jason Samsb8c5a842009-07-31 20:40:47 -070061public class Allocation extends BaseObj {
Miao Wang8c150922015-10-26 17:44:10 -070062 private static final int MAX_NUMBER_IO_INPUT_ALLOC = 16;
63
Jason Sams43ee06852009-08-12 17:54:11 -070064 Type mType;
Yang Nie1798e42016-04-07 11:17:59 -070065 boolean mOwningType = false;
Jason Sams8a647432010-03-01 15:31:04 -080066 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080067 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070068 Allocation mAdaptedAllocation;
Tim Murray2f2472c2013-08-22 14:55:26 -070069 int mSize;
Miao Wang8c150922015-10-26 17:44:10 -070070 MipmapControl mMipmapControl;
Jason Samsba862d12011-07-07 15:24:42 -070071
Miao Wang8c150922015-10-26 17:44:10 -070072 long mTimeStamp = -1;
Jason Sams615e7ce2012-01-13 14:01:20 -080073 boolean mReadAllowed = true;
74 boolean mWriteAllowed = true;
Miao Wang87e908d2015-03-02 15:15:15 -080075 boolean mAutoPadding = false;
Jason Sams46ba27e32015-02-06 17:45:15 -080076 int mSelectedX;
Jason Samsba862d12011-07-07 15:24:42 -070077 int mSelectedY;
78 int mSelectedZ;
79 int mSelectedLOD;
Jason Sams46ba27e32015-02-06 17:45:15 -080080 int mSelectedArray[];
Jason Samsba862d12011-07-07 15:24:42 -070081 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
82
83 int mCurrentDimX;
84 int mCurrentDimY;
85 int mCurrentDimZ;
86 int mCurrentCount;
Tim Murray460a0492013-11-19 12:45:54 -080087 static HashMap<Long, Allocation> mAllocationMap =
88 new HashMap<Long, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070089 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070090
Jason Sams1e68bac2015-03-17 16:36:55 -070091 private Surface mGetSurfaceSurface = null;
Miao Wang0facf022015-11-25 11:21:13 -080092 private ByteBuffer mByteBuffer = null;
93 private long mByteBufferStride = -1;
Jason Sams1e68bac2015-03-17 16:36:55 -070094
Jason Sams3042d262013-11-25 18:28:33 -080095 private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
96 final Class c = d.getClass();
97 if (!c.isArray()) {
98 throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
99 }
100 final Class cmp = c.getComponentType();
101 if (!cmp.isPrimitive()) {
102 throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
103 }
104
105 if (cmp == Long.TYPE) {
106 if (checkType) {
107 validateIsInt64();
108 return mType.mElement.mType;
109 }
110 return Element.DataType.SIGNED_64;
111 }
112
113 if (cmp == Integer.TYPE) {
114 if (checkType) {
115 validateIsInt32();
116 return mType.mElement.mType;
117 }
118 return Element.DataType.SIGNED_32;
119 }
120
121 if (cmp == Short.TYPE) {
122 if (checkType) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800123 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -0800124 return mType.mElement.mType;
125 }
126 return Element.DataType.SIGNED_16;
127 }
128
129 if (cmp == Byte.TYPE) {
130 if (checkType) {
131 validateIsInt8();
132 return mType.mElement.mType;
133 }
134 return Element.DataType.SIGNED_8;
135 }
136
137 if (cmp == Float.TYPE) {
138 if (checkType) {
139 validateIsFloat32();
140 }
141 return Element.DataType.FLOAT_32;
142 }
143
144 if (cmp == Double.TYPE) {
145 if (checkType) {
146 validateIsFloat64();
147 }
148 return Element.DataType.FLOAT_64;
149 }
Pirama Arumuga Nainar3934dad2016-03-28 12:00:00 -0700150
151 throw new RSIllegalArgumentException("Parameter of type " + cmp.getSimpleName() +
152 "[] is not compatible with data type " + mType.mElement.mType.name() +
153 " of allocation");
Jason Sams3042d262013-11-25 18:28:33 -0800154 }
155
156
Tim Murrayc11e25c2013-04-09 11:01:01 -0700157 /**
158 * The usage of the Allocation. These signal to RenderScript where to place
159 * the Allocation in memory.
160 *
161 */
Jason Sams5476b452010-12-08 16:14:36 -0800162
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700163 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700164 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -0800165 */
Jason Sams5476b452010-12-08 16:14:36 -0800166 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800167
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700168 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700169 * The Allocation will be used as a texture source by one or more graphics
170 * programs.
Jason Samsf7086092011-01-12 13:28:37 -0800171 *
172 */
Jason Sams5476b452010-12-08 16:14:36 -0800173 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800174
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700175 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700176 * The Allocation will be used as a graphics mesh.
177 *
178 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800179 *
180 */
Jason Sams5476b452010-12-08 16:14:36 -0800181 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800182
183
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700184 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700185 * The Allocation will be used as the source of shader constants by one or
186 * more programs.
187 *
188 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800189 *
190 */
Jason Sams5476b452010-12-08 16:14:36 -0800191 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
192
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700193 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700194 * The Allocation will be used as a target for offscreen rendering
195 *
196 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700197 *
198 */
199 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
200
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700201 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700202 * The Allocation will be used as a {@link android.view.Surface}
203 * consumer. This usage will cause the Allocation to be created
204 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800205 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800206 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700207 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800208
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700209 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700210 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700211 * producer. The dimensions and format of the {@link
Jason Sams3a1b8e42013-09-24 15:18:52 -0700212 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700213 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800214 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800215 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700216 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700217
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700218 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700219 * The Allocation's backing store will be inherited from another object
220 * (usually a {@link android.graphics.Bitmap}); copying to or from the
221 * original source Bitmap will cause a synchronization rather than a full
222 * copy. {@link #syncAll} may also be used to synchronize the Allocation
223 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800224 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700225 * <p>This is set by default for allocations created with {@link
226 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800227 *
228 */
229 public static final int USAGE_SHARED = 0x0080;
230
231 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700232 * Controls mipmap behavior when using the bitmap creation and update
233 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800234 */
Jason Sams4ef66502010-12-10 16:03:15 -0800235 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700236 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700237 * No mipmaps will be generated and the type generated from the incoming
238 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800239 */
Jason Sams5476b452010-12-08 16:14:36 -0800240 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800241
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700242 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700243 * A full mipmap chain will be created in script memory. The Type of
244 * the Allocation will contain a full mipmap chain. On upload, the full
245 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800246 */
Jason Sams5476b452010-12-08 16:14:36 -0800247 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800248
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700249 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700250 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
251 * not contain mipmaps. On upload, the allocation data will contain a
252 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800253 */
Jason Sams5476b452010-12-08 16:14:36 -0800254 MIPMAP_ON_SYNC_TO_TEXTURE(2);
255
256 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800257 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800258 mID = id;
259 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700260 }
261
Jason Sams48fe5342011-07-08 13:52:30 -0700262
Tim Murray460a0492013-11-19 12:45:54 -0800263 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700264 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700265 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700266 }
Jason Samse07694b2012-04-03 15:36:36 -0700267 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700268 }
269
Jason Sams03d2d002012-03-23 13:51:56 -0700270
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700271 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700272 * Get the {@link android.renderscript.Element} of the {@link
273 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700274 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700275 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700276 *
277 */
278 public Element getElement() {
279 return mType.getElement();
280 }
281
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700282 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700283 * Get the usage flags of the Allocation.
284 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700285 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700286 *
287 */
288 public int getUsage() {
289 return mUsage;
290 }
291
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700292 /**
Miao Wang8c150922015-10-26 17:44:10 -0700293 * @hide
294 * Get the Mipmap control flag of the Allocation.
295 *
296 * @return the Mipmap control flag of the Allocation
297 *
298 */
299 public MipmapControl getMipmap() {
300 return mMipmapControl;
301 }
302
303 /**
Miao Wang3231e8e2016-04-01 15:10:47 -0700304 * Specifies the mapping between the Allocation's cells and an array's elements
305 * when data is copied from the Allocation to the array, or vice-versa.
Miao Wang9ee76072016-03-29 15:56:55 -0700306 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700307 * Only applies to an Allocation whose Element is a vector of length 3 (such as
308 * {@link Element#U8_3} or {@link Element#RGB_888}). Enabling this feature may make
309 * copying data from the Allocation to an array or vice-versa less efficient.
310 *
311 * <p> Vec3 Element cells are stored in an Allocation as Vec4 Element cells with
312 * the same {@link android.renderscript.Element.DataType}, with the fourth vector
313 * component treated as padding. When this feature is enabled, only the data components,
314 * i.e. the first 3 vector components of each cell, will be mapped between the array
315 * and the Allocation. When disabled, explicit mapping of the padding components
316 * is required, as described in the following example.
317 *
318 * <p> For example, when copying an integer array to an Allocation of two {@link
319 * Element#I32_3} cells using {@link #copyFrom(int[])}:
320 * <p> When disabled:
321 * The array must have at least 8 integers, with the first 4 integers copied
322 * to the first cell of the Allocation, and the next 4 integers copied to
323 * the second cell. The 4th and 8th integers are mapped as the padding components.
324 *
325 * <p> When enabled:
326 * The array just needs to have at least 6 integers, with the first 3 integers
327 * copied to the the first cell as data components, and the next 3 copied to
328 * the second cell. There is no mapping for the padding components.
329 *
330 * <p> Similarly, when copying a byte array to an Allocation of two {@link
331 * Element#I32_3} cells, using {@link #copyFromUnchecked(int[])}:
332 * <p> When disabled:
333 * The array must have at least 32 bytes, with the first 16 bytes copied
334 * to the first cell of the Allocation, and the next 16 bytes copied to
335 * the second cell. The 13th-16th and 29th-32nd bytes are mapped as padding
336 * components.
337 *
338 * <p> When enabled:
339 * The array just needs to have at least 24 bytes, with the first 12 bytes copied
340 * to the first cell of the Allocation, and the next 12 bytes copied to
341 * the second cell. There is no mapping for the padding components.
342 *
343 * <p> Similar to copying data to an Allocation from an array, when copying data from an
344 * Allocation to an array, the padding components for Vec3 Element cells will not be
345 * copied/mapped to the array if AutoPadding is enabled.
346 *
347 * <p> Default: Disabled.
Miao Wang87e908d2015-03-02 15:15:15 -0800348 *
Miao Wang179e8b52015-04-15 17:44:32 -0700349 * @param useAutoPadding True: enable AutoPadding; False: disable AutoPadding
Miao Wang87e908d2015-03-02 15:15:15 -0800350 *
351 */
352 public void setAutoPadding(boolean useAutoPadding) {
353 mAutoPadding = useAutoPadding;
354 }
355
356 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700357 * Get the size of the Allocation in bytes.
358 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700359 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700360 *
361 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700362 public int getBytesSize() {
Tim Murray04f0d6e2013-12-17 17:15:25 -0800363 if (mType.mDimYuv != 0) {
364 return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
365 }
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700366 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700367 }
368
Jason Sams452a7662011-07-07 16:05:18 -0700369 private void updateCacheInfo(Type t) {
370 mCurrentDimX = t.getX();
371 mCurrentDimY = t.getY();
372 mCurrentDimZ = t.getZ();
373 mCurrentCount = mCurrentDimX;
374 if (mCurrentDimY > 1) {
375 mCurrentCount *= mCurrentDimY;
376 }
377 if (mCurrentDimZ > 1) {
378 mCurrentCount *= mCurrentDimZ;
379 }
380 }
Jason Samsba862d12011-07-07 15:24:42 -0700381
Tim Murraya3145512012-12-04 17:59:29 -0800382 private void setBitmap(Bitmap b) {
383 mBitmap = b;
384 }
385
Tim Murray460a0492013-11-19 12:45:54 -0800386 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700387 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800388 if ((usage & ~(USAGE_SCRIPT |
389 USAGE_GRAPHICS_TEXTURE |
390 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700391 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800392 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800393 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800394 USAGE_IO_OUTPUT |
395 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800396 throw new RSIllegalArgumentException("Unknown usage specified.");
397 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800398
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700399 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800400 mWriteAllowed = false;
401
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700402 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800403 USAGE_GRAPHICS_TEXTURE |
404 USAGE_SCRIPT)) != 0) {
405 throw new RSIllegalArgumentException("Invalid usage combination.");
406 }
407 }
Jason Sams9bf18922013-04-13 19:48:36 -0700408
Jason Sams5476b452010-12-08 16:14:36 -0800409 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800410 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700411
Jason Sams452a7662011-07-07 16:05:18 -0700412 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700413 // TODO: A3D doesn't have Type info during creation, so we can't
414 // calculate the size ahead of time. We can possibly add a method
415 // to update the size in the future if it seems reasonable.
416 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700417 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700418 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700419 try {
420 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
421 } catch (Exception e) {
422 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
423 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
424 }
Yang Ni6484b6b2016-03-24 09:40:32 -0700425 guard.open("destroy");
Tim Murray2f2472c2013-08-22 14:55:26 -0700426 }
427
Yang Nie1798e42016-04-07 11:17:59 -0700428 Allocation(long id, RenderScript rs, Type t, boolean owningType, int usage, MipmapControl mips) {
Miao Wang8c150922015-10-26 17:44:10 -0700429 this(id, rs, t, usage);
Yang Nie1798e42016-04-07 11:17:59 -0700430 mOwningType = owningType;
Miao Wang8c150922015-10-26 17:44:10 -0700431 mMipmapControl = mips;
432 }
433
Tim Murray2f2472c2013-08-22 14:55:26 -0700434 protected void finalize() throws Throwable {
435 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
436 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700437 }
438
Jason Sams3042d262013-11-25 18:28:33 -0800439 private void validateIsInt64() {
440 if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
441 (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
442 return;
443 }
444 throw new RSIllegalArgumentException(
445 "64 bit integer source does not match allocation type " + mType.mElement.mType);
446 }
447
Jason Samsb97b2512011-01-16 15:04:08 -0800448 private void validateIsInt32() {
449 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
450 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
451 return;
452 }
453 throw new RSIllegalArgumentException(
454 "32 bit integer source does not match allocation type " + mType.mElement.mType);
455 }
456
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800457 private void validateIsInt16OrFloat16() {
Jason Samsb97b2512011-01-16 15:04:08 -0800458 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800459 (mType.mElement.mType == Element.DataType.UNSIGNED_16) ||
460 (mType.mElement.mType == Element.DataType.FLOAT_16)) {
Jason Samsb97b2512011-01-16 15:04:08 -0800461 return;
462 }
463 throw new RSIllegalArgumentException(
464 "16 bit integer source does not match allocation type " + mType.mElement.mType);
465 }
466
467 private void validateIsInt8() {
468 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
469 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
470 return;
471 }
472 throw new RSIllegalArgumentException(
473 "8 bit integer source does not match allocation type " + mType.mElement.mType);
474 }
475
476 private void validateIsFloat32() {
477 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
478 return;
479 }
480 throw new RSIllegalArgumentException(
481 "32 bit float source does not match allocation type " + mType.mElement.mType);
482 }
483
Jason Sams3042d262013-11-25 18:28:33 -0800484 private void validateIsFloat64() {
485 if (mType.mElement.mType == Element.DataType.FLOAT_64) {
486 return;
487 }
488 throw new RSIllegalArgumentException(
489 "64 bit float source does not match allocation type " + mType.mElement.mType);
490 }
491
Jason Samsb97b2512011-01-16 15:04:08 -0800492 private void validateIsObject() {
493 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
494 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
495 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
496 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
497 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
498 (mType.mElement.mType == Element.DataType.RS_MESH) ||
499 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
500 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
501 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
502 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
503 return;
504 }
505 throw new RSIllegalArgumentException(
506 "Object source does not match allocation type " + mType.mElement.mType);
507 }
508
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700509 @Override
510 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800511 super.updateFromNative();
Tim Murray460a0492013-11-19 12:45:54 -0800512 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700513 if(typeID != 0) {
514 mType = new Type(typeID, mRS);
515 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700516 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700517 }
518 }
519
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700520 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700521 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700522 *
523 * @return Type
524 *
525 */
Jason Samsea87e962010-01-12 12:12:28 -0800526 public Type getType() {
527 return mType;
528 }
529
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700530 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700531 * Propagate changes from one usage of the Allocation to the
532 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700533 *
534 */
Jason Sams5476b452010-12-08 16:14:36 -0800535 public void syncAll(int srcLocation) {
Chris Craik06d29842015-06-02 17:19:24 -0700536 try {
537 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
538 switch (srcLocation) {
539 case USAGE_GRAPHICS_TEXTURE:
540 case USAGE_SCRIPT:
541 if ((mUsage & USAGE_SHARED) != 0) {
542 copyFrom(mBitmap);
543 }
544 break;
545 case USAGE_GRAPHICS_CONSTANTS:
546 case USAGE_GRAPHICS_VERTEX:
547 break;
548 case USAGE_SHARED:
549 if ((mUsage & USAGE_SHARED) != 0) {
550 copyTo(mBitmap);
551 }
552 break;
553 default:
554 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
Tim Murray78e64942013-04-09 17:28:56 -0700555 }
Chris Craik06d29842015-06-02 17:19:24 -0700556 mRS.validate();
557 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
558 } finally {
559 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800560 }
Jason Sams5476b452010-12-08 16:14:36 -0800561 }
562
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700563 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700564 * Send a buffer to the output stream. The contents of the Allocation will
565 * be undefined after this operation. This operation is only valid if {@link
566 * #USAGE_IO_OUTPUT} is set on the Allocation.
567 *
Jason Sams163766c2012-02-15 12:04:24 -0800568 *
Jason Sams163766c2012-02-15 12:04:24 -0800569 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700570 public void ioSend() {
Chris Craik06d29842015-06-02 17:19:24 -0700571 try {
572 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
573 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
574 throw new RSIllegalArgumentException(
575 "Can only send buffer if IO_OUTPUT usage specified.");
576 }
577 mRS.validate();
578 mRS.nAllocationIoSend(getID(mRS));
579 } finally {
580 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800581 }
Jason Sams163766c2012-02-15 12:04:24 -0800582 }
583
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700584 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700585 * Receive the latest input into the Allocation. This operation
586 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800587 *
Jason Sams163766c2012-02-15 12:04:24 -0800588 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700589 public void ioReceive() {
Chris Craik06d29842015-06-02 17:19:24 -0700590 try {
591 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
592 if ((mUsage & USAGE_IO_INPUT) == 0) {
593 throw new RSIllegalArgumentException(
594 "Can only receive if IO_INPUT usage specified.");
595 }
596 mRS.validate();
Miao Wang8c150922015-10-26 17:44:10 -0700597 mTimeStamp = mRS.nAllocationIoReceive(getID(mRS));
Chris Craik06d29842015-06-02 17:19:24 -0700598 } finally {
599 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800600 }
Jason Sams163766c2012-02-15 12:04:24 -0800601 }
602
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700603 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700604 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700605 *
606 * @param d Source array.
607 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800608 public void copyFrom(BaseObj[] d) {
Chris Craik06d29842015-06-02 17:19:24 -0700609 try {
610 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
611 mRS.validate();
612 validateIsObject();
613 if (d.length != mCurrentCount) {
614 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
615 mCurrentCount + ", array length = " + d.length);
616 }
Tim Murray460a0492013-11-19 12:45:54 -0800617
Chris Craik06d29842015-06-02 17:19:24 -0700618 if (RenderScript.sPointerSize == 8) {
619 long i[] = new long[d.length * 4];
620 for (int ct=0; ct < d.length; ct++) {
621 i[ct * 4] = d[ct].getID(mRS);
622 }
623 copy1DRangeFromUnchecked(0, mCurrentCount, i);
624 } else {
625 int i[] = new int[d.length];
626 for (int ct=0; ct < d.length; ct++) {
627 i[ct] = (int) d[ct].getID(mRS);
628 }
629 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray3de3dc72014-07-01 16:56:18 -0700630 }
Chris Craik06d29842015-06-02 17:19:24 -0700631 } finally {
632 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800633 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700634 }
635
Jason Samsfb9f82c2011-01-12 14:53:25 -0800636 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800637 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800638 if (bc == null) {
639 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
640 }
Jason Sams252c0782011-01-11 17:42:52 -0800641 switch (bc) {
642 case ALPHA_8:
643 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
644 throw new RSIllegalArgumentException("Allocation kind is " +
645 mType.getElement().mKind + ", type " +
646 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700647 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800648 " bytes, passed bitmap was " + bc);
649 }
650 break;
651 case ARGB_8888:
652 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700653 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800654 throw new RSIllegalArgumentException("Allocation kind is " +
655 mType.getElement().mKind + ", type " +
656 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700657 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800658 " bytes, passed bitmap was " + bc);
659 }
660 break;
661 case RGB_565:
662 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700663 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800664 throw new RSIllegalArgumentException("Allocation kind is " +
665 mType.getElement().mKind + ", type " +
666 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700667 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800668 " bytes, passed bitmap was " + bc);
669 }
670 break;
671 case ARGB_4444:
672 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700673 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800674 throw new RSIllegalArgumentException("Allocation kind is " +
675 mType.getElement().mKind + ", type " +
676 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700677 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800678 " bytes, passed bitmap was " + bc);
679 }
680 break;
681
682 }
Jason Sams4ef66502010-12-10 16:03:15 -0800683 }
684
Jason Samsfb9f82c2011-01-12 14:53:25 -0800685 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700686 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800687 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
688 }
689 }
690
Jason Sams3042d262013-11-25 18:28:33 -0800691 private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -0700692 try {
693 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
694 mRS.validate();
695 if (mCurrentDimZ > 0) {
696 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
697 } else if (mCurrentDimY > 0) {
698 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
699 } else {
700 copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
701 }
702 } finally {
703 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams3042d262013-11-25 18:28:33 -0800704 }
Jason Sams3042d262013-11-25 18:28:33 -0800705 }
706
Miao Wang3231e8e2016-04-01 15:10:47 -0700707
Jason Sams3042d262013-11-25 18:28:33 -0800708 /**
709 * Copy into this Allocation from an array. This method does not guarantee
710 * that the Allocation is compatible with the input buffer; it copies memory
711 * without reinterpretation.
712 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700713 * <p> If the Allocation does not have Vec3 Elements, then the size of the
714 * array in bytes must be at least the size of the Allocation {@link
715 * #getBytesSize getBytesSize()}.
716 *
717 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
718 * is disabled, then the size of the array in bytes must be at least the size
719 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
720 * the cells must be part of the array.
721 *
722 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
723 * is enabled, then the size of the array in bytes must be at least 3/4 the size
724 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
725 * the cells must not be part of the array.
726 *
727 * @param array The source array
Jason Sams3042d262013-11-25 18:28:33 -0800728 */
729 public void copyFromUnchecked(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700730 try {
731 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
732 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
733 java.lang.reflect.Array.getLength(array));
734 } finally {
735 Trace.traceEnd(RenderScript.TRACE_TAG);
736 }
Jason Sams3042d262013-11-25 18:28:33 -0800737 }
738
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700739 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700740 * Copy into this Allocation from an array. This method does not guarantee
741 * that the Allocation is compatible with the input buffer; it copies memory
742 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800743 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700744 * <p> If the Allocation does not have Vec3 Elements, then the size of the
745 * array in bytes must be at least the size of the Allocation {@link
746 * #getBytesSize getBytesSize()}.
747 *
748 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
749 * is disabled, then the size of the array in bytes must be at least the size
750 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
751 * the cells must be part of the array.
752 *
753 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
754 * is enabled, then the size of the array in bytes must be at least 3/4 the size
755 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
756 * the cells must not be part of the array.
757 *
758 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800759 */
760 public void copyFromUnchecked(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800761 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800762 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700763
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700764 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700765 * Copy into this Allocation from an array. This method does not guarantee
766 * that the Allocation is compatible with the input buffer; it copies memory
767 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800768 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700769 * <p> If the Allocation does not have Vec3 Elements, then the size of the
770 * array in bytes must be at least the size of the Allocation {@link
771 * #getBytesSize getBytesSize()}.
772 *
773 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
774 * is disabled, then the size of the array in bytes must be at least the size
775 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
776 * the cells must be part of the array.
777 *
778 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
779 * is enabled, then the size of the array in bytes must be at least 3/4 the size
780 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
781 * the cells must not be part of the array.
782 *
783 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800784 */
785 public void copyFromUnchecked(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800786 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800787 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700788
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700789 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700790 * Copy into this Allocation from an array. This method does not guarantee
791 * that the Allocation is compatible with the input buffer; it copies memory
792 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800793 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700794 * <p> If the Allocation does not have Vec3 Elements, then the size of the
795 * array in bytes must be at least the size of the Allocation {@link
796 * #getBytesSize getBytesSize()}.
797 *
798 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
799 * is disabled, then the size of the array in bytes must be at least the size
800 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
801 * the cells must be part of the array.
802 *
803 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
804 * is enabled, then the size of the array in bytes must be at least 3/4 the size
805 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
806 * the cells must not be part of the array.
807 *
808 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800809 */
810 public void copyFromUnchecked(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800811 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800812 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700813
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700814 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700815 * Copy into this Allocation from an array. This method does not guarantee
816 * that the Allocation is compatible with the input buffer; it copies memory
817 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800818 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700819 * <p> If the Allocation does not have Vec3 Elements, then the size of the
820 * array in bytes must be at least the size of the Allocation {@link
821 * #getBytesSize getBytesSize()}.
822 *
823 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
824 * is disabled, then the size of the array in bytes must be at least the size
825 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
826 * the cells must be part of the array.
827 *
828 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
829 * is enabled, then the size of the array in bytes must be at least 3/4 the size
830 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
831 * the cells must not be part of the array.
832 *
833 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800834 */
835 public void copyFromUnchecked(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800836 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800837 }
838
Tim Murray6d7a53c2013-05-23 16:59:23 -0700839
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700840 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700841 * Copy into this Allocation from an array. This variant is type checked
842 * and will generate exceptions if the Allocation's {@link
Jason Sams3042d262013-11-25 18:28:33 -0800843 * android.renderscript.Element} does not match the array's
844 * primitive type.
845 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700846 * <p> If the Allocation does not have Vec3 Elements, then the size of the
847 * array in bytes must be at least the size of the Allocation {@link
848 * #getBytesSize getBytesSize()}.
849 *
850 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
851 * is disabled, then the size of the array in bytes must be at least the size
852 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
853 * the cells must be part of the array.
854 *
855 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
856 * is enabled, then the size of the array in bytes must be at least 3/4 the size
857 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
858 * the cells must not be part of the array.
859 *
860 * @param array The source array
Jason Sams3042d262013-11-25 18:28:33 -0800861 */
862 public void copyFrom(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700863 try {
864 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
865 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
866 java.lang.reflect.Array.getLength(array));
867 } finally {
868 Trace.traceEnd(RenderScript.TRACE_TAG);
869 }
Jason Sams3042d262013-11-25 18:28:33 -0800870 }
871
872 /**
873 * Copy into this Allocation from an array. This variant is type checked
874 * and will generate exceptions if the Allocation's {@link
Miao Wang3231e8e2016-04-01 15:10:47 -0700875 * android.renderscript.Element} is not a 32 bit integer nor a vector of 32 bit
876 * integers {@link android.renderscript.Element.DataType}.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800877 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700878 * <p> If the Allocation does not have Vec3 Elements, then the size of the
879 * array in bytes must be at least the size of the Allocation {@link
880 * #getBytesSize getBytesSize()}.
881 *
882 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
883 * is disabled, then the size of the array in bytes must be at least the size
884 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
885 * the cells must be part of the array.
886 *
887 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
888 * is enabled, then the size of the array in bytes must be at least 3/4 the size
889 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
890 * the cells must not be part of the array.
891 *
892 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800893 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800894 public void copyFrom(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800895 validateIsInt32();
896 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800897 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800898
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700899 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700900 * Copy into this Allocation from an array. This variant is type checked
901 * and will generate exceptions if the Allocation's {@link
Miao Wang3231e8e2016-04-01 15:10:47 -0700902 * android.renderscript.Element} is not a 16 bit integer nor a vector of 16 bit
903 * integers {@link android.renderscript.Element.DataType}.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800904 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700905 * <p> If the Allocation does not have Vec3 Elements, then the size of the
906 * array in bytes must be at least the size of the Allocation {@link
907 * #getBytesSize getBytesSize()}.
908 *
909 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
910 * is disabled, then the size of the array in bytes must be at least the size
911 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
912 * the cells must be part of the array.
913 *
914 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
915 * is enabled, then the size of the array in bytes must be at least 3/4 the size
916 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
917 * the cells must not be part of the array.
918 *
919 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800920 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800921 public void copyFrom(short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800922 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -0800923 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800924 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800925
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700926 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700927 * Copy into this Allocation from an array. This variant is type checked
928 * and will generate exceptions if the Allocation's {@link
Miao Wang3231e8e2016-04-01 15:10:47 -0700929 * android.renderscript.Element} is not an 8 bit integer nor a vector of 8 bit
930 * integers {@link android.renderscript.Element.DataType}.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800931 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700932 * <p> If the Allocation does not have Vec3 Elements, then the size of the
933 * array in bytes must be at least the size of the Allocation {@link
934 * #getBytesSize getBytesSize()}.
935 *
936 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
937 * is disabled, then the size of the array in bytes must be at least the size
938 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
939 * the cells must be part of the array.
940 *
941 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
942 * is enabled, then the size of the array in bytes must be at least 3/4 the size
943 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
944 * the cells must not be part of the array.
945 *
946 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800947 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800948 public void copyFrom(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800949 validateIsInt8();
950 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800951 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800952
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700953 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700954 * Copy into this Allocation from an array. This variant is type checked
955 * and will generate exceptions if the Allocation's {@link
Miao Wang3231e8e2016-04-01 15:10:47 -0700956 * android.renderscript.Element} is neither a 32 bit float nor a vector of
957 * 32 bit floats {@link android.renderscript.Element.DataType}.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800958 *
Miao Wang3231e8e2016-04-01 15:10:47 -0700959 * <p> If the Allocation does not have Vec3 Elements, then the size of the
960 * array in bytes must be at least the size of the Allocation {@link
961 * #getBytesSize getBytesSize()}.
962 *
963 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
964 * is disabled, then the size of the array in bytes must be at least the size
965 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
966 * the cells must be part of the array.
967 *
968 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
969 * is enabled, then the size of the array in bytes must be at least 3/4 the size
970 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
971 * the cells must not be part of the array.
972 *
973 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -0800974 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800975 public void copyFrom(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800976 validateIsFloat32();
977 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800978 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800979
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700980 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700981 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
982 * height, width, and format of the bitmap must match the existing
983 * allocation.
984 *
985 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
986 * android.graphics.Bitmap} used to create the Allocation with {@link
987 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
988 * this will synchronize the Allocation with the latest data from the {@link
989 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800990 *
991 * @param b the source bitmap
992 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800993 public void copyFrom(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -0700994 try {
995 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
996 mRS.validate();
997 if (b.getConfig() == null) {
998 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
999 Canvas c = new Canvas(newBitmap);
1000 c.drawBitmap(b, 0, 0, null);
1001 copyFrom(newBitmap);
1002 return;
1003 }
1004 validateBitmapSize(b);
1005 validateBitmapFormat(b);
1006 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
1007 } finally {
1008 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -08001009 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001010 }
1011
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001012 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001013 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -07001014 * must be identical.
1015 *
1016 * @param a the source allocation
1017 */
1018 public void copyFrom(Allocation a) {
Chris Craik06d29842015-06-02 17:19:24 -07001019 try {
1020 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
1021 mRS.validate();
1022 if (!mType.equals(a.getType())) {
1023 throw new RSIllegalArgumentException("Types of allocations must match.");
1024 }
1025 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
1026 } finally {
1027 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -07001028 }
Tim Murrayf671fb02012-10-03 13:50:05 -07001029 }
1030
Tim Murrayf671fb02012-10-03 13:50:05 -07001031 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001032 * This is only intended to be used by auto-generated code reflected from
1033 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -08001034 *
1035 * @param xoff
1036 * @param fp
1037 */
Jason Sams21b41032011-01-16 15:05:41 -08001038 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -08001039 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001040 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -07001041 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -07001042 int data_length = fp.getPos();
Jason Samsa70f4162010-03-26 15:33:42 -07001043
Stephen Hinesfa1275a2014-06-17 17:25:04 -07001044 int count = data_length / eSize;
1045 if ((eSize * count) != data_length) {
1046 throw new RSIllegalArgumentException("Field packer length " + data_length +
Jason Samsa70f4162010-03-26 15:33:42 -07001047 " not divisible by element size " + eSize + ".");
1048 }
Jason Samsba862d12011-07-07 15:24:42 -07001049 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -07001050 }
1051
Miao Wang45cec0a2015-03-04 16:40:21 -08001052
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001053 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001054 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -08001055 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -08001056 *
1057 * @param xoff
1058 * @param component_number
1059 * @param fp
1060 */
Jason Sams21b41032011-01-16 15:05:41 -08001061 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001062 setFromFieldPacker(xoff, 0, 0, component_number, fp);
1063 }
1064
1065 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001066 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -08001067 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -08001068 *
1069 * @param xoff
1070 * @param yoff
Miao Wangc8e237e2015-02-20 18:36:32 -08001071 * @param zoff
1072 * @param component_number
1073 * @param fp
1074 */
1075 public void setFromFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -08001076 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -07001077 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -08001078 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -07001079 }
1080 if(xoff < 0) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001081 throw new RSIllegalArgumentException("Offset x must be >= 0.");
1082 }
1083 if(yoff < 0) {
1084 throw new RSIllegalArgumentException("Offset y must be >= 0.");
1085 }
1086 if(zoff < 0) {
1087 throw new RSIllegalArgumentException("Offset z must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -07001088 }
1089
1090 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -07001091 int data_length = fp.getPos();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001092 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -08001093 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -07001094
Stephen Hinesfa1275a2014-06-17 17:25:04 -07001095 if (data_length != eSize) {
1096 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
Jason Sams49bdaf02010-08-31 13:50:42 -07001097 " does not match component size " + eSize + ".");
1098 }
1099
Miao Wangc8e237e2015-02-20 18:36:32 -08001100 mRS.nAllocationElementData(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1101 component_number, data, data_length);
Jason Samsa70f4162010-03-26 15:33:42 -07001102 }
1103
Miao Wang87e908d2015-03-02 15:15:15 -08001104 private void data1DChecks(int off, int count, int len, int dataSize, boolean usePadding) {
Jason Sams771bebb2009-12-07 12:40:12 -08001105 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -07001106 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001107 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -07001108 }
1109 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -08001110 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -07001111 }
Jason Samsba862d12011-07-07 15:24:42 -07001112 if((off + count) > mCurrentCount) {
1113 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -07001114 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -07001115 }
Miao Wang87e908d2015-03-02 15:15:15 -08001116 if(usePadding) {
1117 if(len < dataSize / 4 * 3) {
1118 throw new RSIllegalArgumentException("Array too small for allocation type.");
1119 }
1120 } else {
1121 if(len < dataSize) {
1122 throw new RSIllegalArgumentException("Array too small for allocation type.");
1123 }
Jason Sams768bc022009-09-21 19:41:04 -07001124 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001125 }
1126
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001127 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001128 * Generate a mipmap chain. This is only valid if the Type of the Allocation
1129 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -08001130 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001131 * <p>This function will generate a complete set of mipmaps from the top
1132 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001133 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001134 * <p>If the Allocation is also using other memory spaces, a call to {@link
1135 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001136 */
1137 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -07001138 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -08001139 }
1140
Jason Sams3042d262013-11-25 18:28:33 -08001141 private void copy1DRangeFromUnchecked(int off, int count, Object array,
1142 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001143 try {
1144 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
1145 final int dataSize = mType.mElement.getBytesSize() * count;
1146 // AutoPadding for Vec3 Element
1147 boolean usePadding = false;
1148 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1149 usePadding = true;
1150 }
1151 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
1152 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
1153 mType.mElement.mType.mSize, usePadding);
1154 } finally {
1155 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001156 }
Jason Sams3042d262013-11-25 18:28:33 -08001157 }
1158
Miao Wang3231e8e2016-04-01 15:10:47 -07001159
Jason Sams3042d262013-11-25 18:28:33 -08001160 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001161 * Copy an array into a 1D region of this Allocation. This method does not
Jason Sams3042d262013-11-25 18:28:33 -08001162 * guarantee that the Allocation is compatible with the input buffer.
1163 *
Miao Wang3231e8e2016-04-01 15:10:47 -07001164 * <p> The size of the region is: count * {@link #getElement}.{@link
1165 * Element#getBytesSize}.
1166 *
1167 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1168 * array in bytes must be at least the size of the region.
1169 *
1170 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1171 * is disabled, then the size of the array in bytes must be at least the size
1172 * of the region. The padding bytes for the cells must be part of the array.
1173 *
1174 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1175 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1176 * of the region. The padding bytes for the cells must not be part of the array.
1177 *
Jason Sams3042d262013-11-25 18:28:33 -08001178 * @param off The offset of the first element to be copied.
1179 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001180 * @param array The source array
Jason Sams3042d262013-11-25 18:28:33 -08001181 */
1182 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
1183 copy1DRangeFromUnchecked(off, count, array,
1184 validateObjectIsPrimitiveArray(array, false),
1185 java.lang.reflect.Array.getLength(array));
1186 }
1187
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001188 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001189 * Copy an array into a 1D region of this Allocation. This method does not
Tim Murrayc11e25c2013-04-09 11:01:01 -07001190 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001191 *
Miao Wang3231e8e2016-04-01 15:10:47 -07001192 * <p> The size of the region is: count * {@link #getElement}.{@link
1193 * Element#getBytesSize}.
1194 *
1195 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1196 * array in bytes must be at least the size of the region.
1197 *
1198 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1199 * is disabled, then the size of the array in bytes must be at least the size
1200 * of the region. The padding bytes for the cells must be part of the array.
1201 *
1202 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1203 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1204 * of the region. The padding bytes for the cells must not be part of the array.
1205 *
Jason Sams4fa3eed2011-01-19 15:44:38 -08001206 * @param off The offset of the first element to be copied.
1207 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001208 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001209 */
1210 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001211 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -07001212 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001213
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001214 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001215 * Copy an array into a 1D region of this Allocation. This method does not
Tim Murrayc11e25c2013-04-09 11:01:01 -07001216 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001217 *
Miao Wang3231e8e2016-04-01 15:10:47 -07001218 * <p> The size of the region is: count * {@link #getElement}.{@link
1219 * Element#getBytesSize}.
1220 *
1221 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1222 * array in bytes must be at least the size of the region.
1223 *
1224 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1225 * is disabled, then the size of the array in bytes must be at least the size
1226 * of the region. The padding bytes for the cells must be part of the array.
1227 *
1228 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1229 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1230 * of the region. The padding bytes for the cells must not be part of the array.
1231 *
Jason Sams4fa3eed2011-01-19 15:44:38 -08001232 * @param off The offset of the first element to be copied.
1233 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001234 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001235 */
1236 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001237 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -07001238 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001239
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001240 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001241 * Copy an array into a 1D region of this Allocation. This method does not
Tim Murrayc11e25c2013-04-09 11:01:01 -07001242 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001243 *
Miao Wang3231e8e2016-04-01 15:10:47 -07001244 * <p> The size of the region is: count * {@link #getElement}.{@link
1245 * Element#getBytesSize}.
1246 *
1247 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1248 * array in bytes must be at least the size of the region.
1249 *
1250 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1251 * is disabled, then the size of the array in bytes must be at least the size
1252 * of the region. The padding bytes for the cells must be part of the array.
1253 *
1254 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1255 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1256 * of the region. The padding bytes for the cells must not be part of the array.
1257 *
Jason Sams4fa3eed2011-01-19 15:44:38 -08001258 * @param off The offset of the first element to be copied.
1259 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001260 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001261 */
1262 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001263 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -07001264 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001265
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001266 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001267 * Copy an array into a 1D region of this Allocation. This method does not
Tim Murrayc11e25c2013-04-09 11:01:01 -07001268 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001269 *
Miao Wang3231e8e2016-04-01 15:10:47 -07001270 * <p> The size of the region is: count * {@link #getElement}.{@link
1271 * Element#getBytesSize}.
1272 *
1273 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1274 * array in bytes must be at least the size of the region.
1275 *
1276 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1277 * is disabled, then the size of the array in bytes must be at least the size
1278 * of the region. The padding bytes for the cells must be part of the array.
1279 *
1280 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1281 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1282 * of the region. The padding bytes for the cells must not be part of the array.
1283 *
Jason Sams4fa3eed2011-01-19 15:44:38 -08001284 * @param off The offset of the first element to be copied.
1285 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001286 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001287 */
1288 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001289 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
1290 }
1291
Jason Sams3042d262013-11-25 18:28:33 -08001292 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001293 * Copy an array into a 1D region of this Allocation. This variant is type checked
1294 * and will generate exceptions if the Allocation's {@link
1295 * android.renderscript.Element} does not match the component type
1296 * of the array passed in.
1297 *
1298 * <p> The size of the region is: count * {@link #getElement}.{@link
1299 * Element#getBytesSize}.
1300 *
1301 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1302 * array in bytes must be at least the size of the region.
1303 *
1304 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1305 * is disabled, then the size of the array in bytes must be at least the size
1306 * of the region. The padding bytes for the cells must be part of the array.
1307 *
1308 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1309 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1310 * of the region. The padding bytes for the cells must not be part of the array.
Jason Sams3042d262013-11-25 18:28:33 -08001311 *
1312 * @param off The offset of the first element to be copied.
1313 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001314 * @param array The source array.
Jason Sams3042d262013-11-25 18:28:33 -08001315 */
1316 public void copy1DRangeFrom(int off, int count, Object array) {
1317 copy1DRangeFromUnchecked(off, count, array,
1318 validateObjectIsPrimitiveArray(array, true),
1319 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -07001320 }
1321
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001322 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001323 * Copy an array into a 1D region of this Allocation. This variant is type checked
1324 * and will generate exceptions if the Allocation's {@link
1325 * android.renderscript.Element} is not an 32 bit integer nor a vector of 32 bit
1326 * integers {@link android.renderscript.Element.DataType}.
1327 *
1328 * <p> The size of the region is: count * {@link #getElement}.{@link
1329 * Element#getBytesSize}.
1330 *
1331 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1332 * array in bytes must be at least the size of the region.
1333 *
1334 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1335 * is disabled, then the size of the array in bytes must be at least the size
1336 * of the region. The padding bytes for the cells must be part of the array.
1337 *
1338 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1339 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1340 * of the region. The padding bytes for the cells must not be part of the array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001341 *
1342 * @param off The offset of the first element to be copied.
1343 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001344 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001345 */
Jason Samsb97b2512011-01-16 15:04:08 -08001346 public void copy1DRangeFrom(int off, int count, int[] d) {
1347 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001348 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001349 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001350
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001351 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001352 * Copy an array into a 1D region of this Allocation. This variant is type checked
1353 * and will generate exceptions if the Allocation's {@link
1354 * android.renderscript.Element} is not an 16 bit integer nor a vector of 16 bit
1355 * integers {@link android.renderscript.Element.DataType}.
1356 *
1357 * <p> The size of the region is: count * {@link #getElement}.{@link
1358 * Element#getBytesSize}.
1359 *
1360 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1361 * array in bytes must be at least the size of the region.
1362 *
1363 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1364 * is disabled, then the size of the array in bytes must be at least the size
1365 * of the region. The padding bytes for the cells must be part of the array.
1366 *
1367 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1368 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1369 * of the region. The padding bytes for the cells must not be part of the array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001370 *
1371 * @param off The offset of the first element to be copied.
1372 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001373 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001374 */
Jason Samsb97b2512011-01-16 15:04:08 -08001375 public void copy1DRangeFrom(int off, int count, short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001376 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -08001377 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001378 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001379
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001380 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001381 * Copy an array into a 1D region of this Allocation. This variant is type checked
1382 * and will generate exceptions if the Allocation's {@link
1383 * android.renderscript.Element} is not an 8 bit integer nor a vector of 8 bit
1384 * integers {@link android.renderscript.Element.DataType}.
1385 *
1386 * <p> The size of the region is: count * {@link #getElement}.{@link
1387 * Element#getBytesSize}.
1388 *
1389 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1390 * array in bytes must be at least the size of the region.
1391 *
1392 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1393 * is disabled, then the size of the array in bytes must be at least the size
1394 * of the region. The padding bytes for the cells must be part of the array.
1395 *
1396 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1397 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1398 * of the region. The padding bytes for the cells must not be part of the array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001399 *
1400 * @param off The offset of the first element to be copied.
1401 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001402 * @param d the source array
Jason Sams4fa3eed2011-01-19 15:44:38 -08001403 */
Jason Samsb97b2512011-01-16 15:04:08 -08001404 public void copy1DRangeFrom(int off, int count, byte[] d) {
1405 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001406 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001407 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001408
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001409 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001410 * Copy an array into a 1D region of this Allocation. This variant is type checked
1411 * and will generate exceptions if the Allocation's {@link
1412 * android.renderscript.Element} is neither a 32 bit float nor a vector of
1413 * 32 bit floats {@link android.renderscript.Element.DataType}.
1414 *
1415 * <p> The size of the region is: count * {@link #getElement}.{@link
1416 * Element#getBytesSize}.
1417 *
1418 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1419 * array in bytes must be at least the size of the region.
1420 *
1421 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1422 * is disabled, then the size of the array in bytes must be at least the size
1423 * of the region. The padding bytes for the cells must be part of the array.
1424 *
1425 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1426 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1427 * of the region. The padding bytes for the cells must not be part of the array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001428 *
1429 * @param off The offset of the first element to be copied.
1430 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07001431 * @param d the source array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001432 */
Jason Samsb97b2512011-01-16 15:04:08 -08001433 public void copy1DRangeFrom(int off, int count, float[] d) {
1434 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001435 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001436 }
Jason Sams3042d262013-11-25 18:28:33 -08001437
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001438 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001439 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001440 *
1441 * @param off The offset of the first element to be copied.
1442 * @param count The number of elements to be copied.
1443 * @param data the source data allocation.
1444 * @param dataOff off The offset of the first element in data to
1445 * be copied.
1446 */
1447 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001448 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -07001449 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001450 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001451 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001452 data.mSelectedLOD, data.mSelectedFace.mID);
Chris Craik5c705d62015-06-01 10:39:36 -07001453 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001454 }
1455
Jason Samsfb9f82c2011-01-12 14:53:25 -08001456 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001457 if (mAdaptedAllocation != null) {
1458
1459 } else {
1460
1461 if (xoff < 0 || yoff < 0) {
1462 throw new RSIllegalArgumentException("Offset cannot be negative.");
1463 }
1464 if (h < 0 || w < 0) {
1465 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1466 }
1467 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1468 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1469 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001470 }
1471 }
Jason Sams768bc022009-09-21 19:41:04 -07001472
Jason Sams3042d262013-11-25 18:28:33 -08001473 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1474 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001475 try {
1476 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
1477 mRS.validate();
1478 validate2DRange(xoff, yoff, w, h);
1479 final int dataSize = mType.mElement.getBytesSize() * w * h;
1480 // AutoPadding for Vec3 Element
1481 boolean usePadding = false;
1482 int sizeBytes = arrayLen * dt.mSize;
1483 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1484 if (dataSize / 4 * 3 > sizeBytes) {
1485 throw new RSIllegalArgumentException("Array too small for allocation type.");
1486 }
1487 usePadding = true;
1488 sizeBytes = dataSize;
1489 } else {
1490 if (dataSize > sizeBytes) {
1491 throw new RSIllegalArgumentException("Array too small for allocation type.");
1492 }
Miao Wang87e908d2015-03-02 15:15:15 -08001493 }
Chris Craik06d29842015-06-02 17:19:24 -07001494 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1495 array, sizeBytes, dt,
1496 mType.mElement.mType.mSize, usePadding);
1497 } finally {
1498 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001499 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001500 }
1501
Jason Sams3042d262013-11-25 18:28:33 -08001502 /**
1503 * Copy from an array into a rectangular region in this Allocation. The
Miao Wang3231e8e2016-04-01 15:10:47 -07001504 * array is assumed to be tightly packed. This variant is type checked
1505 * and will generate exceptions if the Allocation's {@link
1506 * android.renderscript.Element} does not match the input data type.
1507 *
1508 * <p> The size of the region is: w * h * {@link #getElement}.{@link
1509 * Element#getBytesSize}.
1510 *
1511 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1512 * array in bytes must be at least the size of the region.
1513 *
1514 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1515 * is disabled, then the size of the array in bytes must be at least the size
1516 * of the region. The padding bytes for the cells must be part of the array.
1517 *
1518 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1519 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1520 * of the region. The padding bytes for the cells must not be part of the array.
Jason Sams3042d262013-11-25 18:28:33 -08001521 *
1522 * @param xoff X offset of the region to update in this Allocation
1523 * @param yoff Y offset of the region to update in this Allocation
1524 * @param w Width of the region to update
1525 * @param h Height of the region to update
Ying Wang16229812013-11-26 15:45:12 -08001526 * @param array Data to be placed into the Allocation
Jason Sams3042d262013-11-25 18:28:33 -08001527 */
1528 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001529 try {
1530 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1531 copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
1532 validateObjectIsPrimitiveArray(array, true),
1533 java.lang.reflect.Array.getLength(array));
1534 } finally {
1535 Trace.traceEnd(RenderScript.TRACE_TAG);
1536 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001537 }
1538
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001539 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001540 * Copy from an array into a rectangular region in this Allocation. The
Miao Wang3231e8e2016-04-01 15:10:47 -07001541 * array is assumed to be tightly packed. This variant is type checked
1542 * and will generate exceptions if the Allocation's {@link
1543 * android.renderscript.Element} is not an 8 bit integer nor a vector of 8 bit
1544 * integers {@link android.renderscript.Element.DataType}.
1545 *
1546 * <p> The size of the region is: w * h * {@link #getElement}.{@link
1547 * Element#getBytesSize}.
1548 *
1549 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1550 * array in bytes must be at least the size of the region.
1551 *
1552 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1553 * is disabled, then the size of the array in bytes must be at least the size
1554 * of the region. The padding bytes for the cells must be part of the array.
1555 *
1556 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1557 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1558 * of the region. The padding bytes for the cells must not be part of the array.
Jason Samsf7086092011-01-12 13:28:37 -08001559 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001560 * @param xoff X offset of the region to update in this Allocation
1561 * @param yoff Y offset of the region to update in this Allocation
1562 * @param w Width of the region to update
1563 * @param h Height of the region to update
1564 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001565 */
1566 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001567 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001568 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1569 Element.DataType.SIGNED_8, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001570 }
1571
Tim Murrayc11e25c2013-04-09 11:01:01 -07001572 /**
1573 * Copy from an array into a rectangular region in this Allocation. The
Miao Wang3231e8e2016-04-01 15:10:47 -07001574 * array is assumed to be tightly packed. This variant is type checked
1575 * and will generate exceptions if the Allocation's {@link
1576 * android.renderscript.Element} is not a 16 bit integer nor a vector of 16 bit
1577 * integers {@link android.renderscript.Element.DataType}.
1578 *
1579 * <p> The size of the region is: w * h * {@link #getElement}.{@link
1580 * Element#getBytesSize}.
1581 *
1582 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1583 * array in bytes must be at least the size of the region.
1584 *
1585 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1586 * is disabled, then the size of the array in bytes must be at least the size
1587 * of the region. The padding bytes for the cells must be part of the array.
1588 *
1589 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1590 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1591 * of the region. The padding bytes for the cells must not be part of the array.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001592 *
1593 * @param xoff X offset of the region to update in this Allocation
1594 * @param yoff Y offset of the region to update in this Allocation
1595 * @param w Width of the region to update
1596 * @param h Height of the region to update
1597 * @param data to be placed into the Allocation
1598 */
Jason Samsf7086092011-01-12 13:28:37 -08001599 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001600 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -08001601 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1602 Element.DataType.SIGNED_16, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001603 }
1604
Tim Murrayc11e25c2013-04-09 11:01:01 -07001605 /**
1606 * Copy from an array into a rectangular region in this Allocation. The
Miao Wang3231e8e2016-04-01 15:10:47 -07001607 * array is assumed to be tightly packed. This variant is type checked
1608 * and will generate exceptions if the Allocation's {@link
1609 * android.renderscript.Element} is not a 32 bit integer nor a vector of 32 bit
1610 * integers {@link android.renderscript.Element.DataType}.
1611 *
1612 * <p> The size of the region is: w * h * {@link #getElement}.{@link
1613 * Element#getBytesSize}.
1614 *
1615 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1616 * array in bytes must be at least the size of the region.
1617 *
1618 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1619 * is disabled, then the size of the array in bytes must be at least the size
1620 * of the region. The padding bytes for the cells must be part of the array.
1621 *
1622 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1623 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1624 * of the region. The padding bytes for the cells must not be part of the array.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001625 *
1626 * @param xoff X offset of the region to update in this Allocation
1627 * @param yoff Y offset of the region to update in this Allocation
1628 * @param w Width of the region to update
1629 * @param h Height of the region to update
1630 * @param data to be placed into the Allocation
1631 */
Jason Samsf7086092011-01-12 13:28:37 -08001632 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001633 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001634 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1635 Element.DataType.SIGNED_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001636 }
1637
Tim Murrayc11e25c2013-04-09 11:01:01 -07001638 /**
1639 * Copy from an array into a rectangular region in this Allocation. The
Miao Wang3231e8e2016-04-01 15:10:47 -07001640 * array is assumed to be tightly packed. This variant is type checked
1641 * and will generate exceptions if the Allocation's {@link
1642 * android.renderscript.Element} is neither a 32 bit float nor a vector of
1643 * 32 bit floats {@link android.renderscript.Element.DataType}.
1644 *
1645 * <p> The size of the region is: w * h * {@link #getElement}.{@link
1646 * Element#getBytesSize}.
1647 *
1648 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1649 * array in bytes must be at least the size of the region.
1650 *
1651 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1652 * is disabled, then the size of the array in bytes must be at least the size
1653 * of the region. The padding bytes for the cells must be part of the array.
1654 *
1655 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1656 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1657 * of the region. The padding bytes for the cells must not be part of the array.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001658 *
1659 * @param xoff X offset of the region to update in this Allocation
1660 * @param yoff Y offset of the region to update in this Allocation
1661 * @param w Width of the region to update
1662 * @param h Height of the region to update
1663 * @param data to be placed into the Allocation
1664 */
Jason Samsf7086092011-01-12 13:28:37 -08001665 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001666 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001667 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1668 Element.DataType.FLOAT_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001669 }
1670
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001671 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001672 * Copy a rectangular region from an Allocation into a rectangular region in
1673 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001674 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001675 * @param xoff X offset of the region in this Allocation
1676 * @param yoff Y offset of the region in this Allocation
1677 * @param w Width of the region to update.
1678 * @param h Height of the region to update.
1679 * @param data source Allocation.
1680 * @param dataXoff X offset in source Allocation
1681 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001682 */
1683 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1684 Allocation data, int dataXoff, int dataYoff) {
Chris Craik06d29842015-06-02 17:19:24 -07001685 try {
1686 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1687 mRS.validate();
1688 validate2DRange(xoff, yoff, w, h);
1689 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
1690 mSelectedLOD, mSelectedFace.mID,
1691 w, h, data.getID(mRS), dataXoff, dataYoff,
1692 data.mSelectedLOD, data.mSelectedFace.mID);
1693 } finally {
1694 Trace.traceEnd(RenderScript.TRACE_TAG);
1695 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001696 }
1697
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001698 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001699 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1700 * and width of the update will use the height and width of the {@link
1701 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001702 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001703 * @param xoff X offset of the region to update in this Allocation
1704 * @param yoff Y offset of the region to update in this Allocation
1705 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001706 */
1707 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Chris Craik5c705d62015-06-01 10:39:36 -07001708 try {
1709 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1710 mRS.validate();
1711 if (data.getConfig() == null) {
1712 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1713 Canvas c = new Canvas(newBitmap);
1714 c.drawBitmap(data, 0, 0, null);
1715 copy2DRangeFrom(xoff, yoff, newBitmap);
1716 return;
1717 }
1718 validateBitmapFormat(data);
1719 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
1720 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
1721 } finally {
1722 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -08001723 }
Jason Samsfa445b92011-01-07 17:00:07 -08001724 }
1725
Jason Samsb05d6892013-04-09 15:59:24 -07001726 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1727 if (mAdaptedAllocation != null) {
1728
1729 } else {
1730
1731 if (xoff < 0 || yoff < 0 || zoff < 0) {
1732 throw new RSIllegalArgumentException("Offset cannot be negative.");
1733 }
1734 if (h < 0 || w < 0 || d < 0) {
1735 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1736 }
1737 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1738 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1739 }
1740 }
1741 }
1742
1743 /**
Miao Wang258db502015-03-03 14:05:36 -08001744 * Copy a rectangular region from the array into the allocation.
1745 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001746 *
Miao Wang258db502015-03-03 14:05:36 -08001747 * The data type of the array is not required to be the same as
1748 * the element data type.
Jason Samsb05d6892013-04-09 15:59:24 -07001749 */
Jason Sams3042d262013-11-25 18:28:33 -08001750 private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1751 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001752 try {
1753 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
1754 mRS.validate();
1755 validate3DRange(xoff, yoff, zoff, w, h, d);
1756 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1757 // AutoPadding for Vec3 Element
1758 boolean usePadding = false;
1759 int sizeBytes = arrayLen * dt.mSize;
1760 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1761 if (dataSize / 4 * 3 > sizeBytes) {
1762 throw new RSIllegalArgumentException("Array too small for allocation type.");
1763 }
1764 usePadding = true;
1765 sizeBytes = dataSize;
1766 } else {
1767 if (dataSize > sizeBytes) {
1768 throw new RSIllegalArgumentException("Array too small for allocation type.");
1769 }
Miao Wang87e908d2015-03-02 15:15:15 -08001770 }
Chris Craik06d29842015-06-02 17:19:24 -07001771 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1772 array, sizeBytes, dt,
1773 mType.mElement.mType.mSize, usePadding);
1774 } finally {
1775 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001776 }
Jason Samsb05d6892013-04-09 15:59:24 -07001777 }
1778
1779 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001780 * Copy from an array into a 3D region in this Allocation. The
1781 * array is assumed to be tightly packed. This variant is type checked
1782 * and will generate exceptions if the Allocation's {@link
1783 * android.renderscript.Element} does not match the input data type.
1784 *
1785 * <p> The size of the region is: w * h * d * {@link #getElement}.{@link
1786 * Element#getBytesSize}.
1787 *
1788 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1789 * array in bytes must be at least the size of the region.
1790 *
1791 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1792 * is disabled, then the size of the array in bytes must be at least the size
1793 * of the region. The padding bytes for the cells must be part of the array.
1794 *
1795 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1796 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1797 * of the region. The padding bytes for the cells must not be part of the array.
Jason Samsb05d6892013-04-09 15:59:24 -07001798 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001799 * @param xoff X offset of the region to update in this Allocation
1800 * @param yoff Y offset of the region to update in this Allocation
1801 * @param zoff Z offset of the region to update in this Allocation
1802 * @param w Width of the region to update
1803 * @param h Height of the region to update
1804 * @param d Depth of the region to update
Miao Wang87e908d2015-03-02 15:15:15 -08001805 * @param array to be placed into the allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001806 */
Jason Sams3042d262013-11-25 18:28:33 -08001807 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001808 try {
1809 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
1810 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
1811 validateObjectIsPrimitiveArray(array, true),
1812 java.lang.reflect.Array.getLength(array));
1813 } finally {
1814 Trace.traceEnd(RenderScript.TRACE_TAG);
1815 }
Jason Samsb05d6892013-04-09 15:59:24 -07001816 }
1817
1818 /**
Jason Samsb05d6892013-04-09 15:59:24 -07001819 * Copy a rectangular region into the allocation from another
1820 * allocation.
1821 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001822 * @param xoff X offset of the region to update in this Allocation
1823 * @param yoff Y offset of the region to update in this Allocation
1824 * @param zoff Z offset of the region to update in this Allocation
1825 * @param w Width of the region to update.
1826 * @param h Height of the region to update.
1827 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001828 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001829 * @param dataXoff X offset of the region in the source Allocation
1830 * @param dataYoff Y offset of the region in the source Allocation
1831 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001832 */
1833 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1834 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1835 mRS.validate();
1836 validate3DRange(xoff, yoff, zoff, w, h, d);
1837 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1838 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1839 data.mSelectedLOD);
1840 }
1841
Jason Samsfa445b92011-01-07 17:00:07 -08001842
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001843 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001844 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1845 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001846 *
1847 * @param b The bitmap to be set from the Allocation.
1848 */
Jason Samsfa445b92011-01-07 17:00:07 -08001849 public void copyTo(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -07001850 try {
1851 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1852 mRS.validate();
1853 validateBitmapFormat(b);
1854 validateBitmapSize(b);
1855 mRS.nAllocationCopyToBitmap(getID(mRS), b);
1856 } finally {
1857 Trace.traceEnd(RenderScript.TRACE_TAG);
1858 }
Jason Samsfa445b92011-01-07 17:00:07 -08001859 }
1860
Jason Sams3042d262013-11-25 18:28:33 -08001861 private void copyTo(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001862 try {
1863 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1864 mRS.validate();
1865 boolean usePadding = false;
1866 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1867 usePadding = true;
Miao Wangd9b63282015-04-03 09:15:39 -07001868 }
Chris Craik06d29842015-06-02 17:19:24 -07001869 if (usePadding) {
1870 if (dt.mSize * arrayLen < mSize / 4 * 3) {
1871 throw new RSIllegalArgumentException(
1872 "Size of output array cannot be smaller than size of allocation.");
1873 }
1874 } else {
1875 if (dt.mSize * arrayLen < mSize) {
1876 throw new RSIllegalArgumentException(
1877 "Size of output array cannot be smaller than size of allocation.");
1878 }
Miao Wangd9b63282015-04-03 09:15:39 -07001879 }
Chris Craik06d29842015-06-02 17:19:24 -07001880 mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
1881 } finally {
1882 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wangd9b63282015-04-03 09:15:39 -07001883 }
Jason Sams3042d262013-11-25 18:28:33 -08001884 }
1885
1886 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001887 * Copy from the Allocation into an array. The method is type checked
1888 * and will generate exceptions if the Allocation's {@link
1889 * android.renderscript.Element} does not match the input data type.
1890 *
1891 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1892 * array in bytes must be at least the size of the Allocation {@link
1893 * #getBytesSize getBytesSize()}.
1894 *
1895 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1896 * is disabled, then the size of the array in bytes must be at least the size
1897 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1898 * the cells will be part of the array.
1899 *
1900 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1901 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1902 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1903 * the cells must not be part of the array.
Jason Sams3042d262013-11-25 18:28:33 -08001904 *
1905 * @param array The array to be set from the Allocation.
1906 */
1907 public void copyTo(Object array) {
1908 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1909 java.lang.reflect.Array.getLength(array));
1910 }
1911
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001912 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001913 * Copy from the Allocation into a byte array. This variant is type checked
1914 * and will generate exceptions if the Allocation's {@link
1915 * android.renderscript.Element} is neither an 8 bit integer nor a vector of 8 bit
1916 * integers {@link android.renderscript.Element.DataType}.
1917 *
1918 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1919 * array in bytes must be at least the size of the Allocation {@link
1920 * #getBytesSize getBytesSize()}.
1921 *
1922 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1923 * is disabled, then the size of the array in bytes must be at least the size
1924 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1925 * the cells will be part of the array.
1926 *
1927 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1928 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1929 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1930 * the cells must not be part of the array.
Jason Sams48fe5342011-07-08 13:52:30 -07001931 *
1932 * @param d The array to be set from the Allocation.
1933 */
Jason Samsfa445b92011-01-07 17:00:07 -08001934 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001935 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001936 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001937 }
1938
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001939 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001940 * Copy from the Allocation into a short array. This variant is type checked
1941 * and will generate exceptions if the Allocation's {@link
1942 * android.renderscript.Element} is not a 16 bit integer nor a vector of 16 bit
1943 * integers {@link android.renderscript.Element.DataType}.
1944 *
1945 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1946 * array in bytes must be at least the size of the Allocation {@link
1947 * #getBytesSize getBytesSize()}.
1948 *
1949 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1950 * is disabled, then the size of the array in bytes must be at least the size
1951 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1952 * the cells will be part of the array.
1953 *
1954 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1955 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1956 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1957 * the cells must not be part of the array.
Jason Sams48fe5342011-07-08 13:52:30 -07001958 *
1959 * @param d The array to be set from the Allocation.
1960 */
Jason Samsfa445b92011-01-07 17:00:07 -08001961 public void copyTo(short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001962 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -08001963 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001964 }
1965
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001966 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001967 * Copy from the Allocation into a int array. This variant is type checked
1968 * and will generate exceptions if the Allocation's {@link
1969 * android.renderscript.Element} is not a 32 bit integer nor a vector of 32 bit
1970 * integers {@link android.renderscript.Element.DataType}.
1971 *
1972 * <p> If the Allocation does not have Vec3 Elements, then the size of the
1973 * array in bytes must be at least the size of the Allocation {@link
1974 * #getBytesSize getBytesSize()}.
1975 *
1976 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1977 * is disabled, then the size of the array in bytes must be at least the size
1978 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1979 * the cells will be part of the array.
1980 *
1981 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1982 * is enabled, then the size of the array in bytes must be at least 3/4 the size
1983 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1984 * the cells must not be part of the array.
Jason Sams48fe5342011-07-08 13:52:30 -07001985 *
1986 * @param d The array to be set from the Allocation.
1987 */
Jason Samsfa445b92011-01-07 17:00:07 -08001988 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001989 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001990 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001991 }
1992
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001993 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07001994 * Copy from the Allocation into a float array. This variant is type checked
1995 * and will generate exceptions if the Allocation's {@link
1996 * android.renderscript.Element} is neither a 32 bit float nor a vector of
1997 * 32 bit floats {@link android.renderscript.Element.DataType}.
1998 *
1999 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2000 * array in bytes must be at least the size of the Allocation {@link
2001 * #getBytesSize getBytesSize()}.
2002 *
2003 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2004 * is disabled, then the size of the array in bytes must be at least the size
2005 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
2006 * the cells will be part of the array.
2007 *
2008 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2009 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2010 * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
2011 * the cells must not be part of the array.
Jason Sams48fe5342011-07-08 13:52:30 -07002012 *
2013 * @param d The array to be set from the Allocation.
2014 */
Jason Samsfa445b92011-01-07 17:00:07 -08002015 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08002016 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08002017 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07002018 }
2019
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002020 /**
Miao Wang3c613272015-05-11 11:41:55 -07002021 * @hide
2022 *
Miao Wang45cec0a2015-03-04 16:40:21 -08002023 * This is only intended to be used by auto-generated code reflected from
2024 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -08002025 *
2026 * @param xoff
2027 * @param yoff
2028 * @param zoff
2029 * @param component_number
Miao Wang258db502015-03-03 14:05:36 -08002030 * @param fp
Miao Wangc8e237e2015-02-20 18:36:32 -08002031 */
Miao Wang45cec0a2015-03-04 16:40:21 -08002032 public void copyToFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -08002033 mRS.validate();
2034 if (component_number >= mType.mElement.mElements.length) {
2035 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
2036 }
2037 if(xoff < 0) {
2038 throw new RSIllegalArgumentException("Offset x must be >= 0.");
2039 }
2040 if(yoff < 0) {
2041 throw new RSIllegalArgumentException("Offset y must be >= 0.");
2042 }
2043 if(zoff < 0) {
2044 throw new RSIllegalArgumentException("Offset z must be >= 0.");
2045 }
2046
Miao Wang45cec0a2015-03-04 16:40:21 -08002047 final byte[] data = fp.getData();
Miao Wangbfa5e652015-05-04 15:29:25 -07002048 int data_length = data.length;
Miao Wangc8e237e2015-02-20 18:36:32 -08002049 int eSize = mType.mElement.mElements[component_number].getBytesSize();
2050 eSize *= mType.mElement.mArraySizes[component_number];
2051
Miao Wang45cec0a2015-03-04 16:40:21 -08002052 if (data_length != eSize) {
2053 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
2054 " does not match component size " + eSize + ".");
Miao Wangc8e237e2015-02-20 18:36:32 -08002055 }
2056
2057 mRS.nAllocationElementRead(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Miao Wang45cec0a2015-03-04 16:40:21 -08002058 component_number, data, data_length);
Miao Wangc8e237e2015-02-20 18:36:32 -08002059 }
2060 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002061 * Resize a 1D allocation. The contents of the allocation are preserved.
2062 * If new elements are allocated objects are created with null contents and
2063 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08002064 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002065 * <p>If the new region is smaller the references of any objects outside the
2066 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08002067 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002068 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08002069 *
2070 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07002071 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002072 * @deprecated RenderScript objects should be immutable once created. The
Tim Murraycd38b762014-08-13 13:20:25 -07002073 * replacement is to create a new allocation and copy the contents. This
2074 * function will throw an exception if API 21 or higher is used.
Jason Samsf7086092011-01-12 13:28:37 -08002075 */
Jason Sams31a7e422010-10-26 13:09:17 -07002076 public synchronized void resize(int dimX) {
Tim Murraycd38b762014-08-13 13:20:25 -07002077 if (mRS.getApplicationContext().getApplicationInfo().targetSdkVersion >= 21) {
2078 throw new RSRuntimeException("Resize is not allowed in API 21+.");
2079 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002080 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08002081 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07002082 }
Jason Samse07694b2012-04-03 15:36:36 -07002083 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07002084 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07002085
Tim Murray460a0492013-11-19 12:45:54 -08002086 long typeID = mRS.nAllocationGetType(getID(mRS));
Yang Nie1798e42016-04-07 11:17:59 -07002087 // Sets zero the mID so that the finalizer of the old mType value won't
2088 // destroy the native object that is being reused.
2089 mType.setID(0);
Jason Sams31a7e422010-10-26 13:09:17 -07002090 mType = new Type(typeID, mRS);
2091 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07002092 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07002093 }
2094
Miao Wangc8e237e2015-02-20 18:36:32 -08002095 private void copy1DRangeToUnchecked(int off, int count, Object array,
2096 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07002097 try {
2098 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked");
2099 final int dataSize = mType.mElement.getBytesSize() * count;
2100 // AutoPadding for Vec3 Element
2101 boolean usePadding = false;
2102 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
2103 usePadding = true;
2104 }
2105 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
2106 mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
2107 mType.mElement.mType.mSize, usePadding);
2108 } finally {
2109 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08002110 }
Miao Wangc8e237e2015-02-20 18:36:32 -08002111 }
2112
2113 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002114 * Copy a 1D region of this Allocation into an array. This method does not
Miao Wangc8e237e2015-02-20 18:36:32 -08002115 * guarantee that the Allocation is compatible with the input buffer.
2116 *
Miao Wang3231e8e2016-04-01 15:10:47 -07002117 * <p> The size of the region is: count * {@link #getElement}.{@link
2118 * Element#getBytesSize}.
2119 *
2120 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2121 * array in bytes must be at least the size of the region.
2122 *
2123 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2124 * is disabled, then the size of the array in bytes must be at least the size
2125 * of the region. The padding bytes for the cells must be part of the array.
2126 *
2127 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2128 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2129 * of the region. The padding bytes for the cells must not be part of the array.
2130 *
Miao Wangc8e237e2015-02-20 18:36:32 -08002131 * @param off The offset of the first element to be copied.
2132 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002133 * @param array The dest array
Miao Wangc8e237e2015-02-20 18:36:32 -08002134 */
2135 public void copy1DRangeToUnchecked(int off, int count, Object array) {
2136 copy1DRangeToUnchecked(off, count, array,
2137 validateObjectIsPrimitiveArray(array, false),
2138 java.lang.reflect.Array.getLength(array));
2139 }
2140
2141 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002142 * Copy a 1D region of this Allocation into an array. This method does not
Miao Wangc8e237e2015-02-20 18:36:32 -08002143 * guarantee that the Allocation is compatible with the input buffer.
2144 *
Miao Wang3231e8e2016-04-01 15:10:47 -07002145 * <p> The size of the region is: count * {@link #getElement}.{@link
2146 * Element#getBytesSize}.
2147 *
2148 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2149 * array in bytes must be at least the size of the region.
2150 *
2151 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2152 * is disabled, then the size of the array in bytes must be at least the size
2153 * of the region. The padding bytes for the cells must be part of the array.
2154 *
2155 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2156 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2157 * of the region. The padding bytes for the cells must not be part of the array.
2158 *
Miao Wangc8e237e2015-02-20 18:36:32 -08002159 * @param off The offset of the first element to be copied.
2160 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002161 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002162 */
2163 public void copy1DRangeToUnchecked(int off, int count, int[] d) {
2164 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
2165 }
2166
2167 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002168 * Copy a 1D region of this Allocation into an array. This method does not
Miao Wangc8e237e2015-02-20 18:36:32 -08002169 * guarantee that the Allocation is compatible with the input buffer.
2170 *
Miao Wang3231e8e2016-04-01 15:10:47 -07002171 * <p> The size of the region is: count * {@link #getElement}.{@link
2172 * Element#getBytesSize}.
2173 *
2174 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2175 * array in bytes must be at least the size of the region.
2176 *
2177 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2178 * is disabled, then the size of the array in bytes must be at least the size
2179 * of the region. The padding bytes for the cells must be part of the array.
2180 *
2181 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2182 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2183 * of the region. The padding bytes for the cells must not be part of the array.
2184 *
Miao Wangc8e237e2015-02-20 18:36:32 -08002185 * @param off The offset of the first element to be copied.
2186 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002187 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002188 */
2189 public void copy1DRangeToUnchecked(int off, int count, short[] d) {
2190 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
2191 }
2192
2193 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002194 * Copy a 1D region of this Allocation into an array. This method does not
Miao Wangc8e237e2015-02-20 18:36:32 -08002195 * guarantee that the Allocation is compatible with the input buffer.
2196 *
Miao Wang3231e8e2016-04-01 15:10:47 -07002197 * <p> The size of the region is: count * {@link #getElement}.{@link
2198 * Element#getBytesSize}.
2199 *
2200 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2201 * array in bytes must be at least the size of the region.
2202 *
2203 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2204 * is disabled, then the size of the array in bytes must be at least the size
2205 * of the region. The padding bytes for the cells must be part of the array.
2206 *
2207 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2208 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2209 * of the region. The padding bytes for the cells must not be part of the array.
2210 *
Miao Wangc8e237e2015-02-20 18:36:32 -08002211 * @param off The offset of the first element to be copied.
2212 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002213 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002214 */
2215 public void copy1DRangeToUnchecked(int off, int count, byte[] d) {
2216 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
2217 }
2218
2219 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002220 * Copy a 1D region of this Allocation into an array. This method does not
Miao Wangc8e237e2015-02-20 18:36:32 -08002221 * guarantee that the Allocation is compatible with the input buffer.
2222 *
Miao Wang3231e8e2016-04-01 15:10:47 -07002223 * <p> The size of the region is: count * {@link #getElement}.{@link
2224 * Element#getBytesSize}.
2225 *
2226 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2227 * array in bytes must be at least the size of the region.
2228 *
2229 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2230 * is disabled, then the size of the array in bytes must be at least the size
2231 * of the region. The padding bytes for the cells must be part of the array.
2232 *
2233 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2234 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2235 * of the region. The padding bytes for the cells must not be part of the array.
2236 *
Miao Wangc8e237e2015-02-20 18:36:32 -08002237 * @param off The offset of the first element to be copied.
2238 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002239 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002240 */
2241 public void copy1DRangeToUnchecked(int off, int count, float[] d) {
2242 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
2243 }
2244
Miao Wangc8e237e2015-02-20 18:36:32 -08002245 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002246 * Copy a 1D region of this Allocation into an array. This method is type checked
2247 * and will generate exceptions if the Allocation's {@link
2248 * android.renderscript.Element} does not match the component type
2249 * of the array passed in.
2250 *
2251 * <p> The size of the region is: count * {@link #getElement}.{@link
2252 * Element#getBytesSize}.
2253 *
2254 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2255 * array in bytes must be at least the size of the region.
2256 *
2257 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2258 * is disabled, then the size of the array in bytes must be at least the size
2259 * of the region. The padding bytes for the cells must be part of the array.
2260 *
2261 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2262 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2263 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002264 *
2265 * @param off The offset of the first element to be copied.
2266 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002267 * @param array The source array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002268 */
2269 public void copy1DRangeTo(int off, int count, Object array) {
2270 copy1DRangeToUnchecked(off, count, array,
2271 validateObjectIsPrimitiveArray(array, true),
2272 java.lang.reflect.Array.getLength(array));
2273 }
2274
2275 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002276 * Copy a 1D region of this Allocation into an array. This variant is type checked
2277 * and will generate exceptions if the Allocation's {@link
2278 * android.renderscript.Element} is neither a 32 bit integer nor a vector of 32 bit
2279 * integers {@link android.renderscript.Element.DataType}.
2280 *
2281 * <p> The size of the region is: count * {@link #getElement}.{@link
2282 * Element#getBytesSize}.
2283 *
2284 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2285 * array in bytes must be at least the size of the region.
2286 *
2287 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2288 * is disabled, then the size of the array in bytes must be at least the size
2289 * of the region. The padding bytes for the cells must be part of the array.
2290 *
2291 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2292 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2293 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002294 *
2295 * @param off The offset of the first element to be copied.
2296 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002297 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002298 */
2299 public void copy1DRangeTo(int off, int count, int[] d) {
2300 validateIsInt32();
2301 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
2302 }
2303
2304 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002305 * Copy a 1D region of this Allocation into an array. This variant is type checked
2306 * and will generate exceptions if the Allocation's {@link
2307 * android.renderscript.Element} is neither a 16 bit integer nor a vector of 16 bit
2308 * integers {@link android.renderscript.Element.DataType}.
2309 *
2310 * <p> The size of the region is: count * {@link #getElement}.{@link
2311 * Element#getBytesSize}.
2312 *
2313 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2314 * array in bytes must be at least the size of the region.
2315 *
2316 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2317 * is disabled, then the size of the array in bytes must be at least the size
2318 * of the region. The padding bytes for the cells must be part of the array.
2319 *
2320 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2321 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2322 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002323 *
2324 * @param off The offset of the first element to be copied.
2325 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002326 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002327 */
2328 public void copy1DRangeTo(int off, int count, short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08002329 validateIsInt16OrFloat16();
Miao Wangc8e237e2015-02-20 18:36:32 -08002330 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
2331 }
2332
2333 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002334 * Copy a 1D region of this Allocation into an array. This variant is type checked
2335 * and will generate exceptions if the Allocation's {@link
2336 * android.renderscript.Element} is neither an 8 bit integer nor a vector of 8 bit
2337 * integers {@link android.renderscript.Element.DataType}.
2338 *
2339 * <p> The size of the region is: count * {@link #getElement}.{@link
2340 * Element#getBytesSize}.
2341 *
2342 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2343 * array in bytes must be at least the size of the region.
2344 *
2345 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2346 * is disabled, then the size of the array in bytes must be at least the size
2347 * of the region. The padding bytes for the cells must be part of the array.
2348 *
2349 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2350 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2351 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002352 *
2353 * @param off The offset of the first element to be copied.
2354 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002355 * @param d the source array
Miao Wangc8e237e2015-02-20 18:36:32 -08002356 */
2357 public void copy1DRangeTo(int off, int count, byte[] d) {
2358 validateIsInt8();
2359 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
2360 }
2361
2362 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002363 * Copy a 1D region of this Allocation into an array. This variant is type checked
2364 * and will generate exceptions if the Allocation's {@link
2365 * android.renderscript.Element} is neither a 32 bit float nor a vector of
2366 * 32 bit floats {@link android.renderscript.Element.DataType}.
2367 *
2368 * <p> The size of the region is: count * {@link #getElement}.{@link
2369 * Element#getBytesSize}.
2370 *
2371 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2372 * array in bytes must be at least the size of the region.
2373 *
2374 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2375 * is disabled, then the size of the array in bytes must be at least the size
2376 * of the region. The padding bytes for the cells must be part of the array.
2377 *
2378 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2379 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2380 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002381 *
2382 * @param off The offset of the first element to be copied.
2383 * @param count The number of elements to be copied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002384 * @param d the source array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002385 */
2386 public void copy1DRangeTo(int off, int count, float[] d) {
2387 validateIsFloat32();
2388 copy1DRangeToUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
2389 }
2390
2391
2392 void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
2393 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07002394 try {
2395 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked");
2396 mRS.validate();
2397 validate2DRange(xoff, yoff, w, h);
2398 final int dataSize = mType.mElement.getBytesSize() * w * h;
2399 // AutoPadding for Vec3 Element
2400 boolean usePadding = false;
2401 int sizeBytes = arrayLen * dt.mSize;
2402 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
2403 if (dataSize / 4 * 3 > sizeBytes) {
2404 throw new RSIllegalArgumentException("Array too small for allocation type.");
2405 }
2406 usePadding = true;
2407 sizeBytes = dataSize;
2408 } else {
2409 if (dataSize > sizeBytes) {
2410 throw new RSIllegalArgumentException("Array too small for allocation type.");
2411 }
Miao Wang87e908d2015-03-02 15:15:15 -08002412 }
Chris Craik06d29842015-06-02 17:19:24 -07002413 mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
2414 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
2415 } finally {
2416 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08002417 }
Miao Wangc8e237e2015-02-20 18:36:32 -08002418 }
2419
2420 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002421 * Copy from a rectangular region in this Allocation into an array. This
2422 * method is type checked and will generate exceptions if the Allocation's
2423 * {@link android.renderscript.Element} does not match the component type
2424 * of the array passed in.
2425 *
2426 * <p> The size of the region is: w * h * {@link #getElement}.{@link
2427 * Element#getBytesSize}.
2428 *
2429 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2430 * array in bytes must be at least the size of the region.
2431 *
2432 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2433 * is disabled, then the size of the array in bytes must be at least the size
2434 * of the region. The padding bytes for the cells must be part of the array.
2435 *
2436 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2437 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2438 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002439 *
2440 * @param xoff X offset of the region to copy in this Allocation
2441 * @param yoff Y offset of the region to copy in this Allocation
2442 * @param w Width of the region to copy
2443 * @param h Height of the region to copy
2444 * @param array Dest Array to be copied into
2445 */
2446 public void copy2DRangeTo(int xoff, int yoff, int w, int h, Object array) {
2447 copy2DRangeToUnchecked(xoff, yoff, w, h, array,
2448 validateObjectIsPrimitiveArray(array, true),
2449 java.lang.reflect.Array.getLength(array));
2450 }
2451
2452 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002453 * Copy from a rectangular region in this Allocation into an array. This
2454 * variant is type checked and will generate exceptions if the Allocation's
2455 * {@link android.renderscript.Element} is neither an 8 bit integer nor a vector
2456 * of 8 bit integers {@link android.renderscript.Element.DataType}.
2457 *
2458 * <p> The size of the region is: w * h * {@link #getElement}.{@link
2459 * Element#getBytesSize}.
2460 *
2461 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2462 * array in bytes must be at least the size of the region.
2463 *
2464 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2465 * is disabled, then the size of the array in bytes must be at least the size
2466 * of the region. The padding bytes for the cells must be part of the array.
2467 *
2468 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2469 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2470 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002471 *
2472 * @param xoff X offset of the region to copy in this Allocation
2473 * @param yoff Y offset of the region to copy in this Allocation
2474 * @param w Width of the region to copy
2475 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08002476 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08002477 */
2478 public void copy2DRangeTo(int xoff, int yoff, int w, int h, byte[] data) {
2479 validateIsInt8();
2480 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
2481 Element.DataType.SIGNED_8, data.length);
2482 }
2483
2484 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002485 * Copy from a rectangular region in this Allocation into an array. This
2486 * variant is type checked and will generate exceptions if the Allocation's
2487 * {@link android.renderscript.Element} is neither a 16 bit integer nor a vector
2488 * of 16 bit integers {@link android.renderscript.Element.DataType}.
2489 *
2490 * <p> The size of the region is: w * h * {@link #getElement}.{@link
2491 * Element#getBytesSize}.
2492 *
2493 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2494 * array in bytes must be at least the size of the region.
2495 *
2496 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2497 * is disabled, then the size of the array in bytes must be at least the size
2498 * of the region. The padding bytes for the cells must be part of the array.
2499 *
2500 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2501 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2502 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002503 *
2504 * @param xoff X offset of the region to copy in this Allocation
2505 * @param yoff Y offset of the region to copy in this Allocation
2506 * @param w Width of the region to copy
2507 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08002508 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08002509 */
2510 public void copy2DRangeTo(int xoff, int yoff, int w, int h, short[] data) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08002511 validateIsInt16OrFloat16();
Miao Wangc8e237e2015-02-20 18:36:32 -08002512 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
2513 Element.DataType.SIGNED_16, data.length);
2514 }
2515
2516 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002517 * Copy from a rectangular region in this Allocation into an array. This
2518 * variant is type checked and will generate exceptions if the Allocation's
2519 * {@link android.renderscript.Element} is neither a 32 bit integer nor a vector
2520 * of 32 bit integers {@link android.renderscript.Element.DataType}.
2521 *
2522 * <p> The size of the region is: w * h * {@link #getElement}.{@link
2523 * Element#getBytesSize}.
2524 *
2525 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2526 * array in bytes must be at least the size of the region.
2527 *
2528 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2529 * is disabled, then the size of the array in bytes must be at least the size
2530 * of the region. The padding bytes for the cells must be part of the array.
2531 *
2532 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2533 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2534 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002535 *
2536 * @param xoff X offset of the region to copy in this Allocation
2537 * @param yoff Y offset of the region to copy in this Allocation
2538 * @param w Width of the region to copy
2539 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08002540 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08002541 */
2542 public void copy2DRangeTo(int xoff, int yoff, int w, int h, int[] data) {
2543 validateIsInt32();
2544 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
2545 Element.DataType.SIGNED_32, data.length);
2546 }
2547
2548 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002549 * Copy from a rectangular region in this Allocation into an array. This
2550 * variant is type checked and will generate exceptions if the Allocation's
2551 * {@link android.renderscript.Element} is neither a 32 bit float nor a vector
2552 * of 32 bit floats {@link android.renderscript.Element.DataType}.
2553 *
2554 * <p> The size of the region is: w * h * {@link #getElement}.{@link
2555 * Element#getBytesSize}.
2556 *
2557 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2558 * array in bytes must be at least the size of the region.
2559 *
2560 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2561 * is disabled, then the size of the array in bytes must be at least the size
2562 * of the region. The padding bytes for the cells must be part of the array.
2563 *
2564 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2565 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2566 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002567 *
2568 * @param xoff X offset of the region to copy in this Allocation
2569 * @param yoff Y offset of the region to copy in this Allocation
2570 * @param w Width of the region to copy
2571 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08002572 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08002573 */
2574 public void copy2DRangeTo(int xoff, int yoff, int w, int h, float[] data) {
2575 validateIsFloat32();
2576 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
2577 Element.DataType.FLOAT_32, data.length);
2578 }
2579
2580
2581 /**
Miao Wang3231e8e2016-04-01 15:10:47 -07002582 * Copy from a 3D region in this Allocation into an array. This method does
2583 * not guarantee that the Allocation is compatible with the input buffer.
Miao Wang258db502015-03-03 14:05:36 -08002584 * The array is assumed to be tightly packed.
Miao Wangc8e237e2015-02-20 18:36:32 -08002585 *
Miao Wang258db502015-03-03 14:05:36 -08002586 * The data type of the array is not required to be the same as
2587 * the element data type.
Miao Wangc8e237e2015-02-20 18:36:32 -08002588 */
2589 private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
2590 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07002591 try {
2592 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked");
2593 mRS.validate();
2594 validate3DRange(xoff, yoff, zoff, w, h, d);
2595 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
2596 // AutoPadding for Vec3 Element
2597 boolean usePadding = false;
2598 int sizeBytes = arrayLen * dt.mSize;
2599 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
2600 if (dataSize / 4 * 3 > sizeBytes) {
2601 throw new RSIllegalArgumentException("Array too small for allocation type.");
2602 }
2603 usePadding = true;
2604 sizeBytes = dataSize;
2605 } else {
2606 if (dataSize > sizeBytes) {
2607 throw new RSIllegalArgumentException("Array too small for allocation type.");
2608 }
Miao Wang87e908d2015-03-02 15:15:15 -08002609 }
Chris Craik06d29842015-06-02 17:19:24 -07002610 mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
2611 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
2612 } finally {
2613 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08002614 }
Miao Wangc8e237e2015-02-20 18:36:32 -08002615 }
2616
Miao Wang258db502015-03-03 14:05:36 -08002617 /*
Miao Wang3231e8e2016-04-01 15:10:47 -07002618 * Copy from a 3D region in this Allocation into an array. This
2619 * method is type checked and will generate exceptions if the Allocation's
2620 * {@link android.renderscript.Element} does not match the component type
2621 * of the array passed in.
2622 *
2623 * <p> The size of the region is: w * h * d * {@link #getElement}.{@link
2624 * Element#getBytesSize}.
2625 *
2626 * <p> If the Allocation does not have Vec3 Elements, then the size of the
2627 * array in bytes must be at least the size of the region.
2628 *
2629 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2630 * is disabled, then the size of the array in bytes must be at least the size
2631 * of the region. The padding bytes for the cells must be part of the array.
2632 *
2633 * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2634 * is enabled, then the size of the array in bytes must be at least 3/4 the size
2635 * of the region. The padding bytes for the cells must not be part of the array.
Miao Wangc8e237e2015-02-20 18:36:32 -08002636 *
2637 * @param xoff X offset of the region to copy in this Allocation
2638 * @param yoff Y offset of the region to copy in this Allocation
2639 * @param zoff Z offset of the region to copy in this Allocation
2640 * @param w Width of the region to copy
2641 * @param h Height of the region to copy
2642 * @param d Depth of the region to copy
2643 * @param array Dest Array to be copied into
2644 */
2645 public void copy3DRangeTo(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
2646 copy3DRangeToUnchecked(xoff, yoff, zoff, w, h, d, array,
2647 validateObjectIsPrimitiveArray(array, true),
2648 java.lang.reflect.Array.getLength(array));
2649 }
Jason Samsb8c5a842009-07-31 20:40:47 -07002650
2651 // creation
2652
Jason Sams49a05d72010-12-29 14:31:29 -08002653 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07002654 static {
2655 mBitmapOptions.inScaled = false;
2656 }
2657
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002658 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002659 * Creates a new Allocation with the given {@link
2660 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002661 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002662 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002663 * @param mips specifies desired mipmap behaviour for the
2664 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07002665 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002666 * utilized
2667 */
2668 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07002669 try {
2670 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
2671 rs.validate();
2672 if (type.getID(rs) == 0) {
2673 throw new RSInvalidStateException("Bad Type");
2674 }
Yang Ni6484b6b2016-03-24 09:40:32 -07002675 // TODO: What if there is an exception after this? The native allocation would leak.
Chris Craik06d29842015-06-02 17:19:24 -07002676 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
2677 if (id == 0) {
2678 throw new RSRuntimeException("Allocation creation failed.");
2679 }
Yang Nie1798e42016-04-07 11:17:59 -07002680 return new Allocation(id, rs, type, false, usage, mips);
Chris Craik06d29842015-06-02 17:19:24 -07002681 } finally {
2682 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams1bada8c2009-08-09 17:01:55 -07002683 }
Jason Sams857d0c72011-11-23 15:02:15 -08002684 }
2685
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002686 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002687 * Creates an Allocation with the size specified by the type and no mipmaps
2688 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002689 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002690 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002691 * @param type renderscript type describing data layout
2692 * @param usage bit field specifying how the allocation is
2693 * utilized
2694 *
2695 * @return allocation
2696 */
Jason Samse5d37122010-12-16 00:33:33 -08002697 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
2698 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
2699 }
2700
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002701 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002702 * Creates an Allocation for use by scripts with a given {@link
2703 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002704 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002705 * @param rs Context to which the Allocation will belong.
2706 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002707 *
2708 * @return allocation
2709 */
Jason Sams5476b452010-12-08 16:14:36 -08002710 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08002711 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08002712 }
Jason Sams1bada8c2009-08-09 17:01:55 -07002713
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002714 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002715 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002716 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002717 * @param rs Context to which the Allocation will belong.
2718 * @param e Element to use in the Allocation
2719 * @param count the number of Elements in the Allocation
2720 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002721 * utilized
2722 *
2723 * @return allocation
2724 */
Jason Sams5476b452010-12-08 16:14:36 -08002725 static public Allocation createSized(RenderScript rs, Element e,
2726 int count, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07002727 try {
2728 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
2729 rs.validate();
2730 Type.Builder b = new Type.Builder(rs, e);
2731 b.setX(count);
2732 Type t = b.create();
Jason Sams768bc022009-09-21 19:41:04 -07002733
Chris Craik06d29842015-06-02 17:19:24 -07002734 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
2735 if (id == 0) {
2736 throw new RSRuntimeException("Allocation creation failed.");
2737 }
Yang Nie1798e42016-04-07 11:17:59 -07002738 return new Allocation(id, rs, t, true, usage, MipmapControl.MIPMAP_NONE);
Chris Craik06d29842015-06-02 17:19:24 -07002739 } finally {
2740 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07002741 }
Jason Sams5476b452010-12-08 16:14:36 -08002742 }
2743
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002744 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002745 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002746 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002747 * @param rs Context to which the Allocation will belong.
2748 * @param e Element to use in the Allocation
2749 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002750 *
2751 * @return allocation
2752 */
Jason Sams5476b452010-12-08 16:14:36 -08002753 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08002754 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07002755 }
2756
Jason Sams49a05d72010-12-29 14:31:29 -08002757 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08002758 final Bitmap.Config bc = b.getConfig();
2759 if (bc == Bitmap.Config.ALPHA_8) {
2760 return Element.A_8(rs);
2761 }
2762 if (bc == Bitmap.Config.ARGB_4444) {
2763 return Element.RGBA_4444(rs);
2764 }
2765 if (bc == Bitmap.Config.ARGB_8888) {
2766 return Element.RGBA_8888(rs);
2767 }
2768 if (bc == Bitmap.Config.RGB_565) {
2769 return Element.RGB_565(rs);
2770 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08002771 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08002772 }
2773
Jason Sams49a05d72010-12-29 14:31:29 -08002774 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002775 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08002776 Element e = elementFromBitmap(rs, b);
2777 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002778 tb.setX(b.getWidth());
2779 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08002780 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08002781 return tb.create();
2782 }
2783
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002784 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002785 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002786 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002787 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07002788 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002789 * @param mips specifies desired mipmap behaviour for the
2790 * allocation
2791 * @param usage bit field specifying how the allocation is
2792 * utilized
2793 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002794 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002795 *
2796 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002797 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002798 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002799 int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07002800 try {
2801 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
2802 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08002803
Chris Craik06d29842015-06-02 17:19:24 -07002804 // WAR undocumented color formats
2805 if (b.getConfig() == null) {
2806 if ((usage & USAGE_SHARED) != 0) {
2807 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
2808 }
2809 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
2810 Canvas c = new Canvas(newBitmap);
2811 c.drawBitmap(b, 0, 0, null);
2812 return createFromBitmap(rs, newBitmap, mips, usage);
Tim Murrayabd5db92013-02-28 11:45:22 -08002813 }
Tim Murrayabd5db92013-02-28 11:45:22 -08002814
Chris Craik06d29842015-06-02 17:19:24 -07002815 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08002816
Chris Craik06d29842015-06-02 17:19:24 -07002817 // enable optimized bitmap path only with no mipmap and script-only usage
2818 if (mips == MipmapControl.MIPMAP_NONE &&
2819 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
2820 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
2821 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
2822 if (id == 0) {
2823 throw new RSRuntimeException("Load failed.");
2824 }
2825
2826 // keep a reference to the Bitmap around to prevent GC
Yang Nie1798e42016-04-07 11:17:59 -07002827 Allocation alloc = new Allocation(id, rs, t, true, usage, mips);
Chris Craik06d29842015-06-02 17:19:24 -07002828 alloc.setBitmap(b);
2829 return alloc;
2830 }
2831
2832
2833 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08002834 if (id == 0) {
2835 throw new RSRuntimeException("Load failed.");
2836 }
Yang Nie1798e42016-04-07 11:17:59 -07002837 return new Allocation(id, rs, t, true, usage, mips);
Chris Craik06d29842015-06-02 17:19:24 -07002838 } finally {
2839 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murraya3145512012-12-04 17:59:29 -08002840 }
Jason Sams5476b452010-12-08 16:14:36 -08002841 }
2842
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002843 /**
Miao Wang0facf022015-11-25 11:21:13 -08002844 * Gets or creates a ByteBuffer that contains the raw data of the current Allocation.
Miao Wang3231e8e2016-04-01 15:10:47 -07002845 * <p> If the Allocation is created with USAGE_IO_INPUT, the returned ByteBuffer
Miao Wang0facf022015-11-25 11:21:13 -08002846 * would contain the up-to-date data as READ ONLY.
2847 * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
2848 * the Allocation has certain alignment. The size of each row including padding,
2849 * called stride, can be queried using the {@link #getStride()} method.
2850 *
2851 * Note: Operating on the ByteBuffer of a destroyed Allocation will triger errors.
2852 *
2853 * @return ByteBuffer The ByteBuffer associated with raw data pointer of the Allocation.
2854 */
2855 public ByteBuffer getByteBuffer() {
2856 // Create a new ByteBuffer if it is not initialized or using IO_INPUT.
2857 if (mType.hasFaces()) {
2858 throw new RSInvalidStateException("Cubemap is not supported for getByteBuffer().");
2859 }
2860 if (mType.getYuv() == android.graphics.ImageFormat.NV21 ||
2861 mType.getYuv() == android.graphics.ImageFormat.YV12 ||
2862 mType.getYuv() == android.graphics.ImageFormat.YUV_420_888 ) {
2863 throw new RSInvalidStateException("YUV format is not supported for getByteBuffer().");
2864 }
2865 if (mByteBuffer == null || (mUsage & USAGE_IO_INPUT) != 0) {
2866 int xBytesSize = mType.getX() * mType.getElement().getBytesSize();
2867 long[] stride = new long[1];
2868 mByteBuffer = mRS.nAllocationGetByteBuffer(getID(mRS), stride, xBytesSize, mType.getY(), mType.getZ());
2869 mByteBufferStride = stride[0];
2870 }
2871 if ((mUsage & USAGE_IO_INPUT) != 0) {
2872 return mByteBuffer.asReadOnlyBuffer();
2873 }
2874 return mByteBuffer;
2875 }
2876
2877 /**
Miao Wang8c150922015-10-26 17:44:10 -07002878 * Creates a new Allocation Array with the given {@link
2879 * android.renderscript.Type}, and usage flags.
2880 * Note: If the input allocation is of usage: USAGE_IO_INPUT,
2881 * the created Allocation will be sharing the same BufferQueue.
2882 *
2883 * @param rs RenderScript context
2884 * @param t RenderScript type describing data layout
2885 * @param usage bit field specifying how the Allocation is
2886 * utilized
2887 * @param numAlloc Number of Allocations in the array.
2888 * @return Allocation[]
2889 */
2890 public static Allocation[] createAllocations(RenderScript rs, Type t, int usage, int numAlloc) {
2891 try {
2892 Trace.traceBegin(RenderScript.TRACE_TAG, "createAllocations");
2893 rs.validate();
2894 if (t.getID(rs) == 0) {
2895 throw new RSInvalidStateException("Bad Type");
2896 }
2897
2898 Allocation[] mAllocationArray = new Allocation[numAlloc];
2899 mAllocationArray[0] = createTyped(rs, t, usage);
2900 if ((usage & USAGE_IO_INPUT) != 0) {
2901 if (numAlloc > MAX_NUMBER_IO_INPUT_ALLOC) {
Yang Nic48a09c2017-04-28 08:49:01 -07002902 mAllocationArray[0].destroy();
Miao Wang8c150922015-10-26 17:44:10 -07002903 throw new RSIllegalArgumentException("Exceeds the max number of Allocations allowed: " +
2904 MAX_NUMBER_IO_INPUT_ALLOC);
2905 }
2906 mAllocationArray[0].setupBufferQueue(numAlloc);;
2907 }
2908
2909 for (int i=1; i<numAlloc; i++) {
Yang Nie1798e42016-04-07 11:17:59 -07002910 mAllocationArray[i] = createFromAllocation(rs, mAllocationArray[0]);
Miao Wang8c150922015-10-26 17:44:10 -07002911 }
2912 return mAllocationArray;
2913 } finally {
2914 Trace.traceEnd(RenderScript.TRACE_TAG);
2915 }
2916 }
2917
2918 /**
2919 * Creates a new Allocation with the given {@link
2920 * android.renderscript.Allocation}. The same data layout of
2921 * the input Allocation will be applied.
Miao Wang3231e8e2016-04-01 15:10:47 -07002922 * <p> If the input allocation is of usage: USAGE_IO_INPUT, the created
Miao Wang8c150922015-10-26 17:44:10 -07002923 * Allocation will be sharing the same BufferQueue.
2924 *
2925 * @param rs Context to which the allocation will belong.
2926 * @param alloc RenderScript Allocation describing data layout.
2927 * @return Allocation sharing the same data structure.
2928 */
Yang Nie1798e42016-04-07 11:17:59 -07002929 static Allocation createFromAllocation(RenderScript rs, Allocation alloc) {
Miao Wang8c150922015-10-26 17:44:10 -07002930 try {
2931 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromAllcation");
2932 rs.validate();
2933 if (alloc.getID(rs) == 0) {
2934 throw new RSInvalidStateException("Bad input Allocation");
2935 }
2936
2937 Type type = alloc.getType();
2938 int usage = alloc.getUsage();
2939 MipmapControl mips = alloc.getMipmap();
2940 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
2941 if (id == 0) {
2942 throw new RSRuntimeException("Allocation creation failed.");
2943 }
Yang Nie1798e42016-04-07 11:17:59 -07002944 Allocation outAlloc = new Allocation(id, rs, type, false, usage, mips);
Miao Wang8c150922015-10-26 17:44:10 -07002945 if ((usage & USAGE_IO_INPUT) != 0) {
2946 outAlloc.shareBufferQueue(alloc);
2947 }
2948 return outAlloc;
2949 } finally {
2950 Trace.traceEnd(RenderScript.TRACE_TAG);
2951 }
2952 }
2953
2954 /**
2955 * Initialize BufferQueue with specified max number of buffers.
2956 */
2957 void setupBufferQueue(int numAlloc) {
2958 mRS.validate();
2959 if ((mUsage & USAGE_IO_INPUT) == 0) {
2960 throw new RSInvalidStateException("Allocation is not USAGE_IO_INPUT.");
2961 }
2962 mRS.nAllocationSetupBufferQueue(getID(mRS), numAlloc);
2963 }
2964
2965 /**
2966 * Share the BufferQueue with another {@link #USAGE_IO_INPUT} Allocation.
2967 *
2968 * @param alloc Allocation to associate with allocation
2969 */
2970 void shareBufferQueue(Allocation alloc) {
2971 mRS.validate();
2972 if ((mUsage & USAGE_IO_INPUT) == 0) {
2973 throw new RSInvalidStateException("Allocation is not USAGE_IO_INPUT.");
2974 }
2975 mGetSurfaceSurface = alloc.getSurface();
2976 mRS.nAllocationShareBufferQueue(getID(mRS), alloc.getID(mRS));
2977 }
2978
2979 /**
Miao Wang0facf022015-11-25 11:21:13 -08002980 * Gets the stride of the Allocation.
2981 * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
2982 * the Allocation has certain alignment. The size of each row including such
2983 * padding is called stride.
2984 *
2985 * @return the stride. For 1D Allocation, the stride will be the number of
2986 * bytes of this Allocation. For 2D and 3D Allocations, the stride
2987 * will be the stride in X dimension measuring in bytes.
2988 */
2989 public long getStride() {
2990 if (mByteBufferStride == -1) {
2991 getByteBuffer();
2992 }
2993 return mByteBufferStride;
2994 }
2995
2996 /**
Miao Wang8c150922015-10-26 17:44:10 -07002997 * Get the timestamp for the most recent buffer held by this Allocation.
2998 * The timestamp is guaranteed to be unique and monotonically increasing.
2999 * Default value: -1. The timestamp will be updated after each {@link
3000 * #ioReceive ioReceive()} call.
3001 *
3002 * It can be used to identify the images by comparing the unique timestamps
3003 * when used with {@link android.hardware.camera2} APIs.
3004 * Example steps:
3005 * 1. Save {@link android.hardware.camera2.TotalCaptureResult} when the
3006 * capture is completed.
3007 * 2. Get the timestamp after {@link #ioReceive ioReceive()} call.
3008 * 3. Comparing totalCaptureResult.get(CaptureResult.SENSOR_TIMESTAMP) with
3009 * alloc.getTimeStamp().
3010 * @return long Timestamp associated with the buffer held by the Allocation.
3011 */
3012 public long getTimeStamp() {
3013 return mTimeStamp;
3014 }
3015
3016 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003017 * Returns the handle to a raw buffer that is being managed by the screen
3018 * compositor. This operation is only valid for Allocations with {@link
3019 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07003020 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07003021 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07003022 *
3023 */
3024 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08003025 if ((mUsage & USAGE_IO_INPUT) == 0) {
3026 throw new RSInvalidStateException("Allocation is not a surface texture.");
3027 }
Jason Sams1e68bac2015-03-17 16:36:55 -07003028
3029 if (mGetSurfaceSurface == null) {
3030 mGetSurfaceSurface = mRS.nAllocationGetSurface(getID(mRS));
3031 }
3032
3033 return mGetSurfaceSurface;
Jason Samsfb9aa9f2012-03-28 15:30:07 -07003034 }
3035
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003036 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003037 * Associate a {@link android.view.Surface} with this Allocation. This
3038 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07003039 *
3040 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08003041 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07003042 public void setSurface(Surface sur) {
3043 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08003044 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
3045 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
3046 }
3047
Jason Samse07694b2012-04-03 15:36:36 -07003048 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08003049 }
3050
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003051 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003052 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08003053 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07003054 * <p>With target API version 18 or greater, this Allocation will be created
3055 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
3056 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
3057 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003058 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003059 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003060 * @param b bitmap source for the allocation data
3061 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07003062 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003063 *
3064 */
Jason Sams6d8eb262010-12-15 01:41:00 -08003065 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08003066 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
3067 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07003068 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08003069 }
Jason Sams6d8eb262010-12-15 01:41:00 -08003070 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
3071 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08003072 }
3073
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003074 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003075 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
3076 * containing the horizontal list of cube faces. Each face must be a square,
3077 * have the same size as all other faces, and have a width that is a power
3078 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003079 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003080 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07003081 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003082 * format: right, left, top, bottom, front, back
3083 * @param mips specifies desired mipmap behaviour for the cubemap
3084 * @param usage bit field specifying how the cubemap is utilized
3085 *
3086 * @return allocation containing cubemap data
3087 *
3088 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003089 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08003090 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08003091 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003092 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08003093
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003094 int height = b.getHeight();
3095 int width = b.getWidth();
3096
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08003097 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003098 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
3099 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08003100 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003101 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003102 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08003103 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003104 if (!isPow2) {
3105 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
3106 }
3107
3108 Element e = elementFromBitmap(rs, b);
3109 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08003110 tb.setX(height);
3111 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08003112 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08003113 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003114 Type t = tb.create();
3115
Tim Murray460a0492013-11-19 12:45:54 -08003116 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003117 if(id == 0) {
3118 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
3119 }
Yang Nie1798e42016-04-07 11:17:59 -07003120 return new Allocation(id, rs, t, true, usage, mips);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08003121 }
3122
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003123 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003124 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
3125 * from a {@link android.graphics.Bitmap} containing the horizontal list of
3126 * cube faces. Each face must be a square, have the same size as all other
3127 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003128 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003129 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003130 * @param b bitmap with cubemap faces layed out in the following
3131 * format: right, left, top, bottom, front, back
3132 *
3133 * @return allocation containing cubemap data
3134 *
3135 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003136 static public Allocation createCubemapFromBitmap(RenderScript rs,
3137 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08003138 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08003139 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08003140 }
3141
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003142 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003143 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
3144 * objects containing the cube faces. Each face must be a square, have the
3145 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003146 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003147 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003148 * @param xpos cubemap face in the positive x direction
3149 * @param xneg cubemap face in the negative x direction
3150 * @param ypos cubemap face in the positive y direction
3151 * @param yneg cubemap face in the negative y direction
3152 * @param zpos cubemap face in the positive z direction
3153 * @param zneg cubemap face in the negative z direction
3154 * @param mips specifies desired mipmap behaviour for the cubemap
3155 * @param usage bit field specifying how the cubemap is utilized
3156 *
3157 * @return allocation containing cubemap data
3158 *
3159 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003160 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
3161 Bitmap xpos,
3162 Bitmap xneg,
3163 Bitmap ypos,
3164 Bitmap yneg,
3165 Bitmap zpos,
3166 Bitmap zneg,
3167 MipmapControl mips,
3168 int usage) {
3169 int height = xpos.getHeight();
3170 if (xpos.getWidth() != height ||
3171 xneg.getWidth() != height || xneg.getHeight() != height ||
3172 ypos.getWidth() != height || ypos.getHeight() != height ||
3173 yneg.getWidth() != height || yneg.getHeight() != height ||
3174 zpos.getWidth() != height || zpos.getHeight() != height ||
3175 zneg.getWidth() != height || zneg.getHeight() != height) {
3176 throw new RSIllegalArgumentException("Only square cube map faces supported");
3177 }
3178 boolean isPow2 = (height & (height - 1)) == 0;
3179 if (!isPow2) {
3180 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
3181 }
3182
3183 Element e = elementFromBitmap(rs, xpos);
3184 Type.Builder tb = new Type.Builder(rs, e);
3185 tb.setX(height);
3186 tb.setY(height);
3187 tb.setFaces(true);
3188 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
3189 Type t = tb.create();
3190 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
3191
3192 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07003193 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003194 adapter.copyFrom(xpos);
3195 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
3196 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07003197 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003198 adapter.copyFrom(ypos);
3199 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
3200 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07003201 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003202 adapter.copyFrom(zpos);
3203 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
3204 adapter.copyFrom(zneg);
3205
3206 return cubemap;
3207 }
3208
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003209 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003210 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
3211 * from 6 {@link android.graphics.Bitmap} objects containing the cube
3212 * faces. Each face must be a square, have the same size as all other faces,
3213 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003214 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003215 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003216 * @param xpos cubemap face in the positive x direction
3217 * @param xneg cubemap face in the negative x direction
3218 * @param ypos cubemap face in the positive y direction
3219 * @param yneg cubemap face in the negative y direction
3220 * @param zpos cubemap face in the positive z direction
3221 * @param zneg cubemap face in the negative z direction
3222 *
3223 * @return allocation containing cubemap data
3224 *
3225 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08003226 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
3227 Bitmap xpos,
3228 Bitmap xneg,
3229 Bitmap ypos,
3230 Bitmap yneg,
3231 Bitmap zpos,
3232 Bitmap zneg) {
3233 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
3234 zpos, zneg, MipmapControl.MIPMAP_NONE,
3235 USAGE_GRAPHICS_TEXTURE);
3236 }
3237
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003238 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003239 * Creates an Allocation from the Bitmap referenced
3240 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003241 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003242 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003243 * @param res application resources
3244 * @param id resource id to load the data from
3245 * @param mips specifies desired mipmap behaviour for the
3246 * allocation
3247 * @param usage bit field specifying how the allocation is
3248 * utilized
3249 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07003250 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003251 *
3252 */
Jason Sams5476b452010-12-08 16:14:36 -08003253 static public Allocation createFromBitmapResource(RenderScript rs,
3254 Resources res,
3255 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08003256 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08003257 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07003258
Jason Sams771bebb2009-12-07 12:40:12 -08003259 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07003260 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
3261 throw new RSIllegalArgumentException("Unsupported usage specified.");
3262 }
Jason Sams5476b452010-12-08 16:14:36 -08003263 Bitmap b = BitmapFactory.decodeResource(res, id);
3264 Allocation alloc = createFromBitmap(rs, b, mips, usage);
3265 b.recycle();
3266 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07003267 }
3268
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003269 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003270 * Creates a non-mipmapped Allocation to use as a graphics texture from the
3271 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003272 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07003273 * <p>With target API version 18 or greater, this allocation will be created
3274 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
3275 * target API version 17 or lower, this allocation will be created with
3276 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08003277 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003278 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003279 * @param res application resources
3280 * @param id resource id to load the data from
3281 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07003282 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003283 *
3284 */
Jason Sams5476b452010-12-08 16:14:36 -08003285 static public Allocation createFromBitmapResource(RenderScript rs,
3286 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08003287 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08003288 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
3289 return createFromBitmapResource(rs, res, id,
3290 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07003291 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08003292 }
Jason Sams6d8eb262010-12-15 01:41:00 -08003293 return createFromBitmapResource(rs, res, id,
3294 MipmapControl.MIPMAP_NONE,
3295 USAGE_GRAPHICS_TEXTURE);
3296 }
3297
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07003298 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003299 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003300 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08003301 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08003302 * @param str string to create the allocation from
3303 * @param usage bit field specifying how the allocaiton is
3304 * utilized
3305 *
3306 */
Jason Sams5476b452010-12-08 16:14:36 -08003307 static public Allocation createFromString(RenderScript rs,
3308 String str,
3309 int usage) {
3310 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07003311 byte[] allocArray = null;
3312 try {
3313 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08003314 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08003315 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07003316 return alloc;
3317 }
3318 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08003319 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07003320 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07003321 }
Jason Sams739c8262013-04-11 18:07:52 -07003322
3323 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003324 * Interface to handle notification when new buffers are available via
3325 * {@link #USAGE_IO_INPUT}. An application will receive one notification
3326 * when a buffer is available. Additional buffers will not trigger new
3327 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07003328 */
Jason Sams42ef2382013-08-29 13:30:59 -07003329 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07003330 public void onBufferAvailable(Allocation a);
3331 }
3332
3333 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07003334 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07003335 *
Jason Sams42ef2382013-08-29 13:30:59 -07003336 * @param callback instance of the OnBufferAvailableListener
3337 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07003338 */
Jason Sams42ef2382013-08-29 13:30:59 -07003339 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07003340 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08003341 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07003342 mBufferNotifier = callback;
3343 }
3344 }
3345
Tim Murrayb730d862014-08-18 16:14:24 -07003346 static void sendBufferNotification(long id) {
Jason Sams739c8262013-04-11 18:07:52 -07003347 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08003348 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07003349
3350 if ((a != null) && (a.mBufferNotifier != null)) {
3351 a.mBufferNotifier.onBufferAvailable(a);
3352 }
3353 }
3354 }
3355
Miao Wangf0f6e802015-02-03 17:16:43 -08003356 /**
3357 * For USAGE_IO_OUTPUT, destroy() implies setSurface(null).
3358 *
3359 */
3360 @Override
3361 public void destroy() {
3362 if((mUsage & USAGE_IO_OUTPUT) != 0) {
3363 setSurface(null);
3364 }
Yang Nie1798e42016-04-07 11:17:59 -07003365
3366 if (mType != null && mOwningType) {
3367 mType.destroy();
Yang Nie1798e42016-04-07 11:17:59 -07003368 }
3369
Miao Wangf0f6e802015-02-03 17:16:43 -08003370 super.destroy();
3371 }
Yang Nie1798e42016-04-07 11:17:59 -07003372
Jason Samsb8c5a842009-07-31 20:40:47 -07003373}