blob: 3cda6de14ff55b5a03680743e47f379f9fd474f9 [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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-12-06 15:59:59 -0800516 }
Tim Murray460a0492013-11-19 12:45:54 -0800517
Tim Murray3de3dc72014-07-01 16:56:18 -0700518 if (RenderScript.sPointerSize == 8) {
519 long i[] = new long[d.length * 4];
520 for (int ct=0; ct < d.length; ct++) {
521 i[ct * 4] = d[ct].getID(mRS);
522 }
523 copy1DRangeFromUnchecked(0, mCurrentCount, i);
524 } else {
525 int i[] = new int[d.length];
526 for (int ct=0; ct < d.length; ct++) {
527 i[ct] = (int)d[ct].getID(mRS);
528 }
529 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800530 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700531 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700532 }
533
Jason Samsfb9f82c2011-01-12 14:53:25 -0800534 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800535 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800536 if (bc == null) {
537 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
538 }
Jason Sams252c0782011-01-11 17:42:52 -0800539 switch (bc) {
540 case ALPHA_8:
541 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
542 throw new RSIllegalArgumentException("Allocation kind is " +
543 mType.getElement().mKind + ", type " +
544 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700545 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800546 " bytes, passed bitmap was " + bc);
547 }
548 break;
549 case ARGB_8888:
550 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700551 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800552 throw new RSIllegalArgumentException("Allocation kind is " +
553 mType.getElement().mKind + ", type " +
554 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700555 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800556 " bytes, passed bitmap was " + bc);
557 }
558 break;
559 case RGB_565:
560 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700561 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800562 throw new RSIllegalArgumentException("Allocation kind is " +
563 mType.getElement().mKind + ", type " +
564 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700565 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800566 " bytes, passed bitmap was " + bc);
567 }
568 break;
569 case ARGB_4444:
570 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700571 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800572 throw new RSIllegalArgumentException("Allocation kind is " +
573 mType.getElement().mKind + ", type " +
574 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700575 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800576 " bytes, passed bitmap was " + bc);
577 }
578 break;
579
580 }
Jason Sams4ef66502010-12-10 16:03:15 -0800581 }
582
Jason Samsfb9f82c2011-01-12 14:53:25 -0800583 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700584 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800585 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
586 }
587 }
588
Jason Sams3042d262013-11-25 18:28:33 -0800589 private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
590 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
591 mRS.validate();
592 if (mCurrentDimZ > 0) {
593 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
594 } else if (mCurrentDimY > 0) {
595 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
596 } else {
597 copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
598 }
599 Trace.traceEnd(RenderScript.TRACE_TAG);
600 }
601
602 /**
603 * Copy into this Allocation from an array. This method does not guarantee
604 * that the Allocation is compatible with the input buffer; it copies memory
605 * without reinterpretation.
606 *
607 * @param array The source data array
608 */
609 public void copyFromUnchecked(Object array) {
610 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
611 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
612 java.lang.reflect.Array.getLength(array));
613 Trace.traceEnd(RenderScript.TRACE_TAG);
614 }
615
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700616 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700617 * Copy into this Allocation from an array. This method does not guarantee
618 * that the Allocation is compatible with the input buffer; it copies memory
619 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800620 *
621 * @param d the source data array
622 */
623 public void copyFromUnchecked(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800624 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800625 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700626
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700627 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700628 * Copy into this Allocation from an array. This method does not guarantee
629 * that the Allocation is compatible with the input buffer; it copies memory
630 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800631 *
632 * @param d the source data array
633 */
634 public void copyFromUnchecked(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800635 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800636 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700637
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700638 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700639 * Copy into this Allocation from an array. This method does not guarantee
640 * that the Allocation is compatible with the input buffer; it copies memory
641 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800642 *
643 * @param d the source data array
644 */
645 public void copyFromUnchecked(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800646 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800647 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700648
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700649 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700650 * Copy into this Allocation from an array. This method does not guarantee
651 * that the Allocation is compatible with the input buffer; it copies memory
652 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800653 *
654 * @param d the source data array
655 */
656 public void copyFromUnchecked(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800657 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800658 }
659
Tim Murray6d7a53c2013-05-23 16:59:23 -0700660
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700661 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700662 * Copy into this Allocation from an array. This variant is type checked
663 * and will generate exceptions if the Allocation's {@link
Jason Sams3042d262013-11-25 18:28:33 -0800664 * android.renderscript.Element} does not match the array's
665 * primitive type.
666 *
Ying Wang16229812013-11-26 15:45:12 -0800667 * @param array The source data array
Jason Sams3042d262013-11-25 18:28:33 -0800668 */
669 public void copyFrom(Object array) {
670 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
671 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
672 java.lang.reflect.Array.getLength(array));
673 Trace.traceEnd(RenderScript.TRACE_TAG);
674 }
675
676 /**
677 * Copy into this Allocation from an array. This variant is type checked
678 * and will generate exceptions if the Allocation's {@link
Tim Murrayc11e25c2013-04-09 11:01:01 -0700679 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800680 *
681 * @param d the source data array
682 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800683 public void copyFrom(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800684 validateIsInt32();
685 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800686 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800687
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700688 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700689 * Copy into this Allocation from an array. This variant is type checked
690 * and will generate exceptions if the Allocation's {@link
691 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800692 *
693 * @param d the source data array
694 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800695 public void copyFrom(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800696 validateIsInt16();
697 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800698 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800699
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700700 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700701 * Copy into this Allocation from an array. This variant is type checked
702 * and will generate exceptions if the Allocation's {@link
703 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800704 *
705 * @param d the source data array
706 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800707 public void copyFrom(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800708 validateIsInt8();
709 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800710 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800711
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700712 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700713 * Copy into this Allocation from an array. This variant is type checked
714 * and will generate exceptions if the Allocation's {@link
715 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800716 *
717 * @param d the source data array
718 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800719 public void copyFrom(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800720 validateIsFloat32();
721 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800722 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800723
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700724 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700725 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
726 * height, width, and format of the bitmap must match the existing
727 * allocation.
728 *
729 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
730 * android.graphics.Bitmap} used to create the Allocation with {@link
731 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
732 * this will synchronize the Allocation with the latest data from the {@link
733 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800734 *
735 * @param b the source bitmap
736 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800737 public void copyFrom(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700738 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsfb9f82c2011-01-12 14:53:25 -0800739 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -0800740 if (b.getConfig() == null) {
741 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
742 Canvas c = new Canvas(newBitmap);
743 c.drawBitmap(b, 0, 0, null);
744 copyFrom(newBitmap);
745 return;
746 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800747 validateBitmapSize(b);
748 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700749 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700750 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700751 }
752
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700753 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700754 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700755 * must be identical.
756 *
757 * @param a the source allocation
758 */
759 public void copyFrom(Allocation a) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700760 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Tim Murrayf671fb02012-10-03 13:50:05 -0700761 mRS.validate();
762 if (!mType.equals(a.getType())) {
763 throw new RSIllegalArgumentException("Types of allocations must match.");
764 }
765 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700766 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700767 }
768
Tim Murrayf671fb02012-10-03 13:50:05 -0700769 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700770 * This is only intended to be used by auto-generated code reflected from
771 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800772 *
773 * @param xoff
774 * @param fp
775 */
Jason Sams21b41032011-01-16 15:05:41 -0800776 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800777 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700778 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700779 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700780 int data_length = fp.getPos();
Jason Samsa70f4162010-03-26 15:33:42 -0700781
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700782 int count = data_length / eSize;
783 if ((eSize * count) != data_length) {
784 throw new RSIllegalArgumentException("Field packer length " + data_length +
Jason Samsa70f4162010-03-26 15:33:42 -0700785 " not divisible by element size " + eSize + ".");
786 }
Jason Samsba862d12011-07-07 15:24:42 -0700787 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700788 }
789
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700790 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700791 * This is only intended to be used by auto-generated code reflected from
792 * the RenderScript script files.
Jason Samsfa445b92011-01-07 17:00:07 -0800793 *
794 * @param xoff
795 * @param component_number
796 * @param fp
797 */
Jason Sams21b41032011-01-16 15:05:41 -0800798 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800799 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700800 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800801 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700802 }
803 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800804 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700805 }
806
807 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700808 int data_length = fp.getPos();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700809 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800810 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700811
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700812 if (data_length != eSize) {
813 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700814 " does not match component size " + eSize + ".");
815 }
816
Jason Sams48fe5342011-07-08 13:52:30 -0700817 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700818 component_number, data, data_length);
Jason Samsa70f4162010-03-26 15:33:42 -0700819 }
820
Jason Sams768bc022009-09-21 19:41:04 -0700821 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800822 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700823 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800824 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700825 }
826 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800827 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700828 }
Jason Samsba862d12011-07-07 15:24:42 -0700829 if((off + count) > mCurrentCount) {
830 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700831 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700832 }
Jason Samsba862d12011-07-07 15:24:42 -0700833 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800834 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700835 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700836 }
837
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700838 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700839 * Generate a mipmap chain. This is only valid if the Type of the Allocation
840 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800841 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700842 * <p>This function will generate a complete set of mipmaps from the top
843 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800844 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700845 * <p>If the Allocation is also using other memory spaces, a call to {@link
846 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800847 */
848 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700849 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800850 }
851
Jason Sams3042d262013-11-25 18:28:33 -0800852 private void copy1DRangeFromUnchecked(int off, int count, Object array,
853 Element.DataType dt, int arrayLen) {
854 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
855 final int dataSize = mType.mElement.getBytesSize() * count;
856 data1DChecks(off, count, arrayLen * dt.mSize, dataSize);
857 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt);
858 Trace.traceEnd(RenderScript.TRACE_TAG);
859 }
860
861 /**
862 * Copy an array into part of this Allocation. This method does not
863 * guarantee that the Allocation is compatible with the input buffer.
864 *
865 * @param off The offset of the first element to be copied.
866 * @param count The number of elements to be copied.
867 * @param array The source data array
868 */
869 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
870 copy1DRangeFromUnchecked(off, count, array,
871 validateObjectIsPrimitiveArray(array, false),
872 java.lang.reflect.Array.getLength(array));
873 }
874
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700875 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700876 * Copy an array into part of this Allocation. This method does not
877 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800878 *
879 * @param off The offset of the first element to be copied.
880 * @param count The number of elements to be copied.
881 * @param d the source data array
882 */
883 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800884 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700885 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700886
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700887 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700888 * Copy an array into part of this Allocation. This method does not
889 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800890 *
891 * @param off The offset of the first element to be copied.
892 * @param count The number of elements to be copied.
893 * @param d the source data array
894 */
895 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800896 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700897 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700898
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700899 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700900 * Copy an array into part of this Allocation. This method does not
901 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800902 *
903 * @param off The offset of the first element to be copied.
904 * @param count The number of elements to be copied.
905 * @param d the source data array
906 */
907 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800908 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700909 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700910
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700911 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700912 * Copy an array into part of this Allocation. This method does not
913 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800914 *
915 * @param off The offset of the first element to be copied.
916 * @param count The number of elements to be copied.
917 * @param d the source data array
918 */
919 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800920 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
921 }
922
923
924 /**
925 * Copy an array into part of this Allocation. This variant is type checked
926 * and will generate exceptions if the Allocation type does not
927 * match the component type of the array passed in.
928 *
929 * @param off The offset of the first element to be copied.
930 * @param count The number of elements to be copied.
931 * @param array The source data array.
932 */
933 public void copy1DRangeFrom(int off, int count, Object array) {
934 copy1DRangeFromUnchecked(off, count, array,
935 validateObjectIsPrimitiveArray(array, true),
936 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -0700937 }
938
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700939 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700940 * Copy an array into part of this Allocation. This variant is type checked
941 * and will generate exceptions if the Allocation type is not a 32 bit
942 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800943 *
944 * @param off The offset of the first element to be copied.
945 * @param count The number of elements to be copied.
946 * @param d the source data array
947 */
Jason Samsb97b2512011-01-16 15:04:08 -0800948 public void copy1DRangeFrom(int off, int count, int[] d) {
949 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -0800950 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800951 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800952
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700953 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700954 * Copy an array into part of this Allocation. This variant is type checked
955 * and will generate exceptions if the Allocation type is not a 16 bit
956 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800957 *
958 * @param off The offset of the first element to be copied.
959 * @param count The number of elements to be copied.
960 * @param d the source data array
961 */
Jason Samsb97b2512011-01-16 15:04:08 -0800962 public void copy1DRangeFrom(int off, int count, short[] d) {
963 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -0800964 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800965 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800966
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700967 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700968 * Copy an array into part of this Allocation. This variant is type checked
969 * and will generate exceptions if the Allocation type is not an 8 bit
970 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800971 *
972 * @param off The offset of the first element to be copied.
973 * @param count The number of elements to be copied.
974 * @param d the source data array
975 */
Jason Samsb97b2512011-01-16 15:04:08 -0800976 public void copy1DRangeFrom(int off, int count, byte[] d) {
977 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -0800978 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800979 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800980
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700981 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700982 * Copy an array into part of this Allocation. This variant is type checked
983 * and will generate exceptions if the Allocation type is not a 32 bit float
984 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800985 *
986 * @param off The offset of the first element to be copied.
987 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700988 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800989 */
Jason Samsb97b2512011-01-16 15:04:08 -0800990 public void copy1DRangeFrom(int off, int count, float[] d) {
991 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -0800992 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800993 }
Jason Sams3042d262013-11-25 18:28:33 -0800994
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700995 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700996 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700997 *
998 * @param off The offset of the first element to be copied.
999 * @param count The number of elements to be copied.
1000 * @param data the source data allocation.
1001 * @param dataOff off The offset of the first element in data to
1002 * be copied.
1003 */
1004 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001005 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -07001006 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001007 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001008 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001009 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001010 }
1011
Jason Samsfb9f82c2011-01-12 14:53:25 -08001012 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001013 if (mAdaptedAllocation != null) {
1014
1015 } else {
1016
1017 if (xoff < 0 || yoff < 0) {
1018 throw new RSIllegalArgumentException("Offset cannot be negative.");
1019 }
1020 if (h < 0 || w < 0) {
1021 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1022 }
1023 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1024 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1025 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001026 }
1027 }
Jason Sams768bc022009-09-21 19:41:04 -07001028
Jason Sams3042d262013-11-25 18:28:33 -08001029 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1030 Element.DataType dt, int arrayLen) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001031 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001032 mRS.validate();
1033 validate2DRange(xoff, yoff, w, h);
Jason Sams3042d262013-11-25 18:28:33 -08001034 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1035 array, arrayLen * dt.mSize, dt);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001036 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001037 }
1038
Jason Sams3042d262013-11-25 18:28:33 -08001039 /**
1040 * Copy from an array into a rectangular region in this Allocation. The
1041 * array is assumed to be tightly packed.
1042 *
1043 * @param xoff X offset of the region to update in this Allocation
1044 * @param yoff Y offset of the region to update in this Allocation
1045 * @param w Width of the region to update
1046 * @param h Height of the region to update
Ying Wang16229812013-11-26 15:45:12 -08001047 * @param array Data to be placed into the Allocation
Jason Sams3042d262013-11-25 18:28:33 -08001048 */
1049 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
1050 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1051 copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
1052 validateObjectIsPrimitiveArray(array, true),
1053 java.lang.reflect.Array.getLength(array));
Tim Murray6d7a53c2013-05-23 16:59:23 -07001054 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001055 }
1056
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001057 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001058 * Copy from an array into a rectangular region in this Allocation. The
1059 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -08001060 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001061 * @param xoff X offset of the region to update in this Allocation
1062 * @param yoff Y offset of the region to update in this Allocation
1063 * @param w Width of the region to update
1064 * @param h Height of the region to update
1065 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001066 */
1067 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001068 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001069 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1070 Element.DataType.SIGNED_8, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001071 }
1072
Tim Murrayc11e25c2013-04-09 11:01:01 -07001073 /**
1074 * Copy from an array into a rectangular region in this Allocation. The
1075 * array is assumed to be tightly packed.
1076 *
1077 * @param xoff X offset of the region to update in this Allocation
1078 * @param yoff Y offset of the region to update in this Allocation
1079 * @param w Width of the region to update
1080 * @param h Height of the region to update
1081 * @param data to be placed into the Allocation
1082 */
Jason Samsf7086092011-01-12 13:28:37 -08001083 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001084 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001085 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1086 Element.DataType.SIGNED_16, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001087 }
1088
Tim Murrayc11e25c2013-04-09 11:01:01 -07001089 /**
1090 * Copy from an array into a rectangular region in this Allocation. The
1091 * array is assumed to be tightly packed.
1092 *
1093 * @param xoff X offset of the region to update in this Allocation
1094 * @param yoff Y offset of the region to update in this Allocation
1095 * @param w Width of the region to update
1096 * @param h Height of the region to update
1097 * @param data to be placed into the Allocation
1098 */
Jason Samsf7086092011-01-12 13:28:37 -08001099 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001100 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001101 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1102 Element.DataType.SIGNED_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001103 }
1104
Tim Murrayc11e25c2013-04-09 11:01:01 -07001105 /**
1106 * Copy from an array into a rectangular region in this Allocation. The
1107 * array is assumed to be tightly packed.
1108 *
1109 * @param xoff X offset of the region to update in this Allocation
1110 * @param yoff Y offset of the region to update in this Allocation
1111 * @param w Width of the region to update
1112 * @param h Height of the region to update
1113 * @param data to be placed into the Allocation
1114 */
Jason Samsf7086092011-01-12 13:28:37 -08001115 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001116 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001117 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1118 Element.DataType.FLOAT_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001119 }
1120
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001121 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001122 * Copy a rectangular region from an Allocation into a rectangular region in
1123 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001124 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001125 * @param xoff X offset of the region in this Allocation
1126 * @param yoff Y offset of the region in this Allocation
1127 * @param w Width of the region to update.
1128 * @param h Height of the region to update.
1129 * @param data source Allocation.
1130 * @param dataXoff X offset in source Allocation
1131 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001132 */
1133 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1134 Allocation data, int dataXoff, int dataYoff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001135 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001136 mRS.validate();
1137 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -07001138 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -07001139 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001140 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -07001141 data.mSelectedLOD, data.mSelectedFace.mID);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001142 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001143 }
1144
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001145 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001146 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1147 * and width of the update will use the height and width of the {@link
1148 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001149 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001150 * @param xoff X offset of the region to update in this Allocation
1151 * @param yoff Y offset of the region to update in this Allocation
1152 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001153 */
1154 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001155 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Jason Samsfa445b92011-01-07 17:00:07 -08001156 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001157 if (data.getConfig() == null) {
1158 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1159 Canvas c = new Canvas(newBitmap);
1160 c.drawBitmap(data, 0, 0, null);
1161 copy2DRangeFrom(xoff, yoff, newBitmap);
Jason Samsb05d6892013-04-09 15:59:24 -07001162 return;
Tim Murrayabd5db92013-02-28 11:45:22 -08001163 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001164 validateBitmapFormat(data);
1165 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -07001166 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001167 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001168 }
1169
Jason Samsb05d6892013-04-09 15:59:24 -07001170 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1171 if (mAdaptedAllocation != null) {
1172
1173 } else {
1174
1175 if (xoff < 0 || yoff < 0 || zoff < 0) {
1176 throw new RSIllegalArgumentException("Offset cannot be negative.");
1177 }
1178 if (h < 0 || w < 0 || d < 0) {
1179 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1180 }
1181 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1182 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1183 }
1184 }
1185 }
1186
1187 /**
1188 * @hide
1189 *
1190 */
Jason Sams3042d262013-11-25 18:28:33 -08001191 private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1192 Object array, Element.DataType dt, int arrayLen) {
1193 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
Jason Samsb05d6892013-04-09 15:59:24 -07001194 mRS.validate();
1195 validate3DRange(xoff, yoff, zoff, w, h, d);
Jason Sams3042d262013-11-25 18:28:33 -08001196 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1197 array, arrayLen * dt.mSize, dt);
1198 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb05d6892013-04-09 15:59:24 -07001199 }
1200
1201 /**
1202 * @hide
Jason Samsb05d6892013-04-09 15:59:24 -07001203 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001204 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001205 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001206 * @param xoff X offset of the region to update in this Allocation
1207 * @param yoff Y offset of the region to update in this Allocation
1208 * @param zoff Z offset of the region to update in this Allocation
1209 * @param w Width of the region to update
1210 * @param h Height of the region to update
1211 * @param d Depth of the region to update
Jason Samsb05d6892013-04-09 15:59:24 -07001212 * @param data to be placed into the allocation
1213 */
Jason Sams3042d262013-11-25 18:28:33 -08001214 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1215 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
1216 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
1217 validateObjectIsPrimitiveArray(array, true),
1218 java.lang.reflect.Array.getLength(array));
1219 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb05d6892013-04-09 15:59:24 -07001220 }
1221
1222 /**
1223 * @hide
1224 * Copy a rectangular region into the allocation from another
1225 * allocation.
1226 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001227 * @param xoff X offset of the region to update in this Allocation
1228 * @param yoff Y offset of the region to update in this Allocation
1229 * @param zoff Z offset of the region to update in this Allocation
1230 * @param w Width of the region to update.
1231 * @param h Height of the region to update.
1232 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001233 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001234 * @param dataXoff X offset of the region in the source Allocation
1235 * @param dataYoff Y offset of the region in the source Allocation
1236 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001237 */
1238 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1239 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1240 mRS.validate();
1241 validate3DRange(xoff, yoff, zoff, w, h, d);
1242 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1243 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1244 data.mSelectedLOD);
1245 }
1246
Jason Samsfa445b92011-01-07 17:00:07 -08001247
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001248 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001249 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1250 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001251 *
1252 * @param b The bitmap to be set from the Allocation.
1253 */
Jason Samsfa445b92011-01-07 17:00:07 -08001254 public void copyTo(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001255 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsfb9f82c2011-01-12 14:53:25 -08001256 mRS.validate();
1257 validateBitmapFormat(b);
1258 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -07001259 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001260 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001261 }
1262
Jason Sams3042d262013-11-25 18:28:33 -08001263 private void copyTo(Object array, Element.DataType dt, int arrayLen) {
1264 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1265 mRS.validate();
1266 mRS.nAllocationRead(getID(mRS), array, dt);
1267 Trace.traceEnd(RenderScript.TRACE_TAG);
1268 }
1269
1270 /**
1271 * Copy from the Allocation into an array. The array must be at
1272 * least as large as the Allocation. The
1273 * {@link android.renderscript.Element} must match the component
1274 * type of the array passed in.
1275 *
1276 * @param array The array to be set from the Allocation.
1277 */
1278 public void copyTo(Object array) {
1279 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1280 java.lang.reflect.Array.getLength(array));
1281 }
1282
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001283 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001284 * Copy from the Allocation into a byte array. The array must be at least
1285 * as large as the Allocation. The allocation must be of an 8 bit integer
1286 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001287 *
1288 * @param d The array to be set from the Allocation.
1289 */
Jason Samsfa445b92011-01-07 17:00:07 -08001290 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001291 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001292 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001293 }
1294
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001295 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001296 * Copy from the Allocation into a short array. The array must be at least
1297 * as large as the Allocation. The allocation must be of an 16 bit integer
1298 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001299 *
1300 * @param d The array to be set from the Allocation.
1301 */
Jason Samsfa445b92011-01-07 17:00:07 -08001302 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001303 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001304 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001305 }
1306
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001307 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001308 * Copy from the Allocation into a int array. The array must be at least as
1309 * large as the Allocation. The allocation must be of an 32 bit integer
1310 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001311 *
1312 * @param d The array to be set from the Allocation.
1313 */
Jason Samsfa445b92011-01-07 17:00:07 -08001314 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001315 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001316 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001317 }
1318
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001319 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001320 * Copy from the Allocation into a float array. The array must be at least
1321 * as large as the Allocation. The allocation must be of an 32 bit float
1322 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001323 *
1324 * @param d The array to be set from the Allocation.
1325 */
Jason Samsfa445b92011-01-07 17:00:07 -08001326 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001327 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001328 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001329 }
1330
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001331 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001332 * Resize a 1D allocation. The contents of the allocation are preserved.
1333 * If new elements are allocated objects are created with null contents and
1334 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001335 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001336 * <p>If the new region is smaller the references of any objects outside the
1337 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001338 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001339 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001340 *
1341 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001342 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001343 * @deprecated RenderScript objects should be immutable once created. The
Tim Murraycd38b762014-08-13 13:20:25 -07001344 * replacement is to create a new allocation and copy the contents. This
1345 * function will throw an exception if API 21 or higher is used.
Jason Samsf7086092011-01-12 13:28:37 -08001346 */
Jason Sams31a7e422010-10-26 13:09:17 -07001347 public synchronized void resize(int dimX) {
Tim Murraycd38b762014-08-13 13:20:25 -07001348 if (mRS.getApplicationContext().getApplicationInfo().targetSdkVersion >= 21) {
1349 throw new RSRuntimeException("Resize is not allowed in API 21+.");
1350 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001351 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001352 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001353 }
Jason Samse07694b2012-04-03 15:36:36 -07001354 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001355 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001356
Tim Murray460a0492013-11-19 12:45:54 -08001357 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001358 mType = new Type(typeID, mRS);
1359 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001360 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001361 }
1362
Jason Samsb8c5a842009-07-31 20:40:47 -07001363
1364 // creation
1365
Jason Sams49a05d72010-12-29 14:31:29 -08001366 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001367 static {
1368 mBitmapOptions.inScaled = false;
1369 }
1370
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001371 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001372 * Creates a new Allocation with the given {@link
1373 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001374 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001375 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001376 * @param mips specifies desired mipmap behaviour for the
1377 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001378 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001379 * utilized
1380 */
1381 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001382 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
Jason Sams771bebb2009-12-07 12:40:12 -08001383 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001384 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001385 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001386 }
Tim Murray460a0492013-11-19 12:45:54 -08001387 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001388 if (id == 0) {
1389 throw new RSRuntimeException("Allocation creation failed.");
1390 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001391 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams857d0c72011-11-23 15:02:15 -08001392 return new Allocation(id, rs, type, usage);
1393 }
1394
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001395 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001396 * Creates an Allocation with the size specified by the type and no mipmaps
1397 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001398 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001399 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001400 * @param type renderscript type describing data layout
1401 * @param usage bit field specifying how the allocation is
1402 * utilized
1403 *
1404 * @return allocation
1405 */
Jason Samse5d37122010-12-16 00:33:33 -08001406 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1407 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1408 }
1409
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001410 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001411 * Creates an Allocation for use by scripts with a given {@link
1412 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001413 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001414 * @param rs Context to which the Allocation will belong.
1415 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001416 *
1417 * @return allocation
1418 */
Jason Sams5476b452010-12-08 16:14:36 -08001419 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001420 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001421 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001422
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001423 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001424 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001425 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001426 * @param rs Context to which the Allocation will belong.
1427 * @param e Element to use in the Allocation
1428 * @param count the number of Elements in the Allocation
1429 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001430 * utilized
1431 *
1432 * @return allocation
1433 */
Jason Sams5476b452010-12-08 16:14:36 -08001434 static public Allocation createSized(RenderScript rs, Element e,
1435 int count, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001436 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
Jason Sams771bebb2009-12-07 12:40:12 -08001437 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001438 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001439 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001440 Type t = b.create();
1441
Tim Murray460a0492013-11-19 12:45:54 -08001442 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001443 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001444 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001445 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001446 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001447 return new Allocation(id, rs, t, usage);
1448 }
1449
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001450 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001451 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001452 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001453 * @param rs Context to which the Allocation will belong.
1454 * @param e Element to use in the Allocation
1455 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001456 *
1457 * @return allocation
1458 */
Jason Sams5476b452010-12-08 16:14:36 -08001459 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001460 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001461 }
1462
Jason Sams49a05d72010-12-29 14:31:29 -08001463 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001464 final Bitmap.Config bc = b.getConfig();
1465 if (bc == Bitmap.Config.ALPHA_8) {
1466 return Element.A_8(rs);
1467 }
1468 if (bc == Bitmap.Config.ARGB_4444) {
1469 return Element.RGBA_4444(rs);
1470 }
1471 if (bc == Bitmap.Config.ARGB_8888) {
1472 return Element.RGBA_8888(rs);
1473 }
1474 if (bc == Bitmap.Config.RGB_565) {
1475 return Element.RGB_565(rs);
1476 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001477 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001478 }
1479
Jason Sams49a05d72010-12-29 14:31:29 -08001480 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001481 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001482 Element e = elementFromBitmap(rs, b);
1483 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001484 tb.setX(b.getWidth());
1485 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001486 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001487 return tb.create();
1488 }
1489
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001490 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001491 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001492 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001493 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001494 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001495 * @param mips specifies desired mipmap behaviour for the
1496 * allocation
1497 * @param usage bit field specifying how the allocation is
1498 * utilized
1499 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001500 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001501 *
1502 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001503 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001504 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001505 int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001506 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
Jason Sams771bebb2009-12-07 12:40:12 -08001507 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001508
1509 // WAR undocumented color formats
1510 if (b.getConfig() == null) {
1511 if ((usage & USAGE_SHARED) != 0) {
1512 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1513 }
1514 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1515 Canvas c = new Canvas(newBitmap);
1516 c.drawBitmap(b, 0, 0, null);
1517 return createFromBitmap(rs, newBitmap, mips, usage);
1518 }
1519
Jason Sams5476b452010-12-08 16:14:36 -08001520 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001521
Tim Murraya3145512012-12-04 17:59:29 -08001522 // enable optimized bitmap path only with no mipmap and script-only usage
1523 if (mips == MipmapControl.MIPMAP_NONE &&
1524 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Tim Murray78e64942013-04-09 17:28:56 -07001525 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
Tim Murray460a0492013-11-19 12:45:54 -08001526 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08001527 if (id == 0) {
1528 throw new RSRuntimeException("Load failed.");
1529 }
1530
1531 // keep a reference to the Bitmap around to prevent GC
1532 Allocation alloc = new Allocation(id, rs, t, usage);
1533 alloc.setBitmap(b);
1534 return alloc;
1535 }
1536
Jason Sams9bf18922013-04-13 19:48:36 -07001537
Tim Murray460a0492013-11-19 12:45:54 -08001538 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001539 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001540 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001541 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001542 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001543 return new Allocation(id, rs, t, usage);
1544 }
1545
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001546 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001547 * Returns the handle to a raw buffer that is being managed by the screen
1548 * compositor. This operation is only valid for Allocations with {@link
1549 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001550 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001551 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001552 *
1553 */
1554 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08001555 if ((mUsage & USAGE_IO_INPUT) == 0) {
1556 throw new RSInvalidStateException("Allocation is not a surface texture.");
1557 }
1558 return mRS.nAllocationGetSurface(getID(mRS));
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001559 }
1560
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001561 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001562 * Associate a {@link android.view.Surface} with this Allocation. This
1563 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001564 *
1565 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001566 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001567 public void setSurface(Surface sur) {
1568 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001569 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1570 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1571 }
1572
Jason Samse07694b2012-04-03 15:36:36 -07001573 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001574 }
1575
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001576 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001577 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08001578 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001579 * <p>With target API version 18 or greater, this Allocation will be created
1580 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
1581 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
1582 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001583 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001584 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001585 * @param b bitmap source for the allocation data
1586 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001587 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001588 *
1589 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001590 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08001591 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1592 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07001593 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08001594 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001595 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1596 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001597 }
1598
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001599 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001600 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1601 * containing the horizontal list of cube faces. Each face must be a square,
1602 * have the same size as all other faces, and have a width that is a power
1603 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001604 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001605 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001606 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001607 * format: right, left, top, bottom, front, back
1608 * @param mips specifies desired mipmap behaviour for the cubemap
1609 * @param usage bit field specifying how the cubemap is utilized
1610 *
1611 * @return allocation containing cubemap data
1612 *
1613 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001614 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001615 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001616 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001617 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001618
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001619 int height = b.getHeight();
1620 int width = b.getWidth();
1621
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001622 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001623 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1624 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001625 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001626 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001627 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001628 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001629 if (!isPow2) {
1630 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1631 }
1632
1633 Element e = elementFromBitmap(rs, b);
1634 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001635 tb.setX(height);
1636 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001637 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001638 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001639 Type t = tb.create();
1640
Tim Murray460a0492013-11-19 12:45:54 -08001641 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001642 if(id == 0) {
1643 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1644 }
Jason Sams5476b452010-12-08 16:14:36 -08001645 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001646 }
1647
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001648 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001649 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
1650 * from a {@link android.graphics.Bitmap} containing the horizontal list of
1651 * cube faces. Each face must be a square, have the same size as all other
1652 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001653 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001654 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001655 * @param b bitmap with cubemap faces layed out in the following
1656 * format: right, left, top, bottom, front, back
1657 *
1658 * @return allocation containing cubemap data
1659 *
1660 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001661 static public Allocation createCubemapFromBitmap(RenderScript rs,
1662 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001663 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001664 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001665 }
1666
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001667 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001668 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
1669 * objects containing the cube faces. Each face must be a square, have the
1670 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001671 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001672 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001673 * @param xpos cubemap face in the positive x direction
1674 * @param xneg cubemap face in the negative x direction
1675 * @param ypos cubemap face in the positive y direction
1676 * @param yneg cubemap face in the negative y direction
1677 * @param zpos cubemap face in the positive z direction
1678 * @param zneg cubemap face in the negative z direction
1679 * @param mips specifies desired mipmap behaviour for the cubemap
1680 * @param usage bit field specifying how the cubemap is utilized
1681 *
1682 * @return allocation containing cubemap data
1683 *
1684 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001685 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1686 Bitmap xpos,
1687 Bitmap xneg,
1688 Bitmap ypos,
1689 Bitmap yneg,
1690 Bitmap zpos,
1691 Bitmap zneg,
1692 MipmapControl mips,
1693 int usage) {
1694 int height = xpos.getHeight();
1695 if (xpos.getWidth() != height ||
1696 xneg.getWidth() != height || xneg.getHeight() != height ||
1697 ypos.getWidth() != height || ypos.getHeight() != height ||
1698 yneg.getWidth() != height || yneg.getHeight() != height ||
1699 zpos.getWidth() != height || zpos.getHeight() != height ||
1700 zneg.getWidth() != height || zneg.getHeight() != height) {
1701 throw new RSIllegalArgumentException("Only square cube map faces supported");
1702 }
1703 boolean isPow2 = (height & (height - 1)) == 0;
1704 if (!isPow2) {
1705 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1706 }
1707
1708 Element e = elementFromBitmap(rs, xpos);
1709 Type.Builder tb = new Type.Builder(rs, e);
1710 tb.setX(height);
1711 tb.setY(height);
1712 tb.setFaces(true);
1713 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1714 Type t = tb.create();
1715 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1716
1717 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001718 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001719 adapter.copyFrom(xpos);
1720 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1721 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001722 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001723 adapter.copyFrom(ypos);
1724 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1725 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001726 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001727 adapter.copyFrom(zpos);
1728 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1729 adapter.copyFrom(zneg);
1730
1731 return cubemap;
1732 }
1733
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001734 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001735 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
1736 * from 6 {@link android.graphics.Bitmap} objects containing the cube
1737 * faces. Each face must be a square, have the same size as all other faces,
1738 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001739 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001740 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001741 * @param xpos cubemap face in the positive x direction
1742 * @param xneg cubemap face in the negative x direction
1743 * @param ypos cubemap face in the positive y direction
1744 * @param yneg cubemap face in the negative y direction
1745 * @param zpos cubemap face in the positive z direction
1746 * @param zneg cubemap face in the negative z direction
1747 *
1748 * @return allocation containing cubemap data
1749 *
1750 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001751 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1752 Bitmap xpos,
1753 Bitmap xneg,
1754 Bitmap ypos,
1755 Bitmap yneg,
1756 Bitmap zpos,
1757 Bitmap zneg) {
1758 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1759 zpos, zneg, MipmapControl.MIPMAP_NONE,
1760 USAGE_GRAPHICS_TEXTURE);
1761 }
1762
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001763 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001764 * Creates an Allocation from the Bitmap referenced
1765 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001766 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001767 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001768 * @param res application resources
1769 * @param id resource id to load the data from
1770 * @param mips specifies desired mipmap behaviour for the
1771 * allocation
1772 * @param usage bit field specifying how the allocation is
1773 * utilized
1774 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001775 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001776 *
1777 */
Jason Sams5476b452010-12-08 16:14:36 -08001778 static public Allocation createFromBitmapResource(RenderScript rs,
1779 Resources res,
1780 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001781 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001782 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001783
Jason Sams771bebb2009-12-07 12:40:12 -08001784 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07001785 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
1786 throw new RSIllegalArgumentException("Unsupported usage specified.");
1787 }
Jason Sams5476b452010-12-08 16:14:36 -08001788 Bitmap b = BitmapFactory.decodeResource(res, id);
1789 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1790 b.recycle();
1791 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001792 }
1793
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001794 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001795 * Creates a non-mipmapped Allocation to use as a graphics texture from the
1796 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001797 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001798 * <p>With target API version 18 or greater, this allocation will be created
1799 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
1800 * target API version 17 or lower, this allocation will be created with
1801 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08001802 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001803 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001804 * @param res application resources
1805 * @param id resource id to load the data from
1806 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001807 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001808 *
1809 */
Jason Sams5476b452010-12-08 16:14:36 -08001810 static public Allocation createFromBitmapResource(RenderScript rs,
1811 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001812 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08001813 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1814 return createFromBitmapResource(rs, res, id,
1815 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07001816 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08001817 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001818 return createFromBitmapResource(rs, res, id,
1819 MipmapControl.MIPMAP_NONE,
1820 USAGE_GRAPHICS_TEXTURE);
1821 }
1822
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001823 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001824 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001825 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001826 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001827 * @param str string to create the allocation from
1828 * @param usage bit field specifying how the allocaiton is
1829 * utilized
1830 *
1831 */
Jason Sams5476b452010-12-08 16:14:36 -08001832 static public Allocation createFromString(RenderScript rs,
1833 String str,
1834 int usage) {
1835 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001836 byte[] allocArray = null;
1837 try {
1838 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001839 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001840 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001841 return alloc;
1842 }
1843 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001844 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001845 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001846 }
Jason Sams739c8262013-04-11 18:07:52 -07001847
1848 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001849 * Interface to handle notification when new buffers are available via
1850 * {@link #USAGE_IO_INPUT}. An application will receive one notification
1851 * when a buffer is available. Additional buffers will not trigger new
1852 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07001853 */
Jason Sams42ef2382013-08-29 13:30:59 -07001854 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07001855 public void onBufferAvailable(Allocation a);
1856 }
1857
1858 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001859 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07001860 *
Jason Sams42ef2382013-08-29 13:30:59 -07001861 * @param callback instance of the OnBufferAvailableListener
1862 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07001863 */
Jason Sams42ef2382013-08-29 13:30:59 -07001864 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07001865 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08001866 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07001867 mBufferNotifier = callback;
1868 }
1869 }
1870
Tim Murrayb730d862014-08-18 16:14:24 -07001871 static void sendBufferNotification(long id) {
Jason Sams739c8262013-04-11 18:07:52 -07001872 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08001873 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07001874
1875 if ((a != null) && (a.mBufferNotifier != null)) {
1876 a.mBufferNotifier.onBufferAvailable(a);
1877 }
1878 }
1879 }
1880
Jason Samsb8c5a842009-07-31 20:40:47 -07001881}