blob: 20b7ee7d78741f844a71d4a4ad20d1e102ea01a4 [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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-12-06 15:59:59 -0800520 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800521 // FIXME: requires 64-bit path
522
Jason Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsf70b0fc82012-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();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700779 int data_length = fp.getPos();
Jason Samsa70f4162010-03-26 15:33:42 -0700780
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700781 int count = data_length / eSize;
782 if ((eSize * count) != data_length) {
783 throw new RSIllegalArgumentException("Field packer length " + data_length +
Jason Samsa70f4162010-03-26 15:33:42 -0700784 " not divisible by element size " + eSize + ".");
785 }
Jason Samsba862d12011-07-07 15:24:42 -0700786 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700787 }
788
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700789 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700790 * This is only intended to be used by auto-generated code reflected from
791 * the RenderScript script files.
Jason Samsfa445b92011-01-07 17:00:07 -0800792 *
793 * @param xoff
794 * @param component_number
795 * @param fp
796 */
Jason Sams21b41032011-01-16 15:05:41 -0800797 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800798 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700799 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800800 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700801 }
802 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800803 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700804 }
805
806 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700807 int data_length = fp.getPos();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700808 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800809 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700810
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700811 if (data_length != eSize) {
812 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700813 " does not match component size " + eSize + ".");
814 }
815
Jason Sams48fe5342011-07-08 13:52:30 -0700816 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700817 component_number, data, data_length);
Jason Samsa70f4162010-03-26 15:33:42 -0700818 }
819
Jason Sams768bc022009-09-21 19:41:04 -0700820 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800821 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700822 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800823 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700824 }
825 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800826 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700827 }
Jason Samsba862d12011-07-07 15:24:42 -0700828 if((off + count) > mCurrentCount) {
829 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700830 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700831 }
Jason Samsba862d12011-07-07 15:24:42 -0700832 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800833 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700834 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700835 }
836
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700837 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700838 * Generate a mipmap chain. This is only valid if the Type of the Allocation
839 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800840 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700841 * <p>This function will generate a complete set of mipmaps from the top
842 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800843 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700844 * <p>If the Allocation is also using other memory spaces, a call to {@link
845 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800846 */
847 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700848 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800849 }
850
Jason Sams1136bb92013-11-25 18:28:33 -0800851 private void copy1DRangeFromUnchecked(int off, int count, Object array,
852 Element.DataType dt, int arrayLen) {
853 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
854 final int dataSize = mType.mElement.getBytesSize() * count;
855 data1DChecks(off, count, arrayLen * dt.mSize, dataSize);
856 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt);
857 Trace.traceEnd(RenderScript.TRACE_TAG);
858 }
859
860 /**
861 * Copy an array into part of this Allocation. This method does not
862 * guarantee that the Allocation is compatible with the input buffer.
863 *
864 * @param off The offset of the first element to be copied.
865 * @param count The number of elements to be copied.
866 * @param array The source data array
867 * @hide
868 */
869 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
870 copy1DRangeFromUnchecked(off, count, array,
871 validateObjectIsPrimitiveArray(array, false),
872 java.lang.reflect.Array.getLength(array));
873 }
874
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700875 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700876 * Copy an array into part of this Allocation. This method does not
877 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800878 *
879 * @param off The offset of the first element to be copied.
880 * @param count The number of elements to be copied.
881 * @param d the source data array
882 */
883 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800884 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700885 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700886
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700887 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700888 * Copy an array into part of this Allocation. This method does not
889 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800890 *
891 * @param off The offset of the first element to be copied.
892 * @param count The number of elements to be copied.
893 * @param d the source data array
894 */
895 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800896 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700897 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700898
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700899 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700900 * Copy an array into part of this Allocation. This method does not
901 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800902 *
903 * @param off The offset of the first element to be copied.
904 * @param count The number of elements to be copied.
905 * @param d the source data array
906 */
907 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800908 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700909 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700910
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700911 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700912 * Copy an array into part of this Allocation. This method does not
913 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800914 *
915 * @param off The offset of the first element to be copied.
916 * @param count The number of elements to be copied.
917 * @param d the source data array
918 */
919 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams1136bb92013-11-25 18:28:33 -0800920 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
921 }
922
923
924 /**
925 * Copy an array into part of this Allocation. This variant is type checked
926 * and will generate exceptions if the Allocation type does not
927 * match the component type of the array passed in.
928 *
929 * @param off The offset of the first element to be copied.
930 * @param count The number of elements to be copied.
931 * @param array The source data array.
932 * @hide
933 */
934 public void copy1DRangeFrom(int off, int count, Object array) {
935 copy1DRangeFromUnchecked(off, count, array,
936 validateObjectIsPrimitiveArray(array, true),
937 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -0700938 }
939
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700940 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700941 * Copy an array into part of this Allocation. This variant is type checked
942 * and will generate exceptions if the Allocation type is not a 32 bit
943 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800944 *
945 * @param off The offset of the first element to be copied.
946 * @param count The number of elements to be copied.
947 * @param d the source data array
948 */
Jason Samsb97b2512011-01-16 15:04:08 -0800949 public void copy1DRangeFrom(int off, int count, int[] d) {
950 validateIsInt32();
Jason Sams1136bb92013-11-25 18:28:33 -0800951 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800952 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800953
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700954 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700955 * Copy an array into part of this Allocation. This variant is type checked
956 * and will generate exceptions if the Allocation type is not a 16 bit
957 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800958 *
959 * @param off The offset of the first element to be copied.
960 * @param count The number of elements to be copied.
961 * @param d the source data array
962 */
Jason Samsb97b2512011-01-16 15:04:08 -0800963 public void copy1DRangeFrom(int off, int count, short[] d) {
964 validateIsInt16();
Jason Sams1136bb92013-11-25 18:28:33 -0800965 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800966 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800967
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700968 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700969 * Copy an array into part of this Allocation. This variant is type checked
970 * and will generate exceptions if the Allocation type is not an 8 bit
971 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800972 *
973 * @param off The offset of the first element to be copied.
974 * @param count The number of elements to be copied.
975 * @param d the source data array
976 */
Jason Samsb97b2512011-01-16 15:04:08 -0800977 public void copy1DRangeFrom(int off, int count, byte[] d) {
978 validateIsInt8();
Jason Sams1136bb92013-11-25 18:28:33 -0800979 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800980 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800981
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700982 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700983 * Copy an array into part of this Allocation. This variant is type checked
984 * and will generate exceptions if the Allocation type is not a 32 bit float
985 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800986 *
987 * @param off The offset of the first element to be copied.
988 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700989 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800990 */
Jason Samsb97b2512011-01-16 15:04:08 -0800991 public void copy1DRangeFrom(int off, int count, float[] d) {
992 validateIsFloat32();
Jason Sams1136bb92013-11-25 18:28:33 -0800993 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800994 }
Jason Sams1136bb92013-11-25 18:28:33 -0800995
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700996 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700997 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700998 *
999 * @param off The offset of the first element to be copied.
1000 * @param count The number of elements to be copied.
1001 * @param data the source data allocation.
1002 * @param dataOff off The offset of the first element in data to
1003 * be copied.
1004 */
1005 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001006 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -07001007 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001008 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001009 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001010 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001011 }
1012
Jason Samsfb9f82c2011-01-12 14:53:25 -08001013 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001014 if (mAdaptedAllocation != null) {
1015
1016 } else {
1017
1018 if (xoff < 0 || yoff < 0) {
1019 throw new RSIllegalArgumentException("Offset cannot be negative.");
1020 }
1021 if (h < 0 || w < 0) {
1022 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1023 }
1024 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1025 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1026 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001027 }
1028 }
Jason Sams768bc022009-09-21 19:41:04 -07001029
Jason Sams1136bb92013-11-25 18:28:33 -08001030 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1031 Element.DataType dt, int arrayLen) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001032 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001033 mRS.validate();
1034 validate2DRange(xoff, yoff, w, h);
Jason Sams1136bb92013-11-25 18:28:33 -08001035 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1036 array, arrayLen * dt.mSize, dt);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001037 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001038 }
1039
Jason Sams1136bb92013-11-25 18:28:33 -08001040 /**
1041 * Copy from an array into a rectangular region in this Allocation. The
1042 * array is assumed to be tightly packed.
1043 *
1044 * @param xoff X offset of the region to update in this Allocation
1045 * @param yoff Y offset of the region to update in this Allocation
1046 * @param w Width of the region to update
1047 * @param h Height of the region to update
1048 * @param data to be placed into the Allocation
1049 * @hide
1050 */
1051 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
1052 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1053 copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
1054 validateObjectIsPrimitiveArray(array, true),
1055 java.lang.reflect.Array.getLength(array));
Tim Murray6d7a53c2013-05-23 16:59:23 -07001056 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001057 }
1058
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001059 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001060 * Copy from an array into a rectangular region in this Allocation. The
1061 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -08001062 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001063 * @param xoff X offset of the region to update in this Allocation
1064 * @param yoff Y offset of the region to update in this Allocation
1065 * @param w Width of the region to update
1066 * @param h Height of the region to update
1067 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001068 */
1069 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001070 validateIsInt8();
Jason Sams1136bb92013-11-25 18:28:33 -08001071 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1072 Element.DataType.SIGNED_8, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001073 }
1074
Tim Murrayc11e25c2013-04-09 11:01:01 -07001075 /**
1076 * Copy from an array into a rectangular region in this Allocation. The
1077 * array is assumed to be tightly packed.
1078 *
1079 * @param xoff X offset of the region to update in this Allocation
1080 * @param yoff Y offset of the region to update in this Allocation
1081 * @param w Width of the region to update
1082 * @param h Height of the region to update
1083 * @param data to be placed into the Allocation
1084 */
Jason Samsf7086092011-01-12 13:28:37 -08001085 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001086 validateIsInt16();
Jason Sams1136bb92013-11-25 18:28:33 -08001087 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1088 Element.DataType.SIGNED_16, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001089 }
1090
Tim Murrayc11e25c2013-04-09 11:01:01 -07001091 /**
1092 * Copy from an array into a rectangular region in this Allocation. The
1093 * array is assumed to be tightly packed.
1094 *
1095 * @param xoff X offset of the region to update in this Allocation
1096 * @param yoff Y offset of the region to update in this Allocation
1097 * @param w Width of the region to update
1098 * @param h Height of the region to update
1099 * @param data to be placed into the Allocation
1100 */
Jason Samsf7086092011-01-12 13:28:37 -08001101 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001102 validateIsInt32();
Jason Sams1136bb92013-11-25 18:28:33 -08001103 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1104 Element.DataType.SIGNED_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001105 }
1106
Tim Murrayc11e25c2013-04-09 11:01:01 -07001107 /**
1108 * Copy from an array into a rectangular region in this Allocation. The
1109 * array is assumed to be tightly packed.
1110 *
1111 * @param xoff X offset of the region to update in this Allocation
1112 * @param yoff Y offset of the region to update in this Allocation
1113 * @param w Width of the region to update
1114 * @param h Height of the region to update
1115 * @param data to be placed into the Allocation
1116 */
Jason Samsf7086092011-01-12 13:28:37 -08001117 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001118 validateIsFloat32();
Jason Sams1136bb92013-11-25 18:28:33 -08001119 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1120 Element.DataType.FLOAT_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001121 }
1122
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001123 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001124 * Copy a rectangular region from an Allocation into a rectangular region in
1125 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001126 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001127 * @param xoff X offset of the region in this Allocation
1128 * @param yoff Y offset of the region in this Allocation
1129 * @param w Width of the region to update.
1130 * @param h Height of the region to update.
1131 * @param data source Allocation.
1132 * @param dataXoff X offset in source Allocation
1133 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001134 */
1135 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1136 Allocation data, int dataXoff, int dataYoff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001137 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001138 mRS.validate();
1139 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -07001140 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -07001141 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001142 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -07001143 data.mSelectedLOD, data.mSelectedFace.mID);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001144 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001145 }
1146
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001147 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001148 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1149 * and width of the update will use the height and width of the {@link
1150 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001151 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001152 * @param xoff X offset of the region to update in this Allocation
1153 * @param yoff Y offset of the region to update in this Allocation
1154 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001155 */
1156 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001157 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Jason Samsfa445b92011-01-07 17:00:07 -08001158 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001159 if (data.getConfig() == null) {
1160 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1161 Canvas c = new Canvas(newBitmap);
1162 c.drawBitmap(data, 0, 0, null);
1163 copy2DRangeFrom(xoff, yoff, newBitmap);
Jason Samsb05d6892013-04-09 15:59:24 -07001164 return;
Tim Murrayabd5db92013-02-28 11:45:22 -08001165 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001166 validateBitmapFormat(data);
1167 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -07001168 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001169 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001170 }
1171
Jason Samsb05d6892013-04-09 15:59:24 -07001172 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1173 if (mAdaptedAllocation != null) {
1174
1175 } else {
1176
1177 if (xoff < 0 || yoff < 0 || zoff < 0) {
1178 throw new RSIllegalArgumentException("Offset cannot be negative.");
1179 }
1180 if (h < 0 || w < 0 || d < 0) {
1181 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1182 }
1183 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1184 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1185 }
1186 }
1187 }
1188
1189 /**
1190 * @hide
1191 *
1192 */
Jason Sams1136bb92013-11-25 18:28:33 -08001193 private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1194 Object array, Element.DataType dt, int arrayLen) {
1195 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
Jason Samsb05d6892013-04-09 15:59:24 -07001196 mRS.validate();
1197 validate3DRange(xoff, yoff, zoff, w, h, d);
Jason Sams1136bb92013-11-25 18:28:33 -08001198 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1199 array, arrayLen * dt.mSize, dt);
1200 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb05d6892013-04-09 15:59:24 -07001201 }
1202
1203 /**
1204 * @hide
Jason Samsb05d6892013-04-09 15:59:24 -07001205 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001206 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001207 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001208 * @param xoff X offset of the region to update in this Allocation
1209 * @param yoff Y offset of the region to update in this Allocation
1210 * @param zoff Z offset of the region to update in this Allocation
1211 * @param w Width of the region to update
1212 * @param h Height of the region to update
1213 * @param d Depth of the region to update
Jason Samsb05d6892013-04-09 15:59:24 -07001214 * @param data to be placed into the allocation
1215 */
Jason Sams1136bb92013-11-25 18:28:33 -08001216 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1217 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
1218 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
1219 validateObjectIsPrimitiveArray(array, true),
1220 java.lang.reflect.Array.getLength(array));
1221 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb05d6892013-04-09 15:59:24 -07001222 }
1223
1224 /**
1225 * @hide
1226 * Copy a rectangular region into the allocation from another
1227 * allocation.
1228 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001229 * @param xoff X offset of the region to update in this Allocation
1230 * @param yoff Y offset of the region to update in this Allocation
1231 * @param zoff Z offset of the region to update in this Allocation
1232 * @param w Width of the region to update.
1233 * @param h Height of the region to update.
1234 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001235 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001236 * @param dataXoff X offset of the region in the source Allocation
1237 * @param dataYoff Y offset of the region in the source Allocation
1238 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001239 */
1240 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1241 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1242 mRS.validate();
1243 validate3DRange(xoff, yoff, zoff, w, h, d);
1244 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1245 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1246 data.mSelectedLOD);
1247 }
1248
Jason Samsfa445b92011-01-07 17:00:07 -08001249
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001250 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001251 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1252 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001253 *
1254 * @param b The bitmap to be set from the Allocation.
1255 */
Jason Samsfa445b92011-01-07 17:00:07 -08001256 public void copyTo(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001257 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsfb9f82c2011-01-12 14:53:25 -08001258 mRS.validate();
1259 validateBitmapFormat(b);
1260 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -07001261 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001262 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001263 }
1264
Jason Sams1136bb92013-11-25 18:28:33 -08001265 private void copyTo(Object array, Element.DataType dt, int arrayLen) {
1266 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1267 mRS.validate();
1268 mRS.nAllocationRead(getID(mRS), array, dt);
1269 Trace.traceEnd(RenderScript.TRACE_TAG);
1270 }
1271
1272 /**
1273 * Copy from the Allocation into an array. The array must be at
1274 * least as large as the Allocation. The
1275 * {@link android.renderscript.Element} must match the component
1276 * type of the array passed in.
1277 *
1278 * @param array The array to be set from the Allocation.
1279 * @hide
1280 */
1281 public void copyTo(Object array) {
1282 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1283 java.lang.reflect.Array.getLength(array));
1284 }
1285
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001286 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001287 * Copy from the Allocation into a byte array. The array must be at least
1288 * as large as the Allocation. The allocation must be of an 8 bit integer
1289 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001290 *
1291 * @param d The array to be set from the Allocation.
1292 */
Jason Samsfa445b92011-01-07 17:00:07 -08001293 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001294 validateIsInt8();
Jason Sams1136bb92013-11-25 18:28:33 -08001295 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001296 }
1297
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001298 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001299 * Copy from the Allocation into a short array. The array must be at least
1300 * as large as the Allocation. The allocation must be of an 16 bit integer
1301 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001302 *
1303 * @param d The array to be set from the Allocation.
1304 */
Jason Samsfa445b92011-01-07 17:00:07 -08001305 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001306 validateIsInt16();
Jason Sams1136bb92013-11-25 18:28:33 -08001307 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001308 }
1309
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001310 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001311 * Copy from the Allocation into a int array. The array must be at least as
1312 * large as the Allocation. The allocation must be of an 32 bit integer
1313 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001314 *
1315 * @param d The array to be set from the Allocation.
1316 */
Jason Samsfa445b92011-01-07 17:00:07 -08001317 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001318 validateIsInt32();
Jason Sams1136bb92013-11-25 18:28:33 -08001319 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001320 }
1321
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001322 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001323 * Copy from the Allocation into a float array. The array must be at least
1324 * as large as the Allocation. The allocation must be of an 32 bit float
1325 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001326 *
1327 * @param d The array to be set from the Allocation.
1328 */
Jason Samsfa445b92011-01-07 17:00:07 -08001329 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001330 validateIsFloat32();
Jason Sams1136bb92013-11-25 18:28:33 -08001331 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001332 }
1333
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001334 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001335 * Resize a 1D allocation. The contents of the allocation are preserved.
1336 * If new elements are allocated objects are created with null contents and
1337 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001338 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001339 * <p>If the new region is smaller the references of any objects outside the
1340 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001341 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001342 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001343 *
1344 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001345 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001346 * @deprecated RenderScript objects should be immutable once created. The
1347 * replacement is to create a new allocation and copy the contents.
Jason Samsf7086092011-01-12 13:28:37 -08001348 */
Jason Sams31a7e422010-10-26 13:09:17 -07001349 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001350 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001351 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001352 }
Jason Samse07694b2012-04-03 15:36:36 -07001353 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001354 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001355
Tim Murray7a629fa2013-11-19 12:45:54 -08001356 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001357 mType = new Type(typeID, mRS);
1358 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001359 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001360 }
1361
Jason Samsb8c5a842009-07-31 20:40:47 -07001362
1363 // creation
1364
Jason Sams49a05d72010-12-29 14:31:29 -08001365 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001366 static {
1367 mBitmapOptions.inScaled = false;
1368 }
1369
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001370 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001371 * Creates a new Allocation with the given {@link
1372 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001373 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001374 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001375 * @param mips specifies desired mipmap behaviour for the
1376 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001377 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001378 * utilized
1379 */
1380 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001381 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
Jason Sams771bebb2009-12-07 12:40:12 -08001382 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001383 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001384 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001385 }
Tim Murray7a629fa2013-11-19 12:45:54 -08001386 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001387 if (id == 0) {
1388 throw new RSRuntimeException("Allocation creation failed.");
1389 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001390 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams857d0c72011-11-23 15:02:15 -08001391 return new Allocation(id, rs, type, usage);
1392 }
1393
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001394 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001395 * Creates an Allocation with the size specified by the type and no mipmaps
1396 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001397 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001398 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001399 * @param type renderscript type describing data layout
1400 * @param usage bit field specifying how the allocation is
1401 * utilized
1402 *
1403 * @return allocation
1404 */
Jason Samse5d37122010-12-16 00:33:33 -08001405 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1406 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1407 }
1408
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001409 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001410 * Creates an Allocation for use by scripts with a given {@link
1411 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001412 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001413 * @param rs Context to which the Allocation will belong.
1414 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001415 *
1416 * @return allocation
1417 */
Jason Sams5476b452010-12-08 16:14:36 -08001418 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001419 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001420 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001421
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001422 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001423 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001424 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001425 * @param rs Context to which the Allocation will belong.
1426 * @param e Element to use in the Allocation
1427 * @param count the number of Elements in the Allocation
1428 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001429 * utilized
1430 *
1431 * @return allocation
1432 */
Jason Sams5476b452010-12-08 16:14:36 -08001433 static public Allocation createSized(RenderScript rs, Element e,
1434 int count, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001435 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
Jason Sams771bebb2009-12-07 12:40:12 -08001436 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001437 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001438 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001439 Type t = b.create();
1440
Tim Murray7a629fa2013-11-19 12:45:54 -08001441 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001442 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001443 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001444 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001445 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001446 return new Allocation(id, rs, t, usage);
1447 }
1448
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001449 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001450 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001451 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001452 * @param rs Context to which the Allocation will belong.
1453 * @param e Element to use in the Allocation
1454 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001455 *
1456 * @return allocation
1457 */
Jason Sams5476b452010-12-08 16:14:36 -08001458 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001459 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001460 }
1461
Jason Sams49a05d72010-12-29 14:31:29 -08001462 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001463 final Bitmap.Config bc = b.getConfig();
1464 if (bc == Bitmap.Config.ALPHA_8) {
1465 return Element.A_8(rs);
1466 }
1467 if (bc == Bitmap.Config.ARGB_4444) {
1468 return Element.RGBA_4444(rs);
1469 }
1470 if (bc == Bitmap.Config.ARGB_8888) {
1471 return Element.RGBA_8888(rs);
1472 }
1473 if (bc == Bitmap.Config.RGB_565) {
1474 return Element.RGB_565(rs);
1475 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001476 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001477 }
1478
Jason Sams49a05d72010-12-29 14:31:29 -08001479 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001480 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001481 Element e = elementFromBitmap(rs, b);
1482 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001483 tb.setX(b.getWidth());
1484 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001485 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001486 return tb.create();
1487 }
1488
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001489 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001490 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001491 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001492 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001493 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001494 * @param mips specifies desired mipmap behaviour for the
1495 * allocation
1496 * @param usage bit field specifying how the allocation is
1497 * utilized
1498 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001499 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001500 *
1501 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001502 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001503 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001504 int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001505 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
Jason Sams771bebb2009-12-07 12:40:12 -08001506 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001507
1508 // WAR undocumented color formats
1509 if (b.getConfig() == null) {
1510 if ((usage & USAGE_SHARED) != 0) {
1511 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1512 }
1513 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1514 Canvas c = new Canvas(newBitmap);
1515 c.drawBitmap(b, 0, 0, null);
1516 return createFromBitmap(rs, newBitmap, mips, usage);
1517 }
1518
Jason Sams5476b452010-12-08 16:14:36 -08001519 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001520
Tim Murraya3145512012-12-04 17:59:29 -08001521 // enable optimized bitmap path only with no mipmap and script-only usage
1522 if (mips == MipmapControl.MIPMAP_NONE &&
1523 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Tim Murray78e64942013-04-09 17:28:56 -07001524 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
Tim Murray7a629fa2013-11-19 12:45:54 -08001525 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08001526 if (id == 0) {
1527 throw new RSRuntimeException("Load failed.");
1528 }
1529
1530 // keep a reference to the Bitmap around to prevent GC
1531 Allocation alloc = new Allocation(id, rs, t, usage);
1532 alloc.setBitmap(b);
1533 return alloc;
1534 }
1535
Jason Sams9bf18922013-04-13 19:48:36 -07001536
Tim Murray7a629fa2013-11-19 12:45:54 -08001537 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001538 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001539 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001540 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001541 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001542 return new Allocation(id, rs, t, usage);
1543 }
1544
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001545 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001546 * Returns the handle to a raw buffer that is being managed by the screen
1547 * compositor. This operation is only valid for Allocations with {@link
1548 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001549 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001550 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001551 *
1552 */
1553 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08001554 if ((mUsage & USAGE_IO_INPUT) == 0) {
1555 throw new RSInvalidStateException("Allocation is not a surface texture.");
1556 }
1557 return mRS.nAllocationGetSurface(getID(mRS));
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001558 }
1559
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001560 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001561 * Associate a {@link android.view.Surface} with this Allocation. This
1562 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001563 *
1564 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001565 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001566 public void setSurface(Surface sur) {
1567 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001568 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1569 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1570 }
1571
Jason Samse07694b2012-04-03 15:36:36 -07001572 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001573 }
1574
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001575 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001576 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08001577 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001578 * <p>With target API version 18 or greater, this Allocation will be created
1579 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
1580 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
1581 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001582 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001583 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001584 * @param b bitmap source for the allocation data
1585 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001586 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001587 *
1588 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001589 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08001590 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1591 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07001592 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08001593 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001594 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1595 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001596 }
1597
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001598 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001599 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1600 * containing the horizontal list of cube faces. Each face must be a square,
1601 * have the same size as all other faces, and have a width that is a power
1602 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001603 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001604 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001605 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001606 * format: right, left, top, bottom, front, back
1607 * @param mips specifies desired mipmap behaviour for the cubemap
1608 * @param usage bit field specifying how the cubemap is utilized
1609 *
1610 * @return allocation containing cubemap data
1611 *
1612 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001613 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001614 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001615 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001616 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001617
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001618 int height = b.getHeight();
1619 int width = b.getWidth();
1620
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001621 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001622 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1623 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001624 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001625 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001626 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001627 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001628 if (!isPow2) {
1629 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1630 }
1631
1632 Element e = elementFromBitmap(rs, b);
1633 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001634 tb.setX(height);
1635 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001636 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001637 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001638 Type t = tb.create();
1639
Tim Murray7a629fa2013-11-19 12:45:54 -08001640 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001641 if(id == 0) {
1642 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1643 }
Jason Sams5476b452010-12-08 16:14:36 -08001644 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001645 }
1646
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001647 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001648 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
1649 * from a {@link android.graphics.Bitmap} containing the horizontal list of
1650 * cube faces. Each face must be a square, have the same size as all other
1651 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001652 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001653 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001654 * @param b bitmap with cubemap faces layed out in the following
1655 * format: right, left, top, bottom, front, back
1656 *
1657 * @return allocation containing cubemap data
1658 *
1659 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001660 static public Allocation createCubemapFromBitmap(RenderScript rs,
1661 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001662 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001663 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001664 }
1665
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001666 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001667 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
1668 * objects containing the cube faces. Each face must be a square, have the
1669 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001670 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001671 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001672 * @param xpos cubemap face in the positive x direction
1673 * @param xneg cubemap face in the negative x direction
1674 * @param ypos cubemap face in the positive y direction
1675 * @param yneg cubemap face in the negative y direction
1676 * @param zpos cubemap face in the positive z direction
1677 * @param zneg cubemap face in the negative z direction
1678 * @param mips specifies desired mipmap behaviour for the cubemap
1679 * @param usage bit field specifying how the cubemap is utilized
1680 *
1681 * @return allocation containing cubemap data
1682 *
1683 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001684 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1685 Bitmap xpos,
1686 Bitmap xneg,
1687 Bitmap ypos,
1688 Bitmap yneg,
1689 Bitmap zpos,
1690 Bitmap zneg,
1691 MipmapControl mips,
1692 int usage) {
1693 int height = xpos.getHeight();
1694 if (xpos.getWidth() != height ||
1695 xneg.getWidth() != height || xneg.getHeight() != height ||
1696 ypos.getWidth() != height || ypos.getHeight() != height ||
1697 yneg.getWidth() != height || yneg.getHeight() != height ||
1698 zpos.getWidth() != height || zpos.getHeight() != height ||
1699 zneg.getWidth() != height || zneg.getHeight() != height) {
1700 throw new RSIllegalArgumentException("Only square cube map faces supported");
1701 }
1702 boolean isPow2 = (height & (height - 1)) == 0;
1703 if (!isPow2) {
1704 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1705 }
1706
1707 Element e = elementFromBitmap(rs, xpos);
1708 Type.Builder tb = new Type.Builder(rs, e);
1709 tb.setX(height);
1710 tb.setY(height);
1711 tb.setFaces(true);
1712 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1713 Type t = tb.create();
1714 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1715
1716 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001717 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001718 adapter.copyFrom(xpos);
1719 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1720 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001721 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001722 adapter.copyFrom(ypos);
1723 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1724 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001725 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001726 adapter.copyFrom(zpos);
1727 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1728 adapter.copyFrom(zneg);
1729
1730 return cubemap;
1731 }
1732
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001733 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001734 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
1735 * from 6 {@link android.graphics.Bitmap} objects containing the cube
1736 * faces. Each face must be a square, have the same size as all other faces,
1737 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001738 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001739 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001740 * @param xpos cubemap face in the positive x direction
1741 * @param xneg cubemap face in the negative x direction
1742 * @param ypos cubemap face in the positive y direction
1743 * @param yneg cubemap face in the negative y direction
1744 * @param zpos cubemap face in the positive z direction
1745 * @param zneg cubemap face in the negative z direction
1746 *
1747 * @return allocation containing cubemap data
1748 *
1749 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001750 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1751 Bitmap xpos,
1752 Bitmap xneg,
1753 Bitmap ypos,
1754 Bitmap yneg,
1755 Bitmap zpos,
1756 Bitmap zneg) {
1757 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1758 zpos, zneg, MipmapControl.MIPMAP_NONE,
1759 USAGE_GRAPHICS_TEXTURE);
1760 }
1761
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001762 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001763 * Creates an Allocation from the Bitmap referenced
1764 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001765 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001766 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001767 * @param res application resources
1768 * @param id resource id to load the data from
1769 * @param mips specifies desired mipmap behaviour for the
1770 * allocation
1771 * @param usage bit field specifying how the allocation is
1772 * utilized
1773 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001774 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001775 *
1776 */
Jason Sams5476b452010-12-08 16:14:36 -08001777 static public Allocation createFromBitmapResource(RenderScript rs,
1778 Resources res,
1779 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001780 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001781 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001782
Jason Sams771bebb2009-12-07 12:40:12 -08001783 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07001784 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
1785 throw new RSIllegalArgumentException("Unsupported usage specified.");
1786 }
Jason Sams5476b452010-12-08 16:14:36 -08001787 Bitmap b = BitmapFactory.decodeResource(res, id);
1788 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1789 b.recycle();
1790 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001791 }
1792
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001793 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001794 * Creates a non-mipmapped Allocation to use as a graphics texture from the
1795 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001796 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001797 * <p>With target API version 18 or greater, this allocation will be created
1798 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
1799 * target API version 17 or lower, this allocation will be created with
1800 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08001801 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001802 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001803 * @param res application resources
1804 * @param id resource id to load the data from
1805 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001806 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001807 *
1808 */
Jason Sams5476b452010-12-08 16:14:36 -08001809 static public Allocation createFromBitmapResource(RenderScript rs,
1810 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001811 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08001812 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1813 return createFromBitmapResource(rs, res, id,
1814 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07001815 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08001816 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001817 return createFromBitmapResource(rs, res, id,
1818 MipmapControl.MIPMAP_NONE,
1819 USAGE_GRAPHICS_TEXTURE);
1820 }
1821
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001822 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001823 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001824 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001825 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001826 * @param str string to create the allocation from
1827 * @param usage bit field specifying how the allocaiton is
1828 * utilized
1829 *
1830 */
Jason Sams5476b452010-12-08 16:14:36 -08001831 static public Allocation createFromString(RenderScript rs,
1832 String str,
1833 int usage) {
1834 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001835 byte[] allocArray = null;
1836 try {
1837 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001838 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001839 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001840 return alloc;
1841 }
1842 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001843 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001844 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001845 }
Jason Sams739c8262013-04-11 18:07:52 -07001846
1847 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001848 * Interface to handle notification when new buffers are available via
1849 * {@link #USAGE_IO_INPUT}. An application will receive one notification
1850 * when a buffer is available. Additional buffers will not trigger new
1851 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07001852 */
Jason Sams42ef2382013-08-29 13:30:59 -07001853 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07001854 public void onBufferAvailable(Allocation a);
1855 }
1856
1857 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001858 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07001859 *
Jason Sams42ef2382013-08-29 13:30:59 -07001860 * @param callback instance of the OnBufferAvailableListener
1861 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07001862 */
Jason Sams42ef2382013-08-29 13:30:59 -07001863 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07001864 synchronized(mAllocationMap) {
Tim Murray7a629fa2013-11-19 12:45:54 -08001865 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07001866 mBufferNotifier = callback;
1867 }
1868 }
1869
1870 static void sendBufferNotification(int id) {
1871 synchronized(mAllocationMap) {
Tim Murray7a629fa2013-11-19 12:45:54 -08001872 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07001873
1874 if ((a != null) && (a.mBufferNotifier != null)) {
1875 a.mBufferNotifier.onBufferAvailable(a);
1876 }
1877 }
1878 }
1879
Jason Samsb8c5a842009-07-31 20:40:47 -07001880}
1881