blob: f2ef06520b925a5d7e0e5ce2ab36453d1cde4f52 [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
Stephen Hines9069ee82012-02-13 18:25:54 -08002 * Copyright (C) 2008-2012 The Android Open Source Project
Jason Samsb8c5a842009-07-31 20:40:47 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
Miao Wang0facf022015-11-25 11:21:13 -080019import java.nio.ByteBuffer;
Jason Sams739c8262013-04-11 18:07:52 -070020import java.util.HashMap;
Miao Wang0facf022015-11-25 11:21:13 -080021
Jason Samsb8c5a842009-07-31 20:40:47 -070022import android.content.res.Resources;
23import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
Tim Murrayabd5db92013-02-28 11:45:22 -080025import android.graphics.Canvas;
Tim Murray6d7a53c2013-05-23 16:59:23 -070026import android.os.Trace;
Miao Wang0facf022015-11-25 11:21:13 -080027import android.util.Log;
28import android.view.Surface;
Jason Samsb8c5a842009-07-31 20:40:47 -070029
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070030/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070031 * <p> This class provides the primary method through which data is passed to
32 * and from RenderScript kernels. An Allocation provides the backing store for
33 * a given {@link android.renderscript.Type}. </p>
Jason Samsa23d4e72011-01-04 18:59:12 -080034 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070035 * <p>An Allocation also contains a set of usage flags that denote how the
36 * Allocation could be used. For example, an Allocation may have usage flags
37 * specifying that it can be used from a script as well as input to a {@link
38 * android.renderscript.Sampler}. A developer must synchronize across these
39 * different usages using {@link android.renderscript.Allocation#syncAll} in
40 * order to ensure that different users of the Allocation have a consistent view
41 * of memory. For example, in the case where an Allocation is used as the output
42 * of one kernel and as Sampler input in a later kernel, a developer must call
43 * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
44 * second kernel to ensure correctness.
Jason Samsa23d4e72011-01-04 18:59:12 -080045 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070046 * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
47 * more complex Element types, the {@link #copyFromUnchecked} methods can be
48 * used to copy from byte arrays or similar constructs.</p>
Jason Samsb8c5a842009-07-31 20:40:47 -070049 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080050 * <div class="special reference">
51 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070052 * <p>For more information about creating an application that uses RenderScript, read the
53 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080054 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070055 **/
Chris Craik06d29842015-06-02 17:19:24 -070056
Jason Samsb8c5a842009-07-31 20:40:47 -070057public class Allocation extends BaseObj {
Miao Wang8c150922015-10-26 17:44:10 -070058 private static final int MAX_NUMBER_IO_INPUT_ALLOC = 16;
59
Jason Sams43ee06852009-08-12 17:54:11 -070060 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080061 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080062 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070063 Allocation mAdaptedAllocation;
Tim Murray2f2472c2013-08-22 14:55:26 -070064 int mSize;
Miao Wang8c150922015-10-26 17:44:10 -070065 MipmapControl mMipmapControl;
Jason Samsba862d12011-07-07 15:24:42 -070066
Miao Wang8c150922015-10-26 17:44:10 -070067 long mTimeStamp = -1;
Jason Sams615e7ce2012-01-13 14:01:20 -080068 boolean mReadAllowed = true;
69 boolean mWriteAllowed = true;
Miao Wang87e908d2015-03-02 15:15:15 -080070 boolean mAutoPadding = false;
Jason Sams46ba27e32015-02-06 17:45:15 -080071 int mSelectedX;
Jason Samsba862d12011-07-07 15:24:42 -070072 int mSelectedY;
73 int mSelectedZ;
74 int mSelectedLOD;
Jason Sams46ba27e32015-02-06 17:45:15 -080075 int mSelectedArray[];
Jason Samsba862d12011-07-07 15:24:42 -070076 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
77
78 int mCurrentDimX;
79 int mCurrentDimY;
80 int mCurrentDimZ;
81 int mCurrentCount;
Tim Murray460a0492013-11-19 12:45:54 -080082 static HashMap<Long, Allocation> mAllocationMap =
83 new HashMap<Long, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070084 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070085
Jason Sams1e68bac2015-03-17 16:36:55 -070086 private Surface mGetSurfaceSurface = null;
Miao Wang0facf022015-11-25 11:21:13 -080087 private ByteBuffer mByteBuffer = null;
88 private long mByteBufferStride = -1;
Jason Sams1e68bac2015-03-17 16:36:55 -070089
Jason Sams3042d262013-11-25 18:28:33 -080090 private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
91 final Class c = d.getClass();
92 if (!c.isArray()) {
93 throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
94 }
95 final Class cmp = c.getComponentType();
96 if (!cmp.isPrimitive()) {
97 throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
98 }
99
100 if (cmp == Long.TYPE) {
101 if (checkType) {
102 validateIsInt64();
103 return mType.mElement.mType;
104 }
105 return Element.DataType.SIGNED_64;
106 }
107
108 if (cmp == Integer.TYPE) {
109 if (checkType) {
110 validateIsInt32();
111 return mType.mElement.mType;
112 }
113 return Element.DataType.SIGNED_32;
114 }
115
116 if (cmp == Short.TYPE) {
117 if (checkType) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800118 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -0800119 return mType.mElement.mType;
120 }
121 return Element.DataType.SIGNED_16;
122 }
123
124 if (cmp == Byte.TYPE) {
125 if (checkType) {
126 validateIsInt8();
127 return mType.mElement.mType;
128 }
129 return Element.DataType.SIGNED_8;
130 }
131
132 if (cmp == Float.TYPE) {
133 if (checkType) {
134 validateIsFloat32();
135 }
136 return Element.DataType.FLOAT_32;
137 }
138
139 if (cmp == Double.TYPE) {
140 if (checkType) {
141 validateIsFloat64();
142 }
143 return Element.DataType.FLOAT_64;
144 }
Pirama Arumuga Nainar3934dad2016-03-28 12:00:00 -0700145
146 throw new RSIllegalArgumentException("Parameter of type " + cmp.getSimpleName() +
147 "[] is not compatible with data type " + mType.mElement.mType.name() +
148 " of allocation");
Jason Sams3042d262013-11-25 18:28:33 -0800149 }
150
151
Tim Murrayc11e25c2013-04-09 11:01:01 -0700152 /**
153 * The usage of the Allocation. These signal to RenderScript where to place
154 * the Allocation in memory.
155 *
156 */
Jason Sams5476b452010-12-08 16:14:36 -0800157
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700158 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700159 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -0800160 */
Jason Sams5476b452010-12-08 16:14:36 -0800161 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800162
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700163 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700164 * The Allocation will be used as a texture source by one or more graphics
165 * programs.
Jason Samsf7086092011-01-12 13:28:37 -0800166 *
167 */
Jason Sams5476b452010-12-08 16:14:36 -0800168 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800169
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700170 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700171 * The Allocation will be used as a graphics mesh.
172 *
173 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800174 *
175 */
Jason Sams5476b452010-12-08 16:14:36 -0800176 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800177
178
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700179 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700180 * The Allocation will be used as the source of shader constants by one or
181 * more programs.
182 *
183 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800184 *
185 */
Jason Sams5476b452010-12-08 16:14:36 -0800186 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
187
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700188 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700189 * The Allocation will be used as a target for offscreen rendering
190 *
191 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700192 *
193 */
194 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
195
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700196 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700197 * The Allocation will be used as a {@link android.view.Surface}
198 * consumer. This usage will cause the Allocation to be created
199 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800200 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800201 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700202 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800203
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700204 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700205 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700206 * producer. The dimensions and format of the {@link
Jason Sams3a1b8e42013-09-24 15:18:52 -0700207 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700208 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800209 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800210 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700211 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700212
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700213 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700214 * The Allocation's backing store will be inherited from another object
215 * (usually a {@link android.graphics.Bitmap}); copying to or from the
216 * original source Bitmap will cause a synchronization rather than a full
217 * copy. {@link #syncAll} may also be used to synchronize the Allocation
218 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800219 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700220 * <p>This is set by default for allocations created with {@link
221 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800222 *
223 */
224 public static final int USAGE_SHARED = 0x0080;
225
226 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700227 * Controls mipmap behavior when using the bitmap creation and update
228 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800229 */
Jason Sams4ef66502010-12-10 16:03:15 -0800230 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700231 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700232 * No mipmaps will be generated and the type generated from the incoming
233 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800234 */
Jason Sams5476b452010-12-08 16:14:36 -0800235 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800236
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700237 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700238 * A full mipmap chain will be created in script memory. The Type of
239 * the Allocation will contain a full mipmap chain. On upload, the full
240 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800241 */
Jason Sams5476b452010-12-08 16:14:36 -0800242 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800243
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700244 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700245 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
246 * not contain mipmaps. On upload, the allocation data will contain a
247 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800248 */
Jason Sams5476b452010-12-08 16:14:36 -0800249 MIPMAP_ON_SYNC_TO_TEXTURE(2);
250
251 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800252 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800253 mID = id;
254 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700255 }
256
Jason Sams48fe5342011-07-08 13:52:30 -0700257
Tim Murray460a0492013-11-19 12:45:54 -0800258 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700259 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700260 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700261 }
Jason Samse07694b2012-04-03 15:36:36 -0700262 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700263 }
264
Jason Sams03d2d002012-03-23 13:51:56 -0700265
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700266 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700267 * Get the {@link android.renderscript.Element} of the {@link
268 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700269 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700270 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700271 *
272 */
273 public Element getElement() {
274 return mType.getElement();
275 }
276
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700277 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700278 * Get the usage flags of the Allocation.
279 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700280 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700281 *
282 */
283 public int getUsage() {
284 return mUsage;
285 }
286
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700287 /**
Miao Wang8c150922015-10-26 17:44:10 -0700288 * @hide
289 * Get the Mipmap control flag of the Allocation.
290 *
291 * @return the Mipmap control flag of the Allocation
292 *
293 */
294 public MipmapControl getMipmap() {
295 return mMipmapControl;
296 }
297
298 /**
Miao Wang87e908d2015-03-02 15:15:15 -0800299 * Enable/Disable AutoPadding for Vec3 elements.
Miao Wangd7ecab12015-03-26 18:00:15 -0700300 * By default: Diabled.
Miao Wang87e908d2015-03-02 15:15:15 -0800301 *
Miao Wang179e8b52015-04-15 17:44:32 -0700302 * @param useAutoPadding True: enable AutoPadding; False: disable AutoPadding
Miao Wang87e908d2015-03-02 15:15:15 -0800303 *
304 */
305 public void setAutoPadding(boolean useAutoPadding) {
306 mAutoPadding = useAutoPadding;
307 }
308
309 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700310 * Get the size of the Allocation in bytes.
311 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700312 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700313 *
314 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700315 public int getBytesSize() {
Tim Murray04f0d6e2013-12-17 17:15:25 -0800316 if (mType.mDimYuv != 0) {
317 return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
318 }
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700319 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700320 }
321
Jason Sams452a7662011-07-07 16:05:18 -0700322 private void updateCacheInfo(Type t) {
323 mCurrentDimX = t.getX();
324 mCurrentDimY = t.getY();
325 mCurrentDimZ = t.getZ();
326 mCurrentCount = mCurrentDimX;
327 if (mCurrentDimY > 1) {
328 mCurrentCount *= mCurrentDimY;
329 }
330 if (mCurrentDimZ > 1) {
331 mCurrentCount *= mCurrentDimZ;
332 }
333 }
Jason Samsba862d12011-07-07 15:24:42 -0700334
Tim Murraya3145512012-12-04 17:59:29 -0800335 private void setBitmap(Bitmap b) {
336 mBitmap = b;
337 }
338
Tim Murray460a0492013-11-19 12:45:54 -0800339 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700340 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800341 if ((usage & ~(USAGE_SCRIPT |
342 USAGE_GRAPHICS_TEXTURE |
343 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700344 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800345 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800346 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800347 USAGE_IO_OUTPUT |
348 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800349 throw new RSIllegalArgumentException("Unknown usage specified.");
350 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800351
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700352 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800353 mWriteAllowed = false;
354
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700355 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800356 USAGE_GRAPHICS_TEXTURE |
357 USAGE_SCRIPT)) != 0) {
358 throw new RSIllegalArgumentException("Invalid usage combination.");
359 }
360 }
Jason Sams9bf18922013-04-13 19:48:36 -0700361
Jason Sams5476b452010-12-08 16:14:36 -0800362 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800363 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700364
Jason Sams452a7662011-07-07 16:05:18 -0700365 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700366 // TODO: A3D doesn't have Type info during creation, so we can't
367 // calculate the size ahead of time. We can possibly add a method
368 // to update the size in the future if it seems reasonable.
369 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700370 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700371 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700372 try {
373 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
374 } catch (Exception e) {
375 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
376 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
377 }
378 }
379
Miao Wang8c150922015-10-26 17:44:10 -0700380 Allocation(long id, RenderScript rs, Type t, int usage, MipmapControl mips) {
381 this(id, rs, t, usage);
382 mMipmapControl = mips;
383 }
384
Tim Murray2f2472c2013-08-22 14:55:26 -0700385 protected void finalize() throws Throwable {
386 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
387 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700388 }
389
Jason Sams3042d262013-11-25 18:28:33 -0800390 private void validateIsInt64() {
391 if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
392 (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
393 return;
394 }
395 throw new RSIllegalArgumentException(
396 "64 bit integer source does not match allocation type " + mType.mElement.mType);
397 }
398
Jason Samsb97b2512011-01-16 15:04:08 -0800399 private void validateIsInt32() {
400 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
401 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
402 return;
403 }
404 throw new RSIllegalArgumentException(
405 "32 bit integer source does not match allocation type " + mType.mElement.mType);
406 }
407
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800408 private void validateIsInt16OrFloat16() {
Jason Samsb97b2512011-01-16 15:04:08 -0800409 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800410 (mType.mElement.mType == Element.DataType.UNSIGNED_16) ||
411 (mType.mElement.mType == Element.DataType.FLOAT_16)) {
Jason Samsb97b2512011-01-16 15:04:08 -0800412 return;
413 }
414 throw new RSIllegalArgumentException(
415 "16 bit integer source does not match allocation type " + mType.mElement.mType);
416 }
417
418 private void validateIsInt8() {
419 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
420 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
421 return;
422 }
423 throw new RSIllegalArgumentException(
424 "8 bit integer source does not match allocation type " + mType.mElement.mType);
425 }
426
427 private void validateIsFloat32() {
428 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
429 return;
430 }
431 throw new RSIllegalArgumentException(
432 "32 bit float source does not match allocation type " + mType.mElement.mType);
433 }
434
Jason Sams3042d262013-11-25 18:28:33 -0800435 private void validateIsFloat64() {
436 if (mType.mElement.mType == Element.DataType.FLOAT_64) {
437 return;
438 }
439 throw new RSIllegalArgumentException(
440 "64 bit float source does not match allocation type " + mType.mElement.mType);
441 }
442
Jason Samsb97b2512011-01-16 15:04:08 -0800443 private void validateIsObject() {
444 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
445 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
446 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
447 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
448 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
449 (mType.mElement.mType == Element.DataType.RS_MESH) ||
450 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
451 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
452 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
453 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
454 return;
455 }
456 throw new RSIllegalArgumentException(
457 "Object source does not match allocation type " + mType.mElement.mType);
458 }
459
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700460 @Override
461 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800462 super.updateFromNative();
Tim Murray460a0492013-11-19 12:45:54 -0800463 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700464 if(typeID != 0) {
465 mType = new Type(typeID, mRS);
466 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700467 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700468 }
469 }
470
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700471 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700472 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700473 *
474 * @return Type
475 *
476 */
Jason Samsea87e962010-01-12 12:12:28 -0800477 public Type getType() {
478 return mType;
479 }
480
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700481 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700482 * Propagate changes from one usage of the Allocation to the
483 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700484 *
485 */
Jason Sams5476b452010-12-08 16:14:36 -0800486 public void syncAll(int srcLocation) {
Chris Craik06d29842015-06-02 17:19:24 -0700487 try {
488 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
489 switch (srcLocation) {
490 case USAGE_GRAPHICS_TEXTURE:
491 case USAGE_SCRIPT:
492 if ((mUsage & USAGE_SHARED) != 0) {
493 copyFrom(mBitmap);
494 }
495 break;
496 case USAGE_GRAPHICS_CONSTANTS:
497 case USAGE_GRAPHICS_VERTEX:
498 break;
499 case USAGE_SHARED:
500 if ((mUsage & USAGE_SHARED) != 0) {
501 copyTo(mBitmap);
502 }
503 break;
504 default:
505 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
Tim Murray78e64942013-04-09 17:28:56 -0700506 }
Chris Craik06d29842015-06-02 17:19:24 -0700507 mRS.validate();
508 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
509 } finally {
510 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800511 }
Jason Sams5476b452010-12-08 16:14:36 -0800512 }
513
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700514 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700515 * Send a buffer to the output stream. The contents of the Allocation will
516 * be undefined after this operation. This operation is only valid if {@link
517 * #USAGE_IO_OUTPUT} is set on the Allocation.
518 *
Jason Sams163766c2012-02-15 12:04:24 -0800519 *
Jason Sams163766c2012-02-15 12:04:24 -0800520 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700521 public void ioSend() {
Chris Craik06d29842015-06-02 17:19:24 -0700522 try {
523 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
524 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
525 throw new RSIllegalArgumentException(
526 "Can only send buffer if IO_OUTPUT usage specified.");
527 }
528 mRS.validate();
529 mRS.nAllocationIoSend(getID(mRS));
530 } finally {
531 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800532 }
Jason Sams163766c2012-02-15 12:04:24 -0800533 }
534
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700535 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700536 * Receive the latest input into the Allocation. This operation
537 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800538 *
Jason Sams163766c2012-02-15 12:04:24 -0800539 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700540 public void ioReceive() {
Chris Craik06d29842015-06-02 17:19:24 -0700541 try {
542 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
543 if ((mUsage & USAGE_IO_INPUT) == 0) {
544 throw new RSIllegalArgumentException(
545 "Can only receive if IO_INPUT usage specified.");
546 }
547 mRS.validate();
Miao Wang8c150922015-10-26 17:44:10 -0700548 mTimeStamp = mRS.nAllocationIoReceive(getID(mRS));
Chris Craik06d29842015-06-02 17:19:24 -0700549 } finally {
550 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800551 }
Jason Sams163766c2012-02-15 12:04:24 -0800552 }
553
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700554 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700555 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700556 *
557 * @param d Source array.
558 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800559 public void copyFrom(BaseObj[] d) {
Chris Craik06d29842015-06-02 17:19:24 -0700560 try {
561 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
562 mRS.validate();
563 validateIsObject();
564 if (d.length != mCurrentCount) {
565 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
566 mCurrentCount + ", array length = " + d.length);
567 }
Tim Murray460a0492013-11-19 12:45:54 -0800568
Chris Craik06d29842015-06-02 17:19:24 -0700569 if (RenderScript.sPointerSize == 8) {
570 long i[] = new long[d.length * 4];
571 for (int ct=0; ct < d.length; ct++) {
572 i[ct * 4] = d[ct].getID(mRS);
573 }
574 copy1DRangeFromUnchecked(0, mCurrentCount, i);
575 } else {
576 int i[] = new int[d.length];
577 for (int ct=0; ct < d.length; ct++) {
578 i[ct] = (int) d[ct].getID(mRS);
579 }
580 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray3de3dc72014-07-01 16:56:18 -0700581 }
Chris Craik06d29842015-06-02 17:19:24 -0700582 } finally {
583 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800584 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700585 }
586
Jason Samsfb9f82c2011-01-12 14:53:25 -0800587 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800588 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800589 if (bc == null) {
590 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
591 }
Jason Sams252c0782011-01-11 17:42:52 -0800592 switch (bc) {
593 case ALPHA_8:
594 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
595 throw new RSIllegalArgumentException("Allocation kind is " +
596 mType.getElement().mKind + ", type " +
597 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700598 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800599 " bytes, passed bitmap was " + bc);
600 }
601 break;
602 case ARGB_8888:
603 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700604 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800605 throw new RSIllegalArgumentException("Allocation kind is " +
606 mType.getElement().mKind + ", type " +
607 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700608 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800609 " bytes, passed bitmap was " + bc);
610 }
611 break;
612 case RGB_565:
613 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700614 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800615 throw new RSIllegalArgumentException("Allocation kind is " +
616 mType.getElement().mKind + ", type " +
617 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700618 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800619 " bytes, passed bitmap was " + bc);
620 }
621 break;
622 case ARGB_4444:
623 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700624 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800625 throw new RSIllegalArgumentException("Allocation kind is " +
626 mType.getElement().mKind + ", type " +
627 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700628 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800629 " bytes, passed bitmap was " + bc);
630 }
631 break;
632
633 }
Jason Sams4ef66502010-12-10 16:03:15 -0800634 }
635
Jason Samsfb9f82c2011-01-12 14:53:25 -0800636 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700637 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800638 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
639 }
640 }
641
Jason Sams3042d262013-11-25 18:28:33 -0800642 private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -0700643 try {
644 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
645 mRS.validate();
646 if (mCurrentDimZ > 0) {
647 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
648 } else if (mCurrentDimY > 0) {
649 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
650 } else {
651 copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
652 }
653 } finally {
654 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams3042d262013-11-25 18:28:33 -0800655 }
Jason Sams3042d262013-11-25 18:28:33 -0800656 }
657
658 /**
659 * Copy into this Allocation from an array. This method does not guarantee
660 * that the Allocation is compatible with the input buffer; it copies memory
661 * without reinterpretation.
662 *
663 * @param array The source data array
664 */
665 public void copyFromUnchecked(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700666 try {
667 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
668 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
669 java.lang.reflect.Array.getLength(array));
670 } finally {
671 Trace.traceEnd(RenderScript.TRACE_TAG);
672 }
Jason Sams3042d262013-11-25 18:28:33 -0800673 }
674
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700675 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700676 * Copy into this Allocation from an array. This method does not guarantee
677 * that the Allocation is compatible with the input buffer; it copies memory
678 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800679 *
680 * @param d the source data array
681 */
682 public void copyFromUnchecked(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800683 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800684 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700685
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700686 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700687 * Copy into this Allocation from an array. This method does not guarantee
688 * that the Allocation is compatible with the input buffer; it copies memory
689 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800690 *
691 * @param d the source data array
692 */
693 public void copyFromUnchecked(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800694 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800695 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700696
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700697 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700698 * Copy into this Allocation from an array. This method does not guarantee
699 * that the Allocation is compatible with the input buffer; it copies memory
700 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800701 *
702 * @param d the source data array
703 */
704 public void copyFromUnchecked(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800705 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800706 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700707
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700708 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700709 * Copy into this Allocation from an array. This method does not guarantee
710 * that the Allocation is compatible with the input buffer; it copies memory
711 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800712 *
713 * @param d the source data array
714 */
715 public void copyFromUnchecked(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800716 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800717 }
718
Tim Murray6d7a53c2013-05-23 16:59:23 -0700719
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700720 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700721 * Copy into this Allocation from an array. This variant is type checked
722 * and will generate exceptions if the Allocation's {@link
Jason Sams3042d262013-11-25 18:28:33 -0800723 * android.renderscript.Element} does not match the array's
724 * primitive type.
725 *
Ying Wang16229812013-11-26 15:45:12 -0800726 * @param array The source data array
Jason Sams3042d262013-11-25 18:28:33 -0800727 */
728 public void copyFrom(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700729 try {
730 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
731 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
732 java.lang.reflect.Array.getLength(array));
733 } finally {
734 Trace.traceEnd(RenderScript.TRACE_TAG);
735 }
Jason Sams3042d262013-11-25 18:28:33 -0800736 }
737
738 /**
739 * Copy into this Allocation from an array. This variant is type checked
740 * and will generate exceptions if the Allocation's {@link
Tim Murrayc11e25c2013-04-09 11:01:01 -0700741 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800742 *
743 * @param d the source data array
744 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800745 public void copyFrom(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800746 validateIsInt32();
747 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800748 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800749
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700750 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700751 * Copy into this Allocation from an array. This variant is type checked
752 * and will generate exceptions if the Allocation's {@link
753 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800754 *
755 * @param d the source data array
756 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800757 public void copyFrom(short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800758 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -0800759 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800760 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800761
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700762 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700763 * Copy into this Allocation from an array. This variant is type checked
764 * and will generate exceptions if the Allocation's {@link
765 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800766 *
767 * @param d the source data array
768 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800769 public void copyFrom(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800770 validateIsInt8();
771 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800772 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800773
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700774 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700775 * Copy into this Allocation from an array. This variant is type checked
776 * and will generate exceptions if the Allocation's {@link
777 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800778 *
779 * @param d the source data array
780 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800781 public void copyFrom(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800782 validateIsFloat32();
783 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800784 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800785
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700786 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700787 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
788 * height, width, and format of the bitmap must match the existing
789 * allocation.
790 *
791 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
792 * android.graphics.Bitmap} used to create the Allocation with {@link
793 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
794 * this will synchronize the Allocation with the latest data from the {@link
795 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800796 *
797 * @param b the source bitmap
798 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800799 public void copyFrom(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -0700800 try {
801 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
802 mRS.validate();
803 if (b.getConfig() == null) {
804 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
805 Canvas c = new Canvas(newBitmap);
806 c.drawBitmap(b, 0, 0, null);
807 copyFrom(newBitmap);
808 return;
809 }
810 validateBitmapSize(b);
811 validateBitmapFormat(b);
812 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
813 } finally {
814 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -0800815 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700816 }
817
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700818 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700819 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700820 * must be identical.
821 *
822 * @param a the source allocation
823 */
824 public void copyFrom(Allocation a) {
Chris Craik06d29842015-06-02 17:19:24 -0700825 try {
826 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
827 mRS.validate();
828 if (!mType.equals(a.getType())) {
829 throw new RSIllegalArgumentException("Types of allocations must match.");
830 }
831 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
832 } finally {
833 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700834 }
Tim Murrayf671fb02012-10-03 13:50:05 -0700835 }
836
Tim Murrayf671fb02012-10-03 13:50:05 -0700837 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700838 * This is only intended to be used by auto-generated code reflected from
839 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800840 *
841 * @param xoff
842 * @param fp
843 */
Jason Sams21b41032011-01-16 15:05:41 -0800844 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800845 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700846 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700847 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700848 int data_length = fp.getPos();
Jason Samsa70f4162010-03-26 15:33:42 -0700849
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700850 int count = data_length / eSize;
851 if ((eSize * count) != data_length) {
852 throw new RSIllegalArgumentException("Field packer length " + data_length +
Jason Samsa70f4162010-03-26 15:33:42 -0700853 " not divisible by element size " + eSize + ".");
854 }
Jason Samsba862d12011-07-07 15:24:42 -0700855 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700856 }
857
Miao Wang45cec0a2015-03-04 16:40:21 -0800858
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700859 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700860 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -0800861 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800862 *
863 * @param xoff
864 * @param component_number
865 * @param fp
866 */
Jason Sams21b41032011-01-16 15:05:41 -0800867 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800868 setFromFieldPacker(xoff, 0, 0, component_number, fp);
869 }
870
871 /**
Miao Wangc8e237e2015-02-20 18:36:32 -0800872 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -0800873 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -0800874 *
875 * @param xoff
876 * @param yoff
Miao Wangc8e237e2015-02-20 18:36:32 -0800877 * @param zoff
878 * @param component_number
879 * @param fp
880 */
881 public void setFromFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800882 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700883 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800884 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700885 }
886 if(xoff < 0) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800887 throw new RSIllegalArgumentException("Offset x must be >= 0.");
888 }
889 if(yoff < 0) {
890 throw new RSIllegalArgumentException("Offset y must be >= 0.");
891 }
892 if(zoff < 0) {
893 throw new RSIllegalArgumentException("Offset z must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700894 }
895
896 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700897 int data_length = fp.getPos();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700898 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800899 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700900
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700901 if (data_length != eSize) {
902 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700903 " does not match component size " + eSize + ".");
904 }
905
Miao Wangc8e237e2015-02-20 18:36:32 -0800906 mRS.nAllocationElementData(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
907 component_number, data, data_length);
Jason Samsa70f4162010-03-26 15:33:42 -0700908 }
909
Miao Wang87e908d2015-03-02 15:15:15 -0800910 private void data1DChecks(int off, int count, int len, int dataSize, boolean usePadding) {
Jason Sams771bebb2009-12-07 12:40:12 -0800911 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700912 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800913 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700914 }
915 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800916 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700917 }
Jason Samsba862d12011-07-07 15:24:42 -0700918 if((off + count) > mCurrentCount) {
919 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700920 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700921 }
Miao Wang87e908d2015-03-02 15:15:15 -0800922 if(usePadding) {
923 if(len < dataSize / 4 * 3) {
924 throw new RSIllegalArgumentException("Array too small for allocation type.");
925 }
926 } else {
927 if(len < dataSize) {
928 throw new RSIllegalArgumentException("Array too small for allocation type.");
929 }
Jason Sams768bc022009-09-21 19:41:04 -0700930 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700931 }
932
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700933 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700934 * Generate a mipmap chain. This is only valid if the Type of the Allocation
935 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800936 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700937 * <p>This function will generate a complete set of mipmaps from the top
938 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800939 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700940 * <p>If the Allocation is also using other memory spaces, a call to {@link
941 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800942 */
943 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700944 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800945 }
946
Jason Sams3042d262013-11-25 18:28:33 -0800947 private void copy1DRangeFromUnchecked(int off, int count, Object array,
948 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -0700949 try {
950 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
951 final int dataSize = mType.mElement.getBytesSize() * count;
952 // AutoPadding for Vec3 Element
953 boolean usePadding = false;
954 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
955 usePadding = true;
956 }
957 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
958 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
959 mType.mElement.mType.mSize, usePadding);
960 } finally {
961 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -0800962 }
Jason Sams3042d262013-11-25 18:28:33 -0800963 }
964
965 /**
966 * Copy an array into part of this Allocation. This method does not
967 * guarantee that the Allocation is compatible with the input buffer.
968 *
969 * @param off The offset of the first element to be copied.
970 * @param count The number of elements to be copied.
971 * @param array The source data array
972 */
973 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
974 copy1DRangeFromUnchecked(off, count, array,
975 validateObjectIsPrimitiveArray(array, false),
976 java.lang.reflect.Array.getLength(array));
977 }
978
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700979 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700980 * Copy an array into part of this Allocation. This method does not
981 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800982 *
983 * @param off The offset of the first element to be copied.
984 * @param count The number of elements to be copied.
985 * @param d the source data array
986 */
987 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800988 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700989 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700990
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700991 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700992 * Copy an array into part of this Allocation. This method does not
993 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800994 *
995 * @param off The offset of the first element to be copied.
996 * @param count The number of elements to be copied.
997 * @param d the source data array
998 */
999 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001000 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -07001001 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001002
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001003 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001004 * Copy an array into part of this Allocation. This method does not
1005 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001006 *
1007 * @param off The offset of the first element to be copied.
1008 * @param count The number of elements to be copied.
1009 * @param d the source data array
1010 */
1011 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001012 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -07001013 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001014
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001015 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001016 * Copy an array into part of this Allocation. This method does not
1017 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001018 *
1019 * @param off The offset of the first element to be copied.
1020 * @param count The number of elements to be copied.
1021 * @param d the source data array
1022 */
1023 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001024 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
1025 }
1026
1027
1028 /**
1029 * Copy an array into part of this Allocation. This variant is type checked
1030 * and will generate exceptions if the Allocation type does not
1031 * match the component type of the array passed in.
1032 *
1033 * @param off The offset of the first element to be copied.
1034 * @param count The number of elements to be copied.
1035 * @param array The source data array.
1036 */
1037 public void copy1DRangeFrom(int off, int count, Object array) {
1038 copy1DRangeFromUnchecked(off, count, array,
1039 validateObjectIsPrimitiveArray(array, true),
1040 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -07001041 }
1042
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001043 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001044 * Copy an array into part of this Allocation. This variant is type checked
1045 * and will generate exceptions if the Allocation type is not a 32 bit
1046 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001047 *
1048 * @param off The offset of the first element to be copied.
1049 * @param count The number of elements to be copied.
1050 * @param d the source data array
1051 */
Jason Samsb97b2512011-01-16 15:04:08 -08001052 public void copy1DRangeFrom(int off, int count, int[] d) {
1053 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001054 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001055 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001056
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001057 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001058 * Copy an array into part of this Allocation. This variant is type checked
1059 * and will generate exceptions if the Allocation type is not a 16 bit
1060 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001061 *
1062 * @param off The offset of the first element to be copied.
1063 * @param count The number of elements to be copied.
1064 * @param d the source data array
1065 */
Jason Samsb97b2512011-01-16 15:04:08 -08001066 public void copy1DRangeFrom(int off, int count, short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001067 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -08001068 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001069 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001070
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001071 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001072 * Copy an array into part of this Allocation. This variant is type checked
1073 * and will generate exceptions if the Allocation type is not an 8 bit
1074 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001075 *
1076 * @param off The offset of the first element to be copied.
1077 * @param count The number of elements to be copied.
1078 * @param d the source data array
1079 */
Jason Samsb97b2512011-01-16 15:04:08 -08001080 public void copy1DRangeFrom(int off, int count, byte[] d) {
1081 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001082 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001083 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001084
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001085 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001086 * Copy an array into part of this Allocation. This variant is type checked
1087 * and will generate exceptions if the Allocation type is not a 32 bit float
1088 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001089 *
1090 * @param off The offset of the first element to be copied.
1091 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001092 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001093 */
Jason Samsb97b2512011-01-16 15:04:08 -08001094 public void copy1DRangeFrom(int off, int count, float[] d) {
1095 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001096 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001097 }
Jason Sams3042d262013-11-25 18:28:33 -08001098
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001099 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001100 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001101 *
1102 * @param off The offset of the first element to be copied.
1103 * @param count The number of elements to be copied.
1104 * @param data the source data allocation.
1105 * @param dataOff off The offset of the first element in data to
1106 * be copied.
1107 */
1108 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001109 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -07001110 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001111 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001112 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001113 data.mSelectedLOD, data.mSelectedFace.mID);
Chris Craik5c705d62015-06-01 10:39:36 -07001114 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001115 }
1116
Jason Samsfb9f82c2011-01-12 14:53:25 -08001117 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001118 if (mAdaptedAllocation != null) {
1119
1120 } else {
1121
1122 if (xoff < 0 || yoff < 0) {
1123 throw new RSIllegalArgumentException("Offset cannot be negative.");
1124 }
1125 if (h < 0 || w < 0) {
1126 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1127 }
1128 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1129 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1130 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001131 }
1132 }
Jason Sams768bc022009-09-21 19:41:04 -07001133
Jason Sams3042d262013-11-25 18:28:33 -08001134 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1135 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001136 try {
1137 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
1138 mRS.validate();
1139 validate2DRange(xoff, yoff, w, h);
1140 final int dataSize = mType.mElement.getBytesSize() * w * h;
1141 // AutoPadding for Vec3 Element
1142 boolean usePadding = false;
1143 int sizeBytes = arrayLen * dt.mSize;
1144 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1145 if (dataSize / 4 * 3 > sizeBytes) {
1146 throw new RSIllegalArgumentException("Array too small for allocation type.");
1147 }
1148 usePadding = true;
1149 sizeBytes = dataSize;
1150 } else {
1151 if (dataSize > sizeBytes) {
1152 throw new RSIllegalArgumentException("Array too small for allocation type.");
1153 }
Miao Wang87e908d2015-03-02 15:15:15 -08001154 }
Chris Craik06d29842015-06-02 17:19:24 -07001155 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1156 array, sizeBytes, dt,
1157 mType.mElement.mType.mSize, usePadding);
1158 } finally {
1159 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001160 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001161 }
1162
Jason Sams3042d262013-11-25 18:28:33 -08001163 /**
1164 * Copy from an array into a rectangular region in this Allocation. The
1165 * array is assumed to be tightly packed.
1166 *
1167 * @param xoff X offset of the region to update in this Allocation
1168 * @param yoff Y offset of the region to update in this Allocation
1169 * @param w Width of the region to update
1170 * @param h Height of the region to update
Ying Wang16229812013-11-26 15:45:12 -08001171 * @param array Data to be placed into the Allocation
Jason Sams3042d262013-11-25 18:28:33 -08001172 */
1173 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001174 try {
1175 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1176 copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
1177 validateObjectIsPrimitiveArray(array, true),
1178 java.lang.reflect.Array.getLength(array));
1179 } finally {
1180 Trace.traceEnd(RenderScript.TRACE_TAG);
1181 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001182 }
1183
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001184 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001185 * Copy from an array into a rectangular region in this Allocation. The
1186 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -08001187 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001188 * @param xoff X offset of the region to update in this Allocation
1189 * @param yoff Y offset of the region to update in this Allocation
1190 * @param w Width of the region to update
1191 * @param h Height of the region to update
1192 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001193 */
1194 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001195 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001196 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1197 Element.DataType.SIGNED_8, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001198 }
1199
Tim Murrayc11e25c2013-04-09 11:01:01 -07001200 /**
1201 * Copy from an array into a rectangular region in this Allocation. The
1202 * array is assumed to be tightly packed.
1203 *
1204 * @param xoff X offset of the region to update in this Allocation
1205 * @param yoff Y offset of the region to update in this Allocation
1206 * @param w Width of the region to update
1207 * @param h Height of the region to update
1208 * @param data to be placed into the Allocation
1209 */
Jason Samsf7086092011-01-12 13:28:37 -08001210 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001211 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -08001212 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1213 Element.DataType.SIGNED_16, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001214 }
1215
Tim Murrayc11e25c2013-04-09 11:01:01 -07001216 /**
1217 * Copy from an array into a rectangular region in this Allocation. The
1218 * array is assumed to be tightly packed.
1219 *
1220 * @param xoff X offset of the region to update in this Allocation
1221 * @param yoff Y offset of the region to update in this Allocation
1222 * @param w Width of the region to update
1223 * @param h Height of the region to update
1224 * @param data to be placed into the Allocation
1225 */
Jason Samsf7086092011-01-12 13:28:37 -08001226 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001227 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001228 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1229 Element.DataType.SIGNED_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001230 }
1231
Tim Murrayc11e25c2013-04-09 11:01:01 -07001232 /**
1233 * Copy from an array into a rectangular region in this Allocation. The
1234 * array is assumed to be tightly packed.
1235 *
1236 * @param xoff X offset of the region to update in this Allocation
1237 * @param yoff Y offset of the region to update in this Allocation
1238 * @param w Width of the region to update
1239 * @param h Height of the region to update
1240 * @param data to be placed into the Allocation
1241 */
Jason Samsf7086092011-01-12 13:28:37 -08001242 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001243 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001244 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1245 Element.DataType.FLOAT_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001246 }
1247
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001248 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001249 * Copy a rectangular region from an Allocation into a rectangular region in
1250 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001251 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001252 * @param xoff X offset of the region in this Allocation
1253 * @param yoff Y offset of the region in this Allocation
1254 * @param w Width of the region to update.
1255 * @param h Height of the region to update.
1256 * @param data source Allocation.
1257 * @param dataXoff X offset in source Allocation
1258 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001259 */
1260 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1261 Allocation data, int dataXoff, int dataYoff) {
Chris Craik06d29842015-06-02 17:19:24 -07001262 try {
1263 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1264 mRS.validate();
1265 validate2DRange(xoff, yoff, w, h);
1266 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
1267 mSelectedLOD, mSelectedFace.mID,
1268 w, h, data.getID(mRS), dataXoff, dataYoff,
1269 data.mSelectedLOD, data.mSelectedFace.mID);
1270 } finally {
1271 Trace.traceEnd(RenderScript.TRACE_TAG);
1272 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001273 }
1274
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001275 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001276 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1277 * and width of the update will use the height and width of the {@link
1278 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001279 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001280 * @param xoff X offset of the region to update in this Allocation
1281 * @param yoff Y offset of the region to update in this Allocation
1282 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001283 */
1284 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Chris Craik5c705d62015-06-01 10:39:36 -07001285 try {
1286 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1287 mRS.validate();
1288 if (data.getConfig() == null) {
1289 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1290 Canvas c = new Canvas(newBitmap);
1291 c.drawBitmap(data, 0, 0, null);
1292 copy2DRangeFrom(xoff, yoff, newBitmap);
1293 return;
1294 }
1295 validateBitmapFormat(data);
1296 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
1297 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
1298 } finally {
1299 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -08001300 }
Jason Samsfa445b92011-01-07 17:00:07 -08001301 }
1302
Jason Samsb05d6892013-04-09 15:59:24 -07001303 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1304 if (mAdaptedAllocation != null) {
1305
1306 } else {
1307
1308 if (xoff < 0 || yoff < 0 || zoff < 0) {
1309 throw new RSIllegalArgumentException("Offset cannot be negative.");
1310 }
1311 if (h < 0 || w < 0 || d < 0) {
1312 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1313 }
1314 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1315 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1316 }
1317 }
1318 }
1319
1320 /**
Miao Wang258db502015-03-03 14:05:36 -08001321 * Copy a rectangular region from the array into the allocation.
1322 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001323 *
Miao Wang258db502015-03-03 14:05:36 -08001324 * The data type of the array is not required to be the same as
1325 * the element data type.
Jason Samsb05d6892013-04-09 15:59:24 -07001326 */
Jason Sams3042d262013-11-25 18:28:33 -08001327 private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1328 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001329 try {
1330 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
1331 mRS.validate();
1332 validate3DRange(xoff, yoff, zoff, w, h, d);
1333 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1334 // AutoPadding for Vec3 Element
1335 boolean usePadding = false;
1336 int sizeBytes = arrayLen * dt.mSize;
1337 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1338 if (dataSize / 4 * 3 > sizeBytes) {
1339 throw new RSIllegalArgumentException("Array too small for allocation type.");
1340 }
1341 usePadding = true;
1342 sizeBytes = dataSize;
1343 } else {
1344 if (dataSize > sizeBytes) {
1345 throw new RSIllegalArgumentException("Array too small for allocation type.");
1346 }
Miao Wang87e908d2015-03-02 15:15:15 -08001347 }
Chris Craik06d29842015-06-02 17:19:24 -07001348 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1349 array, sizeBytes, dt,
1350 mType.mElement.mType.mSize, usePadding);
1351 } finally {
1352 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001353 }
Jason Samsb05d6892013-04-09 15:59:24 -07001354 }
1355
1356 /**
Jason Samsb05d6892013-04-09 15:59:24 -07001357 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001358 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001359 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001360 * @param xoff X offset of the region to update in this Allocation
1361 * @param yoff Y offset of the region to update in this Allocation
1362 * @param zoff Z offset of the region to update in this Allocation
1363 * @param w Width of the region to update
1364 * @param h Height of the region to update
1365 * @param d Depth of the region to update
Miao Wang87e908d2015-03-02 15:15:15 -08001366 * @param array to be placed into the allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001367 */
Jason Sams3042d262013-11-25 18:28:33 -08001368 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001369 try {
1370 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
1371 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
1372 validateObjectIsPrimitiveArray(array, true),
1373 java.lang.reflect.Array.getLength(array));
1374 } finally {
1375 Trace.traceEnd(RenderScript.TRACE_TAG);
1376 }
Jason Samsb05d6892013-04-09 15:59:24 -07001377 }
1378
1379 /**
Jason Samsb05d6892013-04-09 15:59:24 -07001380 * Copy a rectangular region into the allocation from another
1381 * allocation.
1382 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001383 * @param xoff X offset of the region to update in this Allocation
1384 * @param yoff Y offset of the region to update in this Allocation
1385 * @param zoff Z offset of the region to update in this Allocation
1386 * @param w Width of the region to update.
1387 * @param h Height of the region to update.
1388 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001389 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001390 * @param dataXoff X offset of the region in the source Allocation
1391 * @param dataYoff Y offset of the region in the source Allocation
1392 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001393 */
1394 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1395 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1396 mRS.validate();
1397 validate3DRange(xoff, yoff, zoff, w, h, d);
1398 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1399 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1400 data.mSelectedLOD);
1401 }
1402
Jason Samsfa445b92011-01-07 17:00:07 -08001403
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001404 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001405 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1406 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001407 *
1408 * @param b The bitmap to be set from the Allocation.
1409 */
Jason Samsfa445b92011-01-07 17:00:07 -08001410 public void copyTo(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -07001411 try {
1412 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1413 mRS.validate();
1414 validateBitmapFormat(b);
1415 validateBitmapSize(b);
1416 mRS.nAllocationCopyToBitmap(getID(mRS), b);
1417 } finally {
1418 Trace.traceEnd(RenderScript.TRACE_TAG);
1419 }
Jason Samsfa445b92011-01-07 17:00:07 -08001420 }
1421
Jason Sams3042d262013-11-25 18:28:33 -08001422 private void copyTo(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001423 try {
1424 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1425 mRS.validate();
1426 boolean usePadding = false;
1427 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1428 usePadding = true;
Miao Wangd9b63282015-04-03 09:15:39 -07001429 }
Chris Craik06d29842015-06-02 17:19:24 -07001430 if (usePadding) {
1431 if (dt.mSize * arrayLen < mSize / 4 * 3) {
1432 throw new RSIllegalArgumentException(
1433 "Size of output array cannot be smaller than size of allocation.");
1434 }
1435 } else {
1436 if (dt.mSize * arrayLen < mSize) {
1437 throw new RSIllegalArgumentException(
1438 "Size of output array cannot be smaller than size of allocation.");
1439 }
Miao Wangd9b63282015-04-03 09:15:39 -07001440 }
Chris Craik06d29842015-06-02 17:19:24 -07001441 mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
1442 } finally {
1443 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wangd9b63282015-04-03 09:15:39 -07001444 }
Jason Sams3042d262013-11-25 18:28:33 -08001445 }
1446
1447 /**
1448 * Copy from the Allocation into an array. The array must be at
1449 * least as large as the Allocation. The
1450 * {@link android.renderscript.Element} must match the component
1451 * type of the array passed in.
1452 *
1453 * @param array The array to be set from the Allocation.
1454 */
1455 public void copyTo(Object array) {
1456 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1457 java.lang.reflect.Array.getLength(array));
1458 }
1459
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001460 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001461 * Copy from the Allocation into a byte array. The array must be at least
1462 * as large as the Allocation. The allocation must be of an 8 bit integer
1463 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001464 *
1465 * @param d The array to be set from the Allocation.
1466 */
Jason Samsfa445b92011-01-07 17:00:07 -08001467 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001468 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001469 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001470 }
1471
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001472 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001473 * Copy from the Allocation into a short array. The array must be at least
1474 * as large as the Allocation. The allocation must be of an 16 bit integer
1475 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001476 *
1477 * @param d The array to be set from the Allocation.
1478 */
Jason Samsfa445b92011-01-07 17:00:07 -08001479 public void copyTo(short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001480 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -08001481 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001482 }
1483
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001484 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001485 * Copy from the Allocation into a int array. The array must be at least as
1486 * large as the Allocation. The allocation must be of an 32 bit integer
1487 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001488 *
1489 * @param d The array to be set from the Allocation.
1490 */
Jason Samsfa445b92011-01-07 17:00:07 -08001491 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001492 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001493 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001494 }
1495
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001496 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001497 * Copy from the Allocation into a float array. The array must be at least
1498 * as large as the Allocation. The allocation must be of an 32 bit float
1499 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001500 *
1501 * @param d The array to be set from the Allocation.
1502 */
Jason Samsfa445b92011-01-07 17:00:07 -08001503 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001504 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001505 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001506 }
1507
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001508 /**
Miao Wang3c613272015-05-11 11:41:55 -07001509 * @hide
1510 *
Miao Wang45cec0a2015-03-04 16:40:21 -08001511 * This is only intended to be used by auto-generated code reflected from
1512 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -08001513 *
1514 * @param xoff
1515 * @param yoff
1516 * @param zoff
1517 * @param component_number
Miao Wang258db502015-03-03 14:05:36 -08001518 * @param fp
Miao Wangc8e237e2015-02-20 18:36:32 -08001519 */
Miao Wang45cec0a2015-03-04 16:40:21 -08001520 public void copyToFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001521 mRS.validate();
1522 if (component_number >= mType.mElement.mElements.length) {
1523 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
1524 }
1525 if(xoff < 0) {
1526 throw new RSIllegalArgumentException("Offset x must be >= 0.");
1527 }
1528 if(yoff < 0) {
1529 throw new RSIllegalArgumentException("Offset y must be >= 0.");
1530 }
1531 if(zoff < 0) {
1532 throw new RSIllegalArgumentException("Offset z must be >= 0.");
1533 }
1534
Miao Wang45cec0a2015-03-04 16:40:21 -08001535 final byte[] data = fp.getData();
Miao Wangbfa5e652015-05-04 15:29:25 -07001536 int data_length = data.length;
Miao Wangc8e237e2015-02-20 18:36:32 -08001537 int eSize = mType.mElement.mElements[component_number].getBytesSize();
1538 eSize *= mType.mElement.mArraySizes[component_number];
1539
Miao Wang45cec0a2015-03-04 16:40:21 -08001540 if (data_length != eSize) {
1541 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
1542 " does not match component size " + eSize + ".");
Miao Wangc8e237e2015-02-20 18:36:32 -08001543 }
1544
1545 mRS.nAllocationElementRead(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Miao Wang45cec0a2015-03-04 16:40:21 -08001546 component_number, data, data_length);
Miao Wangc8e237e2015-02-20 18:36:32 -08001547 }
1548 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001549 * Resize a 1D allocation. The contents of the allocation are preserved.
1550 * If new elements are allocated objects are created with null contents and
1551 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001552 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001553 * <p>If the new region is smaller the references of any objects outside the
1554 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001555 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001556 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001557 *
1558 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001559 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001560 * @deprecated RenderScript objects should be immutable once created. The
Tim Murraycd38b762014-08-13 13:20:25 -07001561 * replacement is to create a new allocation and copy the contents. This
1562 * function will throw an exception if API 21 or higher is used.
Jason Samsf7086092011-01-12 13:28:37 -08001563 */
Jason Sams31a7e422010-10-26 13:09:17 -07001564 public synchronized void resize(int dimX) {
Tim Murraycd38b762014-08-13 13:20:25 -07001565 if (mRS.getApplicationContext().getApplicationInfo().targetSdkVersion >= 21) {
1566 throw new RSRuntimeException("Resize is not allowed in API 21+.");
1567 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001568 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001569 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001570 }
Jason Samse07694b2012-04-03 15:36:36 -07001571 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001572 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001573
Tim Murray460a0492013-11-19 12:45:54 -08001574 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001575 mType = new Type(typeID, mRS);
1576 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001577 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001578 }
1579
Miao Wangc8e237e2015-02-20 18:36:32 -08001580 private void copy1DRangeToUnchecked(int off, int count, Object array,
1581 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001582 try {
1583 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked");
1584 final int dataSize = mType.mElement.getBytesSize() * count;
1585 // AutoPadding for Vec3 Element
1586 boolean usePadding = false;
1587 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1588 usePadding = true;
1589 }
1590 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
1591 mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
1592 mType.mElement.mType.mSize, usePadding);
1593 } finally {
1594 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001595 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001596 }
1597
1598 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001599 * Copy part of this Allocation into an array. This method does not
1600 * guarantee that the Allocation is compatible with the input buffer.
1601 *
1602 * @param off The offset of the first element to be copied.
1603 * @param count The number of elements to be copied.
1604 * @param array The dest data array
1605 */
1606 public void copy1DRangeToUnchecked(int off, int count, Object array) {
1607 copy1DRangeToUnchecked(off, count, array,
1608 validateObjectIsPrimitiveArray(array, false),
1609 java.lang.reflect.Array.getLength(array));
1610 }
1611
1612 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001613 * Copy part of this Allocation into an array. This method does not
1614 * guarantee that the Allocation is compatible with the input buffer.
1615 *
1616 * @param off The offset of the first element to be copied.
1617 * @param count The number of elements to be copied.
1618 * @param d the source data array
1619 */
1620 public void copy1DRangeToUnchecked(int off, int count, int[] d) {
1621 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
1622 }
1623
1624 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001625 * Copy part of this Allocation into an array. This method does not
1626 * guarantee that the Allocation is compatible with the input buffer.
1627 *
1628 * @param off The offset of the first element to be copied.
1629 * @param count The number of elements to be copied.
1630 * @param d the source data array
1631 */
1632 public void copy1DRangeToUnchecked(int off, int count, short[] d) {
1633 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
1634 }
1635
1636 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001637 * Copy part of this Allocation into an array. This method does not
1638 * guarantee that the Allocation is compatible with the input buffer.
1639 *
1640 * @param off The offset of the first element to be copied.
1641 * @param count The number of elements to be copied.
1642 * @param d the source data array
1643 */
1644 public void copy1DRangeToUnchecked(int off, int count, byte[] d) {
1645 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
1646 }
1647
1648 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001649 * Copy part of this Allocation into an array. This method does not
1650 * guarantee that the Allocation is compatible with the input buffer.
1651 *
1652 * @param off The offset of the first element to be copied.
1653 * @param count The number of elements to be copied.
1654 * @param d the source data array
1655 */
1656 public void copy1DRangeToUnchecked(int off, int count, float[] d) {
1657 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
1658 }
1659
1660
1661 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001662 * Copy part of this Allocation into an array. This method does not
1663 * and will generate exceptions if the Allocation type does not
1664 * match the component type of the array passed in.
1665 *
1666 * @param off The offset of the first element to be copied.
1667 * @param count The number of elements to be copied.
1668 * @param array The source data array.
1669 */
1670 public void copy1DRangeTo(int off, int count, Object array) {
1671 copy1DRangeToUnchecked(off, count, array,
1672 validateObjectIsPrimitiveArray(array, true),
1673 java.lang.reflect.Array.getLength(array));
1674 }
1675
1676 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001677 * Copy part of this Allocation into an array. This method does not
1678 * and will generate exceptions if the Allocation type is not a 32 bit
1679 * integer type.
1680 *
1681 * @param off The offset of the first element to be copied.
1682 * @param count The number of elements to be copied.
1683 * @param d the source data array
1684 */
1685 public void copy1DRangeTo(int off, int count, int[] d) {
1686 validateIsInt32();
1687 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
1688 }
1689
1690 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001691 * Copy part of this Allocation into an array. This method does not
1692 * and will generate exceptions if the Allocation type is not a 16 bit
1693 * integer type.
1694 *
1695 * @param off The offset of the first element to be copied.
1696 * @param count The number of elements to be copied.
1697 * @param d the source data array
1698 */
1699 public void copy1DRangeTo(int off, int count, short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001700 validateIsInt16OrFloat16();
Miao Wangc8e237e2015-02-20 18:36:32 -08001701 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
1702 }
1703
1704 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001705 * Copy part of this Allocation into an array. This method does not
1706 * and will generate exceptions if the Allocation type is not an 8 bit
1707 * integer type.
1708 *
1709 * @param off The offset of the first element to be copied.
1710 * @param count The number of elements to be copied.
1711 * @param d the source data array
1712 */
1713 public void copy1DRangeTo(int off, int count, byte[] d) {
1714 validateIsInt8();
1715 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
1716 }
1717
1718 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001719 * Copy part of this Allocation into an array. This method does not
1720 * and will generate exceptions if the Allocation type is not a 32 bit float
1721 * type.
1722 *
1723 * @param off The offset of the first element to be copied.
1724 * @param count The number of elements to be copied.
1725 * @param d the source data array.
1726 */
1727 public void copy1DRangeTo(int off, int count, float[] d) {
1728 validateIsFloat32();
1729 copy1DRangeToUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
1730 }
1731
1732
1733 void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
1734 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001735 try {
1736 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked");
1737 mRS.validate();
1738 validate2DRange(xoff, yoff, w, h);
1739 final int dataSize = mType.mElement.getBytesSize() * w * h;
1740 // AutoPadding for Vec3 Element
1741 boolean usePadding = false;
1742 int sizeBytes = arrayLen * dt.mSize;
1743 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1744 if (dataSize / 4 * 3 > sizeBytes) {
1745 throw new RSIllegalArgumentException("Array too small for allocation type.");
1746 }
1747 usePadding = true;
1748 sizeBytes = dataSize;
1749 } else {
1750 if (dataSize > sizeBytes) {
1751 throw new RSIllegalArgumentException("Array too small for allocation type.");
1752 }
Miao Wang87e908d2015-03-02 15:15:15 -08001753 }
Chris Craik06d29842015-06-02 17:19:24 -07001754 mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1755 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
1756 } finally {
1757 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001758 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001759 }
1760
1761 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001762 * Copy from a rectangular region in this Allocation into an array.
1763 *
1764 * @param xoff X offset of the region to copy in this Allocation
1765 * @param yoff Y offset of the region to copy in this Allocation
1766 * @param w Width of the region to copy
1767 * @param h Height of the region to copy
1768 * @param array Dest Array to be copied into
1769 */
1770 public void copy2DRangeTo(int xoff, int yoff, int w, int h, Object array) {
1771 copy2DRangeToUnchecked(xoff, yoff, w, h, array,
1772 validateObjectIsPrimitiveArray(array, true),
1773 java.lang.reflect.Array.getLength(array));
1774 }
1775
1776 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001777 * Copy from a rectangular region in this Allocation into an array.
1778 *
1779 * @param xoff X offset of the region to copy in this Allocation
1780 * @param yoff Y offset of the region to copy in this Allocation
1781 * @param w Width of the region to copy
1782 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001783 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001784 */
1785 public void copy2DRangeTo(int xoff, int yoff, int w, int h, byte[] data) {
1786 validateIsInt8();
1787 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1788 Element.DataType.SIGNED_8, data.length);
1789 }
1790
1791 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001792 * Copy from a rectangular region in this Allocation into an array.
1793 *
1794 * @param xoff X offset of the region to copy in this Allocation
1795 * @param yoff Y offset of the region to copy in this Allocation
1796 * @param w Width of the region to copy
1797 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001798 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001799 */
1800 public void copy2DRangeTo(int xoff, int yoff, int w, int h, short[] data) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001801 validateIsInt16OrFloat16();
Miao Wangc8e237e2015-02-20 18:36:32 -08001802 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1803 Element.DataType.SIGNED_16, data.length);
1804 }
1805
1806 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001807 * Copy from a rectangular region in this Allocation into an array.
1808 *
1809 * @param xoff X offset of the region to copy in this Allocation
1810 * @param yoff Y offset of the region to copy in this Allocation
1811 * @param w Width of the region to copy
1812 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001813 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001814 */
1815 public void copy2DRangeTo(int xoff, int yoff, int w, int h, int[] data) {
1816 validateIsInt32();
1817 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1818 Element.DataType.SIGNED_32, data.length);
1819 }
1820
1821 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001822 * Copy from a rectangular region in this Allocation into an array.
1823 *
1824 * @param xoff X offset of the region to copy in this Allocation
1825 * @param yoff Y offset of the region to copy in this Allocation
1826 * @param w Width of the region to copy
1827 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001828 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001829 */
1830 public void copy2DRangeTo(int xoff, int yoff, int w, int h, float[] data) {
1831 validateIsFloat32();
1832 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1833 Element.DataType.FLOAT_32, data.length);
1834 }
1835
1836
1837 /**
Miao Wang258db502015-03-03 14:05:36 -08001838 * Copy from a rectangular region in this Allocation into an array.
1839 * The array is assumed to be tightly packed.
Miao Wangc8e237e2015-02-20 18:36:32 -08001840 *
Miao Wang258db502015-03-03 14:05:36 -08001841 * The data type of the array is not required to be the same as
1842 * the element data type.
Miao Wangc8e237e2015-02-20 18:36:32 -08001843 */
1844 private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1845 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001846 try {
1847 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked");
1848 mRS.validate();
1849 validate3DRange(xoff, yoff, zoff, w, h, d);
1850 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1851 // AutoPadding for Vec3 Element
1852 boolean usePadding = false;
1853 int sizeBytes = arrayLen * dt.mSize;
1854 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1855 if (dataSize / 4 * 3 > sizeBytes) {
1856 throw new RSIllegalArgumentException("Array too small for allocation type.");
1857 }
1858 usePadding = true;
1859 sizeBytes = dataSize;
1860 } else {
1861 if (dataSize > sizeBytes) {
1862 throw new RSIllegalArgumentException("Array too small for allocation type.");
1863 }
Miao Wang87e908d2015-03-02 15:15:15 -08001864 }
Chris Craik06d29842015-06-02 17:19:24 -07001865 mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1866 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
1867 } finally {
1868 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001869 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001870 }
1871
Miao Wang258db502015-03-03 14:05:36 -08001872 /*
Miao Wangc8e237e2015-02-20 18:36:32 -08001873 * Copy from a rectangular region in this Allocation into an array.
1874 *
1875 * @param xoff X offset of the region to copy in this Allocation
1876 * @param yoff Y offset of the region to copy in this Allocation
1877 * @param zoff Z offset of the region to copy in this Allocation
1878 * @param w Width of the region to copy
1879 * @param h Height of the region to copy
1880 * @param d Depth of the region to copy
1881 * @param array Dest Array to be copied into
1882 */
1883 public void copy3DRangeTo(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1884 copy3DRangeToUnchecked(xoff, yoff, zoff, w, h, d, array,
1885 validateObjectIsPrimitiveArray(array, true),
1886 java.lang.reflect.Array.getLength(array));
1887 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001888
1889 // creation
1890
Jason Sams49a05d72010-12-29 14:31:29 -08001891 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001892 static {
1893 mBitmapOptions.inScaled = false;
1894 }
1895
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001896 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001897 * Creates a new Allocation with the given {@link
1898 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001899 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001900 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001901 * @param mips specifies desired mipmap behaviour for the
1902 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001903 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001904 * utilized
1905 */
1906 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07001907 try {
1908 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
1909 rs.validate();
1910 if (type.getID(rs) == 0) {
1911 throw new RSInvalidStateException("Bad Type");
1912 }
1913 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
1914 if (id == 0) {
1915 throw new RSRuntimeException("Allocation creation failed.");
1916 }
Miao Wang8c150922015-10-26 17:44:10 -07001917 return new Allocation(id, rs, type, usage, mips);
Chris Craik06d29842015-06-02 17:19:24 -07001918 } finally {
1919 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams1bada8c2009-08-09 17:01:55 -07001920 }
Jason Sams857d0c72011-11-23 15:02:15 -08001921 }
1922
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001923 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001924 * Creates an Allocation with the size specified by the type and no mipmaps
1925 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001926 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001927 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001928 * @param type renderscript type describing data layout
1929 * @param usage bit field specifying how the allocation is
1930 * utilized
1931 *
1932 * @return allocation
1933 */
Jason Samse5d37122010-12-16 00:33:33 -08001934 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1935 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1936 }
1937
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001938 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001939 * Creates an Allocation for use by scripts with a given {@link
1940 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001941 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001942 * @param rs Context to which the Allocation will belong.
1943 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001944 *
1945 * @return allocation
1946 */
Jason Sams5476b452010-12-08 16:14:36 -08001947 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001948 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001949 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001950
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001951 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001952 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001953 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001954 * @param rs Context to which the Allocation will belong.
1955 * @param e Element to use in the Allocation
1956 * @param count the number of Elements in the Allocation
1957 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001958 * utilized
1959 *
1960 * @return allocation
1961 */
Jason Sams5476b452010-12-08 16:14:36 -08001962 static public Allocation createSized(RenderScript rs, Element e,
1963 int count, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07001964 try {
1965 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
1966 rs.validate();
1967 Type.Builder b = new Type.Builder(rs, e);
1968 b.setX(count);
1969 Type t = b.create();
Jason Sams768bc022009-09-21 19:41:04 -07001970
Chris Craik06d29842015-06-02 17:19:24 -07001971 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
1972 if (id == 0) {
1973 throw new RSRuntimeException("Allocation creation failed.");
1974 }
Miao Wang8c150922015-10-26 17:44:10 -07001975 return new Allocation(id, rs, t, usage, MipmapControl.MIPMAP_NONE);
Chris Craik06d29842015-06-02 17:19:24 -07001976 } finally {
1977 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001978 }
Jason Sams5476b452010-12-08 16:14:36 -08001979 }
1980
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001981 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001982 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001983 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001984 * @param rs Context to which the Allocation will belong.
1985 * @param e Element to use in the Allocation
1986 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001987 *
1988 * @return allocation
1989 */
Jason Sams5476b452010-12-08 16:14:36 -08001990 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001991 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001992 }
1993
Jason Sams49a05d72010-12-29 14:31:29 -08001994 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001995 final Bitmap.Config bc = b.getConfig();
1996 if (bc == Bitmap.Config.ALPHA_8) {
1997 return Element.A_8(rs);
1998 }
1999 if (bc == Bitmap.Config.ARGB_4444) {
2000 return Element.RGBA_4444(rs);
2001 }
2002 if (bc == Bitmap.Config.ARGB_8888) {
2003 return Element.RGBA_8888(rs);
2004 }
2005 if (bc == Bitmap.Config.RGB_565) {
2006 return Element.RGB_565(rs);
2007 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08002008 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08002009 }
2010
Jason Sams49a05d72010-12-29 14:31:29 -08002011 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002012 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08002013 Element e = elementFromBitmap(rs, b);
2014 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002015 tb.setX(b.getWidth());
2016 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08002017 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08002018 return tb.create();
2019 }
2020
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002021 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002022 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002023 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002024 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07002025 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002026 * @param mips specifies desired mipmap behaviour for the
2027 * allocation
2028 * @param usage bit field specifying how the allocation is
2029 * utilized
2030 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002031 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002032 *
2033 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002034 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002035 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002036 int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07002037 try {
2038 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
2039 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08002040
Chris Craik06d29842015-06-02 17:19:24 -07002041 // WAR undocumented color formats
2042 if (b.getConfig() == null) {
2043 if ((usage & USAGE_SHARED) != 0) {
2044 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
2045 }
2046 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
2047 Canvas c = new Canvas(newBitmap);
2048 c.drawBitmap(b, 0, 0, null);
2049 return createFromBitmap(rs, newBitmap, mips, usage);
Tim Murrayabd5db92013-02-28 11:45:22 -08002050 }
Tim Murrayabd5db92013-02-28 11:45:22 -08002051
Chris Craik06d29842015-06-02 17:19:24 -07002052 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08002053
Chris Craik06d29842015-06-02 17:19:24 -07002054 // enable optimized bitmap path only with no mipmap and script-only usage
2055 if (mips == MipmapControl.MIPMAP_NONE &&
2056 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
2057 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
2058 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
2059 if (id == 0) {
2060 throw new RSRuntimeException("Load failed.");
2061 }
2062
2063 // keep a reference to the Bitmap around to prevent GC
Miao Wang8c150922015-10-26 17:44:10 -07002064 Allocation alloc = new Allocation(id, rs, t, usage, mips);
Chris Craik06d29842015-06-02 17:19:24 -07002065 alloc.setBitmap(b);
2066 return alloc;
2067 }
2068
2069
2070 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08002071 if (id == 0) {
2072 throw new RSRuntimeException("Load failed.");
2073 }
Miao Wang8c150922015-10-26 17:44:10 -07002074 return new Allocation(id, rs, t, usage, mips);
Chris Craik06d29842015-06-02 17:19:24 -07002075 } finally {
2076 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murraya3145512012-12-04 17:59:29 -08002077 }
Jason Sams5476b452010-12-08 16:14:36 -08002078 }
2079
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002080 /**
Miao Wang0facf022015-11-25 11:21:13 -08002081 * Gets or creates a ByteBuffer that contains the raw data of the current Allocation.
2082 * If the Allocation is created with USAGE_IO_INPUT, the returned ByteBuffer
2083 * would contain the up-to-date data as READ ONLY.
2084 * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
2085 * the Allocation has certain alignment. The size of each row including padding,
2086 * called stride, can be queried using the {@link #getStride()} method.
2087 *
2088 * Note: Operating on the ByteBuffer of a destroyed Allocation will triger errors.
2089 *
2090 * @return ByteBuffer The ByteBuffer associated with raw data pointer of the Allocation.
2091 */
2092 public ByteBuffer getByteBuffer() {
2093 // Create a new ByteBuffer if it is not initialized or using IO_INPUT.
2094 if (mType.hasFaces()) {
2095 throw new RSInvalidStateException("Cubemap is not supported for getByteBuffer().");
2096 }
2097 if (mType.getYuv() == android.graphics.ImageFormat.NV21 ||
2098 mType.getYuv() == android.graphics.ImageFormat.YV12 ||
2099 mType.getYuv() == android.graphics.ImageFormat.YUV_420_888 ) {
2100 throw new RSInvalidStateException("YUV format is not supported for getByteBuffer().");
2101 }
2102 if (mByteBuffer == null || (mUsage & USAGE_IO_INPUT) != 0) {
2103 int xBytesSize = mType.getX() * mType.getElement().getBytesSize();
2104 long[] stride = new long[1];
2105 mByteBuffer = mRS.nAllocationGetByteBuffer(getID(mRS), stride, xBytesSize, mType.getY(), mType.getZ());
2106 mByteBufferStride = stride[0];
2107 }
2108 if ((mUsage & USAGE_IO_INPUT) != 0) {
2109 return mByteBuffer.asReadOnlyBuffer();
2110 }
2111 return mByteBuffer;
2112 }
2113
2114 /**
Miao Wang8c150922015-10-26 17:44:10 -07002115 * Creates a new Allocation Array with the given {@link
2116 * android.renderscript.Type}, and usage flags.
2117 * Note: If the input allocation is of usage: USAGE_IO_INPUT,
2118 * the created Allocation will be sharing the same BufferQueue.
2119 *
2120 * @param rs RenderScript context
2121 * @param t RenderScript type describing data layout
2122 * @param usage bit field specifying how the Allocation is
2123 * utilized
2124 * @param numAlloc Number of Allocations in the array.
2125 * @return Allocation[]
2126 */
2127 public static Allocation[] createAllocations(RenderScript rs, Type t, int usage, int numAlloc) {
2128 try {
2129 Trace.traceBegin(RenderScript.TRACE_TAG, "createAllocations");
2130 rs.validate();
2131 if (t.getID(rs) == 0) {
2132 throw new RSInvalidStateException("Bad Type");
2133 }
2134
2135 Allocation[] mAllocationArray = new Allocation[numAlloc];
2136 mAllocationArray[0] = createTyped(rs, t, usage);
2137 if ((usage & USAGE_IO_INPUT) != 0) {
2138 if (numAlloc > MAX_NUMBER_IO_INPUT_ALLOC) {
2139 throw new RSIllegalArgumentException("Exceeds the max number of Allocations allowed: " +
2140 MAX_NUMBER_IO_INPUT_ALLOC);
2141 }
2142 mAllocationArray[0].setupBufferQueue(numAlloc);;
2143 }
2144
2145 for (int i=1; i<numAlloc; i++) {
2146 mAllocationArray[i] = createFromAllcation(rs, mAllocationArray[0]);
2147 }
2148 return mAllocationArray;
2149 } finally {
2150 Trace.traceEnd(RenderScript.TRACE_TAG);
2151 }
2152 }
2153
2154 /**
2155 * Creates a new Allocation with the given {@link
2156 * android.renderscript.Allocation}. The same data layout of
2157 * the input Allocation will be applied.
2158 * If the input allocation is of usage: USAGE_IO_INPUT, the created
2159 * Allocation will be sharing the same BufferQueue.
2160 *
2161 * @param rs Context to which the allocation will belong.
2162 * @param alloc RenderScript Allocation describing data layout.
2163 * @return Allocation sharing the same data structure.
2164 */
2165 static Allocation createFromAllcation(RenderScript rs, Allocation alloc) {
2166 try {
2167 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromAllcation");
2168 rs.validate();
2169 if (alloc.getID(rs) == 0) {
2170 throw new RSInvalidStateException("Bad input Allocation");
2171 }
2172
2173 Type type = alloc.getType();
2174 int usage = alloc.getUsage();
2175 MipmapControl mips = alloc.getMipmap();
2176 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
2177 if (id == 0) {
2178 throw new RSRuntimeException("Allocation creation failed.");
2179 }
2180 Allocation outAlloc = new Allocation(id, rs, type, usage, mips);
2181 if ((usage & USAGE_IO_INPUT) != 0) {
2182 outAlloc.shareBufferQueue(alloc);
2183 }
2184 return outAlloc;
2185 } finally {
2186 Trace.traceEnd(RenderScript.TRACE_TAG);
2187 }
2188 }
2189
2190 /**
2191 * Initialize BufferQueue with specified max number of buffers.
2192 */
2193 void setupBufferQueue(int numAlloc) {
2194 mRS.validate();
2195 if ((mUsage & USAGE_IO_INPUT) == 0) {
2196 throw new RSInvalidStateException("Allocation is not USAGE_IO_INPUT.");
2197 }
2198 mRS.nAllocationSetupBufferQueue(getID(mRS), numAlloc);
2199 }
2200
2201 /**
2202 * Share the BufferQueue with another {@link #USAGE_IO_INPUT} Allocation.
2203 *
2204 * @param alloc Allocation to associate with allocation
2205 */
2206 void shareBufferQueue(Allocation alloc) {
2207 mRS.validate();
2208 if ((mUsage & USAGE_IO_INPUT) == 0) {
2209 throw new RSInvalidStateException("Allocation is not USAGE_IO_INPUT.");
2210 }
2211 mGetSurfaceSurface = alloc.getSurface();
2212 mRS.nAllocationShareBufferQueue(getID(mRS), alloc.getID(mRS));
2213 }
2214
2215 /**
Miao Wang0facf022015-11-25 11:21:13 -08002216 * Gets the stride of the Allocation.
2217 * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
2218 * the Allocation has certain alignment. The size of each row including such
2219 * padding is called stride.
2220 *
2221 * @return the stride. For 1D Allocation, the stride will be the number of
2222 * bytes of this Allocation. For 2D and 3D Allocations, the stride
2223 * will be the stride in X dimension measuring in bytes.
2224 */
2225 public long getStride() {
2226 if (mByteBufferStride == -1) {
2227 getByteBuffer();
2228 }
2229 return mByteBufferStride;
2230 }
2231
2232 /**
Miao Wang8c150922015-10-26 17:44:10 -07002233 * Get the timestamp for the most recent buffer held by this Allocation.
2234 * The timestamp is guaranteed to be unique and monotonically increasing.
2235 * Default value: -1. The timestamp will be updated after each {@link
2236 * #ioReceive ioReceive()} call.
2237 *
2238 * It can be used to identify the images by comparing the unique timestamps
2239 * when used with {@link android.hardware.camera2} APIs.
2240 * Example steps:
2241 * 1. Save {@link android.hardware.camera2.TotalCaptureResult} when the
2242 * capture is completed.
2243 * 2. Get the timestamp after {@link #ioReceive ioReceive()} call.
2244 * 3. Comparing totalCaptureResult.get(CaptureResult.SENSOR_TIMESTAMP) with
2245 * alloc.getTimeStamp().
2246 * @return long Timestamp associated with the buffer held by the Allocation.
2247 */
2248 public long getTimeStamp() {
2249 return mTimeStamp;
2250 }
2251
2252 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002253 * Returns the handle to a raw buffer that is being managed by the screen
2254 * compositor. This operation is only valid for Allocations with {@link
2255 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002256 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07002257 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002258 *
2259 */
2260 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08002261 if ((mUsage & USAGE_IO_INPUT) == 0) {
2262 throw new RSInvalidStateException("Allocation is not a surface texture.");
2263 }
Jason Sams1e68bac2015-03-17 16:36:55 -07002264
2265 if (mGetSurfaceSurface == null) {
2266 mGetSurfaceSurface = mRS.nAllocationGetSurface(getID(mRS));
2267 }
2268
2269 return mGetSurfaceSurface;
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002270 }
2271
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002272 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002273 * Associate a {@link android.view.Surface} with this Allocation. This
2274 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07002275 *
2276 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08002277 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002278 public void setSurface(Surface sur) {
2279 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08002280 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
2281 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
2282 }
2283
Jason Samse07694b2012-04-03 15:36:36 -07002284 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08002285 }
2286
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002287 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002288 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08002289 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002290 * <p>With target API version 18 or greater, this Allocation will be created
2291 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
2292 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
2293 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002294 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002295 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002296 * @param b bitmap source for the allocation data
2297 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002298 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002299 *
2300 */
Jason Sams6d8eb262010-12-15 01:41:00 -08002301 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08002302 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2303 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07002304 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08002305 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002306 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
2307 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08002308 }
2309
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002310 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002311 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
2312 * containing the horizontal list of cube faces. Each face must be a square,
2313 * have the same size as all other faces, and have a width that is a power
2314 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002315 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002316 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07002317 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002318 * format: right, left, top, bottom, front, back
2319 * @param mips specifies desired mipmap behaviour for the cubemap
2320 * @param usage bit field specifying how the cubemap is utilized
2321 *
2322 * @return allocation containing cubemap data
2323 *
2324 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002325 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002326 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002327 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002328 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08002329
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002330 int height = b.getHeight();
2331 int width = b.getWidth();
2332
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002333 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002334 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
2335 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002336 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002337 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002338 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002339 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002340 if (!isPow2) {
2341 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2342 }
2343
2344 Element e = elementFromBitmap(rs, b);
2345 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002346 tb.setX(height);
2347 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002348 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08002349 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002350 Type t = tb.create();
2351
Tim Murray460a0492013-11-19 12:45:54 -08002352 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002353 if(id == 0) {
2354 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
2355 }
Miao Wang8c150922015-10-26 17:44:10 -07002356 return new Allocation(id, rs, t, usage, mips);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002357 }
2358
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002359 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002360 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
2361 * from a {@link android.graphics.Bitmap} containing the horizontal list of
2362 * cube faces. Each face must be a square, have the same size as all other
2363 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002364 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002365 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002366 * @param b bitmap with cubemap faces layed out in the following
2367 * format: right, left, top, bottom, front, back
2368 *
2369 * @return allocation containing cubemap data
2370 *
2371 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002372 static public Allocation createCubemapFromBitmap(RenderScript rs,
2373 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08002374 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002375 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08002376 }
2377
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002378 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002379 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
2380 * objects containing the cube faces. Each face must be a square, have the
2381 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002382 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002383 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002384 * @param xpos cubemap face in the positive x direction
2385 * @param xneg cubemap face in the negative x direction
2386 * @param ypos cubemap face in the positive y direction
2387 * @param yneg cubemap face in the negative y direction
2388 * @param zpos cubemap face in the positive z direction
2389 * @param zneg cubemap face in the negative z direction
2390 * @param mips specifies desired mipmap behaviour for the cubemap
2391 * @param usage bit field specifying how the cubemap is utilized
2392 *
2393 * @return allocation containing cubemap data
2394 *
2395 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002396 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2397 Bitmap xpos,
2398 Bitmap xneg,
2399 Bitmap ypos,
2400 Bitmap yneg,
2401 Bitmap zpos,
2402 Bitmap zneg,
2403 MipmapControl mips,
2404 int usage) {
2405 int height = xpos.getHeight();
2406 if (xpos.getWidth() != height ||
2407 xneg.getWidth() != height || xneg.getHeight() != height ||
2408 ypos.getWidth() != height || ypos.getHeight() != height ||
2409 yneg.getWidth() != height || yneg.getHeight() != height ||
2410 zpos.getWidth() != height || zpos.getHeight() != height ||
2411 zneg.getWidth() != height || zneg.getHeight() != height) {
2412 throw new RSIllegalArgumentException("Only square cube map faces supported");
2413 }
2414 boolean isPow2 = (height & (height - 1)) == 0;
2415 if (!isPow2) {
2416 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2417 }
2418
2419 Element e = elementFromBitmap(rs, xpos);
2420 Type.Builder tb = new Type.Builder(rs, e);
2421 tb.setX(height);
2422 tb.setY(height);
2423 tb.setFaces(true);
2424 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
2425 Type t = tb.create();
2426 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
2427
2428 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07002429 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002430 adapter.copyFrom(xpos);
2431 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
2432 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002433 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002434 adapter.copyFrom(ypos);
2435 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
2436 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002437 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002438 adapter.copyFrom(zpos);
2439 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
2440 adapter.copyFrom(zneg);
2441
2442 return cubemap;
2443 }
2444
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002445 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002446 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
2447 * from 6 {@link android.graphics.Bitmap} objects containing the cube
2448 * faces. Each face must be a square, have the same size as all other faces,
2449 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002450 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002451 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002452 * @param xpos cubemap face in the positive x direction
2453 * @param xneg cubemap face in the negative x direction
2454 * @param ypos cubemap face in the positive y direction
2455 * @param yneg cubemap face in the negative y direction
2456 * @param zpos cubemap face in the positive z direction
2457 * @param zneg cubemap face in the negative z direction
2458 *
2459 * @return allocation containing cubemap data
2460 *
2461 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002462 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2463 Bitmap xpos,
2464 Bitmap xneg,
2465 Bitmap ypos,
2466 Bitmap yneg,
2467 Bitmap zpos,
2468 Bitmap zneg) {
2469 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
2470 zpos, zneg, MipmapControl.MIPMAP_NONE,
2471 USAGE_GRAPHICS_TEXTURE);
2472 }
2473
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002474 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002475 * Creates an Allocation from the Bitmap referenced
2476 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002477 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002478 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002479 * @param res application resources
2480 * @param id resource id to load the data from
2481 * @param mips specifies desired mipmap behaviour for the
2482 * allocation
2483 * @param usage bit field specifying how the allocation is
2484 * utilized
2485 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002486 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002487 *
2488 */
Jason Sams5476b452010-12-08 16:14:36 -08002489 static public Allocation createFromBitmapResource(RenderScript rs,
2490 Resources res,
2491 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08002492 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002493 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07002494
Jason Sams771bebb2009-12-07 12:40:12 -08002495 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07002496 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
2497 throw new RSIllegalArgumentException("Unsupported usage specified.");
2498 }
Jason Sams5476b452010-12-08 16:14:36 -08002499 Bitmap b = BitmapFactory.decodeResource(res, id);
2500 Allocation alloc = createFromBitmap(rs, b, mips, usage);
2501 b.recycle();
2502 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07002503 }
2504
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002505 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002506 * Creates a non-mipmapped Allocation to use as a graphics texture from the
2507 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002508 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002509 * <p>With target API version 18 or greater, this allocation will be created
2510 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
2511 * target API version 17 or lower, this allocation will be created with
2512 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08002513 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002514 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002515 * @param res application resources
2516 * @param id resource id to load the data from
2517 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002518 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002519 *
2520 */
Jason Sams5476b452010-12-08 16:14:36 -08002521 static public Allocation createFromBitmapResource(RenderScript rs,
2522 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08002523 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08002524 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2525 return createFromBitmapResource(rs, res, id,
2526 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07002527 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08002528 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002529 return createFromBitmapResource(rs, res, id,
2530 MipmapControl.MIPMAP_NONE,
2531 USAGE_GRAPHICS_TEXTURE);
2532 }
2533
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002534 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002535 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002536 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002537 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002538 * @param str string to create the allocation from
2539 * @param usage bit field specifying how the allocaiton is
2540 * utilized
2541 *
2542 */
Jason Sams5476b452010-12-08 16:14:36 -08002543 static public Allocation createFromString(RenderScript rs,
2544 String str,
2545 int usage) {
2546 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002547 byte[] allocArray = null;
2548 try {
2549 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08002550 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002551 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002552 return alloc;
2553 }
2554 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08002555 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002556 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002557 }
Jason Sams739c8262013-04-11 18:07:52 -07002558
2559 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002560 * Interface to handle notification when new buffers are available via
2561 * {@link #USAGE_IO_INPUT}. An application will receive one notification
2562 * when a buffer is available. Additional buffers will not trigger new
2563 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07002564 */
Jason Sams42ef2382013-08-29 13:30:59 -07002565 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07002566 public void onBufferAvailable(Allocation a);
2567 }
2568
2569 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002570 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07002571 *
Jason Sams42ef2382013-08-29 13:30:59 -07002572 * @param callback instance of the OnBufferAvailableListener
2573 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07002574 */
Jason Sams42ef2382013-08-29 13:30:59 -07002575 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07002576 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002577 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07002578 mBufferNotifier = callback;
2579 }
2580 }
2581
Tim Murrayb730d862014-08-18 16:14:24 -07002582 static void sendBufferNotification(long id) {
Jason Sams739c8262013-04-11 18:07:52 -07002583 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002584 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07002585
2586 if ((a != null) && (a.mBufferNotifier != null)) {
2587 a.mBufferNotifier.onBufferAvailable(a);
2588 }
2589 }
2590 }
2591
Miao Wangf0f6e802015-02-03 17:16:43 -08002592 /**
2593 * For USAGE_IO_OUTPUT, destroy() implies setSurface(null).
2594 *
2595 */
2596 @Override
2597 public void destroy() {
2598 if((mUsage & USAGE_IO_OUTPUT) != 0) {
2599 setSurface(null);
2600 }
2601 super.destroy();
2602 }
Jason Samsb8c5a842009-07-31 20:40:47 -07002603}