blob: bd8b6522730f24fb375cfd492f8fafcf1dc2cd11 [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 **/
Chris Craik06d29842015-06-02 17:19:24 -070054
Jason Samsb8c5a842009-07-31 20:40:47 -070055public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070056 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080057 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080058 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070059 Allocation mAdaptedAllocation;
Tim Murray2f2472c2013-08-22 14:55:26 -070060 int mSize;
Jason Samsba862d12011-07-07 15:24:42 -070061
Jason Sams615e7ce2012-01-13 14:01:20 -080062 boolean mReadAllowed = true;
63 boolean mWriteAllowed = true;
Miao Wang87e908d2015-03-02 15:15:15 -080064 boolean mAutoPadding = false;
Jason Sams46ba27e32015-02-06 17:45:15 -080065 int mSelectedX;
Jason Samsba862d12011-07-07 15:24:42 -070066 int mSelectedY;
67 int mSelectedZ;
68 int mSelectedLOD;
Jason Sams46ba27e32015-02-06 17:45:15 -080069 int mSelectedArray[];
Jason Samsba862d12011-07-07 15:24:42 -070070 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
71
72 int mCurrentDimX;
73 int mCurrentDimY;
74 int mCurrentDimZ;
75 int mCurrentCount;
Tim Murray460a0492013-11-19 12:45:54 -080076 static HashMap<Long, Allocation> mAllocationMap =
77 new HashMap<Long, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070078 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070079
Jason Sams1e68bac2015-03-17 16:36:55 -070080 private Surface mGetSurfaceSurface = null;
81
Jason Sams3042d262013-11-25 18:28:33 -080082 private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
83 final Class c = d.getClass();
84 if (!c.isArray()) {
85 throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
86 }
87 final Class cmp = c.getComponentType();
88 if (!cmp.isPrimitive()) {
89 throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
90 }
91
92 if (cmp == Long.TYPE) {
93 if (checkType) {
94 validateIsInt64();
95 return mType.mElement.mType;
96 }
97 return Element.DataType.SIGNED_64;
98 }
99
100 if (cmp == Integer.TYPE) {
101 if (checkType) {
102 validateIsInt32();
103 return mType.mElement.mType;
104 }
105 return Element.DataType.SIGNED_32;
106 }
107
108 if (cmp == Short.TYPE) {
109 if (checkType) {
110 validateIsInt16();
111 return mType.mElement.mType;
112 }
113 return Element.DataType.SIGNED_16;
114 }
115
116 if (cmp == Byte.TYPE) {
117 if (checkType) {
118 validateIsInt8();
119 return mType.mElement.mType;
120 }
121 return Element.DataType.SIGNED_8;
122 }
123
124 if (cmp == Float.TYPE) {
125 if (checkType) {
126 validateIsFloat32();
127 }
128 return Element.DataType.FLOAT_32;
129 }
130
131 if (cmp == Double.TYPE) {
132 if (checkType) {
133 validateIsFloat64();
134 }
135 return Element.DataType.FLOAT_64;
136 }
137 return null;
138 }
139
140
Tim Murrayc11e25c2013-04-09 11:01:01 -0700141 /**
142 * The usage of the Allocation. These signal to RenderScript where to place
143 * the Allocation in memory.
144 *
145 */
Jason Sams5476b452010-12-08 16:14:36 -0800146
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700147 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700148 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -0800149 */
Jason Sams5476b452010-12-08 16:14:36 -0800150 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800151
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700152 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700153 * The Allocation will be used as a texture source by one or more graphics
154 * programs.
Jason Samsf7086092011-01-12 13:28:37 -0800155 *
156 */
Jason Sams5476b452010-12-08 16:14:36 -0800157 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800158
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700159 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700160 * The Allocation will be used as a graphics mesh.
161 *
162 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800163 *
164 */
Jason Sams5476b452010-12-08 16:14:36 -0800165 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800166
167
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700168 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700169 * The Allocation will be used as the source of shader constants by one or
170 * more programs.
171 *
172 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800173 *
174 */
Jason Sams5476b452010-12-08 16:14:36 -0800175 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
176
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700177 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700178 * The Allocation will be used as a target for offscreen rendering
179 *
180 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700181 *
182 */
183 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
184
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700185 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700186 * The Allocation will be used as a {@link android.view.Surface}
187 * consumer. This usage will cause the Allocation to be created
188 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800189 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800190 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700191 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800192
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700193 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700194 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700195 * producer. The dimensions and format of the {@link
Jason Sams3a1b8e42013-09-24 15:18:52 -0700196 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700197 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800198 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800199 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700200 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700201
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700202 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700203 * The Allocation's backing store will be inherited from another object
204 * (usually a {@link android.graphics.Bitmap}); copying to or from the
205 * original source Bitmap will cause a synchronization rather than a full
206 * copy. {@link #syncAll} may also be used to synchronize the Allocation
207 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800208 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700209 * <p>This is set by default for allocations created with {@link
210 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800211 *
212 */
213 public static final int USAGE_SHARED = 0x0080;
214
215 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700216 * Controls mipmap behavior when using the bitmap creation and update
217 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800218 */
Jason Sams4ef66502010-12-10 16:03:15 -0800219 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700220 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700221 * No mipmaps will be generated and the type generated from the incoming
222 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800223 */
Jason Sams5476b452010-12-08 16:14:36 -0800224 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800225
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700226 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700227 * A full mipmap chain will be created in script memory. The Type of
228 * the Allocation will contain a full mipmap chain. On upload, the full
229 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800230 */
Jason Sams5476b452010-12-08 16:14:36 -0800231 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800232
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700233 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700234 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
235 * not contain mipmaps. On upload, the allocation data will contain a
236 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800237 */
Jason Sams5476b452010-12-08 16:14:36 -0800238 MIPMAP_ON_SYNC_TO_TEXTURE(2);
239
240 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800241 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800242 mID = id;
243 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700244 }
245
Jason Sams48fe5342011-07-08 13:52:30 -0700246
Tim Murray460a0492013-11-19 12:45:54 -0800247 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700248 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700249 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700250 }
Jason Samse07694b2012-04-03 15:36:36 -0700251 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700252 }
253
Jason Sams03d2d002012-03-23 13:51:56 -0700254
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700255 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700256 * Get the {@link android.renderscript.Element} of the {@link
257 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700258 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700259 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700260 *
261 */
262 public Element getElement() {
263 return mType.getElement();
264 }
265
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700266 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700267 * Get the usage flags of the Allocation.
268 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700269 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700270 *
271 */
272 public int getUsage() {
273 return mUsage;
274 }
275
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700276 /**
Miao Wang87e908d2015-03-02 15:15:15 -0800277 * @hide
278 * Enable/Disable AutoPadding for Vec3 elements.
279 *
Miao Wang179e8b52015-04-15 17:44:32 -0700280 * @param useAutoPadding True: enable AutoPadding; False: disable AutoPadding
Miao Wang87e908d2015-03-02 15:15:15 -0800281 *
282 */
283 public void setAutoPadding(boolean useAutoPadding) {
284 mAutoPadding = useAutoPadding;
285 }
286
287 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700288 * Get the size of the Allocation in bytes.
289 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700290 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700291 *
292 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700293 public int getBytesSize() {
Tim Murray04f0d6e2013-12-17 17:15:25 -0800294 if (mType.mDimYuv != 0) {
295 return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
296 }
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700297 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700298 }
299
Jason Sams452a7662011-07-07 16:05:18 -0700300 private void updateCacheInfo(Type t) {
301 mCurrentDimX = t.getX();
302 mCurrentDimY = t.getY();
303 mCurrentDimZ = t.getZ();
304 mCurrentCount = mCurrentDimX;
305 if (mCurrentDimY > 1) {
306 mCurrentCount *= mCurrentDimY;
307 }
308 if (mCurrentDimZ > 1) {
309 mCurrentCount *= mCurrentDimZ;
310 }
311 }
Jason Samsba862d12011-07-07 15:24:42 -0700312
Tim Murraya3145512012-12-04 17:59:29 -0800313 private void setBitmap(Bitmap b) {
314 mBitmap = b;
315 }
316
Tim Murray460a0492013-11-19 12:45:54 -0800317 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700318 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800319 if ((usage & ~(USAGE_SCRIPT |
320 USAGE_GRAPHICS_TEXTURE |
321 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700322 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800323 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800324 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800325 USAGE_IO_OUTPUT |
326 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800327 throw new RSIllegalArgumentException("Unknown usage specified.");
328 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800329
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700330 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800331 mWriteAllowed = false;
332
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700333 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800334 USAGE_GRAPHICS_TEXTURE |
335 USAGE_SCRIPT)) != 0) {
336 throw new RSIllegalArgumentException("Invalid usage combination.");
337 }
338 }
Jason Sams9bf18922013-04-13 19:48:36 -0700339
Jason Sams5476b452010-12-08 16:14:36 -0800340 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800341 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700342
Jason Sams452a7662011-07-07 16:05:18 -0700343 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700344 // TODO: A3D doesn't have Type info during creation, so we can't
345 // calculate the size ahead of time. We can possibly add a method
346 // to update the size in the future if it seems reasonable.
347 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700348 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700349 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700350 try {
351 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
352 } catch (Exception e) {
353 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
354 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
355 }
356 }
357
358 protected void finalize() throws Throwable {
359 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
360 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700361 }
362
Jason Sams3042d262013-11-25 18:28:33 -0800363 private void validateIsInt64() {
364 if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
365 (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
366 return;
367 }
368 throw new RSIllegalArgumentException(
369 "64 bit integer source does not match allocation type " + mType.mElement.mType);
370 }
371
Jason Samsb97b2512011-01-16 15:04:08 -0800372 private void validateIsInt32() {
373 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
374 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
375 return;
376 }
377 throw new RSIllegalArgumentException(
378 "32 bit integer source does not match allocation type " + mType.mElement.mType);
379 }
380
381 private void validateIsInt16() {
382 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
383 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
384 return;
385 }
386 throw new RSIllegalArgumentException(
387 "16 bit integer source does not match allocation type " + mType.mElement.mType);
388 }
389
390 private void validateIsInt8() {
391 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
392 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
393 return;
394 }
395 throw new RSIllegalArgumentException(
396 "8 bit integer source does not match allocation type " + mType.mElement.mType);
397 }
398
399 private void validateIsFloat32() {
400 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
401 return;
402 }
403 throw new RSIllegalArgumentException(
404 "32 bit float source does not match allocation type " + mType.mElement.mType);
405 }
406
Jason Sams3042d262013-11-25 18:28:33 -0800407 private void validateIsFloat64() {
408 if (mType.mElement.mType == Element.DataType.FLOAT_64) {
409 return;
410 }
411 throw new RSIllegalArgumentException(
412 "64 bit float source does not match allocation type " + mType.mElement.mType);
413 }
414
Jason Samsb97b2512011-01-16 15:04:08 -0800415 private void validateIsObject() {
416 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
417 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
418 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
419 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
420 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
421 (mType.mElement.mType == Element.DataType.RS_MESH) ||
422 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
423 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
424 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
425 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
426 return;
427 }
428 throw new RSIllegalArgumentException(
429 "Object source does not match allocation type " + mType.mElement.mType);
430 }
431
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700432 @Override
433 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800434 super.updateFromNative();
Tim Murray460a0492013-11-19 12:45:54 -0800435 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700436 if(typeID != 0) {
437 mType = new Type(typeID, mRS);
438 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700439 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700440 }
441 }
442
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700443 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700444 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700445 *
446 * @return Type
447 *
448 */
Jason Samsea87e962010-01-12 12:12:28 -0800449 public Type getType() {
450 return mType;
451 }
452
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700453 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700454 * Propagate changes from one usage of the Allocation to the
455 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700456 *
457 */
Jason Sams5476b452010-12-08 16:14:36 -0800458 public void syncAll(int srcLocation) {
Chris Craik06d29842015-06-02 17:19:24 -0700459 try {
460 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
461 switch (srcLocation) {
462 case USAGE_GRAPHICS_TEXTURE:
463 case USAGE_SCRIPT:
464 if ((mUsage & USAGE_SHARED) != 0) {
465 copyFrom(mBitmap);
466 }
467 break;
468 case USAGE_GRAPHICS_CONSTANTS:
469 case USAGE_GRAPHICS_VERTEX:
470 break;
471 case USAGE_SHARED:
472 if ((mUsage & USAGE_SHARED) != 0) {
473 copyTo(mBitmap);
474 }
475 break;
476 default:
477 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
Tim Murray78e64942013-04-09 17:28:56 -0700478 }
Chris Craik06d29842015-06-02 17:19:24 -0700479 mRS.validate();
480 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
481 } finally {
482 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800483 }
Jason Sams5476b452010-12-08 16:14:36 -0800484 }
485
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700486 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700487 * Send a buffer to the output stream. The contents of the Allocation will
488 * be undefined after this operation. This operation is only valid if {@link
489 * #USAGE_IO_OUTPUT} is set on the Allocation.
490 *
Jason Sams163766c2012-02-15 12:04:24 -0800491 *
Jason Sams163766c2012-02-15 12:04:24 -0800492 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700493 public void ioSend() {
Chris Craik06d29842015-06-02 17:19:24 -0700494 try {
495 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
496 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
497 throw new RSIllegalArgumentException(
498 "Can only send buffer if IO_OUTPUT usage specified.");
499 }
500 mRS.validate();
501 mRS.nAllocationIoSend(getID(mRS));
502 } finally {
503 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800504 }
Jason Sams163766c2012-02-15 12:04:24 -0800505 }
506
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700507 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700508 * Receive the latest input into the Allocation. This operation
509 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800510 *
Jason Sams163766c2012-02-15 12:04:24 -0800511 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700512 public void ioReceive() {
Chris Craik06d29842015-06-02 17:19:24 -0700513 try {
514 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
515 if ((mUsage & USAGE_IO_INPUT) == 0) {
516 throw new RSIllegalArgumentException(
517 "Can only receive if IO_INPUT usage specified.");
518 }
519 mRS.validate();
520 mRS.nAllocationIoReceive(getID(mRS));
521 } finally {
522 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800523 }
Jason Sams163766c2012-02-15 12:04:24 -0800524 }
525
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700526 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700527 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700528 *
529 * @param d Source array.
530 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800531 public void copyFrom(BaseObj[] d) {
Chris Craik06d29842015-06-02 17:19:24 -0700532 try {
533 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
534 mRS.validate();
535 validateIsObject();
536 if (d.length != mCurrentCount) {
537 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
538 mCurrentCount + ", array length = " + d.length);
539 }
Tim Murray460a0492013-11-19 12:45:54 -0800540
Chris Craik06d29842015-06-02 17:19:24 -0700541 if (RenderScript.sPointerSize == 8) {
542 long i[] = new long[d.length * 4];
543 for (int ct=0; ct < d.length; ct++) {
544 i[ct * 4] = d[ct].getID(mRS);
545 }
546 copy1DRangeFromUnchecked(0, mCurrentCount, i);
547 } else {
548 int i[] = new int[d.length];
549 for (int ct=0; ct < d.length; ct++) {
550 i[ct] = (int) d[ct].getID(mRS);
551 }
552 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray3de3dc72014-07-01 16:56:18 -0700553 }
Chris Craik06d29842015-06-02 17:19:24 -0700554 } finally {
555 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800556 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700557 }
558
Jason Samsfb9f82c2011-01-12 14:53:25 -0800559 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800560 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800561 if (bc == null) {
562 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
563 }
Jason Sams252c0782011-01-11 17:42:52 -0800564 switch (bc) {
565 case ALPHA_8:
566 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
567 throw new RSIllegalArgumentException("Allocation kind is " +
568 mType.getElement().mKind + ", type " +
569 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700570 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800571 " bytes, passed bitmap was " + bc);
572 }
573 break;
574 case ARGB_8888:
575 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700576 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800577 throw new RSIllegalArgumentException("Allocation kind is " +
578 mType.getElement().mKind + ", type " +
579 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700580 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800581 " bytes, passed bitmap was " + bc);
582 }
583 break;
584 case RGB_565:
585 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700586 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800587 throw new RSIllegalArgumentException("Allocation kind is " +
588 mType.getElement().mKind + ", type " +
589 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700590 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800591 " bytes, passed bitmap was " + bc);
592 }
593 break;
594 case ARGB_4444:
595 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700596 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800597 throw new RSIllegalArgumentException("Allocation kind is " +
598 mType.getElement().mKind + ", type " +
599 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700600 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800601 " bytes, passed bitmap was " + bc);
602 }
603 break;
604
605 }
Jason Sams4ef66502010-12-10 16:03:15 -0800606 }
607
Jason Samsfb9f82c2011-01-12 14:53:25 -0800608 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700609 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800610 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
611 }
612 }
613
Jason Sams3042d262013-11-25 18:28:33 -0800614 private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -0700615 try {
616 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
617 mRS.validate();
618 if (mCurrentDimZ > 0) {
619 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
620 } else if (mCurrentDimY > 0) {
621 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
622 } else {
623 copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
624 }
625 } finally {
626 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams3042d262013-11-25 18:28:33 -0800627 }
Jason Sams3042d262013-11-25 18:28:33 -0800628 }
629
630 /**
631 * Copy into this Allocation from an array. This method does not guarantee
632 * that the Allocation is compatible with the input buffer; it copies memory
633 * without reinterpretation.
634 *
635 * @param array The source data array
636 */
637 public void copyFromUnchecked(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700638 try {
639 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
640 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
641 java.lang.reflect.Array.getLength(array));
642 } finally {
643 Trace.traceEnd(RenderScript.TRACE_TAG);
644 }
Jason Sams3042d262013-11-25 18:28:33 -0800645 }
646
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700647 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700648 * Copy into this Allocation from an array. This method does not guarantee
649 * that the Allocation is compatible with the input buffer; it copies memory
650 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800651 *
652 * @param d the source data array
653 */
654 public void copyFromUnchecked(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800655 copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800656 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700657
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700658 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700659 * Copy into this Allocation from an array. This method does not guarantee
660 * that the Allocation is compatible with the input buffer; it copies memory
661 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800662 *
663 * @param d the source data array
664 */
665 public void copyFromUnchecked(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800666 copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800667 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700668
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700669 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700670 * Copy into this Allocation from an array. This method does not guarantee
671 * that the Allocation is compatible with the input buffer; it copies memory
672 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800673 *
674 * @param d the source data array
675 */
676 public void copyFromUnchecked(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800677 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800678 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700679
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700680 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700681 * Copy into this Allocation from an array. This method does not guarantee
682 * that the Allocation is compatible with the input buffer; it copies memory
683 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800684 *
685 * @param d the source data array
686 */
687 public void copyFromUnchecked(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800688 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800689 }
690
Tim Murray6d7a53c2013-05-23 16:59:23 -0700691
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700692 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700693 * Copy into this Allocation from an array. This variant is type checked
694 * and will generate exceptions if the Allocation's {@link
Jason Sams3042d262013-11-25 18:28:33 -0800695 * android.renderscript.Element} does not match the array's
696 * primitive type.
697 *
Ying Wang16229812013-11-26 15:45:12 -0800698 * @param array The source data array
Jason Sams3042d262013-11-25 18:28:33 -0800699 */
700 public void copyFrom(Object array) {
Chris Craik06d29842015-06-02 17:19:24 -0700701 try {
702 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
703 copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
704 java.lang.reflect.Array.getLength(array));
705 } finally {
706 Trace.traceEnd(RenderScript.TRACE_TAG);
707 }
Jason Sams3042d262013-11-25 18:28:33 -0800708 }
709
710 /**
711 * Copy into this Allocation from an array. This variant is type checked
712 * and will generate exceptions if the Allocation's {@link
Tim Murrayc11e25c2013-04-09 11:01:01 -0700713 * android.renderscript.Element} is not a 32 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(int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800718 validateIsInt32();
719 copyFromUnchecked(d, Element.DataType.SIGNED_32, 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 16 bit integer 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(short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800730 validateIsInt16();
731 copyFromUnchecked(d, Element.DataType.SIGNED_16, 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 this Allocation from an array. This variant is type checked
736 * and will generate exceptions if the Allocation's {@link
737 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800738 *
739 * @param d the source data array
740 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800741 public void copyFrom(byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800742 validateIsInt8();
743 copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800744 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800745
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700746 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700747 * Copy into this Allocation from an array. This variant is type checked
748 * and will generate exceptions if the Allocation's {@link
749 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800750 *
751 * @param d the source data array
752 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800753 public void copyFrom(float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800754 validateIsFloat32();
755 copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800756 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800757
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700758 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700759 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
760 * height, width, and format of the bitmap must match the existing
761 * allocation.
762 *
763 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
764 * android.graphics.Bitmap} used to create the Allocation with {@link
765 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
766 * this will synchronize the Allocation with the latest data from the {@link
767 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800768 *
769 * @param b the source bitmap
770 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800771 public void copyFrom(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -0700772 try {
773 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
774 mRS.validate();
775 if (b.getConfig() == null) {
776 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
777 Canvas c = new Canvas(newBitmap);
778 c.drawBitmap(b, 0, 0, null);
779 copyFrom(newBitmap);
780 return;
781 }
782 validateBitmapSize(b);
783 validateBitmapFormat(b);
784 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
785 } finally {
786 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -0800787 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700788 }
789
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700790 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700791 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700792 * must be identical.
793 *
794 * @param a the source allocation
795 */
796 public void copyFrom(Allocation a) {
Chris Craik06d29842015-06-02 17:19:24 -0700797 try {
798 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
799 mRS.validate();
800 if (!mType.equals(a.getType())) {
801 throw new RSIllegalArgumentException("Types of allocations must match.");
802 }
803 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
804 } finally {
805 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700806 }
Tim Murrayf671fb02012-10-03 13:50:05 -0700807 }
808
Tim Murrayf671fb02012-10-03 13:50:05 -0700809 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700810 * This is only intended to be used by auto-generated code reflected from
811 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800812 *
813 * @param xoff
814 * @param fp
815 */
Jason Sams21b41032011-01-16 15:05:41 -0800816 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800817 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700818 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700819 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700820 int data_length = fp.getPos();
Jason Samsa70f4162010-03-26 15:33:42 -0700821
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700822 int count = data_length / eSize;
823 if ((eSize * count) != data_length) {
824 throw new RSIllegalArgumentException("Field packer length " + data_length +
Jason Samsa70f4162010-03-26 15:33:42 -0700825 " not divisible by element size " + eSize + ".");
826 }
Jason Samsba862d12011-07-07 15:24:42 -0700827 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700828 }
829
Miao Wang45cec0a2015-03-04 16:40:21 -0800830
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700831 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700832 * This is only intended to be used by auto-generated code reflected from
833 * the RenderScript script files.
Jason Samsfa445b92011-01-07 17:00:07 -0800834 *
835 * @param xoff
836 * @param component_number
837 * @param fp
838 */
Jason Sams21b41032011-01-16 15:05:41 -0800839 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800840 setFromFieldPacker(xoff, 0, 0, component_number, fp);
841 }
842
843 /**
844 * @hide
845 * This is only intended to be used by auto-generated code reflected from
846 * the RenderScript script files.
847 *
848 * @param xoff
849 * @param yoff
Miao Wangc8e237e2015-02-20 18:36:32 -0800850 * @param zoff
851 * @param component_number
852 * @param fp
853 */
854 public void setFromFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800855 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700856 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800857 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700858 }
859 if(xoff < 0) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800860 throw new RSIllegalArgumentException("Offset x must be >= 0.");
861 }
862 if(yoff < 0) {
863 throw new RSIllegalArgumentException("Offset y must be >= 0.");
864 }
865 if(zoff < 0) {
866 throw new RSIllegalArgumentException("Offset z must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700867 }
868
869 final byte[] data = fp.getData();
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700870 int data_length = fp.getPos();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700871 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800872 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700873
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700874 if (data_length != eSize) {
875 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700876 " does not match component size " + eSize + ".");
877 }
878
Miao Wangc8e237e2015-02-20 18:36:32 -0800879 mRS.nAllocationElementData(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
880 component_number, data, data_length);
Jason Samsa70f4162010-03-26 15:33:42 -0700881 }
882
Miao Wang87e908d2015-03-02 15:15:15 -0800883 private void data1DChecks(int off, int count, int len, int dataSize, boolean usePadding) {
Jason Sams771bebb2009-12-07 12:40:12 -0800884 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700885 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800886 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700887 }
888 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800889 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700890 }
Jason Samsba862d12011-07-07 15:24:42 -0700891 if((off + count) > mCurrentCount) {
892 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700893 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700894 }
Miao Wang87e908d2015-03-02 15:15:15 -0800895 if(usePadding) {
896 if(len < dataSize / 4 * 3) {
897 throw new RSIllegalArgumentException("Array too small for allocation type.");
898 }
899 } else {
900 if(len < dataSize) {
901 throw new RSIllegalArgumentException("Array too small for allocation type.");
902 }
Jason Sams768bc022009-09-21 19:41:04 -0700903 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700904 }
905
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700906 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700907 * Generate a mipmap chain. This is only valid if the Type of the Allocation
908 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800909 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700910 * <p>This function will generate a complete set of mipmaps from the top
911 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800912 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700913 * <p>If the Allocation is also using other memory spaces, a call to {@link
914 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800915 */
916 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700917 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800918 }
919
Jason Sams3042d262013-11-25 18:28:33 -0800920 private void copy1DRangeFromUnchecked(int off, int count, Object array,
921 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -0700922 try {
923 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
924 final int dataSize = mType.mElement.getBytesSize() * count;
925 // AutoPadding for Vec3 Element
926 boolean usePadding = false;
927 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
928 usePadding = true;
929 }
930 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
931 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
932 mType.mElement.mType.mSize, usePadding);
933 } finally {
934 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -0800935 }
Jason Sams3042d262013-11-25 18:28:33 -0800936 }
937
938 /**
939 * Copy an array into part of this Allocation. This method does not
940 * guarantee that the Allocation is compatible with the input buffer.
941 *
942 * @param off The offset of the first element to be copied.
943 * @param count The number of elements to be copied.
944 * @param array The source data array
945 */
946 public void copy1DRangeFromUnchecked(int off, int count, Object array) {
947 copy1DRangeFromUnchecked(off, count, array,
948 validateObjectIsPrimitiveArray(array, false),
949 java.lang.reflect.Array.getLength(array));
950 }
951
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700952 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700953 * Copy an array into part of this Allocation. This method does not
954 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800955 *
956 * @param off The offset of the first element to be copied.
957 * @param count The number of elements to be copied.
958 * @param d the source data array
959 */
960 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800961 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700962 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700963
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700964 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700965 * Copy an array into part of this Allocation. This method does not
966 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800967 *
968 * @param off The offset of the first element to be copied.
969 * @param count The number of elements to be copied.
970 * @param d the source data array
971 */
972 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800973 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700974 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700975
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700976 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700977 * Copy an array into part of this Allocation. This method does not
978 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800979 *
980 * @param off The offset of the first element to be copied.
981 * @param count The number of elements to be copied.
982 * @param d the source data array
983 */
984 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800985 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
Jason Sams768bc022009-09-21 19:41:04 -0700986 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700987
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700988 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700989 * Copy an array into part of this Allocation. This method does not
990 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800991 *
992 * @param off The offset of the first element to be copied.
993 * @param count The number of elements to be copied.
994 * @param d the source data array
995 */
996 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams3042d262013-11-25 18:28:33 -0800997 copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
998 }
999
1000
1001 /**
1002 * Copy an array into part of this Allocation. This variant is type checked
1003 * and will generate exceptions if the Allocation type does not
1004 * match the component type of the array passed in.
1005 *
1006 * @param off The offset of the first element to be copied.
1007 * @param count The number of elements to be copied.
1008 * @param array The source data array.
1009 */
1010 public void copy1DRangeFrom(int off, int count, Object array) {
1011 copy1DRangeFromUnchecked(off, count, array,
1012 validateObjectIsPrimitiveArray(array, true),
1013 java.lang.reflect.Array.getLength(array));
Jason Samsb8c5a842009-07-31 20:40:47 -07001014 }
1015
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001016 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001017 * Copy an array into part of this Allocation. This variant is type checked
1018 * and will generate exceptions if the Allocation type is not a 32 bit
1019 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001020 *
1021 * @param off The offset of the first element to be copied.
1022 * @param count The number of elements to be copied.
1023 * @param d the source data array
1024 */
Jason Samsb97b2512011-01-16 15:04:08 -08001025 public void copy1DRangeFrom(int off, int count, int[] d) {
1026 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001027 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001028 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001029
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001030 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001031 * Copy an array into part of this Allocation. This variant is type checked
1032 * and will generate exceptions if the Allocation type is not a 16 bit
1033 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001034 *
1035 * @param off The offset of the first element to be copied.
1036 * @param count The number of elements to be copied.
1037 * @param d the source data array
1038 */
Jason Samsb97b2512011-01-16 15:04:08 -08001039 public void copy1DRangeFrom(int off, int count, short[] d) {
1040 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001041 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001042 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001043
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001044 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001045 * Copy an array into part of this Allocation. This variant is type checked
1046 * and will generate exceptions if the Allocation type is not an 8 bit
1047 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001048 *
1049 * @param off The offset of the first element to be copied.
1050 * @param count The number of elements to be copied.
1051 * @param d the source data array
1052 */
Jason Samsb97b2512011-01-16 15:04:08 -08001053 public void copy1DRangeFrom(int off, int count, byte[] d) {
1054 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001055 copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001056 }
Jason Sams4fa3eed2011-01-19 15:44:38 -08001057
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001058 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001059 * Copy an array into part of this Allocation. This variant is type checked
1060 * and will generate exceptions if the Allocation type is not a 32 bit float
1061 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001062 *
1063 * @param off The offset of the first element to be copied.
1064 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001065 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -08001066 */
Jason Samsb97b2512011-01-16 15:04:08 -08001067 public void copy1DRangeFrom(int off, int count, float[] d) {
1068 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001069 copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
Jason Samsb97b2512011-01-16 15:04:08 -08001070 }
Jason Sams3042d262013-11-25 18:28:33 -08001071
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001072 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001073 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001074 *
1075 * @param off The offset of the first element to be copied.
1076 * @param count The number of elements to be copied.
1077 * @param data the source data allocation.
1078 * @param dataOff off The offset of the first element in data to
1079 * be copied.
1080 */
1081 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001082 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -07001083 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001084 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001085 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -07001086 data.mSelectedLOD, data.mSelectedFace.mID);
Chris Craikf77facd2015-06-01 10:39:36 -07001087 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001088 }
1089
Jason Samsfb9f82c2011-01-12 14:53:25 -08001090 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -07001091 if (mAdaptedAllocation != null) {
1092
1093 } else {
1094
1095 if (xoff < 0 || yoff < 0) {
1096 throw new RSIllegalArgumentException("Offset cannot be negative.");
1097 }
1098 if (h < 0 || w < 0) {
1099 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1100 }
1101 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1102 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1103 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001104 }
1105 }
Jason Sams768bc022009-09-21 19:41:04 -07001106
Jason Sams3042d262013-11-25 18:28:33 -08001107 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
1108 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001109 try {
1110 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
1111 mRS.validate();
1112 validate2DRange(xoff, yoff, w, h);
1113 final int dataSize = mType.mElement.getBytesSize() * w * h;
1114 // AutoPadding for Vec3 Element
1115 boolean usePadding = false;
1116 int sizeBytes = arrayLen * dt.mSize;
1117 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1118 if (dataSize / 4 * 3 > sizeBytes) {
1119 throw new RSIllegalArgumentException("Array too small for allocation type.");
1120 }
1121 usePadding = true;
1122 sizeBytes = dataSize;
1123 } else {
1124 if (dataSize > sizeBytes) {
1125 throw new RSIllegalArgumentException("Array too small for allocation type.");
1126 }
Miao Wang87e908d2015-03-02 15:15:15 -08001127 }
Chris Craik06d29842015-06-02 17:19:24 -07001128 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1129 array, sizeBytes, dt,
1130 mType.mElement.mType.mSize, usePadding);
1131 } finally {
1132 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001133 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001134 }
1135
Jason Sams3042d262013-11-25 18:28:33 -08001136 /**
1137 * Copy from an array into a rectangular region in this Allocation. The
1138 * array is assumed to be tightly packed.
1139 *
1140 * @param xoff X offset of the region to update in this Allocation
1141 * @param yoff Y offset of the region to update in this Allocation
1142 * @param w Width of the region to update
1143 * @param h Height of the region to update
Ying Wang16229812013-11-26 15:45:12 -08001144 * @param array Data to be placed into the Allocation
Jason Sams3042d262013-11-25 18:28:33 -08001145 */
1146 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001147 try {
1148 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1149 copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
1150 validateObjectIsPrimitiveArray(array, true),
1151 java.lang.reflect.Array.getLength(array));
1152 } finally {
1153 Trace.traceEnd(RenderScript.TRACE_TAG);
1154 }
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001155 }
1156
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001157 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001158 * Copy from an array into a rectangular region in this Allocation. The
1159 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -08001160 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001161 * @param xoff X offset of the region to update in this Allocation
1162 * @param yoff Y offset of the region to update in this Allocation
1163 * @param w Width of the region to update
1164 * @param h Height of the region to update
1165 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001166 */
1167 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001168 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001169 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1170 Element.DataType.SIGNED_8, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001171 }
1172
Tim Murrayc11e25c2013-04-09 11:01:01 -07001173 /**
1174 * Copy from an array into a rectangular region in this Allocation. The
1175 * array is assumed to be tightly packed.
1176 *
1177 * @param xoff X offset of the region to update in this Allocation
1178 * @param yoff Y offset of the region to update in this Allocation
1179 * @param w Width of the region to update
1180 * @param h Height of the region to update
1181 * @param data to be placed into the Allocation
1182 */
Jason Samsf7086092011-01-12 13:28:37 -08001183 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001184 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001185 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1186 Element.DataType.SIGNED_16, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001187 }
1188
Tim Murrayc11e25c2013-04-09 11:01:01 -07001189 /**
1190 * Copy from an array into a rectangular region in this Allocation. The
1191 * array is assumed to be tightly packed.
1192 *
1193 * @param xoff X offset of the region to update in this Allocation
1194 * @param yoff Y offset of the region to update in this Allocation
1195 * @param w Width of the region to update
1196 * @param h Height of the region to update
1197 * @param data to be placed into the Allocation
1198 */
Jason Samsf7086092011-01-12 13:28:37 -08001199 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001200 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001201 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1202 Element.DataType.SIGNED_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001203 }
1204
Tim Murrayc11e25c2013-04-09 11:01:01 -07001205 /**
1206 * Copy from an array into a rectangular region in this Allocation. The
1207 * array is assumed to be tightly packed.
1208 *
1209 * @param xoff X offset of the region to update in this Allocation
1210 * @param yoff Y offset of the region to update in this Allocation
1211 * @param w Width of the region to update
1212 * @param h Height of the region to update
1213 * @param data to be placed into the Allocation
1214 */
Jason Samsf7086092011-01-12 13:28:37 -08001215 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -08001216 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001217 copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
1218 Element.DataType.FLOAT_32, data.length);
Jason Samsb8c5a842009-07-31 20:40:47 -07001219 }
1220
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001221 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001222 * Copy a rectangular region from an Allocation into a rectangular region in
1223 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001224 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001225 * @param xoff X offset of the region in this Allocation
1226 * @param yoff Y offset of the region in this Allocation
1227 * @param w Width of the region to update.
1228 * @param h Height of the region to update.
1229 * @param data source Allocation.
1230 * @param dataXoff X offset in source Allocation
1231 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001232 */
1233 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1234 Allocation data, int dataXoff, int dataYoff) {
Chris Craik06d29842015-06-02 17:19:24 -07001235 try {
1236 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1237 mRS.validate();
1238 validate2DRange(xoff, yoff, w, h);
1239 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
1240 mSelectedLOD, mSelectedFace.mID,
1241 w, h, data.getID(mRS), dataXoff, dataYoff,
1242 data.mSelectedLOD, data.mSelectedFace.mID);
1243 } finally {
1244 Trace.traceEnd(RenderScript.TRACE_TAG);
1245 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001246 }
1247
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001248 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001249 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1250 * and width of the update will use the height and width of the {@link
1251 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001252 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001253 * @param xoff X offset of the region to update in this Allocation
1254 * @param yoff Y offset of the region to update in this Allocation
1255 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001256 */
1257 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Chris Craikf77facd2015-06-01 10:39:36 -07001258 try {
1259 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1260 mRS.validate();
1261 if (data.getConfig() == null) {
1262 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1263 Canvas c = new Canvas(newBitmap);
1264 c.drawBitmap(data, 0, 0, null);
1265 copy2DRangeFrom(xoff, yoff, newBitmap);
1266 return;
1267 }
1268 validateBitmapFormat(data);
1269 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
1270 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
1271 } finally {
1272 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayabd5db92013-02-28 11:45:22 -08001273 }
Jason Samsfa445b92011-01-07 17:00:07 -08001274 }
1275
Jason Samsb05d6892013-04-09 15:59:24 -07001276 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1277 if (mAdaptedAllocation != null) {
1278
1279 } else {
1280
1281 if (xoff < 0 || yoff < 0 || zoff < 0) {
1282 throw new RSIllegalArgumentException("Offset cannot be negative.");
1283 }
1284 if (h < 0 || w < 0 || d < 0) {
1285 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1286 }
1287 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1288 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1289 }
1290 }
1291 }
1292
1293 /**
1294 * @hide
1295 *
1296 */
Jason Sams3042d262013-11-25 18:28:33 -08001297 private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1298 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001299 try {
1300 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
1301 mRS.validate();
1302 validate3DRange(xoff, yoff, zoff, w, h, d);
1303 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1304 // AutoPadding for Vec3 Element
1305 boolean usePadding = false;
1306 int sizeBytes = arrayLen * dt.mSize;
1307 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1308 if (dataSize / 4 * 3 > sizeBytes) {
1309 throw new RSIllegalArgumentException("Array too small for allocation type.");
1310 }
1311 usePadding = true;
1312 sizeBytes = dataSize;
1313 } else {
1314 if (dataSize > sizeBytes) {
1315 throw new RSIllegalArgumentException("Array too small for allocation type.");
1316 }
Miao Wang87e908d2015-03-02 15:15:15 -08001317 }
Chris Craik06d29842015-06-02 17:19:24 -07001318 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1319 array, sizeBytes, dt,
1320 mType.mElement.mType.mSize, usePadding);
1321 } finally {
1322 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001323 }
Jason Samsb05d6892013-04-09 15:59:24 -07001324 }
1325
1326 /**
1327 * @hide
Jason Samsb05d6892013-04-09 15:59:24 -07001328 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001329 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001330 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001331 * @param xoff X offset of the region to update in this Allocation
1332 * @param yoff Y offset of the region to update in this Allocation
1333 * @param zoff Z offset of the region to update in this Allocation
1334 * @param w Width of the region to update
1335 * @param h Height of the region to update
1336 * @param d Depth of the region to update
Miao Wang87e908d2015-03-02 15:15:15 -08001337 * @param array to be placed into the allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001338 */
Jason Sams3042d262013-11-25 18:28:33 -08001339 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
Chris Craik06d29842015-06-02 17:19:24 -07001340 try {
1341 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
1342 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
1343 validateObjectIsPrimitiveArray(array, true),
1344 java.lang.reflect.Array.getLength(array));
1345 } finally {
1346 Trace.traceEnd(RenderScript.TRACE_TAG);
1347 }
Jason Samsb05d6892013-04-09 15:59:24 -07001348 }
1349
1350 /**
1351 * @hide
1352 * Copy a rectangular region into the allocation from another
1353 * allocation.
1354 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001355 * @param xoff X offset of the region to update in this Allocation
1356 * @param yoff Y offset of the region to update in this Allocation
1357 * @param zoff Z offset of the region to update in this Allocation
1358 * @param w Width of the region to update.
1359 * @param h Height of the region to update.
1360 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001361 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001362 * @param dataXoff X offset of the region in the source Allocation
1363 * @param dataYoff Y offset of the region in the source Allocation
1364 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001365 */
1366 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1367 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1368 mRS.validate();
1369 validate3DRange(xoff, yoff, zoff, w, h, d);
1370 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1371 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1372 data.mSelectedLOD);
1373 }
1374
Jason Samsfa445b92011-01-07 17:00:07 -08001375
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001376 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001377 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1378 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001379 *
1380 * @param b The bitmap to be set from the Allocation.
1381 */
Jason Samsfa445b92011-01-07 17:00:07 -08001382 public void copyTo(Bitmap b) {
Chris Craik06d29842015-06-02 17:19:24 -07001383 try {
1384 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1385 mRS.validate();
1386 validateBitmapFormat(b);
1387 validateBitmapSize(b);
1388 mRS.nAllocationCopyToBitmap(getID(mRS), b);
1389 } finally {
1390 Trace.traceEnd(RenderScript.TRACE_TAG);
1391 }
Jason Samsfa445b92011-01-07 17:00:07 -08001392 }
1393
Jason Sams3042d262013-11-25 18:28:33 -08001394 private void copyTo(Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001395 try {
1396 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1397 mRS.validate();
1398 boolean usePadding = false;
1399 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1400 usePadding = true;
Miao Wangd9b63282015-04-03 09:15:39 -07001401 }
Chris Craik06d29842015-06-02 17:19:24 -07001402 if (usePadding) {
1403 if (dt.mSize * arrayLen < mSize / 4 * 3) {
1404 throw new RSIllegalArgumentException(
1405 "Size of output array cannot be smaller than size of allocation.");
1406 }
1407 } else {
1408 if (dt.mSize * arrayLen < mSize) {
1409 throw new RSIllegalArgumentException(
1410 "Size of output array cannot be smaller than size of allocation.");
1411 }
Miao Wangd9b63282015-04-03 09:15:39 -07001412 }
Chris Craik06d29842015-06-02 17:19:24 -07001413 mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
1414 } finally {
1415 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wangd9b63282015-04-03 09:15:39 -07001416 }
Jason Sams3042d262013-11-25 18:28:33 -08001417 }
1418
1419 /**
1420 * Copy from the Allocation into an array. The array must be at
1421 * least as large as the Allocation. The
1422 * {@link android.renderscript.Element} must match the component
1423 * type of the array passed in.
1424 *
1425 * @param array The array to be set from the Allocation.
1426 */
1427 public void copyTo(Object array) {
1428 copyTo(array, validateObjectIsPrimitiveArray(array, true),
1429 java.lang.reflect.Array.getLength(array));
1430 }
1431
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001432 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001433 * Copy from the Allocation into a byte array. The array must be at least
1434 * as large as the Allocation. The allocation must be of an 8 bit integer
1435 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001436 *
1437 * @param d The array to be set from the Allocation.
1438 */
Jason Samsfa445b92011-01-07 17:00:07 -08001439 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001440 validateIsInt8();
Jason Sams3042d262013-11-25 18:28:33 -08001441 copyTo(d, Element.DataType.SIGNED_8, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001442 }
1443
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001444 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001445 * Copy from the Allocation into a short array. The array must be at least
1446 * as large as the Allocation. The allocation must be of an 16 bit integer
1447 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001448 *
1449 * @param d The array to be set from the Allocation.
1450 */
Jason Samsfa445b92011-01-07 17:00:07 -08001451 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001452 validateIsInt16();
Jason Sams3042d262013-11-25 18:28:33 -08001453 copyTo(d, Element.DataType.SIGNED_16, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001454 }
1455
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001456 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001457 * Copy from the Allocation into a int array. The array must be at least as
1458 * large as the Allocation. The allocation must be of an 32 bit integer
1459 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001460 *
1461 * @param d The array to be set from the Allocation.
1462 */
Jason Samsfa445b92011-01-07 17:00:07 -08001463 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001464 validateIsInt32();
Jason Sams3042d262013-11-25 18:28:33 -08001465 copyTo(d, Element.DataType.SIGNED_32, d.length);
Jason Samsfa445b92011-01-07 17:00:07 -08001466 }
1467
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001468 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001469 * Copy from the Allocation into a float array. The array must be at least
1470 * as large as the Allocation. The allocation must be of an 32 bit float
1471 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001472 *
1473 * @param d The array to be set from the Allocation.
1474 */
Jason Samsfa445b92011-01-07 17:00:07 -08001475 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001476 validateIsFloat32();
Jason Sams3042d262013-11-25 18:28:33 -08001477 copyTo(d, Element.DataType.FLOAT_32, d.length);
Jason Sams40a29e82009-08-10 14:55:26 -07001478 }
1479
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001480 /**
Miao Wangc8e237e2015-02-20 18:36:32 -08001481 * @hide
Miao Wang45cec0a2015-03-04 16:40:21 -08001482 * This is only intended to be used by auto-generated code reflected from
1483 * the RenderScript script files and should not be used by developers.
Miao Wangc8e237e2015-02-20 18:36:32 -08001484 *
1485 * @param xoff
1486 * @param yoff
1487 * @param zoff
1488 * @param component_number
1489 * @param array
1490 */
Miao Wang45cec0a2015-03-04 16:40:21 -08001491 public void copyToFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001492 mRS.validate();
1493 if (component_number >= mType.mElement.mElements.length) {
1494 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
1495 }
1496 if(xoff < 0) {
1497 throw new RSIllegalArgumentException("Offset x must be >= 0.");
1498 }
1499 if(yoff < 0) {
1500 throw new RSIllegalArgumentException("Offset y must be >= 0.");
1501 }
1502 if(zoff < 0) {
1503 throw new RSIllegalArgumentException("Offset z must be >= 0.");
1504 }
1505
Miao Wang45cec0a2015-03-04 16:40:21 -08001506 final byte[] data = fp.getData();
Miao Wangbfa5e652015-05-04 15:29:25 -07001507 int data_length = data.length;
Miao Wangc8e237e2015-02-20 18:36:32 -08001508 int eSize = mType.mElement.mElements[component_number].getBytesSize();
1509 eSize *= mType.mElement.mArraySizes[component_number];
1510
Miao Wang45cec0a2015-03-04 16:40:21 -08001511 if (data_length != eSize) {
1512 throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
1513 " does not match component size " + eSize + ".");
Miao Wangc8e237e2015-02-20 18:36:32 -08001514 }
1515
1516 mRS.nAllocationElementRead(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Miao Wang45cec0a2015-03-04 16:40:21 -08001517 component_number, data, data_length);
Miao Wangc8e237e2015-02-20 18:36:32 -08001518 }
1519 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001520 * Resize a 1D allocation. The contents of the allocation are preserved.
1521 * If new elements are allocated objects are created with null contents and
1522 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001523 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001524 * <p>If the new region is smaller the references of any objects outside the
1525 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001526 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001527 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001528 *
1529 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001530 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001531 * @deprecated RenderScript objects should be immutable once created. The
Tim Murraycd38b762014-08-13 13:20:25 -07001532 * replacement is to create a new allocation and copy the contents. This
1533 * function will throw an exception if API 21 or higher is used.
Jason Samsf7086092011-01-12 13:28:37 -08001534 */
Jason Sams31a7e422010-10-26 13:09:17 -07001535 public synchronized void resize(int dimX) {
Tim Murraycd38b762014-08-13 13:20:25 -07001536 if (mRS.getApplicationContext().getApplicationInfo().targetSdkVersion >= 21) {
1537 throw new RSRuntimeException("Resize is not allowed in API 21+.");
1538 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001539 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001540 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001541 }
Jason Samse07694b2012-04-03 15:36:36 -07001542 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001543 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001544
Tim Murray460a0492013-11-19 12:45:54 -08001545 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001546 mType = new Type(typeID, mRS);
1547 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001548 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001549 }
1550
Miao Wangc8e237e2015-02-20 18:36:32 -08001551 private void copy1DRangeToUnchecked(int off, int count, Object array,
1552 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001553 try {
1554 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked");
1555 final int dataSize = mType.mElement.getBytesSize() * count;
1556 // AutoPadding for Vec3 Element
1557 boolean usePadding = false;
1558 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1559 usePadding = true;
1560 }
1561 data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
1562 mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
1563 mType.mElement.mType.mSize, usePadding);
1564 } finally {
1565 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001566 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001567 }
1568
1569 /**
1570 * @hide
1571 * Copy part of this Allocation into an array. This method does not
1572 * guarantee that the Allocation is compatible with the input buffer.
1573 *
1574 * @param off The offset of the first element to be copied.
1575 * @param count The number of elements to be copied.
1576 * @param array The dest data array
1577 */
1578 public void copy1DRangeToUnchecked(int off, int count, Object array) {
1579 copy1DRangeToUnchecked(off, count, array,
1580 validateObjectIsPrimitiveArray(array, false),
1581 java.lang.reflect.Array.getLength(array));
1582 }
1583
1584 /**
1585 * @hide
1586 * Copy part of this Allocation into an array. This method does not
1587 * guarantee that the Allocation is compatible with the input buffer.
1588 *
1589 * @param off The offset of the first element to be copied.
1590 * @param count The number of elements to be copied.
1591 * @param d the source data array
1592 */
1593 public void copy1DRangeToUnchecked(int off, int count, int[] d) {
1594 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
1595 }
1596
1597 /**
1598 * @hide
1599 * Copy part of this Allocation into an array. This method does not
1600 * guarantee that the Allocation is compatible with the input buffer.
1601 *
1602 * @param off The offset of the first element to be copied.
1603 * @param count The number of elements to be copied.
1604 * @param d the source data array
1605 */
1606 public void copy1DRangeToUnchecked(int off, int count, short[] d) {
1607 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
1608 }
1609
1610 /**
1611 * @hide
1612 * Copy part of this Allocation into an array. This method does not
1613 * guarantee that the Allocation is compatible with the input buffer.
1614 *
1615 * @param off The offset of the first element to be copied.
1616 * @param count The number of elements to be copied.
1617 * @param d the source data array
1618 */
1619 public void copy1DRangeToUnchecked(int off, int count, byte[] d) {
1620 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
1621 }
1622
1623 /**
1624 * @hide
1625 * Copy part of this Allocation into an array. This method does not
1626 * guarantee that the Allocation is compatible with the input buffer.
1627 *
1628 * @param off The offset of the first element to be copied.
1629 * @param count The number of elements to be copied.
1630 * @param d the source data array
1631 */
1632 public void copy1DRangeToUnchecked(int off, int count, float[] d) {
1633 copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
1634 }
1635
1636
1637 /**
1638 * @hide
1639 * Copy part of this Allocation into an array. This method does not
1640 * and will generate exceptions if the Allocation type does not
1641 * match the component type of the array passed in.
1642 *
1643 * @param off The offset of the first element to be copied.
1644 * @param count The number of elements to be copied.
1645 * @param array The source data array.
1646 */
1647 public void copy1DRangeTo(int off, int count, Object array) {
1648 copy1DRangeToUnchecked(off, count, array,
1649 validateObjectIsPrimitiveArray(array, true),
1650 java.lang.reflect.Array.getLength(array));
1651 }
1652
1653 /**
1654 * @hide
1655 * Copy part of this Allocation into an array. This method does not
1656 * and will generate exceptions if the Allocation type is not a 32 bit
1657 * integer type.
1658 *
1659 * @param off The offset of the first element to be copied.
1660 * @param count The number of elements to be copied.
1661 * @param d the source data array
1662 */
1663 public void copy1DRangeTo(int off, int count, int[] d) {
1664 validateIsInt32();
1665 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
1666 }
1667
1668 /**
1669 * @hide
1670 * Copy part of this Allocation into an array. This method does not
1671 * and will generate exceptions if the Allocation type is not a 16 bit
1672 * integer type.
1673 *
1674 * @param off The offset of the first element to be copied.
1675 * @param count The number of elements to be copied.
1676 * @param d the source data array
1677 */
1678 public void copy1DRangeTo(int off, int count, short[] d) {
1679 validateIsInt16();
1680 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
1681 }
1682
1683 /**
1684 * @hide
1685 * Copy part of this Allocation into an array. This method does not
1686 * and will generate exceptions if the Allocation type is not an 8 bit
1687 * integer type.
1688 *
1689 * @param off The offset of the first element to be copied.
1690 * @param count The number of elements to be copied.
1691 * @param d the source data array
1692 */
1693 public void copy1DRangeTo(int off, int count, byte[] d) {
1694 validateIsInt8();
1695 copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
1696 }
1697
1698 /**
1699 * @hide
1700 * Copy part of this Allocation into an array. This method does not
1701 * and will generate exceptions if the Allocation type is not a 32 bit float
1702 * type.
1703 *
1704 * @param off The offset of the first element to be copied.
1705 * @param count The number of elements to be copied.
1706 * @param d the source data array.
1707 */
1708 public void copy1DRangeTo(int off, int count, float[] d) {
1709 validateIsFloat32();
1710 copy1DRangeToUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
1711 }
1712
1713
1714 void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
1715 Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001716 try {
1717 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked");
1718 mRS.validate();
1719 validate2DRange(xoff, yoff, w, h);
1720 final int dataSize = mType.mElement.getBytesSize() * w * h;
1721 // AutoPadding for Vec3 Element
1722 boolean usePadding = false;
1723 int sizeBytes = arrayLen * dt.mSize;
1724 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1725 if (dataSize / 4 * 3 > sizeBytes) {
1726 throw new RSIllegalArgumentException("Array too small for allocation type.");
1727 }
1728 usePadding = true;
1729 sizeBytes = dataSize;
1730 } else {
1731 if (dataSize > sizeBytes) {
1732 throw new RSIllegalArgumentException("Array too small for allocation type.");
1733 }
Miao Wang87e908d2015-03-02 15:15:15 -08001734 }
Chris Craik06d29842015-06-02 17:19:24 -07001735 mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
1736 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
1737 } finally {
1738 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001739 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001740 }
1741
1742 /**
1743 * @hide
1744 * Copy from a rectangular region in this Allocation into an array.
1745 *
1746 * @param xoff X offset of the region to copy in this Allocation
1747 * @param yoff Y offset of the region to copy in this Allocation
1748 * @param w Width of the region to copy
1749 * @param h Height of the region to copy
1750 * @param array Dest Array to be copied into
1751 */
1752 public void copy2DRangeTo(int xoff, int yoff, int w, int h, Object array) {
1753 copy2DRangeToUnchecked(xoff, yoff, w, h, array,
1754 validateObjectIsPrimitiveArray(array, true),
1755 java.lang.reflect.Array.getLength(array));
1756 }
1757
1758 /**
1759 * @hide
1760 * Copy from a rectangular region in this Allocation into an array.
1761 *
1762 * @param xoff X offset of the region to copy in this Allocation
1763 * @param yoff Y offset of the region to copy in this Allocation
1764 * @param w Width of the region to copy
1765 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001766 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001767 */
1768 public void copy2DRangeTo(int xoff, int yoff, int w, int h, byte[] data) {
1769 validateIsInt8();
1770 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1771 Element.DataType.SIGNED_8, data.length);
1772 }
1773
1774 /**
1775 * @hide
1776 * Copy from a rectangular region in this Allocation into an array.
1777 *
1778 * @param xoff X offset of the region to copy in this Allocation
1779 * @param yoff Y offset of the region to copy in this Allocation
1780 * @param w Width of the region to copy
1781 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001782 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001783 */
1784 public void copy2DRangeTo(int xoff, int yoff, int w, int h, short[] data) {
1785 validateIsInt16();
1786 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1787 Element.DataType.SIGNED_16, data.length);
1788 }
1789
1790 /**
1791 * @hide
1792 * Copy from a rectangular region in this Allocation into an array.
1793 *
1794 * @param xoff X offset of the region to copy in this Allocation
1795 * @param yoff Y offset of the region to copy in this Allocation
1796 * @param w Width of the region to copy
1797 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001798 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001799 */
1800 public void copy2DRangeTo(int xoff, int yoff, int w, int h, int[] data) {
1801 validateIsInt32();
1802 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1803 Element.DataType.SIGNED_32, data.length);
1804 }
1805
1806 /**
1807 * @hide
1808 * Copy from a rectangular region in this Allocation into an array.
1809 *
1810 * @param xoff X offset of the region to copy in this Allocation
1811 * @param yoff Y offset of the region to copy in this Allocation
1812 * @param w Width of the region to copy
1813 * @param h Height of the region to copy
Miao Wang87e908d2015-03-02 15:15:15 -08001814 * @param data Dest Array to be copied into
Miao Wangc8e237e2015-02-20 18:36:32 -08001815 */
1816 public void copy2DRangeTo(int xoff, int yoff, int w, int h, float[] data) {
1817 validateIsFloat32();
1818 copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1819 Element.DataType.FLOAT_32, data.length);
1820 }
1821
1822
1823 /**
1824 * @hide
1825 *
1826 */
1827 private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1828 Object array, Element.DataType dt, int arrayLen) {
Chris Craik06d29842015-06-02 17:19:24 -07001829 try {
1830 Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked");
1831 mRS.validate();
1832 validate3DRange(xoff, yoff, zoff, w, h, d);
1833 final int dataSize = mType.mElement.getBytesSize() * w * h * d;
1834 // AutoPadding for Vec3 Element
1835 boolean usePadding = false;
1836 int sizeBytes = arrayLen * dt.mSize;
1837 if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
1838 if (dataSize / 4 * 3 > sizeBytes) {
1839 throw new RSIllegalArgumentException("Array too small for allocation type.");
1840 }
1841 usePadding = true;
1842 sizeBytes = dataSize;
1843 } else {
1844 if (dataSize > sizeBytes) {
1845 throw new RSIllegalArgumentException("Array too small for allocation type.");
1846 }
Miao Wang87e908d2015-03-02 15:15:15 -08001847 }
Chris Craik06d29842015-06-02 17:19:24 -07001848 mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
1849 array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
1850 } finally {
1851 Trace.traceEnd(RenderScript.TRACE_TAG);
Miao Wang87e908d2015-03-02 15:15:15 -08001852 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001853 }
1854
1855 /**
1856 * @hide
1857 * Copy from a rectangular region in this Allocation into an array.
1858 *
1859 * @param xoff X offset of the region to copy in this Allocation
1860 * @param yoff Y offset of the region to copy in this Allocation
1861 * @param zoff Z offset of the region to copy in this Allocation
1862 * @param w Width of the region to copy
1863 * @param h Height of the region to copy
1864 * @param d Depth of the region to copy
1865 * @param array Dest Array to be copied into
1866 */
1867 public void copy3DRangeTo(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1868 copy3DRangeToUnchecked(xoff, yoff, zoff, w, h, d, array,
1869 validateObjectIsPrimitiveArray(array, true),
1870 java.lang.reflect.Array.getLength(array));
1871 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001872
1873 // creation
1874
Jason Sams49a05d72010-12-29 14:31:29 -08001875 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001876 static {
1877 mBitmapOptions.inScaled = false;
1878 }
1879
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001880 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001881 * Creates a new Allocation with the given {@link
1882 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001883 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001884 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001885 * @param mips specifies desired mipmap behaviour for the
1886 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001887 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001888 * utilized
1889 */
1890 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07001891 try {
1892 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
1893 rs.validate();
1894 if (type.getID(rs) == 0) {
1895 throw new RSInvalidStateException("Bad Type");
1896 }
1897 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
1898 if (id == 0) {
1899 throw new RSRuntimeException("Allocation creation failed.");
1900 }
1901 return new Allocation(id, rs, type, usage);
1902 } finally {
1903 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams1bada8c2009-08-09 17:01:55 -07001904 }
Jason Sams857d0c72011-11-23 15:02:15 -08001905 }
1906
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001907 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001908 * Creates an Allocation with the size specified by the type and no mipmaps
1909 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001910 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001911 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001912 * @param type renderscript type describing data layout
1913 * @param usage bit field specifying how the allocation is
1914 * utilized
1915 *
1916 * @return allocation
1917 */
Jason Samse5d37122010-12-16 00:33:33 -08001918 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1919 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1920 }
1921
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001922 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001923 * Creates an Allocation for use by scripts with a given {@link
1924 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001925 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001926 * @param rs Context to which the Allocation will belong.
1927 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001928 *
1929 * @return allocation
1930 */
Jason Sams5476b452010-12-08 16:14:36 -08001931 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001932 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001933 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001934
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001935 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001936 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001937 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001938 * @param rs Context to which the Allocation will belong.
1939 * @param e Element to use in the Allocation
1940 * @param count the number of Elements in the Allocation
1941 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001942 * utilized
1943 *
1944 * @return allocation
1945 */
Jason Sams5476b452010-12-08 16:14:36 -08001946 static public Allocation createSized(RenderScript rs, Element e,
1947 int count, int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07001948 try {
1949 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
1950 rs.validate();
1951 Type.Builder b = new Type.Builder(rs, e);
1952 b.setX(count);
1953 Type t = b.create();
Jason Sams768bc022009-09-21 19:41:04 -07001954
Chris Craik06d29842015-06-02 17:19:24 -07001955 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
1956 if (id == 0) {
1957 throw new RSRuntimeException("Allocation creation failed.");
1958 }
1959 return new Allocation(id, rs, t, usage);
1960 } finally {
1961 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001962 }
Jason Sams5476b452010-12-08 16:14:36 -08001963 }
1964
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001965 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001966 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001967 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001968 * @param rs Context to which the Allocation will belong.
1969 * @param e Element to use in the Allocation
1970 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001971 *
1972 * @return allocation
1973 */
Jason Sams5476b452010-12-08 16:14:36 -08001974 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001975 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001976 }
1977
Jason Sams49a05d72010-12-29 14:31:29 -08001978 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001979 final Bitmap.Config bc = b.getConfig();
1980 if (bc == Bitmap.Config.ALPHA_8) {
1981 return Element.A_8(rs);
1982 }
1983 if (bc == Bitmap.Config.ARGB_4444) {
1984 return Element.RGBA_4444(rs);
1985 }
1986 if (bc == Bitmap.Config.ARGB_8888) {
1987 return Element.RGBA_8888(rs);
1988 }
1989 if (bc == Bitmap.Config.RGB_565) {
1990 return Element.RGB_565(rs);
1991 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001992 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001993 }
1994
Jason Sams49a05d72010-12-29 14:31:29 -08001995 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001996 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001997 Element e = elementFromBitmap(rs, b);
1998 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001999 tb.setX(b.getWidth());
2000 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08002001 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08002002 return tb.create();
2003 }
2004
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002005 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002006 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002007 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002008 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07002009 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002010 * @param mips specifies desired mipmap behaviour for the
2011 * allocation
2012 * @param usage bit field specifying how the allocation is
2013 * utilized
2014 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002015 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002016 *
2017 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002018 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002019 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002020 int usage) {
Chris Craik06d29842015-06-02 17:19:24 -07002021 try {
2022 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
2023 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08002024
Chris Craik06d29842015-06-02 17:19:24 -07002025 // WAR undocumented color formats
2026 if (b.getConfig() == null) {
2027 if ((usage & USAGE_SHARED) != 0) {
2028 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
2029 }
2030 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
2031 Canvas c = new Canvas(newBitmap);
2032 c.drawBitmap(b, 0, 0, null);
2033 return createFromBitmap(rs, newBitmap, mips, usage);
Tim Murrayabd5db92013-02-28 11:45:22 -08002034 }
Tim Murrayabd5db92013-02-28 11:45:22 -08002035
Chris Craik06d29842015-06-02 17:19:24 -07002036 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08002037
Chris Craik06d29842015-06-02 17:19:24 -07002038 // enable optimized bitmap path only with no mipmap and script-only usage
2039 if (mips == MipmapControl.MIPMAP_NONE &&
2040 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
2041 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
2042 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
2043 if (id == 0) {
2044 throw new RSRuntimeException("Load failed.");
2045 }
2046
2047 // keep a reference to the Bitmap around to prevent GC
2048 Allocation alloc = new Allocation(id, rs, t, usage);
2049 alloc.setBitmap(b);
2050 return alloc;
2051 }
2052
2053
2054 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08002055 if (id == 0) {
2056 throw new RSRuntimeException("Load failed.");
2057 }
Chris Craik06d29842015-06-02 17:19:24 -07002058 return new Allocation(id, rs, t, usage);
2059 } finally {
2060 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murraya3145512012-12-04 17:59:29 -08002061 }
Jason Sams5476b452010-12-08 16:14:36 -08002062 }
2063
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002064 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002065 * Returns the handle to a raw buffer that is being managed by the screen
2066 * compositor. This operation is only valid for Allocations with {@link
2067 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002068 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07002069 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002070 *
2071 */
2072 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08002073 if ((mUsage & USAGE_IO_INPUT) == 0) {
2074 throw new RSInvalidStateException("Allocation is not a surface texture.");
2075 }
Jason Sams1e68bac2015-03-17 16:36:55 -07002076
2077 if (mGetSurfaceSurface == null) {
2078 mGetSurfaceSurface = mRS.nAllocationGetSurface(getID(mRS));
2079 }
2080
2081 return mGetSurfaceSurface;
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002082 }
2083
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002084 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002085 * Associate a {@link android.view.Surface} with this Allocation. This
2086 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07002087 *
2088 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08002089 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07002090 public void setSurface(Surface sur) {
2091 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08002092 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
2093 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
2094 }
2095
Jason Samse07694b2012-04-03 15:36:36 -07002096 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08002097 }
2098
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002099 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002100 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08002101 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002102 * <p>With target API version 18 or greater, this Allocation will be created
2103 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
2104 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
2105 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
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 b bitmap source for the allocation data
2109 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002110 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002111 *
2112 */
Jason Sams6d8eb262010-12-15 01:41:00 -08002113 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08002114 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2115 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07002116 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08002117 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002118 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
2119 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08002120 }
2121
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002122 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002123 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
2124 * containing the horizontal list of cube faces. Each face must be a square,
2125 * have the same size as all other faces, and have a width that is a power
2126 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002127 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002128 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07002129 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002130 * format: right, left, top, bottom, front, back
2131 * @param mips specifies desired mipmap behaviour for the cubemap
2132 * @param usage bit field specifying how the cubemap is utilized
2133 *
2134 * @return allocation containing cubemap data
2135 *
2136 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002137 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08002138 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002139 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002140 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08002141
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002142 int height = b.getHeight();
2143 int width = b.getWidth();
2144
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002145 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002146 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
2147 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002148 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002149 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002150 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002151 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002152 if (!isPow2) {
2153 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2154 }
2155
2156 Element e = elementFromBitmap(rs, b);
2157 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002158 tb.setX(height);
2159 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002160 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08002161 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002162 Type t = tb.create();
2163
Tim Murray460a0492013-11-19 12:45:54 -08002164 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002165 if(id == 0) {
2166 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
2167 }
Jason Sams5476b452010-12-08 16:14:36 -08002168 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08002169 }
2170
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002171 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002172 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
2173 * from a {@link android.graphics.Bitmap} containing the horizontal list of
2174 * cube faces. Each face must be a square, have the same size as all other
2175 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002176 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002177 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002178 * @param b bitmap with cubemap faces layed out in the following
2179 * format: right, left, top, bottom, front, back
2180 *
2181 * @return allocation containing cubemap data
2182 *
2183 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002184 static public Allocation createCubemapFromBitmap(RenderScript rs,
2185 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08002186 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08002187 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08002188 }
2189
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002190 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002191 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
2192 * objects containing the cube faces. Each face must be a square, have the
2193 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002194 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002195 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002196 * @param xpos cubemap face in the positive x direction
2197 * @param xneg cubemap face in the negative x direction
2198 * @param ypos cubemap face in the positive y direction
2199 * @param yneg cubemap face in the negative y direction
2200 * @param zpos cubemap face in the positive z direction
2201 * @param zneg cubemap face in the negative z direction
2202 * @param mips specifies desired mipmap behaviour for the cubemap
2203 * @param usage bit field specifying how the cubemap is utilized
2204 *
2205 * @return allocation containing cubemap data
2206 *
2207 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002208 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2209 Bitmap xpos,
2210 Bitmap xneg,
2211 Bitmap ypos,
2212 Bitmap yneg,
2213 Bitmap zpos,
2214 Bitmap zneg,
2215 MipmapControl mips,
2216 int usage) {
2217 int height = xpos.getHeight();
2218 if (xpos.getWidth() != height ||
2219 xneg.getWidth() != height || xneg.getHeight() != height ||
2220 ypos.getWidth() != height || ypos.getHeight() != height ||
2221 yneg.getWidth() != height || yneg.getHeight() != height ||
2222 zpos.getWidth() != height || zpos.getHeight() != height ||
2223 zneg.getWidth() != height || zneg.getHeight() != height) {
2224 throw new RSIllegalArgumentException("Only square cube map faces supported");
2225 }
2226 boolean isPow2 = (height & (height - 1)) == 0;
2227 if (!isPow2) {
2228 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
2229 }
2230
2231 Element e = elementFromBitmap(rs, xpos);
2232 Type.Builder tb = new Type.Builder(rs, e);
2233 tb.setX(height);
2234 tb.setY(height);
2235 tb.setFaces(true);
2236 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
2237 Type t = tb.create();
2238 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
2239
2240 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07002241 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002242 adapter.copyFrom(xpos);
2243 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
2244 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002245 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002246 adapter.copyFrom(ypos);
2247 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
2248 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07002249 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002250 adapter.copyFrom(zpos);
2251 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
2252 adapter.copyFrom(zneg);
2253
2254 return cubemap;
2255 }
2256
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002257 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002258 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
2259 * from 6 {@link android.graphics.Bitmap} objects containing the cube
2260 * faces. Each face must be a square, have the same size as all other faces,
2261 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002262 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002263 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002264 * @param xpos cubemap face in the positive x direction
2265 * @param xneg cubemap face in the negative x direction
2266 * @param ypos cubemap face in the positive y direction
2267 * @param yneg cubemap face in the negative y direction
2268 * @param zpos cubemap face in the positive z direction
2269 * @param zneg cubemap face in the negative z direction
2270 *
2271 * @return allocation containing cubemap data
2272 *
2273 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08002274 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
2275 Bitmap xpos,
2276 Bitmap xneg,
2277 Bitmap ypos,
2278 Bitmap yneg,
2279 Bitmap zpos,
2280 Bitmap zneg) {
2281 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
2282 zpos, zneg, MipmapControl.MIPMAP_NONE,
2283 USAGE_GRAPHICS_TEXTURE);
2284 }
2285
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002286 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002287 * Creates an Allocation from the Bitmap referenced
2288 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002289 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002290 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002291 * @param res application resources
2292 * @param id resource id to load the data from
2293 * @param mips specifies desired mipmap behaviour for the
2294 * allocation
2295 * @param usage bit field specifying how the allocation is
2296 * utilized
2297 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002298 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002299 *
2300 */
Jason Sams5476b452010-12-08 16:14:36 -08002301 static public Allocation createFromBitmapResource(RenderScript rs,
2302 Resources res,
2303 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08002304 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08002305 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07002306
Jason Sams771bebb2009-12-07 12:40:12 -08002307 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07002308 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
2309 throw new RSIllegalArgumentException("Unsupported usage specified.");
2310 }
Jason Sams5476b452010-12-08 16:14:36 -08002311 Bitmap b = BitmapFactory.decodeResource(res, id);
2312 Allocation alloc = createFromBitmap(rs, b, mips, usage);
2313 b.recycle();
2314 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07002315 }
2316
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002317 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002318 * Creates a non-mipmapped Allocation to use as a graphics texture from the
2319 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002320 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002321 * <p>With target API version 18 or greater, this allocation will be created
2322 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
2323 * target API version 17 or lower, this allocation will be created with
2324 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08002325 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002326 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002327 * @param res application resources
2328 * @param id resource id to load the data from
2329 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07002330 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002331 *
2332 */
Jason Sams5476b452010-12-08 16:14:36 -08002333 static public Allocation createFromBitmapResource(RenderScript rs,
2334 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08002335 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08002336 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
2337 return createFromBitmapResource(rs, res, id,
2338 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07002339 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08002340 }
Jason Sams6d8eb262010-12-15 01:41:00 -08002341 return createFromBitmapResource(rs, res, id,
2342 MipmapControl.MIPMAP_NONE,
2343 USAGE_GRAPHICS_TEXTURE);
2344 }
2345
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07002346 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002347 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002348 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08002349 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08002350 * @param str string to create the allocation from
2351 * @param usage bit field specifying how the allocaiton is
2352 * utilized
2353 *
2354 */
Jason Sams5476b452010-12-08 16:14:36 -08002355 static public Allocation createFromString(RenderScript rs,
2356 String str,
2357 int usage) {
2358 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002359 byte[] allocArray = null;
2360 try {
2361 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08002362 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08002363 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002364 return alloc;
2365 }
2366 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08002367 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002368 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002369 }
Jason Sams739c8262013-04-11 18:07:52 -07002370
2371 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002372 * Interface to handle notification when new buffers are available via
2373 * {@link #USAGE_IO_INPUT}. An application will receive one notification
2374 * when a buffer is available. Additional buffers will not trigger new
2375 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07002376 */
Jason Sams42ef2382013-08-29 13:30:59 -07002377 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07002378 public void onBufferAvailable(Allocation a);
2379 }
2380
2381 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07002382 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07002383 *
Jason Sams42ef2382013-08-29 13:30:59 -07002384 * @param callback instance of the OnBufferAvailableListener
2385 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07002386 */
Jason Sams42ef2382013-08-29 13:30:59 -07002387 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07002388 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002389 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07002390 mBufferNotifier = callback;
2391 }
2392 }
2393
Tim Murrayb730d862014-08-18 16:14:24 -07002394 static void sendBufferNotification(long id) {
Jason Sams739c8262013-04-11 18:07:52 -07002395 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08002396 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07002397
2398 if ((a != null) && (a.mBufferNotifier != null)) {
2399 a.mBufferNotifier.onBufferAvailable(a);
2400 }
2401 }
2402 }
2403
Miao Wangf0f6e802015-02-03 17:16:43 -08002404 /**
2405 * For USAGE_IO_OUTPUT, destroy() implies setSurface(null).
2406 *
2407 */
2408 @Override
2409 public void destroy() {
2410 if((mUsage & USAGE_IO_OUTPUT) != 0) {
2411 setSurface(null);
2412 }
2413 super.destroy();
2414 }
Jason Samsb8c5a842009-07-31 20:40:47 -07002415}