blob: 4e895669653e654d4d81fbeae07e9a0af0ba02b3 [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
Stephen Hines9069ee82012-02-13 18:25:54 -08002 * Copyright (C) 2008-2012 The Android Open Source Project
Jason Samsb8c5a842009-07-31 20:40:47 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
Jason Sams739c8262013-04-11 18:07:52 -070019import java.util.HashMap;
Jason Samsb8c5a842009-07-31 20:40:47 -070020import android.content.res.Resources;
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
Jason Samsfb9aa9f2012-03-28 15:30:07 -070023import android.view.Surface;
Jason Samsb8c5a842009-07-31 20:40:47 -070024import android.util.Log;
Tim Murrayabd5db92013-02-28 11:45:22 -080025import android.graphics.Canvas;
Tim Murray6d7a53c2013-05-23 16:59:23 -070026import android.os.Trace;
Jason Samsb8c5a842009-07-31 20:40:47 -070027
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070028/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070029 * <p> This class provides the primary method through which data is passed to
30 * and from RenderScript kernels. An Allocation provides the backing store for
31 * a given {@link android.renderscript.Type}. </p>
Jason Samsa23d4e72011-01-04 18:59:12 -080032 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070033 * <p>An Allocation also contains a set of usage flags that denote how the
34 * Allocation could be used. For example, an Allocation may have usage flags
35 * specifying that it can be used from a script as well as input to a {@link
36 * android.renderscript.Sampler}. A developer must synchronize across these
37 * different usages using {@link android.renderscript.Allocation#syncAll} in
38 * order to ensure that different users of the Allocation have a consistent view
39 * of memory. For example, in the case where an Allocation is used as the output
40 * of one kernel and as Sampler input in a later kernel, a developer must call
41 * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
42 * second kernel to ensure correctness.
Jason Samsa23d4e72011-01-04 18:59:12 -080043 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070044 * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
45 * more complex Element types, the {@link #copyFromUnchecked} methods can be
46 * used to copy from byte arrays or similar constructs.</p>
Jason Samsb8c5a842009-07-31 20:40:47 -070047 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080048 * <div class="special reference">
49 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070050 * <p>For more information about creating an application that uses RenderScript, read the
51 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080052 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070053 **/
54public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070055 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080056 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080057 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070058 Allocation mAdaptedAllocation;
Tim Murray2f2472c2013-08-22 14:55:26 -070059 int mSize;
Jason Samsba862d12011-07-07 15:24:42 -070060
61 boolean mConstrainedLOD;
62 boolean mConstrainedFace;
63 boolean mConstrainedY;
64 boolean mConstrainedZ;
Jason Sams615e7ce2012-01-13 14:01:20 -080065 boolean mReadAllowed = true;
66 boolean mWriteAllowed = true;
Jason Samsba862d12011-07-07 15:24:42 -070067 int mSelectedY;
68 int mSelectedZ;
69 int mSelectedLOD;
70 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
71
72 int mCurrentDimX;
73 int mCurrentDimY;
74 int mCurrentDimZ;
75 int mCurrentCount;
Tim Murray460a0492013-11-19 12:45:54 -080076 static HashMap<Long, Allocation> mAllocationMap =
77 new HashMap<Long, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070078 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070079
Jason Sams3042d262013-11-25 18:28:33 -080080 private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
81 final Class c = d.getClass();
82 if (!c.isArray()) {
83 throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
84 }
85 final Class cmp = c.getComponentType();
86 if (!cmp.isPrimitive()) {
87 throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
88 }
89
90 if (cmp == Long.TYPE) {
91 if (checkType) {
92 validateIsInt64();
93 return mType.mElement.mType;
94 }
95 return Element.DataType.SIGNED_64;
96 }
97
98 if (cmp == Integer.TYPE) {
99 if (checkType) {
100 validateIsInt32();
101 return mType.mElement.mType;
102 }
103 return Element.DataType.SIGNED_32;
104 }
105
106 if (cmp == Short.TYPE) {
107 if (checkType) {
108 validateIsInt16();
109 return mType.mElement.mType;
110 }
111 return Element.DataType.SIGNED_16;
112 }
113
114 if (cmp == Byte.TYPE) {
115 if (checkType) {
116 validateIsInt8();
117 return mType.mElement.mType;
118 }
119 return Element.DataType.SIGNED_8;
120 }
121
122 if (cmp == Float.TYPE) {
123 if (checkType) {
124 validateIsFloat32();
125 }
126 return Element.DataType.FLOAT_32;
127 }
128
129 if (cmp == Double.TYPE) {
130 if (checkType) {
131 validateIsFloat64();
132 }
133 return Element.DataType.FLOAT_64;
134 }
135 return null;
136 }
137
138
Tim Murrayc11e25c2013-04-09 11:01:01 -0700139 /**
140 * The usage of the Allocation. These signal to RenderScript where to place
141 * the Allocation in memory.
142 *
143 */
Jason Sams5476b452010-12-08 16:14:36 -0800144
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700145 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700146 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -0800147 */
Jason Sams5476b452010-12-08 16:14:36 -0800148 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800149
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700150 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700151 * The Allocation will be used as a texture source by one or more graphics
152 * programs.
Jason Samsf7086092011-01-12 13:28:37 -0800153 *
154 */
Jason Sams5476b452010-12-08 16:14:36 -0800155 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800156
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700157 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700158 * The Allocation will be used as a graphics mesh.
159 *
160 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800161 *
162 */
Jason Sams5476b452010-12-08 16:14:36 -0800163 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800164
165
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700166 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700167 * The Allocation will be used as the source of shader constants by one or
168 * more programs.
169 *
170 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800171 *
172 */
Jason Sams5476b452010-12-08 16:14:36 -0800173 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
174
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700175 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700176 * The Allocation will be used as a target for offscreen rendering
177 *
178 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700179 *
180 */
181 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
182
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700183 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700184 * The Allocation will be used as a {@link android.view.Surface}
185 * consumer. This usage will cause the Allocation to be created
186 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800187 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800188 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700189 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800190
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700191 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700192 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700193 * producer. The dimensions and format of the {@link
Jason Sams3a1b8e42013-09-24 15:18:52 -0700194 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700195 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800196 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800197 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700198 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700199
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700200 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700201 * The Allocation's backing store will be inherited from another object
202 * (usually a {@link android.graphics.Bitmap}); copying to or from the
203 * original source Bitmap will cause a synchronization rather than a full
204 * copy. {@link #syncAll} may also be used to synchronize the Allocation
205 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800206 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700207 * <p>This is set by default for allocations created with {@link
208 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800209 *
210 */
211 public static final int USAGE_SHARED = 0x0080;
212
213 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700214 * Controls mipmap behavior when using the bitmap creation and update
215 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800216 */
Jason Sams4ef66502010-12-10 16:03:15 -0800217 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700218 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700219 * No mipmaps will be generated and the type generated from the incoming
220 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800221 */
Jason Sams5476b452010-12-08 16:14:36 -0800222 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800223
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700224 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700225 * A full mipmap chain will be created in script memory. The Type of
226 * the Allocation will contain a full mipmap chain. On upload, the full
227 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800228 */
Jason Sams5476b452010-12-08 16:14:36 -0800229 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800230
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700231 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700232 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
233 * not contain mipmaps. On upload, the allocation data will contain a
234 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800235 */
Jason Sams5476b452010-12-08 16:14:36 -0800236 MIPMAP_ON_SYNC_TO_TEXTURE(2);
237
238 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800239 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800240 mID = id;
241 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700242 }
243
Jason Sams48fe5342011-07-08 13:52:30 -0700244
Tim Murray460a0492013-11-19 12:45:54 -0800245 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700246 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700247 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700248 }
Jason Samse07694b2012-04-03 15:36:36 -0700249 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700250 }
251
Jason Sams03d2d002012-03-23 13:51:56 -0700252
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700253 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700254 * Get the {@link android.renderscript.Element} of the {@link
255 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700256 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700257 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700258 *
259 */
260 public Element getElement() {
261 return mType.getElement();
262 }
263
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700264 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700265 * Get the usage flags of the Allocation.
266 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700267 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700268 *
269 */
270 public int getUsage() {
271 return mUsage;
272 }
273
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700274 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700275 * Get the size of the Allocation in bytes.
276 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700277 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700278 *
279 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700280 public int getBytesSize() {
Tim Murray04f0d6e2013-12-17 17:15:25 -0800281 if (mType.mDimYuv != 0) {
282 return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
283 }
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700284 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700285 }
286
Jason Sams452a7662011-07-07 16:05:18 -0700287 private void updateCacheInfo(Type t) {
288 mCurrentDimX = t.getX();
289 mCurrentDimY = t.getY();
290 mCurrentDimZ = t.getZ();
291 mCurrentCount = mCurrentDimX;
292 if (mCurrentDimY > 1) {
293 mCurrentCount *= mCurrentDimY;
294 }
295 if (mCurrentDimZ > 1) {
296 mCurrentCount *= mCurrentDimZ;
297 }
298 }
Jason Samsba862d12011-07-07 15:24:42 -0700299
Tim Murraya3145512012-12-04 17:59:29 -0800300 private void setBitmap(Bitmap b) {
301 mBitmap = b;
302 }
303
Tim Murray460a0492013-11-19 12:45:54 -0800304 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700305 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800306 if ((usage & ~(USAGE_SCRIPT |
307 USAGE_GRAPHICS_TEXTURE |
308 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700309 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800310 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800311 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800312 USAGE_IO_OUTPUT |
313 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800314 throw new RSIllegalArgumentException("Unknown usage specified.");
315 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800316
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700317 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800318 mWriteAllowed = false;
319
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700320 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800321 USAGE_GRAPHICS_TEXTURE |
322 USAGE_SCRIPT)) != 0) {
323 throw new RSIllegalArgumentException("Invalid usage combination.");
324 }
325 }
Jason Sams9bf18922013-04-13 19:48:36 -0700326
Jason Sams5476b452010-12-08 16:14:36 -0800327 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800328 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700329
Jason Sams452a7662011-07-07 16:05:18 -0700330 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700331 // TODO: A3D doesn't have Type info during creation, so we can't
332 // calculate the size ahead of time. We can possibly add a method
333 // to update the size in the future if it seems reasonable.
334 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700335 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700336 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700337 try {
338 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
339 } catch (Exception e) {
340 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
341 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
342 }
343 }
344
345 protected void finalize() throws Throwable {
346 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
347 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700348 }
349
Jason Sams3042d262013-11-25 18:28:33 -0800350 private void validateIsInt64() {
351 if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
352 (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
353 return;
354 }
355 throw new RSIllegalArgumentException(
356 "64 bit integer source does not match allocation type " + mType.mElement.mType);
357 }
358
Jason Samsb97b2512011-01-16 15:04:08 -0800359 private void validateIsInt32() {
360 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
361 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
362 return;
363 }
364 throw new RSIllegalArgumentException(
365 "32 bit integer source does not match allocation type " + mType.mElement.mType);
366 }
367
368 private void validateIsInt16() {
369 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
370 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
371 return;
372 }
373 throw new RSIllegalArgumentException(
374 "16 bit integer source does not match allocation type " + mType.mElement.mType);
375 }
376
377 private void validateIsInt8() {
378 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
379 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
380 return;
381 }
382 throw new RSIllegalArgumentException(
383 "8 bit integer source does not match allocation type " + mType.mElement.mType);
384 }
385
386 private void validateIsFloat32() {
387 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
388 return;
389 }
390 throw new RSIllegalArgumentException(
391 "32 bit float source does not match allocation type " + mType.mElement.mType);
392 }
393
Jason Sams3042d262013-11-25 18:28:33 -0800394 private void validateIsFloat64() {
395 if (mType.mElement.mType == Element.DataType.FLOAT_64) {
396 return;
397 }
398 throw new RSIllegalArgumentException(
399 "64 bit float source does not match allocation type " + mType.mElement.mType);
400 }
401
Jason Samsb97b2512011-01-16 15:04:08 -0800402 private void validateIsObject() {
403 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
404 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
405 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
406 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
407 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
408 (mType.mElement.mType == Element.DataType.RS_MESH) ||
409 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
410 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
411 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
412 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
413 return;
414 }
415 throw new RSIllegalArgumentException(
416 "Object source does not match allocation type " + mType.mElement.mType);
417 }
418
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700419 @Override
420 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800421 super.updateFromNative();
Tim Murray460a0492013-11-19 12:45:54 -0800422 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700423 if(typeID != 0) {
424 mType = new Type(typeID, mRS);
425 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700426 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700427 }
428 }
429
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700430 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700431 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700432 *
433 * @return Type
434 *
435 */
Jason Samsea87e962010-01-12 12:12:28 -0800436 public Type getType() {
437 return mType;
438 }
439
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700440 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700441 * Propagate changes from one usage of the Allocation to the
442 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700443 *
444 */
Jason Sams5476b452010-12-08 16:14:36 -0800445 public void syncAll(int srcLocation) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700446 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
Jason Sams5476b452010-12-08 16:14:36 -0800447 switch (srcLocation) {
Jason Sams5476b452010-12-08 16:14:36 -0800448 case USAGE_GRAPHICS_TEXTURE:
Tim Murray78e64942013-04-09 17:28:56 -0700449 case USAGE_SCRIPT:
450 if ((mUsage & USAGE_SHARED) != 0) {
451 copyFrom(mBitmap);
452 }
453 break;
454 case USAGE_GRAPHICS_CONSTANTS:
Jason Sams5476b452010-12-08 16:14:36 -0800455 case USAGE_GRAPHICS_VERTEX:
456 break;
Tim Murray78e64942013-04-09 17:28:56 -0700457 case USAGE_SHARED:
458 if ((mUsage & USAGE_SHARED) != 0) {
459 copyTo(mBitmap);
460 }
461 break;
Jason Sams5476b452010-12-08 16:14:36 -0800462 default:
463 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
464 }
465 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700466 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700467 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800468 }
469
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700470 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700471 * Send a buffer to the output stream. The contents of the Allocation will
472 * be undefined after this operation. This operation is only valid if {@link
473 * #USAGE_IO_OUTPUT} is set on the Allocation.
474 *
Jason Sams163766c2012-02-15 12:04:24 -0800475 *
Jason Sams163766c2012-02-15 12:04:24 -0800476 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700477 public void ioSend() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700478 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
Jason Sams163766c2012-02-15 12:04:24 -0800479 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
480 throw new RSIllegalArgumentException(
481 "Can only send buffer if IO_OUTPUT usage specified.");
482 }
483 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700484 mRS.nAllocationIoSend(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700485 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800486 }
487
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700488 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700489 * Receive the latest input into the Allocation. This operation
490 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800491 *
Jason Sams163766c2012-02-15 12:04:24 -0800492 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700493 public void ioReceive() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700494 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
Jason Sams163766c2012-02-15 12:04:24 -0800495 if ((mUsage & USAGE_IO_INPUT) == 0) {
496 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700497 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800498 }
499 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700500 mRS.nAllocationIoReceive(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700501 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800502 }
503
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700504 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700505 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700506 *
507 * @param d Source array.
508 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800509 public void copyFrom(BaseObj[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700510 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Sams771bebb2009-12-07 12:40:12 -0800511 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800512 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700513 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800514 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700515 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800516 }
Tim Murray460a0492013-11-19 12:45:54 -0800517
Tim Murray3de3dc72014-07-01 16:56:18 -0700518 if (RenderScript.sPointerSize == 8) {
519 long i[] = new long[d.length * 4];
520 for (int ct=0; ct < d.length; ct++) {
521 i[ct * 4] = d[ct].getID(mRS);
522 }
523 copy1DRangeFromUnchecked(0, mCurrentCount, i);
524 } else {
525 int i[] = new int[d.length];
526 for (int ct=0; ct < d.length; ct++) {
527 i[ct] = (int)d[ct].getID(mRS);
528 }
529 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800530 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700531 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700532 }
533
Jason Samsfb9f82c2011-01-12 14:53:25 -0800534 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800535 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800536 if (bc == null) {
537 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
538 }
Jason Sams252c0782011-01-11 17:42:52 -0800539 switch (bc) {
540 case ALPHA_8:
541 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
542 throw new RSIllegalArgumentException("Allocation kind is " +
543 mType.getElement().mKind + ", type " +
544 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700545 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800546 " bytes, passed bitmap was " + bc);
547 }
548 break;
549 case ARGB_8888:
550 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700551 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800552 throw new RSIllegalArgumentException("Allocation kind is " +
553 mType.getElement().mKind + ", type " +
554 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700555 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800556 " bytes, passed bitmap was " + bc);
557 }
558 break;
559 case RGB_565:
560 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700561 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800562 throw new RSIllegalArgumentException("Allocation kind is " +
563 mType.getElement().mKind + ", type " +
564 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700565 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800566 " bytes, passed bitmap was " + bc);
567 }
568 break;
569 case ARGB_4444:
570 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700571 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800572 throw new RSIllegalArgumentException("Allocation kind is " +
573 mType.getElement().mKind + ", type " +
574 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700575 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800576 " bytes, passed bitmap was " + bc);
577 }
578 break;
579
580 }
Jason Sams4ef66502010-12-10 16:03:15 -0800581 }
582
Jason Samsfb9f82c2011-01-12 14:53:25 -0800583 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700584 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800585 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
586 }
587 }
588
Jason Sams3042d262013-11-25 18:28:33 -0800589 private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
590 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
591 mRS.validate();
592 if (mCurrentDimZ > 0) {
593 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
594 } else if (mCurrentDimY > 0) {
595 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
596 } else {
597 copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
598 }
599 Trace.traceEnd(RenderScript.TRACE_TAG);
600 }
601
602 /**
603 * Copy into this Allocation from an array. This method does not guarantee
604 * that the Allocation is compatible with the input buffer; it copies memory
605 * without reinterpretation.
606 *
607 * @param array The source data array
608 */
609 public void copyFromUnchecked(Object array) {
610 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
611 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
612 java.lang.reflect.Array.getLength(array));
613 Trace.traceEnd(RenderScript.TRACE_TAG);
614 }
615
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700616 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700617 * Copy into this Allocation from an array. This method does not guarantee
618 * that the Allocation is compatible with the input buffer; it copies memory
619 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800620 *
621 * @param d the source data array
622 */
623 public void copyFromUnchecked(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800624 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800625 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700626
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700627 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700628 * Copy into this Allocation from an array. This method does not guarantee
629 * that the Allocation is compatible with the input buffer; it copies memory
630 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800631 *
632 * @param d the source data array
633 */
634 public void copyFromUnchecked(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800635 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800636 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700637
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700638 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700639 * Copy into this Allocation from an array. This method does not guarantee
640 * that the Allocation is compatible with the input buffer; it copies memory
641 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800642 *
643 * @param d the source data array
644 */
645 public void copyFromUnchecked(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800646 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800647 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700648
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700649 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700650 * Copy into this Allocation from an array. This method does not guarantee
651 * that the Allocation is compatible with the input buffer; it copies memory
652 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800653 *
654 * @param d the source data array
655 */
656 public void copyFromUnchecked(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800657 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800658 }
659
Tim Murray6d7a53c2013-05-23 16:59:23 -0700660
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700661 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700662 * Copy into this Allocation from an array. This variant is type checked
663 * and will generate exceptions if the Allocation's {@link
Jason Sams3042d262013-11-25 18:28:33 -0800664 * android.renderscript.Element} does not match the array's
665 * primitive type.
666 *
Ying Wang16229812013-11-26 15:45:12 -0800667 * @param array The source data array
Jason Sams3042d262013-11-25 18:28:33 -0800668 */
669 public void copyFrom(Object array) {
670 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
671 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
672 java.lang.reflect.Array.getLength(array));
673 Trace.traceEnd(RenderScript.TRACE_TAG);
674 }
675
676 /**
677 * Copy into this Allocation from an array. This variant is type checked
678 * and will generate exceptions if the Allocation's {@link
Tim Murrayc11e25c2013-04-09 11:01:01 -0700679 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800680 *
681 * @param d the source data array
682 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800683 public void copyFrom(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800684 validateIsInt32();
685 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800686 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800687
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700688 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700689 * Copy into this Allocation from an array. This variant is type checked
690 * and will generate exceptions if the Allocation's {@link
691 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800692 *
693 * @param d the source data array
694 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800695 public void copyFrom(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800696 validateIsInt16();
697 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800698 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800699
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700700 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700701 * Copy into this Allocation from an array. This variant is type checked
702 * and will generate exceptions if the Allocation's {@link
703 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800704 *
705 * @param d the source data array
706 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800707 public void copyFrom(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800708 validateIsInt8();
709 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800710 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800711
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700712 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700713 * Copy into this Allocation from an array. This variant is type checked
714 * and will generate exceptions if the Allocation's {@link
715 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800716 *
717 * @param d the source data array
718 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800719 public void copyFrom(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800720 validateIsFloat32();
721 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800722 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800723
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700724 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700725 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
726 * height, width, and format of the bitmap must match the existing
727 * allocation.
728 *
729 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
730 * android.graphics.Bitmap} used to create the Allocation with {@link
731 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
732 * this will synchronize the Allocation with the latest data from the {@link
733 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800734 *
735 * @param b the source bitmap
736 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800737 public void copyFrom(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700738 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsfb9f82c2011-01-12 14:53:25 -0800739 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -0800740 if (b.getConfig() == null) {
741 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
742 Canvas c = new Canvas(newBitmap);
743 c.drawBitmap(b, 0, 0, null);
744 copyFrom(newBitmap);
745 return;
746 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800747 validateBitmapSize(b);
748 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700749 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700750 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700751 }
752
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700753 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700754 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700755 * must be identical.
756 *
757 * @param a the source allocation
758 */
759 public void copyFrom(Allocation a) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700760 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Tim Murrayf671fb02012-10-03 13:50:05 -0700761 mRS.validate();
762 if (!mType.equals(a.getType())) {
763 throw new RSIllegalArgumentException("Types of allocations must match.");
764 }
765 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700766 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700767 }
768
Tim Murrayf671fb02012-10-03 13:50:05 -0700769 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700770 * This is only intended to be used by auto-generated code reflected from
771 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800772 *
773 * @param xoff
774 * @param fp
775 */
Jason Sams21b41032011-01-16 15:05:41 -0800776 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800777 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700778 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700779 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700780 int data_length = fp.getPos();
Jason Samsa70f4162010-03-26 15:33:42 -0700781
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700782 int count = data_length / eSize;
783 if ((eSize * count) != data_length) {
784 throw new RSIllegalArgumentException("Field packer length " + data_length +
Jason Samsa70f4162010-03-26 15:33:42 -0700785 " not divisible by element size " + eSize + ".");
786 }
Jason Samsba862d12011-07-07 15:24:42 -0700787 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700788 }
789
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700790 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700791 * This is only intended to be used by auto-generated code reflected from
792 * the RenderScript script files.
Jason Samsfa445b92011-01-07 17:00:07 -0800793 *
794 * @param xoff
795 * @param component_number
796 * @param fp
797 */
Jason Sams21b41032011-01-16 15:05:41 -0800798 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800799 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700800 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800801 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700802 }
803 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800804 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700805 }
806
807 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700808 int data_length = fp.getPos();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700809 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800810 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700811
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700812 if (data_length != eSize) {
813 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700814 " does not match component size " + eSize + ".");
815 }
816
Jason Sams48fe5342011-07-08 13:52:30 -0700817 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700818 component_number, data, data_length);
Jason Samsa70f4162010-03-26 15:33:42 -0700819 }
820
Jason Sams768bc022009-09-21 19:41:04 -0700821 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800822 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700823 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800824 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700825 }
826 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800827 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700828 }
Jason Samsba862d12011-07-07 15:24:42 -0700829 if((off + count) > mCurrentCount) {
830 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700831 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700832 }
Jason Samsba862d12011-07-07 15:24:42 -0700833 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800834 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700835 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700836 }
837
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700838 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700839 * Generate a mipmap chain. This is only valid if the Type of the Allocation
840 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800841 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700842 * <p>This function will generate a complete set of mipmaps from the top
843 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800844 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700845 * <p>If the Allocation is also using other memory spaces, a call to {@link
846 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800847 */
848 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700849 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800850 }
851
Jason Sams3042d262013-11-25 18:28:33 -0800852 private void copy1DRangeFromUnchecked(int off, int count, Object array,
853 Element.DataType dt, int arrayLen) {
854 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
855 final int dataSize = mType.mElement.getBytesSize() * count;
856 data1DChecks(off, count, arrayLen * dt.mSize, dataSize);
857 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt);
858 Trace.traceEnd(RenderScript.TRACE_TAG);
859 }
860
861 /**
862 * Copy an array into part of this Allocation. This method does not
863 * guarantee that the Allocation is compatible with the input buffer.
864 *
865 * @param off The offset of the first element to be copied.
866 * @param count The number of elements to be copied.
867 * @param array The source data array
868 */
869 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
870 copy1DRangeFromUnchecked(off, count, array,
871 validateObjectIsPrimitiveArray(array, false),
872 java.lang.reflect.Array.getLength(array));
873 }
874
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700875 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700876 * Copy an array into part of this Allocation. This method does not
877 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800878 *
879 * @param off The offset of the first element to be copied.
880 * @param count The number of elements to be copied.
881 * @param d the source data array
882 */
883 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800884 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700885 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700886
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700887 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700888 * Copy an array into part of this Allocation. This method does not
889 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800890 *
891 * @param off The offset of the first element to be copied.
892 * @param count The number of elements to be copied.
893 * @param d the source data array
894 */
895 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800896 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700897 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700898
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700899 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700900 * Copy an array into part of this Allocation. This method does not
901 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800902 *
903 * @param off The offset of the first element to be copied.
904 * @param count The number of elements to be copied.
905 * @param d the source data array
906 */
907 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800908 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700909 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700910
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700911 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700912 * Copy an array into part of this Allocation. This method does not
913 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800914 *
915 * @param off The offset of the first element to be copied.
916 * @param count The number of elements to be copied.
917 * @param d the source data array
918 */
919 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800920 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
921 }
922
923
924 /**
925 * Copy an array into part of this Allocation. This variant is type checked
926 * and will generate exceptions if the Allocation type does not
927 * match the component type of the array passed in.
928 *
929 * @param off The offset of the first element to be copied.
930 * @param count The number of elements to be copied.
931 * @param array The source data array.
932 */
933 public void copy1DRangeFrom(int off, int count, Object array) {
934 copy1DRangeFromUnchecked(off, count, array,
935 validateObjectIsPrimitiveArray(array, true),
936 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -0700937 }
938
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700939 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700940 * Copy an array into part of this Allocation. This variant is type checked
941 * and will generate exceptions if the Allocation type is not a 32 bit
942 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800943 *
944 * @param off The offset of the first element to be copied.
945 * @param count The number of elements to be copied.
946 * @param d the source data array
947 */
Jason Samsb97b2512011-01-16 15:04:08 -0800948 public void copy1DRangeFrom(int off, int count, int[] d) {
949 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -0800950 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800951 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800952
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700953 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700954 * Copy an array into part of this Allocation. This variant is type checked
955 * and will generate exceptions if the Allocation type is not a 16 bit
956 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800957 *
958 * @param off The offset of the first element to be copied.
959 * @param count The number of elements to be copied.
960 * @param d the source data array
961 */
Jason Samsb97b2512011-01-16 15:04:08 -0800962 public void copy1DRangeFrom(int off, int count, short[] d) {
963 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -0800964 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800965 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800966
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700967 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700968 * Copy an array into part of this Allocation. This variant is type checked
969 * and will generate exceptions if the Allocation type is not an 8 bit
970 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800971 *
972 * @param off The offset of the first element to be copied.
973 * @param count The number of elements to be copied.
974 * @param d the source data array
975 */
Jason Samsb97b2512011-01-16 15:04:08 -0800976 public void copy1DRangeFrom(int off, int count, byte[] d) {
977 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -0800978 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800979 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800980
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700981 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700982 * Copy an array into part of this Allocation. This variant is type checked
983 * and will generate exceptions if the Allocation type is not a 32 bit float
984 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800985 *
986 * @param off The offset of the first element to be copied.
987 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700988 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800989 */
Jason Samsb97b2512011-01-16 15:04:08 -0800990 public void copy1DRangeFrom(int off, int count, float[] d) {
991 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -0800992 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800993 }
Jason Sams3042d262013-11-25 18:28:33 -0800994
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700995 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700996 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700997 *
998 * @param off The offset of the first element to be copied.
999 * @param count The number of elements to be copied.
1000 * @param data the source data allocation.
1001 * @param dataOff off The offset of the first element in data to
1002 * be copied.
1003 */
1004 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001005 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -07001006 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001007 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001008 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001009 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001010 }
1011
Jason Samsfb9f82c2011-01-12 14:53:25 -08001012 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001013 if (mAdaptedAllocation != null) {
1014
1015 } else {
1016
1017 if (xoff < 0 || yoff < 0) {
1018 throw new RSIllegalArgumentException("Offset cannot be negative.");
1019 }
1020 if (h < 0 || w < 0) {
1021 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1022 }
1023 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1024 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1025 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001026 }
1027 }
Jason Sams768bc022009-09-21 19:41:04 -07001028
Jason Sams3042d262013-11-25 18:28:33 -08001029 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1030 Element.DataType dt, int arrayLen) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001031 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001032 mRS.validate();
1033 validate2DRange(xoff, yoff, w, h);
Jason Sams3042d262013-11-25 18:28:33 -08001034 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1035 array, arrayLen * dt.mSize, dt);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001036 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001037 }
1038
Jason Sams3042d262013-11-25 18:28:33 -08001039 /**
1040 * Copy from an array into a rectangular region in this Allocation. The
1041 * array is assumed to be tightly packed.
1042 *
1043 * @param xoff X offset of the region to update in this Allocation
1044 * @param yoff Y offset of the region to update in this Allocation
1045 * @param w Width of the region to update
1046 * @param h Height of the region to update
Ying Wang16229812013-11-26 15:45:12 -08001047 * @param array Data to be placed into the Allocation
Jason Sams3042d262013-11-25 18:28:33 -08001048 */
1049 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
1050 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1051 copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
1052 validateObjectIsPrimitiveArray(array, true),
1053 java.lang.reflect.Array.getLength(array));
Tim Murray6d7a53c2013-05-23 16:59:23 -07001054 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001055 }
1056
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001057 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001058 * Copy from an array into a rectangular region in this Allocation. The
1059 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -08001060 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001061 * @param xoff X offset of the region to update in this Allocation
1062 * @param yoff Y offset of the region to update in this Allocation
1063 * @param w Width of the region to update
1064 * @param h Height of the region to update
1065 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001066 */
1067 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001068 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001069 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1070 Element.DataType.SIGNED_8, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001071 }
1072
Tim Murrayc11e25c2013-04-09 11:01:01 -07001073 /**
1074 * Copy from an array into a rectangular region in this Allocation. The
1075 * array is assumed to be tightly packed.
1076 *
1077 * @param xoff X offset of the region to update in this Allocation
1078 * @param yoff Y offset of the region to update in this Allocation
1079 * @param w Width of the region to update
1080 * @param h Height of the region to update
1081 * @param data to be placed into the Allocation
1082 */
Jason Samsf7086092011-01-12 13:28:37 -08001083 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001084 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001085 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1086 Element.DataType.SIGNED_16, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001087 }
1088
Tim Murrayc11e25c2013-04-09 11:01:01 -07001089 /**
1090 * Copy from an array into a rectangular region in this Allocation. The
1091 * array is assumed to be tightly packed.
1092 *
1093 * @param xoff X offset of the region to update in this Allocation
1094 * @param yoff Y offset of the region to update in this Allocation
1095 * @param w Width of the region to update
1096 * @param h Height of the region to update
1097 * @param data to be placed into the Allocation
1098 */
Jason Samsf7086092011-01-12 13:28:37 -08001099 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001100 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001101 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1102 Element.DataType.SIGNED_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001103 }
1104
Tim Murrayc11e25c2013-04-09 11:01:01 -07001105 /**
1106 * Copy from an array into a rectangular region in this Allocation. The
1107 * array is assumed to be tightly packed.
1108 *
1109 * @param xoff X offset of the region to update in this Allocation
1110 * @param yoff Y offset of the region to update in this Allocation
1111 * @param w Width of the region to update
1112 * @param h Height of the region to update
1113 * @param data to be placed into the Allocation
1114 */
Jason Samsf7086092011-01-12 13:28:37 -08001115 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001116 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001117 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1118 Element.DataType.FLOAT_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001119 }
1120
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001121 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001122 * Copy a rectangular region from an Allocation into a rectangular region in
1123 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001124 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001125 * @param xoff X offset of the region in this Allocation
1126 * @param yoff Y offset of the region in this Allocation
1127 * @param w Width of the region to update.
1128 * @param h Height of the region to update.
1129 * @param data source Allocation.
1130 * @param dataXoff X offset in source Allocation
1131 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001132 */
1133 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1134 Allocation data, int dataXoff, int dataYoff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001135 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001136 mRS.validate();
1137 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -07001138 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -07001139 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001140 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -07001141 data.mSelectedLOD, data.mSelectedFace.mID);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001142 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001143 }
1144
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001145 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001146 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1147 * and width of the update will use the height and width of the {@link
1148 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001149 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001150 * @param xoff X offset of the region to update in this Allocation
1151 * @param yoff Y offset of the region to update in this Allocation
1152 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001153 */
1154 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001155 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Jason Samsfa445b92011-01-07 17:00:07 -08001156 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001157 if (data.getConfig() == null) {
1158 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1159 Canvas c = new Canvas(newBitmap);
1160 c.drawBitmap(data, 0, 0, null);
1161 copy2DRangeFrom(xoff, yoff, newBitmap);
Jason Samsb05d6892013-04-09 15:59:24 -07001162 return;
Tim Murrayabd5db92013-02-28 11:45:22 -08001163 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001164 validateBitmapFormat(data);
1165 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -07001166 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001167 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001168 }
1169
Jason Samsb05d6892013-04-09 15:59:24 -07001170 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1171 if (mAdaptedAllocation != null) {
1172
1173 } else {
1174
1175 if (xoff < 0 || yoff < 0 || zoff < 0) {
1176 throw new RSIllegalArgumentException("Offset cannot be negative.");
1177 }
1178 if (h < 0 || w < 0 || d < 0) {
1179 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1180 }
1181 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1182 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1183 }
1184 }
1185 }
1186
1187 /**
1188 * @hide
1189 *
1190 */
Jason Sams3042d262013-11-25 18:28:33 -08001191 private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1192 Object array, Element.DataType dt, int arrayLen) {
1193 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
Jason Samsb05d6892013-04-09 15:59:24 -07001194 mRS.validate();
1195 validate3DRange(xoff, yoff, zoff, w, h, d);
Jason Sams3042d262013-11-25 18:28:33 -08001196 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1197 array, arrayLen * dt.mSize, dt);
1198 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb05d6892013-04-09 15:59:24 -07001199 }
1200
1201 /**
1202 * @hide
Jason Samsb05d6892013-04-09 15:59:24 -07001203 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001204 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001205 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001206 * @param xoff X offset of the region to update in this Allocation
1207 * @param yoff Y offset of the region to update in this Allocation
1208 * @param zoff Z offset of the region to update in this Allocation
1209 * @param w Width of the region to update
1210 * @param h Height of the region to update
1211 * @param d Depth of the region to update
Jason Samsb05d6892013-04-09 15:59:24 -07001212 * @param data to be placed into the allocation
1213 */
Jason Sams3042d262013-11-25 18:28:33 -08001214 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1215 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
1216 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
1217 validateObjectIsPrimitiveArray(array, true),
1218 java.lang.reflect.Array.getLength(array));
1219 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb05d6892013-04-09 15:59:24 -07001220 }
1221
1222 /**
1223 * @hide
1224 * Copy a rectangular region into the allocation from another
1225 * allocation.
1226 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001227 * @param xoff X offset of the region to update in this Allocation
1228 * @param yoff Y offset of the region to update in this Allocation
1229 * @param zoff Z offset of the region to update in this Allocation
1230 * @param w Width of the region to update.
1231 * @param h Height of the region to update.
1232 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001233 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001234 * @param dataXoff X offset of the region in the source Allocation
1235 * @param dataYoff Y offset of the region in the source Allocation
1236 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001237 */
1238 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1239 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1240 mRS.validate();
1241 validate3DRange(xoff, yoff, zoff, w, h, d);
1242 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1243 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1244 data.mSelectedLOD);
1245 }
1246
Jason Samsfa445b92011-01-07 17:00:07 -08001247
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001248 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001249 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1250 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001251 *
1252 * @param b The bitmap to be set from the Allocation.
1253 */
Jason Samsfa445b92011-01-07 17:00:07 -08001254 public void copyTo(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001255 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsfb9f82c2011-01-12 14:53:25 -08001256 mRS.validate();
1257 validateBitmapFormat(b);
1258 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -07001259 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001260 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001261 }
1262
Jason Sams3042d262013-11-25 18:28:33 -08001263 private void copyTo(Object array, Element.DataType dt, int arrayLen) {
1264 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Miao Wangb590b352015-01-15 11:09:23 -08001265 if (dt.mSize * arrayLen < mSize) {
1266 throw new RSIllegalArgumentException(
1267 "Size of output array cannot be smaller than size of allocation.");
1268 }
Jason Sams3042d262013-11-25 18:28:33 -08001269 mRS.validate();
1270 mRS.nAllocationRead(getID(mRS), array, dt);
1271 Trace.traceEnd(RenderScript.TRACE_TAG);
1272 }
1273
1274 /**
1275 * Copy from the Allocation into an array. The array must be at
1276 * least as large as the Allocation. The
1277 * {@link android.renderscript.Element} must match the component
1278 * type of the array passed in.
1279 *
1280 * @param array The array to be set from the Allocation.
1281 */
1282 public void copyTo(Object array) {
1283 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1284 java.lang.reflect.Array.getLength(array));
1285 }
1286
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001287 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001288 * Copy from the Allocation into a byte array. The array must be at least
1289 * as large as the Allocation. The allocation must be of an 8 bit integer
1290 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001291 *
1292 * @param d The array to be set from the Allocation.
1293 */
Jason Samsfa445b92011-01-07 17:00:07 -08001294 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001295 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001296 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001297 }
1298
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001299 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001300 * Copy from the Allocation into a short array. The array must be at least
1301 * as large as the Allocation. The allocation must be of an 16 bit integer
1302 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001303 *
1304 * @param d The array to be set from the Allocation.
1305 */
Jason Samsfa445b92011-01-07 17:00:07 -08001306 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001307 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001308 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001309 }
1310
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001311 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001312 * Copy from the Allocation into a int array. The array must be at least as
1313 * large as the Allocation. The allocation must be of an 32 bit integer
1314 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001315 *
1316 * @param d The array to be set from the Allocation.
1317 */
Jason Samsfa445b92011-01-07 17:00:07 -08001318 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001319 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001320 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001321 }
1322
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001323 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001324 * Copy from the Allocation into a float array. The array must be at least
1325 * as large as the Allocation. The allocation must be of an 32 bit float
1326 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001327 *
1328 * @param d The array to be set from the Allocation.
1329 */
Jason Samsfa445b92011-01-07 17:00:07 -08001330 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001331 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001332 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001333 }
1334
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001335 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001336 * Resize a 1D allocation. The contents of the allocation are preserved.
1337 * If new elements are allocated objects are created with null contents and
1338 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001339 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001340 * <p>If the new region is smaller the references of any objects outside the
1341 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001342 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001343 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001344 *
1345 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001346 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001347 * @deprecated RenderScript objects should be immutable once created. The
Tim Murraycd38b762014-08-13 13:20:25 -07001348 * replacement is to create a new allocation and copy the contents. This
1349 * function will throw an exception if API 21 or higher is used.
Jason Samsf7086092011-01-12 13:28:37 -08001350 */
Jason Sams31a7e422010-10-26 13:09:17 -07001351 public synchronized void resize(int dimX) {
Tim Murraycd38b762014-08-13 13:20:25 -07001352 if (mRS.getApplicationContext().getApplicationInfo().targetSdkVersion >= 21) {
1353 throw new RSRuntimeException("Resize is not allowed in API 21+.");
1354 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001355 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001356 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001357 }
Jason Samse07694b2012-04-03 15:36:36 -07001358 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001359 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001360
Tim Murray460a0492013-11-19 12:45:54 -08001361 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001362 mType = new Type(typeID, mRS);
1363 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001364 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001365 }
1366
Jason Samsb8c5a842009-07-31 20:40:47 -07001367
1368 // creation
1369
Jason Sams49a05d72010-12-29 14:31:29 -08001370 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001371 static {
1372 mBitmapOptions.inScaled = false;
1373 }
1374
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001375 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001376 * Creates a new Allocation with the given {@link
1377 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001378 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001379 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001380 * @param mips specifies desired mipmap behaviour for the
1381 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001382 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001383 * utilized
1384 */
1385 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001386 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
Jason Sams771bebb2009-12-07 12:40:12 -08001387 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001388 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001389 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001390 }
Tim Murray460a0492013-11-19 12:45:54 -08001391 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001392 if (id == 0) {
1393 throw new RSRuntimeException("Allocation creation failed.");
1394 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001395 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams857d0c72011-11-23 15:02:15 -08001396 return new Allocation(id, rs, type, usage);
1397 }
1398
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001399 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001400 * Creates an Allocation with the size specified by the type and no mipmaps
1401 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001402 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001403 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001404 * @param type renderscript type describing data layout
1405 * @param usage bit field specifying how the allocation is
1406 * utilized
1407 *
1408 * @return allocation
1409 */
Jason Samse5d37122010-12-16 00:33:33 -08001410 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1411 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1412 }
1413
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001414 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001415 * Creates an Allocation for use by scripts with a given {@link
1416 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001417 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001418 * @param rs Context to which the Allocation will belong.
1419 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001420 *
1421 * @return allocation
1422 */
Jason Sams5476b452010-12-08 16:14:36 -08001423 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001424 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001425 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001426
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001427 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001428 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001429 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001430 * @param rs Context to which the Allocation will belong.
1431 * @param e Element to use in the Allocation
1432 * @param count the number of Elements in the Allocation
1433 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001434 * utilized
1435 *
1436 * @return allocation
1437 */
Jason Sams5476b452010-12-08 16:14:36 -08001438 static public Allocation createSized(RenderScript rs, Element e,
1439 int count, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001440 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
Jason Sams771bebb2009-12-07 12:40:12 -08001441 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001442 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001443 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001444 Type t = b.create();
1445
Tim Murray460a0492013-11-19 12:45:54 -08001446 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001447 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001448 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001449 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001450 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001451 return new Allocation(id, rs, t, usage);
1452 }
1453
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001454 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001455 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001456 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001457 * @param rs Context to which the Allocation will belong.
1458 * @param e Element to use in the Allocation
1459 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001460 *
1461 * @return allocation
1462 */
Jason Sams5476b452010-12-08 16:14:36 -08001463 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001464 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001465 }
1466
Jason Sams49a05d72010-12-29 14:31:29 -08001467 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001468 final Bitmap.Config bc = b.getConfig();
1469 if (bc == Bitmap.Config.ALPHA_8) {
1470 return Element.A_8(rs);
1471 }
1472 if (bc == Bitmap.Config.ARGB_4444) {
1473 return Element.RGBA_4444(rs);
1474 }
1475 if (bc == Bitmap.Config.ARGB_8888) {
1476 return Element.RGBA_8888(rs);
1477 }
1478 if (bc == Bitmap.Config.RGB_565) {
1479 return Element.RGB_565(rs);
1480 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001481 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001482 }
1483
Jason Sams49a05d72010-12-29 14:31:29 -08001484 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001485 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001486 Element e = elementFromBitmap(rs, b);
1487 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001488 tb.setX(b.getWidth());
1489 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001490 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001491 return tb.create();
1492 }
1493
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001494 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001495 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001496 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001497 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001498 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001499 * @param mips specifies desired mipmap behaviour for the
1500 * allocation
1501 * @param usage bit field specifying how the allocation is
1502 * utilized
1503 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001504 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001505 *
1506 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001507 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001508 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001509 int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001510 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
Jason Sams771bebb2009-12-07 12:40:12 -08001511 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001512
1513 // WAR undocumented color formats
1514 if (b.getConfig() == null) {
1515 if ((usage & USAGE_SHARED) != 0) {
1516 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1517 }
1518 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1519 Canvas c = new Canvas(newBitmap);
1520 c.drawBitmap(b, 0, 0, null);
1521 return createFromBitmap(rs, newBitmap, mips, usage);
1522 }
1523
Jason Sams5476b452010-12-08 16:14:36 -08001524 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001525
Tim Murraya3145512012-12-04 17:59:29 -08001526 // enable optimized bitmap path only with no mipmap and script-only usage
1527 if (mips == MipmapControl.MIPMAP_NONE &&
1528 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Tim Murray78e64942013-04-09 17:28:56 -07001529 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
Tim Murray460a0492013-11-19 12:45:54 -08001530 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08001531 if (id == 0) {
1532 throw new RSRuntimeException("Load failed.");
1533 }
1534
1535 // keep a reference to the Bitmap around to prevent GC
1536 Allocation alloc = new Allocation(id, rs, t, usage);
1537 alloc.setBitmap(b);
1538 return alloc;
1539 }
1540
Jason Sams9bf18922013-04-13 19:48:36 -07001541
Tim Murray460a0492013-11-19 12:45:54 -08001542 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001543 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001544 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001545 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001546 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001547 return new Allocation(id, rs, t, usage);
1548 }
1549
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001550 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001551 * Returns the handle to a raw buffer that is being managed by the screen
1552 * compositor. This operation is only valid for Allocations with {@link
1553 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001554 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001555 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001556 *
1557 */
1558 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08001559 if ((mUsage & USAGE_IO_INPUT) == 0) {
1560 throw new RSInvalidStateException("Allocation is not a surface texture.");
1561 }
1562 return mRS.nAllocationGetSurface(getID(mRS));
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001563 }
1564
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001565 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001566 * Associate a {@link android.view.Surface} with this Allocation. This
1567 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001568 *
1569 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001570 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001571 public void setSurface(Surface sur) {
1572 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001573 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1574 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1575 }
1576
Jason Samse07694b2012-04-03 15:36:36 -07001577 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001578 }
1579
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001580 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001581 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08001582 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001583 * <p>With target API version 18 or greater, this Allocation will be created
1584 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
1585 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
1586 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001587 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001588 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001589 * @param b bitmap source for the allocation data
1590 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001591 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001592 *
1593 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001594 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08001595 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1596 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07001597 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08001598 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001599 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1600 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001601 }
1602
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001603 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001604 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1605 * containing the horizontal list of cube faces. Each face must be a square,
1606 * have the same size as all other faces, and have a width that is a power
1607 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001608 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001609 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001610 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001611 * format: right, left, top, bottom, front, back
1612 * @param mips specifies desired mipmap behaviour for the cubemap
1613 * @param usage bit field specifying how the cubemap is utilized
1614 *
1615 * @return allocation containing cubemap data
1616 *
1617 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001618 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001619 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001620 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001621 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001622
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001623 int height = b.getHeight();
1624 int width = b.getWidth();
1625
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001626 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001627 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1628 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001629 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001630 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001631 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001632 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001633 if (!isPow2) {
1634 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1635 }
1636
1637 Element e = elementFromBitmap(rs, b);
1638 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001639 tb.setX(height);
1640 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001641 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001642 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001643 Type t = tb.create();
1644
Tim Murray460a0492013-11-19 12:45:54 -08001645 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001646 if(id == 0) {
1647 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1648 }
Jason Sams5476b452010-12-08 16:14:36 -08001649 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001650 }
1651
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001652 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001653 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
1654 * from a {@link android.graphics.Bitmap} containing the horizontal list of
1655 * cube faces. Each face must be a square, have the same size as all other
1656 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001657 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001658 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001659 * @param b bitmap with cubemap faces layed out in the following
1660 * format: right, left, top, bottom, front, back
1661 *
1662 * @return allocation containing cubemap data
1663 *
1664 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001665 static public Allocation createCubemapFromBitmap(RenderScript rs,
1666 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001667 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001668 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001669 }
1670
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001671 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001672 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
1673 * objects containing the cube faces. Each face must be a square, have the
1674 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001675 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001676 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001677 * @param xpos cubemap face in the positive x direction
1678 * @param xneg cubemap face in the negative x direction
1679 * @param ypos cubemap face in the positive y direction
1680 * @param yneg cubemap face in the negative y direction
1681 * @param zpos cubemap face in the positive z direction
1682 * @param zneg cubemap face in the negative z direction
1683 * @param mips specifies desired mipmap behaviour for the cubemap
1684 * @param usage bit field specifying how the cubemap is utilized
1685 *
1686 * @return allocation containing cubemap data
1687 *
1688 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001689 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1690 Bitmap xpos,
1691 Bitmap xneg,
1692 Bitmap ypos,
1693 Bitmap yneg,
1694 Bitmap zpos,
1695 Bitmap zneg,
1696 MipmapControl mips,
1697 int usage) {
1698 int height = xpos.getHeight();
1699 if (xpos.getWidth() != height ||
1700 xneg.getWidth() != height || xneg.getHeight() != height ||
1701 ypos.getWidth() != height || ypos.getHeight() != height ||
1702 yneg.getWidth() != height || yneg.getHeight() != height ||
1703 zpos.getWidth() != height || zpos.getHeight() != height ||
1704 zneg.getWidth() != height || zneg.getHeight() != height) {
1705 throw new RSIllegalArgumentException("Only square cube map faces supported");
1706 }
1707 boolean isPow2 = (height & (height - 1)) == 0;
1708 if (!isPow2) {
1709 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1710 }
1711
1712 Element e = elementFromBitmap(rs, xpos);
1713 Type.Builder tb = new Type.Builder(rs, e);
1714 tb.setX(height);
1715 tb.setY(height);
1716 tb.setFaces(true);
1717 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1718 Type t = tb.create();
1719 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1720
1721 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001722 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001723 adapter.copyFrom(xpos);
1724 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1725 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001726 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001727 adapter.copyFrom(ypos);
1728 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1729 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001730 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001731 adapter.copyFrom(zpos);
1732 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1733 adapter.copyFrom(zneg);
1734
1735 return cubemap;
1736 }
1737
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001738 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001739 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
1740 * from 6 {@link android.graphics.Bitmap} objects containing the cube
1741 * faces. Each face must be a square, have the same size as all other faces,
1742 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001743 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001744 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001745 * @param xpos cubemap face in the positive x direction
1746 * @param xneg cubemap face in the negative x direction
1747 * @param ypos cubemap face in the positive y direction
1748 * @param yneg cubemap face in the negative y direction
1749 * @param zpos cubemap face in the positive z direction
1750 * @param zneg cubemap face in the negative z direction
1751 *
1752 * @return allocation containing cubemap data
1753 *
1754 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001755 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1756 Bitmap xpos,
1757 Bitmap xneg,
1758 Bitmap ypos,
1759 Bitmap yneg,
1760 Bitmap zpos,
1761 Bitmap zneg) {
1762 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1763 zpos, zneg, MipmapControl.MIPMAP_NONE,
1764 USAGE_GRAPHICS_TEXTURE);
1765 }
1766
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001767 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001768 * Creates an Allocation from the Bitmap referenced
1769 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001770 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001771 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001772 * @param res application resources
1773 * @param id resource id to load the data from
1774 * @param mips specifies desired mipmap behaviour for the
1775 * allocation
1776 * @param usage bit field specifying how the allocation is
1777 * utilized
1778 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001779 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001780 *
1781 */
Jason Sams5476b452010-12-08 16:14:36 -08001782 static public Allocation createFromBitmapResource(RenderScript rs,
1783 Resources res,
1784 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001785 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001786 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001787
Jason Sams771bebb2009-12-07 12:40:12 -08001788 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07001789 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
1790 throw new RSIllegalArgumentException("Unsupported usage specified.");
1791 }
Jason Sams5476b452010-12-08 16:14:36 -08001792 Bitmap b = BitmapFactory.decodeResource(res, id);
1793 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1794 b.recycle();
1795 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001796 }
1797
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001798 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001799 * Creates a non-mipmapped Allocation to use as a graphics texture from the
1800 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001801 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001802 * <p>With target API version 18 or greater, this allocation will be created
1803 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
1804 * target API version 17 or lower, this allocation will be created with
1805 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08001806 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001807 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001808 * @param res application resources
1809 * @param id resource id to load the data from
1810 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001811 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001812 *
1813 */
Jason Sams5476b452010-12-08 16:14:36 -08001814 static public Allocation createFromBitmapResource(RenderScript rs,
1815 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001816 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08001817 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1818 return createFromBitmapResource(rs, res, id,
1819 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07001820 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08001821 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001822 return createFromBitmapResource(rs, res, id,
1823 MipmapControl.MIPMAP_NONE,
1824 USAGE_GRAPHICS_TEXTURE);
1825 }
1826
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001827 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001828 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001829 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001830 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001831 * @param str string to create the allocation from
1832 * @param usage bit field specifying how the allocaiton is
1833 * utilized
1834 *
1835 */
Jason Sams5476b452010-12-08 16:14:36 -08001836 static public Allocation createFromString(RenderScript rs,
1837 String str,
1838 int usage) {
1839 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001840 byte[] allocArray = null;
1841 try {
1842 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001843 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001844 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001845 return alloc;
1846 }
1847 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001848 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001849 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001850 }
Jason Sams739c8262013-04-11 18:07:52 -07001851
1852 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001853 * Interface to handle notification when new buffers are available via
1854 * {@link #USAGE_IO_INPUT}. An application will receive one notification
1855 * when a buffer is available. Additional buffers will not trigger new
1856 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07001857 */
Jason Sams42ef2382013-08-29 13:30:59 -07001858 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07001859 public void onBufferAvailable(Allocation a);
1860 }
1861
1862 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001863 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07001864 *
Jason Sams42ef2382013-08-29 13:30:59 -07001865 * @param callback instance of the OnBufferAvailableListener
1866 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07001867 */
Jason Sams42ef2382013-08-29 13:30:59 -07001868 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07001869 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08001870 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07001871 mBufferNotifier = callback;
1872 }
1873 }
1874
Tim Murrayb730d862014-08-18 16:14:24 -07001875 static void sendBufferNotification(long id) {
Jason Sams739c8262013-04-11 18:07:52 -07001876 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08001877 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07001878
1879 if ((a != null) && (a.mBufferNotifier != null)) {
1880 a.mBufferNotifier.onBufferAvailable(a);
1881 }
1882 }
1883 }
1884
Jason Samsb8c5a842009-07-31 20:40:47 -07001885}