blob: a4876b92fadf76461ff14b410b47c932b8c49dd8 [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
Jason Sams739c8262013-04-11 18:07:52 -070019import java.util.HashMap;
Jason Samsb8c5a842009-07-31 20:40:47 -070020import android.content.res.Resources;
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
Jason Samsfb9aa9f2012-03-28 15:30:07 -070023import android.view.Surface;
Jason Samsb8c5a842009-07-31 20:40:47 -070024import android.util.Log;
Tim Murrayabd5db92013-02-28 11:45:22 -080025import android.graphics.Canvas;
Tim Murray6d7a53c2013-05-23 16:59:23 -070026import android.os.Trace;
Jason Samsb8c5a842009-07-31 20:40:47 -070027
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070028/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070029 * <p> This class provides the primary method through which data is passed to
30 * and from RenderScript kernels. An Allocation provides the backing store for
31 * a given {@link android.renderscript.Type}. </p>
Jason Samsa23d4e72011-01-04 18:59:12 -080032 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070033 * <p>An Allocation also contains a set of usage flags that denote how the
34 * Allocation could be used. For example, an Allocation may have usage flags
35 * specifying that it can be used from a script as well as input to a {@link
36 * android.renderscript.Sampler}. A developer must synchronize across these
37 * different usages using {@link android.renderscript.Allocation#syncAll} in
38 * order to ensure that different users of the Allocation have a consistent view
39 * of memory. For example, in the case where an Allocation is used as the output
40 * of one kernel and as Sampler input in a later kernel, a developer must call
41 * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
42 * second kernel to ensure correctness.
Jason Samsa23d4e72011-01-04 18:59:12 -080043 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070044 * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
45 * more complex Element types, the {@link #copyFromUnchecked} methods can be
46 * used to copy from byte arrays or similar constructs.</p>
Jason Samsb8c5a842009-07-31 20:40:47 -070047 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080048 * <div class="special reference">
49 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070050 * <p>For more information about creating an application that uses RenderScript, read the
51 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080052 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070053 **/
Chris Craik06d29842015-06-02 17:19:24 -070054
Jason Samsb8c5a842009-07-31 20:40:47 -070055public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070056 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080057 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080058 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070059 Allocation mAdaptedAllocation;
Tim Murray2f2472c2013-08-22 14:55:26 -070060 int mSize;
Jason Samsba862d12011-07-07 15:24:42 -070061
Jason Sams615e7ce2012-01-13 14:01:20 -080062 boolean mReadAllowed = true;
63 boolean mWriteAllowed = true;
Miao Wang87e908d2015-03-02 15:15:15 -080064 boolean mAutoPadding = false;
Jason Sams46ba27e32015-02-06 17:45:15 -080065 int mSelectedX;
Jason Samsba862d12011-07-07 15:24:42 -070066 int mSelectedY;
67 int mSelectedZ;
68 int mSelectedLOD;
Jason Sams46ba27e32015-02-06 17:45:15 -080069 int mSelectedArray[];
Jason Samsba862d12011-07-07 15:24:42 -070070 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
71
72 int mCurrentDimX;
73 int mCurrentDimY;
74 int mCurrentDimZ;
75 int mCurrentCount;
Tim Murray460a0492013-11-19 12:45:54 -080076 static HashMap<Long, Allocation> mAllocationMap =
77 new HashMap<Long, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070078 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070079
Jason Sams1e68bac2015-03-17 16:36:55 -070080 private Surface mGetSurfaceSurface = null;
81
Jason Sams3042d262013-11-25 18:28:33 -080082 private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
83 final Class c = d.getClass();
84 if (!c.isArray()) {
85 throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
86 }
87 final Class cmp = c.getComponentType();
88 if (!cmp.isPrimitive()) {
89 throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
90 }
91
92 if (cmp == Long.TYPE) {
93 if (checkType) {
94 validateIsInt64();
95 return mType.mElement.mType;
96 }
97 return Element.DataType.SIGNED_64;
98 }
99
100 if (cmp == Integer.TYPE) {
101 if (checkType) {
102 validateIsInt32();
103 return mType.mElement.mType;
104 }
105 return Element.DataType.SIGNED_32;
106 }
107
108 if (cmp == Short.TYPE) {
109 if (checkType) {
110 validateIsInt16();
111 return mType.mElement.mType;
112 }
113 return Element.DataType.SIGNED_16;
114 }
115
116 if (cmp == Byte.TYPE) {
117 if (checkType) {
118 validateIsInt8();
119 return mType.mElement.mType;
120 }
121 return Element.DataType.SIGNED_8;
122 }
123
124 if (cmp == Float.TYPE) {
125 if (checkType) {
126 validateIsFloat32();
127 }
128 return Element.DataType.FLOAT_32;
129 }
130
131 if (cmp == Double.TYPE) {
132 if (checkType) {
133 validateIsFloat64();
134 }
135 return Element.DataType.FLOAT_64;
136 }
137 return null;
138 }
139
140
Tim Murrayc11e25c2013-04-09 11:01:01 -0700141 /**
142 * The usage of the Allocation. These signal to RenderScript where to place
143 * the Allocation in memory.
144 *
145 */
Jason Sams5476b452010-12-08 16:14:36 -0800146
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700147 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700148 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -0800149 */
Jason Sams5476b452010-12-08 16:14:36 -0800150 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800151
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700152 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700153 * The Allocation will be used as a texture source by one or more graphics
154 * programs.
Jason Samsf7086092011-01-12 13:28:37 -0800155 *
156 */
Jason Sams5476b452010-12-08 16:14:36 -0800157 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800158
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700159 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700160 * The Allocation will be used as a graphics mesh.
161 *
162 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800163 *
164 */
Jason Sams5476b452010-12-08 16:14:36 -0800165 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800166
167
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700168 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700169 * The Allocation will be used as the source of shader constants by one or
170 * more programs.
171 *
172 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800173 *
174 */
Jason Sams5476b452010-12-08 16:14:36 -0800175 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
176
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700177 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700178 * The Allocation will be used as a target for offscreen rendering
179 *
180 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700181 *
182 */
183 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
184
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700185 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700186 * The Allocation will be used as a {@link android.view.Surface}
187 * consumer. This usage will cause the Allocation to be created
188 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800189 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800190 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700191 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800192
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700193 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700194 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700195 * producer. The dimensions and format of the {@link
Jason Sams3a1b8e42013-09-24 15:18:52 -0700196 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700197 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800198 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800199 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700200 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700201
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700202 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700203 * The Allocation's backing store will be inherited from another object
204 * (usually a {@link android.graphics.Bitmap}); copying to or from the
205 * original source Bitmap will cause a synchronization rather than a full
206 * copy. {@link #syncAll} may also be used to synchronize the Allocation
207 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800208 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700209 * <p>This is set by default for allocations created with {@link
210 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800211 *
212 */
213 public static final int USAGE_SHARED = 0x0080;
214
215 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700216 * Controls mipmap behavior when using the bitmap creation and update
217 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800218 */
Jason Sams4ef66502010-12-10 16:03:15 -0800219 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700220 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700221 * No mipmaps will be generated and the type generated from the incoming
222 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800223 */
Jason Sams5476b452010-12-08 16:14:36 -0800224 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800225
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700226 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700227 * A full mipmap chain will be created in script memory. The Type of
228 * the Allocation will contain a full mipmap chain. On upload, the full
229 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800230 */
Jason Sams5476b452010-12-08 16:14:36 -0800231 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800232
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700233 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700234 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
235 * not contain mipmaps. On upload, the allocation data will contain a
236 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800237 */
Jason Sams5476b452010-12-08 16:14:36 -0800238 MIPMAP_ON_SYNC_TO_TEXTURE(2);
239
240 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800241 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800242 mID = id;
243 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700244 }
245
Jason Sams48fe5342011-07-08 13:52:30 -0700246
Tim Murray460a0492013-11-19 12:45:54 -0800247 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700248 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700249 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700250 }
Jason Samse07694b2012-04-03 15:36:36 -0700251 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700252 }
253
Jason Sams03d2d002012-03-23 13:51:56 -0700254
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700255 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700256 * Get the {@link android.renderscript.Element} of the {@link
257 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700258 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700259 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700260 *
261 */
262 public Element getElement() {
263 return mType.getElement();
264 }
265
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700266 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700267 * Get the usage flags of the Allocation.
268 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700269 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700270 *
271 */
272 public int getUsage() {
273 return mUsage;
274 }
275
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700276 /**
Miao Wang87e908d2015-03-02 15:15:15 -0800277 * Enable/Disable AutoPadding for Vec3 elements.
Miao Wangd7ecab12015-03-26 18:00:15 -0700278 * By default: Diabled.
Miao Wang87e908d2015-03-02 15:15:15 -0800279 *
Miao Wang179e8b52015-04-15 17:44:32 -0700280 * @param useAutoPadding True: enable AutoPadding; False: disable AutoPadding
Miao Wang87e908d2015-03-02 15:15:15 -0800281 *
282 */
283 public void setAutoPadding(boolean useAutoPadding) {
284 mAutoPadding = useAutoPadding;
285 }
286
287 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700288 * Get the size of the Allocation in bytes.
289 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700290 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700291 *
292 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700293 public int getBytesSize() {
Tim Murray04f0d6e2013-12-17 17:15:25 -0800294 if (mType.mDimYuv != 0) {
295 return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
296 }
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700297 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700298 }
299
Jason Sams452a7662011-07-07 16:05:18 -0700300 private void updateCacheInfo(Type t) {
301 mCurrentDimX = t.getX();
302 mCurrentDimY = t.getY();
303 mCurrentDimZ = t.getZ();
304 mCurrentCount = mCurrentDimX;
305 if (mCurrentDimY > 1) {
306 mCurrentCount *= mCurrentDimY;
307 }
308 if (mCurrentDimZ > 1) {
309 mCurrentCount *= mCurrentDimZ;
310 }
311 }
Jason Samsba862d12011-07-07 15:24:42 -0700312
Tim Murraya3145512012-12-04 17:59:29 -0800313 private void setBitmap(Bitmap b) {
314 mBitmap = b;
315 }
316
Tim Murray460a0492013-11-19 12:45:54 -0800317 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700318 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800319 if ((usage & ~(USAGE_SCRIPT |
320 USAGE_GRAPHICS_TEXTURE |
321 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700322 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800323 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800324 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800325 USAGE_IO_OUTPUT |
326 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800327 throw new RSIllegalArgumentException("Unknown usage specified.");
328 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800329
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700330 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800331 mWriteAllowed = false;
332
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700333 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800334 USAGE_GRAPHICS_TEXTURE |
335 USAGE_SCRIPT)) != 0) {
336 throw new RSIllegalArgumentException("Invalid usage combination.");
337 }
338 }
Jason Sams9bf18922013-04-13 19:48:36 -0700339
Jason Sams5476b452010-12-08 16:14:36 -0800340 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800341 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700342
Jason Sams452a7662011-07-07 16:05:18 -0700343 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700344 // TODO: A3D doesn't have Type info during creation, so we can't
345 // calculate the size ahead of time. We can possibly add a method
346 // to update the size in the future if it seems reasonable.
347 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700348 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700349 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700350 try {
351 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
352 } catch (Exception e) {
353 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
354 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
355 }
356 }
357
358 protected void finalize() throws Throwable {
359 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
360 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700361 }
362
Jason Sams3042d262013-11-25 18:28:33 -0800363 private void validateIsInt64() {
364 if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
365 (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
366 return;
367 }
368 throw new RSIllegalArgumentException(
369 "64 bit integer source does not match allocation type " + mType.mElement.mType);
370 }
371
Jason Samsb97b2512011-01-16 15:04:08 -0800372 private void validateIsInt32() {
373 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
374 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
375 return;
376 }
377 throw new RSIllegalArgumentException(
378 "32 bit integer source does not match allocation type " + mType.mElement.mType);
379 }
380
381 private void validateIsInt16() {
382 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
383 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
384 return;
385 }
386 throw new RSIllegalArgumentException(
387 "16 bit integer source does not match allocation type " + mType.mElement.mType);
388 }
389
390 private void validateIsInt8() {
391 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
392 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
393 return;
394 }
395 throw new RSIllegalArgumentException(
396 "8 bit integer source does not match allocation type " + mType.mElement.mType);
397 }
398
399 private void validateIsFloat32() {
400 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
401 return;
402 }
403 throw new RSIllegalArgumentException(
404 "32 bit float source does not match allocation type " + mType.mElement.mType);
405 }
406
Jason Sams3042d262013-11-25 18:28:33 -0800407 private void validateIsFloat64() {
408 if (mType.mElement.mType == Element.DataType.FLOAT_64) {
409 return;
410 }
411 throw new RSIllegalArgumentException(
412 "64 bit float source does not match allocation type " + mType.mElement.mType);
413 }
414
Jason Samsb97b2512011-01-16 15:04:08 -0800415 private void validateIsObject() {
416 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
417 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
418 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
419 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
420 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
421 (mType.mElement.mType == Element.DataType.RS_MESH) ||
422 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
423 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
424 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
425 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
426 return;
427 }
428 throw new RSIllegalArgumentException(
429 "Object source does not match allocation type " + mType.mElement.mType);
430 }
431
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700432 @Override
433 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800434 super.updateFromNative();
Tim Murray460a0492013-11-19 12:45:54 -0800435 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700436 if(typeID != 0) {
437 mType = new Type(typeID, mRS);
438 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700439 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700440 }
441 }
442
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700443 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700444 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700445 *
446 * @return Type
447 *
448 */
Jason Samsea87e962010-01-12 12:12:28 -0800449 public Type getType() {
450 return mType;
451 }
452
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700453 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700454 * Propagate changes from one usage of the Allocation to the
455 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700456 *
457 */
Jason Sams5476b452010-12-08 16:14:36 -0800458 public void syncAll(int srcLocation) {
Chris Craik06d29842015-06-02 17:19:24 -0700459 try {
460 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
461 switch (srcLocation) {
462 case USAGE_GRAPHICS_TEXTURE:
463 case USAGE_SCRIPT:
464 if ((mUsage & USAGE_SHARED) != 0) {
465 copyFrom(mBitmap);
466 }
467 break;
468 case USAGE_GRAPHICS_CONSTANTS:
469 case USAGE_GRAPHICS_VERTEX:
470 break;
471 case USAGE_SHARED:
472 if ((mUsage & USAGE_SHARED) != 0) {
473 copyTo(mBitmap);
474 }
475 break;
476 default:
477 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
Tim Murray78e64942013-04-09 17:28:56 -0700478 }
Chris Craik06d29842015-06-02 17:19:24 -0700479 mRS.validate();
480 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
481 } finally {
482 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800483 }
Jason Sams5476b452010-12-08 16:14:36 -0800484 }
485
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700486 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700487 * Send a buffer to the output stream. The contents of the Allocation will
488 * be undefined after this operation. This operation is only valid if {@link
489 * #USAGE_IO_OUTPUT} is set on the Allocation.
490 *
Jason Sams163766c2012-02-15 12:04:24 -0800491 *
Jason Sams163766c2012-02-15 12:04:24 -0800492 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700493 public void ioSend() {
Chris Craik06d29842015-06-02 17:19:24 -0700494 try {
495 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
496 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
497 throw new RSIllegalArgumentException(
498 "Can only send buffer if IO_OUTPUT usage specified.");
499 }
500 mRS.validate();
501 mRS.nAllocationIoSend(getID(mRS));
502 } finally {
503 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800504 }
Jason Sams163766c2012-02-15 12:04:24 -0800505 }
506
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700507 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700508 * Receive the latest input into the Allocation. This operation
509 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800510 *
Jason Sams163766c2012-02-15 12:04:24 -0800511 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700512 public void ioReceive() {
Chris Craik06d29842015-06-02 17:19:24 -0700513 try {
514 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
515 if ((mUsage & USAGE_IO_INPUT) == 0) {
516 throw new RSIllegalArgumentException(
517 "Can only receive if IO_INPUT usage specified.");
518 }
519 mRS.validate();
520 mRS.nAllocationIoReceive(getID(mRS));
521 } finally {
522 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800523 }
Jason Sams163766c2012-02-15 12:04:24 -0800524 }
525
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700526 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700527 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700528 *
529 * @param d Source array.
530 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800531 public void copyFrom(BaseObj[] d) {
Chris Craik06d29842015-06-02 17:19:24 -0700532 try {
533 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
534 mRS.validate();
535 validateIsObject();
536 if (d.length != mCurrentCount) {
537 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
538 mCurrentCount + ", array length = " + d.length);
539 }
Tim Murray460a0492013-11-19 12:45:54 -0800540
Chris Craik06d29842015-06-02 17:19:24 -0700541 if (RenderScript.sPointerSize == 8) {
542 long i[] = new long[d.length * 4];
543 for (int ct=0; ct < d.length; ct++) {
544 i[ct * 4] = d[ct].getID(mRS);
545 }
546 copy1DRangeFromUnchecked(0, mCurrentCount, i);
547 } else {
548 int i[] = new int[d.length];
549 for (int ct=0; ct < d.length; ct++) {
550 i[ct] = (int) d[ct].getID(mRS);
551 }
552 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray3de3dc72014-07-01 16:56:18 -0700553 }
Chris Craik06d29842015-06-02 17:19:24 -0700554 } finally {
555 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800556 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700557 }
558
Jason Samsfb9f82c2011-01-12 14:53:25 -0800559 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800560 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800561 if (bc == null) {
562 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
563 }
Jason Sams252c0782011-01-11 17:42:52 -0800564 switch (bc) {
565 case ALPHA_8:
566 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
567 throw new RSIllegalArgumentException("Allocation kind is " +
568 mType.getElement().mKind + ", type " +
569 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700570 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800571 " bytes, passed bitmap was " + bc);
572 }
573 break;
574 case ARGB_8888:
575 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700576 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800577 throw new RSIllegalArgumentException("Allocation kind is " +
578 mType.getElement().mKind + ", type " +
579 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700580 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800581 " bytes, passed bitmap was " + bc);
582 }
583 break;
584 case RGB_565:
585 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700586 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800587 throw new RSIllegalArgumentException("Allocation kind is " +
588 mType.getElement().mKind + ", type " +
589 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700590 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800591 " bytes, passed bitmap was " + bc);
592 }
593 break;
594 case ARGB_4444:
595 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700596 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800597 throw new RSIllegalArgumentException("Allocation kind is " +
598 mType.getElement().mKind + ", type " +
599 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700600 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800601 " bytes, passed bitmap was " + bc);
602 }
603 break;
604
605 }
Jason Sams4ef66502010-12-10 16:03:15 -0800606 }
607
Jason Samsfb9f82c2011-01-12 14:53:25 -0800608 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700609 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800610 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
611 }
612 }
613
Jason Sams3042d262013-11-25 18:28:33 -0800614 private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -0700615 try {
616 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
617 mRS.validate();
618 if (mCurrentDimZ > 0) {
619 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
620 } else if (mCurrentDimY > 0) {
621 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
622 } else {
623 copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
624 }
625 } finally {
626 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams3042d262013-11-25 18:28:33 -0800627 }
Jason Sams3042d262013-11-25 18:28:33 -0800628 }
629
630 /**
631 * Copy into this Allocation from an array. This method does not guarantee
632 * that the Allocation is compatible with the input buffer; it copies memory
633 * without reinterpretation.
634 *
635 * @param array The source data array
636 */
637 public void copyFromUnchecked(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700638 try {
639 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
640 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
641 java.lang.reflect.Array.getLength(array));
642 } finally {
643 Trace.traceEnd(RenderScript.TRACE_TAG);
644 }
Jason Sams3042d262013-11-25 18:28:33 -0800645 }
646
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700647 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700648 * Copy into this Allocation from an array. This method does not guarantee
649 * that the Allocation is compatible with the input buffer; it copies memory
650 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800651 *
652 * @param d the source data array
653 */
654 public void copyFromUnchecked(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800655 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800656 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700657
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700658 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700659 * Copy into this Allocation from an array. This method does not guarantee
660 * that the Allocation is compatible with the input buffer; it copies memory
661 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800662 *
663 * @param d the source data array
664 */
665 public void copyFromUnchecked(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800666 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800667 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700668
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700669 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700670 * Copy into this Allocation from an array. This method does not guarantee
671 * that the Allocation is compatible with the input buffer; it copies memory
672 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800673 *
674 * @param d the source data array
675 */
676 public void copyFromUnchecked(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800677 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800678 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700679
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700680 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700681 * Copy into this Allocation from an array. This method does not guarantee
682 * that the Allocation is compatible with the input buffer; it copies memory
683 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800684 *
685 * @param d the source data array
686 */
687 public void copyFromUnchecked(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800688 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800689 }
690
Tim Murray6d7a53c2013-05-23 16:59:23 -0700691
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700692 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700693 * Copy into this Allocation from an array. This variant is type checked
694 * and will generate exceptions if the Allocation's {@link
Jason Sams3042d262013-11-25 18:28:33 -0800695 * android.renderscript.Element} does not match the array's
696 * primitive type.
697 *
Ying Wang16229812013-11-26 15:45:12 -0800698 * @param array The source data array
Jason Sams3042d262013-11-25 18:28:33 -0800699 */
700 public void copyFrom(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700701 try {
702 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
703 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
704 java.lang.reflect.Array.getLength(array));
705 } finally {
706 Trace.traceEnd(RenderScript.TRACE_TAG);
707 }
Jason Sams3042d262013-11-25 18:28:33 -0800708 }
709
710 /**
711 * Copy into this Allocation from an array. This variant is type checked
712 * and will generate exceptions if the Allocation's {@link
Tim Murrayc11e25c2013-04-09 11:01:01 -0700713 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800714 *
715 * @param d the source data array
716 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800717 public void copyFrom(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800718 validateIsInt32();
719 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800720 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800721
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700722 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700723 * Copy into this Allocation from an array. This variant is type checked
724 * and will generate exceptions if the Allocation's {@link
725 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800726 *
727 * @param d the source data array
728 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800729 public void copyFrom(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800730 validateIsInt16();
731 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800732 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800733
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700734 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700735 * Copy into this Allocation from an array. This variant is type checked
736 * and will generate exceptions if the Allocation's {@link
737 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800738 *
739 * @param d the source data array
740 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800741 public void copyFrom(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800742 validateIsInt8();
743 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800744 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800745
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700746 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700747 * Copy into this Allocation from an array. This variant is type checked
748 * and will generate exceptions if the Allocation's {@link
749 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800750 *
751 * @param d the source data array
752 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800753 public void copyFrom(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800754 validateIsFloat32();
755 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800756 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800757
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700758 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700759 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
760 * height, width, and format of the bitmap must match the existing
761 * allocation.
762 *
763 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
764 * android.graphics.Bitmap} used to create the Allocation with {@link
765 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
766 * this will synchronize the Allocation with the latest data from the {@link
767 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800768 *
769 * @param b the source bitmap
770 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800771 public void copyFrom(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -0700772 try {
773 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
774 mRS.validate();
775 if (b.getConfig() == null) {
776 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
777 Canvas c = new Canvas(newBitmap);
778 c.drawBitmap(b, 0, 0, null);
779 copyFrom(newBitmap);
780 return;
781 }
782 validateBitmapSize(b);
783 validateBitmapFormat(b);
784 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
785 } finally {
786 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -0800787 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700788 }
789
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700790 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700791 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700792 * must be identical.
793 *
794 * @param a the source allocation
795 */
796 public void copyFrom(Allocation a) {
Chris Craik06d29842015-06-02 17:19:24 -0700797 try {
798 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
799 mRS.validate();
800 if (!mType.equals(a.getType())) {
801 throw new RSIllegalArgumentException("Types of allocations must match.");
802 }
803 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
804 } finally {
805 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700806 }
Tim Murrayf671fb02012-10-03 13:50:05 -0700807 }
808
Tim Murrayf671fb02012-10-03 13:50:05 -0700809 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700810 * This is only intended to be used by auto-generated code reflected from
811 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800812 *
813 * @param xoff
814 * @param fp
815 */
Jason Sams21b41032011-01-16 15:05:41 -0800816 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800817 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700818 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700819 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700820 int data_length = fp.getPos();
Jason Samsa70f4162010-03-26 15:33:42 -0700821
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700822 int count = data_length / eSize;
823 if ((eSize * count) != data_length) {
824 throw new RSIllegalArgumentException("Field packer length " + data_length +
Jason Samsa70f4162010-03-26 15:33:42 -0700825 " not divisible by element size " + eSize + ".");
826 }
Jason Samsba862d12011-07-07 15:24:42 -0700827 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700828 }
829
Miao Wang45cec0a2015-03-04 16:40:21 -0800830
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700831 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700832 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -0800833 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800834 *
835 * @param xoff
836 * @param component_number
837 * @param fp
838 */
Jason Sams21b41032011-01-16 15:05:41 -0800839 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800840 setFromFieldPacker(xoff, 0, 0, component_number, fp);
841 }
842
843 /**
Miao Wangc8e237e2015-02-20 18:36:32 -0800844 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -0800845 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -0800846 *
847 * @param xoff
848 * @param yoff
Miao Wangc8e237e2015-02-20 18:36:32 -0800849 * @param zoff
850 * @param component_number
851 * @param fp
852 */
853 public void setFromFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800854 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700855 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800856 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700857 }
858 if(xoff < 0) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800859 throw new RSIllegalArgumentException("Offset x must be >= 0.");
860 }
861 if(yoff < 0) {
862 throw new RSIllegalArgumentException("Offset y must be >= 0.");
863 }
864 if(zoff < 0) {
865 throw new RSIllegalArgumentException("Offset z must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700866 }
867
868 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700869 int data_length = fp.getPos();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700870 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800871 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700872
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700873 if (data_length != eSize) {
874 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700875 " does not match component size " + eSize + ".");
876 }
877
Miao Wangc8e237e2015-02-20 18:36:32 -0800878 mRS.nAllocationElementData(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
879 component_number, data, data_length);
Jason Samsa70f4162010-03-26 15:33:42 -0700880 }
881
Miao Wang87e908d2015-03-02 15:15:15 -0800882 private void data1DChecks(int off, int count, int len, int dataSize, boolean usePadding) {
Jason Sams771bebb2009-12-07 12:40:12 -0800883 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700884 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800885 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700886 }
887 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800888 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700889 }
Jason Samsba862d12011-07-07 15:24:42 -0700890 if((off + count) > mCurrentCount) {
891 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700892 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700893 }
Miao Wang87e908d2015-03-02 15:15:15 -0800894 if(usePadding) {
895 if(len < dataSize / 4 * 3) {
896 throw new RSIllegalArgumentException("Array too small for allocation type.");
897 }
898 } else {
899 if(len < dataSize) {
900 throw new RSIllegalArgumentException("Array too small for allocation type.");
901 }
Jason Sams768bc022009-09-21 19:41:04 -0700902 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700903 }
904
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700905 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700906 * Generate a mipmap chain. This is only valid if the Type of the Allocation
907 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800908 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700909 * <p>This function will generate a complete set of mipmaps from the top
910 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800911 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700912 * <p>If the Allocation is also using other memory spaces, a call to {@link
913 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800914 */
915 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700916 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800917 }
918
Jason Sams3042d262013-11-25 18:28:33 -0800919 private void copy1DRangeFromUnchecked(int off, int count, Object array,
920 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -0700921 try {
922 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
923 final int dataSize = mType.mElement.getBytesSize() * count;
924 // AutoPadding for Vec3 Element
925 boolean usePadding = false;
926 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
927 usePadding = true;
928 }
929 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
930 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
931 mType.mElement.mType.mSize, usePadding);
932 } finally {
933 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -0800934 }
Jason Sams3042d262013-11-25 18:28:33 -0800935 }
936
937 /**
938 * Copy an array into part of this Allocation. This method does not
939 * guarantee that the Allocation is compatible with the input buffer.
940 *
941 * @param off The offset of the first element to be copied.
942 * @param count The number of elements to be copied.
943 * @param array The source data array
944 */
945 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
946 copy1DRangeFromUnchecked(off, count, array,
947 validateObjectIsPrimitiveArray(array, false),
948 java.lang.reflect.Array.getLength(array));
949 }
950
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700951 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700952 * Copy an array into part of this Allocation. This method does not
953 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800954 *
955 * @param off The offset of the first element to be copied.
956 * @param count The number of elements to be copied.
957 * @param d the source data array
958 */
959 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800960 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700961 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700962
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700963 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700964 * Copy an array into part of this Allocation. This method does not
965 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800966 *
967 * @param off The offset of the first element to be copied.
968 * @param count The number of elements to be copied.
969 * @param d the source data array
970 */
971 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800972 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700973 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700974
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700975 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700976 * Copy an array into part of this Allocation. This method does not
977 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800978 *
979 * @param off The offset of the first element to be copied.
980 * @param count The number of elements to be copied.
981 * @param d the source data array
982 */
983 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800984 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700985 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700986
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700987 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700988 * Copy an array into part of this Allocation. This method does not
989 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800990 *
991 * @param off The offset of the first element to be copied.
992 * @param count The number of elements to be copied.
993 * @param d the source data array
994 */
995 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800996 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
997 }
998
999
1000 /**
1001 * Copy an array into part of this Allocation. This variant is type checked
1002 * and will generate exceptions if the Allocation type does not
1003 * match the component type of the array passed in.
1004 *
1005 * @param off The offset of the first element to be copied.
1006 * @param count The number of elements to be copied.
1007 * @param array The source data array.
1008 */
1009 public void copy1DRangeFrom(int off, int count, Object array) {
1010 copy1DRangeFromUnchecked(off, count, array,
1011 validateObjectIsPrimitiveArray(array, true),
1012 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -07001013 }
1014
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001015 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001016 * Copy an array into part of this Allocation. This variant is type checked
1017 * and will generate exceptions if the Allocation type is not a 32 bit
1018 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001019 *
1020 * @param off The offset of the first element to be copied.
1021 * @param count The number of elements to be copied.
1022 * @param d the source data array
1023 */
Jason Samsb97b2512011-01-16 15:04:08 -08001024 public void copy1DRangeFrom(int off, int count, int[] d) {
1025 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001026 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001027 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001028
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001029 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001030 * Copy an array into part of this Allocation. This variant is type checked
1031 * and will generate exceptions if the Allocation type is not a 16 bit
1032 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001033 *
1034 * @param off The offset of the first element to be copied.
1035 * @param count The number of elements to be copied.
1036 * @param d the source data array
1037 */
Jason Samsb97b2512011-01-16 15:04:08 -08001038 public void copy1DRangeFrom(int off, int count, short[] d) {
1039 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001040 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001041 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001042
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001043 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001044 * Copy an array into part of this Allocation. This variant is type checked
1045 * and will generate exceptions if the Allocation type is not an 8 bit
1046 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001047 *
1048 * @param off The offset of the first element to be copied.
1049 * @param count The number of elements to be copied.
1050 * @param d the source data array
1051 */
Jason Samsb97b2512011-01-16 15:04:08 -08001052 public void copy1DRangeFrom(int off, int count, byte[] d) {
1053 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001054 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001055 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001056
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001057 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001058 * Copy an array into part of this Allocation. This variant is type checked
1059 * and will generate exceptions if the Allocation type is not a 32 bit float
1060 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001061 *
1062 * @param off The offset of the first element to be copied.
1063 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001064 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001065 */
Jason Samsb97b2512011-01-16 15:04:08 -08001066 public void copy1DRangeFrom(int off, int count, float[] d) {
1067 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001068 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001069 }
Jason Sams3042d262013-11-25 18:28:33 -08001070
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001071 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001072 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001073 *
1074 * @param off The offset of the first element to be copied.
1075 * @param count The number of elements to be copied.
1076 * @param data the source data allocation.
1077 * @param dataOff off The offset of the first element in data to
1078 * be copied.
1079 */
1080 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001081 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -07001082 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001083 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001084 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001085 data.mSelectedLOD, data.mSelectedFace.mID);
Chris Craik5c705d62015-06-01 10:39:36 -07001086 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001087 }
1088
Jason Samsfb9f82c2011-01-12 14:53:25 -08001089 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001090 if (mAdaptedAllocation != null) {
1091
1092 } else {
1093
1094 if (xoff < 0 || yoff < 0) {
1095 throw new RSIllegalArgumentException("Offset cannot be negative.");
1096 }
1097 if (h < 0 || w < 0) {
1098 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1099 }
1100 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1101 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1102 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001103 }
1104 }
Jason Sams768bc022009-09-21 19:41:04 -07001105
Jason Sams3042d262013-11-25 18:28:33 -08001106 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1107 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001108 try {
1109 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
1110 mRS.validate();
1111 validate2DRange(xoff, yoff, w, h);
1112 final int dataSize = mType.mElement.getBytesSize() * w * h;
1113 // AutoPadding for Vec3 Element
1114 boolean usePadding = false;
1115 int sizeBytes = arrayLen * dt.mSize;
1116 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1117 if (dataSize / 4 * 3 > sizeBytes) {
1118 throw new RSIllegalArgumentException("Array too small for allocation type.");
1119 }
1120 usePadding = true;
1121 sizeBytes = dataSize;
1122 } else {
1123 if (dataSize > sizeBytes) {
1124 throw new RSIllegalArgumentException("Array too small for allocation type.");
1125 }
Miao Wang87e908d2015-03-02 15:15:15 -08001126 }
Chris Craik06d29842015-06-02 17:19:24 -07001127 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1128 array, sizeBytes, dt,
1129 mType.mElement.mType.mSize, usePadding);
1130 } finally {
1131 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001132 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001133 }
1134
Jason Sams3042d262013-11-25 18:28:33 -08001135 /**
1136 * Copy from an array into a rectangular region in this Allocation. The
1137 * array is assumed to be tightly packed.
1138 *
1139 * @param xoff X offset of the region to update in this Allocation
1140 * @param yoff Y offset of the region to update in this Allocation
1141 * @param w Width of the region to update
1142 * @param h Height of the region to update
Ying Wang16229812013-11-26 15:45:12 -08001143 * @param array Data to be placed into the Allocation
Jason Sams3042d262013-11-25 18:28:33 -08001144 */
1145 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001146 try {
1147 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1148 copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
1149 validateObjectIsPrimitiveArray(array, true),
1150 java.lang.reflect.Array.getLength(array));
1151 } finally {
1152 Trace.traceEnd(RenderScript.TRACE_TAG);
1153 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001154 }
1155
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001156 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001157 * Copy from an array into a rectangular region in this Allocation. The
1158 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -08001159 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001160 * @param xoff X offset of the region to update in this Allocation
1161 * @param yoff Y offset of the region to update in this Allocation
1162 * @param w Width of the region to update
1163 * @param h Height of the region to update
1164 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001165 */
1166 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001167 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001168 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1169 Element.DataType.SIGNED_8, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001170 }
1171
Tim Murrayc11e25c2013-04-09 11:01:01 -07001172 /**
1173 * Copy from an array into a rectangular region in this Allocation. The
1174 * array is assumed to be tightly packed.
1175 *
1176 * @param xoff X offset of the region to update in this Allocation
1177 * @param yoff Y offset of the region to update in this Allocation
1178 * @param w Width of the region to update
1179 * @param h Height of the region to update
1180 * @param data to be placed into the Allocation
1181 */
Jason Samsf7086092011-01-12 13:28:37 -08001182 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001183 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001184 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1185 Element.DataType.SIGNED_16, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001186 }
1187
Tim Murrayc11e25c2013-04-09 11:01:01 -07001188 /**
1189 * Copy from an array into a rectangular region in this Allocation. The
1190 * array is assumed to be tightly packed.
1191 *
1192 * @param xoff X offset of the region to update in this Allocation
1193 * @param yoff Y offset of the region to update in this Allocation
1194 * @param w Width of the region to update
1195 * @param h Height of the region to update
1196 * @param data to be placed into the Allocation
1197 */
Jason Samsf7086092011-01-12 13:28:37 -08001198 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001199 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001200 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1201 Element.DataType.SIGNED_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001202 }
1203
Tim Murrayc11e25c2013-04-09 11:01:01 -07001204 /**
1205 * Copy from an array into a rectangular region in this Allocation. The
1206 * array is assumed to be tightly packed.
1207 *
1208 * @param xoff X offset of the region to update in this Allocation
1209 * @param yoff Y offset of the region to update in this Allocation
1210 * @param w Width of the region to update
1211 * @param h Height of the region to update
1212 * @param data to be placed into the Allocation
1213 */
Jason Samsf7086092011-01-12 13:28:37 -08001214 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001215 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001216 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1217 Element.DataType.FLOAT_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001218 }
1219
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001220 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001221 * Copy a rectangular region from an Allocation into a rectangular region in
1222 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001223 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001224 * @param xoff X offset of the region in this Allocation
1225 * @param yoff Y offset of the region in this Allocation
1226 * @param w Width of the region to update.
1227 * @param h Height of the region to update.
1228 * @param data source Allocation.
1229 * @param dataXoff X offset in source Allocation
1230 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001231 */
1232 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1233 Allocation data, int dataXoff, int dataYoff) {
Chris Craik06d29842015-06-02 17:19:24 -07001234 try {
1235 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1236 mRS.validate();
1237 validate2DRange(xoff, yoff, w, h);
1238 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
1239 mSelectedLOD, mSelectedFace.mID,
1240 w, h, data.getID(mRS), dataXoff, dataYoff,
1241 data.mSelectedLOD, data.mSelectedFace.mID);
1242 } finally {
1243 Trace.traceEnd(RenderScript.TRACE_TAG);
1244 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001245 }
1246
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001247 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001248 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1249 * and width of the update will use the height and width of the {@link
1250 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001251 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001252 * @param xoff X offset of the region to update in this Allocation
1253 * @param yoff Y offset of the region to update in this Allocation
1254 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001255 */
1256 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Chris Craik5c705d62015-06-01 10:39:36 -07001257 try {
1258 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1259 mRS.validate();
1260 if (data.getConfig() == null) {
1261 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1262 Canvas c = new Canvas(newBitmap);
1263 c.drawBitmap(data, 0, 0, null);
1264 copy2DRangeFrom(xoff, yoff, newBitmap);
1265 return;
1266 }
1267 validateBitmapFormat(data);
1268 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
1269 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
1270 } finally {
1271 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -08001272 }
Jason Samsfa445b92011-01-07 17:00:07 -08001273 }
1274
Jason Samsb05d6892013-04-09 15:59:24 -07001275 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1276 if (mAdaptedAllocation != null) {
1277
1278 } else {
1279
1280 if (xoff < 0 || yoff < 0 || zoff < 0) {
1281 throw new RSIllegalArgumentException("Offset cannot be negative.");
1282 }
1283 if (h < 0 || w < 0 || d < 0) {
1284 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1285 }
1286 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1287 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1288 }
1289 }
1290 }
1291
1292 /**
Miao Wang258db502015-03-03 14:05:36 -08001293 * Copy a rectangular region from the array into the allocation.
1294 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001295 *
Miao Wang258db502015-03-03 14:05:36 -08001296 * The data type of the array is not required to be the same as
1297 * the element data type.
Jason Samsb05d6892013-04-09 15:59:24 -07001298 */
Jason Sams3042d262013-11-25 18:28:33 -08001299 private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1300 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001301 try {
1302 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
1303 mRS.validate();
1304 validate3DRange(xoff, yoff, zoff, w, h, d);
1305 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1306 // AutoPadding for Vec3 Element
1307 boolean usePadding = false;
1308 int sizeBytes = arrayLen * dt.mSize;
1309 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1310 if (dataSize / 4 * 3 > sizeBytes) {
1311 throw new RSIllegalArgumentException("Array too small for allocation type.");
1312 }
1313 usePadding = true;
1314 sizeBytes = dataSize;
1315 } else {
1316 if (dataSize > sizeBytes) {
1317 throw new RSIllegalArgumentException("Array too small for allocation type.");
1318 }
Miao Wang87e908d2015-03-02 15:15:15 -08001319 }
Chris Craik06d29842015-06-02 17:19:24 -07001320 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1321 array, sizeBytes, dt,
1322 mType.mElement.mType.mSize, usePadding);
1323 } finally {
1324 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001325 }
Jason Samsb05d6892013-04-09 15:59:24 -07001326 }
1327
1328 /**
Jason Samsb05d6892013-04-09 15:59:24 -07001329 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001330 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001331 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001332 * @param xoff X offset of the region to update in this Allocation
1333 * @param yoff Y offset of the region to update in this Allocation
1334 * @param zoff Z offset of the region to update in this Allocation
1335 * @param w Width of the region to update
1336 * @param h Height of the region to update
1337 * @param d Depth of the region to update
Miao Wang87e908d2015-03-02 15:15:15 -08001338 * @param array to be placed into the allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001339 */
Jason Sams3042d262013-11-25 18:28:33 -08001340 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001341 try {
1342 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
1343 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
1344 validateObjectIsPrimitiveArray(array, true),
1345 java.lang.reflect.Array.getLength(array));
1346 } finally {
1347 Trace.traceEnd(RenderScript.TRACE_TAG);
1348 }
Jason Samsb05d6892013-04-09 15:59:24 -07001349 }
1350
1351 /**
Jason Samsb05d6892013-04-09 15:59:24 -07001352 * Copy a rectangular region into the allocation from another
1353 * allocation.
1354 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001355 * @param xoff X offset of the region to update in this Allocation
1356 * @param yoff Y offset of the region to update in this Allocation
1357 * @param zoff Z offset of the region to update in this Allocation
1358 * @param w Width of the region to update.
1359 * @param h Height of the region to update.
1360 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001361 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001362 * @param dataXoff X offset of the region in the source Allocation
1363 * @param dataYoff Y offset of the region in the source Allocation
1364 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001365 */
1366 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1367 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1368 mRS.validate();
1369 validate3DRange(xoff, yoff, zoff, w, h, d);
1370 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1371 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1372 data.mSelectedLOD);
1373 }
1374
Jason Samsfa445b92011-01-07 17:00:07 -08001375
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001376 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001377 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1378 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001379 *
1380 * @param b The bitmap to be set from the Allocation.
1381 */
Jason Samsfa445b92011-01-07 17:00:07 -08001382 public void copyTo(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -07001383 try {
1384 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1385 mRS.validate();
1386 validateBitmapFormat(b);
1387 validateBitmapSize(b);
1388 mRS.nAllocationCopyToBitmap(getID(mRS), b);
1389 } finally {
1390 Trace.traceEnd(RenderScript.TRACE_TAG);
1391 }
Jason Samsfa445b92011-01-07 17:00:07 -08001392 }
1393
Jason Sams3042d262013-11-25 18:28:33 -08001394 private void copyTo(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001395 try {
1396 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1397 mRS.validate();
1398 boolean usePadding = false;
1399 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1400 usePadding = true;
Miao Wangd9b63282015-04-03 09:15:39 -07001401 }
Chris Craik06d29842015-06-02 17:19:24 -07001402 if (usePadding) {
1403 if (dt.mSize * arrayLen < mSize / 4 * 3) {
1404 throw new RSIllegalArgumentException(
1405 "Size of output array cannot be smaller than size of allocation.");
1406 }
1407 } else {
1408 if (dt.mSize * arrayLen < mSize) {
1409 throw new RSIllegalArgumentException(
1410 "Size of output array cannot be smaller than size of allocation.");
1411 }
Miao Wangd9b63282015-04-03 09:15:39 -07001412 }
Chris Craik06d29842015-06-02 17:19:24 -07001413 mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
1414 } finally {
1415 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wangd9b63282015-04-03 09:15:39 -07001416 }
Jason Sams3042d262013-11-25 18:28:33 -08001417 }
1418
1419 /**
1420 * Copy from the Allocation into an array. The array must be at
1421 * least as large as the Allocation. The
1422 * {@link android.renderscript.Element} must match the component
1423 * type of the array passed in.
1424 *
1425 * @param array The array to be set from the Allocation.
1426 */
1427 public void copyTo(Object array) {
1428 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1429 java.lang.reflect.Array.getLength(array));
1430 }
1431
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001432 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001433 * Copy from the Allocation into a byte array. The array must be at least
1434 * as large as the Allocation. The allocation must be of an 8 bit integer
1435 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001436 *
1437 * @param d The array to be set from the Allocation.
1438 */
Jason Samsfa445b92011-01-07 17:00:07 -08001439 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001440 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001441 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001442 }
1443
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001444 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001445 * Copy from the Allocation into a short array. The array must be at least
1446 * as large as the Allocation. The allocation must be of an 16 bit integer
1447 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001448 *
1449 * @param d The array to be set from the Allocation.
1450 */
Jason Samsfa445b92011-01-07 17:00:07 -08001451 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001452 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001453 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001454 }
1455
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001456 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001457 * Copy from the Allocation into a int array. The array must be at least as
1458 * large as the Allocation. The allocation must be of an 32 bit integer
1459 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001460 *
1461 * @param d The array to be set from the Allocation.
1462 */
Jason Samsfa445b92011-01-07 17:00:07 -08001463 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001464 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001465 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001466 }
1467
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001468 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001469 * Copy from the Allocation into a float array. The array must be at least
1470 * as large as the Allocation. The allocation must be of an 32 bit float
1471 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001472 *
1473 * @param d The array to be set from the Allocation.
1474 */
Jason Samsfa445b92011-01-07 17:00:07 -08001475 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001476 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001477 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001478 }
1479
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001480 /**
Miao Wang3c613272015-05-11 11:41:55 -07001481 * @hide
1482 *
Miao Wang45cec0a2015-03-04 16:40:21 -08001483 * This is only intended to be used by auto-generated code reflected from
1484 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -08001485 *
1486 * @param xoff
1487 * @param yoff
1488 * @param zoff
1489 * @param component_number
Miao Wang258db502015-03-03 14:05:36 -08001490 * @param fp
Miao Wangc8e237e2015-02-20 18:36:32 -08001491 */
Miao Wang45cec0a2015-03-04 16:40:21 -08001492 public void copyToFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001493 mRS.validate();
1494 if (component_number >= mType.mElement.mElements.length) {
1495 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
1496 }
1497 if(xoff < 0) {
1498 throw new RSIllegalArgumentException("Offset x must be >= 0.");
1499 }
1500 if(yoff < 0) {
1501 throw new RSIllegalArgumentException("Offset y must be >= 0.");
1502 }
1503 if(zoff < 0) {
1504 throw new RSIllegalArgumentException("Offset z must be >= 0.");
1505 }
1506
Miao Wang45cec0a2015-03-04 16:40:21 -08001507 final byte[] data = fp.getData();
Miao Wangbfa5e652015-05-04 15:29:25 -07001508 int data_length = data.length;
Miao Wangc8e237e2015-02-20 18:36:32 -08001509 int eSize = mType.mElement.mElements[component_number].getBytesSize();
1510 eSize *= mType.mElement.mArraySizes[component_number];
1511
Miao Wang45cec0a2015-03-04 16:40:21 -08001512 if (data_length != eSize) {
1513 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
1514 " does not match component size " + eSize + ".");
Miao Wangc8e237e2015-02-20 18:36:32 -08001515 }
1516
1517 mRS.nAllocationElementRead(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Miao Wang45cec0a2015-03-04 16:40:21 -08001518 component_number, data, data_length);
Miao Wangc8e237e2015-02-20 18:36:32 -08001519 }
1520 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001521 * Resize a 1D allocation. The contents of the allocation are preserved.
1522 * If new elements are allocated objects are created with null contents and
1523 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001524 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001525 * <p>If the new region is smaller the references of any objects outside the
1526 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001527 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001528 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001529 *
1530 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001531 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001532 * @deprecated RenderScript objects should be immutable once created. The
Tim Murraycd38b762014-08-13 13:20:25 -07001533 * replacement is to create a new allocation and copy the contents. This
1534 * function will throw an exception if API 21 or higher is used.
Jason Samsf7086092011-01-12 13:28:37 -08001535 */
Jason Sams31a7e422010-10-26 13:09:17 -07001536 public synchronized void resize(int dimX) {
Tim Murraycd38b762014-08-13 13:20:25 -07001537 if (mRS.getApplicationContext().getApplicationInfo().targetSdkVersion >= 21) {
1538 throw new RSRuntimeException("Resize is not allowed in API 21+.");
1539 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001540 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001541 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001542 }
Jason Samse07694b2012-04-03 15:36:36 -07001543 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001544 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001545
Tim Murray460a0492013-11-19 12:45:54 -08001546 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001547 mType = new Type(typeID, mRS);
1548 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001549 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001550 }
1551
Miao Wangc8e237e2015-02-20 18:36:32 -08001552 private void copy1DRangeToUnchecked(int off, int count, Object array,
1553 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001554 try {
1555 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked");
1556 final int dataSize = mType.mElement.getBytesSize() * count;
1557 // AutoPadding for Vec3 Element
1558 boolean usePadding = false;
1559 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1560 usePadding = true;
1561 }
1562 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
1563 mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
1564 mType.mElement.mType.mSize, usePadding);
1565 } finally {
1566 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001567 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001568 }
1569
1570 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001571 * Copy part of this Allocation into an array. This method does not
1572 * guarantee that the Allocation is compatible with the input buffer.
1573 *
1574 * @param off The offset of the first element to be copied.
1575 * @param count The number of elements to be copied.
1576 * @param array The dest data array
1577 */
1578 public void copy1DRangeToUnchecked(int off, int count, Object array) {
1579 copy1DRangeToUnchecked(off, count, array,
1580 validateObjectIsPrimitiveArray(array, false),
1581 java.lang.reflect.Array.getLength(array));
1582 }
1583
1584 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001585 * Copy part of this Allocation into an array. This method does not
1586 * guarantee that the Allocation is compatible with the input buffer.
1587 *
1588 * @param off The offset of the first element to be copied.
1589 * @param count The number of elements to be copied.
1590 * @param d the source data array
1591 */
1592 public void copy1DRangeToUnchecked(int off, int count, int[] d) {
1593 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
1594 }
1595
1596 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001597 * Copy part of this Allocation into an array. This method does not
1598 * guarantee that the Allocation is compatible with the input buffer.
1599 *
1600 * @param off The offset of the first element to be copied.
1601 * @param count The number of elements to be copied.
1602 * @param d the source data array
1603 */
1604 public void copy1DRangeToUnchecked(int off, int count, short[] d) {
1605 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
1606 }
1607
1608 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001609 * Copy part of this Allocation into an array. This method does not
1610 * guarantee that the Allocation is compatible with the input buffer.
1611 *
1612 * @param off The offset of the first element to be copied.
1613 * @param count The number of elements to be copied.
1614 * @param d the source data array
1615 */
1616 public void copy1DRangeToUnchecked(int off, int count, byte[] d) {
1617 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
1618 }
1619
1620 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001621 * Copy part of this Allocation into an array. This method does not
1622 * guarantee that the Allocation is compatible with the input buffer.
1623 *
1624 * @param off The offset of the first element to be copied.
1625 * @param count The number of elements to be copied.
1626 * @param d the source data array
1627 */
1628 public void copy1DRangeToUnchecked(int off, int count, float[] d) {
1629 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
1630 }
1631
1632
1633 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001634 * Copy part of this Allocation into an array. This method does not
1635 * and will generate exceptions if the Allocation type does not
1636 * match the component type of the array passed in.
1637 *
1638 * @param off The offset of the first element to be copied.
1639 * @param count The number of elements to be copied.
1640 * @param array The source data array.
1641 */
1642 public void copy1DRangeTo(int off, int count, Object array) {
1643 copy1DRangeToUnchecked(off, count, array,
1644 validateObjectIsPrimitiveArray(array, true),
1645 java.lang.reflect.Array.getLength(array));
1646 }
1647
1648 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001649 * Copy part of this Allocation into an array. This method does not
1650 * and will generate exceptions if the Allocation type is not a 32 bit
1651 * integer type.
1652 *
1653 * @param off The offset of the first element to be copied.
1654 * @param count The number of elements to be copied.
1655 * @param d the source data array
1656 */
1657 public void copy1DRangeTo(int off, int count, int[] d) {
1658 validateIsInt32();
1659 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
1660 }
1661
1662 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001663 * Copy part of this Allocation into an array. This method does not
1664 * and will generate exceptions if the Allocation type is not a 16 bit
1665 * integer type.
1666 *
1667 * @param off The offset of the first element to be copied.
1668 * @param count The number of elements to be copied.
1669 * @param d the source data array
1670 */
1671 public void copy1DRangeTo(int off, int count, short[] d) {
1672 validateIsInt16();
1673 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
1674 }
1675
1676 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001677 * Copy part of this Allocation into an array. This method does not
1678 * and will generate exceptions if the Allocation type is not an 8 bit
1679 * integer type.
1680 *
1681 * @param off The offset of the first element to be copied.
1682 * @param count The number of elements to be copied.
1683 * @param d the source data array
1684 */
1685 public void copy1DRangeTo(int off, int count, byte[] d) {
1686 validateIsInt8();
1687 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
1688 }
1689
1690 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001691 * Copy part of this Allocation into an array. This method does not
1692 * and will generate exceptions if the Allocation type is not a 32 bit float
1693 * type.
1694 *
1695 * @param off The offset of the first element to be copied.
1696 * @param count The number of elements to be copied.
1697 * @param d the source data array.
1698 */
1699 public void copy1DRangeTo(int off, int count, float[] d) {
1700 validateIsFloat32();
1701 copy1DRangeToUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
1702 }
1703
1704
1705 void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
1706 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001707 try {
1708 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked");
1709 mRS.validate();
1710 validate2DRange(xoff, yoff, w, h);
1711 final int dataSize = mType.mElement.getBytesSize() * w * h;
1712 // AutoPadding for Vec3 Element
1713 boolean usePadding = false;
1714 int sizeBytes = arrayLen * dt.mSize;
1715 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1716 if (dataSize / 4 * 3 > sizeBytes) {
1717 throw new RSIllegalArgumentException("Array too small for allocation type.");
1718 }
1719 usePadding = true;
1720 sizeBytes = dataSize;
1721 } else {
1722 if (dataSize > sizeBytes) {
1723 throw new RSIllegalArgumentException("Array too small for allocation type.");
1724 }
Miao Wang87e908d2015-03-02 15:15:15 -08001725 }
Chris Craik06d29842015-06-02 17:19:24 -07001726 mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1727 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
1728 } finally {
1729 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001730 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001731 }
1732
1733 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001734 * Copy from a rectangular region in this Allocation into an array.
1735 *
1736 * @param xoff X offset of the region to copy in this Allocation
1737 * @param yoff Y offset of the region to copy in this Allocation
1738 * @param w Width of the region to copy
1739 * @param h Height of the region to copy
1740 * @param array Dest Array to be copied into
1741 */
1742 public void copy2DRangeTo(int xoff, int yoff, int w, int h, Object array) {
1743 copy2DRangeToUnchecked(xoff, yoff, w, h, array,
1744 validateObjectIsPrimitiveArray(array, true),
1745 java.lang.reflect.Array.getLength(array));
1746 }
1747
1748 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001749 * Copy from a rectangular region in this Allocation into an array.
1750 *
1751 * @param xoff X offset of the region to copy in this Allocation
1752 * @param yoff Y offset of the region to copy in this Allocation
1753 * @param w Width of the region to copy
1754 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001755 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001756 */
1757 public void copy2DRangeTo(int xoff, int yoff, int w, int h, byte[] data) {
1758 validateIsInt8();
1759 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1760 Element.DataType.SIGNED_8, data.length);
1761 }
1762
1763 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001764 * Copy from a rectangular region in this Allocation into an array.
1765 *
1766 * @param xoff X offset of the region to copy in this Allocation
1767 * @param yoff Y offset of the region to copy in this Allocation
1768 * @param w Width of the region to copy
1769 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001770 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001771 */
1772 public void copy2DRangeTo(int xoff, int yoff, int w, int h, short[] data) {
1773 validateIsInt16();
1774 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1775 Element.DataType.SIGNED_16, data.length);
1776 }
1777
1778 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001779 * Copy from a rectangular region in this Allocation into an array.
1780 *
1781 * @param xoff X offset of the region to copy in this Allocation
1782 * @param yoff Y offset of the region to copy in this Allocation
1783 * @param w Width of the region to copy
1784 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001785 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001786 */
1787 public void copy2DRangeTo(int xoff, int yoff, int w, int h, int[] data) {
1788 validateIsInt32();
1789 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1790 Element.DataType.SIGNED_32, data.length);
1791 }
1792
1793 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001794 * Copy from a rectangular region in this Allocation into an array.
1795 *
1796 * @param xoff X offset of the region to copy in this Allocation
1797 * @param yoff Y offset of the region to copy in this Allocation
1798 * @param w Width of the region to copy
1799 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001800 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001801 */
1802 public void copy2DRangeTo(int xoff, int yoff, int w, int h, float[] data) {
1803 validateIsFloat32();
1804 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1805 Element.DataType.FLOAT_32, data.length);
1806 }
1807
1808
1809 /**
Miao Wang258db502015-03-03 14:05:36 -08001810 * Copy from a rectangular region in this Allocation into an array.
1811 * The array is assumed to be tightly packed.
Miao Wangc8e237e2015-02-20 18:36:32 -08001812 *
Miao Wang258db502015-03-03 14:05:36 -08001813 * The data type of the array is not required to be the same as
1814 * the element data type.
Miao Wangc8e237e2015-02-20 18:36:32 -08001815 */
1816 private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1817 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001818 try {
1819 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked");
1820 mRS.validate();
1821 validate3DRange(xoff, yoff, zoff, w, h, d);
1822 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1823 // AutoPadding for Vec3 Element
1824 boolean usePadding = false;
1825 int sizeBytes = arrayLen * dt.mSize;
1826 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1827 if (dataSize / 4 * 3 > sizeBytes) {
1828 throw new RSIllegalArgumentException("Array too small for allocation type.");
1829 }
1830 usePadding = true;
1831 sizeBytes = dataSize;
1832 } else {
1833 if (dataSize > sizeBytes) {
1834 throw new RSIllegalArgumentException("Array too small for allocation type.");
1835 }
Miao Wang87e908d2015-03-02 15:15:15 -08001836 }
Chris Craik06d29842015-06-02 17:19:24 -07001837 mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1838 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
1839 } finally {
1840 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001841 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001842 }
1843
Miao Wang258db502015-03-03 14:05:36 -08001844 /*
Miao Wangc8e237e2015-02-20 18:36:32 -08001845 * Copy from a rectangular region in this Allocation into an array.
1846 *
1847 * @param xoff X offset of the region to copy in this Allocation
1848 * @param yoff Y offset of the region to copy in this Allocation
1849 * @param zoff Z offset of the region to copy in this Allocation
1850 * @param w Width of the region to copy
1851 * @param h Height of the region to copy
1852 * @param d Depth of the region to copy
1853 * @param array Dest Array to be copied into
1854 */
1855 public void copy3DRangeTo(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1856 copy3DRangeToUnchecked(xoff, yoff, zoff, w, h, d, array,
1857 validateObjectIsPrimitiveArray(array, true),
1858 java.lang.reflect.Array.getLength(array));
1859 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001860
1861 // creation
1862
Jason Sams49a05d72010-12-29 14:31:29 -08001863 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001864 static {
1865 mBitmapOptions.inScaled = false;
1866 }
1867
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001868 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001869 * Creates a new Allocation with the given {@link
1870 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001871 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001872 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001873 * @param mips specifies desired mipmap behaviour for the
1874 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001875 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001876 * utilized
1877 */
1878 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07001879 try {
1880 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
1881 rs.validate();
1882 if (type.getID(rs) == 0) {
1883 throw new RSInvalidStateException("Bad Type");
1884 }
1885 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
1886 if (id == 0) {
1887 throw new RSRuntimeException("Allocation creation failed.");
1888 }
1889 return new Allocation(id, rs, type, usage);
1890 } finally {
1891 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams1bada8c2009-08-09 17:01:55 -07001892 }
Jason Sams857d0c72011-11-23 15:02:15 -08001893 }
1894
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001895 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001896 * Creates an Allocation with the size specified by the type and no mipmaps
1897 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001898 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001899 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001900 * @param type renderscript type describing data layout
1901 * @param usage bit field specifying how the allocation is
1902 * utilized
1903 *
1904 * @return allocation
1905 */
Jason Samse5d37122010-12-16 00:33:33 -08001906 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1907 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1908 }
1909
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001910 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001911 * Creates an Allocation for use by scripts with a given {@link
1912 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001913 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001914 * @param rs Context to which the Allocation will belong.
1915 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001916 *
1917 * @return allocation
1918 */
Jason Sams5476b452010-12-08 16:14:36 -08001919 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001920 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001921 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001922
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001923 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001924 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001925 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001926 * @param rs Context to which the Allocation will belong.
1927 * @param e Element to use in the Allocation
1928 * @param count the number of Elements in the Allocation
1929 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001930 * utilized
1931 *
1932 * @return allocation
1933 */
Jason Sams5476b452010-12-08 16:14:36 -08001934 static public Allocation createSized(RenderScript rs, Element e,
1935 int count, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07001936 try {
1937 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
1938 rs.validate();
1939 Type.Builder b = new Type.Builder(rs, e);
1940 b.setX(count);
1941 Type t = b.create();
Jason Sams768bc022009-09-21 19:41:04 -07001942
Chris Craik06d29842015-06-02 17:19:24 -07001943 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
1944 if (id == 0) {
1945 throw new RSRuntimeException("Allocation creation failed.");
1946 }
1947 return new Allocation(id, rs, t, usage);
1948 } finally {
1949 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001950 }
Jason Sams5476b452010-12-08 16:14:36 -08001951 }
1952
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001953 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001954 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001955 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001956 * @param rs Context to which the Allocation will belong.
1957 * @param e Element to use in the Allocation
1958 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001959 *
1960 * @return allocation
1961 */
Jason Sams5476b452010-12-08 16:14:36 -08001962 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001963 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001964 }
1965
Jason Sams49a05d72010-12-29 14:31:29 -08001966 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001967 final Bitmap.Config bc = b.getConfig();
1968 if (bc == Bitmap.Config.ALPHA_8) {
1969 return Element.A_8(rs);
1970 }
1971 if (bc == Bitmap.Config.ARGB_4444) {
1972 return Element.RGBA_4444(rs);
1973 }
1974 if (bc == Bitmap.Config.ARGB_8888) {
1975 return Element.RGBA_8888(rs);
1976 }
1977 if (bc == Bitmap.Config.RGB_565) {
1978 return Element.RGB_565(rs);
1979 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001980 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001981 }
1982
Jason Sams49a05d72010-12-29 14:31:29 -08001983 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001984 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001985 Element e = elementFromBitmap(rs, b);
1986 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001987 tb.setX(b.getWidth());
1988 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001989 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001990 return tb.create();
1991 }
1992
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001993 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001994 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001995 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001996 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001997 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001998 * @param mips specifies desired mipmap behaviour for the
1999 * allocation
2000 * @param usage bit field specifying how the allocation is
2001 * utilized
2002 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002003 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002004 *
2005 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002006 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002007 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002008 int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07002009 try {
2010 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
2011 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08002012
Chris Craik06d29842015-06-02 17:19:24 -07002013 // WAR undocumented color formats
2014 if (b.getConfig() == null) {
2015 if ((usage & USAGE_SHARED) != 0) {
2016 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
2017 }
2018 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
2019 Canvas c = new Canvas(newBitmap);
2020 c.drawBitmap(b, 0, 0, null);
2021 return createFromBitmap(rs, newBitmap, mips, usage);
Tim Murrayabd5db92013-02-28 11:45:22 -08002022 }
Tim Murrayabd5db92013-02-28 11:45:22 -08002023
Chris Craik06d29842015-06-02 17:19:24 -07002024 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08002025
Chris Craik06d29842015-06-02 17:19:24 -07002026 // enable optimized bitmap path only with no mipmap and script-only usage
2027 if (mips == MipmapControl.MIPMAP_NONE &&
2028 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
2029 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
2030 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
2031 if (id == 0) {
2032 throw new RSRuntimeException("Load failed.");
2033 }
2034
2035 // keep a reference to the Bitmap around to prevent GC
2036 Allocation alloc = new Allocation(id, rs, t, usage);
2037 alloc.setBitmap(b);
2038 return alloc;
2039 }
2040
2041
2042 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08002043 if (id == 0) {
2044 throw new RSRuntimeException("Load failed.");
2045 }
Chris Craik06d29842015-06-02 17:19:24 -07002046 return new Allocation(id, rs, t, usage);
2047 } finally {
2048 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murraya3145512012-12-04 17:59:29 -08002049 }
Jason Sams5476b452010-12-08 16:14:36 -08002050 }
2051
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002052 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002053 * Returns the handle to a raw buffer that is being managed by the screen
2054 * compositor. This operation is only valid for Allocations with {@link
2055 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002056 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07002057 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002058 *
2059 */
2060 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08002061 if ((mUsage & USAGE_IO_INPUT) == 0) {
2062 throw new RSInvalidStateException("Allocation is not a surface texture.");
2063 }
Jason Sams1e68bac2015-03-17 16:36:55 -07002064
2065 if (mGetSurfaceSurface == null) {
2066 mGetSurfaceSurface = mRS.nAllocationGetSurface(getID(mRS));
2067 }
2068
2069 return mGetSurfaceSurface;
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002070 }
2071
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002072 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002073 * Associate a {@link android.view.Surface} with this Allocation. This
2074 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07002075 *
2076 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08002077 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002078 public void setSurface(Surface sur) {
2079 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08002080 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
2081 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
2082 }
2083
Jason Samse07694b2012-04-03 15:36:36 -07002084 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08002085 }
2086
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002087 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002088 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08002089 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002090 * <p>With target API version 18 or greater, this Allocation will be created
2091 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
2092 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
2093 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002094 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002095 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002096 * @param b bitmap source for the allocation data
2097 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002098 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002099 *
2100 */
Jason Sams6d8eb262010-12-15 01:41:00 -08002101 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08002102 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2103 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07002104 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08002105 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002106 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
2107 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08002108 }
2109
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002110 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002111 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
2112 * containing the horizontal list of cube faces. Each face must be a square,
2113 * have the same size as all other faces, and have a width that is a power
2114 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002115 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002116 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07002117 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002118 * format: right, left, top, bottom, front, back
2119 * @param mips specifies desired mipmap behaviour for the cubemap
2120 * @param usage bit field specifying how the cubemap is utilized
2121 *
2122 * @return allocation containing cubemap data
2123 *
2124 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002125 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002126 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002127 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002128 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08002129
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002130 int height = b.getHeight();
2131 int width = b.getWidth();
2132
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002133 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002134 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
2135 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002136 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002137 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002138 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002139 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002140 if (!isPow2) {
2141 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2142 }
2143
2144 Element e = elementFromBitmap(rs, b);
2145 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002146 tb.setX(height);
2147 tb.setY(height);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08002148 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08002149 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002150 Type t = tb.create();
2151
Tim Murray460a0492013-11-19 12:45:54 -08002152 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002153 if(id == 0) {
2154 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
2155 }
Jason Sams5476b452010-12-08 16:14:36 -08002156 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002157 }
2158
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002159 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002160 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
2161 * from a {@link android.graphics.Bitmap} containing the horizontal list of
2162 * cube faces. Each face must be a square, have the same size as all other
2163 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002164 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002165 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002166 * @param b bitmap with cubemap faces layed out in the following
2167 * format: right, left, top, bottom, front, back
2168 *
2169 * @return allocation containing cubemap data
2170 *
2171 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002172 static public Allocation createCubemapFromBitmap(RenderScript rs,
2173 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08002174 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002175 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08002176 }
2177
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002178 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002179 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
2180 * objects containing the cube faces. Each face must be a square, have the
2181 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002182 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002183 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002184 * @param xpos cubemap face in the positive x direction
2185 * @param xneg cubemap face in the negative x direction
2186 * @param ypos cubemap face in the positive y direction
2187 * @param yneg cubemap face in the negative y direction
2188 * @param zpos cubemap face in the positive z direction
2189 * @param zneg cubemap face in the negative z direction
2190 * @param mips specifies desired mipmap behaviour for the cubemap
2191 * @param usage bit field specifying how the cubemap is utilized
2192 *
2193 * @return allocation containing cubemap data
2194 *
2195 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002196 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2197 Bitmap xpos,
2198 Bitmap xneg,
2199 Bitmap ypos,
2200 Bitmap yneg,
2201 Bitmap zpos,
2202 Bitmap zneg,
2203 MipmapControl mips,
2204 int usage) {
2205 int height = xpos.getHeight();
2206 if (xpos.getWidth() != height ||
2207 xneg.getWidth() != height || xneg.getHeight() != height ||
2208 ypos.getWidth() != height || ypos.getHeight() != height ||
2209 yneg.getWidth() != height || yneg.getHeight() != height ||
2210 zpos.getWidth() != height || zpos.getHeight() != height ||
2211 zneg.getWidth() != height || zneg.getHeight() != height) {
2212 throw new RSIllegalArgumentException("Only square cube map faces supported");
2213 }
2214 boolean isPow2 = (height & (height - 1)) == 0;
2215 if (!isPow2) {
2216 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2217 }
2218
2219 Element e = elementFromBitmap(rs, xpos);
2220 Type.Builder tb = new Type.Builder(rs, e);
2221 tb.setX(height);
2222 tb.setY(height);
2223 tb.setFaces(true);
2224 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
2225 Type t = tb.create();
2226 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
2227
2228 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07002229 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002230 adapter.copyFrom(xpos);
2231 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
2232 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002233 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002234 adapter.copyFrom(ypos);
2235 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
2236 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002237 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002238 adapter.copyFrom(zpos);
2239 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
2240 adapter.copyFrom(zneg);
2241
2242 return cubemap;
2243 }
2244
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002245 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002246 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
2247 * from 6 {@link android.graphics.Bitmap} objects containing the cube
2248 * faces. Each face must be a square, have the same size as all other faces,
2249 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002250 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002251 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002252 * @param xpos cubemap face in the positive x direction
2253 * @param xneg cubemap face in the negative x direction
2254 * @param ypos cubemap face in the positive y direction
2255 * @param yneg cubemap face in the negative y direction
2256 * @param zpos cubemap face in the positive z direction
2257 * @param zneg cubemap face in the negative z direction
2258 *
2259 * @return allocation containing cubemap data
2260 *
2261 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002262 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2263 Bitmap xpos,
2264 Bitmap xneg,
2265 Bitmap ypos,
2266 Bitmap yneg,
2267 Bitmap zpos,
2268 Bitmap zneg) {
2269 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
2270 zpos, zneg, MipmapControl.MIPMAP_NONE,
2271 USAGE_GRAPHICS_TEXTURE);
2272 }
2273
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002274 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002275 * Creates an Allocation from the Bitmap referenced
2276 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002277 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002278 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002279 * @param res application resources
2280 * @param id resource id to load the data from
2281 * @param mips specifies desired mipmap behaviour for the
2282 * allocation
2283 * @param usage bit field specifying how the allocation is
2284 * utilized
2285 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002286 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002287 *
2288 */
Jason Sams5476b452010-12-08 16:14:36 -08002289 static public Allocation createFromBitmapResource(RenderScript rs,
2290 Resources res,
2291 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08002292 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002293 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07002294
Jason Sams771bebb2009-12-07 12:40:12 -08002295 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07002296 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
2297 throw new RSIllegalArgumentException("Unsupported usage specified.");
2298 }
Jason Sams5476b452010-12-08 16:14:36 -08002299 Bitmap b = BitmapFactory.decodeResource(res, id);
2300 Allocation alloc = createFromBitmap(rs, b, mips, usage);
2301 b.recycle();
2302 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07002303 }
2304
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002305 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002306 * Creates a non-mipmapped Allocation to use as a graphics texture from the
2307 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002308 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002309 * <p>With target API version 18 or greater, this allocation will be created
2310 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
2311 * target API version 17 or lower, this allocation will be created with
2312 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08002313 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002314 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002315 * @param res application resources
2316 * @param id resource id to load the data from
2317 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002318 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002319 *
2320 */
Jason Sams5476b452010-12-08 16:14:36 -08002321 static public Allocation createFromBitmapResource(RenderScript rs,
2322 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08002323 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08002324 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2325 return createFromBitmapResource(rs, res, id,
2326 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07002327 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08002328 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002329 return createFromBitmapResource(rs, res, id,
2330 MipmapControl.MIPMAP_NONE,
2331 USAGE_GRAPHICS_TEXTURE);
2332 }
2333
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002334 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002335 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002336 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002337 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002338 * @param str string to create the allocation from
2339 * @param usage bit field specifying how the allocaiton is
2340 * utilized
2341 *
2342 */
Jason Sams5476b452010-12-08 16:14:36 -08002343 static public Allocation createFromString(RenderScript rs,
2344 String str,
2345 int usage) {
2346 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002347 byte[] allocArray = null;
2348 try {
2349 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08002350 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08002351 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002352 return alloc;
2353 }
2354 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08002355 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002356 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002357 }
Jason Sams739c8262013-04-11 18:07:52 -07002358
2359 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002360 * Interface to handle notification when new buffers are available via
2361 * {@link #USAGE_IO_INPUT}. An application will receive one notification
2362 * when a buffer is available. Additional buffers will not trigger new
2363 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07002364 */
Jason Sams42ef2382013-08-29 13:30:59 -07002365 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07002366 public void onBufferAvailable(Allocation a);
2367 }
2368
2369 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002370 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07002371 *
Jason Sams42ef2382013-08-29 13:30:59 -07002372 * @param callback instance of the OnBufferAvailableListener
2373 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07002374 */
Jason Sams42ef2382013-08-29 13:30:59 -07002375 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07002376 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002377 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07002378 mBufferNotifier = callback;
2379 }
2380 }
2381
Tim Murrayb730d862014-08-18 16:14:24 -07002382 static void sendBufferNotification(long id) {
Jason Sams739c8262013-04-11 18:07:52 -07002383 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002384 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07002385
2386 if ((a != null) && (a.mBufferNotifier != null)) {
2387 a.mBufferNotifier.onBufferAvailable(a);
2388 }
2389 }
2390 }
2391
Miao Wangf0f6e802015-02-03 17:16:43 -08002392 /**
2393 * For USAGE_IO_OUTPUT, destroy() implies setSurface(null).
2394 *
2395 */
2396 @Override
2397 public void destroy() {
2398 if((mUsage & USAGE_IO_OUTPUT) != 0) {
2399 setSurface(null);
2400 }
2401 super.destroy();
2402 }
Jason Samsb8c5a842009-07-31 20:40:47 -07002403}