blob: f253ed82b9aad6c1b223292e067bbd3133df78ed [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
Stephen Hines9069ee82012-02-13 18:25:54 -08002 * Copyright (C) 2008-2012 The Android Open Source Project
Jason Samsb8c5a842009-07-31 20:40:47 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
Jason Sams739c8262013-04-11 18:07:52 -070019import java.util.HashMap;
Jason Samsb8c5a842009-07-31 20:40:47 -070020import android.content.res.Resources;
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
Jason Samsfb9aa9f2012-03-28 15:30:07 -070023import android.view.Surface;
Jason Samsb8c5a842009-07-31 20:40:47 -070024import android.util.Log;
Tim Murrayabd5db92013-02-28 11:45:22 -080025import android.graphics.Canvas;
Tim Murray6d7a53c2013-05-23 16:59:23 -070026import android.os.Trace;
Jason Samsb8c5a842009-07-31 20:40:47 -070027
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070028/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070029 * <p> This class provides the primary method through which data is passed to
30 * and from RenderScript kernels. An Allocation provides the backing store for
31 * a given {@link android.renderscript.Type}. </p>
Jason Samsa23d4e72011-01-04 18:59:12 -080032 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070033 * <p>An Allocation also contains a set of usage flags that denote how the
34 * Allocation could be used. For example, an Allocation may have usage flags
35 * specifying that it can be used from a script as well as input to a {@link
36 * android.renderscript.Sampler}. A developer must synchronize across these
37 * different usages using {@link android.renderscript.Allocation#syncAll} in
38 * order to ensure that different users of the Allocation have a consistent view
39 * of memory. For example, in the case where an Allocation is used as the output
40 * of one kernel and as Sampler input in a later kernel, a developer must call
41 * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
42 * second kernel to ensure correctness.
Jason Samsa23d4e72011-01-04 18:59:12 -080043 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070044 * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
45 * more complex Element types, the {@link #copyFromUnchecked} methods can be
46 * used to copy from byte arrays or similar constructs.</p>
Jason Samsb8c5a842009-07-31 20:40:47 -070047 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080048 * <div class="special reference">
49 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070050 * <p>For more information about creating an application that uses RenderScript, read the
51 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080052 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070053 **/
54public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070055 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080056 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080057 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070058 Allocation mAdaptedAllocation;
Tim Murray2f2472c2013-08-22 14:55:26 -070059 int mSize;
Jason Samsba862d12011-07-07 15:24:42 -070060
61 boolean mConstrainedLOD;
62 boolean mConstrainedFace;
63 boolean mConstrainedY;
64 boolean mConstrainedZ;
Jason Sams615e7ce2012-01-13 14:01:20 -080065 boolean mReadAllowed = true;
66 boolean mWriteAllowed = true;
Jason Samsba862d12011-07-07 15:24:42 -070067 int mSelectedY;
68 int mSelectedZ;
69 int mSelectedLOD;
70 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
71
72 int mCurrentDimX;
73 int mCurrentDimY;
74 int mCurrentDimZ;
75 int mCurrentCount;
Tim Murray460a0492013-11-19 12:45:54 -080076 static HashMap<Long, Allocation> mAllocationMap =
77 new HashMap<Long, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070078 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070079
Tim Murrayc11e25c2013-04-09 11:01:01 -070080 /**
81 * The usage of the Allocation. These signal to RenderScript where to place
82 * the Allocation in memory.
83 *
84 */
Jason Sams5476b452010-12-08 16:14:36 -080085
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070086 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -070087 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -080088 */
Jason Sams5476b452010-12-08 16:14:36 -080089 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -080090
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070091 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -070092 * The Allocation will be used as a texture source by one or more graphics
93 * programs.
Jason Samsf7086092011-01-12 13:28:37 -080094 *
95 */
Jason Sams5476b452010-12-08 16:14:36 -080096 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -080097
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070098 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -070099 * The Allocation will be used as a graphics mesh.
100 *
101 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800102 *
103 */
Jason Sams5476b452010-12-08 16:14:36 -0800104 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800105
106
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700107 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700108 * The Allocation will be used as the source of shader constants by one or
109 * more programs.
110 *
111 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800112 *
113 */
Jason Sams5476b452010-12-08 16:14:36 -0800114 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
115
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700116 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700117 * The Allocation will be used as a target for offscreen rendering
118 *
119 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700120 *
121 */
122 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
123
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700124 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700125 * The Allocation will be used as a {@link android.view.Surface}
126 * consumer. This usage will cause the Allocation to be created
127 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800128 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800129 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700130 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800131
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700132 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700133 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700134 * producer. The dimensions and format of the {@link
Jason Sams3a1b8e42013-09-24 15:18:52 -0700135 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700136 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800137 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800138 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700139 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700140
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700141 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700142 * The Allocation's backing store will be inherited from another object
143 * (usually a {@link android.graphics.Bitmap}); copying to or from the
144 * original source Bitmap will cause a synchronization rather than a full
145 * copy. {@link #syncAll} may also be used to synchronize the Allocation
146 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800147 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700148 * <p>This is set by default for allocations created with {@link
149 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800150 *
151 */
152 public static final int USAGE_SHARED = 0x0080;
153
154 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700155 * Controls mipmap behavior when using the bitmap creation and update
156 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800157 */
Jason Sams4ef66502010-12-10 16:03:15 -0800158 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700159 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700160 * No mipmaps will be generated and the type generated from the incoming
161 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800162 */
Jason Sams5476b452010-12-08 16:14:36 -0800163 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800164
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700165 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700166 * A full mipmap chain will be created in script memory. The Type of
167 * the Allocation will contain a full mipmap chain. On upload, the full
168 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800169 */
Jason Sams5476b452010-12-08 16:14:36 -0800170 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800171
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700172 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700173 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
174 * not contain mipmaps. On upload, the allocation data will contain a
175 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800176 */
Jason Sams5476b452010-12-08 16:14:36 -0800177 MIPMAP_ON_SYNC_TO_TEXTURE(2);
178
179 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800180 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800181 mID = id;
182 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700183 }
184
Jason Sams48fe5342011-07-08 13:52:30 -0700185
Tim Murray460a0492013-11-19 12:45:54 -0800186 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700187 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700188 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700189 }
Jason Samse07694b2012-04-03 15:36:36 -0700190 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700191 }
192
Jason Sams03d2d002012-03-23 13:51:56 -0700193
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700194 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700195 * Get the {@link android.renderscript.Element} of the {@link
196 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700197 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700198 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700199 *
200 */
201 public Element getElement() {
202 return mType.getElement();
203 }
204
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700205 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700206 * Get the usage flags of the Allocation.
207 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700208 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700209 *
210 */
211 public int getUsage() {
212 return mUsage;
213 }
214
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700215 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700216 * Get the size of the Allocation in bytes.
217 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700218 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700219 *
220 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700221 public int getBytesSize() {
222 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700223 }
224
Jason Sams452a7662011-07-07 16:05:18 -0700225 private void updateCacheInfo(Type t) {
226 mCurrentDimX = t.getX();
227 mCurrentDimY = t.getY();
228 mCurrentDimZ = t.getZ();
229 mCurrentCount = mCurrentDimX;
230 if (mCurrentDimY > 1) {
231 mCurrentCount *= mCurrentDimY;
232 }
233 if (mCurrentDimZ > 1) {
234 mCurrentCount *= mCurrentDimZ;
235 }
236 }
Jason Samsba862d12011-07-07 15:24:42 -0700237
Tim Murraya3145512012-12-04 17:59:29 -0800238 private void setBitmap(Bitmap b) {
239 mBitmap = b;
240 }
241
Tim Murray460a0492013-11-19 12:45:54 -0800242 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700243 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800244 if ((usage & ~(USAGE_SCRIPT |
245 USAGE_GRAPHICS_TEXTURE |
246 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700247 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800248 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800249 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800250 USAGE_IO_OUTPUT |
251 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800252 throw new RSIllegalArgumentException("Unknown usage specified.");
253 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800254
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700255 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800256 mWriteAllowed = false;
257
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700258 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800259 USAGE_GRAPHICS_TEXTURE |
260 USAGE_SCRIPT)) != 0) {
261 throw new RSIllegalArgumentException("Invalid usage combination.");
262 }
263 }
Jason Sams9bf18922013-04-13 19:48:36 -0700264
Jason Sams5476b452010-12-08 16:14:36 -0800265 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800266 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700267
Jason Sams452a7662011-07-07 16:05:18 -0700268 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700269 // TODO: A3D doesn't have Type info during creation, so we can't
270 // calculate the size ahead of time. We can possibly add a method
271 // to update the size in the future if it seems reasonable.
272 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700273 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700274 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700275 try {
276 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
277 } catch (Exception e) {
278 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
279 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
280 }
281 }
282
283 protected void finalize() throws Throwable {
284 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
285 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700286 }
287
Jason Samsb97b2512011-01-16 15:04:08 -0800288 private void validateIsInt32() {
289 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
290 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
291 return;
292 }
293 throw new RSIllegalArgumentException(
294 "32 bit integer source does not match allocation type " + mType.mElement.mType);
295 }
296
297 private void validateIsInt16() {
298 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
299 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
300 return;
301 }
302 throw new RSIllegalArgumentException(
303 "16 bit integer source does not match allocation type " + mType.mElement.mType);
304 }
305
306 private void validateIsInt8() {
307 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
308 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
309 return;
310 }
311 throw new RSIllegalArgumentException(
312 "8 bit integer source does not match allocation type " + mType.mElement.mType);
313 }
314
315 private void validateIsFloat32() {
316 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
317 return;
318 }
319 throw new RSIllegalArgumentException(
320 "32 bit float source does not match allocation type " + mType.mElement.mType);
321 }
322
323 private void validateIsObject() {
324 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
325 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
326 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
327 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
328 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
329 (mType.mElement.mType == Element.DataType.RS_MESH) ||
330 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
331 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
332 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
333 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
334 return;
335 }
336 throw new RSIllegalArgumentException(
337 "Object source does not match allocation type " + mType.mElement.mType);
338 }
339
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700340 @Override
341 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800342 super.updateFromNative();
Tim Murray460a0492013-11-19 12:45:54 -0800343 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700344 if(typeID != 0) {
345 mType = new Type(typeID, mRS);
346 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700347 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700348 }
349 }
350
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700351 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700352 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700353 *
354 * @return Type
355 *
356 */
Jason Samsea87e962010-01-12 12:12:28 -0800357 public Type getType() {
358 return mType;
359 }
360
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700361 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700362 * Propagate changes from one usage of the Allocation to the
363 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700364 *
365 */
Jason Sams5476b452010-12-08 16:14:36 -0800366 public void syncAll(int srcLocation) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700367 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
Jason Sams5476b452010-12-08 16:14:36 -0800368 switch (srcLocation) {
Jason Sams5476b452010-12-08 16:14:36 -0800369 case USAGE_GRAPHICS_TEXTURE:
Tim Murray78e64942013-04-09 17:28:56 -0700370 case USAGE_SCRIPT:
371 if ((mUsage & USAGE_SHARED) != 0) {
372 copyFrom(mBitmap);
373 }
374 break;
375 case USAGE_GRAPHICS_CONSTANTS:
Jason Sams5476b452010-12-08 16:14:36 -0800376 case USAGE_GRAPHICS_VERTEX:
377 break;
Tim Murray78e64942013-04-09 17:28:56 -0700378 case USAGE_SHARED:
379 if ((mUsage & USAGE_SHARED) != 0) {
380 copyTo(mBitmap);
381 }
382 break;
Jason Sams5476b452010-12-08 16:14:36 -0800383 default:
384 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
385 }
386 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700387 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700388 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800389 }
390
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700391 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700392 * Send a buffer to the output stream. The contents of the Allocation will
393 * be undefined after this operation. This operation is only valid if {@link
394 * #USAGE_IO_OUTPUT} is set on the Allocation.
395 *
Jason Sams163766c2012-02-15 12:04:24 -0800396 *
Jason Sams163766c2012-02-15 12:04:24 -0800397 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700398 public void ioSend() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700399 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
Jason Sams163766c2012-02-15 12:04:24 -0800400 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
401 throw new RSIllegalArgumentException(
402 "Can only send buffer if IO_OUTPUT usage specified.");
403 }
404 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700405 mRS.nAllocationIoSend(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700406 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800407 }
408
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700409 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700410 * Receive the latest input into the Allocation. This operation
411 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800412 *
Jason Sams163766c2012-02-15 12:04:24 -0800413 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700414 public void ioReceive() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700415 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
Jason Sams163766c2012-02-15 12:04:24 -0800416 if ((mUsage & USAGE_IO_INPUT) == 0) {
417 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700418 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800419 }
420 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700421 mRS.nAllocationIoReceive(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700422 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800423 }
424
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700425 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700426 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700427 *
428 * @param d Source array.
429 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800430 public void copyFrom(BaseObj[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700431 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Sams771bebb2009-12-07 12:40:12 -0800432 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800433 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700434 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800435 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700436 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800437 }
Tim Murray460a0492013-11-19 12:45:54 -0800438 // FIXME: requires 64-bit path
439
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800440 int i[] = new int[d.length];
441 for (int ct=0; ct < d.length; ct++) {
Tim Murray460a0492013-11-19 12:45:54 -0800442 i[ct] = (int)d[ct].getID(mRS);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800443 }
Jason Samsba862d12011-07-07 15:24:42 -0700444 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700445 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700446 }
447
Jason Samsfb9f82c2011-01-12 14:53:25 -0800448 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800449 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800450 if (bc == null) {
451 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
452 }
Jason Sams252c0782011-01-11 17:42:52 -0800453 switch (bc) {
454 case ALPHA_8:
455 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
456 throw new RSIllegalArgumentException("Allocation kind is " +
457 mType.getElement().mKind + ", type " +
458 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700459 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800460 " bytes, passed bitmap was " + bc);
461 }
462 break;
463 case ARGB_8888:
464 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700465 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800466 throw new RSIllegalArgumentException("Allocation kind is " +
467 mType.getElement().mKind + ", type " +
468 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700469 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800470 " bytes, passed bitmap was " + bc);
471 }
472 break;
473 case RGB_565:
474 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700475 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800476 throw new RSIllegalArgumentException("Allocation kind is " +
477 mType.getElement().mKind + ", type " +
478 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700479 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800480 " bytes, passed bitmap was " + bc);
481 }
482 break;
483 case ARGB_4444:
484 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700485 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800486 throw new RSIllegalArgumentException("Allocation kind is " +
487 mType.getElement().mKind + ", type " +
488 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700489 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800490 " bytes, passed bitmap was " + bc);
491 }
492 break;
493
494 }
Jason Sams4ef66502010-12-10 16:03:15 -0800495 }
496
Jason Samsfb9f82c2011-01-12 14:53:25 -0800497 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700498 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800499 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
500 }
501 }
502
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700503 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700504 * Copy into this Allocation from an array. This method does not guarantee
505 * that the Allocation is compatible with the input buffer; it copies memory
506 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800507 *
508 * @param d the source data array
509 */
510 public void copyFromUnchecked(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700511 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800512 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700513 if (mCurrentDimZ > 0) {
514 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
515 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800516 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
517 } else {
518 copy1DRangeFromUnchecked(0, mCurrentCount, d);
519 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700520 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800521 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700522
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(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700531 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800532 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700533 if (mCurrentDimZ > 0) {
534 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
535 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800536 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
537 } else {
538 copy1DRangeFromUnchecked(0, mCurrentCount, d);
539 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700540 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800541 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700542
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700543 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700544 * Copy into this Allocation from an array. This method does not guarantee
545 * that the Allocation is compatible with the input buffer; it copies memory
546 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800547 *
548 * @param d the source data array
549 */
550 public void copyFromUnchecked(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700551 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800552 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700553 if (mCurrentDimZ > 0) {
554 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
555 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800556 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
557 } else {
558 copy1DRangeFromUnchecked(0, mCurrentCount, d);
559 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700560 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800561 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700562
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700563 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700564 * Copy into this Allocation from an array. This method does not guarantee
565 * that the Allocation is compatible with the input buffer; it copies memory
566 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800567 *
568 * @param d the source data array
569 */
570 public void copyFromUnchecked(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700571 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800572 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700573 if (mCurrentDimZ > 0) {
574 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
575 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800576 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
577 } else {
578 copy1DRangeFromUnchecked(0, mCurrentCount, d);
579 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700580 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800581 }
582
Tim Murray6d7a53c2013-05-23 16:59:23 -0700583
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700584 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700585 * Copy into this Allocation from an array. This variant is type checked
586 * and will generate exceptions if the Allocation's {@link
587 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800588 *
589 * @param d the source data array
590 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800591 public void copyFrom(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700592 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800593 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700594 if (mCurrentDimZ > 0) {
595 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
596 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800597 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
598 } else {
599 copy1DRangeFrom(0, mCurrentCount, d);
600 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700601 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800602 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800603
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700604 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700605 * Copy into this Allocation from an array. This variant is type checked
606 * and will generate exceptions if the Allocation's {@link
607 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800608 *
609 * @param d the source data array
610 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800611 public void copyFrom(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700612 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800613 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700614 if (mCurrentDimZ > 0) {
615 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
616 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800617 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
618 } else {
619 copy1DRangeFrom(0, mCurrentCount, d);
620 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700621 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800622 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800623
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700624 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700625 * Copy into this Allocation from an array. This variant is type checked
626 * and will generate exceptions if the Allocation's {@link
627 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800628 *
629 * @param d the source data array
630 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800631 public void copyFrom(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700632 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800633 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700634 if (mCurrentDimZ > 0) {
635 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
636 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800637 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
638 } else {
639 copy1DRangeFrom(0, mCurrentCount, d);
640 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700641 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800642 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800643
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700644 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700645 * Copy into this Allocation from an array. This variant is type checked
646 * and will generate exceptions if the Allocation's {@link
647 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800648 *
649 * @param d the source data array
650 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800651 public void copyFrom(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700652 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800653 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700654 if (mCurrentDimZ > 0) {
655 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
656 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800657 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
658 } else {
659 copy1DRangeFrom(0, mCurrentCount, d);
660 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700661 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800662 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800663
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700664 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700665 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
666 * height, width, and format of the bitmap must match the existing
667 * allocation.
668 *
669 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
670 * android.graphics.Bitmap} used to create the Allocation with {@link
671 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
672 * this will synchronize the Allocation with the latest data from the {@link
673 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800674 *
675 * @param b the source bitmap
676 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800677 public void copyFrom(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700678 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsfb9f82c2011-01-12 14:53:25 -0800679 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -0800680 if (b.getConfig() == null) {
681 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
682 Canvas c = new Canvas(newBitmap);
683 c.drawBitmap(b, 0, 0, null);
684 copyFrom(newBitmap);
685 return;
686 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800687 validateBitmapSize(b);
688 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700689 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700690 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700691 }
692
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700693 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700694 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700695 * must be identical.
696 *
697 * @param a the source allocation
698 */
699 public void copyFrom(Allocation a) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700700 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Tim Murrayf671fb02012-10-03 13:50:05 -0700701 mRS.validate();
702 if (!mType.equals(a.getType())) {
703 throw new RSIllegalArgumentException("Types of allocations must match.");
704 }
705 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700706 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700707 }
708
Tim Murrayf671fb02012-10-03 13:50:05 -0700709 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700710 * This is only intended to be used by auto-generated code reflected from
711 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800712 *
713 * @param xoff
714 * @param fp
715 */
Jason Sams21b41032011-01-16 15:05:41 -0800716 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800717 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700718 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700719 final byte[] data = fp.getData();
720
721 int count = data.length / eSize;
722 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800723 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700724 " not divisible by element size " + eSize + ".");
725 }
Jason Samsba862d12011-07-07 15:24:42 -0700726 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700727 }
728
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700729 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700730 * This is only intended to be used by auto-generated code reflected from
731 * the RenderScript script files.
Jason Samsfa445b92011-01-07 17:00:07 -0800732 *
733 * @param xoff
734 * @param component_number
735 * @param fp
736 */
Jason Sams21b41032011-01-16 15:05:41 -0800737 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800738 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700739 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800740 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700741 }
742 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800743 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700744 }
745
746 final byte[] data = fp.getData();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700747 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800748 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700749
750 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800751 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700752 " does not match component size " + eSize + ".");
753 }
754
Jason Sams48fe5342011-07-08 13:52:30 -0700755 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Jason Samsba862d12011-07-07 15:24:42 -0700756 component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700757 }
758
Jason Sams768bc022009-09-21 19:41:04 -0700759 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800760 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700761 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800762 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700763 }
764 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800765 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700766 }
Jason Samsba862d12011-07-07 15:24:42 -0700767 if((off + count) > mCurrentCount) {
768 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700769 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700770 }
Jason Samsba862d12011-07-07 15:24:42 -0700771 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800772 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700773 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700774 }
775
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700776 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700777 * Generate a mipmap chain. This is only valid if the Type of the Allocation
778 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800779 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700780 * <p>This function will generate a complete set of mipmaps from the top
781 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800782 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700783 * <p>If the Allocation is also using other memory spaces, a call to {@link
784 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800785 */
786 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700787 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800788 }
789
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700790 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700791 * Copy an array into part of this Allocation. This method does not
792 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800793 *
794 * @param off The offset of the first element to be copied.
795 * @param count The number of elements to be copied.
796 * @param d the source data array
797 */
798 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700799 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700800 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700801 data1DChecks(off, count, d.length * 4, dataSize);
Jason Samse729a942013-11-06 11:22:02 -0800802 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize, Element.DataType.SIGNED_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700803 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams768bc022009-09-21 19:41:04 -0700804 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700805
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 method does not
808 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800809 *
810 * @param off The offset of the first element to be copied.
811 * @param count The number of elements to be copied.
812 * @param d the source data array
813 */
814 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700815 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700816 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700817 data1DChecks(off, count, d.length * 2, dataSize);
Jason Samse729a942013-11-06 11:22:02 -0800818 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize, Element.DataType.SIGNED_16);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700819 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams768bc022009-09-21 19:41:04 -0700820 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700821
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700822 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700823 * Copy an array into part of this Allocation. This method does not
824 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800825 *
826 * @param off The offset of the first element to be copied.
827 * @param count The number of elements to be copied.
828 * @param d the source data array
829 */
830 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700831 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700832 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700833 data1DChecks(off, count, d.length, dataSize);
Jason Samse729a942013-11-06 11:22:02 -0800834 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize, Element.DataType.SIGNED_8);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700835 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams768bc022009-09-21 19:41:04 -0700836 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700837
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700838 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700839 * Copy an array into part of this Allocation. This method does not
840 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800841 *
842 * @param off The offset of the first element to be copied.
843 * @param count The number of elements to be copied.
844 * @param d the source data array
845 */
846 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700847 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700848 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700849 data1DChecks(off, count, d.length * 4, dataSize);
Jason Samse729a942013-11-06 11:22:02 -0800850 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize, Element.DataType.FLOAT_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700851 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700852 }
853
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700854 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700855 * Copy an array into part of this Allocation. This variant is type checked
856 * and will generate exceptions if the Allocation type is not a 32 bit
857 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800858 *
859 * @param off The offset of the first element to be copied.
860 * @param count The number of elements to be copied.
861 * @param d the source data array
862 */
Jason Samsb97b2512011-01-16 15:04:08 -0800863 public void copy1DRangeFrom(int off, int count, int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700864 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800865 validateIsInt32();
866 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700867 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800868 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800869
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700870 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700871 * Copy an array into part of this Allocation. This variant is type checked
872 * and will generate exceptions if the Allocation type is not a 16 bit
873 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800874 *
875 * @param off The offset of the first element to be copied.
876 * @param count The number of elements to be copied.
877 * @param d the source data array
878 */
Jason Samsb97b2512011-01-16 15:04:08 -0800879 public void copy1DRangeFrom(int off, int count, short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700880 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800881 validateIsInt16();
882 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700883 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800884 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800885
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700886 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700887 * Copy an array into part of this Allocation. This variant is type checked
888 * and will generate exceptions if the Allocation type is not an 8 bit
889 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800890 *
891 * @param off The offset of the first element to be copied.
892 * @param count The number of elements to be copied.
893 * @param d the source data array
894 */
Jason Samsb97b2512011-01-16 15:04:08 -0800895 public void copy1DRangeFrom(int off, int count, byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700896 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800897 validateIsInt8();
898 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700899 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800900 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800901
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700902 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700903 * Copy an array into part of this Allocation. This variant is type checked
904 * and will generate exceptions if the Allocation type is not a 32 bit float
905 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800906 *
907 * @param off The offset of the first element to be copied.
908 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700909 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800910 */
Jason Samsb97b2512011-01-16 15:04:08 -0800911 public void copy1DRangeFrom(int off, int count, float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700912 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800913 validateIsFloat32();
914 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700915 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800916 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700917 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700918 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700919 *
920 * @param off The offset of the first element to be copied.
921 * @param count The number of elements to be copied.
922 * @param data the source data allocation.
923 * @param dataOff off The offset of the first element in data to
924 * be copied.
925 */
926 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700927 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -0700928 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700929 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700930 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700931 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700932 }
933
Jason Samsfb9f82c2011-01-12 14:53:25 -0800934 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -0700935 if (mAdaptedAllocation != null) {
936
937 } else {
938
939 if (xoff < 0 || yoff < 0) {
940 throw new RSIllegalArgumentException("Offset cannot be negative.");
941 }
942 if (h < 0 || w < 0) {
943 throw new RSIllegalArgumentException("Height or width cannot be negative.");
944 }
945 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
946 throw new RSIllegalArgumentException("Updated region larger than allocation.");
947 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800948 }
949 }
Jason Sams768bc022009-09-21 19:41:04 -0700950
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800951 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, byte[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700952 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800953 mRS.validate();
954 validate2DRange(xoff, yoff, w, h);
955 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samse729a942013-11-06 11:22:02 -0800956 w, h, data, data.length, Element.DataType.SIGNED_8);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700957 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800958 }
959
960 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, short[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700961 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800962 mRS.validate();
963 validate2DRange(xoff, yoff, w, h);
964 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samse729a942013-11-06 11:22:02 -0800965 w, h, data, data.length * 2, Element.DataType.SIGNED_16);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700966 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800967 }
968
969 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, int[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700970 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800971 mRS.validate();
972 validate2DRange(xoff, yoff, w, h);
973 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samse729a942013-11-06 11:22:02 -0800974 w, h, data, data.length * 4, Element.DataType.SIGNED_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700975 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800976 }
977
978 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, float[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700979 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800980 mRS.validate();
981 validate2DRange(xoff, yoff, w, h);
982 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samse729a942013-11-06 11:22:02 -0800983 w, h, data, data.length * 4, Element.DataType.FLOAT_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700984 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800985 }
986
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700987 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700988 * Copy from an array into a rectangular region in this Allocation. The
989 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -0800990 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700991 * @param xoff X offset of the region to update in this Allocation
992 * @param yoff Y offset of the region to update in this Allocation
993 * @param w Width of the region to update
994 * @param h Height of the region to update
995 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -0800996 */
997 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700998 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -0800999 validateIsInt8();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001000 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001001 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001002 }
1003
Tim Murrayc11e25c2013-04-09 11:01:01 -07001004 /**
1005 * Copy from an array into a rectangular region in this Allocation. The
1006 * array is assumed to be tightly packed.
1007 *
1008 * @param xoff X offset of the region to update in this Allocation
1009 * @param yoff Y offset of the region to update in this Allocation
1010 * @param w Width of the region to update
1011 * @param h Height of the region to update
1012 * @param data to be placed into the Allocation
1013 */
Jason Samsf7086092011-01-12 13:28:37 -08001014 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001015 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001016 validateIsInt16();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001017 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001018 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001019 }
1020
Tim Murrayc11e25c2013-04-09 11:01:01 -07001021 /**
1022 * Copy from an array into a rectangular region in this Allocation. The
1023 * array is assumed to be tightly packed.
1024 *
1025 * @param xoff X offset of the region to update in this Allocation
1026 * @param yoff Y offset of the region to update in this Allocation
1027 * @param w Width of the region to update
1028 * @param h Height of the region to update
1029 * @param data to be placed into the Allocation
1030 */
Jason Samsf7086092011-01-12 13:28:37 -08001031 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001032 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001033 validateIsInt32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001034 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001035 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001036 }
1037
Tim Murrayc11e25c2013-04-09 11:01:01 -07001038 /**
1039 * Copy from an array into a rectangular region in this Allocation. The
1040 * array is assumed to be tightly packed.
1041 *
1042 * @param xoff X offset of the region to update in this Allocation
1043 * @param yoff Y offset of the region to update in this Allocation
1044 * @param w Width of the region to update
1045 * @param h Height of the region to update
1046 * @param data to be placed into the Allocation
1047 */
Jason Samsf7086092011-01-12 13:28:37 -08001048 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001049 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001050 validateIsFloat32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001051 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001052 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001053 }
1054
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001055 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001056 * Copy a rectangular region from an Allocation into a rectangular region in
1057 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001058 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001059 * @param xoff X offset of the region in this Allocation
1060 * @param yoff Y offset of the region in this Allocation
1061 * @param w Width of the region to update.
1062 * @param h Height of the region to update.
1063 * @param data source Allocation.
1064 * @param dataXoff X offset in source Allocation
1065 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001066 */
1067 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1068 Allocation data, int dataXoff, int dataYoff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001069 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001070 mRS.validate();
1071 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -07001072 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -07001073 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001074 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -07001075 data.mSelectedLOD, data.mSelectedFace.mID);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001076 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001077 }
1078
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001079 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001080 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1081 * and width of the update will use the height and width of the {@link
1082 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001083 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001084 * @param xoff X offset of the region to update in this Allocation
1085 * @param yoff Y offset of the region to update in this Allocation
1086 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001087 */
1088 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001089 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Jason Samsfa445b92011-01-07 17:00:07 -08001090 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001091 if (data.getConfig() == null) {
1092 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1093 Canvas c = new Canvas(newBitmap);
1094 c.drawBitmap(data, 0, 0, null);
1095 copy2DRangeFrom(xoff, yoff, newBitmap);
Jason Samsb05d6892013-04-09 15:59:24 -07001096 return;
Tim Murrayabd5db92013-02-28 11:45:22 -08001097 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001098 validateBitmapFormat(data);
1099 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -07001100 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001101 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001102 }
1103
Jason Samsb05d6892013-04-09 15:59:24 -07001104 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1105 if (mAdaptedAllocation != null) {
1106
1107 } else {
1108
1109 if (xoff < 0 || yoff < 0 || zoff < 0) {
1110 throw new RSIllegalArgumentException("Offset cannot be negative.");
1111 }
1112 if (h < 0 || w < 0 || d < 0) {
1113 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1114 }
1115 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1116 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1117 }
1118 }
1119 }
1120
1121 /**
1122 * @hide
1123 *
1124 */
1125 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1126 mRS.validate();
1127 validate3DRange(xoff, yoff, zoff, w, h, d);
1128 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Jason Samse729a942013-11-06 11:22:02 -08001129 w, h, d, data, data.length, Element.DataType.SIGNED_8);
Jason Samsb05d6892013-04-09 15:59:24 -07001130 }
1131
1132 /**
1133 * @hide
1134 *
1135 */
1136 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1137 mRS.validate();
1138 validate3DRange(xoff, yoff, zoff, w, h, d);
1139 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Jason Samse729a942013-11-06 11:22:02 -08001140 w, h, d, data, data.length * 2, Element.DataType.SIGNED_16);
Jason Samsb05d6892013-04-09 15:59:24 -07001141 }
1142
1143 /**
1144 * @hide
1145 *
1146 */
1147 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
1148 mRS.validate();
1149 validate3DRange(xoff, yoff, zoff, w, h, d);
1150 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Jason Samse729a942013-11-06 11:22:02 -08001151 w, h, d, data, data.length * 4, Element.DataType.SIGNED_32);
Jason Samsb05d6892013-04-09 15:59:24 -07001152 }
1153
1154 /**
1155 * @hide
1156 *
1157 */
1158 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
1159 mRS.validate();
1160 validate3DRange(xoff, yoff, zoff, w, h, d);
1161 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Jason Samse729a942013-11-06 11:22:02 -08001162 w, h, d, data, data.length * 4, Element.DataType.FLOAT_32);
Jason Samsb05d6892013-04-09 15:59:24 -07001163 }
1164
1165
1166 /**
1167 * @hide
1168 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001169 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001170 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001171 * @param xoff X offset of the region to update in this Allocation
1172 * @param yoff Y offset of the region to update in this Allocation
1173 * @param zoff Z offset of the region to update in this Allocation
1174 * @param w Width of the region to update
1175 * @param h Height of the region to update
1176 * @param d Depth of the region to update
Jason Samsb05d6892013-04-09 15:59:24 -07001177 * @param data to be placed into the allocation
1178 */
1179 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1180 validateIsInt8();
1181 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1182 }
1183
1184 /**
1185 * @hide
1186 *
1187 */
1188 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1189 validateIsInt16();
1190 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1191 }
1192
1193 /**
1194 * @hide
1195 *
1196 */
1197 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
1198 validateIsInt32();
1199 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1200 }
1201
1202 /**
1203 * @hide
1204 *
1205 */
1206 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
1207 validateIsFloat32();
1208 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1209 }
1210
1211 /**
1212 * @hide
1213 * Copy a rectangular region into the allocation from another
1214 * allocation.
1215 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001216 * @param xoff X offset of the region to update in this Allocation
1217 * @param yoff Y offset of the region to update in this Allocation
1218 * @param zoff Z offset of the region to update in this Allocation
1219 * @param w Width of the region to update.
1220 * @param h Height of the region to update.
1221 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001222 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001223 * @param dataXoff X offset of the region in the source Allocation
1224 * @param dataYoff Y offset of the region in the source Allocation
1225 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001226 */
1227 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1228 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1229 mRS.validate();
1230 validate3DRange(xoff, yoff, zoff, w, h, d);
1231 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1232 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1233 data.mSelectedLOD);
1234 }
1235
Jason Samsfa445b92011-01-07 17:00:07 -08001236
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001237 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001238 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1239 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001240 *
1241 * @param b The bitmap to be set from the Allocation.
1242 */
Jason Samsfa445b92011-01-07 17:00:07 -08001243 public void copyTo(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001244 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsfb9f82c2011-01-12 14:53:25 -08001245 mRS.validate();
1246 validateBitmapFormat(b);
1247 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -07001248 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001249 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001250 }
1251
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001252 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001253 * Copy from the Allocation into a byte array. The array must be at least
1254 * as large as the Allocation. The allocation must be of an 8 bit integer
1255 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001256 *
1257 * @param d The array to be set from the Allocation.
1258 */
Jason Samsfa445b92011-01-07 17:00:07 -08001259 public void copyTo(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001260 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001261 validateIsInt8();
Jason Sams771bebb2009-12-07 12:40:12 -08001262 mRS.validate();
Jason Sams21659ac2013-11-06 15:08:07 -08001263 mRS.nAllocationRead(getID(mRS), d, Element.DataType.SIGNED_8);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001264 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams40a29e82009-08-10 14:55:26 -07001265 }
1266
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001267 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001268 * Copy from the Allocation into a short array. The array must be at least
1269 * as large as the Allocation. The allocation must be of an 16 bit integer
1270 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001271 *
1272 * @param d The array to be set from the Allocation.
1273 */
Jason Samsfa445b92011-01-07 17:00:07 -08001274 public void copyTo(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001275 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001276 validateIsInt16();
Jason Samsfa445b92011-01-07 17:00:07 -08001277 mRS.validate();
Jason Sams21659ac2013-11-06 15:08:07 -08001278 mRS.nAllocationRead(getID(mRS), d, Element.DataType.SIGNED_16);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001279 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001280 }
1281
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001282 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001283 * Copy from the Allocation into a int array. The array must be at least as
1284 * large as the Allocation. The allocation must be of an 32 bit integer
1285 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001286 *
1287 * @param d The array to be set from the Allocation.
1288 */
Jason Samsfa445b92011-01-07 17:00:07 -08001289 public void copyTo(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001290 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001291 validateIsInt32();
Jason Samsfa445b92011-01-07 17:00:07 -08001292 mRS.validate();
Jason Sams21659ac2013-11-06 15:08:07 -08001293 mRS.nAllocationRead(getID(mRS), d, Element.DataType.SIGNED_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001294 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001295 }
1296
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001297 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001298 * Copy from the Allocation into a float array. The array must be at least
1299 * as large as the Allocation. The allocation must be of an 32 bit float
1300 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001301 *
1302 * @param d The array to be set from the Allocation.
1303 */
Jason Samsfa445b92011-01-07 17:00:07 -08001304 public void copyTo(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001305 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001306 validateIsFloat32();
Jason Sams771bebb2009-12-07 12:40:12 -08001307 mRS.validate();
Jason Sams21659ac2013-11-06 15:08:07 -08001308 mRS.nAllocationRead(getID(mRS), d, Element.DataType.FLOAT_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001309 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams40a29e82009-08-10 14:55:26 -07001310 }
1311
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001312 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001313 * Resize a 1D allocation. The contents of the allocation are preserved.
1314 * If new elements are allocated objects are created with null contents and
1315 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001316 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001317 * <p>If the new region is smaller the references of any objects outside the
1318 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001319 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001320 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001321 *
1322 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001323 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001324 * @deprecated RenderScript objects should be immutable once created. The
1325 * replacement is to create a new allocation and copy the contents.
Jason Samsf7086092011-01-12 13:28:37 -08001326 */
Jason Sams31a7e422010-10-26 13:09:17 -07001327 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001328 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001329 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001330 }
Jason Samse07694b2012-04-03 15:36:36 -07001331 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001332 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001333
Tim Murray460a0492013-11-19 12:45:54 -08001334 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001335 mType = new Type(typeID, mRS);
1336 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001337 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001338 }
1339
Jason Samsb8c5a842009-07-31 20:40:47 -07001340
1341 // creation
1342
Jason Sams49a05d72010-12-29 14:31:29 -08001343 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001344 static {
1345 mBitmapOptions.inScaled = false;
1346 }
1347
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001348 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001349 * Creates a new Allocation with the given {@link
1350 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001351 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001352 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001353 * @param mips specifies desired mipmap behaviour for the
1354 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001355 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001356 * utilized
1357 */
1358 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001359 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
Jason Sams771bebb2009-12-07 12:40:12 -08001360 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001361 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001362 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001363 }
Tim Murray460a0492013-11-19 12:45:54 -08001364 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001365 if (id == 0) {
1366 throw new RSRuntimeException("Allocation creation failed.");
1367 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001368 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams857d0c72011-11-23 15:02:15 -08001369 return new Allocation(id, rs, type, usage);
1370 }
1371
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001372 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001373 * Creates an Allocation with the size specified by the type and no mipmaps
1374 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001375 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001376 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001377 * @param type renderscript type describing data layout
1378 * @param usage bit field specifying how the allocation is
1379 * utilized
1380 *
1381 * @return allocation
1382 */
Jason Samse5d37122010-12-16 00:33:33 -08001383 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1384 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1385 }
1386
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001387 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001388 * Creates an Allocation for use by scripts with a given {@link
1389 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001390 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001391 * @param rs Context to which the Allocation will belong.
1392 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001393 *
1394 * @return allocation
1395 */
Jason Sams5476b452010-12-08 16:14:36 -08001396 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001397 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001398 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001399
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001400 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001401 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001402 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001403 * @param rs Context to which the Allocation will belong.
1404 * @param e Element to use in the Allocation
1405 * @param count the number of Elements in the Allocation
1406 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001407 * utilized
1408 *
1409 * @return allocation
1410 */
Jason Sams5476b452010-12-08 16:14:36 -08001411 static public Allocation createSized(RenderScript rs, Element e,
1412 int count, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001413 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
Jason Sams771bebb2009-12-07 12:40:12 -08001414 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001415 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001416 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001417 Type t = b.create();
1418
Tim Murray460a0492013-11-19 12:45:54 -08001419 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001420 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001421 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001422 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001423 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001424 return new Allocation(id, rs, t, usage);
1425 }
1426
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001427 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001428 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001429 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001430 * @param rs Context to which the Allocation will belong.
1431 * @param e Element to use in the Allocation
1432 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001433 *
1434 * @return allocation
1435 */
Jason Sams5476b452010-12-08 16:14:36 -08001436 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001437 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001438 }
1439
Jason Sams49a05d72010-12-29 14:31:29 -08001440 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001441 final Bitmap.Config bc = b.getConfig();
1442 if (bc == Bitmap.Config.ALPHA_8) {
1443 return Element.A_8(rs);
1444 }
1445 if (bc == Bitmap.Config.ARGB_4444) {
1446 return Element.RGBA_4444(rs);
1447 }
1448 if (bc == Bitmap.Config.ARGB_8888) {
1449 return Element.RGBA_8888(rs);
1450 }
1451 if (bc == Bitmap.Config.RGB_565) {
1452 return Element.RGB_565(rs);
1453 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001454 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001455 }
1456
Jason Sams49a05d72010-12-29 14:31:29 -08001457 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001458 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001459 Element e = elementFromBitmap(rs, b);
1460 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001461 tb.setX(b.getWidth());
1462 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001463 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001464 return tb.create();
1465 }
1466
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001467 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001468 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001469 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001470 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001471 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001472 * @param mips specifies desired mipmap behaviour for the
1473 * allocation
1474 * @param usage bit field specifying how the allocation is
1475 * utilized
1476 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001477 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001478 *
1479 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001480 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001481 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001482 int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001483 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
Jason Sams771bebb2009-12-07 12:40:12 -08001484 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001485
1486 // WAR undocumented color formats
1487 if (b.getConfig() == null) {
1488 if ((usage & USAGE_SHARED) != 0) {
1489 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1490 }
1491 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1492 Canvas c = new Canvas(newBitmap);
1493 c.drawBitmap(b, 0, 0, null);
1494 return createFromBitmap(rs, newBitmap, mips, usage);
1495 }
1496
Jason Sams5476b452010-12-08 16:14:36 -08001497 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001498
Tim Murraya3145512012-12-04 17:59:29 -08001499 // enable optimized bitmap path only with no mipmap and script-only usage
1500 if (mips == MipmapControl.MIPMAP_NONE &&
1501 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Tim Murray78e64942013-04-09 17:28:56 -07001502 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
Tim Murray460a0492013-11-19 12:45:54 -08001503 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08001504 if (id == 0) {
1505 throw new RSRuntimeException("Load failed.");
1506 }
1507
1508 // keep a reference to the Bitmap around to prevent GC
1509 Allocation alloc = new Allocation(id, rs, t, usage);
1510 alloc.setBitmap(b);
1511 return alloc;
1512 }
1513
Jason Sams9bf18922013-04-13 19:48:36 -07001514
Tim Murray460a0492013-11-19 12:45:54 -08001515 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001516 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001517 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001518 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001519 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001520 return new Allocation(id, rs, t, usage);
1521 }
1522
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001523 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001524 * Returns the handle to a raw buffer that is being managed by the screen
1525 * compositor. This operation is only valid for Allocations with {@link
1526 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001527 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001528 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001529 *
1530 */
1531 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08001532 if ((mUsage & USAGE_IO_INPUT) == 0) {
1533 throw new RSInvalidStateException("Allocation is not a surface texture.");
1534 }
1535 return mRS.nAllocationGetSurface(getID(mRS));
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001536 }
1537
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001538 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001539 * Associate a {@link android.view.Surface} with this Allocation. This
1540 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001541 *
1542 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001543 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001544 public void setSurface(Surface sur) {
1545 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001546 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1547 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1548 }
1549
Jason Samse07694b2012-04-03 15:36:36 -07001550 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001551 }
1552
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001553 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001554 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08001555 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001556 * <p>With target API version 18 or greater, this Allocation will be created
1557 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
1558 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
1559 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001560 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001561 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001562 * @param b bitmap source for the allocation data
1563 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001564 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001565 *
1566 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001567 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08001568 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1569 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07001570 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08001571 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001572 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1573 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001574 }
1575
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001576 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001577 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1578 * containing the horizontal list of cube faces. Each face must be a square,
1579 * have the same size as all other faces, and have a width that is a power
1580 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001581 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001582 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001583 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001584 * format: right, left, top, bottom, front, back
1585 * @param mips specifies desired mipmap behaviour for the cubemap
1586 * @param usage bit field specifying how the cubemap is utilized
1587 *
1588 * @return allocation containing cubemap data
1589 *
1590 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001591 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001592 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001593 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001594 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001595
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001596 int height = b.getHeight();
1597 int width = b.getWidth();
1598
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001599 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001600 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1601 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001602 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001603 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001604 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001605 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001606 if (!isPow2) {
1607 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1608 }
1609
1610 Element e = elementFromBitmap(rs, b);
1611 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001612 tb.setX(height);
1613 tb.setY(height);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001614 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001615 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001616 Type t = tb.create();
1617
Tim Murray460a0492013-11-19 12:45:54 -08001618 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001619 if(id == 0) {
1620 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1621 }
Jason Sams5476b452010-12-08 16:14:36 -08001622 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001623 }
1624
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001625 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001626 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
1627 * from a {@link android.graphics.Bitmap} containing the horizontal list of
1628 * cube faces. Each face must be a square, have the same size as all other
1629 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001630 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001631 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001632 * @param b bitmap with cubemap faces layed out in the following
1633 * format: right, left, top, bottom, front, back
1634 *
1635 * @return allocation containing cubemap data
1636 *
1637 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001638 static public Allocation createCubemapFromBitmap(RenderScript rs,
1639 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001640 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001641 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001642 }
1643
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001644 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001645 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
1646 * objects containing the cube faces. Each face must be a square, have the
1647 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001648 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001649 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001650 * @param xpos cubemap face in the positive x direction
1651 * @param xneg cubemap face in the negative x direction
1652 * @param ypos cubemap face in the positive y direction
1653 * @param yneg cubemap face in the negative y direction
1654 * @param zpos cubemap face in the positive z direction
1655 * @param zneg cubemap face in the negative z direction
1656 * @param mips specifies desired mipmap behaviour for the cubemap
1657 * @param usage bit field specifying how the cubemap is utilized
1658 *
1659 * @return allocation containing cubemap data
1660 *
1661 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001662 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1663 Bitmap xpos,
1664 Bitmap xneg,
1665 Bitmap ypos,
1666 Bitmap yneg,
1667 Bitmap zpos,
1668 Bitmap zneg,
1669 MipmapControl mips,
1670 int usage) {
1671 int height = xpos.getHeight();
1672 if (xpos.getWidth() != height ||
1673 xneg.getWidth() != height || xneg.getHeight() != height ||
1674 ypos.getWidth() != height || ypos.getHeight() != height ||
1675 yneg.getWidth() != height || yneg.getHeight() != height ||
1676 zpos.getWidth() != height || zpos.getHeight() != height ||
1677 zneg.getWidth() != height || zneg.getHeight() != height) {
1678 throw new RSIllegalArgumentException("Only square cube map faces supported");
1679 }
1680 boolean isPow2 = (height & (height - 1)) == 0;
1681 if (!isPow2) {
1682 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1683 }
1684
1685 Element e = elementFromBitmap(rs, xpos);
1686 Type.Builder tb = new Type.Builder(rs, e);
1687 tb.setX(height);
1688 tb.setY(height);
1689 tb.setFaces(true);
1690 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1691 Type t = tb.create();
1692 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1693
1694 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001695 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001696 adapter.copyFrom(xpos);
1697 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1698 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001699 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001700 adapter.copyFrom(ypos);
1701 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1702 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001703 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001704 adapter.copyFrom(zpos);
1705 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1706 adapter.copyFrom(zneg);
1707
1708 return cubemap;
1709 }
1710
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001711 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001712 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
1713 * from 6 {@link android.graphics.Bitmap} objects containing the cube
1714 * faces. Each face must be a square, have the same size as all other faces,
1715 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001716 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001717 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001718 * @param xpos cubemap face in the positive x direction
1719 * @param xneg cubemap face in the negative x direction
1720 * @param ypos cubemap face in the positive y direction
1721 * @param yneg cubemap face in the negative y direction
1722 * @param zpos cubemap face in the positive z direction
1723 * @param zneg cubemap face in the negative z direction
1724 *
1725 * @return allocation containing cubemap data
1726 *
1727 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001728 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1729 Bitmap xpos,
1730 Bitmap xneg,
1731 Bitmap ypos,
1732 Bitmap yneg,
1733 Bitmap zpos,
1734 Bitmap zneg) {
1735 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1736 zpos, zneg, MipmapControl.MIPMAP_NONE,
1737 USAGE_GRAPHICS_TEXTURE);
1738 }
1739
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001740 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001741 * Creates an Allocation from the Bitmap referenced
1742 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001743 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001744 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001745 * @param res application resources
1746 * @param id resource id to load the data from
1747 * @param mips specifies desired mipmap behaviour for the
1748 * allocation
1749 * @param usage bit field specifying how the allocation is
1750 * utilized
1751 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001752 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001753 *
1754 */
Jason Sams5476b452010-12-08 16:14:36 -08001755 static public Allocation createFromBitmapResource(RenderScript rs,
1756 Resources res,
1757 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001758 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001759 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001760
Jason Sams771bebb2009-12-07 12:40:12 -08001761 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07001762 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
1763 throw new RSIllegalArgumentException("Unsupported usage specified.");
1764 }
Jason Sams5476b452010-12-08 16:14:36 -08001765 Bitmap b = BitmapFactory.decodeResource(res, id);
1766 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1767 b.recycle();
1768 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001769 }
1770
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001771 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001772 * Creates a non-mipmapped Allocation to use as a graphics texture from the
1773 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001774 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001775 * <p>With target API version 18 or greater, this allocation will be created
1776 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
1777 * target API version 17 or lower, this allocation will be created with
1778 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08001779 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001780 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001781 * @param res application resources
1782 * @param id resource id to load the data from
1783 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001784 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001785 *
1786 */
Jason Sams5476b452010-12-08 16:14:36 -08001787 static public Allocation createFromBitmapResource(RenderScript rs,
1788 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001789 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08001790 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1791 return createFromBitmapResource(rs, res, id,
1792 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07001793 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08001794 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001795 return createFromBitmapResource(rs, res, id,
1796 MipmapControl.MIPMAP_NONE,
1797 USAGE_GRAPHICS_TEXTURE);
1798 }
1799
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001800 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001801 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001802 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001803 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001804 * @param str string to create the allocation from
1805 * @param usage bit field specifying how the allocaiton is
1806 * utilized
1807 *
1808 */
Jason Sams5476b452010-12-08 16:14:36 -08001809 static public Allocation createFromString(RenderScript rs,
1810 String str,
1811 int usage) {
1812 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001813 byte[] allocArray = null;
1814 try {
1815 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001816 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001817 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001818 return alloc;
1819 }
1820 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001821 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001822 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001823 }
Jason Sams739c8262013-04-11 18:07:52 -07001824
1825 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001826 * Interface to handle notification when new buffers are available via
1827 * {@link #USAGE_IO_INPUT}. An application will receive one notification
1828 * when a buffer is available. Additional buffers will not trigger new
1829 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07001830 */
Jason Sams42ef2382013-08-29 13:30:59 -07001831 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07001832 public void onBufferAvailable(Allocation a);
1833 }
1834
1835 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001836 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07001837 *
Jason Sams42ef2382013-08-29 13:30:59 -07001838 * @param callback instance of the OnBufferAvailableListener
1839 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07001840 */
Jason Sams42ef2382013-08-29 13:30:59 -07001841 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07001842 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08001843 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07001844 mBufferNotifier = callback;
1845 }
1846 }
1847
1848 static void sendBufferNotification(int id) {
1849 synchronized(mAllocationMap) {
Tim Murray460a0492013-11-19 12:45:54 -08001850 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07001851
1852 if ((a != null) && (a.mBufferNotifier != null)) {
1853 a.mBufferNotifier.onBufferAvailable(a);
1854 }
1855 }
1856 }
1857
Jason Samsb8c5a842009-07-31 20:40:47 -07001858}
1859