blob: c2bab91c8fbd8587cb10f55b93974a1c7f98fb8d [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 Samsb8c5a842009-07-31 20:40:47 -070019import java.io.IOException;
20import java.io.InputStream;
Jason Sams739c8262013-04-11 18:07:52 -070021import java.util.HashMap;
Jason Samsb8c5a842009-07-31 20:40:47 -070022import android.content.res.Resources;
Romain Guy650a3eb2009-08-31 14:06:43 -070023import android.content.res.AssetManager;
Jason Samsb8c5a842009-07-31 20:40:47 -070024import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
Jason Samsfb9aa9f2012-03-28 15:30:07 -070026import android.view.Surface;
Jason Samsb8c5a842009-07-31 20:40:47 -070027import android.util.Log;
Romain Guy650a3eb2009-08-31 14:06:43 -070028import android.util.TypedValue;
Tim Murrayabd5db92013-02-28 11:45:22 -080029import android.graphics.Canvas;
Tim Murray6d7a53c2013-05-23 16:59:23 -070030import android.os.Trace;
Jason Samsb8c5a842009-07-31 20:40:47 -070031
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070032/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070033 * <p> This class provides the primary method through which data is passed to
34 * and from RenderScript kernels. An Allocation provides the backing store for
35 * a given {@link android.renderscript.Type}. </p>
Jason Samsa23d4e72011-01-04 18:59:12 -080036 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070037 * <p>An Allocation also contains a set of usage flags that denote how the
38 * Allocation could be used. For example, an Allocation may have usage flags
39 * specifying that it can be used from a script as well as input to a {@link
40 * android.renderscript.Sampler}. A developer must synchronize across these
41 * different usages using {@link android.renderscript.Allocation#syncAll} in
42 * order to ensure that different users of the Allocation have a consistent view
43 * of memory. For example, in the case where an Allocation is used as the output
44 * of one kernel and as Sampler input in a later kernel, a developer must call
45 * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
46 * second kernel to ensure correctness.
Jason Samsa23d4e72011-01-04 18:59:12 -080047 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070048 * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
49 * more complex Element types, the {@link #copyFromUnchecked} methods can be
50 * used to copy from byte arrays or similar constructs.</p>
Jason Samsb8c5a842009-07-31 20:40:47 -070051 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080052 * <div class="special reference">
53 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070054 * <p>For more information about creating an application that uses RenderScript, read the
55 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080056 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070057 **/
58public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070059 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080060 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080061 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070062 Allocation mAdaptedAllocation;
Tim Murray2f2472c2013-08-22 14:55:26 -070063 int mSize;
Jason Samsba862d12011-07-07 15:24:42 -070064
65 boolean mConstrainedLOD;
66 boolean mConstrainedFace;
67 boolean mConstrainedY;
68 boolean mConstrainedZ;
Jason Sams615e7ce2012-01-13 14:01:20 -080069 boolean mReadAllowed = true;
70 boolean mWriteAllowed = true;
Jason Samsba862d12011-07-07 15:24:42 -070071 int mSelectedY;
72 int mSelectedZ;
73 int mSelectedLOD;
74 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
75
76 int mCurrentDimX;
77 int mCurrentDimY;
78 int mCurrentDimZ;
79 int mCurrentCount;
Tim Murray7a629fa2013-11-19 12:45:54 -080080 static HashMap<Long, Allocation> mAllocationMap =
81 new HashMap<Long, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070082 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070083
Jason Sams1136bb92013-11-25 18:28:33 -080084 private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
85 final Class c = d.getClass();
86 if (!c.isArray()) {
87 throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
88 }
89 final Class cmp = c.getComponentType();
90 if (!cmp.isPrimitive()) {
91 throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
92 }
93
94 if (cmp == Long.TYPE) {
95 if (checkType) {
96 validateIsInt64();
97 return mType.mElement.mType;
98 }
99 return Element.DataType.SIGNED_64;
100 }
101
102 if (cmp == Integer.TYPE) {
103 if (checkType) {
104 validateIsInt32();
105 return mType.mElement.mType;
106 }
107 return Element.DataType.SIGNED_32;
108 }
109
110 if (cmp == Short.TYPE) {
111 if (checkType) {
112 validateIsInt16();
113 return mType.mElement.mType;
114 }
115 return Element.DataType.SIGNED_16;
116 }
117
118 if (cmp == Byte.TYPE) {
119 if (checkType) {
120 validateIsInt8();
121 return mType.mElement.mType;
122 }
123 return Element.DataType.SIGNED_8;
124 }
125
126 if (cmp == Float.TYPE) {
127 if (checkType) {
128 validateIsFloat32();
129 }
130 return Element.DataType.FLOAT_32;
131 }
132
133 if (cmp == Double.TYPE) {
134 if (checkType) {
135 validateIsFloat64();
136 }
137 return Element.DataType.FLOAT_64;
138 }
139 return null;
140 }
141
142
Tim Murrayc11e25c2013-04-09 11:01:01 -0700143 /**
144 * The usage of the Allocation. These signal to RenderScript where to place
145 * the Allocation in memory.
146 *
147 */
Jason Sams5476b452010-12-08 16:14:36 -0800148
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700149 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700150 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -0800151 */
Jason Sams5476b452010-12-08 16:14:36 -0800152 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800153
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700154 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700155 * The Allocation will be used as a texture source by one or more graphics
156 * programs.
Jason Samsf7086092011-01-12 13:28:37 -0800157 *
158 */
Jason Sams5476b452010-12-08 16:14:36 -0800159 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800160
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700161 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700162 * The Allocation will be used as a graphics mesh.
163 *
164 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800165 *
166 */
Jason Sams5476b452010-12-08 16:14:36 -0800167 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800168
169
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700170 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700171 * The Allocation will be used as the source of shader constants by one or
172 * more programs.
173 *
174 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800175 *
176 */
Jason Sams5476b452010-12-08 16:14:36 -0800177 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
178
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700179 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700180 * The Allocation will be used as a target for offscreen rendering
181 *
182 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700183 *
184 */
185 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
186
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700187 /**
Jason Sams1887d522013-09-24 15:18:52 -0700188 * The Allocation will be used as a {@link android.view.Surface}
189 * consumer. This usage will cause the Allocation to be created
190 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800191 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800192 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700193 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800194
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700195 /**
Jason Sams1887d522013-09-24 15:18:52 -0700196 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700197 * producer. The dimensions and format of the {@link
Jason Sams1887d522013-09-24 15:18:52 -0700198 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700199 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800200 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800201 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700202 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700203
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700204 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700205 * The Allocation's backing store will be inherited from another object
206 * (usually a {@link android.graphics.Bitmap}); copying to or from the
207 * original source Bitmap will cause a synchronization rather than a full
208 * copy. {@link #syncAll} may also be used to synchronize the Allocation
209 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800210 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700211 * <p>This is set by default for allocations created with {@link
212 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800213 *
214 */
215 public static final int USAGE_SHARED = 0x0080;
216
217 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700218 * Controls mipmap behavior when using the bitmap creation and update
219 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800220 */
Jason Sams4ef66502010-12-10 16:03:15 -0800221 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700222 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700223 * No mipmaps will be generated and the type generated from the incoming
224 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800225 */
Jason Sams5476b452010-12-08 16:14:36 -0800226 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800227
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700228 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700229 * A full mipmap chain will be created in script memory. The Type of
230 * the Allocation will contain a full mipmap chain. On upload, the full
231 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800232 */
Jason Sams5476b452010-12-08 16:14:36 -0800233 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800234
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700235 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700236 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
237 * not contain mipmaps. On upload, the allocation data will contain a
238 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800239 */
Jason Sams5476b452010-12-08 16:14:36 -0800240 MIPMAP_ON_SYNC_TO_TEXTURE(2);
241
242 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800243 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800244 mID = id;
245 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700246 }
247
Jason Sams48fe5342011-07-08 13:52:30 -0700248
Tim Murray7a629fa2013-11-19 12:45:54 -0800249 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700250 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700251 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700252 }
Jason Samse07694b2012-04-03 15:36:36 -0700253 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700254 }
255
Jason Sams03d2d002012-03-23 13:51:56 -0700256
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700257 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700258 * Get the {@link android.renderscript.Element} of the {@link
259 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700260 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700261 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700262 *
263 */
264 public Element getElement() {
265 return mType.getElement();
266 }
267
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700268 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700269 * Get the usage flags of the Allocation.
270 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700271 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700272 *
273 */
274 public int getUsage() {
275 return mUsage;
276 }
277
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700278 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700279 * Get the size of the Allocation in bytes.
280 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700281 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700282 *
283 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700284 public int getBytesSize() {
Tim Murraye6eaaf62013-12-17 17:15:25 -0800285 if (mType.mDimYuv != 0) {
286 return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
287 }
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700288 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700289 }
290
Jason Sams452a7662011-07-07 16:05:18 -0700291 private void updateCacheInfo(Type t) {
292 mCurrentDimX = t.getX();
293 mCurrentDimY = t.getY();
294 mCurrentDimZ = t.getZ();
295 mCurrentCount = mCurrentDimX;
296 if (mCurrentDimY > 1) {
297 mCurrentCount *= mCurrentDimY;
298 }
299 if (mCurrentDimZ > 1) {
300 mCurrentCount *= mCurrentDimZ;
301 }
302 }
Jason Samsba862d12011-07-07 15:24:42 -0700303
Tim Murraya3145512012-12-04 17:59:29 -0800304 private void setBitmap(Bitmap b) {
305 mBitmap = b;
306 }
307
Tim Murray7a629fa2013-11-19 12:45:54 -0800308 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700309 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800310 if ((usage & ~(USAGE_SCRIPT |
311 USAGE_GRAPHICS_TEXTURE |
312 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700313 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800314 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800315 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800316 USAGE_IO_OUTPUT |
317 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800318 throw new RSIllegalArgumentException("Unknown usage specified.");
319 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800320
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700321 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800322 mWriteAllowed = false;
323
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700324 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800325 USAGE_GRAPHICS_TEXTURE |
326 USAGE_SCRIPT)) != 0) {
327 throw new RSIllegalArgumentException("Invalid usage combination.");
328 }
329 }
Jason Sams9bf18922013-04-13 19:48:36 -0700330
Jason Sams5476b452010-12-08 16:14:36 -0800331 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800332 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700333
Jason Sams452a7662011-07-07 16:05:18 -0700334 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700335 // TODO: A3D doesn't have Type info during creation, so we can't
336 // calculate the size ahead of time. We can possibly add a method
337 // to update the size in the future if it seems reasonable.
338 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700339 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700340 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700341 try {
342 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
343 } catch (Exception e) {
344 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
345 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
346 }
347 }
348
349 protected void finalize() throws Throwable {
350 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
351 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700352 }
353
Jason Sams1136bb92013-11-25 18:28:33 -0800354 private void validateIsInt64() {
355 if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
356 (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
357 return;
358 }
359 throw new RSIllegalArgumentException(
360 "64 bit integer source does not match allocation type " + mType.mElement.mType);
361 }
362
Jason Samsb97b2512011-01-16 15:04:08 -0800363 private void validateIsInt32() {
364 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
365 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
366 return;
367 }
368 throw new RSIllegalArgumentException(
369 "32 bit integer source does not match allocation type " + mType.mElement.mType);
370 }
371
372 private void validateIsInt16() {
373 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
374 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
375 return;
376 }
377 throw new RSIllegalArgumentException(
378 "16 bit integer source does not match allocation type " + mType.mElement.mType);
379 }
380
381 private void validateIsInt8() {
382 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
383 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
384 return;
385 }
386 throw new RSIllegalArgumentException(
387 "8 bit integer source does not match allocation type " + mType.mElement.mType);
388 }
389
390 private void validateIsFloat32() {
391 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
392 return;
393 }
394 throw new RSIllegalArgumentException(
395 "32 bit float source does not match allocation type " + mType.mElement.mType);
396 }
397
Jason Sams1136bb92013-11-25 18:28:33 -0800398 private void validateIsFloat64() {
399 if (mType.mElement.mType == Element.DataType.FLOAT_64) {
400 return;
401 }
402 throw new RSIllegalArgumentException(
403 "64 bit float source does not match allocation type " + mType.mElement.mType);
404 }
405
Jason Samsb97b2512011-01-16 15:04:08 -0800406 private void validateIsObject() {
407 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
408 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
409 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
410 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
411 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
412 (mType.mElement.mType == Element.DataType.RS_MESH) ||
413 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
414 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
415 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
416 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
417 return;
418 }
419 throw new RSIllegalArgumentException(
420 "Object source does not match allocation type " + mType.mElement.mType);
421 }
422
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700423 @Override
424 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800425 super.updateFromNative();
Tim Murray7a629fa2013-11-19 12:45:54 -0800426 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700427 if(typeID != 0) {
428 mType = new Type(typeID, mRS);
429 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700430 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700431 }
432 }
433
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700434 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700435 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700436 *
437 * @return Type
438 *
439 */
Jason Samsea87e962010-01-12 12:12:28 -0800440 public Type getType() {
441 return mType;
442 }
443
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700444 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700445 * Propagate changes from one usage of the Allocation to the
446 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700447 *
448 */
Jason Sams5476b452010-12-08 16:14:36 -0800449 public void syncAll(int srcLocation) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700450 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
Jason Sams5476b452010-12-08 16:14:36 -0800451 switch (srcLocation) {
Jason Sams5476b452010-12-08 16:14:36 -0800452 case USAGE_GRAPHICS_TEXTURE:
Tim Murray78e64942013-04-09 17:28:56 -0700453 case USAGE_SCRIPT:
454 if ((mUsage & USAGE_SHARED) != 0) {
455 copyFrom(mBitmap);
456 }
457 break;
458 case USAGE_GRAPHICS_CONSTANTS:
Jason Sams5476b452010-12-08 16:14:36 -0800459 case USAGE_GRAPHICS_VERTEX:
460 break;
Tim Murray78e64942013-04-09 17:28:56 -0700461 case USAGE_SHARED:
462 if ((mUsage & USAGE_SHARED) != 0) {
463 copyTo(mBitmap);
464 }
465 break;
Jason Sams5476b452010-12-08 16:14:36 -0800466 default:
467 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
468 }
469 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700470 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700471 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800472 }
473
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700474 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700475 * Send a buffer to the output stream. The contents of the Allocation will
476 * be undefined after this operation. This operation is only valid if {@link
477 * #USAGE_IO_OUTPUT} is set on the Allocation.
478 *
Jason Sams163766c2012-02-15 12:04:24 -0800479 *
Jason Sams163766c2012-02-15 12:04:24 -0800480 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700481 public void ioSend() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700482 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
Jason Sams163766c2012-02-15 12:04:24 -0800483 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
484 throw new RSIllegalArgumentException(
485 "Can only send buffer if IO_OUTPUT usage specified.");
486 }
487 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700488 mRS.nAllocationIoSend(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700489 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800490 }
491
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700492 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700493 * Receive the latest input into the Allocation. This operation
494 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800495 *
Jason Sams163766c2012-02-15 12:04:24 -0800496 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700497 public void ioReceive() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700498 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
Jason Sams163766c2012-02-15 12:04:24 -0800499 if ((mUsage & USAGE_IO_INPUT) == 0) {
500 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700501 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800502 }
503 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700504 mRS.nAllocationIoReceive(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700505 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800506 }
507
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700508 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700509 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700510 *
511 * @param d Source array.
512 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800513 public void copyFrom(BaseObj[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700514 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Sams771bebb2009-12-07 12:40:12 -0800515 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800516 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700517 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800518 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700519 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800520 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800521 // FIXME: requires 64-bit path
522
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800523 int i[] = new int[d.length];
524 for (int ct=0; ct < d.length; ct++) {
Tim Murray7a629fa2013-11-19 12:45:54 -0800525 i[ct] = (int)d[ct].getID(mRS);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800526 }
Jason Samsba862d12011-07-07 15:24:42 -0700527 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700528 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700529 }
530
Jason Samsfb9f82c2011-01-12 14:53:25 -0800531 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800532 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800533 if (bc == null) {
534 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
535 }
Jason Sams252c0782011-01-11 17:42:52 -0800536 switch (bc) {
537 case ALPHA_8:
538 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
539 throw new RSIllegalArgumentException("Allocation kind is " +
540 mType.getElement().mKind + ", type " +
541 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700542 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800543 " bytes, passed bitmap was " + bc);
544 }
545 break;
546 case ARGB_8888:
547 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700548 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800549 throw new RSIllegalArgumentException("Allocation kind is " +
550 mType.getElement().mKind + ", type " +
551 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700552 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800553 " bytes, passed bitmap was " + bc);
554 }
555 break;
556 case RGB_565:
557 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700558 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800559 throw new RSIllegalArgumentException("Allocation kind is " +
560 mType.getElement().mKind + ", type " +
561 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700562 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800563 " bytes, passed bitmap was " + bc);
564 }
565 break;
566 case ARGB_4444:
567 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700568 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800569 throw new RSIllegalArgumentException("Allocation kind is " +
570 mType.getElement().mKind + ", type " +
571 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700572 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800573 " bytes, passed bitmap was " + bc);
574 }
575 break;
576
577 }
Jason Sams4ef66502010-12-10 16:03:15 -0800578 }
579
Jason Samsfb9f82c2011-01-12 14:53:25 -0800580 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700581 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800582 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
583 }
584 }
585
Jason Sams1136bb92013-11-25 18:28:33 -0800586 private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
587 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
588 mRS.validate();
589 if (mCurrentDimZ > 0) {
590 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
591 } else if (mCurrentDimY > 0) {
592 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
593 } else {
594 copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
595 }
596 Trace.traceEnd(RenderScript.TRACE_TAG);
597 }
598
599 /**
600 * Copy into this Allocation from an array. This method does not guarantee
601 * that the Allocation is compatible with the input buffer; it copies memory
602 * without reinterpretation.
603 *
604 * @param array The source data array
605 * @hide
606 */
607 public void copyFromUnchecked(Object array) {
608 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
609 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
610 java.lang.reflect.Array.getLength(array));
611 Trace.traceEnd(RenderScript.TRACE_TAG);
612 }
613
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700614 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700615 * Copy into this Allocation from an array. This method does not guarantee
616 * that the Allocation is compatible with the input buffer; it copies memory
617 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800618 *
619 * @param d the source data array
620 */
621 public void copyFromUnchecked(int[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800622 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800623 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700624
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700625 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700626 * Copy into this Allocation from an array. This method does not guarantee
627 * that the Allocation is compatible with the input buffer; it copies memory
628 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800629 *
630 * @param d the source data array
631 */
632 public void copyFromUnchecked(short[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800633 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800634 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700635
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700636 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700637 * Copy into this Allocation from an array. This method does not guarantee
638 * that the Allocation is compatible with the input buffer; it copies memory
639 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800640 *
641 * @param d the source data array
642 */
643 public void copyFromUnchecked(byte[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800644 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800645 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700646
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700647 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700648 * Copy into this Allocation from an array. This method does not guarantee
649 * that the Allocation is compatible with the input buffer; it copies memory
650 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800651 *
652 * @param d the source data array
653 */
654 public void copyFromUnchecked(float[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800655 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800656 }
657
Tim Murray6d7a53c2013-05-23 16:59:23 -0700658
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700659 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700660 * Copy into this Allocation from an array. This variant is type checked
661 * and will generate exceptions if the Allocation's {@link
Jason Sams1136bb92013-11-25 18:28:33 -0800662 * android.renderscript.Element} does not match the array's
663 * primitive type.
664 *
665 * @param d the source data array
666 * @hide
667 */
668 public void copyFrom(Object array) {
669 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
670 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
671 java.lang.reflect.Array.getLength(array));
672 Trace.traceEnd(RenderScript.TRACE_TAG);
673 }
674
675 /**
676 * Copy into this Allocation from an array. This variant is type checked
677 * and will generate exceptions if the Allocation's {@link
Tim Murrayc11e25c2013-04-09 11:01:01 -0700678 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800679 *
680 * @param d the source data array
681 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800682 public void copyFrom(int[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800683 validateIsInt32();
684 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800685 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800686
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700687 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700688 * Copy into this Allocation from an array. This variant is type checked
689 * and will generate exceptions if the Allocation's {@link
690 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800691 *
692 * @param d the source data array
693 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800694 public void copyFrom(short[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800695 validateIsInt16();
696 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800697 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800698
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700699 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700700 * Copy into this Allocation from an array. This variant is type checked
701 * and will generate exceptions if the Allocation's {@link
702 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800703 *
704 * @param d the source data array
705 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800706 public void copyFrom(byte[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800707 validateIsInt8();
708 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800709 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800710
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700711 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700712 * Copy into this Allocation from an array. This variant is type checked
713 * and will generate exceptions if the Allocation's {@link
714 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800715 *
716 * @param d the source data array
717 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800718 public void copyFrom(float[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800719 validateIsFloat32();
720 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800721 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800722
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700723 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700724 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
725 * height, width, and format of the bitmap must match the existing
726 * allocation.
727 *
728 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
729 * android.graphics.Bitmap} used to create the Allocation with {@link
730 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
731 * this will synchronize the Allocation with the latest data from the {@link
732 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800733 *
734 * @param b the source bitmap
735 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800736 public void copyFrom(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700737 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsfb9f82c2011-01-12 14:53:25 -0800738 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -0800739 if (b.getConfig() == null) {
740 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
741 Canvas c = new Canvas(newBitmap);
742 c.drawBitmap(b, 0, 0, null);
743 copyFrom(newBitmap);
744 return;
745 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800746 validateBitmapSize(b);
747 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700748 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700749 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700750 }
751
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700752 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700753 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700754 * must be identical.
755 *
756 * @param a the source allocation
757 */
758 public void copyFrom(Allocation a) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700759 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Tim Murrayf671fb02012-10-03 13:50:05 -0700760 mRS.validate();
761 if (!mType.equals(a.getType())) {
762 throw new RSIllegalArgumentException("Types of allocations must match.");
763 }
764 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700765 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700766 }
767
Tim Murrayf671fb02012-10-03 13:50:05 -0700768 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700769 * This is only intended to be used by auto-generated code reflected from
770 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800771 *
772 * @param xoff
773 * @param fp
774 */
Jason Sams21b41032011-01-16 15:05:41 -0800775 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800776 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700777 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700778 final byte[] data = fp.getData();
779
780 int count = data.length / eSize;
781 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800782 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700783 " not divisible by element size " + eSize + ".");
784 }
Jason Samsba862d12011-07-07 15:24:42 -0700785 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700786 }
787
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700788 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700789 * This is only intended to be used by auto-generated code reflected from
790 * the RenderScript script files.
Jason Samsfa445b92011-01-07 17:00:07 -0800791 *
792 * @param xoff
793 * @param component_number
794 * @param fp
795 */
Jason Sams21b41032011-01-16 15:05:41 -0800796 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800797 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700798 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800799 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700800 }
801 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800802 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700803 }
804
805 final byte[] data = fp.getData();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700806 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800807 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700808
809 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800810 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700811 " does not match component size " + eSize + ".");
812 }
813
Jason Sams48fe5342011-07-08 13:52:30 -0700814 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Jason Samsba862d12011-07-07 15:24:42 -0700815 component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700816 }
817
Jason Sams768bc022009-09-21 19:41:04 -0700818 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800819 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700820 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800821 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700822 }
823 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800824 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700825 }
Jason Samsba862d12011-07-07 15:24:42 -0700826 if((off + count) > mCurrentCount) {
827 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700828 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700829 }
Jason Samsba862d12011-07-07 15:24:42 -0700830 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800831 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700832 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700833 }
834
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700835 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700836 * Generate a mipmap chain. This is only valid if the Type of the Allocation
837 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800838 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700839 * <p>This function will generate a complete set of mipmaps from the top
840 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800841 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700842 * <p>If the Allocation is also using other memory spaces, a call to {@link
843 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800844 */
845 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700846 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800847 }
848
Jason Sams1136bb92013-11-25 18:28:33 -0800849 private void copy1DRangeFromUnchecked(int off, int count, Object array,
850 Element.DataType dt, int arrayLen) {
851 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
852 final int dataSize = mType.mElement.getBytesSize() * count;
853 data1DChecks(off, count, arrayLen * dt.mSize, dataSize);
854 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt);
855 Trace.traceEnd(RenderScript.TRACE_TAG);
856 }
857
858 /**
859 * Copy an array into part of this Allocation. This method does not
860 * guarantee that the Allocation is compatible with the input buffer.
861 *
862 * @param off The offset of the first element to be copied.
863 * @param count The number of elements to be copied.
864 * @param array The source data array
865 * @hide
866 */
867 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
868 copy1DRangeFromUnchecked(off, count, array,
869 validateObjectIsPrimitiveArray(array, false),
870 java.lang.reflect.Array.getLength(array));
871 }
872
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700873 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700874 * Copy an array into part of this Allocation. This method does not
875 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800876 *
877 * @param off The offset of the first element to be copied.
878 * @param count The number of elements to be copied.
879 * @param d the source data array
880 */
881 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800882 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700883 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700884
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700885 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700886 * Copy an array into part of this Allocation. This method does not
887 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800888 *
889 * @param off The offset of the first element to be copied.
890 * @param count The number of elements to be copied.
891 * @param d the source data array
892 */
893 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800894 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700895 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700896
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700897 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700898 * Copy an array into part of this Allocation. This method does not
899 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800900 *
901 * @param off The offset of the first element to be copied.
902 * @param count The number of elements to be copied.
903 * @param d the source data array
904 */
905 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800906 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700907 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700908
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700909 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700910 * Copy an array into part of this Allocation. This method does not
911 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800912 *
913 * @param off The offset of the first element to be copied.
914 * @param count The number of elements to be copied.
915 * @param d the source data array
916 */
917 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800918 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
919 }
920
921
922 /**
923 * Copy an array into part of this Allocation. This variant is type checked
924 * and will generate exceptions if the Allocation type does not
925 * match the component type of the array passed in.
926 *
927 * @param off The offset of the first element to be copied.
928 * @param count The number of elements to be copied.
929 * @param array The source data array.
930 * @hide
931 */
932 public void copy1DRangeFrom(int off, int count, Object array) {
933 copy1DRangeFromUnchecked(off, count, array,
934 validateObjectIsPrimitiveArray(array, true),
935 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -0700936 }
937
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700938 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700939 * Copy an array into part of this Allocation. This variant is type checked
940 * and will generate exceptions if the Allocation type is not a 32 bit
941 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800942 *
943 * @param off The offset of the first element to be copied.
944 * @param count The number of elements to be copied.
945 * @param d the source data array
946 */
Jason Samsb97b2512011-01-16 15:04:08 -0800947 public void copy1DRangeFrom(int off, int count, int[] d) {
948 validateIsInt32();
Jason Sams1136bb92013-11-25 18:28:33 -0800949 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800950 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800951
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700952 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700953 * Copy an array into part of this Allocation. This variant is type checked
954 * and will generate exceptions if the Allocation type is not a 16 bit
955 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800956 *
957 * @param off The offset of the first element to be copied.
958 * @param count The number of elements to be copied.
959 * @param d the source data array
960 */
Jason Samsb97b2512011-01-16 15:04:08 -0800961 public void copy1DRangeFrom(int off, int count, short[] d) {
962 validateIsInt16();
Jason Sams1136bb92013-11-25 18:28:33 -0800963 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800964 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800965
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700966 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700967 * Copy an array into part of this Allocation. This variant is type checked
968 * and will generate exceptions if the Allocation type is not an 8 bit
969 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800970 *
971 * @param off The offset of the first element to be copied.
972 * @param count The number of elements to be copied.
973 * @param d the source data array
974 */
Jason Samsb97b2512011-01-16 15:04:08 -0800975 public void copy1DRangeFrom(int off, int count, byte[] d) {
976 validateIsInt8();
Jason Sams1136bb92013-11-25 18:28:33 -0800977 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800978 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800979
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700980 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700981 * Copy an array into part of this Allocation. This variant is type checked
982 * and will generate exceptions if the Allocation type is not a 32 bit float
983 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800984 *
985 * @param off The offset of the first element to be copied.
986 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700987 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800988 */
Jason Samsb97b2512011-01-16 15:04:08 -0800989 public void copy1DRangeFrom(int off, int count, float[] d) {
990 validateIsFloat32();
Jason Sams1136bb92013-11-25 18:28:33 -0800991 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800992 }
Jason Sams1136bb92013-11-25 18:28:33 -0800993
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700994 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700995 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700996 *
997 * @param off The offset of the first element to be copied.
998 * @param count The number of elements to be copied.
999 * @param data the source data allocation.
1000 * @param dataOff off The offset of the first element in data to
1001 * be copied.
1002 */
1003 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001004 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -07001005 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001006 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001007 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001008 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001009 }
1010
Jason Samsfb9f82c2011-01-12 14:53:25 -08001011 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001012 if (mAdaptedAllocation != null) {
1013
1014 } else {
1015
1016 if (xoff < 0 || yoff < 0) {
1017 throw new RSIllegalArgumentException("Offset cannot be negative.");
1018 }
1019 if (h < 0 || w < 0) {
1020 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1021 }
1022 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1023 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1024 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001025 }
1026 }
Jason Sams768bc022009-09-21 19:41:04 -07001027
Jason Sams1136bb92013-11-25 18:28:33 -08001028 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1029 Element.DataType dt, int arrayLen) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001030 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001031 mRS.validate();
1032 validate2DRange(xoff, yoff, w, h);
Jason Sams1136bb92013-11-25 18:28:33 -08001033 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1034 array, arrayLen * dt.mSize, dt);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001035 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001036 }
1037
Jason Sams1136bb92013-11-25 18:28:33 -08001038 /**
1039 * Copy from an array into a rectangular region in this Allocation. The
1040 * array is assumed to be tightly packed.
1041 *
1042 * @param xoff X offset of the region to update in this Allocation
1043 * @param yoff Y offset of the region to update in this Allocation
1044 * @param w Width of the region to update
1045 * @param h Height of the region to update
1046 * @param data to be placed into the Allocation
1047 * @hide
1048 */
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 Sams1136bb92013-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 Sams1136bb92013-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 Sams1136bb92013-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 Sams1136bb92013-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 Sams1136bb92013-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 Sams1136bb92013-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 Sams1136bb92013-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 Sams1136bb92013-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 * @hide
1278 */
1279 public void copyTo(Object array) {
1280 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1281 java.lang.reflect.Array.getLength(array));
1282 }
1283
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001284 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001285 * Copy from the Allocation into a byte array. The array must be at least
1286 * as large as the Allocation. The allocation must be of an 8 bit integer
1287 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001288 *
1289 * @param d The array to be set from the Allocation.
1290 */
Jason Samsfa445b92011-01-07 17:00:07 -08001291 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001292 validateIsInt8();
Jason Sams1136bb92013-11-25 18:28:33 -08001293 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001294 }
1295
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001296 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001297 * Copy from the Allocation into a short array. The array must be at least
1298 * as large as the Allocation. The allocation must be of an 16 bit integer
1299 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001300 *
1301 * @param d The array to be set from the Allocation.
1302 */
Jason Samsfa445b92011-01-07 17:00:07 -08001303 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001304 validateIsInt16();
Jason Sams1136bb92013-11-25 18:28:33 -08001305 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001306 }
1307
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001308 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001309 * Copy from the Allocation into a int array. The array must be at least as
1310 * large as the Allocation. The allocation must be of an 32 bit integer
1311 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001312 *
1313 * @param d The array to be set from the Allocation.
1314 */
Jason Samsfa445b92011-01-07 17:00:07 -08001315 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001316 validateIsInt32();
Jason Sams1136bb92013-11-25 18:28:33 -08001317 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001318 }
1319
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001320 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001321 * Copy from the Allocation into a float array. The array must be at least
1322 * as large as the Allocation. The allocation must be of an 32 bit float
1323 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001324 *
1325 * @param d The array to be set from the Allocation.
1326 */
Jason Samsfa445b92011-01-07 17:00:07 -08001327 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001328 validateIsFloat32();
Jason Sams1136bb92013-11-25 18:28:33 -08001329 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001330 }
1331
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001332 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001333 * Resize a 1D allocation. The contents of the allocation are preserved.
1334 * If new elements are allocated objects are created with null contents and
1335 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001336 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001337 * <p>If the new region is smaller the references of any objects outside the
1338 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001339 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001340 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001341 *
1342 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001343 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001344 * @deprecated RenderScript objects should be immutable once created. The
1345 * replacement is to create a new allocation and copy the contents.
Jason Samsf7086092011-01-12 13:28:37 -08001346 */
Jason Sams31a7e422010-10-26 13:09:17 -07001347 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001348 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001349 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001350 }
Jason Samse07694b2012-04-03 15:36:36 -07001351 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001352 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001353
Tim Murray7a629fa2013-11-19 12:45:54 -08001354 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001355 mType = new Type(typeID, mRS);
1356 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001357 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001358 }
1359
Jason Samsb8c5a842009-07-31 20:40:47 -07001360
1361 // creation
1362
Jason Sams49a05d72010-12-29 14:31:29 -08001363 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001364 static {
1365 mBitmapOptions.inScaled = false;
1366 }
1367
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001368 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001369 * Creates a new Allocation with the given {@link
1370 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001371 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001372 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001373 * @param mips specifies desired mipmap behaviour for the
1374 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001375 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001376 * utilized
1377 */
1378 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001379 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
Jason Sams771bebb2009-12-07 12:40:12 -08001380 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001381 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001382 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001383 }
Tim Murray7a629fa2013-11-19 12:45:54 -08001384 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001385 if (id == 0) {
1386 throw new RSRuntimeException("Allocation creation failed.");
1387 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001388 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams857d0c72011-11-23 15:02:15 -08001389 return new Allocation(id, rs, type, usage);
1390 }
1391
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001392 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001393 * Creates an Allocation with the size specified by the type and no mipmaps
1394 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001395 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001396 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001397 * @param type renderscript type describing data layout
1398 * @param usage bit field specifying how the allocation is
1399 * utilized
1400 *
1401 * @return allocation
1402 */
Jason Samse5d37122010-12-16 00:33:33 -08001403 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1404 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1405 }
1406
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001407 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001408 * Creates an Allocation for use by scripts with a given {@link
1409 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001410 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001411 * @param rs Context to which the Allocation will belong.
1412 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001413 *
1414 * @return allocation
1415 */
Jason Sams5476b452010-12-08 16:14:36 -08001416 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001417 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001418 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001419
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001420 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001421 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001422 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001423 * @param rs Context to which the Allocation will belong.
1424 * @param e Element to use in the Allocation
1425 * @param count the number of Elements in the Allocation
1426 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001427 * utilized
1428 *
1429 * @return allocation
1430 */
Jason Sams5476b452010-12-08 16:14:36 -08001431 static public Allocation createSized(RenderScript rs, Element e,
1432 int count, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001433 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
Jason Sams771bebb2009-12-07 12:40:12 -08001434 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001435 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001436 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001437 Type t = b.create();
1438
Tim Murray7a629fa2013-11-19 12:45:54 -08001439 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001440 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001441 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001442 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001443 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001444 return new Allocation(id, rs, t, usage);
1445 }
1446
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001447 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001448 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001449 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001450 * @param rs Context to which the Allocation will belong.
1451 * @param e Element to use in the Allocation
1452 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001453 *
1454 * @return allocation
1455 */
Jason Sams5476b452010-12-08 16:14:36 -08001456 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001457 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001458 }
1459
Jason Sams49a05d72010-12-29 14:31:29 -08001460 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001461 final Bitmap.Config bc = b.getConfig();
1462 if (bc == Bitmap.Config.ALPHA_8) {
1463 return Element.A_8(rs);
1464 }
1465 if (bc == Bitmap.Config.ARGB_4444) {
1466 return Element.RGBA_4444(rs);
1467 }
1468 if (bc == Bitmap.Config.ARGB_8888) {
1469 return Element.RGBA_8888(rs);
1470 }
1471 if (bc == Bitmap.Config.RGB_565) {
1472 return Element.RGB_565(rs);
1473 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001474 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001475 }
1476
Jason Sams49a05d72010-12-29 14:31:29 -08001477 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001478 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001479 Element e = elementFromBitmap(rs, b);
1480 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001481 tb.setX(b.getWidth());
1482 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001483 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001484 return tb.create();
1485 }
1486
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001487 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001488 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001489 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001490 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001491 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001492 * @param mips specifies desired mipmap behaviour for the
1493 * allocation
1494 * @param usage bit field specifying how the allocation is
1495 * utilized
1496 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001497 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001498 *
1499 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001500 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001501 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001502 int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001503 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
Jason Sams771bebb2009-12-07 12:40:12 -08001504 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001505
1506 // WAR undocumented color formats
1507 if (b.getConfig() == null) {
1508 if ((usage & USAGE_SHARED) != 0) {
1509 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1510 }
1511 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1512 Canvas c = new Canvas(newBitmap);
1513 c.drawBitmap(b, 0, 0, null);
1514 return createFromBitmap(rs, newBitmap, mips, usage);
1515 }
1516
Jason Sams5476b452010-12-08 16:14:36 -08001517 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001518
Tim Murraya3145512012-12-04 17:59:29 -08001519 // enable optimized bitmap path only with no mipmap and script-only usage
1520 if (mips == MipmapControl.MIPMAP_NONE &&
1521 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Tim Murray78e64942013-04-09 17:28:56 -07001522 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
Tim Murray7a629fa2013-11-19 12:45:54 -08001523 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08001524 if (id == 0) {
1525 throw new RSRuntimeException("Load failed.");
1526 }
1527
1528 // keep a reference to the Bitmap around to prevent GC
1529 Allocation alloc = new Allocation(id, rs, t, usage);
1530 alloc.setBitmap(b);
1531 return alloc;
1532 }
1533
Jason Sams9bf18922013-04-13 19:48:36 -07001534
Tim Murray7a629fa2013-11-19 12:45:54 -08001535 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001536 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001537 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001538 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001539 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001540 return new Allocation(id, rs, t, usage);
1541 }
1542
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001543 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001544 * Returns the handle to a raw buffer that is being managed by the screen
1545 * compositor. This operation is only valid for Allocations with {@link
1546 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001547 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001548 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001549 *
1550 */
1551 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08001552 if ((mUsage & USAGE_IO_INPUT) == 0) {
1553 throw new RSInvalidStateException("Allocation is not a surface texture.");
1554 }
1555 return mRS.nAllocationGetSurface(getID(mRS));
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001556 }
1557
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001558 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001559 * Associate a {@link android.view.Surface} with this Allocation. This
1560 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001561 *
1562 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001563 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001564 public void setSurface(Surface sur) {
1565 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001566 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1567 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1568 }
1569
Jason Samse07694b2012-04-03 15:36:36 -07001570 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001571 }
1572
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001573 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001574 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08001575 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001576 * <p>With target API version 18 or greater, this Allocation will be created
1577 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
1578 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
1579 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001580 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001581 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001582 * @param b bitmap source for the allocation data
1583 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001584 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001585 *
1586 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001587 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08001588 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1589 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07001590 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08001591 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001592 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1593 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001594 }
1595
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001596 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001597 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1598 * containing the horizontal list of cube faces. Each face must be a square,
1599 * have the same size as all other faces, and have a width that is a power
1600 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001601 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001602 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001603 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001604 * format: right, left, top, bottom, front, back
1605 * @param mips specifies desired mipmap behaviour for the cubemap
1606 * @param usage bit field specifying how the cubemap is utilized
1607 *
1608 * @return allocation containing cubemap data
1609 *
1610 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001611 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001612 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001613 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001614 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001615
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001616 int height = b.getHeight();
1617 int width = b.getWidth();
1618
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001619 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001620 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1621 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001622 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001623 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001624 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001625 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001626 if (!isPow2) {
1627 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1628 }
1629
1630 Element e = elementFromBitmap(rs, b);
1631 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001632 tb.setX(height);
1633 tb.setY(height);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001634 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001635 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001636 Type t = tb.create();
1637
Tim Murray7a629fa2013-11-19 12:45:54 -08001638 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001639 if(id == 0) {
1640 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1641 }
Jason Sams5476b452010-12-08 16:14:36 -08001642 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001643 }
1644
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001645 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001646 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
1647 * from a {@link android.graphics.Bitmap} containing the horizontal list of
1648 * cube faces. Each face must be a square, have the same size as all other
1649 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001650 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001651 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001652 * @param b bitmap with cubemap faces layed out in the following
1653 * format: right, left, top, bottom, front, back
1654 *
1655 * @return allocation containing cubemap data
1656 *
1657 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001658 static public Allocation createCubemapFromBitmap(RenderScript rs,
1659 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001660 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001661 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001662 }
1663
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001664 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001665 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
1666 * objects containing the cube faces. Each face must be a square, have the
1667 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001668 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001669 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001670 * @param xpos cubemap face in the positive x direction
1671 * @param xneg cubemap face in the negative x direction
1672 * @param ypos cubemap face in the positive y direction
1673 * @param yneg cubemap face in the negative y direction
1674 * @param zpos cubemap face in the positive z direction
1675 * @param zneg cubemap face in the negative z direction
1676 * @param mips specifies desired mipmap behaviour for the cubemap
1677 * @param usage bit field specifying how the cubemap is utilized
1678 *
1679 * @return allocation containing cubemap data
1680 *
1681 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001682 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1683 Bitmap xpos,
1684 Bitmap xneg,
1685 Bitmap ypos,
1686 Bitmap yneg,
1687 Bitmap zpos,
1688 Bitmap zneg,
1689 MipmapControl mips,
1690 int usage) {
1691 int height = xpos.getHeight();
1692 if (xpos.getWidth() != height ||
1693 xneg.getWidth() != height || xneg.getHeight() != height ||
1694 ypos.getWidth() != height || ypos.getHeight() != height ||
1695 yneg.getWidth() != height || yneg.getHeight() != height ||
1696 zpos.getWidth() != height || zpos.getHeight() != height ||
1697 zneg.getWidth() != height || zneg.getHeight() != height) {
1698 throw new RSIllegalArgumentException("Only square cube map faces supported");
1699 }
1700 boolean isPow2 = (height & (height - 1)) == 0;
1701 if (!isPow2) {
1702 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1703 }
1704
1705 Element e = elementFromBitmap(rs, xpos);
1706 Type.Builder tb = new Type.Builder(rs, e);
1707 tb.setX(height);
1708 tb.setY(height);
1709 tb.setFaces(true);
1710 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1711 Type t = tb.create();
1712 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1713
1714 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001715 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001716 adapter.copyFrom(xpos);
1717 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1718 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001719 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001720 adapter.copyFrom(ypos);
1721 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1722 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001723 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001724 adapter.copyFrom(zpos);
1725 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1726 adapter.copyFrom(zneg);
1727
1728 return cubemap;
1729 }
1730
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001731 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001732 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
1733 * from 6 {@link android.graphics.Bitmap} objects containing the cube
1734 * faces. Each face must be a square, have the same size as all other faces,
1735 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001736 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001737 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001738 * @param xpos cubemap face in the positive x direction
1739 * @param xneg cubemap face in the negative x direction
1740 * @param ypos cubemap face in the positive y direction
1741 * @param yneg cubemap face in the negative y direction
1742 * @param zpos cubemap face in the positive z direction
1743 * @param zneg cubemap face in the negative z direction
1744 *
1745 * @return allocation containing cubemap data
1746 *
1747 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001748 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1749 Bitmap xpos,
1750 Bitmap xneg,
1751 Bitmap ypos,
1752 Bitmap yneg,
1753 Bitmap zpos,
1754 Bitmap zneg) {
1755 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1756 zpos, zneg, MipmapControl.MIPMAP_NONE,
1757 USAGE_GRAPHICS_TEXTURE);
1758 }
1759
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001760 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001761 * Creates an Allocation from the Bitmap referenced
1762 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001763 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001764 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001765 * @param res application resources
1766 * @param id resource id to load the data from
1767 * @param mips specifies desired mipmap behaviour for the
1768 * allocation
1769 * @param usage bit field specifying how the allocation is
1770 * utilized
1771 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001772 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001773 *
1774 */
Jason Sams5476b452010-12-08 16:14:36 -08001775 static public Allocation createFromBitmapResource(RenderScript rs,
1776 Resources res,
1777 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001778 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001779 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001780
Jason Sams771bebb2009-12-07 12:40:12 -08001781 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07001782 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
1783 throw new RSIllegalArgumentException("Unsupported usage specified.");
1784 }
Jason Sams5476b452010-12-08 16:14:36 -08001785 Bitmap b = BitmapFactory.decodeResource(res, id);
1786 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1787 b.recycle();
1788 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001789 }
1790
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001791 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001792 * Creates a non-mipmapped Allocation to use as a graphics texture from the
1793 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001794 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001795 * <p>With target API version 18 or greater, this allocation will be created
1796 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
1797 * target API version 17 or lower, this allocation will be created with
1798 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08001799 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001800 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001801 * @param res application resources
1802 * @param id resource id to load the data from
1803 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001804 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001805 *
1806 */
Jason Sams5476b452010-12-08 16:14:36 -08001807 static public Allocation createFromBitmapResource(RenderScript rs,
1808 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001809 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08001810 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1811 return createFromBitmapResource(rs, res, id,
1812 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07001813 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08001814 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001815 return createFromBitmapResource(rs, res, id,
1816 MipmapControl.MIPMAP_NONE,
1817 USAGE_GRAPHICS_TEXTURE);
1818 }
1819
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001820 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001821 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001822 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001823 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001824 * @param str string to create the allocation from
1825 * @param usage bit field specifying how the allocaiton is
1826 * utilized
1827 *
1828 */
Jason Sams5476b452010-12-08 16:14:36 -08001829 static public Allocation createFromString(RenderScript rs,
1830 String str,
1831 int usage) {
1832 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001833 byte[] allocArray = null;
1834 try {
1835 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001836 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001837 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001838 return alloc;
1839 }
1840 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001841 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001842 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001843 }
Jason Sams739c8262013-04-11 18:07:52 -07001844
1845 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001846 * Interface to handle notification when new buffers are available via
1847 * {@link #USAGE_IO_INPUT}. An application will receive one notification
1848 * when a buffer is available. Additional buffers will not trigger new
1849 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07001850 */
Jason Sams42ef2382013-08-29 13:30:59 -07001851 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07001852 public void onBufferAvailable(Allocation a);
1853 }
1854
1855 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001856 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07001857 *
Jason Sams42ef2382013-08-29 13:30:59 -07001858 * @param callback instance of the OnBufferAvailableListener
1859 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07001860 */
Jason Sams42ef2382013-08-29 13:30:59 -07001861 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07001862 synchronized(mAllocationMap) {
Tim Murray7a629fa2013-11-19 12:45:54 -08001863 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07001864 mBufferNotifier = callback;
1865 }
1866 }
1867
1868 static void sendBufferNotification(int id) {
1869 synchronized(mAllocationMap) {
Tim Murray7a629fa2013-11-19 12:45:54 -08001870 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07001871
1872 if ((a != null) && (a.mBufferNotifier != null)) {
1873 a.mBufferNotifier.onBufferAvailable(a);
1874 }
1875 }
1876 }
1877
Jason Samsb8c5a842009-07-31 20:40:47 -07001878}
1879