blob: 67d94f9bc3ded51d10f1bad0df7afc3edd4d6cdf [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
Stephen Hines9069ee82012-02-13 18:25:54 -08002 * Copyright (C) 2008-2012 The Android Open Source Project
Jason Samsb8c5a842009-07-31 20:40:47 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
Jason Samsb8c5a842009-07-31 20:40:47 -070019import java.io.IOException;
20import java.io.InputStream;
Jason Sams739c8262013-04-11 18:07:52 -070021import java.util.HashMap;
Jason Samsb8c5a842009-07-31 20:40:47 -070022import android.content.res.Resources;
Romain Guy650a3eb2009-08-31 14:06:43 -070023import android.content.res.AssetManager;
Jason Samsb8c5a842009-07-31 20:40:47 -070024import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
Jason Samsfb9aa9f2012-03-28 15:30:07 -070026import android.view.Surface;
Jason Samsb8c5a842009-07-31 20:40:47 -070027import android.util.Log;
Romain Guy650a3eb2009-08-31 14:06:43 -070028import android.util.TypedValue;
Tim Murrayabd5db92013-02-28 11:45:22 -080029import android.graphics.Canvas;
Tim Murray6d7a53c2013-05-23 16:59:23 -070030import android.os.Trace;
Jason Samsb8c5a842009-07-31 20:40:47 -070031
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070032/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070033 * <p> This class provides the primary method through which data is passed to
34 * and from RenderScript kernels. An Allocation provides the backing store for
35 * a given {@link android.renderscript.Type}. </p>
Jason Samsa23d4e72011-01-04 18:59:12 -080036 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070037 * <p>An Allocation also contains a set of usage flags that denote how the
38 * Allocation could be used. For example, an Allocation may have usage flags
39 * specifying that it can be used from a script as well as input to a {@link
40 * android.renderscript.Sampler}. A developer must synchronize across these
41 * different usages using {@link android.renderscript.Allocation#syncAll} in
42 * order to ensure that different users of the Allocation have a consistent view
43 * of memory. For example, in the case where an Allocation is used as the output
44 * of one kernel and as Sampler input in a later kernel, a developer must call
45 * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
46 * second kernel to ensure correctness.
Jason Samsa23d4e72011-01-04 18:59:12 -080047 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070048 * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
49 * more complex Element types, the {@link #copyFromUnchecked} methods can be
50 * used to copy from byte arrays or similar constructs.</p>
Jason Samsb8c5a842009-07-31 20:40:47 -070051 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080052 * <div class="special reference">
53 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070054 * <p>For more information about creating an application that uses RenderScript, read the
55 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080056 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070057 **/
58public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070059 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080060 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080061 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070062 Allocation mAdaptedAllocation;
Tim Murray2f2472c2013-08-22 14:55:26 -070063 int mSize;
Jason Samsba862d12011-07-07 15:24:42 -070064
65 boolean mConstrainedLOD;
66 boolean mConstrainedFace;
67 boolean mConstrainedY;
68 boolean mConstrainedZ;
Jason Sams615e7ce2012-01-13 14:01:20 -080069 boolean mReadAllowed = true;
70 boolean mWriteAllowed = true;
Jason Samsba862d12011-07-07 15:24:42 -070071 int mSelectedY;
72 int mSelectedZ;
73 int mSelectedLOD;
74 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
75
76 int mCurrentDimX;
77 int mCurrentDimY;
78 int mCurrentDimZ;
79 int mCurrentCount;
Tim Murray7a629fa2013-11-19 12:45:54 -080080 static HashMap<Long, Allocation> mAllocationMap =
81 new HashMap<Long, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070082 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070083
Tim Murrayc11e25c2013-04-09 11:01:01 -070084 /**
85 * The usage of the Allocation. These signal to RenderScript where to place
86 * the Allocation in memory.
87 *
88 */
Jason Sams5476b452010-12-08 16:14:36 -080089
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070090 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -070091 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -080092 */
Jason Sams5476b452010-12-08 16:14:36 -080093 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -080094
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070095 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -070096 * The Allocation will be used as a texture source by one or more graphics
97 * programs.
Jason Samsf7086092011-01-12 13:28:37 -080098 *
99 */
Jason Sams5476b452010-12-08 16:14:36 -0800100 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800101
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700102 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700103 * The Allocation will be used as a graphics mesh.
104 *
105 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800106 *
107 */
Jason Sams5476b452010-12-08 16:14:36 -0800108 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800109
110
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700111 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700112 * The Allocation will be used as the source of shader constants by one or
113 * more programs.
114 *
115 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800116 *
117 */
Jason Sams5476b452010-12-08 16:14:36 -0800118 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
119
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700120 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700121 * The Allocation will be used as a target for offscreen rendering
122 *
123 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700124 *
125 */
126 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
127
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700128 /**
Jason Sams1887d522013-09-24 15:18:52 -0700129 * The Allocation will be used as a {@link android.view.Surface}
130 * consumer. This usage will cause the Allocation to be created
131 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800132 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800133 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700134 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800135
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700136 /**
Jason Sams1887d522013-09-24 15:18:52 -0700137 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700138 * producer. The dimensions and format of the {@link
Jason Sams1887d522013-09-24 15:18:52 -0700139 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700140 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800141 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800142 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700143 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700144
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700145 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700146 * The Allocation's backing store will be inherited from another object
147 * (usually a {@link android.graphics.Bitmap}); copying to or from the
148 * original source Bitmap will cause a synchronization rather than a full
149 * copy. {@link #syncAll} may also be used to synchronize the Allocation
150 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800151 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700152 * <p>This is set by default for allocations created with {@link
153 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800154 *
155 */
156 public static final int USAGE_SHARED = 0x0080;
157
158 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700159 * Controls mipmap behavior when using the bitmap creation and update
160 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800161 */
Jason Sams4ef66502010-12-10 16:03:15 -0800162 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700163 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700164 * No mipmaps will be generated and the type generated from the incoming
165 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800166 */
Jason Sams5476b452010-12-08 16:14:36 -0800167 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800168
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700169 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700170 * A full mipmap chain will be created in script memory. The Type of
171 * the Allocation will contain a full mipmap chain. On upload, the full
172 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800173 */
Jason Sams5476b452010-12-08 16:14:36 -0800174 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800175
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700176 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700177 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
178 * not contain mipmaps. On upload, the allocation data will contain a
179 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800180 */
Jason Sams5476b452010-12-08 16:14:36 -0800181 MIPMAP_ON_SYNC_TO_TEXTURE(2);
182
183 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800184 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800185 mID = id;
186 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700187 }
188
Jason Sams48fe5342011-07-08 13:52:30 -0700189
Tim Murray7a629fa2013-11-19 12:45:54 -0800190 private long getIDSafe() {
Jason Sams48fe5342011-07-08 13:52:30 -0700191 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700192 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700193 }
Jason Samse07694b2012-04-03 15:36:36 -0700194 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700195 }
196
Jason Sams03d2d002012-03-23 13:51:56 -0700197
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700198 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700199 * Get the {@link android.renderscript.Element} of the {@link
200 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700201 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700202 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700203 *
204 */
205 public Element getElement() {
206 return mType.getElement();
207 }
208
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700209 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700210 * Get the usage flags of the Allocation.
211 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700212 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700213 *
214 */
215 public int getUsage() {
216 return mUsage;
217 }
218
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700219 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700220 * Get the size of the Allocation in bytes.
221 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700222 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700223 *
224 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700225 public int getBytesSize() {
Tim Murraye6eaaf62013-12-17 17:15:25 -0800226 if (mType.mDimYuv != 0) {
227 return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
228 }
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700229 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700230 }
231
Jason Sams452a7662011-07-07 16:05:18 -0700232 private void updateCacheInfo(Type t) {
233 mCurrentDimX = t.getX();
234 mCurrentDimY = t.getY();
235 mCurrentDimZ = t.getZ();
236 mCurrentCount = mCurrentDimX;
237 if (mCurrentDimY > 1) {
238 mCurrentCount *= mCurrentDimY;
239 }
240 if (mCurrentDimZ > 1) {
241 mCurrentCount *= mCurrentDimZ;
242 }
243 }
Jason Samsba862d12011-07-07 15:24:42 -0700244
Tim Murraya3145512012-12-04 17:59:29 -0800245 private void setBitmap(Bitmap b) {
246 mBitmap = b;
247 }
248
Tim Murray7a629fa2013-11-19 12:45:54 -0800249 Allocation(long id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700250 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800251 if ((usage & ~(USAGE_SCRIPT |
252 USAGE_GRAPHICS_TEXTURE |
253 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700254 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800255 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800256 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800257 USAGE_IO_OUTPUT |
258 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800259 throw new RSIllegalArgumentException("Unknown usage specified.");
260 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800261
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700262 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800263 mWriteAllowed = false;
264
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700265 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800266 USAGE_GRAPHICS_TEXTURE |
267 USAGE_SCRIPT)) != 0) {
268 throw new RSIllegalArgumentException("Invalid usage combination.");
269 }
270 }
Jason Sams9bf18922013-04-13 19:48:36 -0700271
Jason Sams5476b452010-12-08 16:14:36 -0800272 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800273 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700274
Jason Sams452a7662011-07-07 16:05:18 -0700275 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700276 // TODO: A3D doesn't have Type info during creation, so we can't
277 // calculate the size ahead of time. We can possibly add a method
278 // to update the size in the future if it seems reasonable.
279 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700280 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700281 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700282 try {
283 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
284 } catch (Exception e) {
285 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
286 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
287 }
288 }
289
290 protected void finalize() throws Throwable {
291 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
292 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700293 }
294
Jason Samsb97b2512011-01-16 15:04:08 -0800295 private void validateIsInt32() {
296 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
297 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
298 return;
299 }
300 throw new RSIllegalArgumentException(
301 "32 bit integer source does not match allocation type " + mType.mElement.mType);
302 }
303
304 private void validateIsInt16() {
305 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
306 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
307 return;
308 }
309 throw new RSIllegalArgumentException(
310 "16 bit integer source does not match allocation type " + mType.mElement.mType);
311 }
312
313 private void validateIsInt8() {
314 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
315 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
316 return;
317 }
318 throw new RSIllegalArgumentException(
319 "8 bit integer source does not match allocation type " + mType.mElement.mType);
320 }
321
322 private void validateIsFloat32() {
323 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
324 return;
325 }
326 throw new RSIllegalArgumentException(
327 "32 bit float source does not match allocation type " + mType.mElement.mType);
328 }
329
330 private void validateIsObject() {
331 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
332 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
333 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
334 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
335 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
336 (mType.mElement.mType == Element.DataType.RS_MESH) ||
337 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
338 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
339 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
340 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
341 return;
342 }
343 throw new RSIllegalArgumentException(
344 "Object source does not match allocation type " + mType.mElement.mType);
345 }
346
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700347 @Override
348 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800349 super.updateFromNative();
Tim Murray7a629fa2013-11-19 12:45:54 -0800350 long typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700351 if(typeID != 0) {
352 mType = new Type(typeID, mRS);
353 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700354 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700355 }
356 }
357
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700358 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700359 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700360 *
361 * @return Type
362 *
363 */
Jason Samsea87e962010-01-12 12:12:28 -0800364 public Type getType() {
365 return mType;
366 }
367
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700368 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700369 * Propagate changes from one usage of the Allocation to the
370 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700371 *
372 */
Jason Sams5476b452010-12-08 16:14:36 -0800373 public void syncAll(int srcLocation) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700374 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
Jason Sams5476b452010-12-08 16:14:36 -0800375 switch (srcLocation) {
Jason Sams5476b452010-12-08 16:14:36 -0800376 case USAGE_GRAPHICS_TEXTURE:
Tim Murray78e64942013-04-09 17:28:56 -0700377 case USAGE_SCRIPT:
378 if ((mUsage & USAGE_SHARED) != 0) {
379 copyFrom(mBitmap);
380 }
381 break;
382 case USAGE_GRAPHICS_CONSTANTS:
Jason Sams5476b452010-12-08 16:14:36 -0800383 case USAGE_GRAPHICS_VERTEX:
384 break;
Tim Murray78e64942013-04-09 17:28:56 -0700385 case USAGE_SHARED:
386 if ((mUsage & USAGE_SHARED) != 0) {
387 copyTo(mBitmap);
388 }
389 break;
Jason Sams5476b452010-12-08 16:14:36 -0800390 default:
391 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
392 }
393 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700394 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700395 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800396 }
397
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700398 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700399 * Send a buffer to the output stream. The contents of the Allocation will
400 * be undefined after this operation. This operation is only valid if {@link
401 * #USAGE_IO_OUTPUT} is set on the Allocation.
402 *
Jason Sams163766c2012-02-15 12:04:24 -0800403 *
Jason Sams163766c2012-02-15 12:04:24 -0800404 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700405 public void ioSend() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700406 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
Jason Sams163766c2012-02-15 12:04:24 -0800407 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
408 throw new RSIllegalArgumentException(
409 "Can only send buffer if IO_OUTPUT usage specified.");
410 }
411 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700412 mRS.nAllocationIoSend(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700413 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800414 }
415
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700416 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700417 * Receive the latest input into the Allocation. This operation
418 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800419 *
Jason Sams163766c2012-02-15 12:04:24 -0800420 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700421 public void ioReceive() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700422 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
Jason Sams163766c2012-02-15 12:04:24 -0800423 if ((mUsage & USAGE_IO_INPUT) == 0) {
424 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700425 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800426 }
427 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700428 mRS.nAllocationIoReceive(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700429 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800430 }
431
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700432 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700433 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700434 *
435 * @param d Source array.
436 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800437 public void copyFrom(BaseObj[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700438 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Sams771bebb2009-12-07 12:40:12 -0800439 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800440 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700441 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800442 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700443 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800444 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800445 // FIXME: requires 64-bit path
446
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800447 int i[] = new int[d.length];
448 for (int ct=0; ct < d.length; ct++) {
Tim Murray7a629fa2013-11-19 12:45:54 -0800449 i[ct] = (int)d[ct].getID(mRS);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800450 }
Jason Samsba862d12011-07-07 15:24:42 -0700451 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700452 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700453 }
454
Jason Samsfb9f82c2011-01-12 14:53:25 -0800455 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800456 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800457 if (bc == null) {
458 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
459 }
Jason Sams252c0782011-01-11 17:42:52 -0800460 switch (bc) {
461 case ALPHA_8:
462 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
463 throw new RSIllegalArgumentException("Allocation kind is " +
464 mType.getElement().mKind + ", type " +
465 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700466 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800467 " bytes, passed bitmap was " + bc);
468 }
469 break;
470 case ARGB_8888:
471 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700472 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800473 throw new RSIllegalArgumentException("Allocation kind is " +
474 mType.getElement().mKind + ", type " +
475 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700476 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800477 " bytes, passed bitmap was " + bc);
478 }
479 break;
480 case RGB_565:
481 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700482 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800483 throw new RSIllegalArgumentException("Allocation kind is " +
484 mType.getElement().mKind + ", type " +
485 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700486 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800487 " bytes, passed bitmap was " + bc);
488 }
489 break;
490 case ARGB_4444:
491 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700492 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800493 throw new RSIllegalArgumentException("Allocation kind is " +
494 mType.getElement().mKind + ", type " +
495 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700496 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800497 " bytes, passed bitmap was " + bc);
498 }
499 break;
500
501 }
Jason Sams4ef66502010-12-10 16:03:15 -0800502 }
503
Jason Samsfb9f82c2011-01-12 14:53:25 -0800504 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700505 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800506 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
507 }
508 }
509
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700510 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700511 * Copy into this Allocation from an array. This method does not guarantee
512 * that the Allocation is compatible with the input buffer; it copies memory
513 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800514 *
515 * @param d the source data array
516 */
517 public void copyFromUnchecked(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700518 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800519 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700520 if (mCurrentDimZ > 0) {
521 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
522 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800523 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
524 } else {
525 copy1DRangeFromUnchecked(0, mCurrentCount, d);
526 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700527 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800528 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700529
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700530 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700531 * Copy into this Allocation from an array. This method does not guarantee
532 * that the Allocation is compatible with the input buffer; it copies memory
533 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800534 *
535 * @param d the source data array
536 */
537 public void copyFromUnchecked(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700538 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800539 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700540 if (mCurrentDimZ > 0) {
541 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
542 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800543 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
544 } else {
545 copy1DRangeFromUnchecked(0, mCurrentCount, d);
546 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700547 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800548 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700549
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700550 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700551 * Copy into this Allocation from an array. This method does not guarantee
552 * that the Allocation is compatible with the input buffer; it copies memory
553 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800554 *
555 * @param d the source data array
556 */
557 public void copyFromUnchecked(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700558 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800559 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700560 if (mCurrentDimZ > 0) {
561 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
562 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800563 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
564 } else {
565 copy1DRangeFromUnchecked(0, mCurrentCount, d);
566 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700567 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800568 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700569
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700570 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700571 * Copy into this Allocation from an array. This method does not guarantee
572 * that the Allocation is compatible with the input buffer; it copies memory
573 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800574 *
575 * @param d the source data array
576 */
577 public void copyFromUnchecked(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700578 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800579 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700580 if (mCurrentDimZ > 0) {
581 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
582 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800583 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
584 } else {
585 copy1DRangeFromUnchecked(0, mCurrentCount, d);
586 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700587 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800588 }
589
Tim Murray6d7a53c2013-05-23 16:59:23 -0700590
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700591 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700592 * Copy into this Allocation from an array. This variant is type checked
593 * and will generate exceptions if the Allocation's {@link
594 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800595 *
596 * @param d the source data array
597 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800598 public void copyFrom(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700599 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800600 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700601 if (mCurrentDimZ > 0) {
602 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
603 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800604 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
605 } else {
606 copy1DRangeFrom(0, mCurrentCount, d);
607 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700608 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800609 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800610
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700611 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700612 * Copy into this Allocation from an array. This variant is type checked
613 * and will generate exceptions if the Allocation's {@link
614 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800615 *
616 * @param d the source data array
617 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800618 public void copyFrom(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700619 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800620 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700621 if (mCurrentDimZ > 0) {
622 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
623 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800624 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
625 } else {
626 copy1DRangeFrom(0, mCurrentCount, d);
627 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700628 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800629 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800630
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700631 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700632 * Copy into this Allocation from an array. This variant is type checked
633 * and will generate exceptions if the Allocation's {@link
634 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800635 *
636 * @param d the source data array
637 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800638 public void copyFrom(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700639 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800640 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700641 if (mCurrentDimZ > 0) {
642 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
643 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800644 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
645 } else {
646 copy1DRangeFrom(0, mCurrentCount, d);
647 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700648 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800649 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800650
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700651 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700652 * Copy into this Allocation from an array. This variant is type checked
653 * and will generate exceptions if the Allocation's {@link
654 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800655 *
656 * @param d the source data array
657 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800658 public void copyFrom(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700659 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800660 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700661 if (mCurrentDimZ > 0) {
662 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
663 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800664 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
665 } else {
666 copy1DRangeFrom(0, mCurrentCount, d);
667 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700668 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800669 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800670
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700671 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700672 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
673 * height, width, and format of the bitmap must match the existing
674 * allocation.
675 *
676 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
677 * android.graphics.Bitmap} used to create the Allocation with {@link
678 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
679 * this will synchronize the Allocation with the latest data from the {@link
680 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800681 *
682 * @param b the source bitmap
683 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800684 public void copyFrom(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700685 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsfb9f82c2011-01-12 14:53:25 -0800686 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -0800687 if (b.getConfig() == null) {
688 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
689 Canvas c = new Canvas(newBitmap);
690 c.drawBitmap(b, 0, 0, null);
691 copyFrom(newBitmap);
692 return;
693 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800694 validateBitmapSize(b);
695 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700696 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700697 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700698 }
699
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700700 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700701 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700702 * must be identical.
703 *
704 * @param a the source allocation
705 */
706 public void copyFrom(Allocation a) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700707 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Tim Murrayf671fb02012-10-03 13:50:05 -0700708 mRS.validate();
709 if (!mType.equals(a.getType())) {
710 throw new RSIllegalArgumentException("Types of allocations must match.");
711 }
712 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700713 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700714 }
715
Tim Murrayf671fb02012-10-03 13:50:05 -0700716 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700717 * This is only intended to be used by auto-generated code reflected from
718 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800719 *
720 * @param xoff
721 * @param fp
722 */
Jason Sams21b41032011-01-16 15:05:41 -0800723 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800724 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700725 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700726 final byte[] data = fp.getData();
727
728 int count = data.length / eSize;
729 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800730 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700731 " not divisible by element size " + eSize + ".");
732 }
Jason Samsba862d12011-07-07 15:24:42 -0700733 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700734 }
735
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700736 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700737 * This is only intended to be used by auto-generated code reflected from
738 * the RenderScript script files.
Jason Samsfa445b92011-01-07 17:00:07 -0800739 *
740 * @param xoff
741 * @param component_number
742 * @param fp
743 */
Jason Sams21b41032011-01-16 15:05:41 -0800744 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800745 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700746 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800747 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700748 }
749 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800750 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700751 }
752
753 final byte[] data = fp.getData();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700754 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800755 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700756
757 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800758 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700759 " does not match component size " + eSize + ".");
760 }
761
Jason Sams48fe5342011-07-08 13:52:30 -0700762 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Jason Samsba862d12011-07-07 15:24:42 -0700763 component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700764 }
765
Jason Sams768bc022009-09-21 19:41:04 -0700766 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800767 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700768 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800769 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700770 }
771 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800772 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700773 }
Jason Samsba862d12011-07-07 15:24:42 -0700774 if((off + count) > mCurrentCount) {
775 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700776 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700777 }
Jason Samsba862d12011-07-07 15:24:42 -0700778 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800779 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700780 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700781 }
782
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700783 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700784 * Generate a mipmap chain. This is only valid if the Type of the Allocation
785 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800786 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700787 * <p>This function will generate a complete set of mipmaps from the top
788 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800789 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700790 * <p>If the Allocation is also using other memory spaces, a call to {@link
791 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800792 */
793 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700794 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800795 }
796
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700797 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700798 * Copy an array into part of this Allocation. This method does not
799 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800800 *
801 * @param off The offset of the first element to be copied.
802 * @param count The number of elements to be copied.
803 * @param d the source data array
804 */
805 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700806 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700807 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700808 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams6fcf2e12013-11-06 11:22:02 -0800809 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize, Element.DataType.SIGNED_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700810 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams768bc022009-09-21 19:41:04 -0700811 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700812
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700813 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700814 * Copy an array into part of this Allocation. This method does not
815 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800816 *
817 * @param off The offset of the first element to be copied.
818 * @param count The number of elements to be copied.
819 * @param d the source data array
820 */
821 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700822 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700823 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700824 data1DChecks(off, count, d.length * 2, dataSize);
Jason Sams6fcf2e12013-11-06 11:22:02 -0800825 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize, Element.DataType.SIGNED_16);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700826 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams768bc022009-09-21 19:41:04 -0700827 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700828
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700829 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700830 * Copy an array into part of this Allocation. This method does not
831 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800832 *
833 * @param off The offset of the first element to be copied.
834 * @param count The number of elements to be copied.
835 * @param d the source data array
836 */
837 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700838 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700839 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700840 data1DChecks(off, count, d.length, dataSize);
Jason Sams6fcf2e12013-11-06 11:22:02 -0800841 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize, Element.DataType.SIGNED_8);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700842 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams768bc022009-09-21 19:41:04 -0700843 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700844
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700845 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700846 * Copy an array into part of this Allocation. This method does not
847 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800848 *
849 * @param off The offset of the first element to be copied.
850 * @param count The number of elements to be copied.
851 * @param d the source data array
852 */
853 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700854 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700855 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700856 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams6fcf2e12013-11-06 11:22:02 -0800857 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize, Element.DataType.FLOAT_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700858 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700859 }
860
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700861 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700862 * Copy an array into part of this Allocation. This variant is type checked
863 * and will generate exceptions if the Allocation type is not a 32 bit
864 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800865 *
866 * @param off The offset of the first element to be copied.
867 * @param count The number of elements to be copied.
868 * @param d the source data array
869 */
Jason Samsb97b2512011-01-16 15:04:08 -0800870 public void copy1DRangeFrom(int off, int count, int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700871 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800872 validateIsInt32();
873 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700874 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800875 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800876
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700877 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700878 * Copy an array into part of this Allocation. This variant is type checked
879 * and will generate exceptions if the Allocation type is not a 16 bit
880 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800881 *
882 * @param off The offset of the first element to be copied.
883 * @param count The number of elements to be copied.
884 * @param d the source data array
885 */
Jason Samsb97b2512011-01-16 15:04:08 -0800886 public void copy1DRangeFrom(int off, int count, short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700887 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800888 validateIsInt16();
889 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700890 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800891 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800892
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700893 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700894 * Copy an array into part of this Allocation. This variant is type checked
895 * and will generate exceptions if the Allocation type is not an 8 bit
896 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800897 *
898 * @param off The offset of the first element to be copied.
899 * @param count The number of elements to be copied.
900 * @param d the source data array
901 */
Jason Samsb97b2512011-01-16 15:04:08 -0800902 public void copy1DRangeFrom(int off, int count, byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700903 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800904 validateIsInt8();
905 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700906 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800907 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800908
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700909 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700910 * Copy an array into part of this Allocation. This variant is type checked
911 * and will generate exceptions if the Allocation type is not a 32 bit float
912 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800913 *
914 * @param off The offset of the first element to be copied.
915 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700916 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800917 */
Jason Samsb97b2512011-01-16 15:04:08 -0800918 public void copy1DRangeFrom(int off, int count, float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700919 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800920 validateIsFloat32();
921 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700922 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800923 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700924 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700925 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700926 *
927 * @param off The offset of the first element to be copied.
928 * @param count The number of elements to be copied.
929 * @param data the source data allocation.
930 * @param dataOff off The offset of the first element in data to
931 * be copied.
932 */
933 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700934 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -0700935 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700936 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700937 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700938 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700939 }
940
Jason Samsfb9f82c2011-01-12 14:53:25 -0800941 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -0700942 if (mAdaptedAllocation != null) {
943
944 } else {
945
946 if (xoff < 0 || yoff < 0) {
947 throw new RSIllegalArgumentException("Offset cannot be negative.");
948 }
949 if (h < 0 || w < 0) {
950 throw new RSIllegalArgumentException("Height or width cannot be negative.");
951 }
952 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
953 throw new RSIllegalArgumentException("Updated region larger than allocation.");
954 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800955 }
956 }
Jason Sams768bc022009-09-21 19:41:04 -0700957
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800958 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, byte[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700959 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800960 mRS.validate();
961 validate2DRange(xoff, yoff, w, h);
962 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Sams6fcf2e12013-11-06 11:22:02 -0800963 w, h, data, data.length, Element.DataType.SIGNED_8);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700964 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800965 }
966
967 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, short[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700968 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800969 mRS.validate();
970 validate2DRange(xoff, yoff, w, h);
971 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Sams6fcf2e12013-11-06 11:22:02 -0800972 w, h, data, data.length * 2, Element.DataType.SIGNED_16);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700973 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800974 }
975
976 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, int[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700977 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800978 mRS.validate();
979 validate2DRange(xoff, yoff, w, h);
980 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Sams6fcf2e12013-11-06 11:22:02 -0800981 w, h, data, data.length * 4, Element.DataType.SIGNED_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700982 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800983 }
984
985 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, float[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700986 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800987 mRS.validate();
988 validate2DRange(xoff, yoff, w, h);
989 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Sams6fcf2e12013-11-06 11:22:02 -0800990 w, h, data, data.length * 4, Element.DataType.FLOAT_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700991 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800992 }
993
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700994 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700995 * Copy from an array into a rectangular region in this Allocation. The
996 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -0800997 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700998 * @param xoff X offset of the region to update in this Allocation
999 * @param yoff Y offset of the region to update in this Allocation
1000 * @param w Width of the region to update
1001 * @param h Height of the region to update
1002 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001003 */
1004 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001005 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001006 validateIsInt8();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001007 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001008 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001009 }
1010
Tim Murrayc11e25c2013-04-09 11:01:01 -07001011 /**
1012 * Copy from an array into a rectangular region in this Allocation. The
1013 * array is assumed to be tightly packed.
1014 *
1015 * @param xoff X offset of the region to update in this Allocation
1016 * @param yoff Y offset of the region to update in this Allocation
1017 * @param w Width of the region to update
1018 * @param h Height of the region to update
1019 * @param data to be placed into the Allocation
1020 */
Jason Samsf7086092011-01-12 13:28:37 -08001021 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001022 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001023 validateIsInt16();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001024 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001025 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001026 }
1027
Tim Murrayc11e25c2013-04-09 11:01:01 -07001028 /**
1029 * Copy from an array into a rectangular region in this Allocation. The
1030 * array is assumed to be tightly packed.
1031 *
1032 * @param xoff X offset of the region to update in this Allocation
1033 * @param yoff Y offset of the region to update in this Allocation
1034 * @param w Width of the region to update
1035 * @param h Height of the region to update
1036 * @param data to be placed into the Allocation
1037 */
Jason Samsf7086092011-01-12 13:28:37 -08001038 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001039 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001040 validateIsInt32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001041 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001042 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001043 }
1044
Tim Murrayc11e25c2013-04-09 11:01:01 -07001045 /**
1046 * Copy from an array into a rectangular region in this Allocation. The
1047 * array is assumed to be tightly packed.
1048 *
1049 * @param xoff X offset of the region to update in this Allocation
1050 * @param yoff Y offset of the region to update in this Allocation
1051 * @param w Width of the region to update
1052 * @param h Height of the region to update
1053 * @param data to be placed into the Allocation
1054 */
Jason Samsf7086092011-01-12 13:28:37 -08001055 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001056 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001057 validateIsFloat32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001058 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001059 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001060 }
1061
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001062 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001063 * Copy a rectangular region from an Allocation into a rectangular region in
1064 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001065 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001066 * @param xoff X offset of the region in this Allocation
1067 * @param yoff Y offset of the region in this Allocation
1068 * @param w Width of the region to update.
1069 * @param h Height of the region to update.
1070 * @param data source Allocation.
1071 * @param dataXoff X offset in source Allocation
1072 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001073 */
1074 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1075 Allocation data, int dataXoff, int dataYoff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001076 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001077 mRS.validate();
1078 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -07001079 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -07001080 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001081 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -07001082 data.mSelectedLOD, data.mSelectedFace.mID);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001083 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001084 }
1085
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001086 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001087 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1088 * and width of the update will use the height and width of the {@link
1089 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001090 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001091 * @param xoff X offset of the region to update in this Allocation
1092 * @param yoff Y offset of the region to update in this Allocation
1093 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001094 */
1095 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001096 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Jason Samsfa445b92011-01-07 17:00:07 -08001097 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001098 if (data.getConfig() == null) {
1099 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1100 Canvas c = new Canvas(newBitmap);
1101 c.drawBitmap(data, 0, 0, null);
1102 copy2DRangeFrom(xoff, yoff, newBitmap);
Jason Samsb05d6892013-04-09 15:59:24 -07001103 return;
Tim Murrayabd5db92013-02-28 11:45:22 -08001104 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001105 validateBitmapFormat(data);
1106 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -07001107 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001108 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001109 }
1110
Jason Samsb05d6892013-04-09 15:59:24 -07001111 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1112 if (mAdaptedAllocation != null) {
1113
1114 } else {
1115
1116 if (xoff < 0 || yoff < 0 || zoff < 0) {
1117 throw new RSIllegalArgumentException("Offset cannot be negative.");
1118 }
1119 if (h < 0 || w < 0 || d < 0) {
1120 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1121 }
1122 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1123 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1124 }
1125 }
1126 }
1127
1128 /**
1129 * @hide
1130 *
1131 */
1132 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1133 mRS.validate();
1134 validate3DRange(xoff, yoff, zoff, w, h, d);
1135 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Jason Sams6fcf2e12013-11-06 11:22:02 -08001136 w, h, d, data, data.length, Element.DataType.SIGNED_8);
Jason Samsb05d6892013-04-09 15:59:24 -07001137 }
1138
1139 /**
1140 * @hide
1141 *
1142 */
1143 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1144 mRS.validate();
1145 validate3DRange(xoff, yoff, zoff, w, h, d);
1146 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Jason Sams6fcf2e12013-11-06 11:22:02 -08001147 w, h, d, data, data.length * 2, Element.DataType.SIGNED_16);
Jason Samsb05d6892013-04-09 15:59:24 -07001148 }
1149
1150 /**
1151 * @hide
1152 *
1153 */
1154 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
1155 mRS.validate();
1156 validate3DRange(xoff, yoff, zoff, w, h, d);
1157 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Jason Sams6fcf2e12013-11-06 11:22:02 -08001158 w, h, d, data, data.length * 4, Element.DataType.SIGNED_32);
Jason Samsb05d6892013-04-09 15:59:24 -07001159 }
1160
1161 /**
1162 * @hide
1163 *
1164 */
1165 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
1166 mRS.validate();
1167 validate3DRange(xoff, yoff, zoff, w, h, d);
1168 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Jason Sams6fcf2e12013-11-06 11:22:02 -08001169 w, h, d, data, data.length * 4, Element.DataType.FLOAT_32);
Jason Samsb05d6892013-04-09 15:59:24 -07001170 }
1171
1172
1173 /**
1174 * @hide
1175 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001176 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001177 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001178 * @param xoff X offset of the region to update in this Allocation
1179 * @param yoff Y offset of the region to update in this Allocation
1180 * @param zoff Z offset of the region to update in this Allocation
1181 * @param w Width of the region to update
1182 * @param h Height of the region to update
1183 * @param d Depth of the region to update
Jason Samsb05d6892013-04-09 15:59:24 -07001184 * @param data to be placed into the allocation
1185 */
1186 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1187 validateIsInt8();
1188 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1189 }
1190
1191 /**
1192 * @hide
1193 *
1194 */
1195 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1196 validateIsInt16();
1197 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1198 }
1199
1200 /**
1201 * @hide
1202 *
1203 */
1204 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
1205 validateIsInt32();
1206 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1207 }
1208
1209 /**
1210 * @hide
1211 *
1212 */
1213 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
1214 validateIsFloat32();
1215 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1216 }
1217
1218 /**
1219 * @hide
1220 * Copy a rectangular region into the allocation from another
1221 * allocation.
1222 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001223 * @param xoff X offset of the region to update in this Allocation
1224 * @param yoff Y offset of the region to update in this Allocation
1225 * @param zoff Z offset of the region to update in this Allocation
1226 * @param w Width of the region to update.
1227 * @param h Height of the region to update.
1228 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001229 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001230 * @param dataXoff X offset of the region in the source Allocation
1231 * @param dataYoff Y offset of the region in the source Allocation
1232 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001233 */
1234 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1235 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1236 mRS.validate();
1237 validate3DRange(xoff, yoff, zoff, w, h, d);
1238 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1239 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1240 data.mSelectedLOD);
1241 }
1242
Jason Samsfa445b92011-01-07 17:00:07 -08001243
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001244 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001245 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1246 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001247 *
1248 * @param b The bitmap to be set from the Allocation.
1249 */
Jason Samsfa445b92011-01-07 17:00:07 -08001250 public void copyTo(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001251 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsfb9f82c2011-01-12 14:53:25 -08001252 mRS.validate();
1253 validateBitmapFormat(b);
1254 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -07001255 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001256 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001257 }
1258
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001259 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001260 * Copy from the Allocation into a byte array. The array must be at least
1261 * as large as the Allocation. The allocation must be of an 8 bit integer
1262 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001263 *
1264 * @param d The array to be set from the Allocation.
1265 */
Jason Samsfa445b92011-01-07 17:00:07 -08001266 public void copyTo(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001267 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001268 validateIsInt8();
Jason Sams771bebb2009-12-07 12:40:12 -08001269 mRS.validate();
Jason Sams29868dfa2013-11-06 15:08:07 -08001270 mRS.nAllocationRead(getID(mRS), d, Element.DataType.SIGNED_8);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001271 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams40a29e82009-08-10 14:55:26 -07001272 }
1273
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001274 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001275 * Copy from the Allocation into a short array. The array must be at least
1276 * as large as the Allocation. The allocation must be of an 16 bit integer
1277 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001278 *
1279 * @param d The array to be set from the Allocation.
1280 */
Jason Samsfa445b92011-01-07 17:00:07 -08001281 public void copyTo(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001282 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001283 validateIsInt16();
Jason Samsfa445b92011-01-07 17:00:07 -08001284 mRS.validate();
Jason Sams29868dfa2013-11-06 15:08:07 -08001285 mRS.nAllocationRead(getID(mRS), d, Element.DataType.SIGNED_16);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001286 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001287 }
1288
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001289 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001290 * Copy from the Allocation into a int array. The array must be at least as
1291 * large as the Allocation. The allocation must be of an 32 bit integer
1292 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001293 *
1294 * @param d The array to be set from the Allocation.
1295 */
Jason Samsfa445b92011-01-07 17:00:07 -08001296 public void copyTo(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001297 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001298 validateIsInt32();
Jason Samsfa445b92011-01-07 17:00:07 -08001299 mRS.validate();
Jason Sams29868dfa2013-11-06 15:08:07 -08001300 mRS.nAllocationRead(getID(mRS), d, Element.DataType.SIGNED_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001301 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001302 }
1303
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001304 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001305 * Copy from the Allocation into a float array. The array must be at least
1306 * as large as the Allocation. The allocation must be of an 32 bit float
1307 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001308 *
1309 * @param d The array to be set from the Allocation.
1310 */
Jason Samsfa445b92011-01-07 17:00:07 -08001311 public void copyTo(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001312 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001313 validateIsFloat32();
Jason Sams771bebb2009-12-07 12:40:12 -08001314 mRS.validate();
Jason Sams29868dfa2013-11-06 15:08:07 -08001315 mRS.nAllocationRead(getID(mRS), d, Element.DataType.FLOAT_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001316 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams40a29e82009-08-10 14:55:26 -07001317 }
1318
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001319 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001320 * Resize a 1D allocation. The contents of the allocation are preserved.
1321 * If new elements are allocated objects are created with null contents and
1322 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001323 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001324 * <p>If the new region is smaller the references of any objects outside the
1325 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001326 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001327 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001328 *
1329 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001330 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001331 * @deprecated RenderScript objects should be immutable once created. The
1332 * replacement is to create a new allocation and copy the contents.
Jason Samsf7086092011-01-12 13:28:37 -08001333 */
Jason Sams31a7e422010-10-26 13:09:17 -07001334 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001335 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001336 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001337 }
Jason Samse07694b2012-04-03 15:36:36 -07001338 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001339 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001340
Tim Murray7a629fa2013-11-19 12:45:54 -08001341 long typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001342 mType = new Type(typeID, mRS);
1343 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001344 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001345 }
1346
Jason Samsb8c5a842009-07-31 20:40:47 -07001347
1348 // creation
1349
Jason Sams49a05d72010-12-29 14:31:29 -08001350 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001351 static {
1352 mBitmapOptions.inScaled = false;
1353 }
1354
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001355 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001356 * Creates a new Allocation with the given {@link
1357 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001358 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001359 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001360 * @param mips specifies desired mipmap behaviour for the
1361 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001362 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001363 * utilized
1364 */
1365 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001366 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
Jason Sams771bebb2009-12-07 12:40:12 -08001367 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001368 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001369 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001370 }
Tim Murray7a629fa2013-11-19 12:45:54 -08001371 long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001372 if (id == 0) {
1373 throw new RSRuntimeException("Allocation creation failed.");
1374 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001375 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams857d0c72011-11-23 15:02:15 -08001376 return new Allocation(id, rs, type, usage);
1377 }
1378
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001379 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001380 * Creates an Allocation with the size specified by the type and no mipmaps
1381 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001382 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001383 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001384 * @param type renderscript type describing data layout
1385 * @param usage bit field specifying how the allocation is
1386 * utilized
1387 *
1388 * @return allocation
1389 */
Jason Samse5d37122010-12-16 00:33:33 -08001390 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1391 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1392 }
1393
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001394 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001395 * Creates an Allocation for use by scripts with a given {@link
1396 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001397 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001398 * @param rs Context to which the Allocation will belong.
1399 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001400 *
1401 * @return allocation
1402 */
Jason Sams5476b452010-12-08 16:14:36 -08001403 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001404 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001405 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001406
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001407 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001408 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001409 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001410 * @param rs Context to which the Allocation will belong.
1411 * @param e Element to use in the Allocation
1412 * @param count the number of Elements in the Allocation
1413 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001414 * utilized
1415 *
1416 * @return allocation
1417 */
Jason Sams5476b452010-12-08 16:14:36 -08001418 static public Allocation createSized(RenderScript rs, Element e,
1419 int count, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001420 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
Jason Sams771bebb2009-12-07 12:40:12 -08001421 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001422 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001423 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001424 Type t = b.create();
1425
Tim Murray7a629fa2013-11-19 12:45:54 -08001426 long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001427 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001428 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001429 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001430 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001431 return new Allocation(id, rs, t, usage);
1432 }
1433
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001434 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001435 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001436 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001437 * @param rs Context to which the Allocation will belong.
1438 * @param e Element to use in the Allocation
1439 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001440 *
1441 * @return allocation
1442 */
Jason Sams5476b452010-12-08 16:14:36 -08001443 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001444 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001445 }
1446
Jason Sams49a05d72010-12-29 14:31:29 -08001447 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001448 final Bitmap.Config bc = b.getConfig();
1449 if (bc == Bitmap.Config.ALPHA_8) {
1450 return Element.A_8(rs);
1451 }
1452 if (bc == Bitmap.Config.ARGB_4444) {
1453 return Element.RGBA_4444(rs);
1454 }
1455 if (bc == Bitmap.Config.ARGB_8888) {
1456 return Element.RGBA_8888(rs);
1457 }
1458 if (bc == Bitmap.Config.RGB_565) {
1459 return Element.RGB_565(rs);
1460 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001461 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001462 }
1463
Jason Sams49a05d72010-12-29 14:31:29 -08001464 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001465 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001466 Element e = elementFromBitmap(rs, b);
1467 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001468 tb.setX(b.getWidth());
1469 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001470 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001471 return tb.create();
1472 }
1473
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001474 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001475 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001476 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001477 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001478 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001479 * @param mips specifies desired mipmap behaviour for the
1480 * allocation
1481 * @param usage bit field specifying how the allocation is
1482 * utilized
1483 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001484 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001485 *
1486 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001487 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001488 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001489 int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001490 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
Jason Sams771bebb2009-12-07 12:40:12 -08001491 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001492
1493 // WAR undocumented color formats
1494 if (b.getConfig() == null) {
1495 if ((usage & USAGE_SHARED) != 0) {
1496 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1497 }
1498 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1499 Canvas c = new Canvas(newBitmap);
1500 c.drawBitmap(b, 0, 0, null);
1501 return createFromBitmap(rs, newBitmap, mips, usage);
1502 }
1503
Jason Sams5476b452010-12-08 16:14:36 -08001504 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001505
Tim Murraya3145512012-12-04 17:59:29 -08001506 // enable optimized bitmap path only with no mipmap and script-only usage
1507 if (mips == MipmapControl.MIPMAP_NONE &&
1508 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Tim Murray78e64942013-04-09 17:28:56 -07001509 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
Tim Murray7a629fa2013-11-19 12:45:54 -08001510 long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
Tim Murraya3145512012-12-04 17:59:29 -08001511 if (id == 0) {
1512 throw new RSRuntimeException("Load failed.");
1513 }
1514
1515 // keep a reference to the Bitmap around to prevent GC
1516 Allocation alloc = new Allocation(id, rs, t, usage);
1517 alloc.setBitmap(b);
1518 return alloc;
1519 }
1520
Jason Sams9bf18922013-04-13 19:48:36 -07001521
Tim Murray7a629fa2013-11-19 12:45:54 -08001522 long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001523 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001524 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001525 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001526 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001527 return new Allocation(id, rs, t, usage);
1528 }
1529
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001530 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001531 * Returns the handle to a raw buffer that is being managed by the screen
1532 * compositor. This operation is only valid for Allocations with {@link
1533 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001534 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001535 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001536 *
1537 */
1538 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08001539 if ((mUsage & USAGE_IO_INPUT) == 0) {
1540 throw new RSInvalidStateException("Allocation is not a surface texture.");
1541 }
1542 return mRS.nAllocationGetSurface(getID(mRS));
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001543 }
1544
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001545 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001546 * Associate a {@link android.view.Surface} with this Allocation. This
1547 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001548 *
1549 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001550 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001551 public void setSurface(Surface sur) {
1552 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001553 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1554 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1555 }
1556
Jason Samse07694b2012-04-03 15:36:36 -07001557 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001558 }
1559
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001560 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001561 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08001562 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001563 * <p>With target API version 18 or greater, this Allocation will be created
1564 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
1565 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
1566 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001567 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001568 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001569 * @param b bitmap source for the allocation data
1570 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001571 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001572 *
1573 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001574 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08001575 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1576 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07001577 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08001578 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001579 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1580 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001581 }
1582
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001583 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001584 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1585 * containing the horizontal list of cube faces. Each face must be a square,
1586 * have the same size as all other faces, and have a width that is a power
1587 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001588 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001589 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001590 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001591 * format: right, left, top, bottom, front, back
1592 * @param mips specifies desired mipmap behaviour for the cubemap
1593 * @param usage bit field specifying how the cubemap is utilized
1594 *
1595 * @return allocation containing cubemap data
1596 *
1597 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001598 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001599 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001600 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001601 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001602
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001603 int height = b.getHeight();
1604 int width = b.getWidth();
1605
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001606 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001607 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1608 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001609 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001610 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001611 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001612 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001613 if (!isPow2) {
1614 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1615 }
1616
1617 Element e = elementFromBitmap(rs, b);
1618 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001619 tb.setX(height);
1620 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001621 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001622 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001623 Type t = tb.create();
1624
Tim Murray7a629fa2013-11-19 12:45:54 -08001625 long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001626 if(id == 0) {
1627 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1628 }
Jason Sams5476b452010-12-08 16:14:36 -08001629 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001630 }
1631
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001632 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001633 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
1634 * from a {@link android.graphics.Bitmap} containing the horizontal list of
1635 * cube faces. Each face must be a square, have the same size as all other
1636 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001637 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001638 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001639 * @param b bitmap with cubemap faces layed out in the following
1640 * format: right, left, top, bottom, front, back
1641 *
1642 * @return allocation containing cubemap data
1643 *
1644 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001645 static public Allocation createCubemapFromBitmap(RenderScript rs,
1646 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001647 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001648 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001649 }
1650
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001651 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001652 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
1653 * objects containing the cube faces. Each face must be a square, have the
1654 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001655 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001656 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001657 * @param xpos cubemap face in the positive x direction
1658 * @param xneg cubemap face in the negative x direction
1659 * @param ypos cubemap face in the positive y direction
1660 * @param yneg cubemap face in the negative y direction
1661 * @param zpos cubemap face in the positive z direction
1662 * @param zneg cubemap face in the negative z direction
1663 * @param mips specifies desired mipmap behaviour for the cubemap
1664 * @param usage bit field specifying how the cubemap is utilized
1665 *
1666 * @return allocation containing cubemap data
1667 *
1668 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001669 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1670 Bitmap xpos,
1671 Bitmap xneg,
1672 Bitmap ypos,
1673 Bitmap yneg,
1674 Bitmap zpos,
1675 Bitmap zneg,
1676 MipmapControl mips,
1677 int usage) {
1678 int height = xpos.getHeight();
1679 if (xpos.getWidth() != height ||
1680 xneg.getWidth() != height || xneg.getHeight() != height ||
1681 ypos.getWidth() != height || ypos.getHeight() != height ||
1682 yneg.getWidth() != height || yneg.getHeight() != height ||
1683 zpos.getWidth() != height || zpos.getHeight() != height ||
1684 zneg.getWidth() != height || zneg.getHeight() != height) {
1685 throw new RSIllegalArgumentException("Only square cube map faces supported");
1686 }
1687 boolean isPow2 = (height & (height - 1)) == 0;
1688 if (!isPow2) {
1689 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1690 }
1691
1692 Element e = elementFromBitmap(rs, xpos);
1693 Type.Builder tb = new Type.Builder(rs, e);
1694 tb.setX(height);
1695 tb.setY(height);
1696 tb.setFaces(true);
1697 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1698 Type t = tb.create();
1699 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1700
1701 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001702 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001703 adapter.copyFrom(xpos);
1704 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1705 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001706 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001707 adapter.copyFrom(ypos);
1708 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1709 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001710 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001711 adapter.copyFrom(zpos);
1712 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1713 adapter.copyFrom(zneg);
1714
1715 return cubemap;
1716 }
1717
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001718 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001719 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
1720 * from 6 {@link android.graphics.Bitmap} objects containing the cube
1721 * faces. Each face must be a square, have the same size as all other faces,
1722 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001723 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001724 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001725 * @param xpos cubemap face in the positive x direction
1726 * @param xneg cubemap face in the negative x direction
1727 * @param ypos cubemap face in the positive y direction
1728 * @param yneg cubemap face in the negative y direction
1729 * @param zpos cubemap face in the positive z direction
1730 * @param zneg cubemap face in the negative z direction
1731 *
1732 * @return allocation containing cubemap data
1733 *
1734 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001735 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1736 Bitmap xpos,
1737 Bitmap xneg,
1738 Bitmap ypos,
1739 Bitmap yneg,
1740 Bitmap zpos,
1741 Bitmap zneg) {
1742 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1743 zpos, zneg, MipmapControl.MIPMAP_NONE,
1744 USAGE_GRAPHICS_TEXTURE);
1745 }
1746
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001747 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001748 * Creates an Allocation from the Bitmap referenced
1749 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001750 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001751 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001752 * @param res application resources
1753 * @param id resource id to load the data from
1754 * @param mips specifies desired mipmap behaviour for the
1755 * allocation
1756 * @param usage bit field specifying how the allocation is
1757 * utilized
1758 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001759 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001760 *
1761 */
Jason Sams5476b452010-12-08 16:14:36 -08001762 static public Allocation createFromBitmapResource(RenderScript rs,
1763 Resources res,
1764 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001765 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001766 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001767
Jason Sams771bebb2009-12-07 12:40:12 -08001768 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07001769 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
1770 throw new RSIllegalArgumentException("Unsupported usage specified.");
1771 }
Jason Sams5476b452010-12-08 16:14:36 -08001772 Bitmap b = BitmapFactory.decodeResource(res, id);
1773 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1774 b.recycle();
1775 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001776 }
1777
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001778 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001779 * Creates a non-mipmapped Allocation to use as a graphics texture from the
1780 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001781 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001782 * <p>With target API version 18 or greater, this allocation will be created
1783 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
1784 * target API version 17 or lower, this allocation will be created with
1785 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08001786 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001787 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001788 * @param res application resources
1789 * @param id resource id to load the data from
1790 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001791 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001792 *
1793 */
Jason Sams5476b452010-12-08 16:14:36 -08001794 static public Allocation createFromBitmapResource(RenderScript rs,
1795 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001796 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08001797 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1798 return createFromBitmapResource(rs, res, id,
1799 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07001800 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08001801 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001802 return createFromBitmapResource(rs, res, id,
1803 MipmapControl.MIPMAP_NONE,
1804 USAGE_GRAPHICS_TEXTURE);
1805 }
1806
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001807 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001808 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001809 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001810 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001811 * @param str string to create the allocation from
1812 * @param usage bit field specifying how the allocaiton is
1813 * utilized
1814 *
1815 */
Jason Sams5476b452010-12-08 16:14:36 -08001816 static public Allocation createFromString(RenderScript rs,
1817 String str,
1818 int usage) {
1819 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001820 byte[] allocArray = null;
1821 try {
1822 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001823 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001824 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001825 return alloc;
1826 }
1827 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001828 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001829 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001830 }
Jason Sams739c8262013-04-11 18:07:52 -07001831
1832 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001833 * Interface to handle notification when new buffers are available via
1834 * {@link #USAGE_IO_INPUT}. An application will receive one notification
1835 * when a buffer is available. Additional buffers will not trigger new
1836 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07001837 */
Jason Sams42ef2382013-08-29 13:30:59 -07001838 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07001839 public void onBufferAvailable(Allocation a);
1840 }
1841
1842 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001843 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07001844 *
Jason Sams42ef2382013-08-29 13:30:59 -07001845 * @param callback instance of the OnBufferAvailableListener
1846 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07001847 */
Jason Sams42ef2382013-08-29 13:30:59 -07001848 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07001849 synchronized(mAllocationMap) {
Tim Murray7a629fa2013-11-19 12:45:54 -08001850 mAllocationMap.put(new Long(getID(mRS)), this);
Jason Sams739c8262013-04-11 18:07:52 -07001851 mBufferNotifier = callback;
1852 }
1853 }
1854
1855 static void sendBufferNotification(int id) {
1856 synchronized(mAllocationMap) {
Tim Murray7a629fa2013-11-19 12:45:54 -08001857 Allocation a = mAllocationMap.get(new Long(id));
Jason Sams739c8262013-04-11 18:07:52 -07001858
1859 if ((a != null) && (a.mBufferNotifier != null)) {
1860 a.mBufferNotifier.onBufferAvailable(a);
1861 }
1862 }
1863 }
1864
Jason Samsb8c5a842009-07-31 20:40:47 -07001865}
1866