blob: 2191b54b6bc4a612e088435d3f1ebd6de697df56 [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
61 boolean mConstrainedLOD;
62 boolean mConstrainedFace;
63 boolean mConstrainedY;
64 boolean mConstrainedZ;
Jason Sams615e7ce2012-01-13 14:01:20 -080065 boolean mReadAllowed = true;
66 boolean mWriteAllowed = true;
Jason Samsba862d12011-07-07 15:24:42 -070067 int mSelectedY;
68 int mSelectedZ;
69 int mSelectedLOD;
70 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
71
72 int mCurrentDimX;
73 int mCurrentDimY;
74 int mCurrentDimZ;
75 int mCurrentCount;
Tim Murray460a0492013-11-19 12:45:54 -080076 static HashMap<Long, Allocation> mAllocationMap =
77 new HashMap<Long, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070078 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070079
Jason Sams3042d262013-11-25 18:28:33 -080080 private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
81 final Class c = d.getClass();
82 if (!c.isArray()) {
83 throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
84 }
85 final Class cmp = c.getComponentType();
86 if (!cmp.isPrimitive()) {
87 throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
88 }
89
90 if (cmp == Long.TYPE) {
91 if (checkType) {
92 validateIsInt64();
93 return mType.mElement.mType;
94 }
95 return Element.DataType.SIGNED_64;
96 }
97
98 if (cmp == Integer.TYPE) {
99 if (checkType) {
100 validateIsInt32();
101 return mType.mElement.mType;
102 }
103 return Element.DataType.SIGNED_32;
104 }
105
106 if (cmp == Short.TYPE) {
107 if (checkType) {
108 validateIsInt16();
109 return mType.mElement.mType;
110 }
111 return Element.DataType.SIGNED_16;
112 }
113
114 if (cmp == Byte.TYPE) {
115 if (checkType) {
116 validateIsInt8();
117 return mType.mElement.mType;
118 }
119 return Element.DataType.SIGNED_8;
120 }
121
122 if (cmp == Float.TYPE) {
123 if (checkType) {
124 validateIsFloat32();
125 }
126 return Element.DataType.FLOAT_32;
127 }
128
129 if (cmp == Double.TYPE) {
130 if (checkType) {
131 validateIsFloat64();
132 }
133 return Element.DataType.FLOAT_64;
134 }
135 return null;
136 }
137
138
Tim Murrayc11e25c2013-04-09 11:01:01 -0700139 /**
140 * The usage of the Allocation. These signal to RenderScript where to place
141 * the Allocation in memory.
142 *
143 */
Jason Sams5476b452010-12-08 16:14:36 -0800144
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700145 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700146 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -0800147 */
Jason Sams5476b452010-12-08 16:14:36 -0800148 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800149
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700150 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700151 * The Allocation will be used as a texture source by one or more graphics
152 * programs.
Jason Samsf7086092011-01-12 13:28:37 -0800153 *
154 */
Jason Sams5476b452010-12-08 16:14:36 -0800155 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800156
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700157 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700158 * The Allocation will be used as a graphics mesh.
159 *
160 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800161 *
162 */
Jason Sams5476b452010-12-08 16:14:36 -0800163 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800164
165
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700166 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700167 * The Allocation will be used as the source of shader constants by one or
168 * more programs.
169 *
170 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800171 *
172 */
Jason Sams5476b452010-12-08 16:14:36 -0800173 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
174
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700175 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700176 * The Allocation will be used as a target for offscreen rendering
177 *
178 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700179 *
180 */
181 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
182
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700183 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700184 * The Allocation will be used as a {@link android.view.Surface}
185 * consumer. This usage will cause the Allocation to be created
186 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800187 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800188 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700189 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800190
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700191 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700192 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700193 * producer. The dimensions and format of the {@link
Jason Sams3a1b8e42013-09-24 15:18:52 -0700194 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700195 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800196 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800197 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700198 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700199
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700200 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700201 * The Allocation's backing store will be inherited from another object
202 * (usually a {@link android.graphics.Bitmap}); copying to or from the
203 * original source Bitmap will cause a synchronization rather than a full
204 * copy. {@link #syncAll} may also be used to synchronize the Allocation
205 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800206 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700207 * <p>This is set by default for allocations created with {@link
208 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800209 *
210 */
211 public static final int USAGE_SHARED = 0x0080;
212
213 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700214 * Controls mipmap behavior when using the bitmap creation and update
215 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800216 */
Jason Sams4ef66502010-12-10 16:03:15 -0800217 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700218 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700219 * No mipmaps will be generated and the type generated from the incoming
220 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800221 */
Jason Sams5476b452010-12-08 16:14:36 -0800222 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800223
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700224 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700225 * A full mipmap chain will be created in script memory. The Type of
226 * the Allocation will contain a full mipmap chain. On upload, the full
227 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800228 */
Jason Sams5476b452010-12-08 16:14:36 -0800229 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800230
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700231 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700232 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
233 * not contain mipmaps. On upload, the allocation data will contain a
234 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800235 */
Jason Sams5476b452010-12-08 16:14:36 -0800236 MIPMAP_ON_SYNC_TO_TEXTURE(2);
237
238 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800239 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800240 mID = id;
241 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700242 }
243
Jason Sams48fe5342011-07-08 13:52:30 -0700244
Tim Murray460a0492013-11-19 12:45:54 -0800245 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700246 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700247 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700248 }
Jason Samse07694b2012-04-03 15:36:36 -0700249 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700250 }
251
Jason Sams03d2d002012-03-23 13:51:56 -0700252
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700253 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700254 * Get the {@link android.renderscript.Element} of the {@link
255 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700256 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700257 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700258 *
259 */
260 public Element getElement() {
261 return mType.getElement();
262 }
263
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700264 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700265 * Get the usage flags of the Allocation.
266 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700267 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700268 *
269 */
270 public int getUsage() {
271 return mUsage;
272 }
273
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700274 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700275 * Get the size of the Allocation in bytes.
276 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700277 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700278 *
279 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700280 public int getBytesSize() {
Tim Murray04f0d6e2013-12-17 17:15:25 -0800281 if (mType.mDimYuv != 0) {
282 return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
283 }
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700284 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700285 }
286
Jason Sams452a7662011-07-07 16:05:18 -0700287 private void updateCacheInfo(Type t) {
288 mCurrentDimX = t.getX();
289 mCurrentDimY = t.getY();
290 mCurrentDimZ = t.getZ();
291 mCurrentCount = mCurrentDimX;
292 if (mCurrentDimY > 1) {
293 mCurrentCount *= mCurrentDimY;
294 }
295 if (mCurrentDimZ > 1) {
296 mCurrentCount *= mCurrentDimZ;
297 }
298 }
Jason Samsba862d12011-07-07 15:24:42 -0700299
Tim Murraya3145512012-12-04 17:59:29 -0800300 private void setBitmap(Bitmap b) {
301 mBitmap = b;
302 }
303
Tim Murray460a0492013-11-19 12:45:54 -0800304 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700305 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800306 if ((usage & ~(USAGE_SCRIPT |
307 USAGE_GRAPHICS_TEXTURE |
308 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700309 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800310 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800311 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800312 USAGE_IO_OUTPUT |
313 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800314 throw new RSIllegalArgumentException("Unknown usage specified.");
315 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800316
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700317 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800318 mWriteAllowed = false;
319
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700320 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800321 USAGE_GRAPHICS_TEXTURE |
322 USAGE_SCRIPT)) != 0) {
323 throw new RSIllegalArgumentException("Invalid usage combination.");
324 }
325 }
Jason Sams9bf18922013-04-13 19:48:36 -0700326
Jason Sams5476b452010-12-08 16:14:36 -0800327 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800328 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700329
Jason Sams452a7662011-07-07 16:05:18 -0700330 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700331 // TODO: A3D doesn't have Type info during creation, so we can't
332 // calculate the size ahead of time. We can possibly add a method
333 // to update the size in the future if it seems reasonable.
334 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700335 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700336 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700337 try {
338 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
339 } catch (Exception e) {
340 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
341 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
342 }
343 }
344
345 protected void finalize() throws Throwable {
346 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
347 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700348 }
349
Jason Sams3042d262013-11-25 18:28:33 -0800350 private void validateIsInt64() {
351 if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
352 (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
353 return;
354 }
355 throw new RSIllegalArgumentException(
356 "64 bit integer source does not match allocation type " + mType.mElement.mType);
357 }
358
Jason Samsb97b2512011-01-16 15:04:08 -0800359 private void validateIsInt32() {
360 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
361 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
362 return;
363 }
364 throw new RSIllegalArgumentException(
365 "32 bit integer source does not match allocation type " + mType.mElement.mType);
366 }
367
368 private void validateIsInt16() {
369 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
370 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
371 return;
372 }
373 throw new RSIllegalArgumentException(
374 "16 bit integer source does not match allocation type " + mType.mElement.mType);
375 }
376
377 private void validateIsInt8() {
378 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
379 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
380 return;
381 }
382 throw new RSIllegalArgumentException(
383 "8 bit integer source does not match allocation type " + mType.mElement.mType);
384 }
385
386 private void validateIsFloat32() {
387 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
388 return;
389 }
390 throw new RSIllegalArgumentException(
391 "32 bit float source does not match allocation type " + mType.mElement.mType);
392 }
393
Jason Sams3042d262013-11-25 18:28:33 -0800394 private void validateIsFloat64() {
395 if (mType.mElement.mType == Element.DataType.FLOAT_64) {
396 return;
397 }
398 throw new RSIllegalArgumentException(
399 "64 bit float source does not match allocation type " + mType.mElement.mType);
400 }
401
Jason Samsb97b2512011-01-16 15:04:08 -0800402 private void validateIsObject() {
403 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
404 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
405 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
406 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
407 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
408 (mType.mElement.mType == Element.DataType.RS_MESH) ||
409 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
410 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
411 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
412 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
413 return;
414 }
415 throw new RSIllegalArgumentException(
416 "Object source does not match allocation type " + mType.mElement.mType);
417 }
418
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700419 @Override
420 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800421 super.updateFromNative();
Tim Murray460a0492013-11-19 12:45:54 -0800422 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700423 if(typeID != 0) {
424 mType = new Type(typeID, mRS);
425 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700426 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700427 }
428 }
429
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700430 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700431 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700432 *
433 * @return Type
434 *
435 */
Jason Samsea87e962010-01-12 12:12:28 -0800436 public Type getType() {
437 return mType;
438 }
439
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700440 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700441 * Propagate changes from one usage of the Allocation to the
442 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700443 *
444 */
Jason Sams5476b452010-12-08 16:14:36 -0800445 public void syncAll(int srcLocation) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700446 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
Jason Sams5476b452010-12-08 16:14:36 -0800447 switch (srcLocation) {
Jason Sams5476b452010-12-08 16:14:36 -0800448 case USAGE_GRAPHICS_TEXTURE:
Tim Murray78e64942013-04-09 17:28:56 -0700449 case USAGE_SCRIPT:
450 if ((mUsage & USAGE_SHARED) != 0) {
451 copyFrom(mBitmap);
452 }
453 break;
454 case USAGE_GRAPHICS_CONSTANTS:
Jason Sams5476b452010-12-08 16:14:36 -0800455 case USAGE_GRAPHICS_VERTEX:
456 break;
Tim Murray78e64942013-04-09 17:28:56 -0700457 case USAGE_SHARED:
458 if ((mUsage & USAGE_SHARED) != 0) {
459 copyTo(mBitmap);
460 }
461 break;
Jason Sams5476b452010-12-08 16:14:36 -0800462 default:
463 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
464 }
465 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700466 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700467 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800468 }
469
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700470 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700471 * Send a buffer to the output stream. The contents of the Allocation will
472 * be undefined after this operation. This operation is only valid if {@link
473 * #USAGE_IO_OUTPUT} is set on the Allocation.
474 *
Jason Sams163766c2012-02-15 12:04:24 -0800475 *
Jason Sams163766c2012-02-15 12:04:24 -0800476 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700477 public void ioSend() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700478 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
Jason Sams163766c2012-02-15 12:04:24 -0800479 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
480 throw new RSIllegalArgumentException(
481 "Can only send buffer if IO_OUTPUT usage specified.");
482 }
483 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700484 mRS.nAllocationIoSend(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700485 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800486 }
487
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700488 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700489 * Receive the latest input into the Allocation. This operation
490 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800491 *
Jason Sams163766c2012-02-15 12:04:24 -0800492 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700493 public void ioReceive() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700494 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
Jason Sams163766c2012-02-15 12:04:24 -0800495 if ((mUsage & USAGE_IO_INPUT) == 0) {
496 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700497 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800498 }
499 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700500 mRS.nAllocationIoReceive(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700501 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800502 }
503
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700504 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700505 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700506 *
507 * @param d Source array.
508 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800509 public void copyFrom(BaseObj[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700510 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Sams771bebb2009-12-07 12:40:12 -0800511 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800512 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700513 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800514 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700515 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800516 }
Tim Murray460a0492013-11-19 12:45:54 -0800517 // FIXME: requires 64-bit path
518
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800519 int i[] = new int[d.length];
520 for (int ct=0; ct < d.length; ct++) {
Tim Murray460a0492013-11-19 12:45:54 -0800521 i[ct] = (int)d[ct].getID(mRS);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800522 }
Jason Samsba862d12011-07-07 15:24:42 -0700523 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700524 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700525 }
526
Jason Samsfb9f82c2011-01-12 14:53:25 -0800527 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800528 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800529 if (bc == null) {
530 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
531 }
Jason Sams252c0782011-01-11 17:42:52 -0800532 switch (bc) {
533 case ALPHA_8:
534 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
535 throw new RSIllegalArgumentException("Allocation kind is " +
536 mType.getElement().mKind + ", type " +
537 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700538 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800539 " bytes, passed bitmap was " + bc);
540 }
541 break;
542 case ARGB_8888:
543 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700544 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800545 throw new RSIllegalArgumentException("Allocation kind is " +
546 mType.getElement().mKind + ", type " +
547 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700548 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800549 " bytes, passed bitmap was " + bc);
550 }
551 break;
552 case RGB_565:
553 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700554 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800555 throw new RSIllegalArgumentException("Allocation kind is " +
556 mType.getElement().mKind + ", type " +
557 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700558 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800559 " bytes, passed bitmap was " + bc);
560 }
561 break;
562 case ARGB_4444:
563 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700564 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800565 throw new RSIllegalArgumentException("Allocation kind is " +
566 mType.getElement().mKind + ", type " +
567 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700568 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800569 " bytes, passed bitmap was " + bc);
570 }
571 break;
572
573 }
Jason Sams4ef66502010-12-10 16:03:15 -0800574 }
575
Jason Samsfb9f82c2011-01-12 14:53:25 -0800576 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700577 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800578 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
579 }
580 }
581
Jason Sams3042d262013-11-25 18:28:33 -0800582 private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
583 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
584 mRS.validate();
585 if (mCurrentDimZ > 0) {
586 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
587 } else if (mCurrentDimY > 0) {
588 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
589 } else {
590 copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
591 }
592 Trace.traceEnd(RenderScript.TRACE_TAG);
593 }
594
595 /**
596 * Copy into this Allocation from an array. This method does not guarantee
597 * that the Allocation is compatible with the input buffer; it copies memory
598 * without reinterpretation.
599 *
600 * @param array The source data array
601 */
602 public void copyFromUnchecked(Object array) {
603 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
604 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
605 java.lang.reflect.Array.getLength(array));
606 Trace.traceEnd(RenderScript.TRACE_TAG);
607 }
608
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700609 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700610 * Copy into this Allocation from an array. This method does not guarantee
611 * that the Allocation is compatible with the input buffer; it copies memory
612 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800613 *
614 * @param d the source data array
615 */
616 public void copyFromUnchecked(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800617 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800618 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700619
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700620 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700621 * Copy into this Allocation from an array. This method does not guarantee
622 * that the Allocation is compatible with the input buffer; it copies memory
623 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800624 *
625 * @param d the source data array
626 */
627 public void copyFromUnchecked(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800628 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800629 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700630
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700631 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700632 * Copy into this Allocation from an array. This method does not guarantee
633 * that the Allocation is compatible with the input buffer; it copies memory
634 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800635 *
636 * @param d the source data array
637 */
638 public void copyFromUnchecked(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800639 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800640 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700641
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700642 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700643 * Copy into this Allocation from an array. This method does not guarantee
644 * that the Allocation is compatible with the input buffer; it copies memory
645 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800646 *
647 * @param d the source data array
648 */
649 public void copyFromUnchecked(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800650 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800651 }
652
Tim Murray6d7a53c2013-05-23 16:59:23 -0700653
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700654 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700655 * Copy into this Allocation from an array. This variant is type checked
656 * and will generate exceptions if the Allocation's {@link
Jason Sams3042d262013-11-25 18:28:33 -0800657 * android.renderscript.Element} does not match the array's
658 * primitive type.
659 *
Ying Wang16229812013-11-26 15:45:12 -0800660 * @param array The source data array
Jason Sams3042d262013-11-25 18:28:33 -0800661 */
662 public void copyFrom(Object array) {
663 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
664 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
665 java.lang.reflect.Array.getLength(array));
666 Trace.traceEnd(RenderScript.TRACE_TAG);
667 }
668
669 /**
670 * Copy into this Allocation from an array. This variant is type checked
671 * and will generate exceptions if the Allocation's {@link
Tim Murrayc11e25c2013-04-09 11:01:01 -0700672 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800673 *
674 * @param d the source data array
675 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800676 public void copyFrom(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800677 validateIsInt32();
678 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800679 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800680
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700681 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700682 * Copy into this Allocation from an array. This variant is type checked
683 * and will generate exceptions if the Allocation's {@link
684 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800685 *
686 * @param d the source data array
687 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800688 public void copyFrom(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800689 validateIsInt16();
690 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800691 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800692
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700693 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700694 * Copy into this Allocation from an array. This variant is type checked
695 * and will generate exceptions if the Allocation's {@link
696 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800697 *
698 * @param d the source data array
699 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800700 public void copyFrom(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800701 validateIsInt8();
702 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800703 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800704
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700705 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700706 * Copy into this Allocation from an array. This variant is type checked
707 * and will generate exceptions if the Allocation's {@link
708 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800709 *
710 * @param d the source data array
711 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800712 public void copyFrom(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800713 validateIsFloat32();
714 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800715 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800716
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700717 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700718 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
719 * height, width, and format of the bitmap must match the existing
720 * allocation.
721 *
722 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
723 * android.graphics.Bitmap} used to create the Allocation with {@link
724 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
725 * this will synchronize the Allocation with the latest data from the {@link
726 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800727 *
728 * @param b the source bitmap
729 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800730 public void copyFrom(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700731 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsfb9f82c2011-01-12 14:53:25 -0800732 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -0800733 if (b.getConfig() == null) {
734 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
735 Canvas c = new Canvas(newBitmap);
736 c.drawBitmap(b, 0, 0, null);
737 copyFrom(newBitmap);
738 return;
739 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800740 validateBitmapSize(b);
741 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700742 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700743 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700744 }
745
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700746 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700747 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700748 * must be identical.
749 *
750 * @param a the source allocation
751 */
752 public void copyFrom(Allocation a) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700753 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Tim Murrayf671fb02012-10-03 13:50:05 -0700754 mRS.validate();
755 if (!mType.equals(a.getType())) {
756 throw new RSIllegalArgumentException("Types of allocations must match.");
757 }
758 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700759 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700760 }
761
Tim Murrayf671fb02012-10-03 13:50:05 -0700762 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700763 * This is only intended to be used by auto-generated code reflected from
764 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800765 *
766 * @param xoff
767 * @param fp
768 */
Jason Sams21b41032011-01-16 15:05:41 -0800769 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800770 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700771 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700772 final byte[] data = fp.getData();
773
774 int count = data.length / eSize;
775 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800776 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700777 " not divisible by element size " + eSize + ".");
778 }
Jason Samsba862d12011-07-07 15:24:42 -0700779 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700780 }
781
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700782 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700783 * This is only intended to be used by auto-generated code reflected from
784 * the RenderScript script files.
Jason Samsfa445b92011-01-07 17:00:07 -0800785 *
786 * @param xoff
787 * @param component_number
788 * @param fp
789 */
Jason Sams21b41032011-01-16 15:05:41 -0800790 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800791 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700792 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800793 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700794 }
795 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800796 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700797 }
798
799 final byte[] data = fp.getData();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700800 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800801 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700802
803 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800804 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700805 " does not match component size " + eSize + ".");
806 }
807
Jason Sams48fe5342011-07-08 13:52:30 -0700808 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Jason Samsba862d12011-07-07 15:24:42 -0700809 component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700810 }
811
Jason Sams768bc022009-09-21 19:41:04 -0700812 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800813 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700814 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800815 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700816 }
817 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800818 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700819 }
Jason Samsba862d12011-07-07 15:24:42 -0700820 if((off + count) > mCurrentCount) {
821 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700822 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700823 }
Jason Samsba862d12011-07-07 15:24:42 -0700824 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800825 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700826 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700827 }
828
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700829 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700830 * Generate a mipmap chain. This is only valid if the Type of the Allocation
831 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800832 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700833 * <p>This function will generate a complete set of mipmaps from the top
834 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800835 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700836 * <p>If the Allocation is also using other memory spaces, a call to {@link
837 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800838 */
839 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700840 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800841 }
842
Jason Sams3042d262013-11-25 18:28:33 -0800843 private void copy1DRangeFromUnchecked(int off, int count, Object array,
844 Element.DataType dt, int arrayLen) {
845 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
846 final int dataSize = mType.mElement.getBytesSize() * count;
847 data1DChecks(off, count, arrayLen * dt.mSize, dataSize);
848 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt);
849 Trace.traceEnd(RenderScript.TRACE_TAG);
850 }
851
852 /**
853 * Copy an array into part of this Allocation. This method does not
854 * guarantee that the Allocation is compatible with the input buffer.
855 *
856 * @param off The offset of the first element to be copied.
857 * @param count The number of elements to be copied.
858 * @param array The source data array
859 */
860 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
861 copy1DRangeFromUnchecked(off, count, array,
862 validateObjectIsPrimitiveArray(array, false),
863 java.lang.reflect.Array.getLength(array));
864 }
865
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700866 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700867 * Copy an array into part of this Allocation. This method does not
868 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800869 *
870 * @param off The offset of the first element to be copied.
871 * @param count The number of elements to be copied.
872 * @param d the source data array
873 */
874 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800875 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700876 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700877
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700878 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700879 * Copy an array into part of this Allocation. This method does not
880 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800881 *
882 * @param off The offset of the first element to be copied.
883 * @param count The number of elements to be copied.
884 * @param d the source data array
885 */
886 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800887 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700888 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700889
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700890 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700891 * Copy an array into part of this Allocation. This method does not
892 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800893 *
894 * @param off The offset of the first element to be copied.
895 * @param count The number of elements to be copied.
896 * @param d the source data array
897 */
898 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800899 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700900 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700901
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700902 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700903 * Copy an array into part of this Allocation. This method does not
904 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800905 *
906 * @param off The offset of the first element to be copied.
907 * @param count The number of elements to be copied.
908 * @param d the source data array
909 */
910 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800911 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
912 }
913
914
915 /**
916 * Copy an array into part of this Allocation. This variant is type checked
917 * and will generate exceptions if the Allocation type does not
918 * match the component type of the array passed in.
919 *
920 * @param off The offset of the first element to be copied.
921 * @param count The number of elements to be copied.
922 * @param array The source data array.
923 */
924 public void copy1DRangeFrom(int off, int count, Object array) {
925 copy1DRangeFromUnchecked(off, count, array,
926 validateObjectIsPrimitiveArray(array, true),
927 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -0700928 }
929
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700930 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700931 * Copy an array into part of this Allocation. This variant is type checked
932 * and will generate exceptions if the Allocation type is not a 32 bit
933 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800934 *
935 * @param off The offset of the first element to be copied.
936 * @param count The number of elements to be copied.
937 * @param d the source data array
938 */
Jason Samsb97b2512011-01-16 15:04:08 -0800939 public void copy1DRangeFrom(int off, int count, int[] d) {
940 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -0800941 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800942 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800943
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 variant is type checked
946 * and will generate exceptions if the Allocation type is not a 16 bit
947 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800948 *
949 * @param off The offset of the first element to be copied.
950 * @param count The number of elements to be copied.
951 * @param d the source data array
952 */
Jason Samsb97b2512011-01-16 15:04:08 -0800953 public void copy1DRangeFrom(int off, int count, short[] d) {
954 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -0800955 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800956 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800957
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700958 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700959 * Copy an array into part of this Allocation. This variant is type checked
960 * and will generate exceptions if the Allocation type is not an 8 bit
961 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800962 *
963 * @param off The offset of the first element to be copied.
964 * @param count The number of elements to be copied.
965 * @param d the source data array
966 */
Jason Samsb97b2512011-01-16 15:04:08 -0800967 public void copy1DRangeFrom(int off, int count, byte[] d) {
968 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -0800969 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800970 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800971
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700972 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700973 * Copy an array into part of this Allocation. This variant is type checked
974 * and will generate exceptions if the Allocation type is not a 32 bit float
975 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800976 *
977 * @param off The offset of the first element to be copied.
978 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700979 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800980 */
Jason Samsb97b2512011-01-16 15:04:08 -0800981 public void copy1DRangeFrom(int off, int count, float[] d) {
982 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -0800983 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800984 }
Jason Sams3042d262013-11-25 18:28:33 -0800985
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700986 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700987 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700988 *
989 * @param off The offset of the first element to be copied.
990 * @param count The number of elements to be copied.
991 * @param data the source data allocation.
992 * @param dataOff off The offset of the first element in data to
993 * be copied.
994 */
995 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700996 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -0700997 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700998 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700999 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001000 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001001 }
1002
Jason Samsfb9f82c2011-01-12 14:53:25 -08001003 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001004 if (mAdaptedAllocation != null) {
1005
1006 } else {
1007
1008 if (xoff < 0 || yoff < 0) {
1009 throw new RSIllegalArgumentException("Offset cannot be negative.");
1010 }
1011 if (h < 0 || w < 0) {
1012 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1013 }
1014 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1015 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1016 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001017 }
1018 }
Jason Sams768bc022009-09-21 19:41:04 -07001019
Jason Sams3042d262013-11-25 18:28:33 -08001020 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1021 Element.DataType dt, int arrayLen) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001022 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001023 mRS.validate();
1024 validate2DRange(xoff, yoff, w, h);
Jason Sams3042d262013-11-25 18:28:33 -08001025 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1026 array, arrayLen * dt.mSize, dt);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001027 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001028 }
1029
Jason Sams3042d262013-11-25 18:28:33 -08001030 /**
1031 * Copy from an array into a rectangular region in this Allocation. The
1032 * array is assumed to be tightly packed.
1033 *
1034 * @param xoff X offset of the region to update in this Allocation
1035 * @param yoff Y offset of the region to update in this Allocation
1036 * @param w Width of the region to update
1037 * @param h Height of the region to update
Ying Wang16229812013-11-26 15:45:12 -08001038 * @param array Data to be placed into the Allocation
Jason Sams3042d262013-11-25 18:28:33 -08001039 */
1040 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
1041 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1042 copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
1043 validateObjectIsPrimitiveArray(array, true),
1044 java.lang.reflect.Array.getLength(array));
Tim Murray6d7a53c2013-05-23 16:59:23 -07001045 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001046 }
1047
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001048 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001049 * Copy from an array into a rectangular region in this Allocation. The
1050 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -08001051 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001052 * @param xoff X offset of the region to update in this Allocation
1053 * @param yoff Y offset of the region to update in this Allocation
1054 * @param w Width of the region to update
1055 * @param h Height of the region to update
1056 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001057 */
1058 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001059 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001060 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1061 Element.DataType.SIGNED_8, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001062 }
1063
Tim Murrayc11e25c2013-04-09 11:01:01 -07001064 /**
1065 * Copy from an array into a rectangular region in this Allocation. The
1066 * array is assumed to be tightly packed.
1067 *
1068 * @param xoff X offset of the region to update in this Allocation
1069 * @param yoff Y offset of the region to update in this Allocation
1070 * @param w Width of the region to update
1071 * @param h Height of the region to update
1072 * @param data to be placed into the Allocation
1073 */
Jason Samsf7086092011-01-12 13:28:37 -08001074 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001075 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001076 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1077 Element.DataType.SIGNED_16, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001078 }
1079
Tim Murrayc11e25c2013-04-09 11:01:01 -07001080 /**
1081 * Copy from an array into a rectangular region in this Allocation. The
1082 * array is assumed to be tightly packed.
1083 *
1084 * @param xoff X offset of the region to update in this Allocation
1085 * @param yoff Y offset of the region to update in this Allocation
1086 * @param w Width of the region to update
1087 * @param h Height of the region to update
1088 * @param data to be placed into the Allocation
1089 */
Jason Samsf7086092011-01-12 13:28:37 -08001090 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001091 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001092 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1093 Element.DataType.SIGNED_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001094 }
1095
Tim Murrayc11e25c2013-04-09 11:01:01 -07001096 /**
1097 * Copy from an array into a rectangular region in this Allocation. The
1098 * array is assumed to be tightly packed.
1099 *
1100 * @param xoff X offset of the region to update in this Allocation
1101 * @param yoff Y offset of the region to update in this Allocation
1102 * @param w Width of the region to update
1103 * @param h Height of the region to update
1104 * @param data to be placed into the Allocation
1105 */
Jason Samsf7086092011-01-12 13:28:37 -08001106 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001107 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001108 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1109 Element.DataType.FLOAT_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001110 }
1111
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001112 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001113 * Copy a rectangular region from an Allocation into a rectangular region in
1114 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001115 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001116 * @param xoff X offset of the region in this Allocation
1117 * @param yoff Y offset of the region in this Allocation
1118 * @param w Width of the region to update.
1119 * @param h Height of the region to update.
1120 * @param data source Allocation.
1121 * @param dataXoff X offset in source Allocation
1122 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001123 */
1124 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1125 Allocation data, int dataXoff, int dataYoff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001126 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001127 mRS.validate();
1128 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -07001129 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -07001130 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001131 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -07001132 data.mSelectedLOD, data.mSelectedFace.mID);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001133 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001134 }
1135
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001136 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001137 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1138 * and width of the update will use the height and width of the {@link
1139 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001140 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001141 * @param xoff X offset of the region to update in this Allocation
1142 * @param yoff Y offset of the region to update in this Allocation
1143 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001144 */
1145 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001146 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Jason Samsfa445b92011-01-07 17:00:07 -08001147 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001148 if (data.getConfig() == null) {
1149 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1150 Canvas c = new Canvas(newBitmap);
1151 c.drawBitmap(data, 0, 0, null);
1152 copy2DRangeFrom(xoff, yoff, newBitmap);
Jason Samsb05d6892013-04-09 15:59:24 -07001153 return;
Tim Murrayabd5db92013-02-28 11:45:22 -08001154 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001155 validateBitmapFormat(data);
1156 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -07001157 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001158 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001159 }
1160
Jason Samsb05d6892013-04-09 15:59:24 -07001161 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1162 if (mAdaptedAllocation != null) {
1163
1164 } else {
1165
1166 if (xoff < 0 || yoff < 0 || zoff < 0) {
1167 throw new RSIllegalArgumentException("Offset cannot be negative.");
1168 }
1169 if (h < 0 || w < 0 || d < 0) {
1170 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1171 }
1172 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1173 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1174 }
1175 }
1176 }
1177
1178 /**
1179 * @hide
1180 *
1181 */
Jason Sams3042d262013-11-25 18:28:33 -08001182 private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1183 Object array, Element.DataType dt, int arrayLen) {
1184 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
Jason Samsb05d6892013-04-09 15:59:24 -07001185 mRS.validate();
1186 validate3DRange(xoff, yoff, zoff, w, h, d);
Jason Sams3042d262013-11-25 18:28:33 -08001187 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1188 array, arrayLen * dt.mSize, dt);
1189 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb05d6892013-04-09 15:59:24 -07001190 }
1191
1192 /**
1193 * @hide
Jason Samsb05d6892013-04-09 15:59:24 -07001194 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001195 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001196 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001197 * @param xoff X offset of the region to update in this Allocation
1198 * @param yoff Y offset of the region to update in this Allocation
1199 * @param zoff Z offset of the region to update in this Allocation
1200 * @param w Width of the region to update
1201 * @param h Height of the region to update
1202 * @param d Depth of the region to update
Jason Samsb05d6892013-04-09 15:59:24 -07001203 * @param data to be placed into the allocation
1204 */
Jason Sams3042d262013-11-25 18:28:33 -08001205 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1206 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
1207 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
1208 validateObjectIsPrimitiveArray(array, true),
1209 java.lang.reflect.Array.getLength(array));
1210 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb05d6892013-04-09 15:59:24 -07001211 }
1212
1213 /**
1214 * @hide
1215 * Copy a rectangular region into the allocation from another
1216 * allocation.
1217 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001218 * @param xoff X offset of the region to update in this Allocation
1219 * @param yoff Y offset of the region to update in this Allocation
1220 * @param zoff Z offset of the region to update in this Allocation
1221 * @param w Width of the region to update.
1222 * @param h Height of the region to update.
1223 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001224 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001225 * @param dataXoff X offset of the region in the source Allocation
1226 * @param dataYoff Y offset of the region in the source Allocation
1227 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001228 */
1229 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1230 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1231 mRS.validate();
1232 validate3DRange(xoff, yoff, zoff, w, h, d);
1233 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1234 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1235 data.mSelectedLOD);
1236 }
1237
Jason Samsfa445b92011-01-07 17:00:07 -08001238
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001239 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001240 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1241 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001242 *
1243 * @param b The bitmap to be set from the Allocation.
1244 */
Jason Samsfa445b92011-01-07 17:00:07 -08001245 public void copyTo(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001246 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsfb9f82c2011-01-12 14:53:25 -08001247 mRS.validate();
1248 validateBitmapFormat(b);
1249 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -07001250 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001251 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001252 }
1253
Jason Sams3042d262013-11-25 18:28:33 -08001254 private void copyTo(Object array, Element.DataType dt, int arrayLen) {
1255 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1256 mRS.validate();
1257 mRS.nAllocationRead(getID(mRS), array, dt);
1258 Trace.traceEnd(RenderScript.TRACE_TAG);
1259 }
1260
1261 /**
1262 * Copy from the Allocation into an array. The array must be at
1263 * least as large as the Allocation. The
1264 * {@link android.renderscript.Element} must match the component
1265 * type of the array passed in.
1266 *
1267 * @param array The array to be set from the Allocation.
1268 */
1269 public void copyTo(Object array) {
1270 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1271 java.lang.reflect.Array.getLength(array));
1272 }
1273
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001274 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001275 * Copy from the Allocation into a byte array. The array must be at least
1276 * as large as the Allocation. The allocation must be of an 8 bit integer
1277 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001278 *
1279 * @param d The array to be set from the Allocation.
1280 */
Jason Samsfa445b92011-01-07 17:00:07 -08001281 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001282 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001283 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001284 }
1285
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001286 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001287 * Copy from the Allocation into a short array. The array must be at least
1288 * as large as the Allocation. The allocation must be of an 16 bit integer
1289 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001290 *
1291 * @param d The array to be set from the Allocation.
1292 */
Jason Samsfa445b92011-01-07 17:00:07 -08001293 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001294 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001295 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001296 }
1297
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001298 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001299 * Copy from the Allocation into a int array. The array must be at least as
1300 * large as the Allocation. The allocation must be of an 32 bit integer
1301 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001302 *
1303 * @param d The array to be set from the Allocation.
1304 */
Jason Samsfa445b92011-01-07 17:00:07 -08001305 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001306 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001307 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001308 }
1309
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001310 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001311 * Copy from the Allocation into a float array. The array must be at least
1312 * as large as the Allocation. The allocation must be of an 32 bit float
1313 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001314 *
1315 * @param d The array to be set from the Allocation.
1316 */
Jason Samsfa445b92011-01-07 17:00:07 -08001317 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001318 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001319 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001320 }
1321
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001322 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001323 * Resize a 1D allocation. The contents of the allocation are preserved.
1324 * If new elements are allocated objects are created with null contents and
1325 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001326 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001327 * <p>If the new region is smaller the references of any objects outside the
1328 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001329 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001330 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001331 *
1332 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001333 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001334 * @deprecated RenderScript objects should be immutable once created. The
1335 * replacement is to create a new allocation and copy the contents.
Jason Samsf7086092011-01-12 13:28:37 -08001336 */
Jason Sams31a7e422010-10-26 13:09:17 -07001337 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001338 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001339 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001340 }
Jason Samse07694b2012-04-03 15:36:36 -07001341 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001342 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001343
Tim Murray460a0492013-11-19 12:45:54 -08001344 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001345 mType = new Type(typeID, mRS);
1346 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001347 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001348 }
1349
Jason Samsb8c5a842009-07-31 20:40:47 -07001350
1351 // creation
1352
Jason Sams49a05d72010-12-29 14:31:29 -08001353 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001354 static {
1355 mBitmapOptions.inScaled = false;
1356 }
1357
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001358 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001359 * Creates a new Allocation with the given {@link
1360 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001361 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001362 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001363 * @param mips specifies desired mipmap behaviour for the
1364 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001365 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001366 * utilized
1367 */
1368 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001369 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
Jason Sams771bebb2009-12-07 12:40:12 -08001370 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001371 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001372 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001373 }
Tim Murray460a0492013-11-19 12:45:54 -08001374 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001375 if (id == 0) {
1376 throw new RSRuntimeException("Allocation creation failed.");
1377 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001378 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams857d0c72011-11-23 15:02:15 -08001379 return new Allocation(id, rs, type, usage);
1380 }
1381
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001382 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001383 * Creates an Allocation with the size specified by the type and no mipmaps
1384 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001385 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001386 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001387 * @param type renderscript type describing data layout
1388 * @param usage bit field specifying how the allocation is
1389 * utilized
1390 *
1391 * @return allocation
1392 */
Jason Samse5d37122010-12-16 00:33:33 -08001393 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1394 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1395 }
1396
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001397 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001398 * Creates an Allocation for use by scripts with a given {@link
1399 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001400 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001401 * @param rs Context to which the Allocation will belong.
1402 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001403 *
1404 * @return allocation
1405 */
Jason Sams5476b452010-12-08 16:14:36 -08001406 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001407 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001408 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001409
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001410 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001411 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001412 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001413 * @param rs Context to which the Allocation will belong.
1414 * @param e Element to use in the Allocation
1415 * @param count the number of Elements in the Allocation
1416 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001417 * utilized
1418 *
1419 * @return allocation
1420 */
Jason Sams5476b452010-12-08 16:14:36 -08001421 static public Allocation createSized(RenderScript rs, Element e,
1422 int count, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001423 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
Jason Sams771bebb2009-12-07 12:40:12 -08001424 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001425 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001426 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001427 Type t = b.create();
1428
Tim Murray460a0492013-11-19 12:45:54 -08001429 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001430 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001431 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001432 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001433 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001434 return new Allocation(id, rs, t, usage);
1435 }
1436
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001437 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001438 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001439 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001440 * @param rs Context to which the Allocation will belong.
1441 * @param e Element to use in the Allocation
1442 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001443 *
1444 * @return allocation
1445 */
Jason Sams5476b452010-12-08 16:14:36 -08001446 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001447 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001448 }
1449
Jason Sams49a05d72010-12-29 14:31:29 -08001450 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001451 final Bitmap.Config bc = b.getConfig();
1452 if (bc == Bitmap.Config.ALPHA_8) {
1453 return Element.A_8(rs);
1454 }
1455 if (bc == Bitmap.Config.ARGB_4444) {
1456 return Element.RGBA_4444(rs);
1457 }
1458 if (bc == Bitmap.Config.ARGB_8888) {
1459 return Element.RGBA_8888(rs);
1460 }
1461 if (bc == Bitmap.Config.RGB_565) {
1462 return Element.RGB_565(rs);
1463 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001464 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001465 }
1466
Jason Sams49a05d72010-12-29 14:31:29 -08001467 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001468 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001469 Element e = elementFromBitmap(rs, b);
1470 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001471 tb.setX(b.getWidth());
1472 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001473 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001474 return tb.create();
1475 }
1476
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001477 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001478 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001479 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001480 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001481 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001482 * @param mips specifies desired mipmap behaviour for the
1483 * allocation
1484 * @param usage bit field specifying how the allocation is
1485 * utilized
1486 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001487 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001488 *
1489 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001490 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001491 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001492 int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001493 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
Jason Sams771bebb2009-12-07 12:40:12 -08001494 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001495
1496 // WAR undocumented color formats
1497 if (b.getConfig() == null) {
1498 if ((usage & USAGE_SHARED) != 0) {
1499 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1500 }
1501 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1502 Canvas c = new Canvas(newBitmap);
1503 c.drawBitmap(b, 0, 0, null);
1504 return createFromBitmap(rs, newBitmap, mips, usage);
1505 }
1506
Jason Sams5476b452010-12-08 16:14:36 -08001507 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001508
Tim Murraya3145512012-12-04 17:59:29 -08001509 // enable optimized bitmap path only with no mipmap and script-only usage
1510 if (mips == MipmapControl.MIPMAP_NONE &&
1511 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Tim Murray78e64942013-04-09 17:28:56 -07001512 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
Tim Murray460a0492013-11-19 12:45:54 -08001513 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08001514 if (id == 0) {
1515 throw new RSRuntimeException("Load failed.");
1516 }
1517
1518 // keep a reference to the Bitmap around to prevent GC
1519 Allocation alloc = new Allocation(id, rs, t, usage);
1520 alloc.setBitmap(b);
1521 return alloc;
1522 }
1523
Jason Sams9bf18922013-04-13 19:48:36 -07001524
Tim Murray460a0492013-11-19 12:45:54 -08001525 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001526 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001527 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001528 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001529 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001530 return new Allocation(id, rs, t, usage);
1531 }
1532
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001533 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001534 * Returns the handle to a raw buffer that is being managed by the screen
1535 * compositor. This operation is only valid for Allocations with {@link
1536 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001537 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001538 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001539 *
1540 */
1541 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08001542 if ((mUsage & USAGE_IO_INPUT) == 0) {
1543 throw new RSInvalidStateException("Allocation is not a surface texture.");
1544 }
1545 return mRS.nAllocationGetSurface(getID(mRS));
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001546 }
1547
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001548 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001549 * Associate a {@link android.view.Surface} with this Allocation. This
1550 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001551 *
1552 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001553 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001554 public void setSurface(Surface sur) {
1555 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001556 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1557 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1558 }
1559
Jason Samse07694b2012-04-03 15:36:36 -07001560 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001561 }
1562
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001563 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001564 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08001565 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001566 * <p>With target API version 18 or greater, this Allocation will be created
1567 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
1568 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
1569 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001570 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001571 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001572 * @param b bitmap source for the allocation data
1573 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001574 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001575 *
1576 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001577 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08001578 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1579 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07001580 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08001581 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001582 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1583 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001584 }
1585
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001586 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001587 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1588 * containing the horizontal list of cube faces. Each face must be a square,
1589 * have the same size as all other faces, and have a width that is a power
1590 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001591 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001592 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001593 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001594 * format: right, left, top, bottom, front, back
1595 * @param mips specifies desired mipmap behaviour for the cubemap
1596 * @param usage bit field specifying how the cubemap is utilized
1597 *
1598 * @return allocation containing cubemap data
1599 *
1600 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001601 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001602 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001603 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001604 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001605
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001606 int height = b.getHeight();
1607 int width = b.getWidth();
1608
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001609 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001610 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1611 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001612 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001613 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001614 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001615 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001616 if (!isPow2) {
1617 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1618 }
1619
1620 Element e = elementFromBitmap(rs, b);
1621 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001622 tb.setX(height);
1623 tb.setY(height);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001624 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001625 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001626 Type t = tb.create();
1627
Tim Murray460a0492013-11-19 12:45:54 -08001628 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001629 if(id == 0) {
1630 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1631 }
Jason Sams5476b452010-12-08 16:14:36 -08001632 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001633 }
1634
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001635 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001636 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
1637 * from a {@link android.graphics.Bitmap} containing the horizontal list of
1638 * cube faces. Each face must be a square, have the same size as all other
1639 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001640 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001641 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001642 * @param b bitmap with cubemap faces layed out in the following
1643 * format: right, left, top, bottom, front, back
1644 *
1645 * @return allocation containing cubemap data
1646 *
1647 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001648 static public Allocation createCubemapFromBitmap(RenderScript rs,
1649 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001650 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001651 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001652 }
1653
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001654 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001655 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
1656 * objects containing the cube faces. Each face must be a square, have the
1657 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001658 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001659 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001660 * @param xpos cubemap face in the positive x direction
1661 * @param xneg cubemap face in the negative x direction
1662 * @param ypos cubemap face in the positive y direction
1663 * @param yneg cubemap face in the negative y direction
1664 * @param zpos cubemap face in the positive z direction
1665 * @param zneg cubemap face in the negative z direction
1666 * @param mips specifies desired mipmap behaviour for the cubemap
1667 * @param usage bit field specifying how the cubemap is utilized
1668 *
1669 * @return allocation containing cubemap data
1670 *
1671 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001672 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1673 Bitmap xpos,
1674 Bitmap xneg,
1675 Bitmap ypos,
1676 Bitmap yneg,
1677 Bitmap zpos,
1678 Bitmap zneg,
1679 MipmapControl mips,
1680 int usage) {
1681 int height = xpos.getHeight();
1682 if (xpos.getWidth() != height ||
1683 xneg.getWidth() != height || xneg.getHeight() != height ||
1684 ypos.getWidth() != height || ypos.getHeight() != height ||
1685 yneg.getWidth() != height || yneg.getHeight() != height ||
1686 zpos.getWidth() != height || zpos.getHeight() != height ||
1687 zneg.getWidth() != height || zneg.getHeight() != height) {
1688 throw new RSIllegalArgumentException("Only square cube map faces supported");
1689 }
1690 boolean isPow2 = (height & (height - 1)) == 0;
1691 if (!isPow2) {
1692 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1693 }
1694
1695 Element e = elementFromBitmap(rs, xpos);
1696 Type.Builder tb = new Type.Builder(rs, e);
1697 tb.setX(height);
1698 tb.setY(height);
1699 tb.setFaces(true);
1700 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1701 Type t = tb.create();
1702 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1703
1704 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001705 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001706 adapter.copyFrom(xpos);
1707 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1708 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001709 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001710 adapter.copyFrom(ypos);
1711 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1712 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001713 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001714 adapter.copyFrom(zpos);
1715 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1716 adapter.copyFrom(zneg);
1717
1718 return cubemap;
1719 }
1720
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001721 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001722 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
1723 * from 6 {@link android.graphics.Bitmap} objects containing the cube
1724 * faces. Each face must be a square, have the same size as all other faces,
1725 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001726 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001727 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001728 * @param xpos cubemap face in the positive x direction
1729 * @param xneg cubemap face in the negative x direction
1730 * @param ypos cubemap face in the positive y direction
1731 * @param yneg cubemap face in the negative y direction
1732 * @param zpos cubemap face in the positive z direction
1733 * @param zneg cubemap face in the negative z direction
1734 *
1735 * @return allocation containing cubemap data
1736 *
1737 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001738 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1739 Bitmap xpos,
1740 Bitmap xneg,
1741 Bitmap ypos,
1742 Bitmap yneg,
1743 Bitmap zpos,
1744 Bitmap zneg) {
1745 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1746 zpos, zneg, MipmapControl.MIPMAP_NONE,
1747 USAGE_GRAPHICS_TEXTURE);
1748 }
1749
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001750 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001751 * Creates an Allocation from the Bitmap referenced
1752 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001753 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001754 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001755 * @param res application resources
1756 * @param id resource id to load the data from
1757 * @param mips specifies desired mipmap behaviour for the
1758 * allocation
1759 * @param usage bit field specifying how the allocation is
1760 * utilized
1761 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001762 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001763 *
1764 */
Jason Sams5476b452010-12-08 16:14:36 -08001765 static public Allocation createFromBitmapResource(RenderScript rs,
1766 Resources res,
1767 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001768 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001769 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001770
Jason Sams771bebb2009-12-07 12:40:12 -08001771 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07001772 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
1773 throw new RSIllegalArgumentException("Unsupported usage specified.");
1774 }
Jason Sams5476b452010-12-08 16:14:36 -08001775 Bitmap b = BitmapFactory.decodeResource(res, id);
1776 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1777 b.recycle();
1778 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001779 }
1780
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001781 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001782 * Creates a non-mipmapped Allocation to use as a graphics texture from the
1783 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001784 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001785 * <p>With target API version 18 or greater, this allocation will be created
1786 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
1787 * target API version 17 or lower, this allocation will be created with
1788 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08001789 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001790 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001791 * @param res application resources
1792 * @param id resource id to load the data from
1793 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001794 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001795 *
1796 */
Jason Sams5476b452010-12-08 16:14:36 -08001797 static public Allocation createFromBitmapResource(RenderScript rs,
1798 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001799 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08001800 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1801 return createFromBitmapResource(rs, res, id,
1802 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07001803 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08001804 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001805 return createFromBitmapResource(rs, res, id,
1806 MipmapControl.MIPMAP_NONE,
1807 USAGE_GRAPHICS_TEXTURE);
1808 }
1809
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001810 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001811 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001812 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001813 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001814 * @param str string to create the allocation from
1815 * @param usage bit field specifying how the allocaiton is
1816 * utilized
1817 *
1818 */
Jason Sams5476b452010-12-08 16:14:36 -08001819 static public Allocation createFromString(RenderScript rs,
1820 String str,
1821 int usage) {
1822 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001823 byte[] allocArray = null;
1824 try {
1825 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001826 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001827 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001828 return alloc;
1829 }
1830 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001831 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001832 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001833 }
Jason Sams739c8262013-04-11 18:07:52 -07001834
1835 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001836 * Interface to handle notification when new buffers are available via
1837 * {@link #USAGE_IO_INPUT}. An application will receive one notification
1838 * when a buffer is available. Additional buffers will not trigger new
1839 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07001840 */
Jason Sams42ef2382013-08-29 13:30:59 -07001841 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07001842 public void onBufferAvailable(Allocation a);
1843 }
1844
1845 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001846 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07001847 *
Jason Sams42ef2382013-08-29 13:30:59 -07001848 * @param callback instance of the OnBufferAvailableListener
1849 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07001850 */
Jason Sams42ef2382013-08-29 13:30:59 -07001851 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07001852 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08001853 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07001854 mBufferNotifier = callback;
1855 }
1856 }
1857
1858 static void sendBufferNotification(int id) {
1859 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08001860 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07001861
1862 if ((a != null) && (a.mBufferNotifier != null)) {
1863 a.mBufferNotifier.onBufferAvailable(a);
1864 }
1865 }
1866 }
1867
Jason Samsb8c5a842009-07-31 20:40:47 -07001868}