blob: dca934f82cab3e22a2825205e5eebe0e36d89b48 [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;
Tim Murray2f2472c2013-08-22 14:55:26 -070064 int mSize;
Jason Samsba862d12011-07-07 15:24:42 -070065
66 boolean mConstrainedLOD;
67 boolean mConstrainedFace;
68 boolean mConstrainedY;
69 boolean mConstrainedZ;
Jason Sams615e7ce2012-01-13 14:01:20 -080070 boolean mReadAllowed = true;
71 boolean mWriteAllowed = true;
Jason Samsba862d12011-07-07 15:24:42 -070072 int mSelectedY;
73 int mSelectedZ;
74 int mSelectedLOD;
75 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
76
77 int mCurrentDimX;
78 int mCurrentDimY;
79 int mCurrentDimZ;
80 int mCurrentCount;
Jason Sams739c8262013-04-11 18:07:52 -070081 static HashMap<Integer, Allocation> mAllocationMap =
82 new HashMap<Integer, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070083 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070084
Tim Murrayc11e25c2013-04-09 11:01:01 -070085 /**
86 * The usage of the Allocation. These signal to RenderScript where to place
87 * the Allocation in memory.
88 *
89 */
Jason Sams5476b452010-12-08 16:14:36 -080090
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070091 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -070092 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -080093 */
Jason Sams5476b452010-12-08 16:14:36 -080094 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -080095
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070096 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -070097 * The Allocation will be used as a texture source by one or more graphics
98 * programs.
Jason Samsf7086092011-01-12 13:28:37 -080099 *
100 */
Jason Sams5476b452010-12-08 16:14:36 -0800101 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800102
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700103 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700104 * The Allocation will be used as a graphics mesh.
105 *
106 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800107 *
108 */
Jason Sams5476b452010-12-08 16:14:36 -0800109 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800110
111
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700112 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700113 * The Allocation will be used as the source of shader constants by one or
114 * more programs.
115 *
116 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800117 *
118 */
Jason Sams5476b452010-12-08 16:14:36 -0800119 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
120
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700121 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700122 * The Allocation will be used as a target for offscreen rendering
123 *
124 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700125 *
126 */
127 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
128
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700129 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700130 * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
131 * consumer. This usage will cause the Allocation to be created as
132 * read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800133 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800134 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700135 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800136
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700137 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700138 * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
139 * producer. The dimensions and format of the {@link
140 * android.graphics.SurfaceTexture} will be forced to those of the
141 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800142 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800143 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700144 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700145
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700146 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700147 * The Allocation's backing store will be inherited from another object
148 * (usually a {@link android.graphics.Bitmap}); copying to or from the
149 * original source Bitmap will cause a synchronization rather than a full
150 * copy. {@link #syncAll} may also be used to synchronize the Allocation
151 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800152 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700153 * <p>This is set by default for allocations created with {@link
154 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800155 *
156 */
157 public static final int USAGE_SHARED = 0x0080;
158
159 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700160 * Controls mipmap behavior when using the bitmap creation and update
161 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800162 */
Jason Sams4ef66502010-12-10 16:03:15 -0800163 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700164 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700165 * No mipmaps will be generated and the type generated from the incoming
166 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800167 */
Jason Sams5476b452010-12-08 16:14:36 -0800168 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800169
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700170 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700171 * A full mipmap chain will be created in script memory. The Type of
172 * the Allocation will contain a full mipmap chain. On upload, the full
173 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800174 */
Jason Sams5476b452010-12-08 16:14:36 -0800175 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800176
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700177 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700178 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
179 * not contain mipmaps. On upload, the allocation data will contain a
180 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800181 */
Jason Sams5476b452010-12-08 16:14:36 -0800182 MIPMAP_ON_SYNC_TO_TEXTURE(2);
183
184 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800185 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800186 mID = id;
187 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700188 }
189
Jason Sams48fe5342011-07-08 13:52:30 -0700190
191 private int getIDSafe() {
192 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700193 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700194 }
Jason Samse07694b2012-04-03 15:36:36 -0700195 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700196 }
197
Jason Sams03d2d002012-03-23 13:51:56 -0700198
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700199 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700200 * Get the {@link android.renderscript.Element} of the {@link
201 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700202 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700203 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700204 *
205 */
206 public Element getElement() {
207 return mType.getElement();
208 }
209
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700210 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700211 * Get the usage flags of the Allocation.
212 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700213 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700214 *
215 */
216 public int getUsage() {
217 return mUsage;
218 }
219
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700220 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700221 * Get the size of the Allocation in bytes.
222 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700223 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700224 *
225 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700226 public int getBytesSize() {
227 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700228 }
229
Jason Sams452a7662011-07-07 16:05:18 -0700230 private void updateCacheInfo(Type t) {
231 mCurrentDimX = t.getX();
232 mCurrentDimY = t.getY();
233 mCurrentDimZ = t.getZ();
234 mCurrentCount = mCurrentDimX;
235 if (mCurrentDimY > 1) {
236 mCurrentCount *= mCurrentDimY;
237 }
238 if (mCurrentDimZ > 1) {
239 mCurrentCount *= mCurrentDimZ;
240 }
241 }
Jason Samsba862d12011-07-07 15:24:42 -0700242
Tim Murraya3145512012-12-04 17:59:29 -0800243 private void setBitmap(Bitmap b) {
244 mBitmap = b;
245 }
246
Jason Sams5476b452010-12-08 16:14:36 -0800247 Allocation(int id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700248 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800249 if ((usage & ~(USAGE_SCRIPT |
250 USAGE_GRAPHICS_TEXTURE |
251 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700252 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800253 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800254 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800255 USAGE_IO_OUTPUT |
256 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800257 throw new RSIllegalArgumentException("Unknown usage specified.");
258 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800259
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700260 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800261 mWriteAllowed = false;
262
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700263 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800264 USAGE_GRAPHICS_TEXTURE |
265 USAGE_SCRIPT)) != 0) {
266 throw new RSIllegalArgumentException("Invalid usage combination.");
267 }
268 }
Jason Sams9bf18922013-04-13 19:48:36 -0700269
Jason Sams5476b452010-12-08 16:14:36 -0800270 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800271 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700272
Jason Sams452a7662011-07-07 16:05:18 -0700273 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700274 // TODO: A3D doesn't have Type info during creation, so we can't
275 // calculate the size ahead of time. We can possibly add a method
276 // to update the size in the future if it seems reasonable.
277 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700278 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700279 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700280 try {
281 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
282 } catch (Exception e) {
283 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
284 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
285 }
286 }
287
288 protected void finalize() throws Throwable {
289 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
290 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700291 }
292
Jason Samsb97b2512011-01-16 15:04:08 -0800293 private void validateIsInt32() {
294 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
295 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
296 return;
297 }
298 throw new RSIllegalArgumentException(
299 "32 bit integer source does not match allocation type " + mType.mElement.mType);
300 }
301
302 private void validateIsInt16() {
303 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
304 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
305 return;
306 }
307 throw new RSIllegalArgumentException(
308 "16 bit integer source does not match allocation type " + mType.mElement.mType);
309 }
310
311 private void validateIsInt8() {
312 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
313 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
314 return;
315 }
316 throw new RSIllegalArgumentException(
317 "8 bit integer source does not match allocation type " + mType.mElement.mType);
318 }
319
320 private void validateIsFloat32() {
321 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
322 return;
323 }
324 throw new RSIllegalArgumentException(
325 "32 bit float source does not match allocation type " + mType.mElement.mType);
326 }
327
328 private void validateIsObject() {
329 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
330 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
331 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
332 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
333 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
334 (mType.mElement.mType == Element.DataType.RS_MESH) ||
335 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
336 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
337 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
338 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
339 return;
340 }
341 throw new RSIllegalArgumentException(
342 "Object source does not match allocation type " + mType.mElement.mType);
343 }
344
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700345 @Override
346 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800347 super.updateFromNative();
Jason Samse07694b2012-04-03 15:36:36 -0700348 int typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700349 if(typeID != 0) {
350 mType = new Type(typeID, mRS);
351 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700352 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700353 }
354 }
355
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700356 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700357 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700358 *
359 * @return Type
360 *
361 */
Jason Samsea87e962010-01-12 12:12:28 -0800362 public Type getType() {
363 return mType;
364 }
365
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700366 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700367 * Propagate changes from one usage of the Allocation to the
368 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700369 *
370 */
Jason Sams5476b452010-12-08 16:14:36 -0800371 public void syncAll(int srcLocation) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700372 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
Jason Sams5476b452010-12-08 16:14:36 -0800373 switch (srcLocation) {
Jason Sams5476b452010-12-08 16:14:36 -0800374 case USAGE_GRAPHICS_TEXTURE:
Tim Murray78e64942013-04-09 17:28:56 -0700375 case USAGE_SCRIPT:
376 if ((mUsage & USAGE_SHARED) != 0) {
377 copyFrom(mBitmap);
378 }
379 break;
380 case USAGE_GRAPHICS_CONSTANTS:
Jason Sams5476b452010-12-08 16:14:36 -0800381 case USAGE_GRAPHICS_VERTEX:
382 break;
Tim Murray78e64942013-04-09 17:28:56 -0700383 case USAGE_SHARED:
384 if ((mUsage & USAGE_SHARED) != 0) {
385 copyTo(mBitmap);
386 }
387 break;
Jason Sams5476b452010-12-08 16:14:36 -0800388 default:
389 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
390 }
391 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700392 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700393 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800394 }
395
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700396 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700397 * Send a buffer to the output stream. The contents of the Allocation will
398 * be undefined after this operation. This operation is only valid if {@link
399 * #USAGE_IO_OUTPUT} is set on the Allocation.
400 *
Jason Sams163766c2012-02-15 12:04:24 -0800401 *
Jason Sams163766c2012-02-15 12:04:24 -0800402 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700403 public void ioSend() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700404 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
Jason Sams163766c2012-02-15 12:04:24 -0800405 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
406 throw new RSIllegalArgumentException(
407 "Can only send buffer if IO_OUTPUT usage specified.");
408 }
409 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700410 mRS.nAllocationIoSend(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700411 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800412 }
413
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700414 /**
Jason Samsc5f519c2012-03-29 17:58:15 -0700415 * Delete once code is updated.
416 * @hide
417 */
418 public void ioSendOutput() {
419 ioSend();
420 }
421
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700422 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700423 * Receive the latest input into the Allocation. This operation
424 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800425 *
Jason Sams163766c2012-02-15 12:04:24 -0800426 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700427 public void ioReceive() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700428 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
Jason Sams163766c2012-02-15 12:04:24 -0800429 if ((mUsage & USAGE_IO_INPUT) == 0) {
430 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700431 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800432 }
433 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700434 mRS.nAllocationIoReceive(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700435 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800436 }
437
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700438 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700439 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700440 *
441 * @param d Source array.
442 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800443 public void copyFrom(BaseObj[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700444 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Sams771bebb2009-12-07 12:40:12 -0800445 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800446 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700447 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800448 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700449 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800450 }
451 int i[] = new int[d.length];
452 for (int ct=0; ct < d.length; ct++) {
Jason Samse07694b2012-04-03 15:36:36 -0700453 i[ct] = d[ct].getID(mRS);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800454 }
Jason Samsba862d12011-07-07 15:24:42 -0700455 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700456 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700457 }
458
Jason Samsfb9f82c2011-01-12 14:53:25 -0800459 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800460 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800461 if (bc == null) {
462 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
463 }
Jason Sams252c0782011-01-11 17:42:52 -0800464 switch (bc) {
465 case ALPHA_8:
466 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
467 throw new RSIllegalArgumentException("Allocation kind is " +
468 mType.getElement().mKind + ", type " +
469 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700470 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800471 " bytes, passed bitmap was " + bc);
472 }
473 break;
474 case ARGB_8888:
475 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700476 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800477 throw new RSIllegalArgumentException("Allocation kind is " +
478 mType.getElement().mKind + ", type " +
479 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700480 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800481 " bytes, passed bitmap was " + bc);
482 }
483 break;
484 case RGB_565:
485 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700486 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800487 throw new RSIllegalArgumentException("Allocation kind is " +
488 mType.getElement().mKind + ", type " +
489 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700490 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800491 " bytes, passed bitmap was " + bc);
492 }
493 break;
494 case ARGB_4444:
495 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700496 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800497 throw new RSIllegalArgumentException("Allocation kind is " +
498 mType.getElement().mKind + ", type " +
499 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700500 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800501 " bytes, passed bitmap was " + bc);
502 }
503 break;
504
505 }
Jason Sams4ef66502010-12-10 16:03:15 -0800506 }
507
Jason Samsfb9f82c2011-01-12 14:53:25 -0800508 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700509 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800510 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
511 }
512 }
513
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700514 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700515 * Copy into this Allocation from an array. This method does not guarantee
516 * that the Allocation is compatible with the input buffer; it copies memory
517 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800518 *
519 * @param d the source data array
520 */
521 public void copyFromUnchecked(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700522 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800523 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700524 if (mCurrentDimZ > 0) {
525 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
526 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800527 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
528 } else {
529 copy1DRangeFromUnchecked(0, mCurrentCount, d);
530 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700531 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800532 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700533
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700534 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700535 * Copy into this Allocation from an array. This method does not guarantee
536 * that the Allocation is compatible with the input buffer; it copies memory
537 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800538 *
539 * @param d the source data array
540 */
541 public void copyFromUnchecked(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700542 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800543 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700544 if (mCurrentDimZ > 0) {
545 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
546 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800547 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
548 } else {
549 copy1DRangeFromUnchecked(0, mCurrentCount, d);
550 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700551 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800552 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700553
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700554 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700555 * Copy into this Allocation from an array. This method does not guarantee
556 * that the Allocation is compatible with the input buffer; it copies memory
557 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800558 *
559 * @param d the source data array
560 */
561 public void copyFromUnchecked(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700562 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800563 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700564 if (mCurrentDimZ > 0) {
565 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
566 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800567 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
568 } else {
569 copy1DRangeFromUnchecked(0, mCurrentCount, d);
570 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700571 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800572 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700573
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700574 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700575 * Copy into this Allocation from an array. This method does not guarantee
576 * that the Allocation is compatible with the input buffer; it copies memory
577 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800578 *
579 * @param d the source data array
580 */
581 public void copyFromUnchecked(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700582 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800583 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700584 if (mCurrentDimZ > 0) {
585 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
586 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800587 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
588 } else {
589 copy1DRangeFromUnchecked(0, mCurrentCount, d);
590 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700591 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800592 }
593
Tim Murray6d7a53c2013-05-23 16:59:23 -0700594
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700595 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700596 * Copy into this Allocation from an array. This variant is type checked
597 * and will generate exceptions if the Allocation's {@link
598 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800599 *
600 * @param d the source data array
601 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800602 public void copyFrom(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700603 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800604 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700605 if (mCurrentDimZ > 0) {
606 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
607 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800608 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
609 } else {
610 copy1DRangeFrom(0, mCurrentCount, d);
611 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700612 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800613 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800614
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700615 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700616 * Copy into this Allocation from an array. This variant is type checked
617 * and will generate exceptions if the Allocation's {@link
618 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800619 *
620 * @param d the source data array
621 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800622 public void copyFrom(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700623 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800624 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700625 if (mCurrentDimZ > 0) {
626 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
627 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800628 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
629 } else {
630 copy1DRangeFrom(0, mCurrentCount, d);
631 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700632 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800633 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800634
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700635 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700636 * Copy into this Allocation from an array. This variant is type checked
637 * and will generate exceptions if the Allocation's {@link
638 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800639 *
640 * @param d the source data array
641 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800642 public void copyFrom(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700643 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800644 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700645 if (mCurrentDimZ > 0) {
646 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
647 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800648 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
649 } else {
650 copy1DRangeFrom(0, mCurrentCount, d);
651 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700652 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800653 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800654
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700655 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700656 * Copy into this Allocation from an array. This variant is type checked
657 * and will generate exceptions if the Allocation's {@link
658 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800659 *
660 * @param d the source data array
661 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800662 public void copyFrom(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700663 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800664 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700665 if (mCurrentDimZ > 0) {
666 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
667 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800668 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
669 } else {
670 copy1DRangeFrom(0, mCurrentCount, d);
671 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700672 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800673 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800674
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700675 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700676 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
677 * height, width, and format of the bitmap must match the existing
678 * allocation.
679 *
680 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
681 * android.graphics.Bitmap} used to create the Allocation with {@link
682 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
683 * this will synchronize the Allocation with the latest data from the {@link
684 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800685 *
686 * @param b the source bitmap
687 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800688 public void copyFrom(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700689 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsfb9f82c2011-01-12 14:53:25 -0800690 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -0800691 if (b.getConfig() == null) {
692 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
693 Canvas c = new Canvas(newBitmap);
694 c.drawBitmap(b, 0, 0, null);
695 copyFrom(newBitmap);
696 return;
697 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800698 validateBitmapSize(b);
699 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700700 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700701 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700702 }
703
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700704 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700705 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700706 * must be identical.
707 *
708 * @param a the source allocation
709 */
710 public void copyFrom(Allocation a) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700711 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Tim Murrayf671fb02012-10-03 13:50:05 -0700712 mRS.validate();
713 if (!mType.equals(a.getType())) {
714 throw new RSIllegalArgumentException("Types of allocations must match.");
715 }
716 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700717 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700718 }
719
Tim Murrayf671fb02012-10-03 13:50:05 -0700720 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700721 * This is only intended to be used by auto-generated code reflected from
722 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800723 *
724 * @param xoff
725 * @param fp
726 */
Jason Sams21b41032011-01-16 15:05:41 -0800727 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800728 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700729 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700730 final byte[] data = fp.getData();
731
732 int count = data.length / eSize;
733 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800734 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700735 " not divisible by element size " + eSize + ".");
736 }
Jason Samsba862d12011-07-07 15:24:42 -0700737 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700738 }
739
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700740 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700741 * This is only intended to be used by auto-generated code reflected from
742 * the RenderScript script files.
Jason Samsfa445b92011-01-07 17:00:07 -0800743 *
744 * @param xoff
745 * @param component_number
746 * @param fp
747 */
Jason Sams21b41032011-01-16 15:05:41 -0800748 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc2012-02-22 15:22:41 -0800749 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700750 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800751 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700752 }
753 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800754 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700755 }
756
757 final byte[] data = fp.getData();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700758 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800759 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700760
761 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800762 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700763 " does not match component size " + eSize + ".");
764 }
765
Jason Sams48fe5342011-07-08 13:52:30 -0700766 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Jason Samsba862d12011-07-07 15:24:42 -0700767 component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700768 }
769
Jason Sams768bc022009-09-21 19:41:04 -0700770 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800771 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700772 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800773 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700774 }
775 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800776 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700777 }
Jason Samsba862d12011-07-07 15:24:42 -0700778 if((off + count) > mCurrentCount) {
779 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700780 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700781 }
Jason Samsba862d12011-07-07 15:24:42 -0700782 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800783 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700784 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700785 }
786
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700787 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700788 * Generate a mipmap chain. This is only valid if the Type of the Allocation
789 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800790 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700791 * <p>This function will generate a complete set of mipmaps from the top
792 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800793 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700794 * <p>If the Allocation is also using other memory spaces, a call to {@link
795 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800796 */
797 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700798 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800799 }
800
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, int[] 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 * 4, 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, short[] 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 * 2, 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, byte[] 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, 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 Sams768bc022009-09-21 19:41:04 -0700847 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700848
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 method does not
851 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800852 *
853 * @param off The offset of the first element to be copied.
854 * @param count The number of elements to be copied.
855 * @param d the source data array
856 */
857 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700858 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700859 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700860 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700861 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700862 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700863 }
864
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 32 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, int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700875 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800876 validateIsInt32();
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 a 16 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, short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700891 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800892 validateIsInt16();
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 an 8 bit
900 * integer 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.
904 * @param d the source data array
905 */
Jason Samsb97b2512011-01-16 15:04:08 -0800906 public void copy1DRangeFrom(int off, int count, byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700907 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800908 validateIsInt8();
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 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800912
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700913 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700914 * Copy an array into part of this Allocation. This variant is type checked
915 * and will generate exceptions if the Allocation type is not a 32 bit float
916 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800917 *
918 * @param off The offset of the first element to be copied.
919 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700920 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800921 */
Jason Samsb97b2512011-01-16 15:04:08 -0800922 public void copy1DRangeFrom(int off, int count, float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700923 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800924 validateIsFloat32();
925 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700926 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800927 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700928 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700929 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700930 *
931 * @param off The offset of the first element to be copied.
932 * @param count The number of elements to be copied.
933 * @param data the source data allocation.
934 * @param dataOff off The offset of the first element in data to
935 * be copied.
936 */
937 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700938 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -0700939 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700940 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700941 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700942 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700943 }
944
Jason Samsfb9f82c2011-01-12 14:53:25 -0800945 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -0700946 if (mAdaptedAllocation != null) {
947
948 } else {
949
950 if (xoff < 0 || yoff < 0) {
951 throw new RSIllegalArgumentException("Offset cannot be negative.");
952 }
953 if (h < 0 || w < 0) {
954 throw new RSIllegalArgumentException("Height or width cannot be negative.");
955 }
956 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
957 throw new RSIllegalArgumentException("Updated region larger than allocation.");
958 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800959 }
960 }
Jason Sams768bc022009-09-21 19:41:04 -0700961
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800962 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, byte[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700963 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800964 mRS.validate();
965 validate2DRange(xoff, yoff, w, h);
966 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
967 w, h, data, data.length);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700968 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800969 }
970
971 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, short[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700972 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800973 mRS.validate();
974 validate2DRange(xoff, yoff, w, h);
975 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
976 w, h, data, data.length * 2);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700977 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800978 }
979
980 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, int[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700981 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800982 mRS.validate();
983 validate2DRange(xoff, yoff, w, h);
984 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
985 w, h, data, data.length * 4);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700986 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800987 }
988
989 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, float[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700990 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800991 mRS.validate();
992 validate2DRange(xoff, yoff, w, h);
993 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
994 w, h, data, data.length * 4);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700995 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800996 }
997
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700998 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700999 * Copy from an array into a rectangular region in this Allocation. The
1000 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -08001001 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001002 * @param xoff X offset of the region to update in this Allocation
1003 * @param yoff Y offset of the region to update in this Allocation
1004 * @param w Width of the region to update
1005 * @param h Height of the region to update
1006 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -08001007 */
1008 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001009 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001010 validateIsInt8();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001011 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001012 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001013 }
1014
Tim Murrayc11e25c2013-04-09 11:01:01 -07001015 /**
1016 * Copy from an array into a rectangular region in this Allocation. The
1017 * array is assumed to be tightly packed.
1018 *
1019 * @param xoff X offset of the region to update in this Allocation
1020 * @param yoff Y offset of the region to update in this Allocation
1021 * @param w Width of the region to update
1022 * @param h Height of the region to update
1023 * @param data to be placed into the Allocation
1024 */
Jason Samsf7086092011-01-12 13:28:37 -08001025 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001026 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001027 validateIsInt16();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001028 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001029 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001030 }
1031
Tim Murrayc11e25c2013-04-09 11:01:01 -07001032 /**
1033 * Copy from an array into a rectangular region in this Allocation. The
1034 * array is assumed to be tightly packed.
1035 *
1036 * @param xoff X offset of the region to update in this Allocation
1037 * @param yoff Y offset of the region to update in this Allocation
1038 * @param w Width of the region to update
1039 * @param h Height of the region to update
1040 * @param data to be placed into the Allocation
1041 */
Jason Samsf7086092011-01-12 13:28:37 -08001042 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001043 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001044 validateIsInt32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001045 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001046 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001047 }
1048
Tim Murrayc11e25c2013-04-09 11:01:01 -07001049 /**
1050 * Copy from an array into a rectangular region in this Allocation. The
1051 * array is assumed to be tightly packed.
1052 *
1053 * @param xoff X offset of the region to update in this Allocation
1054 * @param yoff Y offset of the region to update in this Allocation
1055 * @param w Width of the region to update
1056 * @param h Height of the region to update
1057 * @param data to be placed into the Allocation
1058 */
Jason Samsf7086092011-01-12 13:28:37 -08001059 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001060 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001061 validateIsFloat32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001062 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001063 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001064 }
1065
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001066 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001067 * Copy a rectangular region from an Allocation into a rectangular region in
1068 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001069 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001070 * @param xoff X offset of the region in this Allocation
1071 * @param yoff Y offset of the region in this Allocation
1072 * @param w Width of the region to update.
1073 * @param h Height of the region to update.
1074 * @param data source Allocation.
1075 * @param dataXoff X offset in source Allocation
1076 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001077 */
1078 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1079 Allocation data, int dataXoff, int dataYoff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001080 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001081 mRS.validate();
1082 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -07001083 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -07001084 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001085 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -07001086 data.mSelectedLOD, data.mSelectedFace.mID);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001087 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001088 }
1089
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001090 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001091 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1092 * and width of the update will use the height and width of the {@link
1093 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001094 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001095 * @param xoff X offset of the region to update in this Allocation
1096 * @param yoff Y offset of the region to update in this Allocation
1097 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001098 */
1099 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001100 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Jason Samsfa445b92011-01-07 17:00:07 -08001101 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001102 if (data.getConfig() == null) {
1103 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1104 Canvas c = new Canvas(newBitmap);
1105 c.drawBitmap(data, 0, 0, null);
1106 copy2DRangeFrom(xoff, yoff, newBitmap);
Jason Samsb05d6892013-04-09 15:59:24 -07001107 return;
Tim Murrayabd5db92013-02-28 11:45:22 -08001108 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001109 validateBitmapFormat(data);
1110 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -07001111 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001112 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001113 }
1114
Jason Samsb05d6892013-04-09 15:59:24 -07001115 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1116 if (mAdaptedAllocation != null) {
1117
1118 } else {
1119
1120 if (xoff < 0 || yoff < 0 || zoff < 0) {
1121 throw new RSIllegalArgumentException("Offset cannot be negative.");
1122 }
1123 if (h < 0 || w < 0 || d < 0) {
1124 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1125 }
1126 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1127 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1128 }
1129 }
1130 }
1131
1132 /**
1133 * @hide
1134 *
1135 */
1136 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1137 mRS.validate();
1138 validate3DRange(xoff, yoff, zoff, w, h, d);
1139 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1140 w, h, d, data, data.length);
1141 }
1142
1143 /**
1144 * @hide
1145 *
1146 */
1147 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1148 mRS.validate();
1149 validate3DRange(xoff, yoff, zoff, w, h, d);
1150 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1151 w, h, d, data, data.length * 2);
1152 }
1153
1154 /**
1155 * @hide
1156 *
1157 */
1158 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
1159 mRS.validate();
1160 validate3DRange(xoff, yoff, zoff, w, h, d);
1161 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1162 w, h, d, data, data.length * 4);
1163 }
1164
1165 /**
1166 * @hide
1167 *
1168 */
1169 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
1170 mRS.validate();
1171 validate3DRange(xoff, yoff, zoff, w, h, d);
1172 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1173 w, h, d, data, data.length * 4);
1174 }
1175
1176
1177 /**
1178 * @hide
1179 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001180 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001181 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001182 * @param xoff X offset of the region to update in this Allocation
1183 * @param yoff Y offset of the region to update in this Allocation
1184 * @param zoff Z offset of the region to update in this Allocation
1185 * @param w Width of the region to update
1186 * @param h Height of the region to update
1187 * @param d Depth of the region to update
Jason Samsb05d6892013-04-09 15:59:24 -07001188 * @param data to be placed into the allocation
1189 */
1190 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1191 validateIsInt8();
1192 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1193 }
1194
1195 /**
1196 * @hide
1197 *
1198 */
1199 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1200 validateIsInt16();
1201 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1202 }
1203
1204 /**
1205 * @hide
1206 *
1207 */
1208 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
1209 validateIsInt32();
1210 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1211 }
1212
1213 /**
1214 * @hide
1215 *
1216 */
1217 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
1218 validateIsFloat32();
1219 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1220 }
1221
1222 /**
1223 * @hide
1224 * Copy a rectangular region into the allocation from another
1225 * allocation.
1226 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001227 * @param xoff X offset of the region to update in this Allocation
1228 * @param yoff Y offset of the region to update in this Allocation
1229 * @param zoff Z offset of the region to update in this Allocation
1230 * @param w Width of the region to update.
1231 * @param h Height of the region to update.
1232 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001233 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001234 * @param dataXoff X offset of the region in the source Allocation
1235 * @param dataYoff Y offset of the region in the source Allocation
1236 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001237 */
1238 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1239 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1240 mRS.validate();
1241 validate3DRange(xoff, yoff, zoff, w, h, d);
1242 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1243 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1244 data.mSelectedLOD);
1245 }
1246
Jason Samsfa445b92011-01-07 17:00:07 -08001247
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001248 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001249 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1250 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001251 *
1252 * @param b The bitmap to be set from the Allocation.
1253 */
Jason Samsfa445b92011-01-07 17:00:07 -08001254 public void copyTo(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001255 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsfb9f82c2011-01-12 14:53:25 -08001256 mRS.validate();
1257 validateBitmapFormat(b);
1258 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -07001259 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001260 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001261 }
1262
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001263 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001264 * Copy from the Allocation into a byte array. The array must be at least
1265 * as large as the Allocation. The allocation must be of an 8 bit integer
1266 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001267 *
1268 * @param d The array to be set from the Allocation.
1269 */
Jason Samsfa445b92011-01-07 17:00:07 -08001270 public void copyTo(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001271 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001272 validateIsInt8();
Jason Sams771bebb2009-12-07 12:40:12 -08001273 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001274 mRS.nAllocationRead(getID(mRS), d);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001275 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams40a29e82009-08-10 14:55:26 -07001276 }
1277
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001278 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001279 * Copy from the Allocation into a short array. The array must be at least
1280 * as large as the Allocation. The allocation must be of an 16 bit integer
1281 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001282 *
1283 * @param d The array to be set from the Allocation.
1284 */
Jason Samsfa445b92011-01-07 17:00:07 -08001285 public void copyTo(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001286 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001287 validateIsInt16();
Jason Samsfa445b92011-01-07 17:00:07 -08001288 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001289 mRS.nAllocationRead(getID(mRS), d);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001290 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001291 }
1292
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001293 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001294 * Copy from the Allocation into a int array. The array must be at least as
1295 * large as the Allocation. The allocation must be of an 32 bit integer
1296 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001297 *
1298 * @param d The array to be set from the Allocation.
1299 */
Jason Samsfa445b92011-01-07 17:00:07 -08001300 public void copyTo(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001301 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001302 validateIsInt32();
Jason Samsfa445b92011-01-07 17:00:07 -08001303 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001304 mRS.nAllocationRead(getID(mRS), d);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001305 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001306 }
1307
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001308 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001309 * Copy from the Allocation into a float array. The array must be at least
1310 * as large as the Allocation. The allocation must be of an 32 bit float
1311 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001312 *
1313 * @param d The array to be set from the Allocation.
1314 */
Jason Samsfa445b92011-01-07 17:00:07 -08001315 public void copyTo(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001316 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001317 validateIsFloat32();
Jason Sams771bebb2009-12-07 12:40:12 -08001318 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001319 mRS.nAllocationRead(getID(mRS), d);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001320 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams40a29e82009-08-10 14:55:26 -07001321 }
1322
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001323 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001324 * Resize a 1D allocation. The contents of the allocation are preserved.
1325 * If new elements are allocated objects are created with null contents and
1326 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001327 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001328 * <p>If the new region is smaller the references of any objects outside the
1329 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001330 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001331 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001332 *
1333 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001334 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001335 * @deprecated RenderScript objects should be immutable once created. The
1336 * replacement is to create a new allocation and copy the contents.
Jason Samsf7086092011-01-12 13:28:37 -08001337 */
Jason Sams31a7e422010-10-26 13:09:17 -07001338 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001339 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001340 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001341 }
Jason Samse07694b2012-04-03 15:36:36 -07001342 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001343 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001344
Jason Samse07694b2012-04-03 15:36:36 -07001345 int typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001346 mType = new Type(typeID, mRS);
1347 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001348 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001349 }
1350
Jason Samsb8c5a842009-07-31 20:40:47 -07001351
1352 // creation
1353
Jason Sams49a05d72010-12-29 14:31:29 -08001354 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001355 static {
1356 mBitmapOptions.inScaled = false;
1357 }
1358
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001359 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001360 * Creates a new Allocation with the given {@link
1361 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001362 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001363 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001364 * @param mips specifies desired mipmap behaviour for the
1365 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001366 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001367 * utilized
1368 */
1369 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001370 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
Jason Sams771bebb2009-12-07 12:40:12 -08001371 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001372 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001373 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001374 }
Jason Samse07694b2012-04-03 15:36:36 -07001375 int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001376 if (id == 0) {
1377 throw new RSRuntimeException("Allocation creation failed.");
1378 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001379 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams857d0c72011-11-23 15:02:15 -08001380 return new Allocation(id, rs, type, usage);
1381 }
1382
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001383 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001384 * Creates an Allocation with the size specified by the type and no mipmaps
1385 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001386 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001387 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001388 * @param type renderscript type describing data layout
1389 * @param usage bit field specifying how the allocation is
1390 * utilized
1391 *
1392 * @return allocation
1393 */
Jason Samse5d37122010-12-16 00:33:33 -08001394 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1395 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1396 }
1397
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001398 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001399 * Creates an Allocation for use by scripts with a given {@link
1400 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001401 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001402 * @param rs Context to which the Allocation will belong.
1403 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001404 *
1405 * @return allocation
1406 */
Jason Sams5476b452010-12-08 16:14:36 -08001407 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001408 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001409 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001410
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001411 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001412 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001413 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001414 * @param rs Context to which the Allocation will belong.
1415 * @param e Element to use in the Allocation
1416 * @param count the number of Elements in the Allocation
1417 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001418 * utilized
1419 *
1420 * @return allocation
1421 */
Jason Sams5476b452010-12-08 16:14:36 -08001422 static public Allocation createSized(RenderScript rs, Element e,
1423 int count, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001424 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
Jason Sams771bebb2009-12-07 12:40:12 -08001425 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001426 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001427 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001428 Type t = b.create();
1429
Jason Samse07694b2012-04-03 15:36:36 -07001430 int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001431 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001432 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001433 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001434 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001435 return new Allocation(id, rs, t, usage);
1436 }
1437
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001438 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001439 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001440 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001441 * @param rs Context to which the Allocation will belong.
1442 * @param e Element to use in the Allocation
1443 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001444 *
1445 * @return allocation
1446 */
Jason Sams5476b452010-12-08 16:14:36 -08001447 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001448 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001449 }
1450
Jason Sams49a05d72010-12-29 14:31:29 -08001451 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001452 final Bitmap.Config bc = b.getConfig();
1453 if (bc == Bitmap.Config.ALPHA_8) {
1454 return Element.A_8(rs);
1455 }
1456 if (bc == Bitmap.Config.ARGB_4444) {
1457 return Element.RGBA_4444(rs);
1458 }
1459 if (bc == Bitmap.Config.ARGB_8888) {
1460 return Element.RGBA_8888(rs);
1461 }
1462 if (bc == Bitmap.Config.RGB_565) {
1463 return Element.RGB_565(rs);
1464 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001465 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001466 }
1467
Jason Sams49a05d72010-12-29 14:31:29 -08001468 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001469 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001470 Element e = elementFromBitmap(rs, b);
1471 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001472 tb.setX(b.getWidth());
1473 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001474 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001475 return tb.create();
1476 }
1477
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001478 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001479 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001480 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001481 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001482 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001483 * @param mips specifies desired mipmap behaviour for the
1484 * allocation
1485 * @param usage bit field specifying how the allocation is
1486 * utilized
1487 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001488 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001489 *
1490 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001491 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001492 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001493 int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001494 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
Jason Sams771bebb2009-12-07 12:40:12 -08001495 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001496
1497 // WAR undocumented color formats
1498 if (b.getConfig() == null) {
1499 if ((usage & USAGE_SHARED) != 0) {
1500 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1501 }
1502 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1503 Canvas c = new Canvas(newBitmap);
1504 c.drawBitmap(b, 0, 0, null);
1505 return createFromBitmap(rs, newBitmap, mips, usage);
1506 }
1507
Jason Sams5476b452010-12-08 16:14:36 -08001508 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001509
Tim Murraya3145512012-12-04 17:59:29 -08001510 // enable optimized bitmap path only with no mipmap and script-only usage
1511 if (mips == MipmapControl.MIPMAP_NONE &&
1512 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Tim Murray78e64942013-04-09 17:28:56 -07001513 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
Tim Murraya3145512012-12-04 17:59:29 -08001514 int id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
1515 if (id == 0) {
1516 throw new RSRuntimeException("Load failed.");
1517 }
1518
1519 // keep a reference to the Bitmap around to prevent GC
1520 Allocation alloc = new Allocation(id, rs, t, usage);
1521 alloc.setBitmap(b);
1522 return alloc;
1523 }
1524
Jason Sams9bf18922013-04-13 19:48:36 -07001525
Jason Samse07694b2012-04-03 15:36:36 -07001526 int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001527 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001528 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001529 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001530 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001531 return new Allocation(id, rs, t, usage);
1532 }
1533
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001534 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001535 * Returns the handle to a raw buffer that is being managed by the screen
1536 * compositor. This operation is only valid for Allocations with {@link
1537 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001538 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001539 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001540 *
1541 */
1542 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08001543 if ((mUsage & USAGE_IO_INPUT) == 0) {
1544 throw new RSInvalidStateException("Allocation is not a surface texture.");
1545 }
1546 return mRS.nAllocationGetSurface(getID(mRS));
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001547 }
1548
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001549 /**
Jason Samsc089c2f2013-02-22 13:57:36 -08001550 * @hide
1551 */
1552 public void setSurfaceTexture(SurfaceTexture st) {
1553 setSurface(new Surface(st));
1554 }
1555
1556 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001557 * Associate a {@link android.view.Surface} with this Allocation. This
1558 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001559 *
1560 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001561 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001562 public void setSurface(Surface sur) {
1563 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001564 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1565 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1566 }
1567
Jason Samse07694b2012-04-03 15:36:36 -07001568 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001569 }
1570
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001571 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001572 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08001573 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001574 * <p>With target API version 18 or greater, this Allocation will be created
1575 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
1576 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
1577 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001578 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001579 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001580 * @param b bitmap source for the allocation data
1581 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001582 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001583 *
1584 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001585 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08001586 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1587 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07001588 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08001589 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001590 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1591 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001592 }
1593
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001594 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001595 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1596 * containing the horizontal list of cube faces. Each face must be a square,
1597 * have the same size as all other faces, and have a width that is a power
1598 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001599 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001600 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001601 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001602 * format: right, left, top, bottom, front, back
1603 * @param mips specifies desired mipmap behaviour for the cubemap
1604 * @param usage bit field specifying how the cubemap is utilized
1605 *
1606 * @return allocation containing cubemap data
1607 *
1608 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001609 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001610 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001611 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001612 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001613
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001614 int height = b.getHeight();
1615 int width = b.getWidth();
1616
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001617 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001618 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1619 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001620 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001621 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001622 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001623 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001624 if (!isPow2) {
1625 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1626 }
1627
1628 Element e = elementFromBitmap(rs, b);
1629 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001630 tb.setX(height);
1631 tb.setY(height);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001632 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001633 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001634 Type t = tb.create();
1635
Jason Samse07694b2012-04-03 15:36:36 -07001636 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001637 if(id == 0) {
1638 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1639 }
Jason Sams5476b452010-12-08 16:14:36 -08001640 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001641 }
1642
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001643 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001644 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
1645 * from a {@link android.graphics.Bitmap} containing the horizontal list of
1646 * cube faces. Each face must be a square, have the same size as all other
1647 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001648 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001649 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001650 * @param b bitmap with cubemap faces layed out in the following
1651 * format: right, left, top, bottom, front, back
1652 *
1653 * @return allocation containing cubemap data
1654 *
1655 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001656 static public Allocation createCubemapFromBitmap(RenderScript rs,
1657 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001658 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001659 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001660 }
1661
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001662 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001663 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
1664 * objects containing the cube faces. Each face must be a square, have the
1665 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001666 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001667 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001668 * @param xpos cubemap face in the positive x direction
1669 * @param xneg cubemap face in the negative x direction
1670 * @param ypos cubemap face in the positive y direction
1671 * @param yneg cubemap face in the negative y direction
1672 * @param zpos cubemap face in the positive z direction
1673 * @param zneg cubemap face in the negative z direction
1674 * @param mips specifies desired mipmap behaviour for the cubemap
1675 * @param usage bit field specifying how the cubemap is utilized
1676 *
1677 * @return allocation containing cubemap data
1678 *
1679 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001680 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1681 Bitmap xpos,
1682 Bitmap xneg,
1683 Bitmap ypos,
1684 Bitmap yneg,
1685 Bitmap zpos,
1686 Bitmap zneg,
1687 MipmapControl mips,
1688 int usage) {
1689 int height = xpos.getHeight();
1690 if (xpos.getWidth() != height ||
1691 xneg.getWidth() != height || xneg.getHeight() != height ||
1692 ypos.getWidth() != height || ypos.getHeight() != height ||
1693 yneg.getWidth() != height || yneg.getHeight() != height ||
1694 zpos.getWidth() != height || zpos.getHeight() != height ||
1695 zneg.getWidth() != height || zneg.getHeight() != height) {
1696 throw new RSIllegalArgumentException("Only square cube map faces supported");
1697 }
1698 boolean isPow2 = (height & (height - 1)) == 0;
1699 if (!isPow2) {
1700 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1701 }
1702
1703 Element e = elementFromBitmap(rs, xpos);
1704 Type.Builder tb = new Type.Builder(rs, e);
1705 tb.setX(height);
1706 tb.setY(height);
1707 tb.setFaces(true);
1708 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1709 Type t = tb.create();
1710 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1711
1712 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001713 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001714 adapter.copyFrom(xpos);
1715 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1716 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001717 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001718 adapter.copyFrom(ypos);
1719 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1720 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001721 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001722 adapter.copyFrom(zpos);
1723 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1724 adapter.copyFrom(zneg);
1725
1726 return cubemap;
1727 }
1728
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001729 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001730 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
1731 * from 6 {@link android.graphics.Bitmap} objects containing the cube
1732 * faces. Each face must be a square, have the same size as all other faces,
1733 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001734 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001735 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001736 * @param xpos cubemap face in the positive x direction
1737 * @param xneg cubemap face in the negative x direction
1738 * @param ypos cubemap face in the positive y direction
1739 * @param yneg cubemap face in the negative y direction
1740 * @param zpos cubemap face in the positive z direction
1741 * @param zneg cubemap face in the negative z direction
1742 *
1743 * @return allocation containing cubemap data
1744 *
1745 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001746 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1747 Bitmap xpos,
1748 Bitmap xneg,
1749 Bitmap ypos,
1750 Bitmap yneg,
1751 Bitmap zpos,
1752 Bitmap zneg) {
1753 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1754 zpos, zneg, MipmapControl.MIPMAP_NONE,
1755 USAGE_GRAPHICS_TEXTURE);
1756 }
1757
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001758 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001759 * Creates an Allocation from the Bitmap referenced
1760 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001761 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001762 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001763 * @param res application resources
1764 * @param id resource id to load the data from
1765 * @param mips specifies desired mipmap behaviour for the
1766 * allocation
1767 * @param usage bit field specifying how the allocation is
1768 * utilized
1769 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001770 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001771 *
1772 */
Jason Sams5476b452010-12-08 16:14:36 -08001773 static public Allocation createFromBitmapResource(RenderScript rs,
1774 Resources res,
1775 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001776 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001777 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001778
Jason Sams771bebb2009-12-07 12:40:12 -08001779 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07001780 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
1781 throw new RSIllegalArgumentException("Unsupported usage specified.");
1782 }
Jason Sams5476b452010-12-08 16:14:36 -08001783 Bitmap b = BitmapFactory.decodeResource(res, id);
1784 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1785 b.recycle();
1786 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001787 }
1788
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001789 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001790 * Creates a non-mipmapped Allocation to use as a graphics texture from the
1791 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001792 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001793 * <p>With target API version 18 or greater, this allocation will be created
1794 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
1795 * target API version 17 or lower, this allocation will be created with
1796 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08001797 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001798 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001799 * @param res application resources
1800 * @param id resource id to load the data from
1801 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001802 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001803 *
1804 */
Jason Sams5476b452010-12-08 16:14:36 -08001805 static public Allocation createFromBitmapResource(RenderScript rs,
1806 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001807 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08001808 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1809 return createFromBitmapResource(rs, res, id,
1810 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07001811 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08001812 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001813 return createFromBitmapResource(rs, res, id,
1814 MipmapControl.MIPMAP_NONE,
1815 USAGE_GRAPHICS_TEXTURE);
1816 }
1817
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001818 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001819 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001820 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001821 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001822 * @param str string to create the allocation from
1823 * @param usage bit field specifying how the allocaiton is
1824 * utilized
1825 *
1826 */
Jason Sams5476b452010-12-08 16:14:36 -08001827 static public Allocation createFromString(RenderScript rs,
1828 String str,
1829 int usage) {
1830 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001831 byte[] allocArray = null;
1832 try {
1833 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001834 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001835 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001836 return alloc;
1837 }
1838 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001839 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001840 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001841 }
Jason Sams739c8262013-04-11 18:07:52 -07001842
1843 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001844 * Interface to handle notification when new buffers are available via
1845 * {@link #USAGE_IO_INPUT}. An application will receive one notification
1846 * when a buffer is available. Additional buffers will not trigger new
1847 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07001848 */
Jason Sams42ef2382013-08-29 13:30:59 -07001849 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07001850 public void onBufferAvailable(Allocation a);
1851 }
1852
1853 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001854 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07001855 *
Jason Sams42ef2382013-08-29 13:30:59 -07001856 * @param callback instance of the OnBufferAvailableListener
1857 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07001858 */
Jason Sams42ef2382013-08-29 13:30:59 -07001859 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07001860 synchronized(mAllocationMap) {
1861 mAllocationMap.put(new Integer(getID(mRS)), this);
1862 mBufferNotifier = callback;
1863 }
1864 }
1865
1866 static void sendBufferNotification(int id) {
1867 synchronized(mAllocationMap) {
1868 Allocation a = mAllocationMap.get(new Integer(id));
1869
1870 if ((a != null) && (a.mBufferNotifier != null)) {
1871 a.mBufferNotifier.onBufferAvailable(a);
1872 }
1873 }
1874 }
1875
Jason Samsb8c5a842009-07-31 20:40:47 -07001876}
1877