blob: c6afa2cc39206e4f6264d523024ed2c40d731ee0 [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
Jason Sams615e7ce2012-01-13 14:01:20 -080061 boolean mReadAllowed = true;
62 boolean mWriteAllowed = true;
Miao Wang87e908d2015-03-02 15:15:15 -080063 boolean mAutoPadding = false;
Jason Sams46ba27e32015-02-06 17:45:15 -080064 int mSelectedX;
Jason Samsba862d12011-07-07 15:24:42 -070065 int mSelectedY;
66 int mSelectedZ;
67 int mSelectedLOD;
Jason Sams46ba27e32015-02-06 17:45:15 -080068 int mSelectedArray[];
Jason Samsba862d12011-07-07 15:24:42 -070069 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
70
71 int mCurrentDimX;
72 int mCurrentDimY;
73 int mCurrentDimZ;
74 int mCurrentCount;
Tim Murray460a0492013-11-19 12:45:54 -080075 static HashMap<Long, Allocation> mAllocationMap =
76 new HashMap<Long, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070077 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070078
Jason Sams3042d262013-11-25 18:28:33 -080079 private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
80 final Class c = d.getClass();
81 if (!c.isArray()) {
82 throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
83 }
84 final Class cmp = c.getComponentType();
85 if (!cmp.isPrimitive()) {
86 throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
87 }
88
89 if (cmp == Long.TYPE) {
90 if (checkType) {
91 validateIsInt64();
92 return mType.mElement.mType;
93 }
94 return Element.DataType.SIGNED_64;
95 }
96
97 if (cmp == Integer.TYPE) {
98 if (checkType) {
99 validateIsInt32();
100 return mType.mElement.mType;
101 }
102 return Element.DataType.SIGNED_32;
103 }
104
105 if (cmp == Short.TYPE) {
106 if (checkType) {
107 validateIsInt16();
108 return mType.mElement.mType;
109 }
110 return Element.DataType.SIGNED_16;
111 }
112
113 if (cmp == Byte.TYPE) {
114 if (checkType) {
115 validateIsInt8();
116 return mType.mElement.mType;
117 }
118 return Element.DataType.SIGNED_8;
119 }
120
121 if (cmp == Float.TYPE) {
122 if (checkType) {
123 validateIsFloat32();
124 }
125 return Element.DataType.FLOAT_32;
126 }
127
128 if (cmp == Double.TYPE) {
129 if (checkType) {
130 validateIsFloat64();
131 }
132 return Element.DataType.FLOAT_64;
133 }
134 return null;
135 }
136
137
Tim Murrayc11e25c2013-04-09 11:01:01 -0700138 /**
139 * The usage of the Allocation. These signal to RenderScript where to place
140 * the Allocation in memory.
141 *
142 */
Jason Sams5476b452010-12-08 16:14:36 -0800143
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700144 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700145 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -0800146 */
Jason Sams5476b452010-12-08 16:14:36 -0800147 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800148
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700149 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700150 * The Allocation will be used as a texture source by one or more graphics
151 * programs.
Jason Samsf7086092011-01-12 13:28:37 -0800152 *
153 */
Jason Sams5476b452010-12-08 16:14:36 -0800154 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800155
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700156 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700157 * The Allocation will be used as a graphics mesh.
158 *
159 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800160 *
161 */
Jason Sams5476b452010-12-08 16:14:36 -0800162 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800163
164
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700165 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700166 * The Allocation will be used as the source of shader constants by one or
167 * more programs.
168 *
169 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800170 *
171 */
Jason Sams5476b452010-12-08 16:14:36 -0800172 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
173
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700174 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700175 * The Allocation will be used as a target for offscreen rendering
176 *
177 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700178 *
179 */
180 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
181
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700182 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700183 * The Allocation will be used as a {@link android.view.Surface}
184 * consumer. This usage will cause the Allocation to be created
185 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800186 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800187 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700188 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800189
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700190 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700191 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700192 * producer. The dimensions and format of the {@link
Jason Sams3a1b8e42013-09-24 15:18:52 -0700193 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700194 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800195 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800196 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700197 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700198
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700199 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700200 * The Allocation's backing store will be inherited from another object
201 * (usually a {@link android.graphics.Bitmap}); copying to or from the
202 * original source Bitmap will cause a synchronization rather than a full
203 * copy. {@link #syncAll} may also be used to synchronize the Allocation
204 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800205 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700206 * <p>This is set by default for allocations created with {@link
207 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800208 *
209 */
210 public static final int USAGE_SHARED = 0x0080;
211
212 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700213 * Controls mipmap behavior when using the bitmap creation and update
214 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800215 */
Jason Sams4ef66502010-12-10 16:03:15 -0800216 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700217 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700218 * No mipmaps will be generated and the type generated from the incoming
219 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800220 */
Jason Sams5476b452010-12-08 16:14:36 -0800221 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800222
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700223 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700224 * A full mipmap chain will be created in script memory. The Type of
225 * the Allocation will contain a full mipmap chain. On upload, the full
226 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800227 */
Jason Sams5476b452010-12-08 16:14:36 -0800228 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800229
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700230 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700231 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
232 * not contain mipmaps. On upload, the allocation data will contain a
233 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800234 */
Jason Sams5476b452010-12-08 16:14:36 -0800235 MIPMAP_ON_SYNC_TO_TEXTURE(2);
236
237 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800238 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800239 mID = id;
240 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700241 }
242
Jason Sams48fe5342011-07-08 13:52:30 -0700243
Tim Murray460a0492013-11-19 12:45:54 -0800244 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700245 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700246 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700247 }
Jason Samse07694b2012-04-03 15:36:36 -0700248 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700249 }
250
Jason Sams03d2d002012-03-23 13:51:56 -0700251
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700252 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700253 * Get the {@link android.renderscript.Element} of the {@link
254 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700255 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700256 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700257 *
258 */
259 public Element getElement() {
260 return mType.getElement();
261 }
262
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700263 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700264 * Get the usage flags of the Allocation.
265 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700266 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700267 *
268 */
269 public int getUsage() {
270 return mUsage;
271 }
272
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700273 /**
Miao Wang87e908d2015-03-02 15:15:15 -0800274 * @hide
275 * Enable/Disable AutoPadding for Vec3 elements.
276 *
277 * @param useAutoPadding True: enable AutoPadding; flase: disable AutoPadding
278 *
279 */
280 public void setAutoPadding(boolean useAutoPadding) {
281 mAutoPadding = useAutoPadding;
282 }
283
284 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700285 * Get the size of the Allocation in bytes.
286 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700287 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700288 *
289 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700290 public int getBytesSize() {
Tim Murray04f0d6e2013-12-17 17:15:25 -0800291 if (mType.mDimYuv != 0) {
292 return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
293 }
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700294 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700295 }
296
Jason Sams452a7662011-07-07 16:05:18 -0700297 private void updateCacheInfo(Type t) {
298 mCurrentDimX = t.getX();
299 mCurrentDimY = t.getY();
300 mCurrentDimZ = t.getZ();
301 mCurrentCount = mCurrentDimX;
302 if (mCurrentDimY > 1) {
303 mCurrentCount *= mCurrentDimY;
304 }
305 if (mCurrentDimZ > 1) {
306 mCurrentCount *= mCurrentDimZ;
307 }
308 }
Jason Samsba862d12011-07-07 15:24:42 -0700309
Tim Murraya3145512012-12-04 17:59:29 -0800310 private void setBitmap(Bitmap b) {
311 mBitmap = b;
312 }
313
Tim Murray460a0492013-11-19 12:45:54 -0800314 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700315 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800316 if ((usage & ~(USAGE_SCRIPT |
317 USAGE_GRAPHICS_TEXTURE |
318 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700319 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800320 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800321 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800322 USAGE_IO_OUTPUT |
323 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800324 throw new RSIllegalArgumentException("Unknown usage specified.");
325 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800326
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700327 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800328 mWriteAllowed = false;
329
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700330 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800331 USAGE_GRAPHICS_TEXTURE |
332 USAGE_SCRIPT)) != 0) {
333 throw new RSIllegalArgumentException("Invalid usage combination.");
334 }
335 }
Jason Sams9bf18922013-04-13 19:48:36 -0700336
Jason Sams5476b452010-12-08 16:14:36 -0800337 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800338 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700339
Jason Sams452a7662011-07-07 16:05:18 -0700340 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700341 // TODO: A3D doesn't have Type info during creation, so we can't
342 // calculate the size ahead of time. We can possibly add a method
343 // to update the size in the future if it seems reasonable.
344 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700345 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700346 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700347 try {
348 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
349 } catch (Exception e) {
350 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
351 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
352 }
353 }
354
355 protected void finalize() throws Throwable {
356 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
357 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700358 }
359
Jason Sams3042d262013-11-25 18:28:33 -0800360 private void validateIsInt64() {
361 if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
362 (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
363 return;
364 }
365 throw new RSIllegalArgumentException(
366 "64 bit integer source does not match allocation type " + mType.mElement.mType);
367 }
368
Jason Samsb97b2512011-01-16 15:04:08 -0800369 private void validateIsInt32() {
370 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
371 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
372 return;
373 }
374 throw new RSIllegalArgumentException(
375 "32 bit integer source does not match allocation type " + mType.mElement.mType);
376 }
377
378 private void validateIsInt16() {
379 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
380 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
381 return;
382 }
383 throw new RSIllegalArgumentException(
384 "16 bit integer source does not match allocation type " + mType.mElement.mType);
385 }
386
387 private void validateIsInt8() {
388 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
389 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
390 return;
391 }
392 throw new RSIllegalArgumentException(
393 "8 bit integer source does not match allocation type " + mType.mElement.mType);
394 }
395
396 private void validateIsFloat32() {
397 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
398 return;
399 }
400 throw new RSIllegalArgumentException(
401 "32 bit float source does not match allocation type " + mType.mElement.mType);
402 }
403
Jason Sams3042d262013-11-25 18:28:33 -0800404 private void validateIsFloat64() {
405 if (mType.mElement.mType == Element.DataType.FLOAT_64) {
406 return;
407 }
408 throw new RSIllegalArgumentException(
409 "64 bit float source does not match allocation type " + mType.mElement.mType);
410 }
411
Jason Samsb97b2512011-01-16 15:04:08 -0800412 private void validateIsObject() {
413 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
414 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
415 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
416 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
417 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
418 (mType.mElement.mType == Element.DataType.RS_MESH) ||
419 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
420 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
421 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
422 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
423 return;
424 }
425 throw new RSIllegalArgumentException(
426 "Object source does not match allocation type " + mType.mElement.mType);
427 }
428
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700429 @Override
430 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800431 super.updateFromNative();
Tim Murray460a0492013-11-19 12:45:54 -0800432 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700433 if(typeID != 0) {
434 mType = new Type(typeID, mRS);
435 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700436 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700437 }
438 }
439
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700440 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700441 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700442 *
443 * @return Type
444 *
445 */
Jason Samsea87e962010-01-12 12:12:28 -0800446 public Type getType() {
447 return mType;
448 }
449
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700450 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700451 * Propagate changes from one usage of the Allocation to the
452 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700453 *
454 */
Jason Sams5476b452010-12-08 16:14:36 -0800455 public void syncAll(int srcLocation) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700456 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
Jason Sams5476b452010-12-08 16:14:36 -0800457 switch (srcLocation) {
Jason Sams5476b452010-12-08 16:14:36 -0800458 case USAGE_GRAPHICS_TEXTURE:
Tim Murray78e64942013-04-09 17:28:56 -0700459 case USAGE_SCRIPT:
460 if ((mUsage & USAGE_SHARED) != 0) {
461 copyFrom(mBitmap);
462 }
463 break;
464 case USAGE_GRAPHICS_CONSTANTS:
Jason Sams5476b452010-12-08 16:14:36 -0800465 case USAGE_GRAPHICS_VERTEX:
466 break;
Tim Murray78e64942013-04-09 17:28:56 -0700467 case USAGE_SHARED:
468 if ((mUsage & USAGE_SHARED) != 0) {
469 copyTo(mBitmap);
470 }
471 break;
Jason Sams5476b452010-12-08 16:14:36 -0800472 default:
473 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
474 }
475 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700476 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700477 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800478 }
479
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700480 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700481 * Send a buffer to the output stream. The contents of the Allocation will
482 * be undefined after this operation. This operation is only valid if {@link
483 * #USAGE_IO_OUTPUT} is set on the Allocation.
484 *
Jason Sams163766c2012-02-15 12:04:24 -0800485 *
Jason Sams163766c2012-02-15 12:04:24 -0800486 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700487 public void ioSend() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700488 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
Jason Sams163766c2012-02-15 12:04:24 -0800489 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
490 throw new RSIllegalArgumentException(
491 "Can only send buffer if IO_OUTPUT usage specified.");
492 }
493 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700494 mRS.nAllocationIoSend(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700495 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800496 }
497
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700498 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700499 * Receive the latest input into the Allocation. This operation
500 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800501 *
Jason Sams163766c2012-02-15 12:04:24 -0800502 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700503 public void ioReceive() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700504 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
Jason Sams163766c2012-02-15 12:04:24 -0800505 if ((mUsage & USAGE_IO_INPUT) == 0) {
506 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700507 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800508 }
509 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700510 mRS.nAllocationIoReceive(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700511 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800512 }
513
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700514 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700515 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700516 *
517 * @param d Source array.
518 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800519 public void copyFrom(BaseObj[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700520 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Sams771bebb2009-12-07 12:40:12 -0800521 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800522 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700523 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800524 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700525 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800526 }
Tim Murray460a0492013-11-19 12:45:54 -0800527
Tim Murray3de3dc72014-07-01 16:56:18 -0700528 if (RenderScript.sPointerSize == 8) {
529 long i[] = new long[d.length * 4];
530 for (int ct=0; ct < d.length; ct++) {
531 i[ct * 4] = d[ct].getID(mRS);
532 }
533 copy1DRangeFromUnchecked(0, mCurrentCount, i);
534 } else {
535 int i[] = new int[d.length];
536 for (int ct=0; ct < d.length; ct++) {
537 i[ct] = (int)d[ct].getID(mRS);
538 }
539 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800540 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700541 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700542 }
543
Jason Samsfb9f82c2011-01-12 14:53:25 -0800544 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800545 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800546 if (bc == null) {
547 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
548 }
Jason Sams252c0782011-01-11 17:42:52 -0800549 switch (bc) {
550 case ALPHA_8:
551 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
552 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 ARGB_8888:
560 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700561 (mType.getElement().getBytesSize() != 4)) {
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 RGB_565:
570 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
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 case ARGB_4444:
580 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700581 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800582 throw new RSIllegalArgumentException("Allocation kind is " +
583 mType.getElement().mKind + ", type " +
584 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700585 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800586 " bytes, passed bitmap was " + bc);
587 }
588 break;
589
590 }
Jason Sams4ef66502010-12-10 16:03:15 -0800591 }
592
Jason Samsfb9f82c2011-01-12 14:53:25 -0800593 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700594 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800595 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
596 }
597 }
598
Jason Sams3042d262013-11-25 18:28:33 -0800599 private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
600 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
601 mRS.validate();
602 if (mCurrentDimZ > 0) {
603 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
604 } else if (mCurrentDimY > 0) {
605 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
606 } else {
607 copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
608 }
609 Trace.traceEnd(RenderScript.TRACE_TAG);
610 }
611
612 /**
613 * Copy into this Allocation from an array. This method does not guarantee
614 * that the Allocation is compatible with the input buffer; it copies memory
615 * without reinterpretation.
616 *
617 * @param array The source data array
618 */
619 public void copyFromUnchecked(Object array) {
620 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
621 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
622 java.lang.reflect.Array.getLength(array));
623 Trace.traceEnd(RenderScript.TRACE_TAG);
624 }
625
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700626 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700627 * Copy into this Allocation from an array. This method does not guarantee
628 * that the Allocation is compatible with the input buffer; it copies memory
629 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800630 *
631 * @param d the source data array
632 */
633 public void copyFromUnchecked(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800634 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800635 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700636
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700637 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700638 * Copy into this Allocation from an array. This method does not guarantee
639 * that the Allocation is compatible with the input buffer; it copies memory
640 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800641 *
642 * @param d the source data array
643 */
644 public void copyFromUnchecked(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800645 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800646 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700647
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700648 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700649 * Copy into this Allocation from an array. This method does not guarantee
650 * that the Allocation is compatible with the input buffer; it copies memory
651 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800652 *
653 * @param d the source data array
654 */
655 public void copyFromUnchecked(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800656 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800657 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700658
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700659 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700660 * Copy into this Allocation from an array. This method does not guarantee
661 * that the Allocation is compatible with the input buffer; it copies memory
662 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800663 *
664 * @param d the source data array
665 */
666 public void copyFromUnchecked(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800667 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800668 }
669
Tim Murray6d7a53c2013-05-23 16:59:23 -0700670
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700671 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700672 * Copy into this Allocation from an array. This variant is type checked
673 * and will generate exceptions if the Allocation's {@link
Jason Sams3042d262013-11-25 18:28:33 -0800674 * android.renderscript.Element} does not match the array's
675 * primitive type.
676 *
Ying Wang16229812013-11-26 15:45:12 -0800677 * @param array The source data array
Jason Sams3042d262013-11-25 18:28:33 -0800678 */
679 public void copyFrom(Object array) {
680 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
681 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
682 java.lang.reflect.Array.getLength(array));
683 Trace.traceEnd(RenderScript.TRACE_TAG);
684 }
685
686 /**
687 * Copy into this Allocation from an array. This variant is type checked
688 * and will generate exceptions if the Allocation's {@link
Tim Murrayc11e25c2013-04-09 11:01:01 -0700689 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800690 *
691 * @param d the source data array
692 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800693 public void copyFrom(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800694 validateIsInt32();
695 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800696 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800697
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700698 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700699 * Copy into this Allocation from an array. This variant is type checked
700 * and will generate exceptions if the Allocation's {@link
701 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800702 *
703 * @param d the source data array
704 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800705 public void copyFrom(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800706 validateIsInt16();
707 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800708 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800709
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700710 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700711 * Copy into this Allocation from an array. This variant is type checked
712 * and will generate exceptions if the Allocation's {@link
713 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800714 *
715 * @param d the source data array
716 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800717 public void copyFrom(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800718 validateIsInt8();
719 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800720 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800721
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700722 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700723 * Copy into this Allocation from an array. This variant is type checked
724 * and will generate exceptions if the Allocation's {@link
725 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800726 *
727 * @param d the source data array
728 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800729 public void copyFrom(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800730 validateIsFloat32();
731 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800732 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800733
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700734 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700735 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
736 * height, width, and format of the bitmap must match the existing
737 * allocation.
738 *
739 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
740 * android.graphics.Bitmap} used to create the Allocation with {@link
741 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
742 * this will synchronize the Allocation with the latest data from the {@link
743 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800744 *
745 * @param b the source bitmap
746 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800747 public void copyFrom(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700748 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsfb9f82c2011-01-12 14:53:25 -0800749 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -0800750 if (b.getConfig() == null) {
751 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
752 Canvas c = new Canvas(newBitmap);
753 c.drawBitmap(b, 0, 0, null);
754 copyFrom(newBitmap);
755 return;
756 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800757 validateBitmapSize(b);
758 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700759 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700760 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700761 }
762
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700763 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700764 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700765 * must be identical.
766 *
767 * @param a the source allocation
768 */
769 public void copyFrom(Allocation a) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700770 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Tim Murrayf671fb02012-10-03 13:50:05 -0700771 mRS.validate();
772 if (!mType.equals(a.getType())) {
773 throw new RSIllegalArgumentException("Types of allocations must match.");
774 }
775 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700776 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700777 }
778
Tim Murrayf671fb02012-10-03 13:50:05 -0700779 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700780 * This is only intended to be used by auto-generated code reflected from
781 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800782 *
783 * @param xoff
784 * @param fp
785 */
Jason Sams21b41032011-01-16 15:05:41 -0800786 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800787 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700788 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700789 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700790 int data_length = fp.getPos();
Jason Samsa70f4162010-03-26 15:33:42 -0700791
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700792 int count = data_length / eSize;
793 if ((eSize * count) != data_length) {
794 throw new RSIllegalArgumentException("Field packer length " + data_length +
Jason Samsa70f4162010-03-26 15:33:42 -0700795 " not divisible by element size " + eSize + ".");
796 }
Jason Samsba862d12011-07-07 15:24:42 -0700797 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700798 }
799
Miao Wang45cec0a2015-03-04 16:40:21 -0800800
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700801 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700802 * This is only intended to be used by auto-generated code reflected from
803 * the RenderScript script files.
Jason Samsfa445b92011-01-07 17:00:07 -0800804 *
805 * @param xoff
806 * @param component_number
807 * @param fp
808 */
Jason Sams21b41032011-01-16 15:05:41 -0800809 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800810 setFromFieldPacker(xoff, 0, 0, component_number, fp);
811 }
812
813 /**
814 * @hide
815 * This is only intended to be used by auto-generated code reflected from
816 * the RenderScript script files.
817 *
818 * @param xoff
819 * @param yoff
Miao Wangc8e237e2015-02-20 18:36:32 -0800820 * @param zoff
821 * @param component_number
822 * @param fp
823 */
824 public void setFromFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800825 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700826 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800827 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700828 }
829 if(xoff < 0) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800830 throw new RSIllegalArgumentException("Offset x must be >= 0.");
831 }
832 if(yoff < 0) {
833 throw new RSIllegalArgumentException("Offset y must be >= 0.");
834 }
835 if(zoff < 0) {
836 throw new RSIllegalArgumentException("Offset z must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700837 }
838
839 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700840 int data_length = fp.getPos();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700841 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800842 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700843
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700844 if (data_length != eSize) {
845 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700846 " does not match component size " + eSize + ".");
847 }
848
Miao Wangc8e237e2015-02-20 18:36:32 -0800849 mRS.nAllocationElementData(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
850 component_number, data, data_length);
Jason Samsa70f4162010-03-26 15:33:42 -0700851 }
852
Miao Wang87e908d2015-03-02 15:15:15 -0800853 private void data1DChecks(int off, int count, int len, int dataSize, boolean usePadding) {
Jason Sams771bebb2009-12-07 12:40:12 -0800854 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700855 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800856 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700857 }
858 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800859 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700860 }
Jason Samsba862d12011-07-07 15:24:42 -0700861 if((off + count) > mCurrentCount) {
862 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700863 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700864 }
Miao Wang87e908d2015-03-02 15:15:15 -0800865 if(usePadding) {
866 if(len < dataSize / 4 * 3) {
867 throw new RSIllegalArgumentException("Array too small for allocation type.");
868 }
869 } else {
870 if(len < dataSize) {
871 throw new RSIllegalArgumentException("Array too small for allocation type.");
872 }
Jason Sams768bc022009-09-21 19:41:04 -0700873 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700874 }
875
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700876 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700877 * Generate a mipmap chain. This is only valid if the Type of the Allocation
878 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800879 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700880 * <p>This function will generate a complete set of mipmaps from the top
881 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800882 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700883 * <p>If the Allocation is also using other memory spaces, a call to {@link
884 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800885 */
886 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700887 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800888 }
889
Jason Sams3042d262013-11-25 18:28:33 -0800890 private void copy1DRangeFromUnchecked(int off, int count, Object array,
891 Element.DataType dt, int arrayLen) {
892 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
893 final int dataSize = mType.mElement.getBytesSize() * count;
Miao Wang87e908d2015-03-02 15:15:15 -0800894 // AutoPadding for Vec3 Element
895 boolean usePadding = false;
896 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
897 usePadding = true;
898 }
899 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
900 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
901 mType.mElement.mType.mSize, usePadding);
Jason Sams3042d262013-11-25 18:28:33 -0800902 Trace.traceEnd(RenderScript.TRACE_TAG);
903 }
904
905 /**
906 * Copy an array into part of this Allocation. This method does not
907 * guarantee that the Allocation is compatible with the input buffer.
908 *
909 * @param off The offset of the first element to be copied.
910 * @param count The number of elements to be copied.
911 * @param array The source data array
912 */
913 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
914 copy1DRangeFromUnchecked(off, count, array,
915 validateObjectIsPrimitiveArray(array, false),
916 java.lang.reflect.Array.getLength(array));
917 }
918
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700919 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700920 * Copy an array into part of this Allocation. This method does not
921 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800922 *
923 * @param off The offset of the first element to be copied.
924 * @param count The number of elements to be copied.
925 * @param d the source data array
926 */
927 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800928 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700929 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700930
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700931 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700932 * Copy an array into part of this Allocation. This method does not
933 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800934 *
935 * @param off The offset of the first element to be copied.
936 * @param count The number of elements to be copied.
937 * @param d the source data array
938 */
939 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800940 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700941 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700942
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700943 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700944 * Copy an array into part of this Allocation. This method does not
945 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800946 *
947 * @param off The offset of the first element to be copied.
948 * @param count The number of elements to be copied.
949 * @param d the source data array
950 */
951 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800952 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700953 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700954
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700955 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700956 * Copy an array into part of this Allocation. This method does not
957 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800958 *
959 * @param off The offset of the first element to be copied.
960 * @param count The number of elements to be copied.
961 * @param d the source data array
962 */
963 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800964 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
965 }
966
967
968 /**
969 * Copy an array into part of this Allocation. This variant is type checked
970 * and will generate exceptions if the Allocation type does not
971 * match the component type of the array passed in.
972 *
973 * @param off The offset of the first element to be copied.
974 * @param count The number of elements to be copied.
975 * @param array The source data array.
976 */
977 public void copy1DRangeFrom(int off, int count, Object array) {
978 copy1DRangeFromUnchecked(off, count, array,
979 validateObjectIsPrimitiveArray(array, true),
980 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -0700981 }
982
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700983 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700984 * Copy an array into part of this Allocation. This variant is type checked
985 * and will generate exceptions if the Allocation type is not a 32 bit
986 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800987 *
988 * @param off The offset of the first element to be copied.
989 * @param count The number of elements to be copied.
990 * @param d the source data array
991 */
Jason Samsb97b2512011-01-16 15:04:08 -0800992 public void copy1DRangeFrom(int off, int count, int[] d) {
993 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -0800994 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -0800995 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800996
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700997 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700998 * Copy an array into part of this Allocation. This variant is type checked
999 * and will generate exceptions if the Allocation type is not a 16 bit
1000 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001001 *
1002 * @param off The offset of the first element to be copied.
1003 * @param count The number of elements to be copied.
1004 * @param d the source data array
1005 */
Jason Samsb97b2512011-01-16 15:04:08 -08001006 public void copy1DRangeFrom(int off, int count, short[] d) {
1007 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001008 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001009 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001010
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001011 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001012 * Copy an array into part of this Allocation. This variant is type checked
1013 * and will generate exceptions if the Allocation type is not an 8 bit
1014 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001015 *
1016 * @param off The offset of the first element to be copied.
1017 * @param count The number of elements to be copied.
1018 * @param d the source data array
1019 */
Jason Samsb97b2512011-01-16 15:04:08 -08001020 public void copy1DRangeFrom(int off, int count, byte[] d) {
1021 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001022 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001023 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001024
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001025 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001026 * Copy an array into part of this Allocation. This variant is type checked
1027 * and will generate exceptions if the Allocation type is not a 32 bit float
1028 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001029 *
1030 * @param off The offset of the first element to be copied.
1031 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001032 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001033 */
Jason Samsb97b2512011-01-16 15:04:08 -08001034 public void copy1DRangeFrom(int off, int count, float[] d) {
1035 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001036 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001037 }
Jason Sams3042d262013-11-25 18:28:33 -08001038
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001039 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001040 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001041 *
1042 * @param off The offset of the first element to be copied.
1043 * @param count The number of elements to be copied.
1044 * @param data the source data allocation.
1045 * @param dataOff off The offset of the first element in data to
1046 * be copied.
1047 */
1048 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001049 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -07001050 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001051 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001052 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001053 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001054 }
1055
Jason Samsfb9f82c2011-01-12 14:53:25 -08001056 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001057 if (mAdaptedAllocation != null) {
1058
1059 } else {
1060
1061 if (xoff < 0 || yoff < 0) {
1062 throw new RSIllegalArgumentException("Offset cannot be negative.");
1063 }
1064 if (h < 0 || w < 0) {
1065 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1066 }
1067 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1068 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1069 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001070 }
1071 }
Jason Sams768bc022009-09-21 19:41:04 -07001072
Jason Sams3042d262013-11-25 18:28:33 -08001073 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1074 Element.DataType dt, int arrayLen) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001075 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001076 mRS.validate();
1077 validate2DRange(xoff, yoff, w, h);
Miao Wang87e908d2015-03-02 15:15:15 -08001078 final int dataSize = mType.mElement.getBytesSize() * w * h;
1079 // AutoPadding for Vec3 Element
1080 boolean usePadding = false;
1081 int sizeBytes = arrayLen * dt.mSize;
1082 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1083 if (dataSize / 4 * 3 > sizeBytes) {
1084 throw new RSIllegalArgumentException("Array too small for allocation type.");
1085 }
1086 usePadding = true;
1087 sizeBytes = dataSize;
1088 } else {
1089 if (dataSize > sizeBytes) {
1090 throw new RSIllegalArgumentException("Array too small for allocation type.");
1091 }
1092 }
Jason Sams3042d262013-11-25 18:28:33 -08001093 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
Miao Wang87e908d2015-03-02 15:15:15 -08001094 array, sizeBytes, dt,
1095 mType.mElement.mType.mSize, usePadding);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001096 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001097 }
1098
Jason Sams3042d262013-11-25 18:28:33 -08001099 /**
1100 * Copy from an array into a rectangular region in this Allocation. The
1101 * array is assumed to be tightly packed.
1102 *
1103 * @param xoff X offset of the region to update in this Allocation
1104 * @param yoff Y offset of the region to update in this Allocation
1105 * @param w Width of the region to update
1106 * @param h Height of the region to update
Ying Wang16229812013-11-26 15:45:12 -08001107 * @param array Data to be placed into the Allocation
Jason Sams3042d262013-11-25 18:28:33 -08001108 */
1109 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
1110 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1111 copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
1112 validateObjectIsPrimitiveArray(array, true),
1113 java.lang.reflect.Array.getLength(array));
Tim Murray6d7a53c2013-05-23 16:59:23 -07001114 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001115 }
1116
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001117 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001118 * Copy from an array into a rectangular region in this Allocation. The
1119 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -08001120 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001121 * @param xoff X offset of the region to update in this Allocation
1122 * @param yoff Y offset of the region to update in this Allocation
1123 * @param w Width of the region to update
1124 * @param h Height of the region to update
1125 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001126 */
1127 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001128 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001129 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1130 Element.DataType.SIGNED_8, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001131 }
1132
Tim Murrayc11e25c2013-04-09 11:01:01 -07001133 /**
1134 * Copy from an array into a rectangular region in this Allocation. The
1135 * array is assumed to be tightly packed.
1136 *
1137 * @param xoff X offset of the region to update in this Allocation
1138 * @param yoff Y offset of the region to update in this Allocation
1139 * @param w Width of the region to update
1140 * @param h Height of the region to update
1141 * @param data to be placed into the Allocation
1142 */
Jason Samsf7086092011-01-12 13:28:37 -08001143 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001144 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001145 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1146 Element.DataType.SIGNED_16, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001147 }
1148
Tim Murrayc11e25c2013-04-09 11:01:01 -07001149 /**
1150 * Copy from an array into a rectangular region in this Allocation. The
1151 * array is assumed to be tightly packed.
1152 *
1153 * @param xoff X offset of the region to update in this Allocation
1154 * @param yoff Y offset of the region to update in this Allocation
1155 * @param w Width of the region to update
1156 * @param h Height of the region to update
1157 * @param data to be placed into the Allocation
1158 */
Jason Samsf7086092011-01-12 13:28:37 -08001159 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001160 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001161 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1162 Element.DataType.SIGNED_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001163 }
1164
Tim Murrayc11e25c2013-04-09 11:01:01 -07001165 /**
1166 * Copy from an array into a rectangular region in this Allocation. The
1167 * array is assumed to be tightly packed.
1168 *
1169 * @param xoff X offset of the region to update in this Allocation
1170 * @param yoff Y offset of the region to update in this Allocation
1171 * @param w Width of the region to update
1172 * @param h Height of the region to update
1173 * @param data to be placed into the Allocation
1174 */
Jason Samsf7086092011-01-12 13:28:37 -08001175 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001176 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001177 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1178 Element.DataType.FLOAT_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001179 }
1180
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001181 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001182 * Copy a rectangular region from an Allocation into a rectangular region in
1183 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001184 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001185 * @param xoff X offset of the region in this Allocation
1186 * @param yoff Y offset of the region in this Allocation
1187 * @param w Width of the region to update.
1188 * @param h Height of the region to update.
1189 * @param data source Allocation.
1190 * @param dataXoff X offset in source Allocation
1191 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001192 */
1193 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1194 Allocation data, int dataXoff, int dataYoff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001195 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001196 mRS.validate();
1197 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -07001198 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -07001199 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001200 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -07001201 data.mSelectedLOD, data.mSelectedFace.mID);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001202 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001203 }
1204
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001205 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001206 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1207 * and width of the update will use the height and width of the {@link
1208 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001209 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001210 * @param xoff X offset of the region to update in this Allocation
1211 * @param yoff Y offset of the region to update in this Allocation
1212 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001213 */
1214 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001215 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Jason Samsfa445b92011-01-07 17:00:07 -08001216 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001217 if (data.getConfig() == null) {
1218 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1219 Canvas c = new Canvas(newBitmap);
1220 c.drawBitmap(data, 0, 0, null);
1221 copy2DRangeFrom(xoff, yoff, newBitmap);
Jason Samsb05d6892013-04-09 15:59:24 -07001222 return;
Tim Murrayabd5db92013-02-28 11:45:22 -08001223 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001224 validateBitmapFormat(data);
1225 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -07001226 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001227 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001228 }
1229
Jason Samsb05d6892013-04-09 15:59:24 -07001230 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1231 if (mAdaptedAllocation != null) {
1232
1233 } else {
1234
1235 if (xoff < 0 || yoff < 0 || zoff < 0) {
1236 throw new RSIllegalArgumentException("Offset cannot be negative.");
1237 }
1238 if (h < 0 || w < 0 || d < 0) {
1239 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1240 }
1241 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1242 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1243 }
1244 }
1245 }
1246
1247 /**
1248 * @hide
1249 *
1250 */
Jason Sams3042d262013-11-25 18:28:33 -08001251 private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1252 Object array, Element.DataType dt, int arrayLen) {
1253 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
Jason Samsb05d6892013-04-09 15:59:24 -07001254 mRS.validate();
1255 validate3DRange(xoff, yoff, zoff, w, h, d);
Miao Wang87e908d2015-03-02 15:15:15 -08001256 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1257 // AutoPadding for Vec3 Element
1258 boolean usePadding = false;
1259 int sizeBytes = arrayLen * dt.mSize;
1260 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1261 if (dataSize / 4 * 3 > sizeBytes) {
1262 throw new RSIllegalArgumentException("Array too small for allocation type.");
1263 }
1264 usePadding = true;
1265 sizeBytes = dataSize;
1266 } else {
1267 if (dataSize > sizeBytes) {
1268 throw new RSIllegalArgumentException("Array too small for allocation type.");
1269 }
1270 }
Jason Sams3042d262013-11-25 18:28:33 -08001271 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
Miao Wang87e908d2015-03-02 15:15:15 -08001272 array, sizeBytes, dt,
1273 mType.mElement.mType.mSize, usePadding);
Jason Sams3042d262013-11-25 18:28:33 -08001274 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb05d6892013-04-09 15:59:24 -07001275 }
1276
1277 /**
1278 * @hide
Jason Samsb05d6892013-04-09 15:59:24 -07001279 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001280 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001281 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001282 * @param xoff X offset of the region to update in this Allocation
1283 * @param yoff Y offset of the region to update in this Allocation
1284 * @param zoff Z offset of the region to update in this Allocation
1285 * @param w Width of the region to update
1286 * @param h Height of the region to update
1287 * @param d Depth of the region to update
Miao Wang87e908d2015-03-02 15:15:15 -08001288 * @param array to be placed into the allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001289 */
Jason Sams3042d262013-11-25 18:28:33 -08001290 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1291 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
1292 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
1293 validateObjectIsPrimitiveArray(array, true),
1294 java.lang.reflect.Array.getLength(array));
1295 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb05d6892013-04-09 15:59:24 -07001296 }
1297
1298 /**
1299 * @hide
1300 * Copy a rectangular region into the allocation from another
1301 * allocation.
1302 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001303 * @param xoff X offset of the region to update in this Allocation
1304 * @param yoff Y offset of the region to update in this Allocation
1305 * @param zoff Z offset of the region to update in this Allocation
1306 * @param w Width of the region to update.
1307 * @param h Height of the region to update.
1308 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001309 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001310 * @param dataXoff X offset of the region in the source Allocation
1311 * @param dataYoff Y offset of the region in the source Allocation
1312 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001313 */
1314 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1315 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1316 mRS.validate();
1317 validate3DRange(xoff, yoff, zoff, w, h, d);
1318 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1319 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1320 data.mSelectedLOD);
1321 }
1322
Jason Samsfa445b92011-01-07 17:00:07 -08001323
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001324 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001325 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1326 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001327 *
1328 * @param b The bitmap to be set from the Allocation.
1329 */
Jason Samsfa445b92011-01-07 17:00:07 -08001330 public void copyTo(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001331 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsfb9f82c2011-01-12 14:53:25 -08001332 mRS.validate();
1333 validateBitmapFormat(b);
1334 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -07001335 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001336 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001337 }
1338
Jason Sams3042d262013-11-25 18:28:33 -08001339 private void copyTo(Object array, Element.DataType dt, int arrayLen) {
1340 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Miao Wangb590b352015-01-15 11:09:23 -08001341 if (dt.mSize * arrayLen < mSize) {
1342 throw new RSIllegalArgumentException(
1343 "Size of output array cannot be smaller than size of allocation.");
1344 }
Jason Sams3042d262013-11-25 18:28:33 -08001345 mRS.validate();
Miao Wang87e908d2015-03-02 15:15:15 -08001346 boolean usePadding = false;
1347 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1348 usePadding = true;
1349 }
1350 mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
Jason Sams3042d262013-11-25 18:28:33 -08001351 Trace.traceEnd(RenderScript.TRACE_TAG);
1352 }
1353
1354 /**
1355 * Copy from the Allocation into an array. The array must be at
1356 * least as large as the Allocation. The
1357 * {@link android.renderscript.Element} must match the component
1358 * type of the array passed in.
1359 *
1360 * @param array The array to be set from the Allocation.
1361 */
1362 public void copyTo(Object array) {
1363 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1364 java.lang.reflect.Array.getLength(array));
1365 }
1366
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001367 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001368 * Copy from the Allocation into a byte array. The array must be at least
1369 * as large as the Allocation. The allocation must be of an 8 bit integer
1370 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001371 *
1372 * @param d The array to be set from the Allocation.
1373 */
Jason Samsfa445b92011-01-07 17:00:07 -08001374 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001375 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001376 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001377 }
1378
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001379 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001380 * Copy from the Allocation into a short array. The array must be at least
1381 * as large as the Allocation. The allocation must be of an 16 bit integer
1382 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001383 *
1384 * @param d The array to be set from the Allocation.
1385 */
Jason Samsfa445b92011-01-07 17:00:07 -08001386 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001387 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001388 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001389 }
1390
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001391 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001392 * Copy from the Allocation into a int array. The array must be at least as
1393 * large as the Allocation. The allocation must be of an 32 bit integer
1394 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001395 *
1396 * @param d The array to be set from the Allocation.
1397 */
Jason Samsfa445b92011-01-07 17:00:07 -08001398 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001399 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001400 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001401 }
1402
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001403 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001404 * Copy from the Allocation into a float array. The array must be at least
1405 * as large as the Allocation. The allocation must be of an 32 bit float
1406 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001407 *
1408 * @param d The array to be set from the Allocation.
1409 */
Jason Samsfa445b92011-01-07 17:00:07 -08001410 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001411 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001412 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001413 }
1414
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001415 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001416 * @hide
Miao Wang45cec0a2015-03-04 16:40:21 -08001417 * This is only intended to be used by auto-generated code reflected from
1418 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -08001419 *
1420 * @param xoff
1421 * @param yoff
1422 * @param zoff
1423 * @param component_number
1424 * @param array
1425 */
Miao Wang45cec0a2015-03-04 16:40:21 -08001426 public void copyToFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001427 mRS.validate();
1428 if (component_number >= mType.mElement.mElements.length) {
1429 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
1430 }
1431 if(xoff < 0) {
1432 throw new RSIllegalArgumentException("Offset x must be >= 0.");
1433 }
1434 if(yoff < 0) {
1435 throw new RSIllegalArgumentException("Offset y must be >= 0.");
1436 }
1437 if(zoff < 0) {
1438 throw new RSIllegalArgumentException("Offset z must be >= 0.");
1439 }
1440
Miao Wang45cec0a2015-03-04 16:40:21 -08001441 final byte[] data = fp.getData();
1442 int data_length = fp.getPos();
Miao Wangc8e237e2015-02-20 18:36:32 -08001443 int eSize = mType.mElement.mElements[component_number].getBytesSize();
1444 eSize *= mType.mElement.mArraySizes[component_number];
1445
Miao Wang45cec0a2015-03-04 16:40:21 -08001446 if (data_length != eSize) {
1447 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
1448 " does not match component size " + eSize + ".");
Miao Wangc8e237e2015-02-20 18:36:32 -08001449 }
1450
1451 mRS.nAllocationElementRead(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Miao Wang45cec0a2015-03-04 16:40:21 -08001452 component_number, data, data_length);
Miao Wangc8e237e2015-02-20 18:36:32 -08001453 }
1454 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001455 * Resize a 1D allocation. The contents of the allocation are preserved.
1456 * If new elements are allocated objects are created with null contents and
1457 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001458 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001459 * <p>If the new region is smaller the references of any objects outside the
1460 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001461 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001462 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001463 *
1464 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001465 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001466 * @deprecated RenderScript objects should be immutable once created. The
Tim Murraycd38b762014-08-13 13:20:25 -07001467 * replacement is to create a new allocation and copy the contents. This
1468 * function will throw an exception if API 21 or higher is used.
Jason Samsf7086092011-01-12 13:28:37 -08001469 */
Jason Sams31a7e422010-10-26 13:09:17 -07001470 public synchronized void resize(int dimX) {
Tim Murraycd38b762014-08-13 13:20:25 -07001471 if (mRS.getApplicationContext().getApplicationInfo().targetSdkVersion >= 21) {
1472 throw new RSRuntimeException("Resize is not allowed in API 21+.");
1473 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001474 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001475 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001476 }
Jason Samse07694b2012-04-03 15:36:36 -07001477 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001478 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001479
Tim Murray460a0492013-11-19 12:45:54 -08001480 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001481 mType = new Type(typeID, mRS);
1482 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001483 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001484 }
1485
Miao Wangc8e237e2015-02-20 18:36:32 -08001486 private void copy1DRangeToUnchecked(int off, int count, Object array,
1487 Element.DataType dt, int arrayLen) {
1488 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked");
1489 final int dataSize = mType.mElement.getBytesSize() * count;
Miao Wang87e908d2015-03-02 15:15:15 -08001490 // AutoPadding for Vec3 Element
1491 boolean usePadding = false;
1492 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1493 usePadding = true;
1494 }
1495 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
1496 mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
1497 mType.mElement.mType.mSize, usePadding);
Miao Wangc8e237e2015-02-20 18:36:32 -08001498 Trace.traceEnd(RenderScript.TRACE_TAG);
1499 }
1500
1501 /**
1502 * @hide
1503 * Copy part of this Allocation into an array. This method does not
1504 * guarantee that the Allocation is compatible with the input buffer.
1505 *
1506 * @param off The offset of the first element to be copied.
1507 * @param count The number of elements to be copied.
1508 * @param array The dest data array
1509 */
1510 public void copy1DRangeToUnchecked(int off, int count, Object array) {
1511 copy1DRangeToUnchecked(off, count, array,
1512 validateObjectIsPrimitiveArray(array, false),
1513 java.lang.reflect.Array.getLength(array));
1514 }
1515
1516 /**
1517 * @hide
1518 * Copy part of this Allocation into an array. This method does not
1519 * guarantee that the Allocation is compatible with the input buffer.
1520 *
1521 * @param off The offset of the first element to be copied.
1522 * @param count The number of elements to be copied.
1523 * @param d the source data array
1524 */
1525 public void copy1DRangeToUnchecked(int off, int count, int[] d) {
1526 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
1527 }
1528
1529 /**
1530 * @hide
1531 * Copy part of this Allocation into an array. This method does not
1532 * guarantee that the Allocation is compatible with the input buffer.
1533 *
1534 * @param off The offset of the first element to be copied.
1535 * @param count The number of elements to be copied.
1536 * @param d the source data array
1537 */
1538 public void copy1DRangeToUnchecked(int off, int count, short[] d) {
1539 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
1540 }
1541
1542 /**
1543 * @hide
1544 * Copy part of this Allocation into an array. This method does not
1545 * guarantee that the Allocation is compatible with the input buffer.
1546 *
1547 * @param off The offset of the first element to be copied.
1548 * @param count The number of elements to be copied.
1549 * @param d the source data array
1550 */
1551 public void copy1DRangeToUnchecked(int off, int count, byte[] d) {
1552 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
1553 }
1554
1555 /**
1556 * @hide
1557 * Copy part of this Allocation into an array. This method does not
1558 * guarantee that the Allocation is compatible with the input buffer.
1559 *
1560 * @param off The offset of the first element to be copied.
1561 * @param count The number of elements to be copied.
1562 * @param d the source data array
1563 */
1564 public void copy1DRangeToUnchecked(int off, int count, float[] d) {
1565 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
1566 }
1567
1568
1569 /**
1570 * @hide
1571 * Copy part of this Allocation into an array. This method does not
1572 * and will generate exceptions if the Allocation type does not
1573 * match the component type of the array passed in.
1574 *
1575 * @param off The offset of the first element to be copied.
1576 * @param count The number of elements to be copied.
1577 * @param array The source data array.
1578 */
1579 public void copy1DRangeTo(int off, int count, Object array) {
1580 copy1DRangeToUnchecked(off, count, array,
1581 validateObjectIsPrimitiveArray(array, true),
1582 java.lang.reflect.Array.getLength(array));
1583 }
1584
1585 /**
1586 * @hide
1587 * Copy part of this Allocation into an array. This method does not
1588 * and will generate exceptions if the Allocation type is not a 32 bit
1589 * integer type.
1590 *
1591 * @param off The offset of the first element to be copied.
1592 * @param count The number of elements to be copied.
1593 * @param d the source data array
1594 */
1595 public void copy1DRangeTo(int off, int count, int[] d) {
1596 validateIsInt32();
1597 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
1598 }
1599
1600 /**
1601 * @hide
1602 * Copy part of this Allocation into an array. This method does not
1603 * and will generate exceptions if the Allocation type is not a 16 bit
1604 * integer type.
1605 *
1606 * @param off The offset of the first element to be copied.
1607 * @param count The number of elements to be copied.
1608 * @param d the source data array
1609 */
1610 public void copy1DRangeTo(int off, int count, short[] d) {
1611 validateIsInt16();
1612 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
1613 }
1614
1615 /**
1616 * @hide
1617 * Copy part of this Allocation into an array. This method does not
1618 * and will generate exceptions if the Allocation type is not an 8 bit
1619 * integer type.
1620 *
1621 * @param off The offset of the first element to be copied.
1622 * @param count The number of elements to be copied.
1623 * @param d the source data array
1624 */
1625 public void copy1DRangeTo(int off, int count, byte[] d) {
1626 validateIsInt8();
1627 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
1628 }
1629
1630 /**
1631 * @hide
1632 * Copy part of this Allocation into an array. This method does not
1633 * and will generate exceptions if the Allocation type is not a 32 bit float
1634 * type.
1635 *
1636 * @param off The offset of the first element to be copied.
1637 * @param count The number of elements to be copied.
1638 * @param d the source data array.
1639 */
1640 public void copy1DRangeTo(int off, int count, float[] d) {
1641 validateIsFloat32();
1642 copy1DRangeToUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
1643 }
1644
1645
1646 void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
1647 Element.DataType dt, int arrayLen) {
1648 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked");
1649 mRS.validate();
1650 validate2DRange(xoff, yoff, w, h);
Miao Wang87e908d2015-03-02 15:15:15 -08001651 final int dataSize = mType.mElement.getBytesSize() * w * h;
1652 // AutoPadding for Vec3 Element
1653 boolean usePadding = false;
1654 int sizeBytes = arrayLen * dt.mSize;
1655 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1656 if (dataSize / 4 * 3 > sizeBytes) {
1657 throw new RSIllegalArgumentException("Array too small for allocation type.");
1658 }
1659 usePadding = true;
1660 sizeBytes = dataSize;
1661 } else {
1662 if (dataSize > sizeBytes) {
1663 throw new RSIllegalArgumentException("Array too small for allocation type.");
1664 }
1665 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001666 mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
Miao Wang87e908d2015-03-02 15:15:15 -08001667 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
Miao Wangc8e237e2015-02-20 18:36:32 -08001668 Trace.traceEnd(RenderScript.TRACE_TAG);
1669 }
1670
1671 /**
1672 * @hide
1673 * Copy from a rectangular region in this Allocation into an array.
1674 *
1675 * @param xoff X offset of the region to copy in this Allocation
1676 * @param yoff Y offset of the region to copy in this Allocation
1677 * @param w Width of the region to copy
1678 * @param h Height of the region to copy
1679 * @param array Dest Array to be copied into
1680 */
1681 public void copy2DRangeTo(int xoff, int yoff, int w, int h, Object array) {
1682 copy2DRangeToUnchecked(xoff, yoff, w, h, array,
1683 validateObjectIsPrimitiveArray(array, true),
1684 java.lang.reflect.Array.getLength(array));
1685 }
1686
1687 /**
1688 * @hide
1689 * Copy from a rectangular region in this Allocation into an array.
1690 *
1691 * @param xoff X offset of the region to copy in this Allocation
1692 * @param yoff Y offset of the region to copy in this Allocation
1693 * @param w Width of the region to copy
1694 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001695 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001696 */
1697 public void copy2DRangeTo(int xoff, int yoff, int w, int h, byte[] data) {
1698 validateIsInt8();
1699 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1700 Element.DataType.SIGNED_8, data.length);
1701 }
1702
1703 /**
1704 * @hide
1705 * Copy from a rectangular region in this Allocation into an array.
1706 *
1707 * @param xoff X offset of the region to copy in this Allocation
1708 * @param yoff Y offset of the region to copy in this Allocation
1709 * @param w Width of the region to copy
1710 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001711 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001712 */
1713 public void copy2DRangeTo(int xoff, int yoff, int w, int h, short[] data) {
1714 validateIsInt16();
1715 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1716 Element.DataType.SIGNED_16, data.length);
1717 }
1718
1719 /**
1720 * @hide
1721 * Copy from a rectangular region in this Allocation into an array.
1722 *
1723 * @param xoff X offset of the region to copy in this Allocation
1724 * @param yoff Y offset of the region to copy in this Allocation
1725 * @param w Width of the region to copy
1726 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001727 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001728 */
1729 public void copy2DRangeTo(int xoff, int yoff, int w, int h, int[] data) {
1730 validateIsInt32();
1731 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1732 Element.DataType.SIGNED_32, data.length);
1733 }
1734
1735 /**
1736 * @hide
1737 * Copy from a rectangular region in this Allocation into an array.
1738 *
1739 * @param xoff X offset of the region to copy in this Allocation
1740 * @param yoff Y offset of the region to copy in this Allocation
1741 * @param w Width of the region to copy
1742 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001743 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001744 */
1745 public void copy2DRangeTo(int xoff, int yoff, int w, int h, float[] data) {
1746 validateIsFloat32();
1747 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1748 Element.DataType.FLOAT_32, data.length);
1749 }
1750
1751
1752 /**
1753 * @hide
1754 *
1755 */
1756 private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1757 Object array, Element.DataType dt, int arrayLen) {
1758 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked");
1759 mRS.validate();
1760 validate3DRange(xoff, yoff, zoff, w, h, d);
Miao Wang87e908d2015-03-02 15:15:15 -08001761 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1762 // AutoPadding for Vec3 Element
1763 boolean usePadding = false;
1764 int sizeBytes = arrayLen * dt.mSize;
1765 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1766 if (dataSize / 4 * 3 > sizeBytes) {
1767 throw new RSIllegalArgumentException("Array too small for allocation type.");
1768 }
1769 usePadding = true;
1770 sizeBytes = dataSize;
1771 } else {
1772 if (dataSize > sizeBytes) {
1773 throw new RSIllegalArgumentException("Array too small for allocation type.");
1774 }
1775 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001776 mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
Miao Wang87e908d2015-03-02 15:15:15 -08001777 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
Miao Wangc8e237e2015-02-20 18:36:32 -08001778 Trace.traceEnd(RenderScript.TRACE_TAG);
1779 }
1780
1781 /**
1782 * @hide
1783 * Copy from a rectangular region in this Allocation into an array.
1784 *
1785 * @param xoff X offset of the region to copy in this Allocation
1786 * @param yoff Y offset of the region to copy in this Allocation
1787 * @param zoff Z offset of the region to copy in this Allocation
1788 * @param w Width of the region to copy
1789 * @param h Height of the region to copy
1790 * @param d Depth of the region to copy
1791 * @param array Dest Array to be copied into
1792 */
1793 public void copy3DRangeTo(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1794 copy3DRangeToUnchecked(xoff, yoff, zoff, w, h, d, array,
1795 validateObjectIsPrimitiveArray(array, true),
1796 java.lang.reflect.Array.getLength(array));
1797 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001798
1799 // creation
1800
Jason Sams49a05d72010-12-29 14:31:29 -08001801 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001802 static {
1803 mBitmapOptions.inScaled = false;
1804 }
1805
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001806 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001807 * Creates a new Allocation with the given {@link
1808 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001809 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001810 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001811 * @param mips specifies desired mipmap behaviour for the
1812 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001813 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001814 * utilized
1815 */
1816 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001817 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
Jason Sams771bebb2009-12-07 12:40:12 -08001818 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001819 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001820 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001821 }
Tim Murray460a0492013-11-19 12:45:54 -08001822 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001823 if (id == 0) {
1824 throw new RSRuntimeException("Allocation creation failed.");
1825 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001826 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams857d0c72011-11-23 15:02:15 -08001827 return new Allocation(id, rs, type, usage);
1828 }
1829
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001830 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001831 * Creates an Allocation with the size specified by the type and no mipmaps
1832 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001833 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001834 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001835 * @param type renderscript type describing data layout
1836 * @param usage bit field specifying how the allocation is
1837 * utilized
1838 *
1839 * @return allocation
1840 */
Jason Samse5d37122010-12-16 00:33:33 -08001841 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1842 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1843 }
1844
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001845 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001846 * Creates an Allocation for use by scripts with a given {@link
1847 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001848 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001849 * @param rs Context to which the Allocation will belong.
1850 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001851 *
1852 * @return allocation
1853 */
Jason Sams5476b452010-12-08 16:14:36 -08001854 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001855 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001856 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001857
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001858 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001859 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001860 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001861 * @param rs Context to which the Allocation will belong.
1862 * @param e Element to use in the Allocation
1863 * @param count the number of Elements in the Allocation
1864 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001865 * utilized
1866 *
1867 * @return allocation
1868 */
Jason Sams5476b452010-12-08 16:14:36 -08001869 static public Allocation createSized(RenderScript rs, Element e,
1870 int count, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001871 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
Jason Sams771bebb2009-12-07 12:40:12 -08001872 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001873 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001874 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001875 Type t = b.create();
1876
Tim Murray460a0492013-11-19 12:45:54 -08001877 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001878 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001879 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001880 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001881 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001882 return new Allocation(id, rs, t, usage);
1883 }
1884
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001885 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001886 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001887 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001888 * @param rs Context to which the Allocation will belong.
1889 * @param e Element to use in the Allocation
1890 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001891 *
1892 * @return allocation
1893 */
Jason Sams5476b452010-12-08 16:14:36 -08001894 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001895 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001896 }
1897
Jason Sams49a05d72010-12-29 14:31:29 -08001898 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001899 final Bitmap.Config bc = b.getConfig();
1900 if (bc == Bitmap.Config.ALPHA_8) {
1901 return Element.A_8(rs);
1902 }
1903 if (bc == Bitmap.Config.ARGB_4444) {
1904 return Element.RGBA_4444(rs);
1905 }
1906 if (bc == Bitmap.Config.ARGB_8888) {
1907 return Element.RGBA_8888(rs);
1908 }
1909 if (bc == Bitmap.Config.RGB_565) {
1910 return Element.RGB_565(rs);
1911 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001912 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001913 }
1914
Jason Sams49a05d72010-12-29 14:31:29 -08001915 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001916 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001917 Element e = elementFromBitmap(rs, b);
1918 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001919 tb.setX(b.getWidth());
1920 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001921 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001922 return tb.create();
1923 }
1924
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001925 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001926 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001927 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001928 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001929 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001930 * @param mips specifies desired mipmap behaviour for the
1931 * allocation
1932 * @param usage bit field specifying how the allocation is
1933 * utilized
1934 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001935 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001936 *
1937 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001938 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001939 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001940 int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001941 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
Jason Sams771bebb2009-12-07 12:40:12 -08001942 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001943
1944 // WAR undocumented color formats
1945 if (b.getConfig() == null) {
1946 if ((usage & USAGE_SHARED) != 0) {
1947 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1948 }
1949 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1950 Canvas c = new Canvas(newBitmap);
1951 c.drawBitmap(b, 0, 0, null);
1952 return createFromBitmap(rs, newBitmap, mips, usage);
1953 }
1954
Jason Sams5476b452010-12-08 16:14:36 -08001955 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001956
Tim Murraya3145512012-12-04 17:59:29 -08001957 // enable optimized bitmap path only with no mipmap and script-only usage
1958 if (mips == MipmapControl.MIPMAP_NONE &&
1959 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Tim Murray78e64942013-04-09 17:28:56 -07001960 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
Tim Murray460a0492013-11-19 12:45:54 -08001961 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08001962 if (id == 0) {
1963 throw new RSRuntimeException("Load failed.");
1964 }
1965
1966 // keep a reference to the Bitmap around to prevent GC
1967 Allocation alloc = new Allocation(id, rs, t, usage);
1968 alloc.setBitmap(b);
1969 return alloc;
1970 }
1971
Jason Sams9bf18922013-04-13 19:48:36 -07001972
Tim Murray460a0492013-11-19 12:45:54 -08001973 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001974 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001975 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001976 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001977 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001978 return new Allocation(id, rs, t, usage);
1979 }
1980
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001981 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001982 * Returns the handle to a raw buffer that is being managed by the screen
1983 * compositor. This operation is only valid for Allocations with {@link
1984 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001985 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001986 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001987 *
1988 */
1989 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08001990 if ((mUsage & USAGE_IO_INPUT) == 0) {
1991 throw new RSInvalidStateException("Allocation is not a surface texture.");
1992 }
1993 return mRS.nAllocationGetSurface(getID(mRS));
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001994 }
1995
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001996 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001997 * Associate a {@link android.view.Surface} with this Allocation. This
1998 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001999 *
2000 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08002001 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002002 public void setSurface(Surface sur) {
2003 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08002004 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
2005 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
2006 }
2007
Jason Samse07694b2012-04-03 15:36:36 -07002008 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08002009 }
2010
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002011 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002012 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08002013 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002014 * <p>With target API version 18 or greater, this Allocation will be created
2015 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
2016 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
2017 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002018 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002019 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002020 * @param b bitmap source for the allocation data
2021 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002022 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002023 *
2024 */
Jason Sams6d8eb262010-12-15 01:41:00 -08002025 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08002026 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2027 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07002028 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08002029 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002030 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
2031 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08002032 }
2033
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002034 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002035 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
2036 * containing the horizontal list of cube faces. Each face must be a square,
2037 * have the same size as all other faces, and have a width that is a power
2038 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002039 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002040 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07002041 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002042 * format: right, left, top, bottom, front, back
2043 * @param mips specifies desired mipmap behaviour for the cubemap
2044 * @param usage bit field specifying how the cubemap is utilized
2045 *
2046 * @return allocation containing cubemap data
2047 *
2048 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002049 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002050 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002051 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002052 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08002053
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002054 int height = b.getHeight();
2055 int width = b.getWidth();
2056
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002057 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002058 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
2059 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002060 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002061 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002062 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002063 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002064 if (!isPow2) {
2065 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2066 }
2067
2068 Element e = elementFromBitmap(rs, b);
2069 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002070 tb.setX(height);
2071 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002072 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08002073 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002074 Type t = tb.create();
2075
Tim Murray460a0492013-11-19 12:45:54 -08002076 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002077 if(id == 0) {
2078 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
2079 }
Jason Sams5476b452010-12-08 16:14:36 -08002080 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002081 }
2082
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002083 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002084 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
2085 * from a {@link android.graphics.Bitmap} containing the horizontal list of
2086 * cube faces. Each face must be a square, have the same size as all other
2087 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002088 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002089 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002090 * @param b bitmap with cubemap faces layed out in the following
2091 * format: right, left, top, bottom, front, back
2092 *
2093 * @return allocation containing cubemap data
2094 *
2095 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002096 static public Allocation createCubemapFromBitmap(RenderScript rs,
2097 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08002098 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002099 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08002100 }
2101
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002102 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002103 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
2104 * objects containing the cube faces. Each face must be a square, have the
2105 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002106 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002107 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002108 * @param xpos cubemap face in the positive x direction
2109 * @param xneg cubemap face in the negative x direction
2110 * @param ypos cubemap face in the positive y direction
2111 * @param yneg cubemap face in the negative y direction
2112 * @param zpos cubemap face in the positive z direction
2113 * @param zneg cubemap face in the negative z direction
2114 * @param mips specifies desired mipmap behaviour for the cubemap
2115 * @param usage bit field specifying how the cubemap is utilized
2116 *
2117 * @return allocation containing cubemap data
2118 *
2119 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002120 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2121 Bitmap xpos,
2122 Bitmap xneg,
2123 Bitmap ypos,
2124 Bitmap yneg,
2125 Bitmap zpos,
2126 Bitmap zneg,
2127 MipmapControl mips,
2128 int usage) {
2129 int height = xpos.getHeight();
2130 if (xpos.getWidth() != height ||
2131 xneg.getWidth() != height || xneg.getHeight() != height ||
2132 ypos.getWidth() != height || ypos.getHeight() != height ||
2133 yneg.getWidth() != height || yneg.getHeight() != height ||
2134 zpos.getWidth() != height || zpos.getHeight() != height ||
2135 zneg.getWidth() != height || zneg.getHeight() != height) {
2136 throw new RSIllegalArgumentException("Only square cube map faces supported");
2137 }
2138 boolean isPow2 = (height & (height - 1)) == 0;
2139 if (!isPow2) {
2140 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2141 }
2142
2143 Element e = elementFromBitmap(rs, xpos);
2144 Type.Builder tb = new Type.Builder(rs, e);
2145 tb.setX(height);
2146 tb.setY(height);
2147 tb.setFaces(true);
2148 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
2149 Type t = tb.create();
2150 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
2151
2152 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07002153 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002154 adapter.copyFrom(xpos);
2155 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
2156 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002157 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002158 adapter.copyFrom(ypos);
2159 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
2160 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002161 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002162 adapter.copyFrom(zpos);
2163 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
2164 adapter.copyFrom(zneg);
2165
2166 return cubemap;
2167 }
2168
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002169 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002170 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
2171 * from 6 {@link android.graphics.Bitmap} objects containing the cube
2172 * faces. Each face must be a square, have the same size as all other faces,
2173 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002174 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002175 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002176 * @param xpos cubemap face in the positive x direction
2177 * @param xneg cubemap face in the negative x direction
2178 * @param ypos cubemap face in the positive y direction
2179 * @param yneg cubemap face in the negative y direction
2180 * @param zpos cubemap face in the positive z direction
2181 * @param zneg cubemap face in the negative z direction
2182 *
2183 * @return allocation containing cubemap data
2184 *
2185 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002186 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2187 Bitmap xpos,
2188 Bitmap xneg,
2189 Bitmap ypos,
2190 Bitmap yneg,
2191 Bitmap zpos,
2192 Bitmap zneg) {
2193 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
2194 zpos, zneg, MipmapControl.MIPMAP_NONE,
2195 USAGE_GRAPHICS_TEXTURE);
2196 }
2197
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002198 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002199 * Creates an Allocation from the Bitmap referenced
2200 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002201 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002202 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002203 * @param res application resources
2204 * @param id resource id to load the data from
2205 * @param mips specifies desired mipmap behaviour for the
2206 * allocation
2207 * @param usage bit field specifying how the allocation is
2208 * utilized
2209 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002210 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002211 *
2212 */
Jason Sams5476b452010-12-08 16:14:36 -08002213 static public Allocation createFromBitmapResource(RenderScript rs,
2214 Resources res,
2215 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08002216 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002217 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07002218
Jason Sams771bebb2009-12-07 12:40:12 -08002219 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07002220 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
2221 throw new RSIllegalArgumentException("Unsupported usage specified.");
2222 }
Jason Sams5476b452010-12-08 16:14:36 -08002223 Bitmap b = BitmapFactory.decodeResource(res, id);
2224 Allocation alloc = createFromBitmap(rs, b, mips, usage);
2225 b.recycle();
2226 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07002227 }
2228
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002229 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002230 * Creates a non-mipmapped Allocation to use as a graphics texture from the
2231 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002232 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002233 * <p>With target API version 18 or greater, this allocation will be created
2234 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
2235 * target API version 17 or lower, this allocation will be created with
2236 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08002237 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002238 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002239 * @param res application resources
2240 * @param id resource id to load the data from
2241 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002242 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002243 *
2244 */
Jason Sams5476b452010-12-08 16:14:36 -08002245 static public Allocation createFromBitmapResource(RenderScript rs,
2246 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08002247 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08002248 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2249 return createFromBitmapResource(rs, res, id,
2250 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07002251 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08002252 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002253 return createFromBitmapResource(rs, res, id,
2254 MipmapControl.MIPMAP_NONE,
2255 USAGE_GRAPHICS_TEXTURE);
2256 }
2257
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002258 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002259 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002260 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002261 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002262 * @param str string to create the allocation from
2263 * @param usage bit field specifying how the allocaiton is
2264 * utilized
2265 *
2266 */
Jason Sams5476b452010-12-08 16:14:36 -08002267 static public Allocation createFromString(RenderScript rs,
2268 String str,
2269 int usage) {
2270 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002271 byte[] allocArray = null;
2272 try {
2273 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08002274 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002275 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002276 return alloc;
2277 }
2278 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08002279 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002280 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002281 }
Jason Sams739c8262013-04-11 18:07:52 -07002282
2283 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002284 * Interface to handle notification when new buffers are available via
2285 * {@link #USAGE_IO_INPUT}. An application will receive one notification
2286 * when a buffer is available. Additional buffers will not trigger new
2287 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07002288 */
Jason Sams42ef2382013-08-29 13:30:59 -07002289 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07002290 public void onBufferAvailable(Allocation a);
2291 }
2292
2293 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002294 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07002295 *
Jason Sams42ef2382013-08-29 13:30:59 -07002296 * @param callback instance of the OnBufferAvailableListener
2297 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07002298 */
Jason Sams42ef2382013-08-29 13:30:59 -07002299 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07002300 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002301 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07002302 mBufferNotifier = callback;
2303 }
2304 }
2305
Tim Murrayb730d862014-08-18 16:14:24 -07002306 static void sendBufferNotification(long id) {
Jason Sams739c8262013-04-11 18:07:52 -07002307 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002308 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07002309
2310 if ((a != null) && (a.mBufferNotifier != null)) {
2311 a.mBufferNotifier.onBufferAvailable(a);
2312 }
2313 }
2314 }
2315
Miao Wangf0f6e802015-02-03 17:16:43 -08002316 /**
2317 * For USAGE_IO_OUTPUT, destroy() implies setSurface(null).
2318 *
2319 */
2320 @Override
2321 public void destroy() {
2322 if((mUsage & USAGE_IO_OUTPUT) != 0) {
2323 setSurface(null);
2324 }
2325 super.destroy();
2326 }
Jason Samsb8c5a842009-07-31 20:40:47 -07002327}