blob: 70a5821bd6a3dfd6fbb485e621f9f35daa9e89cc [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 **/
54public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070055 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080056 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080057 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070058 Allocation mAdaptedAllocation;
Tim Murray2f2472c2013-08-22 14:55:26 -070059 int mSize;
Jason Samsba862d12011-07-07 15:24:42 -070060
Jason Sams615e7ce2012-01-13 14:01:20 -080061 boolean mReadAllowed = true;
62 boolean mWriteAllowed = true;
Miao Wang87e908d2015-03-02 15:15:15 -080063 boolean mAutoPadding = false;
Jason Sams46ba27e32015-02-06 17:45:15 -080064 int mSelectedX;
Jason Samsba862d12011-07-07 15:24:42 -070065 int mSelectedY;
66 int mSelectedZ;
67 int mSelectedLOD;
Jason Sams46ba27e32015-02-06 17:45:15 -080068 int mSelectedArray[];
Jason Samsba862d12011-07-07 15:24:42 -070069 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
70
71 int mCurrentDimX;
72 int mCurrentDimY;
73 int mCurrentDimZ;
74 int mCurrentCount;
Tim Murray460a0492013-11-19 12:45:54 -080075 static HashMap<Long, Allocation> mAllocationMap =
76 new HashMap<Long, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070077 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070078
Jason Sams1e68bac2015-03-17 16:36:55 -070079 private Surface mGetSurfaceSurface = null;
80
Jason Sams3042d262013-11-25 18:28:33 -080081 private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
82 final Class c = d.getClass();
83 if (!c.isArray()) {
84 throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
85 }
86 final Class cmp = c.getComponentType();
87 if (!cmp.isPrimitive()) {
88 throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
89 }
90
91 if (cmp == Long.TYPE) {
92 if (checkType) {
93 validateIsInt64();
94 return mType.mElement.mType;
95 }
96 return Element.DataType.SIGNED_64;
97 }
98
99 if (cmp == Integer.TYPE) {
100 if (checkType) {
101 validateIsInt32();
102 return mType.mElement.mType;
103 }
104 return Element.DataType.SIGNED_32;
105 }
106
107 if (cmp == Short.TYPE) {
108 if (checkType) {
109 validateIsInt16();
110 return mType.mElement.mType;
111 }
112 return Element.DataType.SIGNED_16;
113 }
114
115 if (cmp == Byte.TYPE) {
116 if (checkType) {
117 validateIsInt8();
118 return mType.mElement.mType;
119 }
120 return Element.DataType.SIGNED_8;
121 }
122
123 if (cmp == Float.TYPE) {
124 if (checkType) {
125 validateIsFloat32();
126 }
127 return Element.DataType.FLOAT_32;
128 }
129
130 if (cmp == Double.TYPE) {
131 if (checkType) {
132 validateIsFloat64();
133 }
134 return Element.DataType.FLOAT_64;
135 }
136 return null;
137 }
138
139
Tim Murrayc11e25c2013-04-09 11:01:01 -0700140 /**
141 * The usage of the Allocation. These signal to RenderScript where to place
142 * the Allocation in memory.
143 *
144 */
Jason Sams5476b452010-12-08 16:14:36 -0800145
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700146 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700147 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -0800148 */
Jason Sams5476b452010-12-08 16:14:36 -0800149 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800150
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700151 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700152 * The Allocation will be used as a texture source by one or more graphics
153 * programs.
Jason Samsf7086092011-01-12 13:28:37 -0800154 *
155 */
Jason Sams5476b452010-12-08 16:14:36 -0800156 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800157
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700158 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700159 * The Allocation will be used as a graphics mesh.
160 *
161 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800162 *
163 */
Jason Sams5476b452010-12-08 16:14:36 -0800164 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800165
166
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700167 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700168 * The Allocation will be used as the source of shader constants by one or
169 * more programs.
170 *
171 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800172 *
173 */
Jason Sams5476b452010-12-08 16:14:36 -0800174 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
175
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700176 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700177 * The Allocation will be used as a target for offscreen rendering
178 *
179 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700180 *
181 */
182 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
183
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700184 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700185 * The Allocation will be used as a {@link android.view.Surface}
186 * consumer. This usage will cause the Allocation to be created
187 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800188 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800189 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700190 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800191
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700192 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700193 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700194 * producer. The dimensions and format of the {@link
Jason Sams3a1b8e42013-09-24 15:18:52 -0700195 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700196 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800197 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800198 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700199 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700200
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700201 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700202 * The Allocation's backing store will be inherited from another object
203 * (usually a {@link android.graphics.Bitmap}); copying to or from the
204 * original source Bitmap will cause a synchronization rather than a full
205 * copy. {@link #syncAll} may also be used to synchronize the Allocation
206 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800207 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700208 * <p>This is set by default for allocations created with {@link
209 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800210 *
211 */
212 public static final int USAGE_SHARED = 0x0080;
213
214 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700215 * Controls mipmap behavior when using the bitmap creation and update
216 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800217 */
Jason Sams4ef66502010-12-10 16:03:15 -0800218 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700219 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700220 * No mipmaps will be generated and the type generated from the incoming
221 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800222 */
Jason Sams5476b452010-12-08 16:14:36 -0800223 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800224
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700225 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700226 * A full mipmap chain will be created in script memory. The Type of
227 * the Allocation will contain a full mipmap chain. On upload, the full
228 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800229 */
Jason Sams5476b452010-12-08 16:14:36 -0800230 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800231
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700232 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700233 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
234 * not contain mipmaps. On upload, the allocation data will contain a
235 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800236 */
Jason Sams5476b452010-12-08 16:14:36 -0800237 MIPMAP_ON_SYNC_TO_TEXTURE(2);
238
239 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800240 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800241 mID = id;
242 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700243 }
244
Jason Sams48fe5342011-07-08 13:52:30 -0700245
Tim Murray460a0492013-11-19 12:45:54 -0800246 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700247 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700248 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700249 }
Jason Samse07694b2012-04-03 15:36:36 -0700250 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700251 }
252
Jason Sams03d2d002012-03-23 13:51:56 -0700253
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700254 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700255 * Get the {@link android.renderscript.Element} of the {@link
256 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700257 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700258 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700259 *
260 */
261 public Element getElement() {
262 return mType.getElement();
263 }
264
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700265 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700266 * Get the usage flags of the Allocation.
267 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700268 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700269 *
270 */
271 public int getUsage() {
272 return mUsage;
273 }
274
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700275 /**
Miao Wang87e908d2015-03-02 15:15:15 -0800276 * Enable/Disable AutoPadding for Vec3 elements.
Miao Wangd7ecab12015-03-26 18:00:15 -0700277 * By default: Diabled.
Miao Wang87e908d2015-03-02 15:15:15 -0800278 *
Miao Wang179e8b52015-04-15 17:44:32 -0700279 * @param useAutoPadding True: enable AutoPadding; False: disable AutoPadding
Miao Wang87e908d2015-03-02 15:15:15 -0800280 *
281 */
282 public void setAutoPadding(boolean useAutoPadding) {
283 mAutoPadding = useAutoPadding;
284 }
285
286 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700287 * Get the size of the Allocation in bytes.
288 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700289 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700290 *
291 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700292 public int getBytesSize() {
Tim Murray04f0d6e2013-12-17 17:15:25 -0800293 if (mType.mDimYuv != 0) {
294 return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
295 }
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700296 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700297 }
298
Jason Sams452a7662011-07-07 16:05:18 -0700299 private void updateCacheInfo(Type t) {
300 mCurrentDimX = t.getX();
301 mCurrentDimY = t.getY();
302 mCurrentDimZ = t.getZ();
303 mCurrentCount = mCurrentDimX;
304 if (mCurrentDimY > 1) {
305 mCurrentCount *= mCurrentDimY;
306 }
307 if (mCurrentDimZ > 1) {
308 mCurrentCount *= mCurrentDimZ;
309 }
310 }
Jason Samsba862d12011-07-07 15:24:42 -0700311
Tim Murraya3145512012-12-04 17:59:29 -0800312 private void setBitmap(Bitmap b) {
313 mBitmap = b;
314 }
315
Tim Murray460a0492013-11-19 12:45:54 -0800316 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700317 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800318 if ((usage & ~(USAGE_SCRIPT |
319 USAGE_GRAPHICS_TEXTURE |
320 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700321 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800322 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800323 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800324 USAGE_IO_OUTPUT |
325 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800326 throw new RSIllegalArgumentException("Unknown usage specified.");
327 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800328
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700329 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800330 mWriteAllowed = false;
331
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700332 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800333 USAGE_GRAPHICS_TEXTURE |
334 USAGE_SCRIPT)) != 0) {
335 throw new RSIllegalArgumentException("Invalid usage combination.");
336 }
337 }
Jason Sams9bf18922013-04-13 19:48:36 -0700338
Jason Sams5476b452010-12-08 16:14:36 -0800339 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800340 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700341
Jason Sams452a7662011-07-07 16:05:18 -0700342 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700343 // TODO: A3D doesn't have Type info during creation, so we can't
344 // calculate the size ahead of time. We can possibly add a method
345 // to update the size in the future if it seems reasonable.
346 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700347 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700348 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700349 try {
350 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
351 } catch (Exception e) {
352 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
353 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
354 }
355 }
356
357 protected void finalize() throws Throwable {
358 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
359 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700360 }
361
Jason Sams3042d262013-11-25 18:28:33 -0800362 private void validateIsInt64() {
363 if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
364 (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
365 return;
366 }
367 throw new RSIllegalArgumentException(
368 "64 bit integer source does not match allocation type " + mType.mElement.mType);
369 }
370
Jason Samsb97b2512011-01-16 15:04:08 -0800371 private void validateIsInt32() {
372 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
373 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
374 return;
375 }
376 throw new RSIllegalArgumentException(
377 "32 bit integer source does not match allocation type " + mType.mElement.mType);
378 }
379
380 private void validateIsInt16() {
381 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
382 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
383 return;
384 }
385 throw new RSIllegalArgumentException(
386 "16 bit integer source does not match allocation type " + mType.mElement.mType);
387 }
388
389 private void validateIsInt8() {
390 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
391 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
392 return;
393 }
394 throw new RSIllegalArgumentException(
395 "8 bit integer source does not match allocation type " + mType.mElement.mType);
396 }
397
398 private void validateIsFloat32() {
399 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
400 return;
401 }
402 throw new RSIllegalArgumentException(
403 "32 bit float source does not match allocation type " + mType.mElement.mType);
404 }
405
Jason Sams3042d262013-11-25 18:28:33 -0800406 private void validateIsFloat64() {
407 if (mType.mElement.mType == Element.DataType.FLOAT_64) {
408 return;
409 }
410 throw new RSIllegalArgumentException(
411 "64 bit float source does not match allocation type " + mType.mElement.mType);
412 }
413
Jason Samsb97b2512011-01-16 15:04:08 -0800414 private void validateIsObject() {
415 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
416 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
417 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
418 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
419 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
420 (mType.mElement.mType == Element.DataType.RS_MESH) ||
421 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
422 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
423 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
424 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
425 return;
426 }
427 throw new RSIllegalArgumentException(
428 "Object source does not match allocation type " + mType.mElement.mType);
429 }
430
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700431 @Override
432 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800433 super.updateFromNative();
Tim Murray460a0492013-11-19 12:45:54 -0800434 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700435 if(typeID != 0) {
436 mType = new Type(typeID, mRS);
437 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700438 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700439 }
440 }
441
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700442 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700443 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700444 *
445 * @return Type
446 *
447 */
Jason Samsea87e962010-01-12 12:12:28 -0800448 public Type getType() {
449 return mType;
450 }
451
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700452 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700453 * Propagate changes from one usage of the Allocation to the
454 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700455 *
456 */
Jason Sams5476b452010-12-08 16:14:36 -0800457 public void syncAll(int srcLocation) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700458 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
Jason Sams5476b452010-12-08 16:14:36 -0800459 switch (srcLocation) {
Jason Sams5476b452010-12-08 16:14:36 -0800460 case USAGE_GRAPHICS_TEXTURE:
Tim Murray78e64942013-04-09 17:28:56 -0700461 case USAGE_SCRIPT:
462 if ((mUsage & USAGE_SHARED) != 0) {
463 copyFrom(mBitmap);
464 }
465 break;
466 case USAGE_GRAPHICS_CONSTANTS:
Jason Sams5476b452010-12-08 16:14:36 -0800467 case USAGE_GRAPHICS_VERTEX:
468 break;
Tim Murray78e64942013-04-09 17:28:56 -0700469 case USAGE_SHARED:
470 if ((mUsage & USAGE_SHARED) != 0) {
471 copyTo(mBitmap);
472 }
473 break;
Jason Sams5476b452010-12-08 16:14:36 -0800474 default:
475 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
476 }
477 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700478 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700479 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800480 }
481
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700482 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700483 * Send a buffer to the output stream. The contents of the Allocation will
484 * be undefined after this operation. This operation is only valid if {@link
485 * #USAGE_IO_OUTPUT} is set on the Allocation.
486 *
Jason Sams163766c2012-02-15 12:04:24 -0800487 *
Jason Sams163766c2012-02-15 12:04:24 -0800488 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700489 public void ioSend() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700490 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
Jason Sams163766c2012-02-15 12:04:24 -0800491 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
492 throw new RSIllegalArgumentException(
493 "Can only send buffer if IO_OUTPUT usage specified.");
494 }
495 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700496 mRS.nAllocationIoSend(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700497 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800498 }
499
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700500 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700501 * Receive the latest input into the Allocation. This operation
502 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800503 *
Jason Sams163766c2012-02-15 12:04:24 -0800504 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700505 public void ioReceive() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700506 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
Jason Sams163766c2012-02-15 12:04:24 -0800507 if ((mUsage & USAGE_IO_INPUT) == 0) {
508 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700509 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800510 }
511 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700512 mRS.nAllocationIoReceive(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700513 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800514 }
515
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700516 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700517 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700518 *
519 * @param d Source array.
520 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800521 public void copyFrom(BaseObj[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700522 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Sams771bebb2009-12-07 12:40:12 -0800523 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800524 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700525 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800526 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700527 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800528 }
Tim Murray460a0492013-11-19 12:45:54 -0800529
Tim Murray3de3dc72014-07-01 16:56:18 -0700530 if (RenderScript.sPointerSize == 8) {
531 long i[] = new long[d.length * 4];
532 for (int ct=0; ct < d.length; ct++) {
533 i[ct * 4] = d[ct].getID(mRS);
534 }
535 copy1DRangeFromUnchecked(0, mCurrentCount, i);
536 } else {
537 int i[] = new int[d.length];
538 for (int ct=0; ct < d.length; ct++) {
539 i[ct] = (int)d[ct].getID(mRS);
540 }
541 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800542 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700543 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700544 }
545
Jason Samsfb9f82c2011-01-12 14:53:25 -0800546 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800547 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800548 if (bc == null) {
549 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
550 }
Jason Sams252c0782011-01-11 17:42:52 -0800551 switch (bc) {
552 case ALPHA_8:
553 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
554 throw new RSIllegalArgumentException("Allocation kind is " +
555 mType.getElement().mKind + ", type " +
556 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700557 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800558 " bytes, passed bitmap was " + bc);
559 }
560 break;
561 case ARGB_8888:
562 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700563 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800564 throw new RSIllegalArgumentException("Allocation kind is " +
565 mType.getElement().mKind + ", type " +
566 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700567 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800568 " bytes, passed bitmap was " + bc);
569 }
570 break;
571 case RGB_565:
572 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700573 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800574 throw new RSIllegalArgumentException("Allocation kind is " +
575 mType.getElement().mKind + ", type " +
576 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700577 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800578 " bytes, passed bitmap was " + bc);
579 }
580 break;
581 case ARGB_4444:
582 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700583 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800584 throw new RSIllegalArgumentException("Allocation kind is " +
585 mType.getElement().mKind + ", type " +
586 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700587 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800588 " bytes, passed bitmap was " + bc);
589 }
590 break;
591
592 }
Jason Sams4ef66502010-12-10 16:03:15 -0800593 }
594
Jason Samsfb9f82c2011-01-12 14:53:25 -0800595 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700596 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800597 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
598 }
599 }
600
Jason Sams3042d262013-11-25 18:28:33 -0800601 private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
602 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
603 mRS.validate();
604 if (mCurrentDimZ > 0) {
605 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
606 } else if (mCurrentDimY > 0) {
607 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
608 } else {
609 copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
610 }
611 Trace.traceEnd(RenderScript.TRACE_TAG);
612 }
613
614 /**
615 * Copy into this Allocation from an array. This method does not guarantee
616 * that the Allocation is compatible with the input buffer; it copies memory
617 * without reinterpretation.
618 *
619 * @param array The source data array
620 */
621 public void copyFromUnchecked(Object array) {
622 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
623 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
624 java.lang.reflect.Array.getLength(array));
625 Trace.traceEnd(RenderScript.TRACE_TAG);
626 }
627
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700628 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700629 * Copy into this Allocation from an array. This method does not guarantee
630 * that the Allocation is compatible with the input buffer; it copies memory
631 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800632 *
633 * @param d the source data array
634 */
635 public void copyFromUnchecked(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800636 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800637 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700638
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700639 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700640 * Copy into this Allocation from an array. This method does not guarantee
641 * that the Allocation is compatible with the input buffer; it copies memory
642 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800643 *
644 * @param d the source data array
645 */
646 public void copyFromUnchecked(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800647 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800648 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700649
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700650 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700651 * Copy into this Allocation from an array. This method does not guarantee
652 * that the Allocation is compatible with the input buffer; it copies memory
653 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800654 *
655 * @param d the source data array
656 */
657 public void copyFromUnchecked(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800658 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800659 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700660
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700661 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700662 * Copy into this Allocation from an array. This method does not guarantee
663 * that the Allocation is compatible with the input buffer; it copies memory
664 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800665 *
666 * @param d the source data array
667 */
668 public void copyFromUnchecked(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800669 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800670 }
671
Tim Murray6d7a53c2013-05-23 16:59:23 -0700672
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700673 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700674 * Copy into this Allocation from an array. This variant is type checked
675 * and will generate exceptions if the Allocation's {@link
Jason Sams3042d262013-11-25 18:28:33 -0800676 * android.renderscript.Element} does not match the array's
677 * primitive type.
678 *
Ying Wang16229812013-11-26 15:45:12 -0800679 * @param array The source data array
Jason Sams3042d262013-11-25 18:28:33 -0800680 */
681 public void copyFrom(Object array) {
682 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
683 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
684 java.lang.reflect.Array.getLength(array));
685 Trace.traceEnd(RenderScript.TRACE_TAG);
686 }
687
688 /**
689 * Copy into this Allocation from an array. This variant is type checked
690 * and will generate exceptions if the Allocation's {@link
Tim Murrayc11e25c2013-04-09 11:01:01 -0700691 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800692 *
693 * @param d the source data array
694 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800695 public void copyFrom(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800696 validateIsInt32();
697 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800698 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800699
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700700 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700701 * Copy into this Allocation from an array. This variant is type checked
702 * and will generate exceptions if the Allocation's {@link
703 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800704 *
705 * @param d the source data array
706 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800707 public void copyFrom(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800708 validateIsInt16();
709 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800710 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800711
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700712 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700713 * Copy into this Allocation from an array. This variant is type checked
714 * and will generate exceptions if the Allocation's {@link
715 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800716 *
717 * @param d the source data array
718 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800719 public void copyFrom(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800720 validateIsInt8();
721 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800722 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800723
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700724 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700725 * Copy into this Allocation from an array. This variant is type checked
726 * and will generate exceptions if the Allocation's {@link
727 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800728 *
729 * @param d the source data array
730 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800731 public void copyFrom(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800732 validateIsFloat32();
733 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800734 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800735
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700736 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700737 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
738 * height, width, and format of the bitmap must match the existing
739 * allocation.
740 *
741 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
742 * android.graphics.Bitmap} used to create the Allocation with {@link
743 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
744 * this will synchronize the Allocation with the latest data from the {@link
745 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800746 *
747 * @param b the source bitmap
748 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800749 public void copyFrom(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700750 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsfb9f82c2011-01-12 14:53:25 -0800751 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -0800752 if (b.getConfig() == null) {
753 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
754 Canvas c = new Canvas(newBitmap);
755 c.drawBitmap(b, 0, 0, null);
756 copyFrom(newBitmap);
757 return;
758 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800759 validateBitmapSize(b);
760 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700761 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700762 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700763 }
764
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700765 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700766 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700767 * must be identical.
768 *
769 * @param a the source allocation
770 */
771 public void copyFrom(Allocation a) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700772 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Tim Murrayf671fb02012-10-03 13:50:05 -0700773 mRS.validate();
774 if (!mType.equals(a.getType())) {
775 throw new RSIllegalArgumentException("Types of allocations must match.");
776 }
777 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700778 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700779 }
780
Tim Murrayf671fb02012-10-03 13:50:05 -0700781 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700782 * This is only intended to be used by auto-generated code reflected from
783 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800784 *
785 * @param xoff
786 * @param fp
787 */
Jason Sams21b41032011-01-16 15:05:41 -0800788 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800789 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700790 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700791 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700792 int data_length = fp.getPos();
Jason Samsa70f4162010-03-26 15:33:42 -0700793
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700794 int count = data_length / eSize;
795 if ((eSize * count) != data_length) {
796 throw new RSIllegalArgumentException("Field packer length " + data_length +
Jason Samsa70f4162010-03-26 15:33:42 -0700797 " not divisible by element size " + eSize + ".");
798 }
Jason Samsba862d12011-07-07 15:24:42 -0700799 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700800 }
801
Miao Wang45cec0a2015-03-04 16:40:21 -0800802
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700803 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700804 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -0800805 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800806 *
807 * @param xoff
808 * @param component_number
809 * @param fp
810 */
Jason Sams21b41032011-01-16 15:05:41 -0800811 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800812 setFromFieldPacker(xoff, 0, 0, component_number, fp);
813 }
814
815 /**
Miao Wangc8e237e2015-02-20 18:36:32 -0800816 * This is only intended to be used by auto-generated code reflected from
Miao Wang258db502015-03-03 14:05:36 -0800817 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -0800818 *
819 * @param xoff
820 * @param yoff
Miao Wangc8e237e2015-02-20 18:36:32 -0800821 * @param zoff
822 * @param component_number
823 * @param fp
824 */
825 public void setFromFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800826 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700827 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800828 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700829 }
830 if(xoff < 0) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800831 throw new RSIllegalArgumentException("Offset x must be >= 0.");
832 }
833 if(yoff < 0) {
834 throw new RSIllegalArgumentException("Offset y must be >= 0.");
835 }
836 if(zoff < 0) {
837 throw new RSIllegalArgumentException("Offset z must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700838 }
839
840 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700841 int data_length = fp.getPos();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700842 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800843 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700844
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700845 if (data_length != eSize) {
846 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700847 " does not match component size " + eSize + ".");
848 }
849
Miao Wangc8e237e2015-02-20 18:36:32 -0800850 mRS.nAllocationElementData(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
851 component_number, data, data_length);
Jason Samsa70f4162010-03-26 15:33:42 -0700852 }
853
Miao Wang87e908d2015-03-02 15:15:15 -0800854 private void data1DChecks(int off, int count, int len, int dataSize, boolean usePadding) {
Jason Sams771bebb2009-12-07 12:40:12 -0800855 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700856 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800857 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700858 }
859 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800860 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700861 }
Jason Samsba862d12011-07-07 15:24:42 -0700862 if((off + count) > mCurrentCount) {
863 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700864 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700865 }
Miao Wang87e908d2015-03-02 15:15:15 -0800866 if(usePadding) {
867 if(len < dataSize / 4 * 3) {
868 throw new RSIllegalArgumentException("Array too small for allocation type.");
869 }
870 } else {
871 if(len < dataSize) {
872 throw new RSIllegalArgumentException("Array too small for allocation type.");
873 }
Jason Sams768bc022009-09-21 19:41:04 -0700874 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700875 }
876
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700877 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700878 * Generate a mipmap chain. This is only valid if the Type of the Allocation
879 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800880 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700881 * <p>This function will generate a complete set of mipmaps from the top
882 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800883 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700884 * <p>If the Allocation is also using other memory spaces, a call to {@link
885 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800886 */
887 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700888 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800889 }
890
Jason Sams3042d262013-11-25 18:28:33 -0800891 private void copy1DRangeFromUnchecked(int off, int count, Object array,
892 Element.DataType dt, int arrayLen) {
893 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
894 final int dataSize = mType.mElement.getBytesSize() * count;
Miao Wang87e908d2015-03-02 15:15:15 -0800895 // AutoPadding for Vec3 Element
896 boolean usePadding = false;
897 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
898 usePadding = true;
899 }
900 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
901 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
902 mType.mElement.mType.mSize, usePadding);
Jason Sams3042d262013-11-25 18:28:33 -0800903 Trace.traceEnd(RenderScript.TRACE_TAG);
904 }
905
906 /**
907 * Copy an array into part of this Allocation. This method does not
908 * guarantee that the Allocation is compatible with the input buffer.
909 *
910 * @param off The offset of the first element to be copied.
911 * @param count The number of elements to be copied.
912 * @param array The source data array
913 */
914 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
915 copy1DRangeFromUnchecked(off, count, array,
916 validateObjectIsPrimitiveArray(array, false),
917 java.lang.reflect.Array.getLength(array));
918 }
919
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700920 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700921 * Copy an array into part of this Allocation. This method does not
922 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800923 *
924 * @param off The offset of the first element to be copied.
925 * @param count The number of elements to be copied.
926 * @param d the source data array
927 */
928 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800929 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700930 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700931
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700932 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700933 * Copy an array into part of this Allocation. This method does not
934 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800935 *
936 * @param off The offset of the first element to be copied.
937 * @param count The number of elements to be copied.
938 * @param d the source data array
939 */
940 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800941 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700942 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700943
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700944 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700945 * Copy an array into part of this Allocation. This method does not
946 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800947 *
948 * @param off The offset of the first element to be copied.
949 * @param count The number of elements to be copied.
950 * @param d the source data array
951 */
952 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800953 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700954 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700955
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700956 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700957 * Copy an array into part of this Allocation. This method does not
958 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800959 *
960 * @param off The offset of the first element to be copied.
961 * @param count The number of elements to be copied.
962 * @param d the source data array
963 */
964 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800965 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
966 }
967
968
969 /**
970 * Copy an array into part of this Allocation. This variant is type checked
971 * and will generate exceptions if the Allocation type does not
972 * match the component type of the array passed in.
973 *
974 * @param off The offset of the first element to be copied.
975 * @param count The number of elements to be copied.
976 * @param array The source data array.
977 */
978 public void copy1DRangeFrom(int off, int count, Object array) {
979 copy1DRangeFromUnchecked(off, count, array,
980 validateObjectIsPrimitiveArray(array, true),
981 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -0700982 }
983
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700984 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700985 * Copy an array into part of this Allocation. This variant is type checked
986 * and will generate exceptions if the Allocation type is not a 32 bit
987 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800988 *
989 * @param off The offset of the first element to be copied.
990 * @param count The number of elements to be copied.
991 * @param d the source data array
992 */
Jason Samsb97b2512011-01-16 15:04:08 -0800993 public void copy1DRangeFrom(int off, int count, int[] d) {
994 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -0800995 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800996 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800997
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700998 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700999 * Copy an array into part of this Allocation. This variant is type checked
1000 * and will generate exceptions if the Allocation type is not a 16 bit
1001 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001002 *
1003 * @param off The offset of the first element to be copied.
1004 * @param count The number of elements to be copied.
1005 * @param d the source data array
1006 */
Jason Samsb97b2512011-01-16 15:04:08 -08001007 public void copy1DRangeFrom(int off, int count, short[] d) {
1008 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001009 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001010 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001011
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001012 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001013 * Copy an array into part of this Allocation. This variant is type checked
1014 * and will generate exceptions if the Allocation type is not an 8 bit
1015 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001016 *
1017 * @param off The offset of the first element to be copied.
1018 * @param count The number of elements to be copied.
1019 * @param d the source data array
1020 */
Jason Samsb97b2512011-01-16 15:04:08 -08001021 public void copy1DRangeFrom(int off, int count, byte[] d) {
1022 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001023 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001024 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001025
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001026 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001027 * Copy an array into part of this Allocation. This variant is type checked
1028 * and will generate exceptions if the Allocation type is not a 32 bit float
1029 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001030 *
1031 * @param off The offset of the first element to be copied.
1032 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001033 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001034 */
Jason Samsb97b2512011-01-16 15:04:08 -08001035 public void copy1DRangeFrom(int off, int count, float[] d) {
1036 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001037 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001038 }
Jason Sams3042d262013-11-25 18:28:33 -08001039
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001040 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001041 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001042 *
1043 * @param off The offset of the first element to be copied.
1044 * @param count The number of elements to be copied.
1045 * @param data the source data allocation.
1046 * @param dataOff off The offset of the first element in data to
1047 * be copied.
1048 */
1049 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001050 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -07001051 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001052 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001053 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001054 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001055 }
1056
Jason Samsfb9f82c2011-01-12 14:53:25 -08001057 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001058 if (mAdaptedAllocation != null) {
1059
1060 } else {
1061
1062 if (xoff < 0 || yoff < 0) {
1063 throw new RSIllegalArgumentException("Offset cannot be negative.");
1064 }
1065 if (h < 0 || w < 0) {
1066 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1067 }
1068 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1069 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1070 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001071 }
1072 }
Jason Sams768bc022009-09-21 19:41:04 -07001073
Jason Sams3042d262013-11-25 18:28:33 -08001074 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1075 Element.DataType dt, int arrayLen) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001076 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001077 mRS.validate();
1078 validate2DRange(xoff, yoff, w, h);
Miao Wang87e908d2015-03-02 15:15:15 -08001079 final int dataSize = mType.mElement.getBytesSize() * w * h;
1080 // AutoPadding for Vec3 Element
1081 boolean usePadding = false;
1082 int sizeBytes = arrayLen * dt.mSize;
1083 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1084 if (dataSize / 4 * 3 > sizeBytes) {
1085 throw new RSIllegalArgumentException("Array too small for allocation type.");
1086 }
1087 usePadding = true;
1088 sizeBytes = dataSize;
1089 } else {
1090 if (dataSize > sizeBytes) {
1091 throw new RSIllegalArgumentException("Array too small for allocation type.");
1092 }
1093 }
Jason Sams3042d262013-11-25 18:28:33 -08001094 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
Miao Wang87e908d2015-03-02 15:15:15 -08001095 array, sizeBytes, dt,
1096 mType.mElement.mType.mSize, usePadding);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001097 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001098 }
1099
Jason Sams3042d262013-11-25 18:28:33 -08001100 /**
1101 * Copy from an array into a rectangular region in this Allocation. The
1102 * array is assumed to be tightly packed.
1103 *
1104 * @param xoff X offset of the region to update in this Allocation
1105 * @param yoff Y offset of the region to update in this Allocation
1106 * @param w Width of the region to update
1107 * @param h Height of the region to update
Ying Wang16229812013-11-26 15:45:12 -08001108 * @param array Data to be placed into the Allocation
Jason Sams3042d262013-11-25 18:28:33 -08001109 */
1110 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
1111 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1112 copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
1113 validateObjectIsPrimitiveArray(array, true),
1114 java.lang.reflect.Array.getLength(array));
Tim Murray6d7a53c2013-05-23 16:59:23 -07001115 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001116 }
1117
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001118 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001119 * Copy from an array into a rectangular region in this Allocation. The
1120 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -08001121 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001122 * @param xoff X offset of the region to update in this Allocation
1123 * @param yoff Y offset of the region to update in this Allocation
1124 * @param w Width of the region to update
1125 * @param h Height of the region to update
1126 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001127 */
1128 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001129 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001130 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1131 Element.DataType.SIGNED_8, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001132 }
1133
Tim Murrayc11e25c2013-04-09 11:01:01 -07001134 /**
1135 * Copy from an array into a rectangular region in this Allocation. The
1136 * array is assumed to be tightly packed.
1137 *
1138 * @param xoff X offset of the region to update in this Allocation
1139 * @param yoff Y offset of the region to update in this Allocation
1140 * @param w Width of the region to update
1141 * @param h Height of the region to update
1142 * @param data to be placed into the Allocation
1143 */
Jason Samsf7086092011-01-12 13:28:37 -08001144 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001145 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001146 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1147 Element.DataType.SIGNED_16, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001148 }
1149
Tim Murrayc11e25c2013-04-09 11:01:01 -07001150 /**
1151 * Copy from an array into a rectangular region in this Allocation. The
1152 * array is assumed to be tightly packed.
1153 *
1154 * @param xoff X offset of the region to update in this Allocation
1155 * @param yoff Y offset of the region to update in this Allocation
1156 * @param w Width of the region to update
1157 * @param h Height of the region to update
1158 * @param data to be placed into the Allocation
1159 */
Jason Samsf7086092011-01-12 13:28:37 -08001160 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001161 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001162 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1163 Element.DataType.SIGNED_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001164 }
1165
Tim Murrayc11e25c2013-04-09 11:01:01 -07001166 /**
1167 * Copy from an array into a rectangular region in this Allocation. The
1168 * array is assumed to be tightly packed.
1169 *
1170 * @param xoff X offset of the region to update in this Allocation
1171 * @param yoff Y offset of the region to update in this Allocation
1172 * @param w Width of the region to update
1173 * @param h Height of the region to update
1174 * @param data to be placed into the Allocation
1175 */
Jason Samsf7086092011-01-12 13:28:37 -08001176 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001177 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001178 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1179 Element.DataType.FLOAT_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001180 }
1181
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001182 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001183 * Copy a rectangular region from an Allocation into a rectangular region in
1184 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001185 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001186 * @param xoff X offset of the region in this Allocation
1187 * @param yoff Y offset of the region in this Allocation
1188 * @param w Width of the region to update.
1189 * @param h Height of the region to update.
1190 * @param data source Allocation.
1191 * @param dataXoff X offset in source Allocation
1192 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001193 */
1194 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1195 Allocation data, int dataXoff, int dataYoff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001196 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001197 mRS.validate();
1198 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -07001199 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -07001200 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001201 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -07001202 data.mSelectedLOD, data.mSelectedFace.mID);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001203 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001204 }
1205
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001206 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001207 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1208 * and width of the update will use the height and width of the {@link
1209 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001210 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001211 * @param xoff X offset of the region to update in this Allocation
1212 * @param yoff Y offset of the region to update in this Allocation
1213 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001214 */
1215 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001216 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Jason Samsfa445b92011-01-07 17:00:07 -08001217 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001218 if (data.getConfig() == null) {
1219 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1220 Canvas c = new Canvas(newBitmap);
1221 c.drawBitmap(data, 0, 0, null);
1222 copy2DRangeFrom(xoff, yoff, newBitmap);
Jason Samsb05d6892013-04-09 15:59:24 -07001223 return;
Tim Murrayabd5db92013-02-28 11:45:22 -08001224 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001225 validateBitmapFormat(data);
1226 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -07001227 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001228 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001229 }
1230
Jason Samsb05d6892013-04-09 15:59:24 -07001231 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1232 if (mAdaptedAllocation != null) {
1233
1234 } else {
1235
1236 if (xoff < 0 || yoff < 0 || zoff < 0) {
1237 throw new RSIllegalArgumentException("Offset cannot be negative.");
1238 }
1239 if (h < 0 || w < 0 || d < 0) {
1240 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1241 }
1242 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1243 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1244 }
1245 }
1246 }
1247
1248 /**
Miao Wang258db502015-03-03 14:05:36 -08001249 * Copy a rectangular region from the array into the allocation.
1250 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001251 *
Miao Wang258db502015-03-03 14:05:36 -08001252 * The data type of the array is not required to be the same as
1253 * the element data type.
Jason Samsb05d6892013-04-09 15:59:24 -07001254 */
Jason Sams3042d262013-11-25 18:28:33 -08001255 private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1256 Object array, Element.DataType dt, int arrayLen) {
1257 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
Jason Samsb05d6892013-04-09 15:59:24 -07001258 mRS.validate();
1259 validate3DRange(xoff, yoff, zoff, w, h, d);
Miao Wang87e908d2015-03-02 15:15:15 -08001260 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1261 // AutoPadding for Vec3 Element
1262 boolean usePadding = false;
1263 int sizeBytes = arrayLen * dt.mSize;
1264 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1265 if (dataSize / 4 * 3 > sizeBytes) {
1266 throw new RSIllegalArgumentException("Array too small for allocation type.");
1267 }
1268 usePadding = true;
1269 sizeBytes = dataSize;
1270 } else {
1271 if (dataSize > sizeBytes) {
1272 throw new RSIllegalArgumentException("Array too small for allocation type.");
1273 }
1274 }
Jason Sams3042d262013-11-25 18:28:33 -08001275 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
Miao Wang87e908d2015-03-02 15:15:15 -08001276 array, sizeBytes, dt,
1277 mType.mElement.mType.mSize, usePadding);
Jason Sams3042d262013-11-25 18:28:33 -08001278 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb05d6892013-04-09 15:59:24 -07001279 }
1280
1281 /**
Jason Samsb05d6892013-04-09 15:59:24 -07001282 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001283 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001284 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001285 * @param xoff X offset of the region to update in this Allocation
1286 * @param yoff Y offset of the region to update in this Allocation
1287 * @param zoff Z offset of the region to update in this Allocation
1288 * @param w Width of the region to update
1289 * @param h Height of the region to update
1290 * @param d Depth of the region to update
Miao Wang87e908d2015-03-02 15:15:15 -08001291 * @param array to be placed into the allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001292 */
Jason Sams3042d262013-11-25 18:28:33 -08001293 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1294 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
1295 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
1296 validateObjectIsPrimitiveArray(array, true),
1297 java.lang.reflect.Array.getLength(array));
1298 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb05d6892013-04-09 15:59:24 -07001299 }
1300
1301 /**
Jason Samsb05d6892013-04-09 15:59:24 -07001302 * Copy a rectangular region into the allocation from another
1303 * allocation.
1304 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001305 * @param xoff X offset of the region to update in this Allocation
1306 * @param yoff Y offset of the region to update in this Allocation
1307 * @param zoff Z offset of the region to update in this Allocation
1308 * @param w Width of the region to update.
1309 * @param h Height of the region to update.
1310 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001311 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001312 * @param dataXoff X offset of the region in the source Allocation
1313 * @param dataYoff Y offset of the region in the source Allocation
1314 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001315 */
1316 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1317 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1318 mRS.validate();
1319 validate3DRange(xoff, yoff, zoff, w, h, d);
1320 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1321 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1322 data.mSelectedLOD);
1323 }
1324
Jason Samsfa445b92011-01-07 17:00:07 -08001325
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001326 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001327 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1328 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001329 *
1330 * @param b The bitmap to be set from the Allocation.
1331 */
Jason Samsfa445b92011-01-07 17:00:07 -08001332 public void copyTo(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001333 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsfb9f82c2011-01-12 14:53:25 -08001334 mRS.validate();
1335 validateBitmapFormat(b);
1336 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -07001337 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001338 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001339 }
1340
Jason Sams3042d262013-11-25 18:28:33 -08001341 private void copyTo(Object array, Element.DataType dt, int arrayLen) {
1342 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1343 mRS.validate();
Miao Wang87e908d2015-03-02 15:15:15 -08001344 boolean usePadding = false;
1345 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1346 usePadding = true;
1347 }
Miao Wangd9b63282015-04-03 09:15:39 -07001348 if (usePadding) {
1349 if (dt.mSize * arrayLen < mSize / 4 * 3) {
1350 throw new RSIllegalArgumentException(
1351 "Size of output array cannot be smaller than size of allocation.");
1352 }
1353 } else {
1354 if (dt.mSize * arrayLen < mSize) {
1355 throw new RSIllegalArgumentException(
1356 "Size of output array cannot be smaller than size of allocation.");
1357 }
1358 }
Miao Wang87e908d2015-03-02 15:15:15 -08001359 mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
Jason Sams3042d262013-11-25 18:28:33 -08001360 Trace.traceEnd(RenderScript.TRACE_TAG);
1361 }
1362
1363 /**
1364 * Copy from the Allocation into an array. The array must be at
1365 * least as large as the Allocation. The
1366 * {@link android.renderscript.Element} must match the component
1367 * type of the array passed in.
1368 *
1369 * @param array The array to be set from the Allocation.
1370 */
1371 public void copyTo(Object array) {
1372 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1373 java.lang.reflect.Array.getLength(array));
1374 }
1375
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001376 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001377 * Copy from the Allocation into a byte array. The array must be at least
1378 * as large as the Allocation. The allocation must be of an 8 bit integer
1379 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001380 *
1381 * @param d The array to be set from the Allocation.
1382 */
Jason Samsfa445b92011-01-07 17:00:07 -08001383 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001384 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001385 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001386 }
1387
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001388 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001389 * Copy from the Allocation into a short array. The array must be at least
1390 * as large as the Allocation. The allocation must be of an 16 bit integer
1391 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001392 *
1393 * @param d The array to be set from the Allocation.
1394 */
Jason Samsfa445b92011-01-07 17:00:07 -08001395 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001396 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001397 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001398 }
1399
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001400 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001401 * Copy from the Allocation into a int array. The array must be at least as
1402 * large as the Allocation. The allocation must be of an 32 bit integer
1403 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001404 *
1405 * @param d The array to be set from the Allocation.
1406 */
Jason Samsfa445b92011-01-07 17:00:07 -08001407 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001408 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001409 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001410 }
1411
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001412 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001413 * Copy from the Allocation into a float array. The array must be at least
1414 * as large as the Allocation. The allocation must be of an 32 bit float
1415 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001416 *
1417 * @param d The array to be set from the Allocation.
1418 */
Jason Samsfa445b92011-01-07 17:00:07 -08001419 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001420 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001421 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001422 }
1423
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001424 /**
Miao Wang3c613272015-05-11 11:41:55 -07001425 * @hide
1426 *
Miao Wang45cec0a2015-03-04 16:40:21 -08001427 * This is only intended to be used by auto-generated code reflected from
1428 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -08001429 *
1430 * @param xoff
1431 * @param yoff
1432 * @param zoff
1433 * @param component_number
Miao Wang258db502015-03-03 14:05:36 -08001434 * @param fp
Miao Wangc8e237e2015-02-20 18:36:32 -08001435 */
Miao Wang45cec0a2015-03-04 16:40:21 -08001436 public void copyToFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001437 mRS.validate();
1438 if (component_number >= mType.mElement.mElements.length) {
1439 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
1440 }
1441 if(xoff < 0) {
1442 throw new RSIllegalArgumentException("Offset x must be >= 0.");
1443 }
1444 if(yoff < 0) {
1445 throw new RSIllegalArgumentException("Offset y must be >= 0.");
1446 }
1447 if(zoff < 0) {
1448 throw new RSIllegalArgumentException("Offset z must be >= 0.");
1449 }
1450
Miao Wang45cec0a2015-03-04 16:40:21 -08001451 final byte[] data = fp.getData();
1452 int data_length = fp.getPos();
Miao Wangc8e237e2015-02-20 18:36:32 -08001453 int eSize = mType.mElement.mElements[component_number].getBytesSize();
1454 eSize *= mType.mElement.mArraySizes[component_number];
1455
Miao Wang45cec0a2015-03-04 16:40:21 -08001456 if (data_length != eSize) {
1457 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
1458 " does not match component size " + eSize + ".");
Miao Wangc8e237e2015-02-20 18:36:32 -08001459 }
1460
1461 mRS.nAllocationElementRead(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Miao Wang45cec0a2015-03-04 16:40:21 -08001462 component_number, data, data_length);
Miao Wangc8e237e2015-02-20 18:36:32 -08001463 }
1464 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001465 * Resize a 1D allocation. The contents of the allocation are preserved.
1466 * If new elements are allocated objects are created with null contents and
1467 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001468 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001469 * <p>If the new region is smaller the references of any objects outside the
1470 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001471 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001472 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001473 *
1474 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001475 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001476 * @deprecated RenderScript objects should be immutable once created. The
Tim Murraycd38b762014-08-13 13:20:25 -07001477 * replacement is to create a new allocation and copy the contents. This
1478 * function will throw an exception if API 21 or higher is used.
Jason Samsf7086092011-01-12 13:28:37 -08001479 */
Jason Sams31a7e422010-10-26 13:09:17 -07001480 public synchronized void resize(int dimX) {
Tim Murraycd38b762014-08-13 13:20:25 -07001481 if (mRS.getApplicationContext().getApplicationInfo().targetSdkVersion >= 21) {
1482 throw new RSRuntimeException("Resize is not allowed in API 21+.");
1483 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001484 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001485 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001486 }
Jason Samse07694b2012-04-03 15:36:36 -07001487 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001488 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001489
Tim Murray460a0492013-11-19 12:45:54 -08001490 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001491 mType = new Type(typeID, mRS);
1492 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001493 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001494 }
1495
Miao Wangc8e237e2015-02-20 18:36:32 -08001496 private void copy1DRangeToUnchecked(int off, int count, Object array,
1497 Element.DataType dt, int arrayLen) {
1498 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked");
1499 final int dataSize = mType.mElement.getBytesSize() * count;
Miao Wang87e908d2015-03-02 15:15:15 -08001500 // AutoPadding for Vec3 Element
1501 boolean usePadding = false;
1502 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1503 usePadding = true;
1504 }
1505 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
1506 mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
1507 mType.mElement.mType.mSize, usePadding);
Miao Wangc8e237e2015-02-20 18:36:32 -08001508 Trace.traceEnd(RenderScript.TRACE_TAG);
1509 }
1510
1511 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001512 * Copy part of this Allocation into an array. This method does not
1513 * guarantee that the Allocation is compatible with the input buffer.
1514 *
1515 * @param off The offset of the first element to be copied.
1516 * @param count The number of elements to be copied.
1517 * @param array The dest data array
1518 */
1519 public void copy1DRangeToUnchecked(int off, int count, Object array) {
1520 copy1DRangeToUnchecked(off, count, array,
1521 validateObjectIsPrimitiveArray(array, false),
1522 java.lang.reflect.Array.getLength(array));
1523 }
1524
1525 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001526 * Copy part of this Allocation into an array. This method does not
1527 * guarantee that the Allocation is compatible with the input buffer.
1528 *
1529 * @param off The offset of the first element to be copied.
1530 * @param count The number of elements to be copied.
1531 * @param d the source data array
1532 */
1533 public void copy1DRangeToUnchecked(int off, int count, int[] d) {
1534 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
1535 }
1536
1537 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001538 * Copy part of this Allocation into an array. This method does not
1539 * guarantee that the Allocation is compatible with the input buffer.
1540 *
1541 * @param off The offset of the first element to be copied.
1542 * @param count The number of elements to be copied.
1543 * @param d the source data array
1544 */
1545 public void copy1DRangeToUnchecked(int off, int count, short[] d) {
1546 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
1547 }
1548
1549 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001550 * Copy part of this Allocation into an array. This method does not
1551 * guarantee that the Allocation is compatible with the input buffer.
1552 *
1553 * @param off The offset of the first element to be copied.
1554 * @param count The number of elements to be copied.
1555 * @param d the source data array
1556 */
1557 public void copy1DRangeToUnchecked(int off, int count, byte[] d) {
1558 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
1559 }
1560
1561 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001562 * Copy part of this Allocation into an array. This method does not
1563 * guarantee that the Allocation is compatible with the input buffer.
1564 *
1565 * @param off The offset of the first element to be copied.
1566 * @param count The number of elements to be copied.
1567 * @param d the source data array
1568 */
1569 public void copy1DRangeToUnchecked(int off, int count, float[] d) {
1570 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
1571 }
1572
1573
1574 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001575 * Copy part of this Allocation into an array. This method does not
1576 * and will generate exceptions if the Allocation type does not
1577 * match the component type of the array passed in.
1578 *
1579 * @param off The offset of the first element to be copied.
1580 * @param count The number of elements to be copied.
1581 * @param array The source data array.
1582 */
1583 public void copy1DRangeTo(int off, int count, Object array) {
1584 copy1DRangeToUnchecked(off, count, array,
1585 validateObjectIsPrimitiveArray(array, true),
1586 java.lang.reflect.Array.getLength(array));
1587 }
1588
1589 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001590 * Copy part of this Allocation into an array. This method does not
1591 * and will generate exceptions if the Allocation type is not a 32 bit
1592 * integer type.
1593 *
1594 * @param off The offset of the first element to be copied.
1595 * @param count The number of elements to be copied.
1596 * @param d the source data array
1597 */
1598 public void copy1DRangeTo(int off, int count, int[] d) {
1599 validateIsInt32();
1600 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
1601 }
1602
1603 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001604 * Copy part of this Allocation into an array. This method does not
1605 * and will generate exceptions if the Allocation type is not a 16 bit
1606 * integer type.
1607 *
1608 * @param off The offset of the first element to be copied.
1609 * @param count The number of elements to be copied.
1610 * @param d the source data array
1611 */
1612 public void copy1DRangeTo(int off, int count, short[] d) {
1613 validateIsInt16();
1614 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
1615 }
1616
1617 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001618 * Copy part of this Allocation into an array. This method does not
1619 * and will generate exceptions if the Allocation type is not an 8 bit
1620 * integer type.
1621 *
1622 * @param off The offset of the first element to be copied.
1623 * @param count The number of elements to be copied.
1624 * @param d the source data array
1625 */
1626 public void copy1DRangeTo(int off, int count, byte[] d) {
1627 validateIsInt8();
1628 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
1629 }
1630
1631 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001632 * Copy part of this Allocation into an array. This method does not
1633 * and will generate exceptions if the Allocation type is not a 32 bit float
1634 * type.
1635 *
1636 * @param off The offset of the first element to be copied.
1637 * @param count The number of elements to be copied.
1638 * @param d the source data array.
1639 */
1640 public void copy1DRangeTo(int off, int count, float[] d) {
1641 validateIsFloat32();
1642 copy1DRangeToUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
1643 }
1644
1645
1646 void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
1647 Element.DataType dt, int arrayLen) {
1648 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked");
1649 mRS.validate();
1650 validate2DRange(xoff, yoff, w, h);
Miao Wang87e908d2015-03-02 15:15:15 -08001651 final int dataSize = mType.mElement.getBytesSize() * w * h;
1652 // AutoPadding for Vec3 Element
1653 boolean usePadding = false;
1654 int sizeBytes = arrayLen * dt.mSize;
1655 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1656 if (dataSize / 4 * 3 > sizeBytes) {
1657 throw new RSIllegalArgumentException("Array too small for allocation type.");
1658 }
1659 usePadding = true;
1660 sizeBytes = dataSize;
1661 } else {
1662 if (dataSize > sizeBytes) {
1663 throw new RSIllegalArgumentException("Array too small for allocation type.");
1664 }
1665 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001666 mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
Miao Wang87e908d2015-03-02 15:15:15 -08001667 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
Miao Wangc8e237e2015-02-20 18:36:32 -08001668 Trace.traceEnd(RenderScript.TRACE_TAG);
1669 }
1670
1671 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001672 * Copy from a rectangular region in this Allocation into an array.
1673 *
1674 * @param xoff X offset of the region to copy in this Allocation
1675 * @param yoff Y offset of the region to copy in this Allocation
1676 * @param w Width of the region to copy
1677 * @param h Height of the region to copy
1678 * @param array Dest Array to be copied into
1679 */
1680 public void copy2DRangeTo(int xoff, int yoff, int w, int h, Object array) {
1681 copy2DRangeToUnchecked(xoff, yoff, w, h, array,
1682 validateObjectIsPrimitiveArray(array, true),
1683 java.lang.reflect.Array.getLength(array));
1684 }
1685
1686 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001687 * Copy from a rectangular region in this Allocation into an array.
1688 *
1689 * @param xoff X offset of the region to copy in this Allocation
1690 * @param yoff Y offset of the region to copy in this Allocation
1691 * @param w Width of the region to copy
1692 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001693 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001694 */
1695 public void copy2DRangeTo(int xoff, int yoff, int w, int h, byte[] data) {
1696 validateIsInt8();
1697 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1698 Element.DataType.SIGNED_8, data.length);
1699 }
1700
1701 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001702 * Copy from a rectangular region in this Allocation into an array.
1703 *
1704 * @param xoff X offset of the region to copy in this Allocation
1705 * @param yoff Y offset of the region to copy in this Allocation
1706 * @param w Width of the region to copy
1707 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001708 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001709 */
1710 public void copy2DRangeTo(int xoff, int yoff, int w, int h, short[] data) {
1711 validateIsInt16();
1712 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1713 Element.DataType.SIGNED_16, data.length);
1714 }
1715
1716 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001717 * Copy from a rectangular region in this Allocation into an array.
1718 *
1719 * @param xoff X offset of the region to copy in this Allocation
1720 * @param yoff Y offset of the region to copy in this Allocation
1721 * @param w Width of the region to copy
1722 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001723 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001724 */
1725 public void copy2DRangeTo(int xoff, int yoff, int w, int h, int[] data) {
1726 validateIsInt32();
1727 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1728 Element.DataType.SIGNED_32, data.length);
1729 }
1730
1731 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001732 * Copy from a rectangular region in this Allocation into an array.
1733 *
1734 * @param xoff X offset of the region to copy in this Allocation
1735 * @param yoff Y offset of the region to copy in this Allocation
1736 * @param w Width of the region to copy
1737 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001738 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001739 */
1740 public void copy2DRangeTo(int xoff, int yoff, int w, int h, float[] data) {
1741 validateIsFloat32();
1742 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1743 Element.DataType.FLOAT_32, data.length);
1744 }
1745
1746
1747 /**
Miao Wang258db502015-03-03 14:05:36 -08001748 * Copy from a rectangular region in this Allocation into an array.
1749 * The array is assumed to be tightly packed.
Miao Wangc8e237e2015-02-20 18:36:32 -08001750 *
Miao Wang258db502015-03-03 14:05:36 -08001751 * The data type of the array is not required to be the same as
1752 * the element data type.
Miao Wangc8e237e2015-02-20 18:36:32 -08001753 */
1754 private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1755 Object array, Element.DataType dt, int arrayLen) {
1756 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked");
1757 mRS.validate();
1758 validate3DRange(xoff, yoff, zoff, w, h, d);
Miao Wang87e908d2015-03-02 15:15:15 -08001759 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1760 // AutoPadding for Vec3 Element
1761 boolean usePadding = false;
1762 int sizeBytes = arrayLen * dt.mSize;
1763 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1764 if (dataSize / 4 * 3 > sizeBytes) {
1765 throw new RSIllegalArgumentException("Array too small for allocation type.");
1766 }
1767 usePadding = true;
1768 sizeBytes = dataSize;
1769 } else {
1770 if (dataSize > sizeBytes) {
1771 throw new RSIllegalArgumentException("Array too small for allocation type.");
1772 }
1773 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001774 mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
Miao Wang87e908d2015-03-02 15:15:15 -08001775 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
Miao Wangc8e237e2015-02-20 18:36:32 -08001776 Trace.traceEnd(RenderScript.TRACE_TAG);
1777 }
1778
Miao Wang258db502015-03-03 14:05:36 -08001779 /*
Miao Wangc8e237e2015-02-20 18:36:32 -08001780 * Copy from a rectangular region in this Allocation into an array.
1781 *
1782 * @param xoff X offset of the region to copy in this Allocation
1783 * @param yoff Y offset of the region to copy in this Allocation
1784 * @param zoff Z offset of the region to copy in this Allocation
1785 * @param w Width of the region to copy
1786 * @param h Height of the region to copy
1787 * @param d Depth of the region to copy
1788 * @param array Dest Array to be copied into
1789 */
1790 public void copy3DRangeTo(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1791 copy3DRangeToUnchecked(xoff, yoff, zoff, w, h, d, array,
1792 validateObjectIsPrimitiveArray(array, true),
1793 java.lang.reflect.Array.getLength(array));
1794 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001795
1796 // creation
1797
Jason Sams49a05d72010-12-29 14:31:29 -08001798 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001799 static {
1800 mBitmapOptions.inScaled = false;
1801 }
1802
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001803 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001804 * Creates a new Allocation with the given {@link
1805 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001806 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001807 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001808 * @param mips specifies desired mipmap behaviour for the
1809 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001810 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001811 * utilized
1812 */
1813 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001814 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
Jason Sams771bebb2009-12-07 12:40:12 -08001815 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001816 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001817 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001818 }
Tim Murray460a0492013-11-19 12:45:54 -08001819 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001820 if (id == 0) {
1821 throw new RSRuntimeException("Allocation creation failed.");
1822 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001823 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams857d0c72011-11-23 15:02:15 -08001824 return new Allocation(id, rs, type, usage);
1825 }
1826
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001827 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001828 * Creates an Allocation with the size specified by the type and no mipmaps
1829 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001830 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001831 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001832 * @param type renderscript type describing data layout
1833 * @param usage bit field specifying how the allocation is
1834 * utilized
1835 *
1836 * @return allocation
1837 */
Jason Samse5d37122010-12-16 00:33:33 -08001838 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1839 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1840 }
1841
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001842 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001843 * Creates an Allocation for use by scripts with a given {@link
1844 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001845 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001846 * @param rs Context to which the Allocation will belong.
1847 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001848 *
1849 * @return allocation
1850 */
Jason Sams5476b452010-12-08 16:14:36 -08001851 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001852 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001853 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001854
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001855 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001856 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001857 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001858 * @param rs Context to which the Allocation will belong.
1859 * @param e Element to use in the Allocation
1860 * @param count the number of Elements in the Allocation
1861 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001862 * utilized
1863 *
1864 * @return allocation
1865 */
Jason Sams5476b452010-12-08 16:14:36 -08001866 static public Allocation createSized(RenderScript rs, Element e,
1867 int count, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001868 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
Jason Sams771bebb2009-12-07 12:40:12 -08001869 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001870 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001871 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001872 Type t = b.create();
1873
Tim Murray460a0492013-11-19 12:45:54 -08001874 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001875 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001876 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001877 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001878 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001879 return new Allocation(id, rs, t, usage);
1880 }
1881
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001882 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001883 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001884 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001885 * @param rs Context to which the Allocation will belong.
1886 * @param e Element to use in the Allocation
1887 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001888 *
1889 * @return allocation
1890 */
Jason Sams5476b452010-12-08 16:14:36 -08001891 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001892 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001893 }
1894
Jason Sams49a05d72010-12-29 14:31:29 -08001895 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001896 final Bitmap.Config bc = b.getConfig();
1897 if (bc == Bitmap.Config.ALPHA_8) {
1898 return Element.A_8(rs);
1899 }
1900 if (bc == Bitmap.Config.ARGB_4444) {
1901 return Element.RGBA_4444(rs);
1902 }
1903 if (bc == Bitmap.Config.ARGB_8888) {
1904 return Element.RGBA_8888(rs);
1905 }
1906 if (bc == Bitmap.Config.RGB_565) {
1907 return Element.RGB_565(rs);
1908 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001909 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001910 }
1911
Jason Sams49a05d72010-12-29 14:31:29 -08001912 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001913 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001914 Element e = elementFromBitmap(rs, b);
1915 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001916 tb.setX(b.getWidth());
1917 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001918 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001919 return tb.create();
1920 }
1921
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001922 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001923 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001924 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001925 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001926 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001927 * @param mips specifies desired mipmap behaviour for the
1928 * allocation
1929 * @param usage bit field specifying how the allocation is
1930 * utilized
1931 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001932 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001933 *
1934 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001935 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001936 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001937 int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001938 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
Jason Sams771bebb2009-12-07 12:40:12 -08001939 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001940
1941 // WAR undocumented color formats
1942 if (b.getConfig() == null) {
1943 if ((usage & USAGE_SHARED) != 0) {
1944 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1945 }
1946 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1947 Canvas c = new Canvas(newBitmap);
1948 c.drawBitmap(b, 0, 0, null);
1949 return createFromBitmap(rs, newBitmap, mips, usage);
1950 }
1951
Jason Sams5476b452010-12-08 16:14:36 -08001952 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001953
Tim Murraya3145512012-12-04 17:59:29 -08001954 // enable optimized bitmap path only with no mipmap and script-only usage
1955 if (mips == MipmapControl.MIPMAP_NONE &&
1956 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Tim Murray78e64942013-04-09 17:28:56 -07001957 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
Tim Murray460a0492013-11-19 12:45:54 -08001958 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08001959 if (id == 0) {
1960 throw new RSRuntimeException("Load failed.");
1961 }
1962
1963 // keep a reference to the Bitmap around to prevent GC
1964 Allocation alloc = new Allocation(id, rs, t, usage);
1965 alloc.setBitmap(b);
1966 return alloc;
1967 }
1968
Jason Sams9bf18922013-04-13 19:48:36 -07001969
Tim Murray460a0492013-11-19 12:45:54 -08001970 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001971 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001972 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001973 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001974 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001975 return new Allocation(id, rs, t, usage);
1976 }
1977
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001978 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001979 * Returns the handle to a raw buffer that is being managed by the screen
1980 * compositor. This operation is only valid for Allocations with {@link
1981 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001982 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001983 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001984 *
1985 */
1986 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08001987 if ((mUsage & USAGE_IO_INPUT) == 0) {
1988 throw new RSInvalidStateException("Allocation is not a surface texture.");
1989 }
Jason Sams1e68bac2015-03-17 16:36:55 -07001990
1991 if (mGetSurfaceSurface == null) {
1992 mGetSurfaceSurface = mRS.nAllocationGetSurface(getID(mRS));
1993 }
1994
1995 return mGetSurfaceSurface;
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001996 }
1997
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001998 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001999 * Associate a {@link android.view.Surface} with this Allocation. This
2000 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07002001 *
2002 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08002003 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002004 public void setSurface(Surface sur) {
2005 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08002006 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
2007 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
2008 }
2009
Jason Samse07694b2012-04-03 15:36:36 -07002010 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08002011 }
2012
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002013 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002014 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08002015 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002016 * <p>With target API version 18 or greater, this Allocation will be created
2017 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
2018 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
2019 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002020 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002021 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002022 * @param b bitmap source for the allocation data
2023 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002024 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002025 *
2026 */
Jason Sams6d8eb262010-12-15 01:41:00 -08002027 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08002028 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2029 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07002030 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08002031 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002032 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
2033 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08002034 }
2035
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002036 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002037 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
2038 * containing the horizontal list of cube faces. Each face must be a square,
2039 * have the same size as all other faces, and have a width that is a power
2040 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002041 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002042 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07002043 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002044 * format: right, left, top, bottom, front, back
2045 * @param mips specifies desired mipmap behaviour for the cubemap
2046 * @param usage bit field specifying how the cubemap is utilized
2047 *
2048 * @return allocation containing cubemap data
2049 *
2050 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002051 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002052 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002053 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002054 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08002055
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002056 int height = b.getHeight();
2057 int width = b.getWidth();
2058
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002059 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002060 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
2061 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002062 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002063 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002064 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002065 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002066 if (!isPow2) {
2067 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2068 }
2069
2070 Element e = elementFromBitmap(rs, b);
2071 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002072 tb.setX(height);
2073 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002074 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08002075 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002076 Type t = tb.create();
2077
Tim Murray460a0492013-11-19 12:45:54 -08002078 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002079 if(id == 0) {
2080 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
2081 }
Jason Sams5476b452010-12-08 16:14:36 -08002082 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002083 }
2084
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002085 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002086 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
2087 * from a {@link android.graphics.Bitmap} containing the horizontal list of
2088 * cube faces. Each face must be a square, have the same size as all other
2089 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002090 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002091 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002092 * @param b bitmap with cubemap faces layed out in the following
2093 * format: right, left, top, bottom, front, back
2094 *
2095 * @return allocation containing cubemap data
2096 *
2097 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002098 static public Allocation createCubemapFromBitmap(RenderScript rs,
2099 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08002100 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002101 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08002102 }
2103
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002104 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002105 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
2106 * objects containing the cube faces. Each face must be a square, have the
2107 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002108 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002109 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002110 * @param xpos cubemap face in the positive x direction
2111 * @param xneg cubemap face in the negative x direction
2112 * @param ypos cubemap face in the positive y direction
2113 * @param yneg cubemap face in the negative y direction
2114 * @param zpos cubemap face in the positive z direction
2115 * @param zneg cubemap face in the negative z direction
2116 * @param mips specifies desired mipmap behaviour for the cubemap
2117 * @param usage bit field specifying how the cubemap is utilized
2118 *
2119 * @return allocation containing cubemap data
2120 *
2121 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002122 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2123 Bitmap xpos,
2124 Bitmap xneg,
2125 Bitmap ypos,
2126 Bitmap yneg,
2127 Bitmap zpos,
2128 Bitmap zneg,
2129 MipmapControl mips,
2130 int usage) {
2131 int height = xpos.getHeight();
2132 if (xpos.getWidth() != height ||
2133 xneg.getWidth() != height || xneg.getHeight() != height ||
2134 ypos.getWidth() != height || ypos.getHeight() != height ||
2135 yneg.getWidth() != height || yneg.getHeight() != height ||
2136 zpos.getWidth() != height || zpos.getHeight() != height ||
2137 zneg.getWidth() != height || zneg.getHeight() != height) {
2138 throw new RSIllegalArgumentException("Only square cube map faces supported");
2139 }
2140 boolean isPow2 = (height & (height - 1)) == 0;
2141 if (!isPow2) {
2142 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2143 }
2144
2145 Element e = elementFromBitmap(rs, xpos);
2146 Type.Builder tb = new Type.Builder(rs, e);
2147 tb.setX(height);
2148 tb.setY(height);
2149 tb.setFaces(true);
2150 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
2151 Type t = tb.create();
2152 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
2153
2154 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07002155 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002156 adapter.copyFrom(xpos);
2157 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
2158 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002159 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002160 adapter.copyFrom(ypos);
2161 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
2162 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002163 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002164 adapter.copyFrom(zpos);
2165 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
2166 adapter.copyFrom(zneg);
2167
2168 return cubemap;
2169 }
2170
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002171 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002172 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
2173 * from 6 {@link android.graphics.Bitmap} objects containing the cube
2174 * faces. Each face must be a square, have the same size as all other faces,
2175 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002176 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002177 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002178 * @param xpos cubemap face in the positive x direction
2179 * @param xneg cubemap face in the negative x direction
2180 * @param ypos cubemap face in the positive y direction
2181 * @param yneg cubemap face in the negative y direction
2182 * @param zpos cubemap face in the positive z direction
2183 * @param zneg cubemap face in the negative z direction
2184 *
2185 * @return allocation containing cubemap data
2186 *
2187 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002188 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2189 Bitmap xpos,
2190 Bitmap xneg,
2191 Bitmap ypos,
2192 Bitmap yneg,
2193 Bitmap zpos,
2194 Bitmap zneg) {
2195 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
2196 zpos, zneg, MipmapControl.MIPMAP_NONE,
2197 USAGE_GRAPHICS_TEXTURE);
2198 }
2199
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002200 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002201 * Creates an Allocation from the Bitmap referenced
2202 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002203 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002204 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002205 * @param res application resources
2206 * @param id resource id to load the data from
2207 * @param mips specifies desired mipmap behaviour for the
2208 * allocation
2209 * @param usage bit field specifying how the allocation is
2210 * utilized
2211 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002212 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002213 *
2214 */
Jason Sams5476b452010-12-08 16:14:36 -08002215 static public Allocation createFromBitmapResource(RenderScript rs,
2216 Resources res,
2217 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08002218 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002219 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07002220
Jason Sams771bebb2009-12-07 12:40:12 -08002221 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07002222 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
2223 throw new RSIllegalArgumentException("Unsupported usage specified.");
2224 }
Jason Sams5476b452010-12-08 16:14:36 -08002225 Bitmap b = BitmapFactory.decodeResource(res, id);
2226 Allocation alloc = createFromBitmap(rs, b, mips, usage);
2227 b.recycle();
2228 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07002229 }
2230
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002231 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002232 * Creates a non-mipmapped Allocation to use as a graphics texture from the
2233 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002234 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002235 * <p>With target API version 18 or greater, this allocation will be created
2236 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
2237 * target API version 17 or lower, this allocation will be created with
2238 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08002239 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002240 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002241 * @param res application resources
2242 * @param id resource id to load the data from
2243 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002244 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002245 *
2246 */
Jason Sams5476b452010-12-08 16:14:36 -08002247 static public Allocation createFromBitmapResource(RenderScript rs,
2248 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08002249 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08002250 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2251 return createFromBitmapResource(rs, res, id,
2252 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07002253 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08002254 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002255 return createFromBitmapResource(rs, res, id,
2256 MipmapControl.MIPMAP_NONE,
2257 USAGE_GRAPHICS_TEXTURE);
2258 }
2259
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002260 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002261 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002262 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002263 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002264 * @param str string to create the allocation from
2265 * @param usage bit field specifying how the allocaiton is
2266 * utilized
2267 *
2268 */
Jason Sams5476b452010-12-08 16:14:36 -08002269 static public Allocation createFromString(RenderScript rs,
2270 String str,
2271 int usage) {
2272 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002273 byte[] allocArray = null;
2274 try {
2275 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08002276 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002277 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002278 return alloc;
2279 }
2280 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08002281 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002282 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002283 }
Jason Sams739c8262013-04-11 18:07:52 -07002284
2285 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002286 * Interface to handle notification when new buffers are available via
2287 * {@link #USAGE_IO_INPUT}. An application will receive one notification
2288 * when a buffer is available. Additional buffers will not trigger new
2289 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07002290 */
Jason Sams42ef2382013-08-29 13:30:59 -07002291 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07002292 public void onBufferAvailable(Allocation a);
2293 }
2294
2295 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002296 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07002297 *
Jason Sams42ef2382013-08-29 13:30:59 -07002298 * @param callback instance of the OnBufferAvailableListener
2299 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07002300 */
Jason Sams42ef2382013-08-29 13:30:59 -07002301 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07002302 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002303 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07002304 mBufferNotifier = callback;
2305 }
2306 }
2307
Tim Murrayb730d862014-08-18 16:14:24 -07002308 static void sendBufferNotification(long id) {
Jason Sams739c8262013-04-11 18:07:52 -07002309 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002310 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07002311
2312 if ((a != null) && (a.mBufferNotifier != null)) {
2313 a.mBufferNotifier.onBufferAvailable(a);
2314 }
2315 }
2316 }
2317
Miao Wangf0f6e802015-02-03 17:16:43 -08002318 /**
2319 * For USAGE_IO_OUTPUT, destroy() implies setSurface(null).
2320 *
2321 */
2322 @Override
2323 public void destroy() {
2324 if((mUsage & USAGE_IO_OUTPUT) != 0) {
2325 setSurface(null);
2326 }
2327 super.destroy();
2328 }
Jason Samsb8c5a842009-07-31 20:40:47 -07002329}