blob: a71ba6364ac89b96c6d71660a4ddc9c5645d3f11 [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 {
Jason Sams43ee06852009-08-12 17:54:11 -070058 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080059 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080060 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070061 Allocation mAdaptedAllocation;
Tim Murray2f2472c2013-08-22 14:55:26 -070062 int mSize;
Jason Samsba862d12011-07-07 15:24:42 -070063
Jason Sams615e7ce2012-01-13 14:01:20 -080064 boolean mReadAllowed = true;
65 boolean mWriteAllowed = true;
Miao Wang87e908d2015-03-02 15:15:15 -080066 boolean mAutoPadding = false;
Jason Sams46ba27e32015-02-06 17:45:15 -080067 int mSelectedX;
Jason Samsba862d12011-07-07 15:24:42 -070068 int mSelectedY;
69 int mSelectedZ;
70 int mSelectedLOD;
Jason Sams46ba27e32015-02-06 17:45:15 -080071 int mSelectedArray[];
Jason Samsba862d12011-07-07 15:24:42 -070072 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
73
74 int mCurrentDimX;
75 int mCurrentDimY;
76 int mCurrentDimZ;
77 int mCurrentCount;
Tim Murray460a0492013-11-19 12:45:54 -080078 static HashMap<Long, Allocation> mAllocationMap =
79 new HashMap<Long, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070080 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070081
Jason Sams1e68bac2015-03-17 16:36:55 -070082 private Surface mGetSurfaceSurface = null;
Miao Wang0facf022015-11-25 11:21:13 -080083 private ByteBuffer mByteBuffer = null;
84 private long mByteBufferStride = -1;
Jason Sams1e68bac2015-03-17 16:36:55 -070085
Jason Sams3042d262013-11-25 18:28:33 -080086 private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
87 final Class c = d.getClass();
88 if (!c.isArray()) {
89 throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
90 }
91 final Class cmp = c.getComponentType();
92 if (!cmp.isPrimitive()) {
93 throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
94 }
95
96 if (cmp == Long.TYPE) {
97 if (checkType) {
98 validateIsInt64();
99 return mType.mElement.mType;
100 }
101 return Element.DataType.SIGNED_64;
102 }
103
104 if (cmp == Integer.TYPE) {
105 if (checkType) {
106 validateIsInt32();
107 return mType.mElement.mType;
108 }
109 return Element.DataType.SIGNED_32;
110 }
111
112 if (cmp == Short.TYPE) {
113 if (checkType) {
114 validateIsInt16();
115 return mType.mElement.mType;
116 }
117 return Element.DataType.SIGNED_16;
118 }
119
120 if (cmp == Byte.TYPE) {
121 if (checkType) {
122 validateIsInt8();
123 return mType.mElement.mType;
124 }
125 return Element.DataType.SIGNED_8;
126 }
127
128 if (cmp == Float.TYPE) {
129 if (checkType) {
130 validateIsFloat32();
131 }
132 return Element.DataType.FLOAT_32;
133 }
134
135 if (cmp == Double.TYPE) {
136 if (checkType) {
137 validateIsFloat64();
138 }
139 return Element.DataType.FLOAT_64;
140 }
141 return null;
142 }
143
144
Tim Murrayc11e25c2013-04-09 11:01:01 -0700145 /**
146 * The usage of the Allocation. These signal to RenderScript where to place
147 * the Allocation in memory.
148 *
149 */
Jason Sams5476b452010-12-08 16:14:36 -0800150
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700151 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700152 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -0800153 */
Jason Sams5476b452010-12-08 16:14:36 -0800154 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800155
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700156 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700157 * The Allocation will be used as a texture source by one or more graphics
158 * programs.
Jason Samsf7086092011-01-12 13:28:37 -0800159 *
160 */
Jason Sams5476b452010-12-08 16:14:36 -0800161 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800162
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700163 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700164 * The Allocation will be used as a graphics mesh.
165 *
166 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800167 *
168 */
Jason Sams5476b452010-12-08 16:14:36 -0800169 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800170
171
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700172 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700173 * The Allocation will be used as the source of shader constants by one or
174 * more programs.
175 *
176 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800177 *
178 */
Jason Sams5476b452010-12-08 16:14:36 -0800179 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
180
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700181 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700182 * The Allocation will be used as a target for offscreen rendering
183 *
184 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700185 *
186 */
187 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
188
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700189 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700190 * The Allocation will be used as a {@link android.view.Surface}
191 * consumer. This usage will cause the Allocation to be created
192 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800193 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800194 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700195 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800196
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700197 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700198 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700199 * producer. The dimensions and format of the {@link
Jason Sams3a1b8e42013-09-24 15:18:52 -0700200 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700201 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800202 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800203 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700204 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700205
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700206 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700207 * The Allocation's backing store will be inherited from another object
208 * (usually a {@link android.graphics.Bitmap}); copying to or from the
209 * original source Bitmap will cause a synchronization rather than a full
210 * copy. {@link #syncAll} may also be used to synchronize the Allocation
211 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800212 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700213 * <p>This is set by default for allocations created with {@link
214 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800215 *
216 */
217 public static final int USAGE_SHARED = 0x0080;
218
219 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700220 * Controls mipmap behavior when using the bitmap creation and update
221 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800222 */
Jason Sams4ef66502010-12-10 16:03:15 -0800223 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700224 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700225 * No mipmaps will be generated and the type generated from the incoming
226 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800227 */
Jason Sams5476b452010-12-08 16:14:36 -0800228 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800229
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700230 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700231 * A full mipmap chain will be created in script memory. The Type of
232 * the Allocation will contain a full mipmap chain. On upload, the full
233 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800234 */
Jason Sams5476b452010-12-08 16:14:36 -0800235 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800236
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700237 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700238 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
239 * not contain mipmaps. On upload, the allocation data will contain a
240 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800241 */
Jason Sams5476b452010-12-08 16:14:36 -0800242 MIPMAP_ON_SYNC_TO_TEXTURE(2);
243
244 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800245 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800246 mID = id;
247 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700248 }
249
Jason Sams48fe5342011-07-08 13:52:30 -0700250
Tim Murray460a0492013-11-19 12:45:54 -0800251 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700252 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700253 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700254 }
Jason Samse07694b2012-04-03 15:36:36 -0700255 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700256 }
257
Jason Sams03d2d002012-03-23 13:51:56 -0700258
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700259 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700260 * Get the {@link android.renderscript.Element} of the {@link
261 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700262 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700263 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700264 *
265 */
266 public Element getElement() {
267 return mType.getElement();
268 }
269
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700270 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700271 * Get the usage flags of the Allocation.
272 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700273 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700274 *
275 */
276 public int getUsage() {
277 return mUsage;
278 }
279
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700280 /**
Miao Wang87e908d2015-03-02 15:15:15 -0800281 * Enable/Disable AutoPadding for Vec3 elements.
Miao Wangd7ecab12015-03-26 18:00:15 -0700282 * By default: Diabled.
Miao Wang87e908d2015-03-02 15:15:15 -0800283 *
Miao Wang179e8b52015-04-15 17:44:32 -0700284 * @param useAutoPadding True: enable AutoPadding; False: disable AutoPadding
Miao Wang87e908d2015-03-02 15:15:15 -0800285 *
286 */
287 public void setAutoPadding(boolean useAutoPadding) {
288 mAutoPadding = useAutoPadding;
289 }
290
291 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700292 * Get the size of the Allocation in bytes.
293 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700294 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700295 *
296 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700297 public int getBytesSize() {
Tim Murray04f0d6e2013-12-17 17:15:25 -0800298 if (mType.mDimYuv != 0) {
299 return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
300 }
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700301 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700302 }
303
Jason Sams452a7662011-07-07 16:05:18 -0700304 private void updateCacheInfo(Type t) {
305 mCurrentDimX = t.getX();
306 mCurrentDimY = t.getY();
307 mCurrentDimZ = t.getZ();
308 mCurrentCount = mCurrentDimX;
309 if (mCurrentDimY > 1) {
310 mCurrentCount *= mCurrentDimY;
311 }
312 if (mCurrentDimZ > 1) {
313 mCurrentCount *= mCurrentDimZ;
314 }
315 }
Jason Samsba862d12011-07-07 15:24:42 -0700316
Tim Murraya3145512012-12-04 17:59:29 -0800317 private void setBitmap(Bitmap b) {
318 mBitmap = b;
319 }
320
Tim Murray460a0492013-11-19 12:45:54 -0800321 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700322 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800323 if ((usage & ~(USAGE_SCRIPT |
324 USAGE_GRAPHICS_TEXTURE |
325 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700326 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800327 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800328 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800329 USAGE_IO_OUTPUT |
330 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800331 throw new RSIllegalArgumentException("Unknown usage specified.");
332 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800333
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700334 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800335 mWriteAllowed = false;
336
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700337 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800338 USAGE_GRAPHICS_TEXTURE |
339 USAGE_SCRIPT)) != 0) {
340 throw new RSIllegalArgumentException("Invalid usage combination.");
341 }
342 }
Jason Sams9bf18922013-04-13 19:48:36 -0700343
Jason Sams5476b452010-12-08 16:14:36 -0800344 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800345 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700346
Jason Sams452a7662011-07-07 16:05:18 -0700347 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700348 // TODO: A3D doesn't have Type info during creation, so we can't
349 // calculate the size ahead of time. We can possibly add a method
350 // to update the size in the future if it seems reasonable.
351 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700352 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700353 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700354 try {
355 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
356 } catch (Exception e) {
357 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
358 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
359 }
360 }
361
362 protected void finalize() throws Throwable {
363 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
364 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700365 }
366
Jason Sams3042d262013-11-25 18:28:33 -0800367 private void validateIsInt64() {
368 if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
369 (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
370 return;
371 }
372 throw new RSIllegalArgumentException(
373 "64 bit integer source does not match allocation type " + mType.mElement.mType);
374 }
375
Jason Samsb97b2512011-01-16 15:04:08 -0800376 private void validateIsInt32() {
377 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
378 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
379 return;
380 }
381 throw new RSIllegalArgumentException(
382 "32 bit integer source does not match allocation type " + mType.mElement.mType);
383 }
384
385 private void validateIsInt16() {
386 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
387 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
388 return;
389 }
390 throw new RSIllegalArgumentException(
391 "16 bit integer source does not match allocation type " + mType.mElement.mType);
392 }
393
394 private void validateIsInt8() {
395 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
396 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
397 return;
398 }
399 throw new RSIllegalArgumentException(
400 "8 bit integer source does not match allocation type " + mType.mElement.mType);
401 }
402
403 private void validateIsFloat32() {
404 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
405 return;
406 }
407 throw new RSIllegalArgumentException(
408 "32 bit float source does not match allocation type " + mType.mElement.mType);
409 }
410
Jason Sams3042d262013-11-25 18:28:33 -0800411 private void validateIsFloat64() {
412 if (mType.mElement.mType == Element.DataType.FLOAT_64) {
413 return;
414 }
415 throw new RSIllegalArgumentException(
416 "64 bit float source does not match allocation type " + mType.mElement.mType);
417 }
418
Jason Samsb97b2512011-01-16 15:04:08 -0800419 private void validateIsObject() {
420 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
421 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
422 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
423 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
424 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
425 (mType.mElement.mType == Element.DataType.RS_MESH) ||
426 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
427 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
428 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
429 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
430 return;
431 }
432 throw new RSIllegalArgumentException(
433 "Object source does not match allocation type " + mType.mElement.mType);
434 }
435
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700436 @Override
437 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800438 super.updateFromNative();
Tim Murray460a0492013-11-19 12:45:54 -0800439 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700440 if(typeID != 0) {
441 mType = new Type(typeID, mRS);
442 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700443 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700444 }
445 }
446
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700447 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700448 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700449 *
450 * @return Type
451 *
452 */
Jason Samsea87e962010-01-12 12:12:28 -0800453 public Type getType() {
454 return mType;
455 }
456
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700457 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700458 * Propagate changes from one usage of the Allocation to the
459 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700460 *
461 */
Jason Sams5476b452010-12-08 16:14:36 -0800462 public void syncAll(int srcLocation) {
Chris Craik06d29842015-06-02 17:19:24 -0700463 try {
464 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
465 switch (srcLocation) {
466 case USAGE_GRAPHICS_TEXTURE:
467 case USAGE_SCRIPT:
468 if ((mUsage & USAGE_SHARED) != 0) {
469 copyFrom(mBitmap);
470 }
471 break;
472 case USAGE_GRAPHICS_CONSTANTS:
473 case USAGE_GRAPHICS_VERTEX:
474 break;
475 case USAGE_SHARED:
476 if ((mUsage & USAGE_SHARED) != 0) {
477 copyTo(mBitmap);
478 }
479 break;
480 default:
481 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
Tim Murray78e64942013-04-09 17:28:56 -0700482 }
Chris Craik06d29842015-06-02 17:19:24 -0700483 mRS.validate();
484 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
485 } finally {
486 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800487 }
Jason Sams5476b452010-12-08 16:14:36 -0800488 }
489
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700490 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700491 * Send a buffer to the output stream. The contents of the Allocation will
492 * be undefined after this operation. This operation is only valid if {@link
493 * #USAGE_IO_OUTPUT} is set on the Allocation.
494 *
Jason Sams163766c2012-02-15 12:04:24 -0800495 *
Jason Sams163766c2012-02-15 12:04:24 -0800496 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700497 public void ioSend() {
Chris Craik06d29842015-06-02 17:19:24 -0700498 try {
499 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
500 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
501 throw new RSIllegalArgumentException(
502 "Can only send buffer if IO_OUTPUT usage specified.");
503 }
504 mRS.validate();
505 mRS.nAllocationIoSend(getID(mRS));
506 } finally {
507 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800508 }
Jason Sams163766c2012-02-15 12:04:24 -0800509 }
510
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700511 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700512 * Receive the latest input into the Allocation. This operation
513 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800514 *
Jason Sams163766c2012-02-15 12:04:24 -0800515 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700516 public void ioReceive() {
Chris Craik06d29842015-06-02 17:19:24 -0700517 try {
518 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
519 if ((mUsage & USAGE_IO_INPUT) == 0) {
520 throw new RSIllegalArgumentException(
521 "Can only receive if IO_INPUT usage specified.");
522 }
523 mRS.validate();
524 mRS.nAllocationIoReceive(getID(mRS));
525 } finally {
526 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800527 }
Jason Sams163766c2012-02-15 12:04:24 -0800528 }
529
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700530 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700531 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700532 *
533 * @param d Source array.
534 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800535 public void copyFrom(BaseObj[] d) {
Chris Craik06d29842015-06-02 17:19:24 -0700536 try {
537 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
538 mRS.validate();
539 validateIsObject();
540 if (d.length != mCurrentCount) {
541 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
542 mCurrentCount + ", array length = " + d.length);
543 }
Tim Murray460a0492013-11-19 12:45:54 -0800544
Chris Craik06d29842015-06-02 17:19:24 -0700545 if (RenderScript.sPointerSize == 8) {
546 long i[] = new long[d.length * 4];
547 for (int ct=0; ct < d.length; ct++) {
548 i[ct * 4] = d[ct].getID(mRS);
549 }
550 copy1DRangeFromUnchecked(0, mCurrentCount, i);
551 } else {
552 int i[] = new int[d.length];
553 for (int ct=0; ct < d.length; ct++) {
554 i[ct] = (int) d[ct].getID(mRS);
555 }
556 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray3de3dc72014-07-01 16:56:18 -0700557 }
Chris Craik06d29842015-06-02 17:19:24 -0700558 } finally {
559 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800560 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700561 }
562
Jason Samsfb9f82c2011-01-12 14:53:25 -0800563 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800564 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800565 if (bc == null) {
566 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
567 }
Jason Sams252c0782011-01-11 17:42:52 -0800568 switch (bc) {
569 case ALPHA_8:
570 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
571 throw new RSIllegalArgumentException("Allocation kind is " +
572 mType.getElement().mKind + ", type " +
573 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700574 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800575 " bytes, passed bitmap was " + bc);
576 }
577 break;
578 case ARGB_8888:
579 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700580 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800581 throw new RSIllegalArgumentException("Allocation kind is " +
582 mType.getElement().mKind + ", type " +
583 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700584 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800585 " bytes, passed bitmap was " + bc);
586 }
587 break;
588 case RGB_565:
589 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700590 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800591 throw new RSIllegalArgumentException("Allocation kind is " +
592 mType.getElement().mKind + ", type " +
593 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700594 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800595 " bytes, passed bitmap was " + bc);
596 }
597 break;
598 case ARGB_4444:
599 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700600 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800601 throw new RSIllegalArgumentException("Allocation kind is " +
602 mType.getElement().mKind + ", type " +
603 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700604 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800605 " bytes, passed bitmap was " + bc);
606 }
607 break;
608
609 }
Jason Sams4ef66502010-12-10 16:03:15 -0800610 }
611
Jason Samsfb9f82c2011-01-12 14:53:25 -0800612 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700613 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800614 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
615 }
616 }
617
Jason Sams3042d262013-11-25 18:28:33 -0800618 private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -0700619 try {
620 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
621 mRS.validate();
622 if (mCurrentDimZ > 0) {
623 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
624 } else if (mCurrentDimY > 0) {
625 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
626 } else {
627 copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
628 }
629 } finally {
630 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams3042d262013-11-25 18:28:33 -0800631 }
Jason Sams3042d262013-11-25 18:28:33 -0800632 }
633
634 /**
635 * Copy into this Allocation from an array. This method does not guarantee
636 * that the Allocation is compatible with the input buffer; it copies memory
637 * without reinterpretation.
638 *
639 * @param array The source data array
640 */
641 public void copyFromUnchecked(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700642 try {
643 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
644 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
645 java.lang.reflect.Array.getLength(array));
646 } finally {
647 Trace.traceEnd(RenderScript.TRACE_TAG);
648 }
Jason Sams3042d262013-11-25 18:28:33 -0800649 }
650
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700651 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700652 * Copy into this Allocation from an array. This method does not guarantee
653 * that the Allocation is compatible with the input buffer; it copies memory
654 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800655 *
656 * @param d the source data array
657 */
658 public void copyFromUnchecked(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800659 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800660 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700661
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700662 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700663 * Copy into this Allocation from an array. This method does not guarantee
664 * that the Allocation is compatible with the input buffer; it copies memory
665 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800666 *
667 * @param d the source data array
668 */
669 public void copyFromUnchecked(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800670 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800671 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700672
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700673 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700674 * Copy into this Allocation from an array. This method does not guarantee
675 * that the Allocation is compatible with the input buffer; it copies memory
676 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800677 *
678 * @param d the source data array
679 */
680 public void copyFromUnchecked(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800681 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800682 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700683
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700684 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700685 * Copy into this Allocation from an array. This method does not guarantee
686 * that the Allocation is compatible with the input buffer; it copies memory
687 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800688 *
689 * @param d the source data array
690 */
691 public void copyFromUnchecked(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800692 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800693 }
694
Tim Murray6d7a53c2013-05-23 16:59:23 -0700695
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700696 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700697 * Copy into this Allocation from an array. This variant is type checked
698 * and will generate exceptions if the Allocation's {@link
Jason Sams3042d262013-11-25 18:28:33 -0800699 * android.renderscript.Element} does not match the array's
700 * primitive type.
701 *
Ying Wang16229812013-11-26 15:45:12 -0800702 * @param array The source data array
Jason Sams3042d262013-11-25 18:28:33 -0800703 */
704 public void copyFrom(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700705 try {
706 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
707 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
708 java.lang.reflect.Array.getLength(array));
709 } finally {
710 Trace.traceEnd(RenderScript.TRACE_TAG);
711 }
Jason Sams3042d262013-11-25 18:28:33 -0800712 }
713
714 /**
715 * Copy into this Allocation from an array. This variant is type checked
716 * and will generate exceptions if the Allocation's {@link
Tim Murrayc11e25c2013-04-09 11:01:01 -0700717 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800718 *
719 * @param d the source data array
720 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800721 public void copyFrom(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800722 validateIsInt32();
723 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800724 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800725
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700726 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700727 * Copy into this Allocation from an array. This variant is type checked
728 * and will generate exceptions if the Allocation's {@link
729 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800730 *
731 * @param d the source data array
732 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800733 public void copyFrom(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800734 validateIsInt16();
735 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800736 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800737
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700738 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700739 * Copy into this Allocation from an array. This variant is type checked
740 * and will generate exceptions if the Allocation's {@link
741 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800742 *
743 * @param d the source data array
744 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800745 public void copyFrom(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800746 validateIsInt8();
747 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800748 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800749
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700750 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700751 * Copy into this Allocation from an array. This variant is type checked
752 * and will generate exceptions if the Allocation's {@link
753 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800754 *
755 * @param d the source data array
756 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800757 public void copyFrom(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800758 validateIsFloat32();
759 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800760 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800761
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700762 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700763 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
764 * height, width, and format of the bitmap must match the existing
765 * allocation.
766 *
767 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
768 * android.graphics.Bitmap} used to create the Allocation with {@link
769 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
770 * this will synchronize the Allocation with the latest data from the {@link
771 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800772 *
773 * @param b the source bitmap
774 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800775 public void copyFrom(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -0700776 try {
777 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
778 mRS.validate();
779 if (b.getConfig() == null) {
780 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
781 Canvas c = new Canvas(newBitmap);
782 c.drawBitmap(b, 0, 0, null);
783 copyFrom(newBitmap);
784 return;
785 }
786 validateBitmapSize(b);
787 validateBitmapFormat(b);
788 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
789 } finally {
790 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -0800791 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700792 }
793
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700794 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700795 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700796 * must be identical.
797 *
798 * @param a the source allocation
799 */
800 public void copyFrom(Allocation a) {
Chris Craik06d29842015-06-02 17:19:24 -0700801 try {
802 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
803 mRS.validate();
804 if (!mType.equals(a.getType())) {
805 throw new RSIllegalArgumentException("Types of allocations must match.");
806 }
807 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
808 } finally {
809 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700810 }
Tim Murrayf671fb02012-10-03 13:50:05 -0700811 }
812
Tim Murrayf671fb02012-10-03 13:50:05 -0700813 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700814 * This is only intended to be used by auto-generated code reflected from
815 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800816 *
817 * @param xoff
818 * @param fp
819 */
Jason Sams21b41032011-01-16 15:05:41 -0800820 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800821 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700822 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700823 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700824 int data_length = fp.getPos();
Jason Samsa70f4162010-03-26 15:33:42 -0700825
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700826 int count = data_length / eSize;
827 if ((eSize * count) != data_length) {
828 throw new RSIllegalArgumentException("Field packer length " + data_length +
Jason Samsa70f4162010-03-26 15:33:42 -0700829 " not divisible by element size " + eSize + ".");
830 }
Jason Samsba862d12011-07-07 15:24:42 -0700831 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700832 }
833
Miao Wang45cec0a2015-03-04 16:40:21 -0800834
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700835 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700836 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -0800837 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800838 *
839 * @param xoff
840 * @param component_number
841 * @param fp
842 */
Jason Sams21b41032011-01-16 15:05:41 -0800843 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800844 setFromFieldPacker(xoff, 0, 0, component_number, fp);
845 }
846
847 /**
Miao Wangc8e237e2015-02-20 18:36:32 -0800848 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -0800849 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -0800850 *
851 * @param xoff
852 * @param yoff
Miao Wangc8e237e2015-02-20 18:36:32 -0800853 * @param zoff
854 * @param component_number
855 * @param fp
856 */
857 public void setFromFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800858 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700859 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800860 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700861 }
862 if(xoff < 0) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800863 throw new RSIllegalArgumentException("Offset x must be >= 0.");
864 }
865 if(yoff < 0) {
866 throw new RSIllegalArgumentException("Offset y must be >= 0.");
867 }
868 if(zoff < 0) {
869 throw new RSIllegalArgumentException("Offset z must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700870 }
871
872 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700873 int data_length = fp.getPos();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700874 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800875 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700876
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700877 if (data_length != eSize) {
878 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700879 " does not match component size " + eSize + ".");
880 }
881
Miao Wangc8e237e2015-02-20 18:36:32 -0800882 mRS.nAllocationElementData(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
883 component_number, data, data_length);
Jason Samsa70f4162010-03-26 15:33:42 -0700884 }
885
Miao Wang87e908d2015-03-02 15:15:15 -0800886 private void data1DChecks(int off, int count, int len, int dataSize, boolean usePadding) {
Jason Sams771bebb2009-12-07 12:40:12 -0800887 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700888 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800889 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700890 }
891 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800892 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700893 }
Jason Samsba862d12011-07-07 15:24:42 -0700894 if((off + count) > mCurrentCount) {
895 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700896 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700897 }
Miao Wang87e908d2015-03-02 15:15:15 -0800898 if(usePadding) {
899 if(len < dataSize / 4 * 3) {
900 throw new RSIllegalArgumentException("Array too small for allocation type.");
901 }
902 } else {
903 if(len < dataSize) {
904 throw new RSIllegalArgumentException("Array too small for allocation type.");
905 }
Jason Sams768bc022009-09-21 19:41:04 -0700906 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700907 }
908
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700909 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700910 * Generate a mipmap chain. This is only valid if the Type of the Allocation
911 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800912 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700913 * <p>This function will generate a complete set of mipmaps from the top
914 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800915 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700916 * <p>If the Allocation is also using other memory spaces, a call to {@link
917 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800918 */
919 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700920 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800921 }
922
Jason Sams3042d262013-11-25 18:28:33 -0800923 private void copy1DRangeFromUnchecked(int off, int count, Object array,
924 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -0700925 try {
926 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
927 final int dataSize = mType.mElement.getBytesSize() * count;
928 // AutoPadding for Vec3 Element
929 boolean usePadding = false;
930 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
931 usePadding = true;
932 }
933 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
934 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
935 mType.mElement.mType.mSize, usePadding);
936 } finally {
937 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -0800938 }
Jason Sams3042d262013-11-25 18:28:33 -0800939 }
940
941 /**
942 * Copy an array into part of this Allocation. This method does not
943 * guarantee that the Allocation is compatible with the input buffer.
944 *
945 * @param off The offset of the first element to be copied.
946 * @param count The number of elements to be copied.
947 * @param array The source data array
948 */
949 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
950 copy1DRangeFromUnchecked(off, count, array,
951 validateObjectIsPrimitiveArray(array, false),
952 java.lang.reflect.Array.getLength(array));
953 }
954
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700955 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700956 * Copy an array into part of this Allocation. This method does not
957 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800958 *
959 * @param off The offset of the first element to be copied.
960 * @param count The number of elements to be copied.
961 * @param d the source data array
962 */
963 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800964 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700965 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700966
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700967 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700968 * Copy an array into part of this Allocation. This method does not
969 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800970 *
971 * @param off The offset of the first element to be copied.
972 * @param count The number of elements to be copied.
973 * @param d the source data array
974 */
975 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800976 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700977 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700978
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700979 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700980 * Copy an array into part of this Allocation. This method does not
981 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800982 *
983 * @param off The offset of the first element to be copied.
984 * @param count The number of elements to be copied.
985 * @param d the source data array
986 */
987 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800988 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700989 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700990
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700991 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700992 * Copy an array into part of this Allocation. This method does not
993 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800994 *
995 * @param off The offset of the first element to be copied.
996 * @param count The number of elements to be copied.
997 * @param d the source data array
998 */
999 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -08001000 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
1001 }
1002
1003
1004 /**
1005 * Copy an array into part of this Allocation. This variant is type checked
1006 * and will generate exceptions if the Allocation type does not
1007 * match the component type of the array passed in.
1008 *
1009 * @param off The offset of the first element to be copied.
1010 * @param count The number of elements to be copied.
1011 * @param array The source data array.
1012 */
1013 public void copy1DRangeFrom(int off, int count, Object array) {
1014 copy1DRangeFromUnchecked(off, count, array,
1015 validateObjectIsPrimitiveArray(array, true),
1016 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -07001017 }
1018
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001019 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001020 * Copy an array into part of this Allocation. This variant is type checked
1021 * and will generate exceptions if the Allocation type is not a 32 bit
1022 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001023 *
1024 * @param off The offset of the first element to be copied.
1025 * @param count The number of elements to be copied.
1026 * @param d the source data array
1027 */
Jason Samsb97b2512011-01-16 15:04:08 -08001028 public void copy1DRangeFrom(int off, int count, int[] d) {
1029 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001030 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001031 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001032
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001033 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001034 * Copy an array into part of this Allocation. This variant is type checked
1035 * and will generate exceptions if the Allocation type is not a 16 bit
1036 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001037 *
1038 * @param off The offset of the first element to be copied.
1039 * @param count The number of elements to be copied.
1040 * @param d the source data array
1041 */
Jason Samsb97b2512011-01-16 15:04:08 -08001042 public void copy1DRangeFrom(int off, int count, short[] d) {
1043 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001044 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001045 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001046
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001047 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001048 * Copy an array into part of this Allocation. This variant is type checked
1049 * and will generate exceptions if the Allocation type is not an 8 bit
1050 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001051 *
1052 * @param off The offset of the first element to be copied.
1053 * @param count The number of elements to be copied.
1054 * @param d the source data array
1055 */
Jason Samsb97b2512011-01-16 15:04:08 -08001056 public void copy1DRangeFrom(int off, int count, byte[] d) {
1057 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001058 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001059 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001060
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001061 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001062 * Copy an array into part of this Allocation. This variant is type checked
1063 * and will generate exceptions if the Allocation type is not a 32 bit float
1064 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001065 *
1066 * @param off The offset of the first element to be copied.
1067 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001068 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001069 */
Jason Samsb97b2512011-01-16 15:04:08 -08001070 public void copy1DRangeFrom(int off, int count, float[] d) {
1071 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001072 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001073 }
Jason Sams3042d262013-11-25 18:28:33 -08001074
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001075 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001076 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001077 *
1078 * @param off The offset of the first element to be copied.
1079 * @param count The number of elements to be copied.
1080 * @param data the source data allocation.
1081 * @param dataOff off The offset of the first element in data to
1082 * be copied.
1083 */
1084 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001085 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -07001086 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001087 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001088 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001089 data.mSelectedLOD, data.mSelectedFace.mID);
Chris Craik5c705d62015-06-01 10:39:36 -07001090 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001091 }
1092
Jason Samsfb9f82c2011-01-12 14:53:25 -08001093 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001094 if (mAdaptedAllocation != null) {
1095
1096 } else {
1097
1098 if (xoff < 0 || yoff < 0) {
1099 throw new RSIllegalArgumentException("Offset cannot be negative.");
1100 }
1101 if (h < 0 || w < 0) {
1102 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1103 }
1104 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1105 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1106 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001107 }
1108 }
Jason Sams768bc022009-09-21 19:41:04 -07001109
Jason Sams3042d262013-11-25 18:28:33 -08001110 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1111 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001112 try {
1113 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
1114 mRS.validate();
1115 validate2DRange(xoff, yoff, w, h);
1116 final int dataSize = mType.mElement.getBytesSize() * w * h;
1117 // AutoPadding for Vec3 Element
1118 boolean usePadding = false;
1119 int sizeBytes = arrayLen * dt.mSize;
1120 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1121 if (dataSize / 4 * 3 > sizeBytes) {
1122 throw new RSIllegalArgumentException("Array too small for allocation type.");
1123 }
1124 usePadding = true;
1125 sizeBytes = dataSize;
1126 } else {
1127 if (dataSize > sizeBytes) {
1128 throw new RSIllegalArgumentException("Array too small for allocation type.");
1129 }
Miao Wang87e908d2015-03-02 15:15:15 -08001130 }
Chris Craik06d29842015-06-02 17:19:24 -07001131 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1132 array, sizeBytes, dt,
1133 mType.mElement.mType.mSize, usePadding);
1134 } finally {
1135 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001136 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001137 }
1138
Jason Sams3042d262013-11-25 18:28:33 -08001139 /**
1140 * Copy from an array into a rectangular region in this Allocation. The
1141 * array is assumed to be tightly packed.
1142 *
1143 * @param xoff X offset of the region to update in this Allocation
1144 * @param yoff Y offset of the region to update in this Allocation
1145 * @param w Width of the region to update
1146 * @param h Height of the region to update
Ying Wang16229812013-11-26 15:45:12 -08001147 * @param array Data to be placed into the Allocation
Jason Sams3042d262013-11-25 18:28:33 -08001148 */
1149 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001150 try {
1151 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1152 copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
1153 validateObjectIsPrimitiveArray(array, true),
1154 java.lang.reflect.Array.getLength(array));
1155 } finally {
1156 Trace.traceEnd(RenderScript.TRACE_TAG);
1157 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001158 }
1159
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001160 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001161 * Copy from an array into a rectangular region in this Allocation. The
1162 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -08001163 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001164 * @param xoff X offset of the region to update in this Allocation
1165 * @param yoff Y offset of the region to update in this Allocation
1166 * @param w Width of the region to update
1167 * @param h Height of the region to update
1168 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001169 */
1170 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001171 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001172 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1173 Element.DataType.SIGNED_8, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001174 }
1175
Tim Murrayc11e25c2013-04-09 11:01:01 -07001176 /**
1177 * Copy from an array into a rectangular region in this Allocation. The
1178 * array is assumed to be tightly packed.
1179 *
1180 * @param xoff X offset of the region to update in this Allocation
1181 * @param yoff Y offset of the region to update in this Allocation
1182 * @param w Width of the region to update
1183 * @param h Height of the region to update
1184 * @param data to be placed into the Allocation
1185 */
Jason Samsf7086092011-01-12 13:28:37 -08001186 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001187 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001188 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1189 Element.DataType.SIGNED_16, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001190 }
1191
Tim Murrayc11e25c2013-04-09 11:01:01 -07001192 /**
1193 * Copy from an array into a rectangular region in this Allocation. The
1194 * array is assumed to be tightly packed.
1195 *
1196 * @param xoff X offset of the region to update in this Allocation
1197 * @param yoff Y offset of the region to update in this Allocation
1198 * @param w Width of the region to update
1199 * @param h Height of the region to update
1200 * @param data to be placed into the Allocation
1201 */
Jason Samsf7086092011-01-12 13:28:37 -08001202 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001203 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001204 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1205 Element.DataType.SIGNED_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001206 }
1207
Tim Murrayc11e25c2013-04-09 11:01:01 -07001208 /**
1209 * Copy from an array into a rectangular region in this Allocation. The
1210 * array is assumed to be tightly packed.
1211 *
1212 * @param xoff X offset of the region to update in this Allocation
1213 * @param yoff Y offset of the region to update in this Allocation
1214 * @param w Width of the region to update
1215 * @param h Height of the region to update
1216 * @param data to be placed into the Allocation
1217 */
Jason Samsf7086092011-01-12 13:28:37 -08001218 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001219 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001220 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1221 Element.DataType.FLOAT_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001222 }
1223
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001224 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001225 * Copy a rectangular region from an Allocation into a rectangular region in
1226 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001227 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001228 * @param xoff X offset of the region in this Allocation
1229 * @param yoff Y offset of the region in this Allocation
1230 * @param w Width of the region to update.
1231 * @param h Height of the region to update.
1232 * @param data source Allocation.
1233 * @param dataXoff X offset in source Allocation
1234 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001235 */
1236 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1237 Allocation data, int dataXoff, int dataYoff) {
Chris Craik06d29842015-06-02 17:19:24 -07001238 try {
1239 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1240 mRS.validate();
1241 validate2DRange(xoff, yoff, w, h);
1242 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
1243 mSelectedLOD, mSelectedFace.mID,
1244 w, h, data.getID(mRS), dataXoff, dataYoff,
1245 data.mSelectedLOD, data.mSelectedFace.mID);
1246 } finally {
1247 Trace.traceEnd(RenderScript.TRACE_TAG);
1248 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001249 }
1250
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001251 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001252 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1253 * and width of the update will use the height and width of the {@link
1254 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001255 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001256 * @param xoff X offset of the region to update in this Allocation
1257 * @param yoff Y offset of the region to update in this Allocation
1258 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001259 */
1260 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Chris Craik5c705d62015-06-01 10:39:36 -07001261 try {
1262 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1263 mRS.validate();
1264 if (data.getConfig() == null) {
1265 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1266 Canvas c = new Canvas(newBitmap);
1267 c.drawBitmap(data, 0, 0, null);
1268 copy2DRangeFrom(xoff, yoff, newBitmap);
1269 return;
1270 }
1271 validateBitmapFormat(data);
1272 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
1273 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
1274 } finally {
1275 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -08001276 }
Jason Samsfa445b92011-01-07 17:00:07 -08001277 }
1278
Jason Samsb05d6892013-04-09 15:59:24 -07001279 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1280 if (mAdaptedAllocation != null) {
1281
1282 } else {
1283
1284 if (xoff < 0 || yoff < 0 || zoff < 0) {
1285 throw new RSIllegalArgumentException("Offset cannot be negative.");
1286 }
1287 if (h < 0 || w < 0 || d < 0) {
1288 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1289 }
1290 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1291 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1292 }
1293 }
1294 }
1295
1296 /**
Miao Wang258db502015-03-03 14:05:36 -08001297 * Copy a rectangular region from the array into the allocation.
1298 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001299 *
Miao Wang258db502015-03-03 14:05:36 -08001300 * The data type of the array is not required to be the same as
1301 * the element data type.
Jason Samsb05d6892013-04-09 15:59:24 -07001302 */
Jason Sams3042d262013-11-25 18:28:33 -08001303 private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1304 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001305 try {
1306 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
1307 mRS.validate();
1308 validate3DRange(xoff, yoff, zoff, w, h, d);
1309 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1310 // AutoPadding for Vec3 Element
1311 boolean usePadding = false;
1312 int sizeBytes = arrayLen * dt.mSize;
1313 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1314 if (dataSize / 4 * 3 > sizeBytes) {
1315 throw new RSIllegalArgumentException("Array too small for allocation type.");
1316 }
1317 usePadding = true;
1318 sizeBytes = dataSize;
1319 } else {
1320 if (dataSize > sizeBytes) {
1321 throw new RSIllegalArgumentException("Array too small for allocation type.");
1322 }
Miao Wang87e908d2015-03-02 15:15:15 -08001323 }
Chris Craik06d29842015-06-02 17:19:24 -07001324 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1325 array, sizeBytes, dt,
1326 mType.mElement.mType.mSize, usePadding);
1327 } finally {
1328 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001329 }
Jason Samsb05d6892013-04-09 15:59:24 -07001330 }
1331
1332 /**
Jason Samsb05d6892013-04-09 15:59:24 -07001333 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001334 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001335 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001336 * @param xoff X offset of the region to update in this Allocation
1337 * @param yoff Y offset of the region to update in this Allocation
1338 * @param zoff Z offset of the region to update in this Allocation
1339 * @param w Width of the region to update
1340 * @param h Height of the region to update
1341 * @param d Depth of the region to update
Miao Wang87e908d2015-03-02 15:15:15 -08001342 * @param array to be placed into the allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001343 */
Jason Sams3042d262013-11-25 18:28:33 -08001344 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001345 try {
1346 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
1347 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
1348 validateObjectIsPrimitiveArray(array, true),
1349 java.lang.reflect.Array.getLength(array));
1350 } finally {
1351 Trace.traceEnd(RenderScript.TRACE_TAG);
1352 }
Jason Samsb05d6892013-04-09 15:59:24 -07001353 }
1354
1355 /**
Jason Samsb05d6892013-04-09 15:59:24 -07001356 * Copy a rectangular region into the allocation from another
1357 * allocation.
1358 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001359 * @param xoff X offset of the region to update in this Allocation
1360 * @param yoff Y offset of the region to update in this Allocation
1361 * @param zoff Z offset of the region to update in this Allocation
1362 * @param w Width of the region to update.
1363 * @param h Height of the region to update.
1364 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001365 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001366 * @param dataXoff X offset of the region in the source Allocation
1367 * @param dataYoff Y offset of the region in the source Allocation
1368 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001369 */
1370 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1371 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1372 mRS.validate();
1373 validate3DRange(xoff, yoff, zoff, w, h, d);
1374 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1375 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1376 data.mSelectedLOD);
1377 }
1378
Jason Samsfa445b92011-01-07 17:00:07 -08001379
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001380 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001381 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1382 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001383 *
1384 * @param b The bitmap to be set from the Allocation.
1385 */
Jason Samsfa445b92011-01-07 17:00:07 -08001386 public void copyTo(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -07001387 try {
1388 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1389 mRS.validate();
1390 validateBitmapFormat(b);
1391 validateBitmapSize(b);
1392 mRS.nAllocationCopyToBitmap(getID(mRS), b);
1393 } finally {
1394 Trace.traceEnd(RenderScript.TRACE_TAG);
1395 }
Jason Samsfa445b92011-01-07 17:00:07 -08001396 }
1397
Jason Sams3042d262013-11-25 18:28:33 -08001398 private void copyTo(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001399 try {
1400 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1401 mRS.validate();
1402 boolean usePadding = false;
1403 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1404 usePadding = true;
Miao Wangd9b63282015-04-03 09:15:39 -07001405 }
Chris Craik06d29842015-06-02 17:19:24 -07001406 if (usePadding) {
1407 if (dt.mSize * arrayLen < mSize / 4 * 3) {
1408 throw new RSIllegalArgumentException(
1409 "Size of output array cannot be smaller than size of allocation.");
1410 }
1411 } else {
1412 if (dt.mSize * arrayLen < mSize) {
1413 throw new RSIllegalArgumentException(
1414 "Size of output array cannot be smaller than size of allocation.");
1415 }
Miao Wangd9b63282015-04-03 09:15:39 -07001416 }
Chris Craik06d29842015-06-02 17:19:24 -07001417 mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
1418 } finally {
1419 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wangd9b63282015-04-03 09:15:39 -07001420 }
Jason Sams3042d262013-11-25 18:28:33 -08001421 }
1422
1423 /**
1424 * Copy from the Allocation into an array. The array must be at
1425 * least as large as the Allocation. The
1426 * {@link android.renderscript.Element} must match the component
1427 * type of the array passed in.
1428 *
1429 * @param array The array to be set from the Allocation.
1430 */
1431 public void copyTo(Object array) {
1432 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1433 java.lang.reflect.Array.getLength(array));
1434 }
1435
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001436 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001437 * Copy from the Allocation into a byte array. The array must be at least
1438 * as large as the Allocation. The allocation must be of an 8 bit integer
1439 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001440 *
1441 * @param d The array to be set from the Allocation.
1442 */
Jason Samsfa445b92011-01-07 17:00:07 -08001443 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001444 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001445 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001446 }
1447
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001448 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001449 * Copy from the Allocation into a short array. The array must be at least
1450 * as large as the Allocation. The allocation must be of an 16 bit integer
1451 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001452 *
1453 * @param d The array to be set from the Allocation.
1454 */
Jason Samsfa445b92011-01-07 17:00:07 -08001455 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001456 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001457 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001458 }
1459
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001460 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001461 * Copy from the Allocation into a int array. The array must be at least as
1462 * large as the Allocation. The allocation must be of an 32 bit integer
1463 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001464 *
1465 * @param d The array to be set from the Allocation.
1466 */
Jason Samsfa445b92011-01-07 17:00:07 -08001467 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001468 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001469 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001470 }
1471
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001472 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001473 * Copy from the Allocation into a float array. The array must be at least
1474 * as large as the Allocation. The allocation must be of an 32 bit float
1475 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001476 *
1477 * @param d The array to be set from the Allocation.
1478 */
Jason Samsfa445b92011-01-07 17:00:07 -08001479 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001480 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001481 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001482 }
1483
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001484 /**
Miao Wang3c613272015-05-11 11:41:55 -07001485 * @hide
1486 *
Miao Wang45cec0a2015-03-04 16:40:21 -08001487 * This is only intended to be used by auto-generated code reflected from
1488 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -08001489 *
1490 * @param xoff
1491 * @param yoff
1492 * @param zoff
1493 * @param component_number
Miao Wang258db502015-03-03 14:05:36 -08001494 * @param fp
Miao Wangc8e237e2015-02-20 18:36:32 -08001495 */
Miao Wang45cec0a2015-03-04 16:40:21 -08001496 public void copyToFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001497 mRS.validate();
1498 if (component_number >= mType.mElement.mElements.length) {
1499 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
1500 }
1501 if(xoff < 0) {
1502 throw new RSIllegalArgumentException("Offset x must be >= 0.");
1503 }
1504 if(yoff < 0) {
1505 throw new RSIllegalArgumentException("Offset y must be >= 0.");
1506 }
1507 if(zoff < 0) {
1508 throw new RSIllegalArgumentException("Offset z must be >= 0.");
1509 }
1510
Miao Wang45cec0a2015-03-04 16:40:21 -08001511 final byte[] data = fp.getData();
Miao Wangbfa5e652015-05-04 15:29:25 -07001512 int data_length = data.length;
Miao Wangc8e237e2015-02-20 18:36:32 -08001513 int eSize = mType.mElement.mElements[component_number].getBytesSize();
1514 eSize *= mType.mElement.mArraySizes[component_number];
1515
Miao Wang45cec0a2015-03-04 16:40:21 -08001516 if (data_length != eSize) {
1517 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
1518 " does not match component size " + eSize + ".");
Miao Wangc8e237e2015-02-20 18:36:32 -08001519 }
1520
1521 mRS.nAllocationElementRead(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Miao Wang45cec0a2015-03-04 16:40:21 -08001522 component_number, data, data_length);
Miao Wangc8e237e2015-02-20 18:36:32 -08001523 }
1524 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001525 * Resize a 1D allocation. The contents of the allocation are preserved.
1526 * If new elements are allocated objects are created with null contents and
1527 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001528 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001529 * <p>If the new region is smaller the references of any objects outside the
1530 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001531 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001532 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001533 *
1534 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001535 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001536 * @deprecated RenderScript objects should be immutable once created. The
Tim Murraycd38b762014-08-13 13:20:25 -07001537 * replacement is to create a new allocation and copy the contents. This
1538 * function will throw an exception if API 21 or higher is used.
Jason Samsf7086092011-01-12 13:28:37 -08001539 */
Jason Sams31a7e422010-10-26 13:09:17 -07001540 public synchronized void resize(int dimX) {
Tim Murraycd38b762014-08-13 13:20:25 -07001541 if (mRS.getApplicationContext().getApplicationInfo().targetSdkVersion >= 21) {
1542 throw new RSRuntimeException("Resize is not allowed in API 21+.");
1543 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001544 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001545 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001546 }
Jason Samse07694b2012-04-03 15:36:36 -07001547 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001548 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001549
Tim Murray460a0492013-11-19 12:45:54 -08001550 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001551 mType = new Type(typeID, mRS);
1552 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001553 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001554 }
1555
Miao Wangc8e237e2015-02-20 18:36:32 -08001556 private void copy1DRangeToUnchecked(int off, int count, Object array,
1557 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001558 try {
1559 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked");
1560 final int dataSize = mType.mElement.getBytesSize() * count;
1561 // AutoPadding for Vec3 Element
1562 boolean usePadding = false;
1563 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1564 usePadding = true;
1565 }
1566 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
1567 mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
1568 mType.mElement.mType.mSize, usePadding);
1569 } finally {
1570 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001571 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001572 }
1573
1574 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001575 * Copy part of this Allocation into an array. This method does not
1576 * guarantee that the Allocation is compatible with the input buffer.
1577 *
1578 * @param off The offset of the first element to be copied.
1579 * @param count The number of elements to be copied.
1580 * @param array The dest data array
1581 */
1582 public void copy1DRangeToUnchecked(int off, int count, Object array) {
1583 copy1DRangeToUnchecked(off, count, array,
1584 validateObjectIsPrimitiveArray(array, false),
1585 java.lang.reflect.Array.getLength(array));
1586 }
1587
1588 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001589 * Copy part of this Allocation into an array. This method does not
1590 * guarantee that the Allocation is compatible with the input buffer.
1591 *
1592 * @param off The offset of the first element to be copied.
1593 * @param count The number of elements to be copied.
1594 * @param d the source data array
1595 */
1596 public void copy1DRangeToUnchecked(int off, int count, int[] d) {
1597 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
1598 }
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 d the source data array
1607 */
1608 public void copy1DRangeToUnchecked(int off, int count, short[] d) {
1609 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
1610 }
1611
1612 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001613 * Copy part of this Allocation into an array. This method does not
1614 * guarantee that the Allocation is compatible with the input buffer.
1615 *
1616 * @param off The offset of the first element to be copied.
1617 * @param count The number of elements to be copied.
1618 * @param d the source data array
1619 */
1620 public void copy1DRangeToUnchecked(int off, int count, byte[] d) {
1621 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
1622 }
1623
1624 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001625 * Copy part of this Allocation into an array. This method does not
1626 * guarantee that the Allocation is compatible with the input buffer.
1627 *
1628 * @param off The offset of the first element to be copied.
1629 * @param count The number of elements to be copied.
1630 * @param d the source data array
1631 */
1632 public void copy1DRangeToUnchecked(int off, int count, float[] d) {
1633 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
1634 }
1635
1636
1637 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001638 * Copy part of this Allocation into an array. This method does not
1639 * and will generate exceptions if the Allocation type does not
1640 * match the component type of the array passed in.
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 array The source data array.
1645 */
1646 public void copy1DRangeTo(int off, int count, Object array) {
1647 copy1DRangeToUnchecked(off, count, array,
1648 validateObjectIsPrimitiveArray(array, true),
1649 java.lang.reflect.Array.getLength(array));
1650 }
1651
1652 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001653 * Copy part of this Allocation into an array. This method does not
1654 * and will generate exceptions if the Allocation type is not a 32 bit
1655 * integer type.
1656 *
1657 * @param off The offset of the first element to be copied.
1658 * @param count The number of elements to be copied.
1659 * @param d the source data array
1660 */
1661 public void copy1DRangeTo(int off, int count, int[] d) {
1662 validateIsInt32();
1663 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
1664 }
1665
1666 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001667 * Copy part of this Allocation into an array. This method does not
1668 * and will generate exceptions if the Allocation type is not a 16 bit
1669 * integer type.
1670 *
1671 * @param off The offset of the first element to be copied.
1672 * @param count The number of elements to be copied.
1673 * @param d the source data array
1674 */
1675 public void copy1DRangeTo(int off, int count, short[] d) {
1676 validateIsInt16();
1677 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
1678 }
1679
1680 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001681 * Copy part of this Allocation into an array. This method does not
1682 * and will generate exceptions if the Allocation type is not an 8 bit
1683 * integer type.
1684 *
1685 * @param off The offset of the first element to be copied.
1686 * @param count The number of elements to be copied.
1687 * @param d the source data array
1688 */
1689 public void copy1DRangeTo(int off, int count, byte[] d) {
1690 validateIsInt8();
1691 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
1692 }
1693
1694 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001695 * Copy part of this Allocation into an array. This method does not
1696 * and will generate exceptions if the Allocation type is not a 32 bit float
1697 * type.
1698 *
1699 * @param off The offset of the first element to be copied.
1700 * @param count The number of elements to be copied.
1701 * @param d the source data array.
1702 */
1703 public void copy1DRangeTo(int off, int count, float[] d) {
1704 validateIsFloat32();
1705 copy1DRangeToUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
1706 }
1707
1708
1709 void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
1710 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001711 try {
1712 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked");
1713 mRS.validate();
1714 validate2DRange(xoff, yoff, w, h);
1715 final int dataSize = mType.mElement.getBytesSize() * w * h;
1716 // AutoPadding for Vec3 Element
1717 boolean usePadding = false;
1718 int sizeBytes = arrayLen * dt.mSize;
1719 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1720 if (dataSize / 4 * 3 > sizeBytes) {
1721 throw new RSIllegalArgumentException("Array too small for allocation type.");
1722 }
1723 usePadding = true;
1724 sizeBytes = dataSize;
1725 } else {
1726 if (dataSize > sizeBytes) {
1727 throw new RSIllegalArgumentException("Array too small for allocation type.");
1728 }
Miao Wang87e908d2015-03-02 15:15:15 -08001729 }
Chris Craik06d29842015-06-02 17:19:24 -07001730 mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1731 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
1732 } finally {
1733 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001734 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001735 }
1736
1737 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001738 * Copy from a rectangular region in this Allocation into an array.
1739 *
1740 * @param xoff X offset of the region to copy in this Allocation
1741 * @param yoff Y offset of the region to copy in this Allocation
1742 * @param w Width of the region to copy
1743 * @param h Height of the region to copy
1744 * @param array Dest Array to be copied into
1745 */
1746 public void copy2DRangeTo(int xoff, int yoff, int w, int h, Object array) {
1747 copy2DRangeToUnchecked(xoff, yoff, w, h, array,
1748 validateObjectIsPrimitiveArray(array, true),
1749 java.lang.reflect.Array.getLength(array));
1750 }
1751
1752 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001753 * Copy from a rectangular region in this Allocation into an array.
1754 *
1755 * @param xoff X offset of the region to copy in this Allocation
1756 * @param yoff Y offset of the region to copy in this Allocation
1757 * @param w Width of the region to copy
1758 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001759 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001760 */
1761 public void copy2DRangeTo(int xoff, int yoff, int w, int h, byte[] data) {
1762 validateIsInt8();
1763 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1764 Element.DataType.SIGNED_8, data.length);
1765 }
1766
1767 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001768 * Copy from a rectangular region in this Allocation into an array.
1769 *
1770 * @param xoff X offset of the region to copy in this Allocation
1771 * @param yoff Y offset of the region to copy in this Allocation
1772 * @param w Width of the region to copy
1773 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001774 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001775 */
1776 public void copy2DRangeTo(int xoff, int yoff, int w, int h, short[] data) {
1777 validateIsInt16();
1778 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1779 Element.DataType.SIGNED_16, data.length);
1780 }
1781
1782 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001783 * Copy from a rectangular region in this Allocation into an array.
1784 *
1785 * @param xoff X offset of the region to copy in this Allocation
1786 * @param yoff Y offset of the region to copy in this Allocation
1787 * @param w Width of the region to copy
1788 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001789 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001790 */
1791 public void copy2DRangeTo(int xoff, int yoff, int w, int h, int[] data) {
1792 validateIsInt32();
1793 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1794 Element.DataType.SIGNED_32, data.length);
1795 }
1796
1797 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001798 * Copy from a rectangular region in this Allocation into an array.
1799 *
1800 * @param xoff X offset of the region to copy in this Allocation
1801 * @param yoff Y offset of the region to copy in this Allocation
1802 * @param w Width of the region to copy
1803 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001804 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001805 */
1806 public void copy2DRangeTo(int xoff, int yoff, int w, int h, float[] data) {
1807 validateIsFloat32();
1808 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1809 Element.DataType.FLOAT_32, data.length);
1810 }
1811
1812
1813 /**
Miao Wang258db502015-03-03 14:05:36 -08001814 * Copy from a rectangular region in this Allocation into an array.
1815 * The array is assumed to be tightly packed.
Miao Wangc8e237e2015-02-20 18:36:32 -08001816 *
Miao Wang258db502015-03-03 14:05:36 -08001817 * The data type of the array is not required to be the same as
1818 * the element data type.
Miao Wangc8e237e2015-02-20 18:36:32 -08001819 */
1820 private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1821 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001822 try {
1823 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked");
1824 mRS.validate();
1825 validate3DRange(xoff, yoff, zoff, w, h, d);
1826 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1827 // AutoPadding for Vec3 Element
1828 boolean usePadding = false;
1829 int sizeBytes = arrayLen * dt.mSize;
1830 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1831 if (dataSize / 4 * 3 > sizeBytes) {
1832 throw new RSIllegalArgumentException("Array too small for allocation type.");
1833 }
1834 usePadding = true;
1835 sizeBytes = dataSize;
1836 } else {
1837 if (dataSize > sizeBytes) {
1838 throw new RSIllegalArgumentException("Array too small for allocation type.");
1839 }
Miao Wang87e908d2015-03-02 15:15:15 -08001840 }
Chris Craik06d29842015-06-02 17:19:24 -07001841 mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1842 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
1843 } finally {
1844 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001845 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001846 }
1847
Miao Wang258db502015-03-03 14:05:36 -08001848 /*
Miao Wangc8e237e2015-02-20 18:36:32 -08001849 * Copy from a rectangular region in this Allocation into an array.
1850 *
1851 * @param xoff X offset of the region to copy in this Allocation
1852 * @param yoff Y offset of the region to copy in this Allocation
1853 * @param zoff Z offset of the region to copy in this Allocation
1854 * @param w Width of the region to copy
1855 * @param h Height of the region to copy
1856 * @param d Depth of the region to copy
1857 * @param array Dest Array to be copied into
1858 */
1859 public void copy3DRangeTo(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1860 copy3DRangeToUnchecked(xoff, yoff, zoff, w, h, d, array,
1861 validateObjectIsPrimitiveArray(array, true),
1862 java.lang.reflect.Array.getLength(array));
1863 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001864
1865 // creation
1866
Jason Sams49a05d72010-12-29 14:31:29 -08001867 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001868 static {
1869 mBitmapOptions.inScaled = false;
1870 }
1871
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001872 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001873 * Creates a new Allocation with the given {@link
1874 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001875 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001876 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001877 * @param mips specifies desired mipmap behaviour for the
1878 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001879 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001880 * utilized
1881 */
1882 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07001883 try {
1884 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
1885 rs.validate();
1886 if (type.getID(rs) == 0) {
1887 throw new RSInvalidStateException("Bad Type");
1888 }
1889 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
1890 if (id == 0) {
1891 throw new RSRuntimeException("Allocation creation failed.");
1892 }
1893 return new Allocation(id, rs, type, usage);
1894 } finally {
1895 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams1bada8c2009-08-09 17:01:55 -07001896 }
Jason Sams857d0c72011-11-23 15:02:15 -08001897 }
1898
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001899 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001900 * Creates an Allocation with the size specified by the type and no mipmaps
1901 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001902 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001903 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001904 * @param type renderscript type describing data layout
1905 * @param usage bit field specifying how the allocation is
1906 * utilized
1907 *
1908 * @return allocation
1909 */
Jason Samse5d37122010-12-16 00:33:33 -08001910 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1911 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1912 }
1913
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001914 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001915 * Creates an Allocation for use by scripts with a given {@link
1916 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001917 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001918 * @param rs Context to which the Allocation will belong.
1919 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001920 *
1921 * @return allocation
1922 */
Jason Sams5476b452010-12-08 16:14:36 -08001923 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001924 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001925 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001926
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001927 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001928 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001929 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001930 * @param rs Context to which the Allocation will belong.
1931 * @param e Element to use in the Allocation
1932 * @param count the number of Elements in the Allocation
1933 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001934 * utilized
1935 *
1936 * @return allocation
1937 */
Jason Sams5476b452010-12-08 16:14:36 -08001938 static public Allocation createSized(RenderScript rs, Element e,
1939 int count, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07001940 try {
1941 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
1942 rs.validate();
1943 Type.Builder b = new Type.Builder(rs, e);
1944 b.setX(count);
1945 Type t = b.create();
Jason Sams768bc022009-09-21 19:41:04 -07001946
Chris Craik06d29842015-06-02 17:19:24 -07001947 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
1948 if (id == 0) {
1949 throw new RSRuntimeException("Allocation creation failed.");
1950 }
1951 return new Allocation(id, rs, t, usage);
1952 } finally {
1953 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001954 }
Jason Sams5476b452010-12-08 16:14:36 -08001955 }
1956
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001957 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001958 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001959 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001960 * @param rs Context to which the Allocation will belong.
1961 * @param e Element to use in the Allocation
1962 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001963 *
1964 * @return allocation
1965 */
Jason Sams5476b452010-12-08 16:14:36 -08001966 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001967 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001968 }
1969
Jason Sams49a05d72010-12-29 14:31:29 -08001970 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001971 final Bitmap.Config bc = b.getConfig();
1972 if (bc == Bitmap.Config.ALPHA_8) {
1973 return Element.A_8(rs);
1974 }
1975 if (bc == Bitmap.Config.ARGB_4444) {
1976 return Element.RGBA_4444(rs);
1977 }
1978 if (bc == Bitmap.Config.ARGB_8888) {
1979 return Element.RGBA_8888(rs);
1980 }
1981 if (bc == Bitmap.Config.RGB_565) {
1982 return Element.RGB_565(rs);
1983 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001984 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001985 }
1986
Jason Sams49a05d72010-12-29 14:31:29 -08001987 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001988 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001989 Element e = elementFromBitmap(rs, b);
1990 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001991 tb.setX(b.getWidth());
1992 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001993 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001994 return tb.create();
1995 }
1996
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001997 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001998 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001999 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002000 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07002001 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002002 * @param mips specifies desired mipmap behaviour for the
2003 * allocation
2004 * @param usage bit field specifying how the allocation is
2005 * utilized
2006 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002007 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002008 *
2009 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002010 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002011 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002012 int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07002013 try {
2014 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
2015 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08002016
Chris Craik06d29842015-06-02 17:19:24 -07002017 // WAR undocumented color formats
2018 if (b.getConfig() == null) {
2019 if ((usage & USAGE_SHARED) != 0) {
2020 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
2021 }
2022 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
2023 Canvas c = new Canvas(newBitmap);
2024 c.drawBitmap(b, 0, 0, null);
2025 return createFromBitmap(rs, newBitmap, mips, usage);
Tim Murrayabd5db92013-02-28 11:45:22 -08002026 }
Tim Murrayabd5db92013-02-28 11:45:22 -08002027
Chris Craik06d29842015-06-02 17:19:24 -07002028 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08002029
Chris Craik06d29842015-06-02 17:19:24 -07002030 // enable optimized bitmap path only with no mipmap and script-only usage
2031 if (mips == MipmapControl.MIPMAP_NONE &&
2032 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
2033 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
2034 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
2035 if (id == 0) {
2036 throw new RSRuntimeException("Load failed.");
2037 }
2038
2039 // keep a reference to the Bitmap around to prevent GC
2040 Allocation alloc = new Allocation(id, rs, t, usage);
2041 alloc.setBitmap(b);
2042 return alloc;
2043 }
2044
2045
2046 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08002047 if (id == 0) {
2048 throw new RSRuntimeException("Load failed.");
2049 }
Chris Craik06d29842015-06-02 17:19:24 -07002050 return new Allocation(id, rs, t, usage);
2051 } finally {
2052 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murraya3145512012-12-04 17:59:29 -08002053 }
Jason Sams5476b452010-12-08 16:14:36 -08002054 }
2055
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002056 /**
Miao Wang0facf022015-11-25 11:21:13 -08002057 * @hide
2058 * Gets or creates a ByteBuffer that contains the raw data of the current Allocation.
2059 * If the Allocation is created with USAGE_IO_INPUT, the returned ByteBuffer
2060 * would contain the up-to-date data as READ ONLY.
2061 * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
2062 * the Allocation has certain alignment. The size of each row including padding,
2063 * called stride, can be queried using the {@link #getStride()} method.
2064 *
2065 * Note: Operating on the ByteBuffer of a destroyed Allocation will triger errors.
2066 *
2067 * @return ByteBuffer The ByteBuffer associated with raw data pointer of the Allocation.
2068 */
2069 public ByteBuffer getByteBuffer() {
2070 // Create a new ByteBuffer if it is not initialized or using IO_INPUT.
2071 if (mType.hasFaces()) {
2072 throw new RSInvalidStateException("Cubemap is not supported for getByteBuffer().");
2073 }
2074 if (mType.getYuv() == android.graphics.ImageFormat.NV21 ||
2075 mType.getYuv() == android.graphics.ImageFormat.YV12 ||
2076 mType.getYuv() == android.graphics.ImageFormat.YUV_420_888 ) {
2077 throw new RSInvalidStateException("YUV format is not supported for getByteBuffer().");
2078 }
2079 if (mByteBuffer == null || (mUsage & USAGE_IO_INPUT) != 0) {
2080 int xBytesSize = mType.getX() * mType.getElement().getBytesSize();
2081 long[] stride = new long[1];
2082 mByteBuffer = mRS.nAllocationGetByteBuffer(getID(mRS), stride, xBytesSize, mType.getY(), mType.getZ());
2083 mByteBufferStride = stride[0];
2084 }
2085 if ((mUsage & USAGE_IO_INPUT) != 0) {
2086 return mByteBuffer.asReadOnlyBuffer();
2087 }
2088 return mByteBuffer;
2089 }
2090
2091 /**
2092 * @hide
2093 * Gets the stride of the Allocation.
2094 * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
2095 * the Allocation has certain alignment. The size of each row including such
2096 * padding is called stride.
2097 *
2098 * @return the stride. For 1D Allocation, the stride will be the number of
2099 * bytes of this Allocation. For 2D and 3D Allocations, the stride
2100 * will be the stride in X dimension measuring in bytes.
2101 */
2102 public long getStride() {
2103 if (mByteBufferStride == -1) {
2104 getByteBuffer();
2105 }
2106 return mByteBufferStride;
2107 }
2108
2109 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002110 * Returns the handle to a raw buffer that is being managed by the screen
2111 * compositor. This operation is only valid for Allocations with {@link
2112 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002113 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07002114 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002115 *
2116 */
2117 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08002118 if ((mUsage & USAGE_IO_INPUT) == 0) {
2119 throw new RSInvalidStateException("Allocation is not a surface texture.");
2120 }
Jason Sams1e68bac2015-03-17 16:36:55 -07002121
2122 if (mGetSurfaceSurface == null) {
2123 mGetSurfaceSurface = mRS.nAllocationGetSurface(getID(mRS));
2124 }
2125
2126 return mGetSurfaceSurface;
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002127 }
2128
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002129 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002130 * Associate a {@link android.view.Surface} with this Allocation. This
2131 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07002132 *
2133 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08002134 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002135 public void setSurface(Surface sur) {
2136 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08002137 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
2138 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
2139 }
2140
Jason Samse07694b2012-04-03 15:36:36 -07002141 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08002142 }
2143
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002144 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002145 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08002146 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002147 * <p>With target API version 18 or greater, this Allocation will be created
2148 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
2149 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
2150 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002151 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002152 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002153 * @param b bitmap source for the allocation data
2154 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002155 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002156 *
2157 */
Jason Sams6d8eb262010-12-15 01:41:00 -08002158 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08002159 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2160 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07002161 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08002162 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002163 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
2164 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08002165 }
2166
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002167 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002168 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
2169 * containing the horizontal list of cube faces. Each face must be a square,
2170 * have the same size as all other faces, and have a width that is a power
2171 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002172 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002173 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07002174 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002175 * format: right, left, top, bottom, front, back
2176 * @param mips specifies desired mipmap behaviour for the cubemap
2177 * @param usage bit field specifying how the cubemap is utilized
2178 *
2179 * @return allocation containing cubemap data
2180 *
2181 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002182 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002183 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002184 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002185 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08002186
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002187 int height = b.getHeight();
2188 int width = b.getWidth();
2189
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002190 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002191 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
2192 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002193 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002194 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002195 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002196 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002197 if (!isPow2) {
2198 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2199 }
2200
2201 Element e = elementFromBitmap(rs, b);
2202 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002203 tb.setX(height);
2204 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002205 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08002206 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002207 Type t = tb.create();
2208
Tim Murray460a0492013-11-19 12:45:54 -08002209 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002210 if(id == 0) {
2211 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
2212 }
Jason Sams5476b452010-12-08 16:14:36 -08002213 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002214 }
2215
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002216 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002217 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
2218 * from a {@link android.graphics.Bitmap} containing the horizontal list of
2219 * cube faces. Each face must be a square, have the same size as all other
2220 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002221 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002222 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002223 * @param b bitmap with cubemap faces layed out in the following
2224 * format: right, left, top, bottom, front, back
2225 *
2226 * @return allocation containing cubemap data
2227 *
2228 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002229 static public Allocation createCubemapFromBitmap(RenderScript rs,
2230 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08002231 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002232 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08002233 }
2234
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002235 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002236 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
2237 * objects containing the cube faces. Each face must be a square, have the
2238 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002239 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002240 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002241 * @param xpos cubemap face in the positive x direction
2242 * @param xneg cubemap face in the negative x direction
2243 * @param ypos cubemap face in the positive y direction
2244 * @param yneg cubemap face in the negative y direction
2245 * @param zpos cubemap face in the positive z direction
2246 * @param zneg cubemap face in the negative z direction
2247 * @param mips specifies desired mipmap behaviour for the cubemap
2248 * @param usage bit field specifying how the cubemap is utilized
2249 *
2250 * @return allocation containing cubemap data
2251 *
2252 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002253 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2254 Bitmap xpos,
2255 Bitmap xneg,
2256 Bitmap ypos,
2257 Bitmap yneg,
2258 Bitmap zpos,
2259 Bitmap zneg,
2260 MipmapControl mips,
2261 int usage) {
2262 int height = xpos.getHeight();
2263 if (xpos.getWidth() != height ||
2264 xneg.getWidth() != height || xneg.getHeight() != height ||
2265 ypos.getWidth() != height || ypos.getHeight() != height ||
2266 yneg.getWidth() != height || yneg.getHeight() != height ||
2267 zpos.getWidth() != height || zpos.getHeight() != height ||
2268 zneg.getWidth() != height || zneg.getHeight() != height) {
2269 throw new RSIllegalArgumentException("Only square cube map faces supported");
2270 }
2271 boolean isPow2 = (height & (height - 1)) == 0;
2272 if (!isPow2) {
2273 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2274 }
2275
2276 Element e = elementFromBitmap(rs, xpos);
2277 Type.Builder tb = new Type.Builder(rs, e);
2278 tb.setX(height);
2279 tb.setY(height);
2280 tb.setFaces(true);
2281 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
2282 Type t = tb.create();
2283 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
2284
2285 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07002286 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002287 adapter.copyFrom(xpos);
2288 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
2289 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002290 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002291 adapter.copyFrom(ypos);
2292 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
2293 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002294 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002295 adapter.copyFrom(zpos);
2296 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
2297 adapter.copyFrom(zneg);
2298
2299 return cubemap;
2300 }
2301
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002302 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002303 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
2304 * from 6 {@link android.graphics.Bitmap} objects containing the cube
2305 * faces. Each face must be a square, have the same size as all other faces,
2306 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002307 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002308 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002309 * @param xpos cubemap face in the positive x direction
2310 * @param xneg cubemap face in the negative x direction
2311 * @param ypos cubemap face in the positive y direction
2312 * @param yneg cubemap face in the negative y direction
2313 * @param zpos cubemap face in the positive z direction
2314 * @param zneg cubemap face in the negative z direction
2315 *
2316 * @return allocation containing cubemap data
2317 *
2318 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002319 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2320 Bitmap xpos,
2321 Bitmap xneg,
2322 Bitmap ypos,
2323 Bitmap yneg,
2324 Bitmap zpos,
2325 Bitmap zneg) {
2326 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
2327 zpos, zneg, MipmapControl.MIPMAP_NONE,
2328 USAGE_GRAPHICS_TEXTURE);
2329 }
2330
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002331 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002332 * Creates an Allocation from the Bitmap referenced
2333 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002334 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002335 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002336 * @param res application resources
2337 * @param id resource id to load the data from
2338 * @param mips specifies desired mipmap behaviour for the
2339 * allocation
2340 * @param usage bit field specifying how the allocation is
2341 * utilized
2342 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002343 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002344 *
2345 */
Jason Sams5476b452010-12-08 16:14:36 -08002346 static public Allocation createFromBitmapResource(RenderScript rs,
2347 Resources res,
2348 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08002349 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002350 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07002351
Jason Sams771bebb2009-12-07 12:40:12 -08002352 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07002353 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
2354 throw new RSIllegalArgumentException("Unsupported usage specified.");
2355 }
Jason Sams5476b452010-12-08 16:14:36 -08002356 Bitmap b = BitmapFactory.decodeResource(res, id);
2357 Allocation alloc = createFromBitmap(rs, b, mips, usage);
2358 b.recycle();
2359 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07002360 }
2361
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002362 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002363 * Creates a non-mipmapped Allocation to use as a graphics texture from the
2364 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002365 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002366 * <p>With target API version 18 or greater, this allocation will be created
2367 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
2368 * target API version 17 or lower, this allocation will be created with
2369 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08002370 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002371 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002372 * @param res application resources
2373 * @param id resource id to load the data from
2374 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002375 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002376 *
2377 */
Jason Sams5476b452010-12-08 16:14:36 -08002378 static public Allocation createFromBitmapResource(RenderScript rs,
2379 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08002380 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08002381 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2382 return createFromBitmapResource(rs, res, id,
2383 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07002384 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08002385 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002386 return createFromBitmapResource(rs, res, id,
2387 MipmapControl.MIPMAP_NONE,
2388 USAGE_GRAPHICS_TEXTURE);
2389 }
2390
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002391 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002392 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002393 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002394 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002395 * @param str string to create the allocation from
2396 * @param usage bit field specifying how the allocaiton is
2397 * utilized
2398 *
2399 */
Jason Sams5476b452010-12-08 16:14:36 -08002400 static public Allocation createFromString(RenderScript rs,
2401 String str,
2402 int usage) {
2403 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002404 byte[] allocArray = null;
2405 try {
2406 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08002407 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002408 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002409 return alloc;
2410 }
2411 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08002412 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002413 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002414 }
Jason Sams739c8262013-04-11 18:07:52 -07002415
2416 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002417 * Interface to handle notification when new buffers are available via
2418 * {@link #USAGE_IO_INPUT}. An application will receive one notification
2419 * when a buffer is available. Additional buffers will not trigger new
2420 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07002421 */
Jason Sams42ef2382013-08-29 13:30:59 -07002422 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07002423 public void onBufferAvailable(Allocation a);
2424 }
2425
2426 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002427 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07002428 *
Jason Sams42ef2382013-08-29 13:30:59 -07002429 * @param callback instance of the OnBufferAvailableListener
2430 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07002431 */
Jason Sams42ef2382013-08-29 13:30:59 -07002432 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07002433 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002434 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07002435 mBufferNotifier = callback;
2436 }
2437 }
2438
Tim Murrayb730d862014-08-18 16:14:24 -07002439 static void sendBufferNotification(long id) {
Jason Sams739c8262013-04-11 18:07:52 -07002440 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002441 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07002442
2443 if ((a != null) && (a.mBufferNotifier != null)) {
2444 a.mBufferNotifier.onBufferAvailable(a);
2445 }
2446 }
2447 }
2448
Miao Wangf0f6e802015-02-03 17:16:43 -08002449 /**
2450 * For USAGE_IO_OUTPUT, destroy() implies setSurface(null).
2451 *
2452 */
2453 @Override
2454 public void destroy() {
2455 if((mUsage & USAGE_IO_OUTPUT) != 0) {
2456 setSurface(null);
2457 }
2458 super.destroy();
2459 }
Jason Samsb8c5a842009-07-31 20:40:47 -07002460}