blob: 7be09f567b5f2fd8479a8aa9a52f3900d69ab68f [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 }
145 return null;
146 }
147
148
Tim Murrayc11e25c2013-04-09 11:01:01 -0700149 /**
150 * The usage of the Allocation. These signal to RenderScript where to place
151 * the Allocation in memory.
152 *
153 */
Jason Sams5476b452010-12-08 16:14:36 -0800154
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700155 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700156 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -0800157 */
Jason Sams5476b452010-12-08 16:14:36 -0800158 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800159
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700160 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700161 * The Allocation will be used as a texture source by one or more graphics
162 * programs.
Jason Samsf7086092011-01-12 13:28:37 -0800163 *
164 */
Jason Sams5476b452010-12-08 16:14:36 -0800165 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800166
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700167 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700168 * The Allocation will be used as a graphics mesh.
169 *
170 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800171 *
172 */
Jason Sams5476b452010-12-08 16:14:36 -0800173 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800174
175
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700176 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700177 * The Allocation will be used as the source of shader constants by one or
178 * more programs.
179 *
180 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800181 *
182 */
Jason Sams5476b452010-12-08 16:14:36 -0800183 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
184
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700185 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700186 * The Allocation will be used as a target for offscreen rendering
187 *
188 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700189 *
190 */
191 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
192
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700193 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700194 * The Allocation will be used as a {@link android.view.Surface}
195 * consumer. This usage will cause the Allocation to be created
196 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800197 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800198 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700199 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800200
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700201 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700202 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700203 * producer. The dimensions and format of the {@link
Jason Sams3a1b8e42013-09-24 15:18:52 -0700204 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700205 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800206 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800207 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700208 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700209
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700210 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700211 * The Allocation's backing store will be inherited from another object
212 * (usually a {@link android.graphics.Bitmap}); copying to or from the
213 * original source Bitmap will cause a synchronization rather than a full
214 * copy. {@link #syncAll} may also be used to synchronize the Allocation
215 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800216 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700217 * <p>This is set by default for allocations created with {@link
218 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800219 *
220 */
221 public static final int USAGE_SHARED = 0x0080;
222
223 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700224 * Controls mipmap behavior when using the bitmap creation and update
225 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800226 */
Jason Sams4ef66502010-12-10 16:03:15 -0800227 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700228 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700229 * No mipmaps will be generated and the type generated from the incoming
230 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800231 */
Jason Sams5476b452010-12-08 16:14:36 -0800232 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800233
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700234 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700235 * A full mipmap chain will be created in script memory. The Type of
236 * the Allocation will contain a full mipmap chain. On upload, the full
237 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800238 */
Jason Sams5476b452010-12-08 16:14:36 -0800239 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800240
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700241 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700242 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
243 * not contain mipmaps. On upload, the allocation data will contain a
244 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800245 */
Jason Sams5476b452010-12-08 16:14:36 -0800246 MIPMAP_ON_SYNC_TO_TEXTURE(2);
247
248 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800249 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800250 mID = id;
251 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700252 }
253
Jason Sams48fe5342011-07-08 13:52:30 -0700254
Tim Murray460a0492013-11-19 12:45:54 -0800255 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700256 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700257 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700258 }
Jason Samse07694b2012-04-03 15:36:36 -0700259 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700260 }
261
Jason Sams03d2d002012-03-23 13:51:56 -0700262
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700263 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700264 * Get the {@link android.renderscript.Element} of the {@link
265 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700266 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700267 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700268 *
269 */
270 public Element getElement() {
271 return mType.getElement();
272 }
273
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700274 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700275 * Get the usage flags of the Allocation.
276 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700277 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700278 *
279 */
280 public int getUsage() {
281 return mUsage;
282 }
283
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700284 /**
Miao Wang8c150922015-10-26 17:44:10 -0700285 * @hide
286 * Get the Mipmap control flag of the Allocation.
287 *
288 * @return the Mipmap control flag of the Allocation
289 *
290 */
291 public MipmapControl getMipmap() {
292 return mMipmapControl;
293 }
294
295 /**
Miao Wang9ee76072016-03-29 15:56:55 -0700296 * Enable/Disable AutoPadding for Vec3 Elements.
297 *
298 * <p> Vec3 Elements, such as {@link Element#U8_3} are treated as Vec4 Elements
299 * with the fourth vector element used as padding. Enabling the AutoPadding feature
300 * will automatically add/remove the padding when you copy to/from an Allocation
301 * with a Vec3 Element.
302 * <p> By default: Disabled.
Miao Wang87e908d2015-03-02 15:15:15 -0800303 *
Miao Wang179e8b52015-04-15 17:44:32 -0700304 * @param useAutoPadding True: enable AutoPadding; False: disable AutoPadding
Miao Wang87e908d2015-03-02 15:15:15 -0800305 *
306 */
307 public void setAutoPadding(boolean useAutoPadding) {
308 mAutoPadding = useAutoPadding;
309 }
310
311 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700312 * Get the size of the Allocation in bytes.
313 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700314 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700315 *
316 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700317 public int getBytesSize() {
Tim Murray04f0d6e2013-12-17 17:15:25 -0800318 if (mType.mDimYuv != 0) {
319 return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
320 }
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700321 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700322 }
323
Jason Sams452a7662011-07-07 16:05:18 -0700324 private void updateCacheInfo(Type t) {
325 mCurrentDimX = t.getX();
326 mCurrentDimY = t.getY();
327 mCurrentDimZ = t.getZ();
328 mCurrentCount = mCurrentDimX;
329 if (mCurrentDimY > 1) {
330 mCurrentCount *= mCurrentDimY;
331 }
332 if (mCurrentDimZ > 1) {
333 mCurrentCount *= mCurrentDimZ;
334 }
335 }
Jason Samsba862d12011-07-07 15:24:42 -0700336
Tim Murraya3145512012-12-04 17:59:29 -0800337 private void setBitmap(Bitmap b) {
338 mBitmap = b;
339 }
340
Tim Murray460a0492013-11-19 12:45:54 -0800341 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700342 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800343 if ((usage & ~(USAGE_SCRIPT |
344 USAGE_GRAPHICS_TEXTURE |
345 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700346 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800347 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800348 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800349 USAGE_IO_OUTPUT |
350 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800351 throw new RSIllegalArgumentException("Unknown usage specified.");
352 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800353
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700354 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800355 mWriteAllowed = false;
356
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700357 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800358 USAGE_GRAPHICS_TEXTURE |
359 USAGE_SCRIPT)) != 0) {
360 throw new RSIllegalArgumentException("Invalid usage combination.");
361 }
362 }
Jason Sams9bf18922013-04-13 19:48:36 -0700363
Jason Sams5476b452010-12-08 16:14:36 -0800364 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800365 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700366
Jason Sams452a7662011-07-07 16:05:18 -0700367 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700368 // TODO: A3D doesn't have Type info during creation, so we can't
369 // calculate the size ahead of time. We can possibly add a method
370 // to update the size in the future if it seems reasonable.
371 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700372 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700373 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700374 try {
375 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
376 } catch (Exception e) {
377 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
378 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
379 }
380 }
381
Miao Wang8c150922015-10-26 17:44:10 -0700382 Allocation(long id, RenderScript rs, Type t, int usage, MipmapControl mips) {
383 this(id, rs, t, usage);
384 mMipmapControl = mips;
385 }
386
Tim Murray2f2472c2013-08-22 14:55:26 -0700387 protected void finalize() throws Throwable {
388 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
389 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700390 }
391
Jason Sams3042d262013-11-25 18:28:33 -0800392 private void validateIsInt64() {
393 if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
394 (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
395 return;
396 }
397 throw new RSIllegalArgumentException(
398 "64 bit integer source does not match allocation type " + mType.mElement.mType);
399 }
400
Jason Samsb97b2512011-01-16 15:04:08 -0800401 private void validateIsInt32() {
402 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
403 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
404 return;
405 }
406 throw new RSIllegalArgumentException(
407 "32 bit integer source does not match allocation type " + mType.mElement.mType);
408 }
409
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800410 private void validateIsInt16OrFloat16() {
Jason Samsb97b2512011-01-16 15:04:08 -0800411 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800412 (mType.mElement.mType == Element.DataType.UNSIGNED_16) ||
413 (mType.mElement.mType == Element.DataType.FLOAT_16)) {
Jason Samsb97b2512011-01-16 15:04:08 -0800414 return;
415 }
416 throw new RSIllegalArgumentException(
417 "16 bit integer source does not match allocation type " + mType.mElement.mType);
418 }
419
420 private void validateIsInt8() {
421 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
422 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
423 return;
424 }
425 throw new RSIllegalArgumentException(
426 "8 bit integer source does not match allocation type " + mType.mElement.mType);
427 }
428
429 private void validateIsFloat32() {
430 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
431 return;
432 }
433 throw new RSIllegalArgumentException(
434 "32 bit float source does not match allocation type " + mType.mElement.mType);
435 }
436
Jason Sams3042d262013-11-25 18:28:33 -0800437 private void validateIsFloat64() {
438 if (mType.mElement.mType == Element.DataType.FLOAT_64) {
439 return;
440 }
441 throw new RSIllegalArgumentException(
442 "64 bit float source does not match allocation type " + mType.mElement.mType);
443 }
444
Jason Samsb97b2512011-01-16 15:04:08 -0800445 private void validateIsObject() {
446 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
447 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
448 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
449 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
450 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
451 (mType.mElement.mType == Element.DataType.RS_MESH) ||
452 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
453 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
454 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
455 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
456 return;
457 }
458 throw new RSIllegalArgumentException(
459 "Object source does not match allocation type " + mType.mElement.mType);
460 }
461
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700462 @Override
463 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800464 super.updateFromNative();
Tim Murray460a0492013-11-19 12:45:54 -0800465 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700466 if(typeID != 0) {
467 mType = new Type(typeID, mRS);
468 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700469 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700470 }
471 }
472
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700473 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700474 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700475 *
476 * @return Type
477 *
478 */
Jason Samsea87e962010-01-12 12:12:28 -0800479 public Type getType() {
480 return mType;
481 }
482
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700483 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700484 * Propagate changes from one usage of the Allocation to the
485 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700486 *
487 */
Jason Sams5476b452010-12-08 16:14:36 -0800488 public void syncAll(int srcLocation) {
Chris Craik06d29842015-06-02 17:19:24 -0700489 try {
490 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
491 switch (srcLocation) {
492 case USAGE_GRAPHICS_TEXTURE:
493 case USAGE_SCRIPT:
494 if ((mUsage & USAGE_SHARED) != 0) {
495 copyFrom(mBitmap);
496 }
497 break;
498 case USAGE_GRAPHICS_CONSTANTS:
499 case USAGE_GRAPHICS_VERTEX:
500 break;
501 case USAGE_SHARED:
502 if ((mUsage & USAGE_SHARED) != 0) {
503 copyTo(mBitmap);
504 }
505 break;
506 default:
507 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
Tim Murray78e64942013-04-09 17:28:56 -0700508 }
Chris Craik06d29842015-06-02 17:19:24 -0700509 mRS.validate();
510 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
511 } finally {
512 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800513 }
Jason Sams5476b452010-12-08 16:14:36 -0800514 }
515
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700516 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700517 * Send a buffer to the output stream. The contents of the Allocation will
518 * be undefined after this operation. This operation is only valid if {@link
519 * #USAGE_IO_OUTPUT} is set on the Allocation.
520 *
Jason Sams163766c2012-02-15 12:04:24 -0800521 *
Jason Sams163766c2012-02-15 12:04:24 -0800522 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700523 public void ioSend() {
Chris Craik06d29842015-06-02 17:19:24 -0700524 try {
525 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
526 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
527 throw new RSIllegalArgumentException(
528 "Can only send buffer if IO_OUTPUT usage specified.");
529 }
530 mRS.validate();
531 mRS.nAllocationIoSend(getID(mRS));
532 } finally {
533 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800534 }
Jason Sams163766c2012-02-15 12:04:24 -0800535 }
536
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700537 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700538 * Receive the latest input into the Allocation. This operation
539 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800540 *
Jason Sams163766c2012-02-15 12:04:24 -0800541 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700542 public void ioReceive() {
Chris Craik06d29842015-06-02 17:19:24 -0700543 try {
544 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
545 if ((mUsage & USAGE_IO_INPUT) == 0) {
546 throw new RSIllegalArgumentException(
547 "Can only receive if IO_INPUT usage specified.");
548 }
549 mRS.validate();
Miao Wang8c150922015-10-26 17:44:10 -0700550 mTimeStamp = mRS.nAllocationIoReceive(getID(mRS));
Chris Craik06d29842015-06-02 17:19:24 -0700551 } finally {
552 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800553 }
Jason Sams163766c2012-02-15 12:04:24 -0800554 }
555
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700556 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700557 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700558 *
559 * @param d Source array.
560 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800561 public void copyFrom(BaseObj[] d) {
Chris Craik06d29842015-06-02 17:19:24 -0700562 try {
563 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
564 mRS.validate();
565 validateIsObject();
566 if (d.length != mCurrentCount) {
567 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
568 mCurrentCount + ", array length = " + d.length);
569 }
Tim Murray460a0492013-11-19 12:45:54 -0800570
Chris Craik06d29842015-06-02 17:19:24 -0700571 if (RenderScript.sPointerSize == 8) {
572 long i[] = new long[d.length * 4];
573 for (int ct=0; ct < d.length; ct++) {
574 i[ct * 4] = d[ct].getID(mRS);
575 }
576 copy1DRangeFromUnchecked(0, mCurrentCount, i);
577 } else {
578 int i[] = new int[d.length];
579 for (int ct=0; ct < d.length; ct++) {
580 i[ct] = (int) d[ct].getID(mRS);
581 }
582 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray3de3dc72014-07-01 16:56:18 -0700583 }
Chris Craik06d29842015-06-02 17:19:24 -0700584 } finally {
585 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800586 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700587 }
588
Jason Samsfb9f82c2011-01-12 14:53:25 -0800589 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800590 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800591 if (bc == null) {
592 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
593 }
Jason Sams252c0782011-01-11 17:42:52 -0800594 switch (bc) {
595 case ALPHA_8:
596 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
597 throw new RSIllegalArgumentException("Allocation kind is " +
598 mType.getElement().mKind + ", type " +
599 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700600 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800601 " bytes, passed bitmap was " + bc);
602 }
603 break;
604 case ARGB_8888:
605 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700606 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800607 throw new RSIllegalArgumentException("Allocation kind is " +
608 mType.getElement().mKind + ", type " +
609 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700610 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800611 " bytes, passed bitmap was " + bc);
612 }
613 break;
614 case RGB_565:
615 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700616 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800617 throw new RSIllegalArgumentException("Allocation kind is " +
618 mType.getElement().mKind + ", type " +
619 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700620 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800621 " bytes, passed bitmap was " + bc);
622 }
623 break;
624 case ARGB_4444:
625 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700626 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800627 throw new RSIllegalArgumentException("Allocation kind is " +
628 mType.getElement().mKind + ", type " +
629 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700630 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800631 " bytes, passed bitmap was " + bc);
632 }
633 break;
634
635 }
Jason Sams4ef66502010-12-10 16:03:15 -0800636 }
637
Jason Samsfb9f82c2011-01-12 14:53:25 -0800638 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700639 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800640 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
641 }
642 }
643
Jason Sams3042d262013-11-25 18:28:33 -0800644 private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -0700645 try {
646 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
647 mRS.validate();
648 if (mCurrentDimZ > 0) {
649 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
650 } else if (mCurrentDimY > 0) {
651 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
652 } else {
653 copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
654 }
655 } finally {
656 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams3042d262013-11-25 18:28:33 -0800657 }
Jason Sams3042d262013-11-25 18:28:33 -0800658 }
659
660 /**
661 * Copy into this Allocation from an array. This method does not guarantee
662 * that the Allocation is compatible with the input buffer; it copies memory
663 * without reinterpretation.
664 *
665 * @param array The source data array
666 */
667 public void copyFromUnchecked(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700668 try {
669 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
670 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
671 java.lang.reflect.Array.getLength(array));
672 } finally {
673 Trace.traceEnd(RenderScript.TRACE_TAG);
674 }
Jason Sams3042d262013-11-25 18:28:33 -0800675 }
676
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700677 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700678 * Copy into this Allocation from an array. This method does not guarantee
679 * that the Allocation is compatible with the input buffer; it copies memory
680 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800681 *
682 * @param d the source data array
683 */
684 public void copyFromUnchecked(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800685 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800686 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700687
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700688 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700689 * Copy into this Allocation from an array. This method does not guarantee
690 * that the Allocation is compatible with the input buffer; it copies memory
691 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800692 *
693 * @param d the source data array
694 */
695 public void copyFromUnchecked(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800696 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800697 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700698
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700699 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700700 * Copy into this Allocation from an array. This method does not guarantee
701 * that the Allocation is compatible with the input buffer; it copies memory
702 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800703 *
704 * @param d the source data array
705 */
706 public void copyFromUnchecked(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800707 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800708 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700709
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700710 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700711 * Copy into this Allocation from an array. This method does not guarantee
712 * that the Allocation is compatible with the input buffer; it copies memory
713 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800714 *
715 * @param d the source data array
716 */
717 public void copyFromUnchecked(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800718 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800719 }
720
Tim Murray6d7a53c2013-05-23 16:59:23 -0700721
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700722 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700723 * Copy into this Allocation from an array. This variant is type checked
724 * and will generate exceptions if the Allocation's {@link
Jason Sams3042d262013-11-25 18:28:33 -0800725 * android.renderscript.Element} does not match the array's
726 * primitive type.
727 *
Ying Wang16229812013-11-26 15:45:12 -0800728 * @param array The source data array
Jason Sams3042d262013-11-25 18:28:33 -0800729 */
730 public void copyFrom(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700731 try {
732 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
733 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
734 java.lang.reflect.Array.getLength(array));
735 } finally {
736 Trace.traceEnd(RenderScript.TRACE_TAG);
737 }
Jason Sams3042d262013-11-25 18:28:33 -0800738 }
739
740 /**
741 * Copy into this Allocation from an array. This variant is type checked
742 * and will generate exceptions if the Allocation's {@link
Tim Murrayc11e25c2013-04-09 11:01:01 -0700743 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800744 *
745 * @param d the source data array
746 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800747 public void copyFrom(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800748 validateIsInt32();
749 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800750 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800751
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700752 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700753 * Copy into this Allocation from an array. This variant is type checked
754 * and will generate exceptions if the Allocation's {@link
755 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800756 *
757 * @param d the source data array
758 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800759 public void copyFrom(short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -0800760 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -0800761 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800762 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800763
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700764 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700765 * Copy into this Allocation from an array. This variant is type checked
766 * and will generate exceptions if the Allocation's {@link
767 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800768 *
769 * @param d the source data array
770 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800771 public void copyFrom(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800772 validateIsInt8();
773 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800774 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800775
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700776 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700777 * Copy into this Allocation from an array. This variant is type checked
778 * and will generate exceptions if the Allocation's {@link
779 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800780 *
781 * @param d the source data array
782 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800783 public void copyFrom(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800784 validateIsFloat32();
785 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800786 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800787
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700788 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700789 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
790 * height, width, and format of the bitmap must match the existing
791 * allocation.
792 *
793 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
794 * android.graphics.Bitmap} used to create the Allocation with {@link
795 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
796 * this will synchronize the Allocation with the latest data from the {@link
797 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800798 *
799 * @param b the source bitmap
800 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800801 public void copyFrom(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -0700802 try {
803 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
804 mRS.validate();
805 if (b.getConfig() == null) {
806 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
807 Canvas c = new Canvas(newBitmap);
808 c.drawBitmap(b, 0, 0, null);
809 copyFrom(newBitmap);
810 return;
811 }
812 validateBitmapSize(b);
813 validateBitmapFormat(b);
814 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
815 } finally {
816 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -0800817 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700818 }
819
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700820 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700821 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700822 * must be identical.
823 *
824 * @param a the source allocation
825 */
826 public void copyFrom(Allocation a) {
Chris Craik06d29842015-06-02 17:19:24 -0700827 try {
828 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
829 mRS.validate();
830 if (!mType.equals(a.getType())) {
831 throw new RSIllegalArgumentException("Types of allocations must match.");
832 }
833 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
834 } finally {
835 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700836 }
Tim Murrayf671fb02012-10-03 13:50:05 -0700837 }
838
Tim Murrayf671fb02012-10-03 13:50:05 -0700839 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700840 * This is only intended to be used by auto-generated code reflected from
841 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800842 *
843 * @param xoff
844 * @param fp
845 */
Jason Sams21b41032011-01-16 15:05:41 -0800846 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800847 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700848 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700849 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700850 int data_length = fp.getPos();
Jason Samsa70f4162010-03-26 15:33:42 -0700851
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700852 int count = data_length / eSize;
853 if ((eSize * count) != data_length) {
854 throw new RSIllegalArgumentException("Field packer length " + data_length +
Jason Samsa70f4162010-03-26 15:33:42 -0700855 " not divisible by element size " + eSize + ".");
856 }
Jason Samsba862d12011-07-07 15:24:42 -0700857 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700858 }
859
Miao Wang45cec0a2015-03-04 16:40:21 -0800860
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700861 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700862 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -0800863 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800864 *
865 * @param xoff
866 * @param component_number
867 * @param fp
868 */
Jason Sams21b41032011-01-16 15:05:41 -0800869 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800870 setFromFieldPacker(xoff, 0, 0, component_number, fp);
871 }
872
873 /**
Miao Wangc8e237e2015-02-20 18:36:32 -0800874 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -0800875 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -0800876 *
877 * @param xoff
878 * @param yoff
Miao Wangc8e237e2015-02-20 18:36:32 -0800879 * @param zoff
880 * @param component_number
881 * @param fp
882 */
883 public void setFromFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800884 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700885 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800886 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700887 }
888 if(xoff < 0) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800889 throw new RSIllegalArgumentException("Offset x must be >= 0.");
890 }
891 if(yoff < 0) {
892 throw new RSIllegalArgumentException("Offset y must be >= 0.");
893 }
894 if(zoff < 0) {
895 throw new RSIllegalArgumentException("Offset z must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700896 }
897
898 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700899 int data_length = fp.getPos();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700900 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800901 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700902
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700903 if (data_length != eSize) {
904 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700905 " does not match component size " + eSize + ".");
906 }
907
Miao Wangc8e237e2015-02-20 18:36:32 -0800908 mRS.nAllocationElementData(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
909 component_number, data, data_length);
Jason Samsa70f4162010-03-26 15:33:42 -0700910 }
911
Miao Wang87e908d2015-03-02 15:15:15 -0800912 private void data1DChecks(int off, int count, int len, int dataSize, boolean usePadding) {
Jason Sams771bebb2009-12-07 12:40:12 -0800913 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700914 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800915 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700916 }
917 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800918 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700919 }
Jason Samsba862d12011-07-07 15:24:42 -0700920 if((off + count) > mCurrentCount) {
921 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700922 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700923 }
Miao Wang87e908d2015-03-02 15:15:15 -0800924 if(usePadding) {
925 if(len < dataSize / 4 * 3) {
926 throw new RSIllegalArgumentException("Array too small for allocation type.");
927 }
928 } else {
929 if(len < dataSize) {
930 throw new RSIllegalArgumentException("Array too small for allocation type.");
931 }
Jason Sams768bc022009-09-21 19:41:04 -0700932 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700933 }
934
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700935 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700936 * Generate a mipmap chain. This is only valid if the Type of the Allocation
937 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800938 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700939 * <p>This function will generate a complete set of mipmaps from the top
940 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800941 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700942 * <p>If the Allocation is also using other memory spaces, a call to {@link
943 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800944 */
945 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700946 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800947 }
948
Jason Sams3042d262013-11-25 18:28:33 -0800949 private void copy1DRangeFromUnchecked(int off, int count, Object array,
950 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -0700951 try {
952 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
953 final int dataSize = mType.mElement.getBytesSize() * count;
954 // AutoPadding for Vec3 Element
955 boolean usePadding = false;
956 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
957 usePadding = true;
958 }
959 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
960 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
961 mType.mElement.mType.mSize, usePadding);
962 } finally {
963 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -0800964 }
Jason Sams3042d262013-11-25 18:28:33 -0800965 }
966
967 /**
968 * Copy an array into part of this Allocation. This method does not
969 * guarantee that the Allocation is compatible with the input buffer.
970 *
971 * @param off The offset of the first element to be copied.
972 * @param count The number of elements to be copied.
973 * @param array The source data array
974 */
975 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
976 copy1DRangeFromUnchecked(off, count, array,
977 validateObjectIsPrimitiveArray(array, false),
978 java.lang.reflect.Array.getLength(array));
979 }
980
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700981 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700982 * Copy an array into part of this Allocation. This method does not
983 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800984 *
985 * @param off The offset of the first element to be copied.
986 * @param count The number of elements to be copied.
987 * @param d the source data array
988 */
989 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800990 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700991 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700992
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700993 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700994 * Copy an array into part of this Allocation. This method does not
995 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800996 *
997 * @param off The offset of the first element to be copied.
998 * @param count The number of elements to be copied.
999 * @param d the source data array
1000 */
1001 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001002 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -07001003 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001004
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001005 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001006 * Copy an array into part of this Allocation. This method does not
1007 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001008 *
1009 * @param off The offset of the first element to be copied.
1010 * @param count The number of elements to be copied.
1011 * @param d the source data array
1012 */
1013 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001014 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -07001015 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001016
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001017 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001018 * Copy an array into part of this Allocation. This method does not
1019 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001020 *
1021 * @param off The offset of the first element to be copied.
1022 * @param count The number of elements to be copied.
1023 * @param d the source data array
1024 */
1025 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001026 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
1027 }
1028
1029
1030 /**
1031 * Copy an array into part of this Allocation. This variant is type checked
1032 * and will generate exceptions if the Allocation type does not
1033 * match the component type of the array passed in.
1034 *
1035 * @param off The offset of the first element to be copied.
1036 * @param count The number of elements to be copied.
1037 * @param array The source data array.
1038 */
1039 public void copy1DRangeFrom(int off, int count, Object array) {
1040 copy1DRangeFromUnchecked(off, count, array,
1041 validateObjectIsPrimitiveArray(array, true),
1042 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -07001043 }
1044
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001045 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001046 * Copy an array into part of this Allocation. This variant is type checked
1047 * and will generate exceptions if the Allocation type is not a 32 bit
1048 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001049 *
1050 * @param off The offset of the first element to be copied.
1051 * @param count The number of elements to be copied.
1052 * @param d the source data array
1053 */
Jason Samsb97b2512011-01-16 15:04:08 -08001054 public void copy1DRangeFrom(int off, int count, int[] d) {
1055 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001056 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001057 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001058
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001059 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001060 * Copy an array into part of this Allocation. This variant is type checked
1061 * and will generate exceptions if the Allocation type is not a 16 bit
1062 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001063 *
1064 * @param off The offset of the first element to be copied.
1065 * @param count The number of elements to be copied.
1066 * @param d the source data array
1067 */
Jason Samsb97b2512011-01-16 15:04:08 -08001068 public void copy1DRangeFrom(int off, int count, short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001069 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -08001070 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001071 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001072
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001073 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001074 * Copy an array into part of this Allocation. This variant is type checked
1075 * and will generate exceptions if the Allocation type is not an 8 bit
1076 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001077 *
1078 * @param off The offset of the first element to be copied.
1079 * @param count The number of elements to be copied.
1080 * @param d the source data array
1081 */
Jason Samsb97b2512011-01-16 15:04:08 -08001082 public void copy1DRangeFrom(int off, int count, byte[] d) {
1083 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001084 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001085 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001086
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001087 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001088 * Copy an array into part of this Allocation. This variant is type checked
1089 * and will generate exceptions if the Allocation type is not a 32 bit float
1090 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001091 *
1092 * @param off The offset of the first element to be copied.
1093 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001094 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001095 */
Jason Samsb97b2512011-01-16 15:04:08 -08001096 public void copy1DRangeFrom(int off, int count, float[] d) {
1097 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001098 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001099 }
Jason Sams3042d262013-11-25 18:28:33 -08001100
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001101 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001102 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001103 *
1104 * @param off The offset of the first element to be copied.
1105 * @param count The number of elements to be copied.
1106 * @param data the source data allocation.
1107 * @param dataOff off The offset of the first element in data to
1108 * be copied.
1109 */
1110 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001111 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -07001112 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001113 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001114 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001115 data.mSelectedLOD, data.mSelectedFace.mID);
Chris Craik5c705d62015-06-01 10:39:36 -07001116 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001117 }
1118
Jason Samsfb9f82c2011-01-12 14:53:25 -08001119 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001120 if (mAdaptedAllocation != null) {
1121
1122 } else {
1123
1124 if (xoff < 0 || yoff < 0) {
1125 throw new RSIllegalArgumentException("Offset cannot be negative.");
1126 }
1127 if (h < 0 || w < 0) {
1128 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1129 }
1130 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1131 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1132 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001133 }
1134 }
Jason Sams768bc022009-09-21 19:41:04 -07001135
Jason Sams3042d262013-11-25 18:28:33 -08001136 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1137 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001138 try {
1139 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
1140 mRS.validate();
1141 validate2DRange(xoff, yoff, w, h);
1142 final int dataSize = mType.mElement.getBytesSize() * w * h;
1143 // AutoPadding for Vec3 Element
1144 boolean usePadding = false;
1145 int sizeBytes = arrayLen * dt.mSize;
1146 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1147 if (dataSize / 4 * 3 > sizeBytes) {
1148 throw new RSIllegalArgumentException("Array too small for allocation type.");
1149 }
1150 usePadding = true;
1151 sizeBytes = dataSize;
1152 } else {
1153 if (dataSize > sizeBytes) {
1154 throw new RSIllegalArgumentException("Array too small for allocation type.");
1155 }
Miao Wang87e908d2015-03-02 15:15:15 -08001156 }
Chris Craik06d29842015-06-02 17:19:24 -07001157 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1158 array, sizeBytes, dt,
1159 mType.mElement.mType.mSize, usePadding);
1160 } finally {
1161 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001162 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001163 }
1164
Jason Sams3042d262013-11-25 18:28:33 -08001165 /**
1166 * Copy from an array into a rectangular region in this Allocation. The
1167 * array is assumed to be tightly packed.
1168 *
1169 * @param xoff X offset of the region to update in this Allocation
1170 * @param yoff Y offset of the region to update in this Allocation
1171 * @param w Width of the region to update
1172 * @param h Height of the region to update
Ying Wang16229812013-11-26 15:45:12 -08001173 * @param array Data to be placed into the Allocation
Jason Sams3042d262013-11-25 18:28:33 -08001174 */
1175 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001176 try {
1177 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1178 copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
1179 validateObjectIsPrimitiveArray(array, true),
1180 java.lang.reflect.Array.getLength(array));
1181 } finally {
1182 Trace.traceEnd(RenderScript.TRACE_TAG);
1183 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001184 }
1185
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001186 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001187 * Copy from an array into a rectangular region in this Allocation. The
1188 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -08001189 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001190 * @param xoff X offset of the region to update in this Allocation
1191 * @param yoff Y offset of the region to update in this Allocation
1192 * @param w Width of the region to update
1193 * @param h Height of the region to update
1194 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001195 */
1196 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001197 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001198 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1199 Element.DataType.SIGNED_8, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001200 }
1201
Tim Murrayc11e25c2013-04-09 11:01:01 -07001202 /**
1203 * Copy from an array into a rectangular region in this Allocation. The
1204 * array is assumed to be tightly packed.
1205 *
1206 * @param xoff X offset of the region to update in this Allocation
1207 * @param yoff Y offset of the region to update in this Allocation
1208 * @param w Width of the region to update
1209 * @param h Height of the region to update
1210 * @param data to be placed into the Allocation
1211 */
Jason Samsf7086092011-01-12 13:28:37 -08001212 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001213 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -08001214 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1215 Element.DataType.SIGNED_16, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001216 }
1217
Tim Murrayc11e25c2013-04-09 11:01:01 -07001218 /**
1219 * Copy from an array into a rectangular region in this Allocation. The
1220 * array is assumed to be tightly packed.
1221 *
1222 * @param xoff X offset of the region to update in this Allocation
1223 * @param yoff Y offset of the region to update in this Allocation
1224 * @param w Width of the region to update
1225 * @param h Height of the region to update
1226 * @param data to be placed into the Allocation
1227 */
Jason Samsf7086092011-01-12 13:28:37 -08001228 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001229 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001230 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1231 Element.DataType.SIGNED_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001232 }
1233
Tim Murrayc11e25c2013-04-09 11:01:01 -07001234 /**
1235 * Copy from an array into a rectangular region in this Allocation. The
1236 * array is assumed to be tightly packed.
1237 *
1238 * @param xoff X offset of the region to update in this Allocation
1239 * @param yoff Y offset of the region to update in this Allocation
1240 * @param w Width of the region to update
1241 * @param h Height of the region to update
1242 * @param data to be placed into the Allocation
1243 */
Jason Samsf7086092011-01-12 13:28:37 -08001244 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001245 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001246 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1247 Element.DataType.FLOAT_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001248 }
1249
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001250 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001251 * Copy a rectangular region from an Allocation into a rectangular region in
1252 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001253 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001254 * @param xoff X offset of the region in this Allocation
1255 * @param yoff Y offset of the region in this Allocation
1256 * @param w Width of the region to update.
1257 * @param h Height of the region to update.
1258 * @param data source Allocation.
1259 * @param dataXoff X offset in source Allocation
1260 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001261 */
1262 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1263 Allocation data, int dataXoff, int dataYoff) {
Chris Craik06d29842015-06-02 17:19:24 -07001264 try {
1265 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1266 mRS.validate();
1267 validate2DRange(xoff, yoff, w, h);
1268 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
1269 mSelectedLOD, mSelectedFace.mID,
1270 w, h, data.getID(mRS), dataXoff, dataYoff,
1271 data.mSelectedLOD, data.mSelectedFace.mID);
1272 } finally {
1273 Trace.traceEnd(RenderScript.TRACE_TAG);
1274 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001275 }
1276
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001277 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001278 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1279 * and width of the update will use the height and width of the {@link
1280 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001281 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001282 * @param xoff X offset of the region to update in this Allocation
1283 * @param yoff Y offset of the region to update in this Allocation
1284 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001285 */
1286 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Chris Craik5c705d62015-06-01 10:39:36 -07001287 try {
1288 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1289 mRS.validate();
1290 if (data.getConfig() == null) {
1291 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1292 Canvas c = new Canvas(newBitmap);
1293 c.drawBitmap(data, 0, 0, null);
1294 copy2DRangeFrom(xoff, yoff, newBitmap);
1295 return;
1296 }
1297 validateBitmapFormat(data);
1298 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
1299 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
1300 } finally {
1301 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -08001302 }
Jason Samsfa445b92011-01-07 17:00:07 -08001303 }
1304
Jason Samsb05d6892013-04-09 15:59:24 -07001305 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1306 if (mAdaptedAllocation != null) {
1307
1308 } else {
1309
1310 if (xoff < 0 || yoff < 0 || zoff < 0) {
1311 throw new RSIllegalArgumentException("Offset cannot be negative.");
1312 }
1313 if (h < 0 || w < 0 || d < 0) {
1314 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1315 }
1316 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1317 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1318 }
1319 }
1320 }
1321
1322 /**
Miao Wang258db502015-03-03 14:05:36 -08001323 * Copy a rectangular region from the array into the allocation.
1324 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001325 *
Miao Wang258db502015-03-03 14:05:36 -08001326 * The data type of the array is not required to be the same as
1327 * the element data type.
Jason Samsb05d6892013-04-09 15:59:24 -07001328 */
Jason Sams3042d262013-11-25 18:28:33 -08001329 private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1330 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001331 try {
1332 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
1333 mRS.validate();
1334 validate3DRange(xoff, yoff, zoff, w, h, d);
1335 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1336 // AutoPadding for Vec3 Element
1337 boolean usePadding = false;
1338 int sizeBytes = arrayLen * dt.mSize;
1339 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1340 if (dataSize / 4 * 3 > sizeBytes) {
1341 throw new RSIllegalArgumentException("Array too small for allocation type.");
1342 }
1343 usePadding = true;
1344 sizeBytes = dataSize;
1345 } else {
1346 if (dataSize > sizeBytes) {
1347 throw new RSIllegalArgumentException("Array too small for allocation type.");
1348 }
Miao Wang87e908d2015-03-02 15:15:15 -08001349 }
Chris Craik06d29842015-06-02 17:19:24 -07001350 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1351 array, sizeBytes, dt,
1352 mType.mElement.mType.mSize, usePadding);
1353 } finally {
1354 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001355 }
Jason Samsb05d6892013-04-09 15:59:24 -07001356 }
1357
1358 /**
Jason Samsb05d6892013-04-09 15:59:24 -07001359 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001360 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001361 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001362 * @param xoff X offset of the region to update in this Allocation
1363 * @param yoff Y offset of the region to update in this Allocation
1364 * @param zoff Z offset of the region to update in this Allocation
1365 * @param w Width of the region to update
1366 * @param h Height of the region to update
1367 * @param d Depth of the region to update
Miao Wang87e908d2015-03-02 15:15:15 -08001368 * @param array to be placed into the allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001369 */
Jason Sams3042d262013-11-25 18:28:33 -08001370 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001371 try {
1372 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
1373 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
1374 validateObjectIsPrimitiveArray(array, true),
1375 java.lang.reflect.Array.getLength(array));
1376 } finally {
1377 Trace.traceEnd(RenderScript.TRACE_TAG);
1378 }
Jason Samsb05d6892013-04-09 15:59:24 -07001379 }
1380
1381 /**
Jason Samsb05d6892013-04-09 15:59:24 -07001382 * Copy a rectangular region into the allocation from another
1383 * allocation.
1384 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001385 * @param xoff X offset of the region to update in this Allocation
1386 * @param yoff Y offset of the region to update in this Allocation
1387 * @param zoff Z offset of the region to update in this Allocation
1388 * @param w Width of the region to update.
1389 * @param h Height of the region to update.
1390 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001391 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001392 * @param dataXoff X offset of the region in the source Allocation
1393 * @param dataYoff Y offset of the region in the source Allocation
1394 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001395 */
1396 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1397 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1398 mRS.validate();
1399 validate3DRange(xoff, yoff, zoff, w, h, d);
1400 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1401 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1402 data.mSelectedLOD);
1403 }
1404
Jason Samsfa445b92011-01-07 17:00:07 -08001405
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001406 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001407 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1408 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001409 *
1410 * @param b The bitmap to be set from the Allocation.
1411 */
Jason Samsfa445b92011-01-07 17:00:07 -08001412 public void copyTo(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -07001413 try {
1414 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1415 mRS.validate();
1416 validateBitmapFormat(b);
1417 validateBitmapSize(b);
1418 mRS.nAllocationCopyToBitmap(getID(mRS), b);
1419 } finally {
1420 Trace.traceEnd(RenderScript.TRACE_TAG);
1421 }
Jason Samsfa445b92011-01-07 17:00:07 -08001422 }
1423
Jason Sams3042d262013-11-25 18:28:33 -08001424 private void copyTo(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001425 try {
1426 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1427 mRS.validate();
1428 boolean usePadding = false;
1429 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1430 usePadding = true;
Miao Wangd9b63282015-04-03 09:15:39 -07001431 }
Chris Craik06d29842015-06-02 17:19:24 -07001432 if (usePadding) {
1433 if (dt.mSize * arrayLen < mSize / 4 * 3) {
1434 throw new RSIllegalArgumentException(
1435 "Size of output array cannot be smaller than size of allocation.");
1436 }
1437 } else {
1438 if (dt.mSize * arrayLen < mSize) {
1439 throw new RSIllegalArgumentException(
1440 "Size of output array cannot be smaller than size of allocation.");
1441 }
Miao Wangd9b63282015-04-03 09:15:39 -07001442 }
Chris Craik06d29842015-06-02 17:19:24 -07001443 mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
1444 } finally {
1445 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wangd9b63282015-04-03 09:15:39 -07001446 }
Jason Sams3042d262013-11-25 18:28:33 -08001447 }
1448
1449 /**
1450 * Copy from the Allocation into an array. The array must be at
1451 * least as large as the Allocation. The
1452 * {@link android.renderscript.Element} must match the component
1453 * type of the array passed in.
1454 *
1455 * @param array The array to be set from the Allocation.
1456 */
1457 public void copyTo(Object array) {
1458 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1459 java.lang.reflect.Array.getLength(array));
1460 }
1461
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001462 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001463 * Copy from the Allocation into a byte array. The array must be at least
1464 * as large as the Allocation. The allocation must be of an 8 bit integer
1465 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001466 *
1467 * @param d The array to be set from the Allocation.
1468 */
Jason Samsfa445b92011-01-07 17:00:07 -08001469 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001470 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001471 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001472 }
1473
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001474 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001475 * Copy from the Allocation into a short array. The array must be at least
1476 * as large as the Allocation. The allocation must be of an 16 bit integer
1477 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001478 *
1479 * @param d The array to be set from the Allocation.
1480 */
Jason Samsfa445b92011-01-07 17:00:07 -08001481 public void copyTo(short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001482 validateIsInt16OrFloat16();
Jason Sams3042d262013-11-25 18:28:33 -08001483 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001484 }
1485
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001486 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001487 * Copy from the Allocation into a int array. The array must be at least as
1488 * large as the Allocation. The allocation must be of an 32 bit integer
1489 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001490 *
1491 * @param d The array to be set from the Allocation.
1492 */
Jason Samsfa445b92011-01-07 17:00:07 -08001493 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001494 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001495 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001496 }
1497
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001498 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001499 * Copy from the Allocation into a float array. The array must be at least
1500 * as large as the Allocation. The allocation must be of an 32 bit float
1501 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001502 *
1503 * @param d The array to be set from the Allocation.
1504 */
Jason Samsfa445b92011-01-07 17:00:07 -08001505 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001506 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001507 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001508 }
1509
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001510 /**
Miao Wang3c613272015-05-11 11:41:55 -07001511 * @hide
1512 *
Miao Wang45cec0a2015-03-04 16:40:21 -08001513 * This is only intended to be used by auto-generated code reflected from
1514 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -08001515 *
1516 * @param xoff
1517 * @param yoff
1518 * @param zoff
1519 * @param component_number
Miao Wang258db502015-03-03 14:05:36 -08001520 * @param fp
Miao Wangc8e237e2015-02-20 18:36:32 -08001521 */
Miao Wang45cec0a2015-03-04 16:40:21 -08001522 public void copyToFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001523 mRS.validate();
1524 if (component_number >= mType.mElement.mElements.length) {
1525 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
1526 }
1527 if(xoff < 0) {
1528 throw new RSIllegalArgumentException("Offset x must be >= 0.");
1529 }
1530 if(yoff < 0) {
1531 throw new RSIllegalArgumentException("Offset y must be >= 0.");
1532 }
1533 if(zoff < 0) {
1534 throw new RSIllegalArgumentException("Offset z must be >= 0.");
1535 }
1536
Miao Wang45cec0a2015-03-04 16:40:21 -08001537 final byte[] data = fp.getData();
Miao Wangbfa5e652015-05-04 15:29:25 -07001538 int data_length = data.length;
Miao Wangc8e237e2015-02-20 18:36:32 -08001539 int eSize = mType.mElement.mElements[component_number].getBytesSize();
1540 eSize *= mType.mElement.mArraySizes[component_number];
1541
Miao Wang45cec0a2015-03-04 16:40:21 -08001542 if (data_length != eSize) {
1543 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
1544 " does not match component size " + eSize + ".");
Miao Wangc8e237e2015-02-20 18:36:32 -08001545 }
1546
1547 mRS.nAllocationElementRead(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Miao Wang45cec0a2015-03-04 16:40:21 -08001548 component_number, data, data_length);
Miao Wangc8e237e2015-02-20 18:36:32 -08001549 }
1550 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001551 * Resize a 1D allocation. The contents of the allocation are preserved.
1552 * If new elements are allocated objects are created with null contents and
1553 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001554 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001555 * <p>If the new region is smaller the references of any objects outside the
1556 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001557 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001558 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001559 *
1560 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001561 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001562 * @deprecated RenderScript objects should be immutable once created. The
Tim Murraycd38b762014-08-13 13:20:25 -07001563 * replacement is to create a new allocation and copy the contents. This
1564 * function will throw an exception if API 21 or higher is used.
Jason Samsf7086092011-01-12 13:28:37 -08001565 */
Jason Sams31a7e422010-10-26 13:09:17 -07001566 public synchronized void resize(int dimX) {
Tim Murraycd38b762014-08-13 13:20:25 -07001567 if (mRS.getApplicationContext().getApplicationInfo().targetSdkVersion >= 21) {
1568 throw new RSRuntimeException("Resize is not allowed in API 21+.");
1569 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001570 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001571 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001572 }
Jason Samse07694b2012-04-03 15:36:36 -07001573 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001574 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001575
Tim Murray460a0492013-11-19 12:45:54 -08001576 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001577 mType = new Type(typeID, mRS);
1578 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001579 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001580 }
1581
Miao Wangc8e237e2015-02-20 18:36:32 -08001582 private void copy1DRangeToUnchecked(int off, int count, Object array,
1583 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001584 try {
1585 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked");
1586 final int dataSize = mType.mElement.getBytesSize() * count;
1587 // AutoPadding for Vec3 Element
1588 boolean usePadding = false;
1589 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1590 usePadding = true;
1591 }
1592 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
1593 mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
1594 mType.mElement.mType.mSize, usePadding);
1595 } finally {
1596 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001597 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001598 }
1599
1600 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001601 * Copy part of this Allocation into an array. This method does not
1602 * guarantee that the Allocation is compatible with the input buffer.
1603 *
1604 * @param off The offset of the first element to be copied.
1605 * @param count The number of elements to be copied.
1606 * @param array The dest data array
1607 */
1608 public void copy1DRangeToUnchecked(int off, int count, Object array) {
1609 copy1DRangeToUnchecked(off, count, array,
1610 validateObjectIsPrimitiveArray(array, false),
1611 java.lang.reflect.Array.getLength(array));
1612 }
1613
1614 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001615 * Copy part of this Allocation into an array. This method does not
1616 * guarantee that the Allocation is compatible with the input buffer.
1617 *
1618 * @param off The offset of the first element to be copied.
1619 * @param count The number of elements to be copied.
1620 * @param d the source data array
1621 */
1622 public void copy1DRangeToUnchecked(int off, int count, int[] d) {
1623 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
1624 }
1625
1626 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001627 * Copy part of this Allocation into an array. This method does not
1628 * guarantee that the Allocation is compatible with the input buffer.
1629 *
1630 * @param off The offset of the first element to be copied.
1631 * @param count The number of elements to be copied.
1632 * @param d the source data array
1633 */
1634 public void copy1DRangeToUnchecked(int off, int count, short[] d) {
1635 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
1636 }
1637
1638 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001639 * Copy part of this Allocation into an array. This method does not
1640 * guarantee that the Allocation is compatible with the input buffer.
1641 *
1642 * @param off The offset of the first element to be copied.
1643 * @param count The number of elements to be copied.
1644 * @param d the source data array
1645 */
1646 public void copy1DRangeToUnchecked(int off, int count, byte[] d) {
1647 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
1648 }
1649
1650 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001651 * Copy part of this Allocation into an array. This method does not
1652 * guarantee that the Allocation is compatible with the input buffer.
1653 *
1654 * @param off The offset of the first element to be copied.
1655 * @param count The number of elements to be copied.
1656 * @param d the source data array
1657 */
1658 public void copy1DRangeToUnchecked(int off, int count, float[] d) {
1659 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
1660 }
1661
1662
1663 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001664 * Copy part of this Allocation into an array. This method does not
1665 * and will generate exceptions if the Allocation type does not
1666 * match the component type of the array passed in.
1667 *
1668 * @param off The offset of the first element to be copied.
1669 * @param count The number of elements to be copied.
1670 * @param array The source data array.
1671 */
1672 public void copy1DRangeTo(int off, int count, Object array) {
1673 copy1DRangeToUnchecked(off, count, array,
1674 validateObjectIsPrimitiveArray(array, true),
1675 java.lang.reflect.Array.getLength(array));
1676 }
1677
1678 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001679 * Copy part of this Allocation into an array. This method does not
1680 * and will generate exceptions if the Allocation type is not a 32 bit
1681 * integer type.
1682 *
1683 * @param off The offset of the first element to be copied.
1684 * @param count The number of elements to be copied.
1685 * @param d the source data array
1686 */
1687 public void copy1DRangeTo(int off, int count, int[] d) {
1688 validateIsInt32();
1689 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
1690 }
1691
1692 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001693 * Copy part of this Allocation into an array. This method does not
1694 * and will generate exceptions if the Allocation type is not a 16 bit
1695 * integer type.
1696 *
1697 * @param off The offset of the first element to be copied.
1698 * @param count The number of elements to be copied.
1699 * @param d the source data array
1700 */
1701 public void copy1DRangeTo(int off, int count, short[] d) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001702 validateIsInt16OrFloat16();
Miao Wangc8e237e2015-02-20 18:36:32 -08001703 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
1704 }
1705
1706 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001707 * Copy part of this Allocation into an array. This method does not
1708 * and will generate exceptions if the Allocation type is not an 8 bit
1709 * integer type.
1710 *
1711 * @param off The offset of the first element to be copied.
1712 * @param count The number of elements to be copied.
1713 * @param d the source data array
1714 */
1715 public void copy1DRangeTo(int off, int count, byte[] d) {
1716 validateIsInt8();
1717 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
1718 }
1719
1720 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001721 * Copy part of this Allocation into an array. This method does not
1722 * and will generate exceptions if the Allocation type is not a 32 bit float
1723 * type.
1724 *
1725 * @param off The offset of the first element to be copied.
1726 * @param count The number of elements to be copied.
1727 * @param d the source data array.
1728 */
1729 public void copy1DRangeTo(int off, int count, float[] d) {
1730 validateIsFloat32();
1731 copy1DRangeToUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
1732 }
1733
1734
1735 void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
1736 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001737 try {
1738 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked");
1739 mRS.validate();
1740 validate2DRange(xoff, yoff, w, h);
1741 final int dataSize = mType.mElement.getBytesSize() * w * h;
1742 // AutoPadding for Vec3 Element
1743 boolean usePadding = false;
1744 int sizeBytes = arrayLen * dt.mSize;
1745 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1746 if (dataSize / 4 * 3 > sizeBytes) {
1747 throw new RSIllegalArgumentException("Array too small for allocation type.");
1748 }
1749 usePadding = true;
1750 sizeBytes = dataSize;
1751 } else {
1752 if (dataSize > sizeBytes) {
1753 throw new RSIllegalArgumentException("Array too small for allocation type.");
1754 }
Miao Wang87e908d2015-03-02 15:15:15 -08001755 }
Chris Craik06d29842015-06-02 17:19:24 -07001756 mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1757 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
1758 } finally {
1759 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001760 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001761 }
1762
1763 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001764 * Copy from a rectangular region in this Allocation into an array.
1765 *
1766 * @param xoff X offset of the region to copy in this Allocation
1767 * @param yoff Y offset of the region to copy in this Allocation
1768 * @param w Width of the region to copy
1769 * @param h Height of the region to copy
1770 * @param array Dest Array to be copied into
1771 */
1772 public void copy2DRangeTo(int xoff, int yoff, int w, int h, Object array) {
1773 copy2DRangeToUnchecked(xoff, yoff, w, h, array,
1774 validateObjectIsPrimitiveArray(array, true),
1775 java.lang.reflect.Array.getLength(array));
1776 }
1777
1778 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001779 * Copy from a rectangular region in this Allocation into an array.
1780 *
1781 * @param xoff X offset of the region to copy in this Allocation
1782 * @param yoff Y offset of the region to copy in this Allocation
1783 * @param w Width of the region to copy
1784 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001785 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001786 */
1787 public void copy2DRangeTo(int xoff, int yoff, int w, int h, byte[] data) {
1788 validateIsInt8();
1789 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1790 Element.DataType.SIGNED_8, data.length);
1791 }
1792
1793 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001794 * Copy from a rectangular region in this Allocation into an array.
1795 *
1796 * @param xoff X offset of the region to copy in this Allocation
1797 * @param yoff Y offset of the region to copy in this Allocation
1798 * @param w Width of the region to copy
1799 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001800 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001801 */
1802 public void copy2DRangeTo(int xoff, int yoff, int w, int h, short[] data) {
Pirama Arumuga Nainarf51bb352016-02-26 09:16:17 -08001803 validateIsInt16OrFloat16();
Miao Wangc8e237e2015-02-20 18:36:32 -08001804 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1805 Element.DataType.SIGNED_16, data.length);
1806 }
1807
1808 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001809 * Copy from a rectangular region in this Allocation into an array.
1810 *
1811 * @param xoff X offset of the region to copy in this Allocation
1812 * @param yoff Y offset of the region to copy in this Allocation
1813 * @param w Width of the region to copy
1814 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001815 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001816 */
1817 public void copy2DRangeTo(int xoff, int yoff, int w, int h, int[] data) {
1818 validateIsInt32();
1819 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1820 Element.DataType.SIGNED_32, data.length);
1821 }
1822
1823 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001824 * Copy from a rectangular region in this Allocation into an array.
1825 *
1826 * @param xoff X offset of the region to copy in this Allocation
1827 * @param yoff Y offset of the region to copy in this Allocation
1828 * @param w Width of the region to copy
1829 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001830 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001831 */
1832 public void copy2DRangeTo(int xoff, int yoff, int w, int h, float[] data) {
1833 validateIsFloat32();
1834 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1835 Element.DataType.FLOAT_32, data.length);
1836 }
1837
1838
1839 /**
Miao Wang258db502015-03-03 14:05:36 -08001840 * Copy from a rectangular region in this Allocation into an array.
1841 * The array is assumed to be tightly packed.
Miao Wangc8e237e2015-02-20 18:36:32 -08001842 *
Miao Wang258db502015-03-03 14:05:36 -08001843 * The data type of the array is not required to be the same as
1844 * the element data type.
Miao Wangc8e237e2015-02-20 18:36:32 -08001845 */
1846 private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1847 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001848 try {
1849 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked");
1850 mRS.validate();
1851 validate3DRange(xoff, yoff, zoff, w, h, d);
1852 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1853 // AutoPadding for Vec3 Element
1854 boolean usePadding = false;
1855 int sizeBytes = arrayLen * dt.mSize;
1856 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1857 if (dataSize / 4 * 3 > sizeBytes) {
1858 throw new RSIllegalArgumentException("Array too small for allocation type.");
1859 }
1860 usePadding = true;
1861 sizeBytes = dataSize;
1862 } else {
1863 if (dataSize > sizeBytes) {
1864 throw new RSIllegalArgumentException("Array too small for allocation type.");
1865 }
Miao Wang87e908d2015-03-02 15:15:15 -08001866 }
Chris Craik06d29842015-06-02 17:19:24 -07001867 mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1868 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
1869 } finally {
1870 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001871 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001872 }
1873
Miao Wang258db502015-03-03 14:05:36 -08001874 /*
Miao Wangc8e237e2015-02-20 18:36:32 -08001875 * Copy from a rectangular region in this Allocation into an array.
1876 *
1877 * @param xoff X offset of the region to copy in this Allocation
1878 * @param yoff Y offset of the region to copy in this Allocation
1879 * @param zoff Z offset of the region to copy in this Allocation
1880 * @param w Width of the region to copy
1881 * @param h Height of the region to copy
1882 * @param d Depth of the region to copy
1883 * @param array Dest Array to be copied into
1884 */
1885 public void copy3DRangeTo(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1886 copy3DRangeToUnchecked(xoff, yoff, zoff, w, h, d, array,
1887 validateObjectIsPrimitiveArray(array, true),
1888 java.lang.reflect.Array.getLength(array));
1889 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001890
1891 // creation
1892
Jason Sams49a05d72010-12-29 14:31:29 -08001893 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001894 static {
1895 mBitmapOptions.inScaled = false;
1896 }
1897
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001898 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001899 * Creates a new Allocation with the given {@link
1900 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001901 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001902 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001903 * @param mips specifies desired mipmap behaviour for the
1904 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001905 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001906 * utilized
1907 */
1908 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07001909 try {
1910 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
1911 rs.validate();
1912 if (type.getID(rs) == 0) {
1913 throw new RSInvalidStateException("Bad Type");
1914 }
1915 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
1916 if (id == 0) {
1917 throw new RSRuntimeException("Allocation creation failed.");
1918 }
Miao Wang8c150922015-10-26 17:44:10 -07001919 return new Allocation(id, rs, type, usage, mips);
Chris Craik06d29842015-06-02 17:19:24 -07001920 } finally {
1921 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams1bada8c2009-08-09 17:01:55 -07001922 }
Jason Sams857d0c72011-11-23 15:02:15 -08001923 }
1924
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001925 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001926 * Creates an Allocation with the size specified by the type and no mipmaps
1927 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001928 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001929 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001930 * @param type renderscript type describing data layout
1931 * @param usage bit field specifying how the allocation is
1932 * utilized
1933 *
1934 * @return allocation
1935 */
Jason Samse5d37122010-12-16 00:33:33 -08001936 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1937 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1938 }
1939
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001940 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001941 * Creates an Allocation for use by scripts with a given {@link
1942 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001943 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001944 * @param rs Context to which the Allocation will belong.
1945 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001946 *
1947 * @return allocation
1948 */
Jason Sams5476b452010-12-08 16:14:36 -08001949 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001950 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001951 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001952
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001953 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001954 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001955 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001956 * @param rs Context to which the Allocation will belong.
1957 * @param e Element to use in the Allocation
1958 * @param count the number of Elements in the Allocation
1959 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001960 * utilized
1961 *
1962 * @return allocation
1963 */
Jason Sams5476b452010-12-08 16:14:36 -08001964 static public Allocation createSized(RenderScript rs, Element e,
1965 int count, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07001966 try {
1967 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
1968 rs.validate();
1969 Type.Builder b = new Type.Builder(rs, e);
1970 b.setX(count);
1971 Type t = b.create();
Jason Sams768bc022009-09-21 19:41:04 -07001972
Chris Craik06d29842015-06-02 17:19:24 -07001973 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
1974 if (id == 0) {
1975 throw new RSRuntimeException("Allocation creation failed.");
1976 }
Miao Wang8c150922015-10-26 17:44:10 -07001977 return new Allocation(id, rs, t, usage, MipmapControl.MIPMAP_NONE);
Chris Craik06d29842015-06-02 17:19:24 -07001978 } finally {
1979 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001980 }
Jason Sams5476b452010-12-08 16:14:36 -08001981 }
1982
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001983 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001984 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001985 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001986 * @param rs Context to which the Allocation will belong.
1987 * @param e Element to use in the Allocation
1988 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001989 *
1990 * @return allocation
1991 */
Jason Sams5476b452010-12-08 16:14:36 -08001992 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001993 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001994 }
1995
Jason Sams49a05d72010-12-29 14:31:29 -08001996 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001997 final Bitmap.Config bc = b.getConfig();
1998 if (bc == Bitmap.Config.ALPHA_8) {
1999 return Element.A_8(rs);
2000 }
2001 if (bc == Bitmap.Config.ARGB_4444) {
2002 return Element.RGBA_4444(rs);
2003 }
2004 if (bc == Bitmap.Config.ARGB_8888) {
2005 return Element.RGBA_8888(rs);
2006 }
2007 if (bc == Bitmap.Config.RGB_565) {
2008 return Element.RGB_565(rs);
2009 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08002010 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08002011 }
2012
Jason Sams49a05d72010-12-29 14:31:29 -08002013 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002014 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08002015 Element e = elementFromBitmap(rs, b);
2016 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002017 tb.setX(b.getWidth());
2018 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08002019 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08002020 return tb.create();
2021 }
2022
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002023 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002024 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002025 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002026 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07002027 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002028 * @param mips specifies desired mipmap behaviour for the
2029 * allocation
2030 * @param usage bit field specifying how the allocation is
2031 * utilized
2032 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002033 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002034 *
2035 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002036 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002037 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002038 int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07002039 try {
2040 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
2041 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08002042
Chris Craik06d29842015-06-02 17:19:24 -07002043 // WAR undocumented color formats
2044 if (b.getConfig() == null) {
2045 if ((usage & USAGE_SHARED) != 0) {
2046 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
2047 }
2048 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
2049 Canvas c = new Canvas(newBitmap);
2050 c.drawBitmap(b, 0, 0, null);
2051 return createFromBitmap(rs, newBitmap, mips, usage);
Tim Murrayabd5db92013-02-28 11:45:22 -08002052 }
Tim Murrayabd5db92013-02-28 11:45:22 -08002053
Chris Craik06d29842015-06-02 17:19:24 -07002054 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08002055
Chris Craik06d29842015-06-02 17:19:24 -07002056 // enable optimized bitmap path only with no mipmap and script-only usage
2057 if (mips == MipmapControl.MIPMAP_NONE &&
2058 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
2059 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
2060 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
2061 if (id == 0) {
2062 throw new RSRuntimeException("Load failed.");
2063 }
2064
2065 // keep a reference to the Bitmap around to prevent GC
Miao Wang8c150922015-10-26 17:44:10 -07002066 Allocation alloc = new Allocation(id, rs, t, usage, mips);
Chris Craik06d29842015-06-02 17:19:24 -07002067 alloc.setBitmap(b);
2068 return alloc;
2069 }
2070
2071
2072 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08002073 if (id == 0) {
2074 throw new RSRuntimeException("Load failed.");
2075 }
Miao Wang8c150922015-10-26 17:44:10 -07002076 return new Allocation(id, rs, t, usage, mips);
Chris Craik06d29842015-06-02 17:19:24 -07002077 } finally {
2078 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murraya3145512012-12-04 17:59:29 -08002079 }
Jason Sams5476b452010-12-08 16:14:36 -08002080 }
2081
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002082 /**
Miao Wang0facf022015-11-25 11:21:13 -08002083 * Gets or creates a ByteBuffer that contains the raw data of the current Allocation.
2084 * If the Allocation is created with USAGE_IO_INPUT, the returned ByteBuffer
2085 * would contain the up-to-date data as READ ONLY.
2086 * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
2087 * the Allocation has certain alignment. The size of each row including padding,
2088 * called stride, can be queried using the {@link #getStride()} method.
2089 *
2090 * Note: Operating on the ByteBuffer of a destroyed Allocation will triger errors.
2091 *
2092 * @return ByteBuffer The ByteBuffer associated with raw data pointer of the Allocation.
2093 */
2094 public ByteBuffer getByteBuffer() {
2095 // Create a new ByteBuffer if it is not initialized or using IO_INPUT.
2096 if (mType.hasFaces()) {
2097 throw new RSInvalidStateException("Cubemap is not supported for getByteBuffer().");
2098 }
2099 if (mType.getYuv() == android.graphics.ImageFormat.NV21 ||
2100 mType.getYuv() == android.graphics.ImageFormat.YV12 ||
2101 mType.getYuv() == android.graphics.ImageFormat.YUV_420_888 ) {
2102 throw new RSInvalidStateException("YUV format is not supported for getByteBuffer().");
2103 }
2104 if (mByteBuffer == null || (mUsage & USAGE_IO_INPUT) != 0) {
2105 int xBytesSize = mType.getX() * mType.getElement().getBytesSize();
2106 long[] stride = new long[1];
2107 mByteBuffer = mRS.nAllocationGetByteBuffer(getID(mRS), stride, xBytesSize, mType.getY(), mType.getZ());
2108 mByteBufferStride = stride[0];
2109 }
2110 if ((mUsage & USAGE_IO_INPUT) != 0) {
2111 return mByteBuffer.asReadOnlyBuffer();
2112 }
2113 return mByteBuffer;
2114 }
2115
2116 /**
Miao Wang8c150922015-10-26 17:44:10 -07002117 * Creates a new Allocation Array with the given {@link
2118 * android.renderscript.Type}, and usage flags.
2119 * Note: If the input allocation is of usage: USAGE_IO_INPUT,
2120 * the created Allocation will be sharing the same BufferQueue.
2121 *
2122 * @param rs RenderScript context
2123 * @param t RenderScript type describing data layout
2124 * @param usage bit field specifying how the Allocation is
2125 * utilized
2126 * @param numAlloc Number of Allocations in the array.
2127 * @return Allocation[]
2128 */
2129 public static Allocation[] createAllocations(RenderScript rs, Type t, int usage, int numAlloc) {
2130 try {
2131 Trace.traceBegin(RenderScript.TRACE_TAG, "createAllocations");
2132 rs.validate();
2133 if (t.getID(rs) == 0) {
2134 throw new RSInvalidStateException("Bad Type");
2135 }
2136
2137 Allocation[] mAllocationArray = new Allocation[numAlloc];
2138 mAllocationArray[0] = createTyped(rs, t, usage);
2139 if ((usage & USAGE_IO_INPUT) != 0) {
2140 if (numAlloc > MAX_NUMBER_IO_INPUT_ALLOC) {
2141 throw new RSIllegalArgumentException("Exceeds the max number of Allocations allowed: " +
2142 MAX_NUMBER_IO_INPUT_ALLOC);
2143 }
2144 mAllocationArray[0].setupBufferQueue(numAlloc);;
2145 }
2146
2147 for (int i=1; i<numAlloc; i++) {
2148 mAllocationArray[i] = createFromAllcation(rs, mAllocationArray[0]);
2149 }
2150 return mAllocationArray;
2151 } finally {
2152 Trace.traceEnd(RenderScript.TRACE_TAG);
2153 }
2154 }
2155
2156 /**
2157 * Creates a new Allocation with the given {@link
2158 * android.renderscript.Allocation}. The same data layout of
2159 * the input Allocation will be applied.
2160 * If the input allocation is of usage: USAGE_IO_INPUT, the created
2161 * Allocation will be sharing the same BufferQueue.
2162 *
2163 * @param rs Context to which the allocation will belong.
2164 * @param alloc RenderScript Allocation describing data layout.
2165 * @return Allocation sharing the same data structure.
2166 */
2167 static Allocation createFromAllcation(RenderScript rs, Allocation alloc) {
2168 try {
2169 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromAllcation");
2170 rs.validate();
2171 if (alloc.getID(rs) == 0) {
2172 throw new RSInvalidStateException("Bad input Allocation");
2173 }
2174
2175 Type type = alloc.getType();
2176 int usage = alloc.getUsage();
2177 MipmapControl mips = alloc.getMipmap();
2178 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
2179 if (id == 0) {
2180 throw new RSRuntimeException("Allocation creation failed.");
2181 }
2182 Allocation outAlloc = new Allocation(id, rs, type, usage, mips);
2183 if ((usage & USAGE_IO_INPUT) != 0) {
2184 outAlloc.shareBufferQueue(alloc);
2185 }
2186 return outAlloc;
2187 } finally {
2188 Trace.traceEnd(RenderScript.TRACE_TAG);
2189 }
2190 }
2191
2192 /**
2193 * Initialize BufferQueue with specified max number of buffers.
2194 */
2195 void setupBufferQueue(int numAlloc) {
2196 mRS.validate();
2197 if ((mUsage & USAGE_IO_INPUT) == 0) {
2198 throw new RSInvalidStateException("Allocation is not USAGE_IO_INPUT.");
2199 }
2200 mRS.nAllocationSetupBufferQueue(getID(mRS), numAlloc);
2201 }
2202
2203 /**
2204 * Share the BufferQueue with another {@link #USAGE_IO_INPUT} Allocation.
2205 *
2206 * @param alloc Allocation to associate with allocation
2207 */
2208 void shareBufferQueue(Allocation alloc) {
2209 mRS.validate();
2210 if ((mUsage & USAGE_IO_INPUT) == 0) {
2211 throw new RSInvalidStateException("Allocation is not USAGE_IO_INPUT.");
2212 }
2213 mGetSurfaceSurface = alloc.getSurface();
2214 mRS.nAllocationShareBufferQueue(getID(mRS), alloc.getID(mRS));
2215 }
2216
2217 /**
Miao Wang0facf022015-11-25 11:21:13 -08002218 * Gets the stride of the Allocation.
2219 * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
2220 * the Allocation has certain alignment. The size of each row including such
2221 * padding is called stride.
2222 *
2223 * @return the stride. For 1D Allocation, the stride will be the number of
2224 * bytes of this Allocation. For 2D and 3D Allocations, the stride
2225 * will be the stride in X dimension measuring in bytes.
2226 */
2227 public long getStride() {
2228 if (mByteBufferStride == -1) {
2229 getByteBuffer();
2230 }
2231 return mByteBufferStride;
2232 }
2233
2234 /**
Miao Wang8c150922015-10-26 17:44:10 -07002235 * Get the timestamp for the most recent buffer held by this Allocation.
2236 * The timestamp is guaranteed to be unique and monotonically increasing.
2237 * Default value: -1. The timestamp will be updated after each {@link
2238 * #ioReceive ioReceive()} call.
2239 *
2240 * It can be used to identify the images by comparing the unique timestamps
2241 * when used with {@link android.hardware.camera2} APIs.
2242 * Example steps:
2243 * 1. Save {@link android.hardware.camera2.TotalCaptureResult} when the
2244 * capture is completed.
2245 * 2. Get the timestamp after {@link #ioReceive ioReceive()} call.
2246 * 3. Comparing totalCaptureResult.get(CaptureResult.SENSOR_TIMESTAMP) with
2247 * alloc.getTimeStamp().
2248 * @return long Timestamp associated with the buffer held by the Allocation.
2249 */
2250 public long getTimeStamp() {
2251 return mTimeStamp;
2252 }
2253
2254 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002255 * Returns the handle to a raw buffer that is being managed by the screen
2256 * compositor. This operation is only valid for Allocations with {@link
2257 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002258 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07002259 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002260 *
2261 */
2262 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08002263 if ((mUsage & USAGE_IO_INPUT) == 0) {
2264 throw new RSInvalidStateException("Allocation is not a surface texture.");
2265 }
Jason Sams1e68bac2015-03-17 16:36:55 -07002266
2267 if (mGetSurfaceSurface == null) {
2268 mGetSurfaceSurface = mRS.nAllocationGetSurface(getID(mRS));
2269 }
2270
2271 return mGetSurfaceSurface;
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002272 }
2273
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002274 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002275 * Associate a {@link android.view.Surface} with this Allocation. This
2276 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07002277 *
2278 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08002279 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002280 public void setSurface(Surface sur) {
2281 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08002282 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
2283 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
2284 }
2285
Jason Samse07694b2012-04-03 15:36:36 -07002286 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08002287 }
2288
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002289 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002290 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08002291 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002292 * <p>With target API version 18 or greater, this Allocation will be created
2293 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
2294 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
2295 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002296 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002297 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002298 * @param b bitmap source for the allocation data
2299 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002300 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002301 *
2302 */
Jason Sams6d8eb262010-12-15 01:41:00 -08002303 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08002304 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2305 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07002306 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08002307 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002308 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
2309 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08002310 }
2311
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002312 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002313 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
2314 * containing the horizontal list of cube faces. Each face must be a square,
2315 * have the same size as all other faces, and have a width that is a power
2316 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002317 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002318 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07002319 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002320 * format: right, left, top, bottom, front, back
2321 * @param mips specifies desired mipmap behaviour for the cubemap
2322 * @param usage bit field specifying how the cubemap is utilized
2323 *
2324 * @return allocation containing cubemap data
2325 *
2326 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002327 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002328 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002329 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002330 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08002331
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002332 int height = b.getHeight();
2333 int width = b.getWidth();
2334
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002335 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002336 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
2337 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002338 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002339 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002340 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002341 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002342 if (!isPow2) {
2343 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2344 }
2345
2346 Element e = elementFromBitmap(rs, b);
2347 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002348 tb.setX(height);
2349 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002350 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08002351 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002352 Type t = tb.create();
2353
Tim Murray460a0492013-11-19 12:45:54 -08002354 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002355 if(id == 0) {
2356 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
2357 }
Miao Wang8c150922015-10-26 17:44:10 -07002358 return new Allocation(id, rs, t, usage, mips);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002359 }
2360
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002361 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002362 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
2363 * from a {@link android.graphics.Bitmap} containing the horizontal list of
2364 * cube faces. Each face must be a square, have the same size as all other
2365 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002366 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002367 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002368 * @param b bitmap with cubemap faces layed out in the following
2369 * format: right, left, top, bottom, front, back
2370 *
2371 * @return allocation containing cubemap data
2372 *
2373 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002374 static public Allocation createCubemapFromBitmap(RenderScript rs,
2375 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08002376 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002377 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08002378 }
2379
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002380 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002381 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
2382 * objects containing the cube faces. Each face must be a square, have the
2383 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002384 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002385 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002386 * @param xpos cubemap face in the positive x direction
2387 * @param xneg cubemap face in the negative x direction
2388 * @param ypos cubemap face in the positive y direction
2389 * @param yneg cubemap face in the negative y direction
2390 * @param zpos cubemap face in the positive z direction
2391 * @param zneg cubemap face in the negative z direction
2392 * @param mips specifies desired mipmap behaviour for the cubemap
2393 * @param usage bit field specifying how the cubemap is utilized
2394 *
2395 * @return allocation containing cubemap data
2396 *
2397 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002398 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2399 Bitmap xpos,
2400 Bitmap xneg,
2401 Bitmap ypos,
2402 Bitmap yneg,
2403 Bitmap zpos,
2404 Bitmap zneg,
2405 MipmapControl mips,
2406 int usage) {
2407 int height = xpos.getHeight();
2408 if (xpos.getWidth() != height ||
2409 xneg.getWidth() != height || xneg.getHeight() != height ||
2410 ypos.getWidth() != height || ypos.getHeight() != height ||
2411 yneg.getWidth() != height || yneg.getHeight() != height ||
2412 zpos.getWidth() != height || zpos.getHeight() != height ||
2413 zneg.getWidth() != height || zneg.getHeight() != height) {
2414 throw new RSIllegalArgumentException("Only square cube map faces supported");
2415 }
2416 boolean isPow2 = (height & (height - 1)) == 0;
2417 if (!isPow2) {
2418 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2419 }
2420
2421 Element e = elementFromBitmap(rs, xpos);
2422 Type.Builder tb = new Type.Builder(rs, e);
2423 tb.setX(height);
2424 tb.setY(height);
2425 tb.setFaces(true);
2426 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
2427 Type t = tb.create();
2428 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
2429
2430 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07002431 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002432 adapter.copyFrom(xpos);
2433 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
2434 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002435 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002436 adapter.copyFrom(ypos);
2437 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
2438 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002439 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002440 adapter.copyFrom(zpos);
2441 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
2442 adapter.copyFrom(zneg);
2443
2444 return cubemap;
2445 }
2446
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002447 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002448 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
2449 * from 6 {@link android.graphics.Bitmap} objects containing the cube
2450 * faces. Each face must be a square, have the same size as all other faces,
2451 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002452 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002453 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002454 * @param xpos cubemap face in the positive x direction
2455 * @param xneg cubemap face in the negative x direction
2456 * @param ypos cubemap face in the positive y direction
2457 * @param yneg cubemap face in the negative y direction
2458 * @param zpos cubemap face in the positive z direction
2459 * @param zneg cubemap face in the negative z direction
2460 *
2461 * @return allocation containing cubemap data
2462 *
2463 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002464 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2465 Bitmap xpos,
2466 Bitmap xneg,
2467 Bitmap ypos,
2468 Bitmap yneg,
2469 Bitmap zpos,
2470 Bitmap zneg) {
2471 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
2472 zpos, zneg, MipmapControl.MIPMAP_NONE,
2473 USAGE_GRAPHICS_TEXTURE);
2474 }
2475
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002476 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002477 * Creates an Allocation from the Bitmap referenced
2478 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002479 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002480 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002481 * @param res application resources
2482 * @param id resource id to load the data from
2483 * @param mips specifies desired mipmap behaviour for the
2484 * allocation
2485 * @param usage bit field specifying how the allocation is
2486 * utilized
2487 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002488 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002489 *
2490 */
Jason Sams5476b452010-12-08 16:14:36 -08002491 static public Allocation createFromBitmapResource(RenderScript rs,
2492 Resources res,
2493 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08002494 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002495 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07002496
Jason Sams771bebb2009-12-07 12:40:12 -08002497 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07002498 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
2499 throw new RSIllegalArgumentException("Unsupported usage specified.");
2500 }
Jason Sams5476b452010-12-08 16:14:36 -08002501 Bitmap b = BitmapFactory.decodeResource(res, id);
2502 Allocation alloc = createFromBitmap(rs, b, mips, usage);
2503 b.recycle();
2504 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07002505 }
2506
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002507 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002508 * Creates a non-mipmapped Allocation to use as a graphics texture from the
2509 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002510 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002511 * <p>With target API version 18 or greater, this allocation will be created
2512 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
2513 * target API version 17 or lower, this allocation will be created with
2514 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08002515 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002516 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002517 * @param res application resources
2518 * @param id resource id to load the data from
2519 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002520 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002521 *
2522 */
Jason Sams5476b452010-12-08 16:14:36 -08002523 static public Allocation createFromBitmapResource(RenderScript rs,
2524 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08002525 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08002526 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2527 return createFromBitmapResource(rs, res, id,
2528 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07002529 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08002530 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002531 return createFromBitmapResource(rs, res, id,
2532 MipmapControl.MIPMAP_NONE,
2533 USAGE_GRAPHICS_TEXTURE);
2534 }
2535
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002536 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002537 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002538 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002539 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002540 * @param str string to create the allocation from
2541 * @param usage bit field specifying how the allocaiton is
2542 * utilized
2543 *
2544 */
Jason Sams5476b452010-12-08 16:14:36 -08002545 static public Allocation createFromString(RenderScript rs,
2546 String str,
2547 int usage) {
2548 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002549 byte[] allocArray = null;
2550 try {
2551 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08002552 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002553 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002554 return alloc;
2555 }
2556 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08002557 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002558 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002559 }
Jason Sams739c8262013-04-11 18:07:52 -07002560
2561 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002562 * Interface to handle notification when new buffers are available via
2563 * {@link #USAGE_IO_INPUT}. An application will receive one notification
2564 * when a buffer is available. Additional buffers will not trigger new
2565 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07002566 */
Jason Sams42ef2382013-08-29 13:30:59 -07002567 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07002568 public void onBufferAvailable(Allocation a);
2569 }
2570
2571 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002572 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07002573 *
Jason Sams42ef2382013-08-29 13:30:59 -07002574 * @param callback instance of the OnBufferAvailableListener
2575 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07002576 */
Jason Sams42ef2382013-08-29 13:30:59 -07002577 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07002578 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002579 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07002580 mBufferNotifier = callback;
2581 }
2582 }
2583
Tim Murrayb730d862014-08-18 16:14:24 -07002584 static void sendBufferNotification(long id) {
Jason Sams739c8262013-04-11 18:07:52 -07002585 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002586 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07002587
2588 if ((a != null) && (a.mBufferNotifier != null)) {
2589 a.mBufferNotifier.onBufferAvailable(a);
2590 }
2591 }
2592 }
2593
Miao Wangf0f6e802015-02-03 17:16:43 -08002594 /**
2595 * For USAGE_IO_OUTPUT, destroy() implies setSurface(null).
2596 *
2597 */
2598 @Override
2599 public void destroy() {
2600 if((mUsage & USAGE_IO_OUTPUT) != 0) {
2601 setSurface(null);
2602 }
2603 super.destroy();
2604 }
Jason Samsb8c5a842009-07-31 20:40:47 -07002605}