blob: b460a4ddee5f43ba182f0aa2f29cd01209039d8d [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
Stephen Hines9069ee82012-02-13 18:25:54 -08002 * Copyright (C) 2008-2012 The Android Open Source Project
Jason Samsb8c5a842009-07-31 20:40:47 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
Jason Samsb8c5a842009-07-31 20:40:47 -070019import java.io.IOException;
20import java.io.InputStream;
Jason Sams739c8262013-04-11 18:07:52 -070021import java.util.HashMap;
Jason Samsb8c5a842009-07-31 20:40:47 -070022import android.content.res.Resources;
Romain Guy650a3eb2009-08-31 14:06:43 -070023import android.content.res.AssetManager;
Jason Samsb8c5a842009-07-31 20:40:47 -070024import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
Jason Samsfb9aa9f2012-03-28 15:30:07 -070026import android.view.Surface;
Jason Sams615e7ce2012-01-13 14:01:20 -080027import android.graphics.SurfaceTexture;
Jason Samsb8c5a842009-07-31 20:40:47 -070028import android.util.Log;
Romain Guy650a3eb2009-08-31 14:06:43 -070029import android.util.TypedValue;
Tim Murrayabd5db92013-02-28 11:45:22 -080030import android.graphics.Canvas;
Jason Samsb8c5a842009-07-31 20:40:47 -070031
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070032/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070033 * <p> This class provides the primary method through which data is passed to
34 * and from RenderScript kernels. An Allocation provides the backing store for
35 * a given {@link android.renderscript.Type}. </p>
Jason Samsa23d4e72011-01-04 18:59:12 -080036 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070037 * <p>An Allocation also contains a set of usage flags that denote how the
38 * Allocation could be used. For example, an Allocation may have usage flags
39 * specifying that it can be used from a script as well as input to a {@link
40 * android.renderscript.Sampler}. A developer must synchronize across these
41 * different usages using {@link android.renderscript.Allocation#syncAll} in
42 * order to ensure that different users of the Allocation have a consistent view
43 * of memory. For example, in the case where an Allocation is used as the output
44 * of one kernel and as Sampler input in a later kernel, a developer must call
45 * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
46 * second kernel to ensure correctness.
Jason Samsa23d4e72011-01-04 18:59:12 -080047 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070048 * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
49 * more complex Element types, the {@link #copyFromUnchecked} methods can be
50 * used to copy from byte arrays or similar constructs.</p>
Jason Samsb8c5a842009-07-31 20:40:47 -070051 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080052 * <div class="special reference">
53 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070054 * <p>For more information about creating an application that uses RenderScript, read the
55 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080056 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070057 **/
58public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070059 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080060 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080061 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070062 Allocation mAdaptedAllocation;
63
64 boolean mConstrainedLOD;
65 boolean mConstrainedFace;
66 boolean mConstrainedY;
67 boolean mConstrainedZ;
Jason Sams615e7ce2012-01-13 14:01:20 -080068 boolean mReadAllowed = true;
69 boolean mWriteAllowed = true;
Jason Samsba862d12011-07-07 15:24:42 -070070 int mSelectedY;
71 int mSelectedZ;
72 int mSelectedLOD;
73 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
74
75 int mCurrentDimX;
76 int mCurrentDimY;
77 int mCurrentDimZ;
78 int mCurrentCount;
Jason Sams739c8262013-04-11 18:07:52 -070079 static HashMap<Integer, Allocation> mAllocationMap =
80 new HashMap<Integer, Allocation>();
81 IoInputNotifier mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070082
Tim Murrayc11e25c2013-04-09 11:01:01 -070083 /**
84 * The usage of the Allocation. These signal to RenderScript where to place
85 * the Allocation in memory.
86 *
87 */
Jason Sams5476b452010-12-08 16:14:36 -080088
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070089 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -070090 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -080091 */
Jason Sams5476b452010-12-08 16:14:36 -080092 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -080093
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070094 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -070095 * The Allocation will be used as a texture source by one or more graphics
96 * programs.
Jason Samsf7086092011-01-12 13:28:37 -080097 *
98 */
Jason Sams5476b452010-12-08 16:14:36 -080099 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800100
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700101 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700102 * The Allocation will be used as a graphics mesh.
103 *
104 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800105 *
106 */
Jason Sams5476b452010-12-08 16:14:36 -0800107 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800108
109
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700110 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700111 * The Allocation will be used as the source of shader constants by one or
112 * more programs.
113 *
114 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800115 *
116 */
Jason Sams5476b452010-12-08 16:14:36 -0800117 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
118
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700119 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700120 * The Allocation will be used as a target for offscreen rendering
121 *
122 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700123 *
124 */
125 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
126
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700127 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700128 * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
129 * consumer. This usage will cause the Allocation to be created as
130 * read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800131 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800132 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700133 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800134
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700135 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700136 * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
137 * producer. The dimensions and format of the {@link
138 * android.graphics.SurfaceTexture} will be forced to those of the
139 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800140 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800141 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700142 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700143
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700144 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700145 * The Allocation's backing store will be inherited from another object
146 * (usually a {@link android.graphics.Bitmap}); copying to or from the
147 * original source Bitmap will cause a synchronization rather than a full
148 * copy. {@link #syncAll} may also be used to synchronize the Allocation
149 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800150 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700151 * <p>This is set by default for allocations created with {@link
152 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800153 *
154 */
155 public static final int USAGE_SHARED = 0x0080;
156
157 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700158 * Controls mipmap behavior when using the bitmap creation and update
159 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800160 */
Jason Sams4ef66502010-12-10 16:03:15 -0800161 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700162 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700163 * No mipmaps will be generated and the type generated from the incoming
164 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800165 */
Jason Sams5476b452010-12-08 16:14:36 -0800166 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800167
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700168 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700169 * A full mipmap chain will be created in script memory. The Type of
170 * the Allocation will contain a full mipmap chain. On upload, the full
171 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800172 */
Jason Sams5476b452010-12-08 16:14:36 -0800173 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800174
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700175 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700176 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
177 * not contain mipmaps. On upload, the allocation data will contain a
178 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800179 */
Jason Sams5476b452010-12-08 16:14:36 -0800180 MIPMAP_ON_SYNC_TO_TEXTURE(2);
181
182 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800183 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800184 mID = id;
185 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700186 }
187
Jason Sams48fe5342011-07-08 13:52:30 -0700188
189 private int getIDSafe() {
190 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700191 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700192 }
Jason Samse07694b2012-04-03 15:36:36 -0700193 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700194 }
195
Jason Sams03d2d002012-03-23 13:51:56 -0700196
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700197 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700198 * Get the {@link android.renderscript.Element} of the {@link
199 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700200 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700201 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700202 *
203 */
204 public Element getElement() {
205 return mType.getElement();
206 }
207
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700208 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700209 * Get the usage flags of the Allocation.
210 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700211 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700212 *
213 */
214 public int getUsage() {
215 return mUsage;
216 }
217
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700218 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700219 * Get the size of the Allocation in bytes.
220 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700221 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700222 *
223 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700224 public int getBytesSize() {
225 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700226 }
227
Jason Sams452a7662011-07-07 16:05:18 -0700228 private void updateCacheInfo(Type t) {
229 mCurrentDimX = t.getX();
230 mCurrentDimY = t.getY();
231 mCurrentDimZ = t.getZ();
232 mCurrentCount = mCurrentDimX;
233 if (mCurrentDimY > 1) {
234 mCurrentCount *= mCurrentDimY;
235 }
236 if (mCurrentDimZ > 1) {
237 mCurrentCount *= mCurrentDimZ;
238 }
239 }
Jason Samsba862d12011-07-07 15:24:42 -0700240
Tim Murraya3145512012-12-04 17:59:29 -0800241 private void setBitmap(Bitmap b) {
242 mBitmap = b;
243 }
244
Jason Sams5476b452010-12-08 16:14:36 -0800245 Allocation(int id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700246 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800247 if ((usage & ~(USAGE_SCRIPT |
248 USAGE_GRAPHICS_TEXTURE |
249 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700250 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800251 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800252 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800253 USAGE_IO_OUTPUT |
254 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800255 throw new RSIllegalArgumentException("Unknown usage specified.");
256 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800257
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700258 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800259 mWriteAllowed = false;
260
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700261 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800262 USAGE_GRAPHICS_TEXTURE |
263 USAGE_SCRIPT)) != 0) {
264 throw new RSIllegalArgumentException("Invalid usage combination.");
265 }
266 }
Jason Sams9bf18922013-04-13 19:48:36 -0700267
Jason Sams5476b452010-12-08 16:14:36 -0800268 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800269 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700270
Jason Sams452a7662011-07-07 16:05:18 -0700271 if (t != null) {
272 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700273 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700274 }
275
Jason Samsb97b2512011-01-16 15:04:08 -0800276 private void validateIsInt32() {
277 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
278 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
279 return;
280 }
281 throw new RSIllegalArgumentException(
282 "32 bit integer source does not match allocation type " + mType.mElement.mType);
283 }
284
285 private void validateIsInt16() {
286 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
287 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
288 return;
289 }
290 throw new RSIllegalArgumentException(
291 "16 bit integer source does not match allocation type " + mType.mElement.mType);
292 }
293
294 private void validateIsInt8() {
295 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
296 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
297 return;
298 }
299 throw new RSIllegalArgumentException(
300 "8 bit integer source does not match allocation type " + mType.mElement.mType);
301 }
302
303 private void validateIsFloat32() {
304 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
305 return;
306 }
307 throw new RSIllegalArgumentException(
308 "32 bit float source does not match allocation type " + mType.mElement.mType);
309 }
310
311 private void validateIsObject() {
312 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
313 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
314 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
315 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
316 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
317 (mType.mElement.mType == Element.DataType.RS_MESH) ||
318 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
319 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
320 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
321 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
322 return;
323 }
324 throw new RSIllegalArgumentException(
325 "Object source does not match allocation type " + mType.mElement.mType);
326 }
327
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700328 @Override
329 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800330 super.updateFromNative();
Jason Samse07694b2012-04-03 15:36:36 -0700331 int typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700332 if(typeID != 0) {
333 mType = new Type(typeID, mRS);
334 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700335 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700336 }
337 }
338
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700339 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700340 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700341 *
342 * @return Type
343 *
344 */
Jason Samsea87e962010-01-12 12:12:28 -0800345 public Type getType() {
346 return mType;
347 }
348
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700349 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700350 * Propagate changes from one usage of the Allocation to the
351 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700352 *
353 */
Jason Sams5476b452010-12-08 16:14:36 -0800354 public void syncAll(int srcLocation) {
355 switch (srcLocation) {
Jason Sams5476b452010-12-08 16:14:36 -0800356 case USAGE_GRAPHICS_TEXTURE:
Tim Murray78e64942013-04-09 17:28:56 -0700357 case USAGE_SCRIPT:
358 if ((mUsage & USAGE_SHARED) != 0) {
359 copyFrom(mBitmap);
360 }
361 break;
362 case USAGE_GRAPHICS_CONSTANTS:
Jason Sams5476b452010-12-08 16:14:36 -0800363 case USAGE_GRAPHICS_VERTEX:
364 break;
Tim Murray78e64942013-04-09 17:28:56 -0700365 case USAGE_SHARED:
366 if ((mUsage & USAGE_SHARED) != 0) {
367 copyTo(mBitmap);
368 }
369 break;
Jason Sams5476b452010-12-08 16:14:36 -0800370 default:
371 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
372 }
373 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700374 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Jason Sams5476b452010-12-08 16:14:36 -0800375 }
376
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700377 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700378 * Send a buffer to the output stream. The contents of the Allocation will
379 * be undefined after this operation. This operation is only valid if {@link
380 * #USAGE_IO_OUTPUT} is set on the Allocation.
381 *
Jason Sams163766c2012-02-15 12:04:24 -0800382 *
Jason Sams163766c2012-02-15 12:04:24 -0800383 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700384 public void ioSend() {
Jason Sams163766c2012-02-15 12:04:24 -0800385 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
386 throw new RSIllegalArgumentException(
387 "Can only send buffer if IO_OUTPUT usage specified.");
388 }
389 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700390 mRS.nAllocationIoSend(getID(mRS));
Jason Sams163766c2012-02-15 12:04:24 -0800391 }
392
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700393 /**
Jason Samsc5f519c2012-03-29 17:58:15 -0700394 * Delete once code is updated.
395 * @hide
396 */
397 public void ioSendOutput() {
398 ioSend();
399 }
400
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700401 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700402 * Receive the latest input into the Allocation. This operation
403 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800404 *
Jason Sams163766c2012-02-15 12:04:24 -0800405 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700406 public void ioReceive() {
Jason Sams163766c2012-02-15 12:04:24 -0800407 if ((mUsage & USAGE_IO_INPUT) == 0) {
408 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700409 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800410 }
411 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700412 mRS.nAllocationIoReceive(getID(mRS));
Jason Sams163766c2012-02-15 12:04:24 -0800413 }
414
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700415 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700416 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700417 *
418 * @param d Source array.
419 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800420 public void copyFrom(BaseObj[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800421 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800422 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700423 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800424 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700425 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800426 }
427 int i[] = new int[d.length];
428 for (int ct=0; ct < d.length; ct++) {
Jason Samse07694b2012-04-03 15:36:36 -0700429 i[ct] = d[ct].getID(mRS);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800430 }
Jason Samsba862d12011-07-07 15:24:42 -0700431 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Jason Samsb8c5a842009-07-31 20:40:47 -0700432 }
433
Jason Samsfb9f82c2011-01-12 14:53:25 -0800434 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800435 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800436 if (bc == null) {
437 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
438 }
Jason Sams252c0782011-01-11 17:42:52 -0800439 switch (bc) {
440 case ALPHA_8:
441 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
442 throw new RSIllegalArgumentException("Allocation kind is " +
443 mType.getElement().mKind + ", type " +
444 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700445 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800446 " bytes, passed bitmap was " + bc);
447 }
448 break;
449 case ARGB_8888:
450 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700451 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800452 throw new RSIllegalArgumentException("Allocation kind is " +
453 mType.getElement().mKind + ", type " +
454 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700455 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800456 " bytes, passed bitmap was " + bc);
457 }
458 break;
459 case RGB_565:
460 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700461 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800462 throw new RSIllegalArgumentException("Allocation kind is " +
463 mType.getElement().mKind + ", type " +
464 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700465 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800466 " bytes, passed bitmap was " + bc);
467 }
468 break;
469 case ARGB_4444:
470 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700471 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800472 throw new RSIllegalArgumentException("Allocation kind is " +
473 mType.getElement().mKind + ", type " +
474 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700475 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800476 " bytes, passed bitmap was " + bc);
477 }
478 break;
479
480 }
Jason Sams4ef66502010-12-10 16:03:15 -0800481 }
482
Jason Samsfb9f82c2011-01-12 14:53:25 -0800483 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700484 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800485 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
486 }
487 }
488
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700489 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700490 * Copy into this Allocation from an array. This method does not guarantee
491 * that the Allocation is compatible with the input buffer; it copies memory
492 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800493 *
494 * @param d the source data array
495 */
496 public void copyFromUnchecked(int[] d) {
497 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700498 if (mCurrentDimZ > 0) {
499 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
500 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800501 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
502 } else {
503 copy1DRangeFromUnchecked(0, mCurrentCount, d);
504 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800505 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700506 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700507 * Copy into this Allocation from an array. This method does not guarantee
508 * that the Allocation is compatible with the input buffer; it copies memory
509 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800510 *
511 * @param d the source data array
512 */
513 public void copyFromUnchecked(short[] d) {
514 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700515 if (mCurrentDimZ > 0) {
516 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
517 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800518 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
519 } else {
520 copy1DRangeFromUnchecked(0, mCurrentCount, d);
521 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800522 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700523 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700524 * Copy into this Allocation from an array. This method does not guarantee
525 * that the Allocation is compatible with the input buffer; it copies memory
526 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800527 *
528 * @param d the source data array
529 */
530 public void copyFromUnchecked(byte[] d) {
531 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700532 if (mCurrentDimZ > 0) {
533 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
534 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800535 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
536 } else {
537 copy1DRangeFromUnchecked(0, mCurrentCount, d);
538 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800539 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700540 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700541 * Copy into this Allocation from an array. This method does not guarantee
542 * that the Allocation is compatible with the input buffer; it copies memory
543 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800544 *
545 * @param d the source data array
546 */
547 public void copyFromUnchecked(float[] d) {
548 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700549 if (mCurrentDimZ > 0) {
550 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
551 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800552 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
553 } else {
554 copy1DRangeFromUnchecked(0, mCurrentCount, d);
555 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800556 }
557
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700558 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700559 * Copy into this Allocation from an array. This variant is type checked
560 * and will generate exceptions if the Allocation's {@link
561 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800562 *
563 * @param d the source data array
564 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800565 public void copyFrom(int[] d) {
566 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700567 if (mCurrentDimZ > 0) {
568 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
569 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800570 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
571 } else {
572 copy1DRangeFrom(0, mCurrentCount, d);
573 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800574 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800575
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700576 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700577 * Copy into this Allocation from an array. This variant is type checked
578 * and will generate exceptions if the Allocation's {@link
579 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800580 *
581 * @param d the source data array
582 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800583 public void copyFrom(short[] d) {
584 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700585 if (mCurrentDimZ > 0) {
586 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
587 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800588 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
589 } else {
590 copy1DRangeFrom(0, mCurrentCount, d);
591 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800592 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800593
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700594 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700595 * Copy into this Allocation from an array. This variant is type checked
596 * and will generate exceptions if the Allocation's {@link
597 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800598 *
599 * @param d the source data array
600 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800601 public void copyFrom(byte[] d) {
602 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700603 if (mCurrentDimZ > 0) {
604 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
605 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800606 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
607 } else {
608 copy1DRangeFrom(0, mCurrentCount, d);
609 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800610 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800611
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700612 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700613 * Copy into this Allocation from an array. This variant is type checked
614 * and will generate exceptions if the Allocation's {@link
615 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800616 *
617 * @param d the source data array
618 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800619 public void copyFrom(float[] d) {
620 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700621 if (mCurrentDimZ > 0) {
622 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
623 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800624 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
625 } else {
626 copy1DRangeFrom(0, mCurrentCount, d);
627 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800628 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800629
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700630 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700631 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
632 * height, width, and format of the bitmap must match the existing
633 * allocation.
634 *
635 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
636 * android.graphics.Bitmap} used to create the Allocation with {@link
637 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
638 * this will synchronize the Allocation with the latest data from the {@link
639 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800640 *
641 * @param b the source bitmap
642 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800643 public void copyFrom(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800644 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -0800645 if (b.getConfig() == null) {
646 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
647 Canvas c = new Canvas(newBitmap);
648 c.drawBitmap(b, 0, 0, null);
649 copyFrom(newBitmap);
650 return;
651 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800652 validateBitmapSize(b);
653 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700654 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700655 }
656
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700657 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700658 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700659 * must be identical.
660 *
661 * @param a the source allocation
662 */
663 public void copyFrom(Allocation a) {
664 mRS.validate();
665 if (!mType.equals(a.getType())) {
666 throw new RSIllegalArgumentException("Types of allocations must match.");
667 }
668 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
669 }
670
671
672 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700673 * This is only intended to be used by auto-generated code reflected from
674 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800675 *
676 * @param xoff
677 * @param fp
678 */
Jason Sams21b41032011-01-16 15:05:41 -0800679 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800680 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700681 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700682 final byte[] data = fp.getData();
683
684 int count = data.length / eSize;
685 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800686 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700687 " not divisible by element size " + eSize + ".");
688 }
Jason Samsba862d12011-07-07 15:24:42 -0700689 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700690 }
691
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700692 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700693 * This is only intended to be used by auto-generated code reflected from
694 * the RenderScript script files.
Jason Samsfa445b92011-01-07 17:00:07 -0800695 *
696 * @param xoff
697 * @param component_number
698 * @param fp
699 */
Jason Sams21b41032011-01-16 15:05:41 -0800700 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800701 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700702 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800703 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700704 }
705 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800706 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700707 }
708
709 final byte[] data = fp.getData();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700710 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800711 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700712
713 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800714 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700715 " does not match component size " + eSize + ".");
716 }
717
Jason Sams48fe5342011-07-08 13:52:30 -0700718 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Jason Samsba862d12011-07-07 15:24:42 -0700719 component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700720 }
721
Jason Sams768bc022009-09-21 19:41:04 -0700722 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800723 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700724 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800725 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700726 }
727 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800728 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700729 }
Jason Samsba862d12011-07-07 15:24:42 -0700730 if((off + count) > mCurrentCount) {
731 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700732 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700733 }
Jason Samsba862d12011-07-07 15:24:42 -0700734 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800735 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700736 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700737 }
738
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700739 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700740 * Generate a mipmap chain. This is only valid if the Type of the Allocation
741 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800742 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700743 * <p>This function will generate a complete set of mipmaps from the top
744 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800745 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700746 * <p>If the Allocation is also using other memory spaces, a call to {@link
747 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800748 */
749 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700750 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800751 }
752
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700753 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700754 * Copy an array into part of this Allocation. This method does not
755 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800756 *
757 * @param off The offset of the first element to be copied.
758 * @param count The number of elements to be copied.
759 * @param d the source data array
760 */
761 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700762 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700763 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700764 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700765 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700766 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700767 * Copy an array into part of this Allocation. This method does not
768 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800769 *
770 * @param off The offset of the first element to be copied.
771 * @param count The number of elements to be copied.
772 * @param d the source data array
773 */
774 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700775 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700776 data1DChecks(off, count, d.length * 2, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700777 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700778 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700779 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700780 * Copy an array into part of this Allocation. This method does not
781 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800782 *
783 * @param off The offset of the first element to be copied.
784 * @param count The number of elements to be copied.
785 * @param d the source data array
786 */
787 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700788 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700789 data1DChecks(off, count, d.length, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700790 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700791 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700792 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700793 * Copy an array into part of this Allocation. This method does not
794 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800795 *
796 * @param off The offset of the first element to be copied.
797 * @param count The number of elements to be copied.
798 * @param d the source data array
799 */
800 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700801 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700802 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700803 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700804 }
805
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700806 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700807 * Copy an array into part of this Allocation. This variant is type checked
808 * and will generate exceptions if the Allocation type is not a 32 bit
809 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800810 *
811 * @param off The offset of the first element to be copied.
812 * @param count The number of elements to be copied.
813 * @param d the source data array
814 */
Jason Samsb97b2512011-01-16 15:04:08 -0800815 public void copy1DRangeFrom(int off, int count, int[] d) {
816 validateIsInt32();
817 copy1DRangeFromUnchecked(off, count, d);
818 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800819
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700820 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700821 * Copy an array into part of this Allocation. This variant is type checked
822 * and will generate exceptions if the Allocation type is not a 16 bit
823 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800824 *
825 * @param off The offset of the first element to be copied.
826 * @param count The number of elements to be copied.
827 * @param d the source data array
828 */
Jason Samsb97b2512011-01-16 15:04:08 -0800829 public void copy1DRangeFrom(int off, int count, short[] d) {
830 validateIsInt16();
831 copy1DRangeFromUnchecked(off, count, d);
832 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800833
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700834 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700835 * Copy an array into part of this Allocation. This variant is type checked
836 * and will generate exceptions if the Allocation type is not an 8 bit
837 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800838 *
839 * @param off The offset of the first element to be copied.
840 * @param count The number of elements to be copied.
841 * @param d the source data array
842 */
Jason Samsb97b2512011-01-16 15:04:08 -0800843 public void copy1DRangeFrom(int off, int count, byte[] d) {
844 validateIsInt8();
845 copy1DRangeFromUnchecked(off, count, d);
846 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800847
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700848 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700849 * Copy an array into part of this Allocation. This variant is type checked
850 * and will generate exceptions if the Allocation type is not a 32 bit float
851 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800852 *
853 * @param off The offset of the first element to be copied.
854 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700855 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800856 */
Jason Samsb97b2512011-01-16 15:04:08 -0800857 public void copy1DRangeFrom(int off, int count, float[] d) {
858 validateIsFloat32();
859 copy1DRangeFromUnchecked(off, count, d);
860 }
861
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700862 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700863 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700864 *
865 * @param off The offset of the first element to be copied.
866 * @param count The number of elements to be copied.
867 * @param data the source data allocation.
868 * @param dataOff off The offset of the first element in data to
869 * be copied.
870 */
871 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Jason Sams48fe5342011-07-08 13:52:30 -0700872 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700873 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700874 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700875 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700876 }
877
Jason Samsfb9f82c2011-01-12 14:53:25 -0800878 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -0700879 if (mAdaptedAllocation != null) {
880
881 } else {
882
883 if (xoff < 0 || yoff < 0) {
884 throw new RSIllegalArgumentException("Offset cannot be negative.");
885 }
886 if (h < 0 || w < 0) {
887 throw new RSIllegalArgumentException("Height or width cannot be negative.");
888 }
889 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
890 throw new RSIllegalArgumentException("Updated region larger than allocation.");
891 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800892 }
893 }
Jason Sams768bc022009-09-21 19:41:04 -0700894
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800895 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, byte[] data) {
896 mRS.validate();
897 validate2DRange(xoff, yoff, w, h);
898 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
899 w, h, data, data.length);
900 }
901
902 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, short[] data) {
903 mRS.validate();
904 validate2DRange(xoff, yoff, w, h);
905 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
906 w, h, data, data.length * 2);
907 }
908
909 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, int[] data) {
910 mRS.validate();
911 validate2DRange(xoff, yoff, w, h);
912 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
913 w, h, data, data.length * 4);
914 }
915
916 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, float[] data) {
917 mRS.validate();
918 validate2DRange(xoff, yoff, w, h);
919 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
920 w, h, data, data.length * 4);
921 }
922
923
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700924 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700925 * Copy from an array into a rectangular region in this Allocation. The
926 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -0800927 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700928 * @param xoff X offset of the region to update in this Allocation
929 * @param yoff Y offset of the region to update in this Allocation
930 * @param w Width of the region to update
931 * @param h Height of the region to update
932 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -0800933 */
934 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -0800935 validateIsInt8();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800936 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Jason Samsfa445b92011-01-07 17:00:07 -0800937 }
938
Tim Murrayc11e25c2013-04-09 11:01:01 -0700939 /**
940 * Copy from an array into a rectangular region in this Allocation. The
941 * array is assumed to be tightly packed.
942 *
943 * @param xoff X offset of the region to update in this Allocation
944 * @param yoff Y offset of the region to update in this Allocation
945 * @param w Width of the region to update
946 * @param h Height of the region to update
947 * @param data to be placed into the Allocation
948 */
Jason Samsf7086092011-01-12 13:28:37 -0800949 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -0800950 validateIsInt16();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800951 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Jason Samsfa445b92011-01-07 17:00:07 -0800952 }
953
Tim Murrayc11e25c2013-04-09 11:01:01 -0700954 /**
955 * Copy from an array into a rectangular region in this Allocation. The
956 * array is assumed to be tightly packed.
957 *
958 * @param xoff X offset of the region to update in this Allocation
959 * @param yoff Y offset of the region to update in this Allocation
960 * @param w Width of the region to update
961 * @param h Height of the region to update
962 * @param data to be placed into the Allocation
963 */
Jason Samsf7086092011-01-12 13:28:37 -0800964 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -0800965 validateIsInt32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800966 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Jason Samsb8c5a842009-07-31 20:40:47 -0700967 }
968
Tim Murrayc11e25c2013-04-09 11:01:01 -0700969 /**
970 * Copy from an array into a rectangular region in this Allocation. The
971 * array is assumed to be tightly packed.
972 *
973 * @param xoff X offset of the region to update in this Allocation
974 * @param yoff Y offset of the region to update in this Allocation
975 * @param w Width of the region to update
976 * @param h Height of the region to update
977 * @param data to be placed into the Allocation
978 */
Jason Samsf7086092011-01-12 13:28:37 -0800979 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -0800980 validateIsFloat32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800981 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Jason Samsb8c5a842009-07-31 20:40:47 -0700982 }
983
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700984 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700985 * Copy a rectangular region from an Allocation into a rectangular region in
986 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700987 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700988 * @param xoff X offset of the region in this Allocation
989 * @param yoff Y offset of the region in this Allocation
990 * @param w Width of the region to update.
991 * @param h Height of the region to update.
992 * @param data source Allocation.
993 * @param dataXoff X offset in source Allocation
994 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700995 */
996 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
997 Allocation data, int dataXoff, int dataYoff) {
998 mRS.validate();
999 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -07001000 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -07001001 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001002 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -07001003 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001004 }
1005
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001006 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001007 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1008 * and width of the update will use the height and width of the {@link
1009 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001010 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001011 * @param xoff X offset of the region to update in this Allocation
1012 * @param yoff Y offset of the region to update in this Allocation
1013 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001014 */
1015 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Jason Samsfa445b92011-01-07 17:00:07 -08001016 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001017 if (data.getConfig() == null) {
1018 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1019 Canvas c = new Canvas(newBitmap);
1020 c.drawBitmap(data, 0, 0, null);
1021 copy2DRangeFrom(xoff, yoff, newBitmap);
Jason Samsb05d6892013-04-09 15:59:24 -07001022 return;
Tim Murrayabd5db92013-02-28 11:45:22 -08001023 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001024 validateBitmapFormat(data);
1025 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -07001026 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Jason Samsfa445b92011-01-07 17:00:07 -08001027 }
1028
Jason Samsb05d6892013-04-09 15:59:24 -07001029 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1030 if (mAdaptedAllocation != null) {
1031
1032 } else {
1033
1034 if (xoff < 0 || yoff < 0 || zoff < 0) {
1035 throw new RSIllegalArgumentException("Offset cannot be negative.");
1036 }
1037 if (h < 0 || w < 0 || d < 0) {
1038 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1039 }
1040 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1041 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1042 }
1043 }
1044 }
1045
1046 /**
1047 * @hide
1048 *
1049 */
1050 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1051 mRS.validate();
1052 validate3DRange(xoff, yoff, zoff, w, h, d);
1053 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1054 w, h, d, data, data.length);
1055 }
1056
1057 /**
1058 * @hide
1059 *
1060 */
1061 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1062 mRS.validate();
1063 validate3DRange(xoff, yoff, zoff, w, h, d);
1064 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1065 w, h, d, data, data.length * 2);
1066 }
1067
1068 /**
1069 * @hide
1070 *
1071 */
1072 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
1073 mRS.validate();
1074 validate3DRange(xoff, yoff, zoff, w, h, d);
1075 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1076 w, h, d, data, data.length * 4);
1077 }
1078
1079 /**
1080 * @hide
1081 *
1082 */
1083 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
1084 mRS.validate();
1085 validate3DRange(xoff, yoff, zoff, w, h, d);
1086 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1087 w, h, d, data, data.length * 4);
1088 }
1089
1090
1091 /**
1092 * @hide
1093 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001094 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001095 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001096 * @param xoff X offset of the region to update in this Allocation
1097 * @param yoff Y offset of the region to update in this Allocation
1098 * @param zoff Z offset of the region to update in this Allocation
1099 * @param w Width of the region to update
1100 * @param h Height of the region to update
1101 * @param d Depth of the region to update
Jason Samsb05d6892013-04-09 15:59:24 -07001102 * @param data to be placed into the allocation
1103 */
1104 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1105 validateIsInt8();
1106 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1107 }
1108
1109 /**
1110 * @hide
1111 *
1112 */
1113 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1114 validateIsInt16();
1115 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1116 }
1117
1118 /**
1119 * @hide
1120 *
1121 */
1122 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
1123 validateIsInt32();
1124 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1125 }
1126
1127 /**
1128 * @hide
1129 *
1130 */
1131 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
1132 validateIsFloat32();
1133 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1134 }
1135
1136 /**
1137 * @hide
1138 * Copy a rectangular region into the allocation from another
1139 * allocation.
1140 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001141 * @param xoff X offset of the region to update in this Allocation
1142 * @param yoff Y offset of the region to update in this Allocation
1143 * @param zoff Z offset of the region to update in this Allocation
1144 * @param w Width of the region to update.
1145 * @param h Height of the region to update.
1146 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001147 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001148 * @param dataXoff X offset of the region in the source Allocation
1149 * @param dataYoff Y offset of the region in the source Allocation
1150 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001151 */
1152 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1153 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1154 mRS.validate();
1155 validate3DRange(xoff, yoff, zoff, w, h, d);
1156 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1157 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1158 data.mSelectedLOD);
1159 }
1160
Jason Samsfa445b92011-01-07 17:00:07 -08001161
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001162 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001163 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1164 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001165 *
1166 * @param b The bitmap to be set from the Allocation.
1167 */
Jason Samsfa445b92011-01-07 17:00:07 -08001168 public void copyTo(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -08001169 mRS.validate();
1170 validateBitmapFormat(b);
1171 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -07001172 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Jason Samsfa445b92011-01-07 17:00:07 -08001173 }
1174
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001175 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001176 * Copy from the Allocation into a byte array. The array must be at least
1177 * as large as the Allocation. The allocation must be of an 8 bit integer
1178 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001179 *
1180 * @param d The array to be set from the Allocation.
1181 */
Jason Samsfa445b92011-01-07 17:00:07 -08001182 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001183 validateIsInt8();
Jason Sams771bebb2009-12-07 12:40:12 -08001184 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001185 mRS.nAllocationRead(getID(mRS), d);
Jason Sams40a29e82009-08-10 14:55:26 -07001186 }
1187
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001188 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001189 * Copy from the Allocation into a short array. The array must be at least
1190 * as large as the Allocation. The allocation must be of an 16 bit integer
1191 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001192 *
1193 * @param d The array to be set from the Allocation.
1194 */
Jason Samsfa445b92011-01-07 17:00:07 -08001195 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001196 validateIsInt16();
Jason Samsfa445b92011-01-07 17:00:07 -08001197 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001198 mRS.nAllocationRead(getID(mRS), d);
Jason Samsfa445b92011-01-07 17:00:07 -08001199 }
1200
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001201 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001202 * Copy from the Allocation into a int array. The array must be at least as
1203 * large as the Allocation. The allocation must be of an 32 bit integer
1204 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001205 *
1206 * @param d The array to be set from the Allocation.
1207 */
Jason Samsfa445b92011-01-07 17:00:07 -08001208 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001209 validateIsInt32();
Jason Samsfa445b92011-01-07 17:00:07 -08001210 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001211 mRS.nAllocationRead(getID(mRS), d);
Jason Samsfa445b92011-01-07 17:00:07 -08001212 }
1213
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001214 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001215 * Copy from the Allocation into a float array. The array must be at least
1216 * as large as the Allocation. The allocation must be of an 32 bit float
1217 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001218 *
1219 * @param d The array to be set from the Allocation.
1220 */
Jason Samsfa445b92011-01-07 17:00:07 -08001221 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001222 validateIsFloat32();
Jason Sams771bebb2009-12-07 12:40:12 -08001223 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001224 mRS.nAllocationRead(getID(mRS), d);
Jason Sams40a29e82009-08-10 14:55:26 -07001225 }
1226
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001227 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001228 * Resize a 1D allocation. The contents of the allocation are preserved.
1229 * If new elements are allocated objects are created with null contents and
1230 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001231 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001232 * <p>If the new region is smaller the references of any objects outside the
1233 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001234 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001235 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001236 *
1237 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001238 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001239 * @deprecated RenderScript objects should be immutable once created. The
1240 * replacement is to create a new allocation and copy the contents.
Jason Samsf7086092011-01-12 13:28:37 -08001241 */
Jason Sams31a7e422010-10-26 13:09:17 -07001242 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001243 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001244 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001245 }
Jason Samse07694b2012-04-03 15:36:36 -07001246 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001247 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001248
Jason Samse07694b2012-04-03 15:36:36 -07001249 int typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001250 mType = new Type(typeID, mRS);
1251 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001252 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001253 }
1254
Jason Samsb8c5a842009-07-31 20:40:47 -07001255
1256 // creation
1257
Jason Sams49a05d72010-12-29 14:31:29 -08001258 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001259 static {
1260 mBitmapOptions.inScaled = false;
1261 }
1262
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001263 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001264 * Creates a new Allocation with the given {@link
1265 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001266 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001267 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001268 * @param mips specifies desired mipmap behaviour for the
1269 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001270 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001271 * utilized
1272 */
1273 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001274 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001275 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001276 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001277 }
Jason Samse07694b2012-04-03 15:36:36 -07001278 int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001279 if (id == 0) {
1280 throw new RSRuntimeException("Allocation creation failed.");
1281 }
1282 return new Allocation(id, rs, type, usage);
1283 }
1284
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001285 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001286 * Creates an Allocation with the size specified by the type and no mipmaps
1287 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001288 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001289 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001290 * @param type renderscript type describing data layout
1291 * @param usage bit field specifying how the allocation is
1292 * utilized
1293 *
1294 * @return allocation
1295 */
Jason Samse5d37122010-12-16 00:33:33 -08001296 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1297 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1298 }
1299
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001300 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001301 * Creates an Allocation for use by scripts with a given {@link
1302 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001303 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001304 * @param rs Context to which the Allocation will belong.
1305 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001306 *
1307 * @return allocation
1308 */
Jason Sams5476b452010-12-08 16:14:36 -08001309 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001310 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001311 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001312
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001313 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001314 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001315 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001316 * @param rs Context to which the Allocation will belong.
1317 * @param e Element to use in the Allocation
1318 * @param count the number of Elements in the Allocation
1319 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001320 * utilized
1321 *
1322 * @return allocation
1323 */
Jason Sams5476b452010-12-08 16:14:36 -08001324 static public Allocation createSized(RenderScript rs, Element e,
1325 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001326 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001327 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001328 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001329 Type t = b.create();
1330
Jason Samse07694b2012-04-03 15:36:36 -07001331 int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001332 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001333 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001334 }
Jason Sams5476b452010-12-08 16:14:36 -08001335 return new Allocation(id, rs, t, usage);
1336 }
1337
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001338 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001339 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001340 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001341 * @param rs Context to which the Allocation will belong.
1342 * @param e Element to use in the Allocation
1343 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001344 *
1345 * @return allocation
1346 */
Jason Sams5476b452010-12-08 16:14:36 -08001347 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001348 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001349 }
1350
Jason Sams49a05d72010-12-29 14:31:29 -08001351 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001352 final Bitmap.Config bc = b.getConfig();
1353 if (bc == Bitmap.Config.ALPHA_8) {
1354 return Element.A_8(rs);
1355 }
1356 if (bc == Bitmap.Config.ARGB_4444) {
1357 return Element.RGBA_4444(rs);
1358 }
1359 if (bc == Bitmap.Config.ARGB_8888) {
1360 return Element.RGBA_8888(rs);
1361 }
1362 if (bc == Bitmap.Config.RGB_565) {
1363 return Element.RGB_565(rs);
1364 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001365 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001366 }
1367
Jason Sams49a05d72010-12-29 14:31:29 -08001368 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001369 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001370 Element e = elementFromBitmap(rs, b);
1371 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001372 tb.setX(b.getWidth());
1373 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001374 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001375 return tb.create();
1376 }
1377
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001378 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001379 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001380 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001381 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001382 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001383 * @param mips specifies desired mipmap behaviour for the
1384 * allocation
1385 * @param usage bit field specifying how the allocation is
1386 * utilized
1387 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001388 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001389 *
1390 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001391 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001392 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001393 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001394 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001395
1396 // WAR undocumented color formats
1397 if (b.getConfig() == null) {
1398 if ((usage & USAGE_SHARED) != 0) {
1399 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1400 }
1401 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1402 Canvas c = new Canvas(newBitmap);
1403 c.drawBitmap(b, 0, 0, null);
1404 return createFromBitmap(rs, newBitmap, mips, usage);
1405 }
1406
Jason Sams5476b452010-12-08 16:14:36 -08001407 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001408
Tim Murraya3145512012-12-04 17:59:29 -08001409 // enable optimized bitmap path only with no mipmap and script-only usage
1410 if (mips == MipmapControl.MIPMAP_NONE &&
1411 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Tim Murray78e64942013-04-09 17:28:56 -07001412 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
Tim Murraya3145512012-12-04 17:59:29 -08001413 int id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
1414 if (id == 0) {
1415 throw new RSRuntimeException("Load failed.");
1416 }
1417
1418 // keep a reference to the Bitmap around to prevent GC
1419 Allocation alloc = new Allocation(id, rs, t, usage);
1420 alloc.setBitmap(b);
1421 return alloc;
1422 }
1423
Jason Sams9bf18922013-04-13 19:48:36 -07001424
Jason Samse07694b2012-04-03 15:36:36 -07001425 int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001426 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001427 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001428 }
Jason Sams5476b452010-12-08 16:14:36 -08001429 return new Allocation(id, rs, t, usage);
1430 }
1431
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001432 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001433 * Returns the handle to a raw buffer that is being managed by the screen
1434 * compositor. This operation is only valid for Allocations with {@link
1435 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001436 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001437 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001438 *
1439 */
1440 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08001441 if ((mUsage & USAGE_IO_INPUT) == 0) {
1442 throw new RSInvalidStateException("Allocation is not a surface texture.");
1443 }
1444 return mRS.nAllocationGetSurface(getID(mRS));
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001445 }
1446
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001447 /**
Jason Samsc089c2f2013-02-22 13:57:36 -08001448 * @hide
1449 */
1450 public void setSurfaceTexture(SurfaceTexture st) {
1451 setSurface(new Surface(st));
1452 }
1453
1454 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001455 * Associate a {@link android.view.Surface} with this Allocation. This
1456 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001457 *
1458 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001459 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001460 public void setSurface(Surface sur) {
1461 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001462 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1463 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1464 }
1465
Jason Samse07694b2012-04-03 15:36:36 -07001466 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001467 }
1468
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001469 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001470 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08001471 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001472 * <p>With target API version 18 or greater, this Allocation will be created
1473 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
1474 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
1475 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001476 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001477 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001478 * @param b bitmap source for the allocation data
1479 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001480 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001481 *
1482 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001483 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08001484 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1485 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07001486 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08001487 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001488 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1489 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001490 }
1491
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001492 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001493 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1494 * containing the horizontal list of cube faces. Each face must be a square,
1495 * have the same size as all other faces, and have a width that is a power
1496 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001497 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001498 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001499 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001500 * format: right, left, top, bottom, front, back
1501 * @param mips specifies desired mipmap behaviour for the cubemap
1502 * @param usage bit field specifying how the cubemap is utilized
1503 *
1504 * @return allocation containing cubemap data
1505 *
1506 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001507 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001508 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001509 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001510 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001511
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001512 int height = b.getHeight();
1513 int width = b.getWidth();
1514
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001515 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001516 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1517 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001518 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001519 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001520 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001521 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001522 if (!isPow2) {
1523 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1524 }
1525
1526 Element e = elementFromBitmap(rs, b);
1527 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001528 tb.setX(height);
1529 tb.setY(height);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001530 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001531 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001532 Type t = tb.create();
1533
Jason Samse07694b2012-04-03 15:36:36 -07001534 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001535 if(id == 0) {
1536 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1537 }
Jason Sams5476b452010-12-08 16:14:36 -08001538 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001539 }
1540
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001541 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001542 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
1543 * from a {@link android.graphics.Bitmap} containing the horizontal list of
1544 * cube faces. Each face must be a square, have the same size as all other
1545 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001546 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001547 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001548 * @param b bitmap with cubemap faces layed out in the following
1549 * format: right, left, top, bottom, front, back
1550 *
1551 * @return allocation containing cubemap data
1552 *
1553 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001554 static public Allocation createCubemapFromBitmap(RenderScript rs,
1555 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001556 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001557 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001558 }
1559
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001560 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001561 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
1562 * objects containing the cube faces. Each face must be a square, have the
1563 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001564 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001565 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001566 * @param xpos cubemap face in the positive x direction
1567 * @param xneg cubemap face in the negative x direction
1568 * @param ypos cubemap face in the positive y direction
1569 * @param yneg cubemap face in the negative y direction
1570 * @param zpos cubemap face in the positive z direction
1571 * @param zneg cubemap face in the negative z direction
1572 * @param mips specifies desired mipmap behaviour for the cubemap
1573 * @param usage bit field specifying how the cubemap is utilized
1574 *
1575 * @return allocation containing cubemap data
1576 *
1577 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001578 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1579 Bitmap xpos,
1580 Bitmap xneg,
1581 Bitmap ypos,
1582 Bitmap yneg,
1583 Bitmap zpos,
1584 Bitmap zneg,
1585 MipmapControl mips,
1586 int usage) {
1587 int height = xpos.getHeight();
1588 if (xpos.getWidth() != height ||
1589 xneg.getWidth() != height || xneg.getHeight() != height ||
1590 ypos.getWidth() != height || ypos.getHeight() != height ||
1591 yneg.getWidth() != height || yneg.getHeight() != height ||
1592 zpos.getWidth() != height || zpos.getHeight() != height ||
1593 zneg.getWidth() != height || zneg.getHeight() != height) {
1594 throw new RSIllegalArgumentException("Only square cube map faces supported");
1595 }
1596 boolean isPow2 = (height & (height - 1)) == 0;
1597 if (!isPow2) {
1598 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1599 }
1600
1601 Element e = elementFromBitmap(rs, xpos);
1602 Type.Builder tb = new Type.Builder(rs, e);
1603 tb.setX(height);
1604 tb.setY(height);
1605 tb.setFaces(true);
1606 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1607 Type t = tb.create();
1608 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1609
1610 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001611 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001612 adapter.copyFrom(xpos);
1613 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1614 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001615 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001616 adapter.copyFrom(ypos);
1617 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1618 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001619 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001620 adapter.copyFrom(zpos);
1621 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1622 adapter.copyFrom(zneg);
1623
1624 return cubemap;
1625 }
1626
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001627 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001628 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
1629 * from 6 {@link android.graphics.Bitmap} objects containing the cube
1630 * faces. Each face must be a square, have the same size as all other faces,
1631 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001632 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001633 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001634 * @param xpos cubemap face in the positive x direction
1635 * @param xneg cubemap face in the negative x direction
1636 * @param ypos cubemap face in the positive y direction
1637 * @param yneg cubemap face in the negative y direction
1638 * @param zpos cubemap face in the positive z direction
1639 * @param zneg cubemap face in the negative z direction
1640 *
1641 * @return allocation containing cubemap data
1642 *
1643 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001644 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1645 Bitmap xpos,
1646 Bitmap xneg,
1647 Bitmap ypos,
1648 Bitmap yneg,
1649 Bitmap zpos,
1650 Bitmap zneg) {
1651 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1652 zpos, zneg, MipmapControl.MIPMAP_NONE,
1653 USAGE_GRAPHICS_TEXTURE);
1654 }
1655
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001656 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001657 * Creates an Allocation from the Bitmap referenced
1658 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001659 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001660 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001661 * @param res application resources
1662 * @param id resource id to load the data from
1663 * @param mips specifies desired mipmap behaviour for the
1664 * allocation
1665 * @param usage bit field specifying how the allocation is
1666 * utilized
1667 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001668 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001669 *
1670 */
Jason Sams5476b452010-12-08 16:14:36 -08001671 static public Allocation createFromBitmapResource(RenderScript rs,
1672 Resources res,
1673 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001674 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001675 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001676
Jason Sams771bebb2009-12-07 12:40:12 -08001677 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07001678 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
1679 throw new RSIllegalArgumentException("Unsupported usage specified.");
1680 }
Jason Sams5476b452010-12-08 16:14:36 -08001681 Bitmap b = BitmapFactory.decodeResource(res, id);
1682 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1683 b.recycle();
1684 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001685 }
1686
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001687 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001688 * Creates a non-mipmapped Allocation to use as a graphics texture from the
1689 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001690 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001691 * <p>With target API version 18 or greater, this allocation will be created
1692 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
1693 * target API version 17 or lower, this allocation will be created with
1694 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08001695 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001696 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001697 * @param res application resources
1698 * @param id resource id to load the data from
1699 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001700 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001701 *
1702 */
Jason Sams5476b452010-12-08 16:14:36 -08001703 static public Allocation createFromBitmapResource(RenderScript rs,
1704 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001705 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08001706 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1707 return createFromBitmapResource(rs, res, id,
1708 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07001709 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08001710 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001711 return createFromBitmapResource(rs, res, id,
1712 MipmapControl.MIPMAP_NONE,
1713 USAGE_GRAPHICS_TEXTURE);
1714 }
1715
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001716 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001717 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001718 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001719 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001720 * @param str string to create the allocation from
1721 * @param usage bit field specifying how the allocaiton is
1722 * utilized
1723 *
1724 */
Jason Sams5476b452010-12-08 16:14:36 -08001725 static public Allocation createFromString(RenderScript rs,
1726 String str,
1727 int usage) {
1728 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001729 byte[] allocArray = null;
1730 try {
1731 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001732 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001733 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001734 return alloc;
1735 }
1736 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001737 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001738 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001739 }
Jason Sams739c8262013-04-11 18:07:52 -07001740
1741 /**
Jason Samsf64cca92013-04-19 12:56:37 -07001742 * @hide
1743 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001744 * Interface to handle notification when new buffers are available via
1745 * {@link #USAGE_IO_INPUT}. An application will receive one notification
1746 * when a buffer is available. Additional buffers will not trigger new
1747 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07001748 */
1749 public interface IoInputNotifier {
1750 public void onBufferAvailable(Allocation a);
1751 }
1752
1753 /**
Jason Samsf64cca92013-04-19 12:56:37 -07001754 * @hide
1755 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001756 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07001757 *
Jason Samsc876cc42013-04-11 20:22:31 -07001758 * @param callback instance of the IoInputNotifier class to be called
Jason Sams739c8262013-04-11 18:07:52 -07001759 * when buffer arrive.
1760 */
1761 public void setIoInputNotificationHandler(IoInputNotifier callback) {
1762 synchronized(mAllocationMap) {
1763 mAllocationMap.put(new Integer(getID(mRS)), this);
1764 mBufferNotifier = callback;
1765 }
1766 }
1767
1768 static void sendBufferNotification(int id) {
1769 synchronized(mAllocationMap) {
1770 Allocation a = mAllocationMap.get(new Integer(id));
1771
1772 if ((a != null) && (a.mBufferNotifier != null)) {
1773 a.mBufferNotifier.onBufferAvailable(a);
1774 }
1775 }
1776 }
1777
Jason Samsb8c5a842009-07-31 20:40:47 -07001778}
1779