blob: 362b586376068ccbb703d50df9101db22e27f9bf [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
Stephen Hines9069ee82012-02-13 18:25:54 -08002 * Copyright (C) 2008-2012 The Android Open Source Project
Jason Samsb8c5a842009-07-31 20:40:47 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
Jason Samsb8c5a842009-07-31 20:40:47 -070019import java.io.IOException;
20import java.io.InputStream;
Jason Sams739c8262013-04-11 18:07:52 -070021import java.util.HashMap;
Jason Samsb8c5a842009-07-31 20:40:47 -070022import android.content.res.Resources;
Romain Guy650a3eb2009-08-31 14:06:43 -070023import android.content.res.AssetManager;
Jason Samsb8c5a842009-07-31 20:40:47 -070024import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
Jason Samsfb9aa9f2012-03-28 15:30:07 -070026import android.view.Surface;
Jason Sams615e7ce2012-01-13 14:01:20 -080027import android.graphics.SurfaceTexture;
Jason Samsb8c5a842009-07-31 20:40:47 -070028import android.util.Log;
Romain Guy650a3eb2009-08-31 14:06:43 -070029import android.util.TypedValue;
Tim Murrayabd5db92013-02-28 11:45:22 -080030import android.graphics.Canvas;
Tim Murray6d7a53c2013-05-23 16:59:23 -070031import android.os.Trace;
Jason Samsb8c5a842009-07-31 20:40:47 -070032
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070033/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070034 * <p> This class provides the primary method through which data is passed to
35 * and from RenderScript kernels. An Allocation provides the backing store for
36 * a given {@link android.renderscript.Type}. </p>
Jason Samsa23d4e72011-01-04 18:59:12 -080037 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070038 * <p>An Allocation also contains a set of usage flags that denote how the
39 * Allocation could be used. For example, an Allocation may have usage flags
40 * specifying that it can be used from a script as well as input to a {@link
41 * android.renderscript.Sampler}. A developer must synchronize across these
42 * different usages using {@link android.renderscript.Allocation#syncAll} in
43 * order to ensure that different users of the Allocation have a consistent view
44 * of memory. For example, in the case where an Allocation is used as the output
45 * of one kernel and as Sampler input in a later kernel, a developer must call
46 * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
47 * second kernel to ensure correctness.
Jason Samsa23d4e72011-01-04 18:59:12 -080048 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070049 * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
50 * more complex Element types, the {@link #copyFromUnchecked} methods can be
51 * used to copy from byte arrays or similar constructs.</p>
Jason Samsb8c5a842009-07-31 20:40:47 -070052 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080053 * <div class="special reference">
54 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070055 * <p>For more information about creating an application that uses RenderScript, read the
56 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080057 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070058 **/
59public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070060 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080061 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080062 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070063 Allocation mAdaptedAllocation;
64
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;
Jason Sams739c8262013-04-11 18:07:52 -070080 static HashMap<Integer, Allocation> mAllocationMap =
81 new HashMap<Integer, Allocation>();
82 IoInputNotifier 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 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700129 * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
130 * consumer. This usage will cause the Allocation to be created as
131 * 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 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700137 * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
138 * producer. The dimensions and format of the {@link
139 * android.graphics.SurfaceTexture} will be forced to those of the
140 * 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
190 private int getIDSafe() {
191 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() {
226 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700227 }
228
Jason Sams452a7662011-07-07 16:05:18 -0700229 private void updateCacheInfo(Type t) {
230 mCurrentDimX = t.getX();
231 mCurrentDimY = t.getY();
232 mCurrentDimZ = t.getZ();
233 mCurrentCount = mCurrentDimX;
234 if (mCurrentDimY > 1) {
235 mCurrentCount *= mCurrentDimY;
236 }
237 if (mCurrentDimZ > 1) {
238 mCurrentCount *= mCurrentDimZ;
239 }
240 }
Jason Samsba862d12011-07-07 15:24:42 -0700241
Tim Murraya3145512012-12-04 17:59:29 -0800242 private void setBitmap(Bitmap b) {
243 mBitmap = b;
244 }
245
Jason Sams5476b452010-12-08 16:14:36 -0800246 Allocation(int id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700247 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800248 if ((usage & ~(USAGE_SCRIPT |
249 USAGE_GRAPHICS_TEXTURE |
250 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700251 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800252 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800253 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800254 USAGE_IO_OUTPUT |
255 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800256 throw new RSIllegalArgumentException("Unknown usage specified.");
257 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800258
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700259 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800260 mWriteAllowed = false;
261
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700262 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800263 USAGE_GRAPHICS_TEXTURE |
264 USAGE_SCRIPT)) != 0) {
265 throw new RSIllegalArgumentException("Invalid usage combination.");
266 }
267 }
Jason Sams9bf18922013-04-13 19:48:36 -0700268
Jason Sams5476b452010-12-08 16:14:36 -0800269 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800270 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700271
Jason Sams452a7662011-07-07 16:05:18 -0700272 if (t != null) {
273 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700274 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700275 }
276
Jason Samsb97b2512011-01-16 15:04:08 -0800277 private void validateIsInt32() {
278 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
279 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
280 return;
281 }
282 throw new RSIllegalArgumentException(
283 "32 bit integer source does not match allocation type " + mType.mElement.mType);
284 }
285
286 private void validateIsInt16() {
287 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
288 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
289 return;
290 }
291 throw new RSIllegalArgumentException(
292 "16 bit integer source does not match allocation type " + mType.mElement.mType);
293 }
294
295 private void validateIsInt8() {
296 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
297 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
298 return;
299 }
300 throw new RSIllegalArgumentException(
301 "8 bit integer source does not match allocation type " + mType.mElement.mType);
302 }
303
304 private void validateIsFloat32() {
305 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
306 return;
307 }
308 throw new RSIllegalArgumentException(
309 "32 bit float source does not match allocation type " + mType.mElement.mType);
310 }
311
312 private void validateIsObject() {
313 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
314 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
315 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
316 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
317 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
318 (mType.mElement.mType == Element.DataType.RS_MESH) ||
319 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
320 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
321 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
322 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
323 return;
324 }
325 throw new RSIllegalArgumentException(
326 "Object source does not match allocation type " + mType.mElement.mType);
327 }
328
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700329 @Override
330 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800331 super.updateFromNative();
Jason Samse07694b2012-04-03 15:36:36 -0700332 int typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700333 if(typeID != 0) {
334 mType = new Type(typeID, mRS);
335 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700336 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700337 }
338 }
339
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700340 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700341 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700342 *
343 * @return Type
344 *
345 */
Jason Samsea87e962010-01-12 12:12:28 -0800346 public Type getType() {
347 return mType;
348 }
349
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700350 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700351 * Propagate changes from one usage of the Allocation to the
352 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700353 *
354 */
Jason Sams5476b452010-12-08 16:14:36 -0800355 public void syncAll(int srcLocation) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700356 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
Jason Sams5476b452010-12-08 16:14:36 -0800357 switch (srcLocation) {
Jason Sams5476b452010-12-08 16:14:36 -0800358 case USAGE_GRAPHICS_TEXTURE:
Tim Murray78e64942013-04-09 17:28:56 -0700359 case USAGE_SCRIPT:
360 if ((mUsage & USAGE_SHARED) != 0) {
361 copyFrom(mBitmap);
362 }
363 break;
364 case USAGE_GRAPHICS_CONSTANTS:
Jason Sams5476b452010-12-08 16:14:36 -0800365 case USAGE_GRAPHICS_VERTEX:
366 break;
Tim Murray78e64942013-04-09 17:28:56 -0700367 case USAGE_SHARED:
368 if ((mUsage & USAGE_SHARED) != 0) {
369 copyTo(mBitmap);
370 }
371 break;
Jason Sams5476b452010-12-08 16:14:36 -0800372 default:
373 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
374 }
375 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700376 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700377 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800378 }
379
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700380 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700381 * Send a buffer to the output stream. The contents of the Allocation will
382 * be undefined after this operation. This operation is only valid if {@link
383 * #USAGE_IO_OUTPUT} is set on the Allocation.
384 *
Jason Sams163766c2012-02-15 12:04:24 -0800385 *
Jason Sams163766c2012-02-15 12:04:24 -0800386 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700387 public void ioSend() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700388 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
Jason Sams163766c2012-02-15 12:04:24 -0800389 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
390 throw new RSIllegalArgumentException(
391 "Can only send buffer if IO_OUTPUT usage specified.");
392 }
393 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700394 mRS.nAllocationIoSend(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700395 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800396 }
397
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700398 /**
Jason Samsc5f519c2012-03-29 17:58:15 -0700399 * Delete once code is updated.
400 * @hide
401 */
402 public void ioSendOutput() {
403 ioSend();
404 }
405
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700406 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700407 * Receive the latest input into the Allocation. This operation
408 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800409 *
Jason Sams163766c2012-02-15 12:04:24 -0800410 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700411 public void ioReceive() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700412 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
Jason Sams163766c2012-02-15 12:04:24 -0800413 if ((mUsage & USAGE_IO_INPUT) == 0) {
414 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700415 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800416 }
417 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700418 mRS.nAllocationIoReceive(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700419 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800420 }
421
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700422 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700423 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700424 *
425 * @param d Source array.
426 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800427 public void copyFrom(BaseObj[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700428 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Sams771bebb2009-12-07 12:40:12 -0800429 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800430 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700431 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800432 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700433 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800434 }
435 int i[] = new int[d.length];
436 for (int ct=0; ct < d.length; ct++) {
Jason Samse07694b2012-04-03 15:36:36 -0700437 i[ct] = d[ct].getID(mRS);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800438 }
Jason Samsba862d12011-07-07 15:24:42 -0700439 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700440 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700441 }
442
Jason Samsfb9f82c2011-01-12 14:53:25 -0800443 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800444 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800445 if (bc == null) {
446 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
447 }
Jason Sams252c0782011-01-11 17:42:52 -0800448 switch (bc) {
449 case ALPHA_8:
450 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
451 throw new RSIllegalArgumentException("Allocation kind is " +
452 mType.getElement().mKind + ", type " +
453 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700454 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800455 " bytes, passed bitmap was " + bc);
456 }
457 break;
458 case ARGB_8888:
459 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700460 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800461 throw new RSIllegalArgumentException("Allocation kind is " +
462 mType.getElement().mKind + ", type " +
463 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700464 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800465 " bytes, passed bitmap was " + bc);
466 }
467 break;
468 case RGB_565:
469 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700470 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800471 throw new RSIllegalArgumentException("Allocation kind is " +
472 mType.getElement().mKind + ", type " +
473 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700474 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800475 " bytes, passed bitmap was " + bc);
476 }
477 break;
478 case ARGB_4444:
479 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700480 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800481 throw new RSIllegalArgumentException("Allocation kind is " +
482 mType.getElement().mKind + ", type " +
483 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700484 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800485 " bytes, passed bitmap was " + bc);
486 }
487 break;
488
489 }
Jason Sams4ef66502010-12-10 16:03:15 -0800490 }
491
Jason Samsfb9f82c2011-01-12 14:53:25 -0800492 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700493 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800494 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
495 }
496 }
497
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700498 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700499 * Copy into this Allocation from an array. This method does not guarantee
500 * that the Allocation is compatible with the input buffer; it copies memory
501 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800502 *
503 * @param d the source data array
504 */
505 public void copyFromUnchecked(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700506 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800507 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700508 if (mCurrentDimZ > 0) {
509 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
510 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800511 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
512 } else {
513 copy1DRangeFromUnchecked(0, mCurrentCount, d);
514 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700515 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800516 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700517
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700518 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700519 * Copy into this Allocation from an array. This method does not guarantee
520 * that the Allocation is compatible with the input buffer; it copies memory
521 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800522 *
523 * @param d the source data array
524 */
525 public void copyFromUnchecked(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700526 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800527 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700528 if (mCurrentDimZ > 0) {
529 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
530 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800531 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
532 } else {
533 copy1DRangeFromUnchecked(0, mCurrentCount, d);
534 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700535 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800536 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700537
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700538 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700539 * Copy into this Allocation from an array. This method does not guarantee
540 * that the Allocation is compatible with the input buffer; it copies memory
541 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800542 *
543 * @param d the source data array
544 */
545 public void copyFromUnchecked(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700546 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800547 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700548 if (mCurrentDimZ > 0) {
549 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
550 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800551 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
552 } else {
553 copy1DRangeFromUnchecked(0, mCurrentCount, d);
554 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700555 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800556 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700557
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700558 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700559 * Copy into this Allocation from an array. This method does not guarantee
560 * that the Allocation is compatible with the input buffer; it copies memory
561 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800562 *
563 * @param d the source data array
564 */
565 public void copyFromUnchecked(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700566 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800567 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700568 if (mCurrentDimZ > 0) {
569 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
570 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800571 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
572 } else {
573 copy1DRangeFromUnchecked(0, mCurrentCount, d);
574 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700575 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800576 }
577
Tim Murray6d7a53c2013-05-23 16:59:23 -0700578
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700579 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700580 * Copy into this Allocation from an array. This variant is type checked
581 * and will generate exceptions if the Allocation's {@link
582 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800583 *
584 * @param d the source data array
585 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800586 public void copyFrom(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700587 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800588 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700589 if (mCurrentDimZ > 0) {
590 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
591 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800592 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
593 } else {
594 copy1DRangeFrom(0, mCurrentCount, d);
595 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700596 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800597 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800598
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700599 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700600 * Copy into this Allocation from an array. This variant is type checked
601 * and will generate exceptions if the Allocation's {@link
602 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800603 *
604 * @param d the source data array
605 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800606 public void copyFrom(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700607 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800608 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700609 if (mCurrentDimZ > 0) {
610 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
611 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800612 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
613 } else {
614 copy1DRangeFrom(0, mCurrentCount, d);
615 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700616 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800617 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800618
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700619 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700620 * Copy into this Allocation from an array. This variant is type checked
621 * and will generate exceptions if the Allocation's {@link
622 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800623 *
624 * @param d the source data array
625 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800626 public void copyFrom(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700627 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800628 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700629 if (mCurrentDimZ > 0) {
630 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
631 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800632 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
633 } else {
634 copy1DRangeFrom(0, mCurrentCount, d);
635 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700636 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800637 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800638
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700639 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700640 * Copy into this Allocation from an array. This variant is type checked
641 * and will generate exceptions if the Allocation's {@link
642 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800643 *
644 * @param d the source data array
645 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800646 public void copyFrom(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700647 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800648 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700649 if (mCurrentDimZ > 0) {
650 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
651 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800652 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
653 } else {
654 copy1DRangeFrom(0, mCurrentCount, d);
655 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700656 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800657 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800658
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700659 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700660 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
661 * height, width, and format of the bitmap must match the existing
662 * allocation.
663 *
664 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
665 * android.graphics.Bitmap} used to create the Allocation with {@link
666 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
667 * this will synchronize the Allocation with the latest data from the {@link
668 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800669 *
670 * @param b the source bitmap
671 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800672 public void copyFrom(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700673 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsfb9f82c2011-01-12 14:53:25 -0800674 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -0800675 if (b.getConfig() == null) {
676 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
677 Canvas c = new Canvas(newBitmap);
678 c.drawBitmap(b, 0, 0, null);
679 copyFrom(newBitmap);
680 return;
681 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800682 validateBitmapSize(b);
683 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700684 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700685 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700686 }
687
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700688 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700689 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700690 * must be identical.
691 *
692 * @param a the source allocation
693 */
694 public void copyFrom(Allocation a) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700695 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Tim Murrayf671fb02012-10-03 13:50:05 -0700696 mRS.validate();
697 if (!mType.equals(a.getType())) {
698 throw new RSIllegalArgumentException("Types of allocations must match.");
699 }
700 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700701 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700702 }
703
Tim Murrayf671fb02012-10-03 13:50:05 -0700704 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700705 * This is only intended to be used by auto-generated code reflected from
706 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800707 *
708 * @param xoff
709 * @param fp
710 */
Jason Sams21b41032011-01-16 15:05:41 -0800711 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800712 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700713 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700714 final byte[] data = fp.getData();
715
716 int count = data.length / eSize;
717 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800718 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700719 " not divisible by element size " + eSize + ".");
720 }
Jason Samsba862d12011-07-07 15:24:42 -0700721 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700722 }
723
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700724 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700725 * This is only intended to be used by auto-generated code reflected from
726 * the RenderScript script files.
Jason Samsfa445b92011-01-07 17:00:07 -0800727 *
728 * @param xoff
729 * @param component_number
730 * @param fp
731 */
Jason Sams21b41032011-01-16 15:05:41 -0800732 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800733 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700734 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800735 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700736 }
737 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800738 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700739 }
740
741 final byte[] data = fp.getData();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700742 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800743 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700744
745 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800746 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700747 " does not match component size " + eSize + ".");
748 }
749
Jason Sams48fe5342011-07-08 13:52:30 -0700750 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Jason Samsba862d12011-07-07 15:24:42 -0700751 component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700752 }
753
Jason Sams768bc022009-09-21 19:41:04 -0700754 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800755 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700756 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800757 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700758 }
759 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800760 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700761 }
Jason Samsba862d12011-07-07 15:24:42 -0700762 if((off + count) > mCurrentCount) {
763 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700764 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700765 }
Jason Samsba862d12011-07-07 15:24:42 -0700766 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800767 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700768 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700769 }
770
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700771 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700772 * Generate a mipmap chain. This is only valid if the Type of the Allocation
773 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800774 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700775 * <p>This function will generate a complete set of mipmaps from the top
776 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800777 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700778 * <p>If the Allocation is also using other memory spaces, a call to {@link
779 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800780 */
781 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700782 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800783 }
784
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700785 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700786 * Copy an array into part of this Allocation. This method does not
787 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800788 *
789 * @param off The offset of the first element to be copied.
790 * @param count The number of elements to be copied.
791 * @param d the source data array
792 */
793 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700794 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700795 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700796 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700797 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700798 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams768bc022009-09-21 19:41:04 -0700799 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700800
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700801 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700802 * Copy an array into part of this Allocation. This method does not
803 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800804 *
805 * @param off The offset of the first element to be copied.
806 * @param count The number of elements to be copied.
807 * @param d the source data array
808 */
809 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700810 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700811 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700812 data1DChecks(off, count, d.length * 2, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700813 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700814 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams768bc022009-09-21 19:41:04 -0700815 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700816
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700817 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700818 * Copy an array into part of this Allocation. This method does not
819 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800820 *
821 * @param off The offset of the first element to be copied.
822 * @param count The number of elements to be copied.
823 * @param d the source data array
824 */
825 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700826 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700827 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700828 data1DChecks(off, count, d.length, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700829 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700830 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams768bc022009-09-21 19:41:04 -0700831 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700832
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700833 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700834 * Copy an array into part of this Allocation. This method does not
835 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800836 *
837 * @param off The offset of the first element to be copied.
838 * @param count The number of elements to be copied.
839 * @param d the source data array
840 */
841 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700842 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700843 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700844 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700845 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700846 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700847 }
848
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700849 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700850 * Copy an array into part of this Allocation. This variant is type checked
851 * and will generate exceptions if the Allocation type is not a 32 bit
852 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800853 *
854 * @param off The offset of the first element to be copied.
855 * @param count The number of elements to be copied.
856 * @param d the source data array
857 */
Jason Samsb97b2512011-01-16 15:04:08 -0800858 public void copy1DRangeFrom(int off, int count, int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700859 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800860 validateIsInt32();
861 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700862 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800863 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800864
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700865 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700866 * Copy an array into part of this Allocation. This variant is type checked
867 * and will generate exceptions if the Allocation type is not a 16 bit
868 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800869 *
870 * @param off The offset of the first element to be copied.
871 * @param count The number of elements to be copied.
872 * @param d the source data array
873 */
Jason Samsb97b2512011-01-16 15:04:08 -0800874 public void copy1DRangeFrom(int off, int count, short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700875 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800876 validateIsInt16();
877 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700878 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800879 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800880
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700881 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700882 * Copy an array into part of this Allocation. This variant is type checked
883 * and will generate exceptions if the Allocation type is not an 8 bit
884 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800885 *
886 * @param off The offset of the first element to be copied.
887 * @param count The number of elements to be copied.
888 * @param d the source data array
889 */
Jason Samsb97b2512011-01-16 15:04:08 -0800890 public void copy1DRangeFrom(int off, int count, byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700891 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800892 validateIsInt8();
893 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700894 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800895 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800896
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700897 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700898 * Copy an array into part of this Allocation. This variant is type checked
899 * and will generate exceptions if the Allocation type is not a 32 bit float
900 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800901 *
902 * @param off The offset of the first element to be copied.
903 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700904 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800905 */
Jason Samsb97b2512011-01-16 15:04:08 -0800906 public void copy1DRangeFrom(int off, int count, float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700907 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800908 validateIsFloat32();
909 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700910 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800911 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700912 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700913 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700914 *
915 * @param off The offset of the first element to be copied.
916 * @param count The number of elements to be copied.
917 * @param data the source data allocation.
918 * @param dataOff off The offset of the first element in data to
919 * be copied.
920 */
921 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700922 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -0700923 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700924 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700925 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700926 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700927 }
928
Jason Samsfb9f82c2011-01-12 14:53:25 -0800929 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -0700930 if (mAdaptedAllocation != null) {
931
932 } else {
933
934 if (xoff < 0 || yoff < 0) {
935 throw new RSIllegalArgumentException("Offset cannot be negative.");
936 }
937 if (h < 0 || w < 0) {
938 throw new RSIllegalArgumentException("Height or width cannot be negative.");
939 }
940 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
941 throw new RSIllegalArgumentException("Updated region larger than allocation.");
942 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800943 }
944 }
Jason Sams768bc022009-09-21 19:41:04 -0700945
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800946 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, byte[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700947 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800948 mRS.validate();
949 validate2DRange(xoff, yoff, w, h);
950 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
951 w, h, data, data.length);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700952 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800953 }
954
955 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, short[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700956 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800957 mRS.validate();
958 validate2DRange(xoff, yoff, w, h);
959 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
960 w, h, data, data.length * 2);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700961 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800962 }
963
964 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, int[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700965 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800966 mRS.validate();
967 validate2DRange(xoff, yoff, w, h);
968 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
969 w, h, data, data.length * 4);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700970 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800971 }
972
973 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, float[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700974 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800975 mRS.validate();
976 validate2DRange(xoff, yoff, w, h);
977 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
978 w, h, data, data.length * 4);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700979 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800980 }
981
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700982 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700983 * Copy from an array into a rectangular region in this Allocation. The
984 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -0800985 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700986 * @param xoff X offset of the region to update in this Allocation
987 * @param yoff Y offset of the region to update in this Allocation
988 * @param w Width of the region to update
989 * @param h Height of the region to update
990 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -0800991 */
992 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700993 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -0800994 validateIsInt8();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800995 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700996 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -0800997 }
998
Tim Murrayc11e25c2013-04-09 11:01:01 -0700999 /**
1000 * Copy from an array into a rectangular region in this Allocation. The
1001 * array is assumed to be tightly packed.
1002 *
1003 * @param xoff X offset of the region to update in this Allocation
1004 * @param yoff Y offset of the region to update in this Allocation
1005 * @param w Width of the region to update
1006 * @param h Height of the region to update
1007 * @param data to be placed into the Allocation
1008 */
Jason Samsf7086092011-01-12 13:28:37 -08001009 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001010 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001011 validateIsInt16();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001012 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001013 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001014 }
1015
Tim Murrayc11e25c2013-04-09 11:01:01 -07001016 /**
1017 * Copy from an array into a rectangular region in this Allocation. The
1018 * array is assumed to be tightly packed.
1019 *
1020 * @param xoff X offset of the region to update in this Allocation
1021 * @param yoff Y offset of the region to update in this Allocation
1022 * @param w Width of the region to update
1023 * @param h Height of the region to update
1024 * @param data to be placed into the Allocation
1025 */
Jason Samsf7086092011-01-12 13:28:37 -08001026 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001027 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001028 validateIsInt32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001029 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001030 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001031 }
1032
Tim Murrayc11e25c2013-04-09 11:01:01 -07001033 /**
1034 * Copy from an array into a rectangular region in this Allocation. The
1035 * array is assumed to be tightly packed.
1036 *
1037 * @param xoff X offset of the region to update in this Allocation
1038 * @param yoff Y offset of the region to update in this Allocation
1039 * @param w Width of the region to update
1040 * @param h Height of the region to update
1041 * @param data to be placed into the Allocation
1042 */
Jason Samsf7086092011-01-12 13:28:37 -08001043 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001044 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001045 validateIsFloat32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001046 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001047 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001048 }
1049
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001050 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001051 * Copy a rectangular region from an Allocation into a rectangular region in
1052 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001053 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001054 * @param xoff X offset of the region in this Allocation
1055 * @param yoff Y offset of the region in this Allocation
1056 * @param w Width of the region to update.
1057 * @param h Height of the region to update.
1058 * @param data source Allocation.
1059 * @param dataXoff X offset in source Allocation
1060 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001061 */
1062 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1063 Allocation data, int dataXoff, int dataYoff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001064 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001065 mRS.validate();
1066 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -07001067 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -07001068 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001069 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -07001070 data.mSelectedLOD, data.mSelectedFace.mID);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001071 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001072 }
1073
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001074 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001075 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1076 * and width of the update will use the height and width of the {@link
1077 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001078 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001079 * @param xoff X offset of the region to update in this Allocation
1080 * @param yoff Y offset of the region to update in this Allocation
1081 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001082 */
1083 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001084 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Jason Samsfa445b92011-01-07 17:00:07 -08001085 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001086 if (data.getConfig() == null) {
1087 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1088 Canvas c = new Canvas(newBitmap);
1089 c.drawBitmap(data, 0, 0, null);
1090 copy2DRangeFrom(xoff, yoff, newBitmap);
Jason Samsb05d6892013-04-09 15:59:24 -07001091 return;
Tim Murrayabd5db92013-02-28 11:45:22 -08001092 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001093 validateBitmapFormat(data);
1094 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -07001095 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001096 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001097 }
1098
Jason Samsb05d6892013-04-09 15:59:24 -07001099 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1100 if (mAdaptedAllocation != null) {
1101
1102 } else {
1103
1104 if (xoff < 0 || yoff < 0 || zoff < 0) {
1105 throw new RSIllegalArgumentException("Offset cannot be negative.");
1106 }
1107 if (h < 0 || w < 0 || d < 0) {
1108 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1109 }
1110 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1111 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1112 }
1113 }
1114 }
1115
1116 /**
1117 * @hide
1118 *
1119 */
1120 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1121 mRS.validate();
1122 validate3DRange(xoff, yoff, zoff, w, h, d);
1123 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1124 w, h, d, data, data.length);
1125 }
1126
1127 /**
1128 * @hide
1129 *
1130 */
1131 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1132 mRS.validate();
1133 validate3DRange(xoff, yoff, zoff, w, h, d);
1134 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1135 w, h, d, data, data.length * 2);
1136 }
1137
1138 /**
1139 * @hide
1140 *
1141 */
1142 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
1143 mRS.validate();
1144 validate3DRange(xoff, yoff, zoff, w, h, d);
1145 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1146 w, h, d, data, data.length * 4);
1147 }
1148
1149 /**
1150 * @hide
1151 *
1152 */
1153 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
1154 mRS.validate();
1155 validate3DRange(xoff, yoff, zoff, w, h, d);
1156 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1157 w, h, d, data, data.length * 4);
1158 }
1159
1160
1161 /**
1162 * @hide
1163 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001164 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001165 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001166 * @param xoff X offset of the region to update in this Allocation
1167 * @param yoff Y offset of the region to update in this Allocation
1168 * @param zoff Z offset of the region to update in this Allocation
1169 * @param w Width of the region to update
1170 * @param h Height of the region to update
1171 * @param d Depth of the region to update
Jason Samsb05d6892013-04-09 15:59:24 -07001172 * @param data to be placed into the allocation
1173 */
1174 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1175 validateIsInt8();
1176 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1177 }
1178
1179 /**
1180 * @hide
1181 *
1182 */
1183 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1184 validateIsInt16();
1185 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1186 }
1187
1188 /**
1189 * @hide
1190 *
1191 */
1192 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
1193 validateIsInt32();
1194 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1195 }
1196
1197 /**
1198 * @hide
1199 *
1200 */
1201 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
1202 validateIsFloat32();
1203 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1204 }
1205
1206 /**
1207 * @hide
1208 * Copy a rectangular region into the allocation from another
1209 * allocation.
1210 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001211 * @param xoff X offset of the region to update in this Allocation
1212 * @param yoff Y offset of the region to update in this Allocation
1213 * @param zoff Z offset of the region to update in this Allocation
1214 * @param w Width of the region to update.
1215 * @param h Height of the region to update.
1216 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001217 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001218 * @param dataXoff X offset of the region in the source Allocation
1219 * @param dataYoff Y offset of the region in the source Allocation
1220 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001221 */
1222 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1223 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1224 mRS.validate();
1225 validate3DRange(xoff, yoff, zoff, w, h, d);
1226 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1227 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1228 data.mSelectedLOD);
1229 }
1230
Jason Samsfa445b92011-01-07 17:00:07 -08001231
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001232 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001233 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1234 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001235 *
1236 * @param b The bitmap to be set from the Allocation.
1237 */
Jason Samsfa445b92011-01-07 17:00:07 -08001238 public void copyTo(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001239 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsfb9f82c2011-01-12 14:53:25 -08001240 mRS.validate();
1241 validateBitmapFormat(b);
1242 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -07001243 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001244 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001245 }
1246
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001247 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001248 * Copy from the Allocation into a byte array. The array must be at least
1249 * as large as the Allocation. The allocation must be of an 8 bit integer
1250 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001251 *
1252 * @param d The array to be set from the Allocation.
1253 */
Jason Samsfa445b92011-01-07 17:00:07 -08001254 public void copyTo(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001255 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001256 validateIsInt8();
Jason Sams771bebb2009-12-07 12:40:12 -08001257 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001258 mRS.nAllocationRead(getID(mRS), d);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001259 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams40a29e82009-08-10 14:55:26 -07001260 }
1261
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001262 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001263 * Copy from the Allocation into a short array. The array must be at least
1264 * as large as the Allocation. The allocation must be of an 16 bit integer
1265 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001266 *
1267 * @param d The array to be set from the Allocation.
1268 */
Jason Samsfa445b92011-01-07 17:00:07 -08001269 public void copyTo(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001270 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001271 validateIsInt16();
Jason Samsfa445b92011-01-07 17:00:07 -08001272 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001273 mRS.nAllocationRead(getID(mRS), d);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001274 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001275 }
1276
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001277 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001278 * Copy from the Allocation into a int array. The array must be at least as
1279 * large as the Allocation. The allocation must be of an 32 bit integer
1280 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001281 *
1282 * @param d The array to be set from the Allocation.
1283 */
Jason Samsfa445b92011-01-07 17:00:07 -08001284 public void copyTo(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001285 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001286 validateIsInt32();
Jason Samsfa445b92011-01-07 17:00:07 -08001287 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001288 mRS.nAllocationRead(getID(mRS), d);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001289 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001290 }
1291
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001292 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001293 * Copy from the Allocation into a float array. The array must be at least
1294 * as large as the Allocation. The allocation must be of an 32 bit float
1295 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001296 *
1297 * @param d The array to be set from the Allocation.
1298 */
Jason Samsfa445b92011-01-07 17:00:07 -08001299 public void copyTo(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001300 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001301 validateIsFloat32();
Jason Sams771bebb2009-12-07 12:40:12 -08001302 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001303 mRS.nAllocationRead(getID(mRS), d);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001304 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams40a29e82009-08-10 14:55:26 -07001305 }
1306
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001307 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001308 * Resize a 1D allocation. The contents of the allocation are preserved.
1309 * If new elements are allocated objects are created with null contents and
1310 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001311 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001312 * <p>If the new region is smaller the references of any objects outside the
1313 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001314 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001315 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001316 *
1317 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001318 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001319 * @deprecated RenderScript objects should be immutable once created. The
1320 * replacement is to create a new allocation and copy the contents.
Jason Samsf7086092011-01-12 13:28:37 -08001321 */
Jason Sams31a7e422010-10-26 13:09:17 -07001322 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001323 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001324 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001325 }
Jason Samse07694b2012-04-03 15:36:36 -07001326 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001327 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001328
Jason Samse07694b2012-04-03 15:36:36 -07001329 int typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001330 mType = new Type(typeID, mRS);
1331 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001332 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001333 }
1334
Jason Samsb8c5a842009-07-31 20:40:47 -07001335
1336 // creation
1337
Jason Sams49a05d72010-12-29 14:31:29 -08001338 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001339 static {
1340 mBitmapOptions.inScaled = false;
1341 }
1342
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001343 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001344 * Creates a new Allocation with the given {@link
1345 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001346 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001347 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001348 * @param mips specifies desired mipmap behaviour for the
1349 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001350 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001351 * utilized
1352 */
1353 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001354 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
Jason Sams771bebb2009-12-07 12:40:12 -08001355 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001356 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001357 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001358 }
Jason Samse07694b2012-04-03 15:36:36 -07001359 int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001360 if (id == 0) {
1361 throw new RSRuntimeException("Allocation creation failed.");
1362 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001363 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams857d0c72011-11-23 15:02:15 -08001364 return new Allocation(id, rs, type, usage);
1365 }
1366
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001367 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001368 * Creates an Allocation with the size specified by the type and no mipmaps
1369 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001370 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001371 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001372 * @param type renderscript type describing data layout
1373 * @param usage bit field specifying how the allocation is
1374 * utilized
1375 *
1376 * @return allocation
1377 */
Jason Samse5d37122010-12-16 00:33:33 -08001378 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1379 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1380 }
1381
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001382 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001383 * Creates an Allocation for use by scripts with a given {@link
1384 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001385 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001386 * @param rs Context to which the Allocation will belong.
1387 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001388 *
1389 * @return allocation
1390 */
Jason Sams5476b452010-12-08 16:14:36 -08001391 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001392 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001393 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001394
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001395 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001396 * Creates an Allocation with a specified number of given elements
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 e Element to use in the Allocation
1400 * @param count the number of Elements in the Allocation
1401 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001402 * utilized
1403 *
1404 * @return allocation
1405 */
Jason Sams5476b452010-12-08 16:14:36 -08001406 static public Allocation createSized(RenderScript rs, Element e,
1407 int count, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001408 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
Jason Sams771bebb2009-12-07 12:40:12 -08001409 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001410 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001411 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001412 Type t = b.create();
1413
Jason Samse07694b2012-04-03 15:36:36 -07001414 int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001415 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001416 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001417 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001418 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001419 return new Allocation(id, rs, t, usage);
1420 }
1421
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001422 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001423 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001424 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001425 * @param rs Context to which the Allocation will belong.
1426 * @param e Element to use in the Allocation
1427 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001428 *
1429 * @return allocation
1430 */
Jason Sams5476b452010-12-08 16:14:36 -08001431 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001432 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001433 }
1434
Jason Sams49a05d72010-12-29 14:31:29 -08001435 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001436 final Bitmap.Config bc = b.getConfig();
1437 if (bc == Bitmap.Config.ALPHA_8) {
1438 return Element.A_8(rs);
1439 }
1440 if (bc == Bitmap.Config.ARGB_4444) {
1441 return Element.RGBA_4444(rs);
1442 }
1443 if (bc == Bitmap.Config.ARGB_8888) {
1444 return Element.RGBA_8888(rs);
1445 }
1446 if (bc == Bitmap.Config.RGB_565) {
1447 return Element.RGB_565(rs);
1448 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001449 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001450 }
1451
Jason Sams49a05d72010-12-29 14:31:29 -08001452 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001453 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001454 Element e = elementFromBitmap(rs, b);
1455 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001456 tb.setX(b.getWidth());
1457 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001458 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001459 return tb.create();
1460 }
1461
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001462 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001463 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001464 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001465 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001466 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001467 * @param mips specifies desired mipmap behaviour for the
1468 * allocation
1469 * @param usage bit field specifying how the allocation is
1470 * utilized
1471 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001472 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001473 *
1474 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001475 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001476 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001477 int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001478 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
Jason Sams771bebb2009-12-07 12:40:12 -08001479 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001480
1481 // WAR undocumented color formats
1482 if (b.getConfig() == null) {
1483 if ((usage & USAGE_SHARED) != 0) {
1484 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1485 }
1486 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1487 Canvas c = new Canvas(newBitmap);
1488 c.drawBitmap(b, 0, 0, null);
1489 return createFromBitmap(rs, newBitmap, mips, usage);
1490 }
1491
Jason Sams5476b452010-12-08 16:14:36 -08001492 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001493
Tim Murraya3145512012-12-04 17:59:29 -08001494 // enable optimized bitmap path only with no mipmap and script-only usage
1495 if (mips == MipmapControl.MIPMAP_NONE &&
1496 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Tim Murray78e64942013-04-09 17:28:56 -07001497 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
Tim Murraya3145512012-12-04 17:59:29 -08001498 int id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
1499 if (id == 0) {
1500 throw new RSRuntimeException("Load failed.");
1501 }
1502
1503 // keep a reference to the Bitmap around to prevent GC
1504 Allocation alloc = new Allocation(id, rs, t, usage);
1505 alloc.setBitmap(b);
1506 return alloc;
1507 }
1508
Jason Sams9bf18922013-04-13 19:48:36 -07001509
Jason Samse07694b2012-04-03 15:36:36 -07001510 int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001511 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001512 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001513 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001514 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001515 return new Allocation(id, rs, t, usage);
1516 }
1517
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001518 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001519 * Returns the handle to a raw buffer that is being managed by the screen
1520 * compositor. This operation is only valid for Allocations with {@link
1521 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001522 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001523 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001524 *
1525 */
1526 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08001527 if ((mUsage & USAGE_IO_INPUT) == 0) {
1528 throw new RSInvalidStateException("Allocation is not a surface texture.");
1529 }
1530 return mRS.nAllocationGetSurface(getID(mRS));
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001531 }
1532
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001533 /**
Jason Samsc089c2f2013-02-22 13:57:36 -08001534 * @hide
1535 */
1536 public void setSurfaceTexture(SurfaceTexture st) {
1537 setSurface(new Surface(st));
1538 }
1539
1540 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001541 * Associate a {@link android.view.Surface} with this Allocation. This
1542 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001543 *
1544 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001545 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001546 public void setSurface(Surface sur) {
1547 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001548 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1549 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1550 }
1551
Jason Samse07694b2012-04-03 15:36:36 -07001552 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001553 }
1554
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001555 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001556 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08001557 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001558 * <p>With target API version 18 or greater, this Allocation will be created
1559 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
1560 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
1561 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001562 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001563 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001564 * @param b bitmap source for the allocation data
1565 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001566 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001567 *
1568 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001569 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08001570 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1571 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07001572 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08001573 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001574 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1575 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001576 }
1577
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001578 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001579 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1580 * containing the horizontal list of cube faces. Each face must be a square,
1581 * have the same size as all other faces, and have a width that is a power
1582 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001583 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001584 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001585 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001586 * format: right, left, top, bottom, front, back
1587 * @param mips specifies desired mipmap behaviour for the cubemap
1588 * @param usage bit field specifying how the cubemap is utilized
1589 *
1590 * @return allocation containing cubemap data
1591 *
1592 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001593 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001594 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001595 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001596 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001597
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001598 int height = b.getHeight();
1599 int width = b.getWidth();
1600
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001601 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001602 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1603 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001604 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001605 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001606 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001607 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001608 if (!isPow2) {
1609 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1610 }
1611
1612 Element e = elementFromBitmap(rs, b);
1613 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001614 tb.setX(height);
1615 tb.setY(height);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001616 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001617 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001618 Type t = tb.create();
1619
Jason Samse07694b2012-04-03 15:36:36 -07001620 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001621 if(id == 0) {
1622 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1623 }
Jason Sams5476b452010-12-08 16:14:36 -08001624 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001625 }
1626
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001627 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001628 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
1629 * from a {@link android.graphics.Bitmap} containing the horizontal list of
1630 * cube faces. Each face must be a square, have the same size as all other
1631 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001632 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001633 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001634 * @param b bitmap with cubemap faces layed out in the following
1635 * format: right, left, top, bottom, front, back
1636 *
1637 * @return allocation containing cubemap data
1638 *
1639 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001640 static public Allocation createCubemapFromBitmap(RenderScript rs,
1641 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001642 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001643 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001644 }
1645
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001646 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001647 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
1648 * objects containing the cube faces. Each face must be a square, have the
1649 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001650 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001651 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001652 * @param xpos cubemap face in the positive x direction
1653 * @param xneg cubemap face in the negative x direction
1654 * @param ypos cubemap face in the positive y direction
1655 * @param yneg cubemap face in the negative y direction
1656 * @param zpos cubemap face in the positive z direction
1657 * @param zneg cubemap face in the negative z direction
1658 * @param mips specifies desired mipmap behaviour for the cubemap
1659 * @param usage bit field specifying how the cubemap is utilized
1660 *
1661 * @return allocation containing cubemap data
1662 *
1663 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001664 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1665 Bitmap xpos,
1666 Bitmap xneg,
1667 Bitmap ypos,
1668 Bitmap yneg,
1669 Bitmap zpos,
1670 Bitmap zneg,
1671 MipmapControl mips,
1672 int usage) {
1673 int height = xpos.getHeight();
1674 if (xpos.getWidth() != height ||
1675 xneg.getWidth() != height || xneg.getHeight() != height ||
1676 ypos.getWidth() != height || ypos.getHeight() != height ||
1677 yneg.getWidth() != height || yneg.getHeight() != height ||
1678 zpos.getWidth() != height || zpos.getHeight() != height ||
1679 zneg.getWidth() != height || zneg.getHeight() != height) {
1680 throw new RSIllegalArgumentException("Only square cube map faces supported");
1681 }
1682 boolean isPow2 = (height & (height - 1)) == 0;
1683 if (!isPow2) {
1684 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1685 }
1686
1687 Element e = elementFromBitmap(rs, xpos);
1688 Type.Builder tb = new Type.Builder(rs, e);
1689 tb.setX(height);
1690 tb.setY(height);
1691 tb.setFaces(true);
1692 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1693 Type t = tb.create();
1694 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1695
1696 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001697 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001698 adapter.copyFrom(xpos);
1699 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1700 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001701 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001702 adapter.copyFrom(ypos);
1703 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1704 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001705 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001706 adapter.copyFrom(zpos);
1707 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1708 adapter.copyFrom(zneg);
1709
1710 return cubemap;
1711 }
1712
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001713 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001714 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
1715 * from 6 {@link android.graphics.Bitmap} objects containing the cube
1716 * faces. Each face must be a square, have the same size as all other faces,
1717 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001718 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001719 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001720 * @param xpos cubemap face in the positive x direction
1721 * @param xneg cubemap face in the negative x direction
1722 * @param ypos cubemap face in the positive y direction
1723 * @param yneg cubemap face in the negative y direction
1724 * @param zpos cubemap face in the positive z direction
1725 * @param zneg cubemap face in the negative z direction
1726 *
1727 * @return allocation containing cubemap data
1728 *
1729 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001730 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1731 Bitmap xpos,
1732 Bitmap xneg,
1733 Bitmap ypos,
1734 Bitmap yneg,
1735 Bitmap zpos,
1736 Bitmap zneg) {
1737 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1738 zpos, zneg, MipmapControl.MIPMAP_NONE,
1739 USAGE_GRAPHICS_TEXTURE);
1740 }
1741
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001742 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001743 * Creates an Allocation from the Bitmap referenced
1744 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001745 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001746 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001747 * @param res application resources
1748 * @param id resource id to load the data from
1749 * @param mips specifies desired mipmap behaviour for the
1750 * allocation
1751 * @param usage bit field specifying how the allocation is
1752 * utilized
1753 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001754 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001755 *
1756 */
Jason Sams5476b452010-12-08 16:14:36 -08001757 static public Allocation createFromBitmapResource(RenderScript rs,
1758 Resources res,
1759 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001760 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001761 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001762
Jason Sams771bebb2009-12-07 12:40:12 -08001763 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07001764 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
1765 throw new RSIllegalArgumentException("Unsupported usage specified.");
1766 }
Jason Sams5476b452010-12-08 16:14:36 -08001767 Bitmap b = BitmapFactory.decodeResource(res, id);
1768 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1769 b.recycle();
1770 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001771 }
1772
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001773 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001774 * Creates a non-mipmapped Allocation to use as a graphics texture from the
1775 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001776 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001777 * <p>With target API version 18 or greater, this allocation will be created
1778 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
1779 * target API version 17 or lower, this allocation will be created with
1780 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08001781 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001782 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001783 * @param res application resources
1784 * @param id resource id to load the data from
1785 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001786 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001787 *
1788 */
Jason Sams5476b452010-12-08 16:14:36 -08001789 static public Allocation createFromBitmapResource(RenderScript rs,
1790 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001791 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08001792 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1793 return createFromBitmapResource(rs, res, id,
1794 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07001795 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08001796 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001797 return createFromBitmapResource(rs, res, id,
1798 MipmapControl.MIPMAP_NONE,
1799 USAGE_GRAPHICS_TEXTURE);
1800 }
1801
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001802 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001803 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001804 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001805 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001806 * @param str string to create the allocation from
1807 * @param usage bit field specifying how the allocaiton is
1808 * utilized
1809 *
1810 */
Jason Sams5476b452010-12-08 16:14:36 -08001811 static public Allocation createFromString(RenderScript rs,
1812 String str,
1813 int usage) {
1814 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001815 byte[] allocArray = null;
1816 try {
1817 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001818 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001819 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001820 return alloc;
1821 }
1822 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001823 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001824 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001825 }
Jason Sams739c8262013-04-11 18:07:52 -07001826
1827 /**
Jason Samsf64cca92013-04-19 12:56:37 -07001828 * @hide
1829 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001830 * Interface to handle notification when new buffers are available via
1831 * {@link #USAGE_IO_INPUT}. An application will receive one notification
1832 * when a buffer is available. Additional buffers will not trigger new
1833 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07001834 */
1835 public interface IoInputNotifier {
1836 public void onBufferAvailable(Allocation a);
1837 }
1838
1839 /**
Jason Samsf64cca92013-04-19 12:56:37 -07001840 * @hide
1841 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001842 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07001843 *
Jason Samsc876cc42013-04-11 20:22:31 -07001844 * @param callback instance of the IoInputNotifier class to be called
Jason Sams739c8262013-04-11 18:07:52 -07001845 * when buffer arrive.
1846 */
1847 public void setIoInputNotificationHandler(IoInputNotifier callback) {
1848 synchronized(mAllocationMap) {
1849 mAllocationMap.put(new Integer(getID(mRS)), this);
1850 mBufferNotifier = callback;
1851 }
1852 }
1853
1854 static void sendBufferNotification(int id) {
1855 synchronized(mAllocationMap) {
1856 Allocation a = mAllocationMap.get(new Integer(id));
1857
1858 if ((a != null) && (a.mBufferNotifier != null)) {
1859 a.mBufferNotifier.onBufferAvailable(a);
1860 }
1861 }
1862 }
1863
Jason Samsb8c5a842009-07-31 20:40:47 -07001864}
1865