blob: 8e69f562336b5ac3bff91fa14b6f361eb3a40d45 [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
Stephen Hines9069ee82012-02-13 18:25:54 -08002 * Copyright (C) 2008-2012 The Android Open Source Project
Jason Samsb8c5a842009-07-31 20:40:47 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
Jason Samsb8c5a842009-07-31 20:40:47 -070019import java.io.IOException;
20import java.io.InputStream;
Jason Sams739c8262013-04-11 18:07:52 -070021import java.util.HashMap;
Jason Samsb8c5a842009-07-31 20:40:47 -070022import android.content.res.Resources;
Romain Guy650a3eb2009-08-31 14:06:43 -070023import android.content.res.AssetManager;
Jason Samsb8c5a842009-07-31 20:40:47 -070024import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
Jason Samsfb9aa9f2012-03-28 15:30:07 -070026import android.view.Surface;
Jason Samsb8c5a842009-07-31 20:40:47 -070027import android.util.Log;
Romain Guy650a3eb2009-08-31 14:06:43 -070028import android.util.TypedValue;
Tim Murrayabd5db92013-02-28 11:45:22 -080029import android.graphics.Canvas;
Tim Murray6d7a53c2013-05-23 16:59:23 -070030import android.os.Trace;
Jason Samsb8c5a842009-07-31 20:40:47 -070031
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070032/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070033 * <p> This class provides the primary method through which data is passed to
34 * and from RenderScript kernels. An Allocation provides the backing store for
35 * a given {@link android.renderscript.Type}. </p>
Jason Samsa23d4e72011-01-04 18:59:12 -080036 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070037 * <p>An Allocation also contains a set of usage flags that denote how the
38 * Allocation could be used. For example, an Allocation may have usage flags
39 * specifying that it can be used from a script as well as input to a {@link
40 * android.renderscript.Sampler}. A developer must synchronize across these
41 * different usages using {@link android.renderscript.Allocation#syncAll} in
42 * order to ensure that different users of the Allocation have a consistent view
43 * of memory. For example, in the case where an Allocation is used as the output
44 * of one kernel and as Sampler input in a later kernel, a developer must call
45 * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
46 * second kernel to ensure correctness.
Jason Samsa23d4e72011-01-04 18:59:12 -080047 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070048 * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
49 * more complex Element types, the {@link #copyFromUnchecked} methods can be
50 * used to copy from byte arrays or similar constructs.</p>
Jason Samsb8c5a842009-07-31 20:40:47 -070051 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080052 * <div class="special reference">
53 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070054 * <p>For more information about creating an application that uses RenderScript, read the
55 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080056 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070057 **/
58public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070059 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080060 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080061 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070062 Allocation mAdaptedAllocation;
Tim Murray2f2472c2013-08-22 14:55:26 -070063 int mSize;
Jason Samsba862d12011-07-07 15:24:42 -070064
65 boolean mConstrainedLOD;
66 boolean mConstrainedFace;
67 boolean mConstrainedY;
68 boolean mConstrainedZ;
Jason Sams615e7ce2012-01-13 14:01:20 -080069 boolean mReadAllowed = true;
70 boolean mWriteAllowed = true;
Jason Samsba862d12011-07-07 15:24:42 -070071 int mSelectedY;
72 int mSelectedZ;
73 int mSelectedLOD;
74 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
75
76 int mCurrentDimX;
77 int mCurrentDimY;
78 int mCurrentDimZ;
79 int mCurrentCount;
Jason Sams739c8262013-04-11 18:07:52 -070080 static HashMap<Integer, Allocation> mAllocationMap =
81 new HashMap<Integer, Allocation>();
Jason Sams42ef2382013-08-29 13:30:59 -070082 OnBufferAvailableListener mBufferNotifier;
Jason Samsba862d12011-07-07 15:24:42 -070083
Tim Murrayc11e25c2013-04-09 11:01:01 -070084 /**
85 * The usage of the Allocation. These signal to RenderScript where to place
86 * the Allocation in memory.
87 *
88 */
Jason Sams5476b452010-12-08 16:14:36 -080089
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070090 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -070091 * The Allocation will be bound to and accessed by scripts.
Jason Samsf7086092011-01-12 13:28:37 -080092 */
Jason Sams5476b452010-12-08 16:14:36 -080093 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -080094
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070095 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -070096 * The Allocation will be used as a texture source by one or more graphics
97 * programs.
Jason Samsf7086092011-01-12 13:28:37 -080098 *
99 */
Jason Sams5476b452010-12-08 16:14:36 -0800100 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800101
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700102 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700103 * The Allocation will be used as a graphics mesh.
104 *
105 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800106 *
107 */
Jason Sams5476b452010-12-08 16:14:36 -0800108 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800109
110
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700111 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700112 * The Allocation will be used as the source of shader constants by one or
113 * more programs.
114 *
115 * This was deprecated in API level 16.
Jason Samsf7086092011-01-12 13:28:37 -0800116 *
117 */
Jason Sams5476b452010-12-08 16:14:36 -0800118 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
119
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700120 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700121 * The Allocation will be used as a target for offscreen rendering
122 *
123 * This was deprecated in API level 16.
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700124 *
125 */
126 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
127
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700128 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700129 * The Allocation will be used as a {@link android.view.Surface}
130 * consumer. This usage will cause the Allocation to be created
131 * as read-only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800132 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800133 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700134 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800135
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700136 /**
Jason Sams3a1b8e42013-09-24 15:18:52 -0700137 * The Allocation will be used as a {@link android.view.Surface}
Tim Murrayc11e25c2013-04-09 11:01:01 -0700138 * producer. The dimensions and format of the {@link
Jason Sams3a1b8e42013-09-24 15:18:52 -0700139 * android.view.Surface} will be forced to those of the
Tim Murrayc11e25c2013-04-09 11:01:01 -0700140 * Allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800141 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800142 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700143 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700144
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700145 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700146 * The Allocation's backing store will be inherited from another object
147 * (usually a {@link android.graphics.Bitmap}); copying to or from the
148 * original source Bitmap will cause a synchronization rather than a full
149 * copy. {@link #syncAll} may also be used to synchronize the Allocation
150 * and the source Bitmap.
Tim Murray00bb4542012-12-17 16:35:06 -0800151 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700152 * <p>This is set by default for allocations created with {@link
153 * #createFromBitmap} in API version 18 and higher.</p>
Tim Murray00bb4542012-12-17 16:35:06 -0800154 *
155 */
156 public static final int USAGE_SHARED = 0x0080;
157
158 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700159 * Controls mipmap behavior when using the bitmap creation and update
160 * functions.
Jason Samsf7086092011-01-12 13:28:37 -0800161 */
Jason Sams4ef66502010-12-10 16:03:15 -0800162 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700163 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700164 * No mipmaps will be generated and the type generated from the incoming
165 * bitmap will not contain additional LODs.
Jason Samsf7086092011-01-12 13:28:37 -0800166 */
Jason Sams5476b452010-12-08 16:14:36 -0800167 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800168
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700169 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700170 * A full mipmap chain will be created in script memory. The Type of
171 * the Allocation will contain a full mipmap chain. On upload, the full
172 * chain will be transferred.
Jason Samsf7086092011-01-12 13:28:37 -0800173 */
Jason Sams5476b452010-12-08 16:14:36 -0800174 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800175
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700176 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700177 * The Type of the Allocation will be the same as MIPMAP_NONE. It will
178 * not contain mipmaps. On upload, the allocation data will contain a
179 * full mipmap chain generated from the top level in script memory.
Jason Samsf7086092011-01-12 13:28:37 -0800180 */
Jason Sams5476b452010-12-08 16:14:36 -0800181 MIPMAP_ON_SYNC_TO_TEXTURE(2);
182
183 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800184 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800185 mID = id;
186 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700187 }
188
Jason Sams48fe5342011-07-08 13:52:30 -0700189
190 private int getIDSafe() {
191 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700192 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700193 }
Jason Samse07694b2012-04-03 15:36:36 -0700194 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700195 }
196
Jason Sams03d2d002012-03-23 13:51:56 -0700197
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700198 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700199 * Get the {@link android.renderscript.Element} of the {@link
200 * android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700201 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700202 * @return Element
Jason Sams03d2d002012-03-23 13:51:56 -0700203 *
204 */
205 public Element getElement() {
206 return mType.getElement();
207 }
208
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700209 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700210 * Get the usage flags of the Allocation.
211 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700212 * @return usage this Allocation's set of the USAGE_* flags OR'd together
Jason Sams03d2d002012-03-23 13:51:56 -0700213 *
214 */
215 public int getUsage() {
216 return mUsage;
217 }
218
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700219 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700220 * Get the size of the Allocation in bytes.
221 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700222 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700223 *
224 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700225 public int getBytesSize() {
226 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700227 }
228
Jason Sams452a7662011-07-07 16:05:18 -0700229 private void updateCacheInfo(Type t) {
230 mCurrentDimX = t.getX();
231 mCurrentDimY = t.getY();
232 mCurrentDimZ = t.getZ();
233 mCurrentCount = mCurrentDimX;
234 if (mCurrentDimY > 1) {
235 mCurrentCount *= mCurrentDimY;
236 }
237 if (mCurrentDimZ > 1) {
238 mCurrentCount *= mCurrentDimZ;
239 }
240 }
Jason Samsba862d12011-07-07 15:24:42 -0700241
Tim Murraya3145512012-12-04 17:59:29 -0800242 private void setBitmap(Bitmap b) {
243 mBitmap = b;
244 }
245
Jason Sams5476b452010-12-08 16:14:36 -0800246 Allocation(int id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700247 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800248 if ((usage & ~(USAGE_SCRIPT |
249 USAGE_GRAPHICS_TEXTURE |
250 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700251 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800252 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800253 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800254 USAGE_IO_OUTPUT |
255 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800256 throw new RSIllegalArgumentException("Unknown usage specified.");
257 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800258
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700259 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800260 mWriteAllowed = false;
261
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700262 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800263 USAGE_GRAPHICS_TEXTURE |
264 USAGE_SCRIPT)) != 0) {
265 throw new RSIllegalArgumentException("Invalid usage combination.");
266 }
267 }
Jason Sams9bf18922013-04-13 19:48:36 -0700268
Jason Sams5476b452010-12-08 16:14:36 -0800269 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800270 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700271
Jason Sams452a7662011-07-07 16:05:18 -0700272 if (t != null) {
Stephen Hines88990da2013-09-09 17:56:07 -0700273 // TODO: A3D doesn't have Type info during creation, so we can't
274 // calculate the size ahead of time. We can possibly add a method
275 // to update the size in the future if it seems reasonable.
276 mSize = mType.getCount() * mType.getElement().getBytesSize();
Jason Sams452a7662011-07-07 16:05:18 -0700277 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700278 }
Tim Murray2f2472c2013-08-22 14:55:26 -0700279 try {
280 RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
281 } catch (Exception e) {
282 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
283 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
284 }
285 }
286
287 protected void finalize() throws Throwable {
288 RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
289 super.finalize();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700290 }
291
Jason Samsb97b2512011-01-16 15:04:08 -0800292 private void validateIsInt32() {
293 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
294 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
295 return;
296 }
297 throw new RSIllegalArgumentException(
298 "32 bit integer source does not match allocation type " + mType.mElement.mType);
299 }
300
301 private void validateIsInt16() {
302 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
303 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
304 return;
305 }
306 throw new RSIllegalArgumentException(
307 "16 bit integer source does not match allocation type " + mType.mElement.mType);
308 }
309
310 private void validateIsInt8() {
311 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
312 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
313 return;
314 }
315 throw new RSIllegalArgumentException(
316 "8 bit integer source does not match allocation type " + mType.mElement.mType);
317 }
318
319 private void validateIsFloat32() {
320 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
321 return;
322 }
323 throw new RSIllegalArgumentException(
324 "32 bit float source does not match allocation type " + mType.mElement.mType);
325 }
326
327 private void validateIsObject() {
328 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
329 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
330 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
331 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
332 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
333 (mType.mElement.mType == Element.DataType.RS_MESH) ||
334 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
335 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
336 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
337 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
338 return;
339 }
340 throw new RSIllegalArgumentException(
341 "Object source does not match allocation type " + mType.mElement.mType);
342 }
343
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700344 @Override
345 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800346 super.updateFromNative();
Jason Samse07694b2012-04-03 15:36:36 -0700347 int typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700348 if(typeID != 0) {
349 mType = new Type(typeID, mRS);
350 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700351 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700352 }
353 }
354
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700355 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700356 * Get the {@link android.renderscript.Type} of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700357 *
358 * @return Type
359 *
360 */
Jason Samsea87e962010-01-12 12:12:28 -0800361 public Type getType() {
362 return mType;
363 }
364
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700365 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700366 * Propagate changes from one usage of the Allocation to the
367 * other usages of the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700368 *
369 */
Jason Sams5476b452010-12-08 16:14:36 -0800370 public void syncAll(int srcLocation) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700371 Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
Jason Sams5476b452010-12-08 16:14:36 -0800372 switch (srcLocation) {
Jason Sams5476b452010-12-08 16:14:36 -0800373 case USAGE_GRAPHICS_TEXTURE:
Tim Murray78e64942013-04-09 17:28:56 -0700374 case USAGE_SCRIPT:
375 if ((mUsage & USAGE_SHARED) != 0) {
376 copyFrom(mBitmap);
377 }
378 break;
379 case USAGE_GRAPHICS_CONSTANTS:
Jason Sams5476b452010-12-08 16:14:36 -0800380 case USAGE_GRAPHICS_VERTEX:
381 break;
Tim Murray78e64942013-04-09 17:28:56 -0700382 case USAGE_SHARED:
383 if ((mUsage & USAGE_SHARED) != 0) {
384 copyTo(mBitmap);
385 }
386 break;
Jason Sams5476b452010-12-08 16:14:36 -0800387 default:
388 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
389 }
390 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700391 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700392 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -0800393 }
394
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700395 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700396 * Send a buffer to the output stream. The contents of the Allocation will
397 * be undefined after this operation. This operation is only valid if {@link
398 * #USAGE_IO_OUTPUT} is set on the Allocation.
399 *
Jason Sams163766c2012-02-15 12:04:24 -0800400 *
Jason Sams163766c2012-02-15 12:04:24 -0800401 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700402 public void ioSend() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700403 Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
Jason Sams163766c2012-02-15 12:04:24 -0800404 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
405 throw new RSIllegalArgumentException(
406 "Can only send buffer if IO_OUTPUT usage specified.");
407 }
408 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700409 mRS.nAllocationIoSend(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700410 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800411 }
412
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700413 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700414 * Receive the latest input into the Allocation. This operation
415 * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
Jason Sams163766c2012-02-15 12:04:24 -0800416 *
Jason Sams163766c2012-02-15 12:04:24 -0800417 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700418 public void ioReceive() {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700419 Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
Jason Sams163766c2012-02-15 12:04:24 -0800420 if ((mUsage & USAGE_IO_INPUT) == 0) {
421 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700422 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800423 }
424 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700425 mRS.nAllocationIoReceive(getID(mRS));
Tim Murray6d7a53c2013-05-23 16:59:23 -0700426 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams163766c2012-02-15 12:04:24 -0800427 }
428
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700429 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700430 * Copy an array of RS objects to the Allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700431 *
432 * @param d Source array.
433 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800434 public void copyFrom(BaseObj[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700435 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Sams771bebb2009-12-07 12:40:12 -0800436 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800437 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700438 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800439 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700440 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800441 }
442 int i[] = new int[d.length];
443 for (int ct=0; ct < d.length; ct++) {
Jason Samse07694b2012-04-03 15:36:36 -0700444 i[ct] = d[ct].getID(mRS);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800445 }
Jason Samsba862d12011-07-07 15:24:42 -0700446 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700447 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700448 }
449
Jason Samsfb9f82c2011-01-12 14:53:25 -0800450 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800451 Bitmap.Config bc = b.getConfig();
Tim Murrayabd5db92013-02-28 11:45:22 -0800452 if (bc == null) {
453 throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
454 }
Jason Sams252c0782011-01-11 17:42:52 -0800455 switch (bc) {
456 case ALPHA_8:
457 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
458 throw new RSIllegalArgumentException("Allocation kind is " +
459 mType.getElement().mKind + ", type " +
460 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700461 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800462 " bytes, passed bitmap was " + bc);
463 }
464 break;
465 case ARGB_8888:
466 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700467 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800468 throw new RSIllegalArgumentException("Allocation kind is " +
469 mType.getElement().mKind + ", type " +
470 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700471 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800472 " bytes, passed bitmap was " + bc);
473 }
474 break;
475 case RGB_565:
476 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700477 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800478 throw new RSIllegalArgumentException("Allocation kind is " +
479 mType.getElement().mKind + ", type " +
480 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700481 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800482 " bytes, passed bitmap was " + bc);
483 }
484 break;
485 case ARGB_4444:
486 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700487 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800488 throw new RSIllegalArgumentException("Allocation kind is " +
489 mType.getElement().mKind + ", type " +
490 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700491 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800492 " bytes, passed bitmap was " + bc);
493 }
494 break;
495
496 }
Jason Sams4ef66502010-12-10 16:03:15 -0800497 }
498
Jason Samsfb9f82c2011-01-12 14:53:25 -0800499 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700500 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800501 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
502 }
503 }
504
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700505 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700506 * Copy into this Allocation from an array. This method does not guarantee
507 * that the Allocation is compatible with the input buffer; it copies memory
508 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800509 *
510 * @param d the source data array
511 */
512 public void copyFromUnchecked(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700513 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800514 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700515 if (mCurrentDimZ > 0) {
516 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
517 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800518 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
519 } else {
520 copy1DRangeFromUnchecked(0, mCurrentCount, d);
521 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700522 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800523 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700524
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700525 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700526 * Copy into this Allocation from an array. This method does not guarantee
527 * that the Allocation is compatible with the input buffer; it copies memory
528 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800529 *
530 * @param d the source data array
531 */
532 public void copyFromUnchecked(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700533 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800534 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700535 if (mCurrentDimZ > 0) {
536 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
537 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800538 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
539 } else {
540 copy1DRangeFromUnchecked(0, mCurrentCount, d);
541 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700542 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800543 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700544
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700545 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700546 * Copy into this Allocation from an array. This method does not guarantee
547 * that the Allocation is compatible with the input buffer; it copies memory
548 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800549 *
550 * @param d the source data array
551 */
552 public void copyFromUnchecked(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700553 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800554 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700555 if (mCurrentDimZ > 0) {
556 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
557 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800558 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
559 } else {
560 copy1DRangeFromUnchecked(0, mCurrentCount, d);
561 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700562 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800563 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700564
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700565 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700566 * Copy into this Allocation from an array. This method does not guarantee
567 * that the Allocation is compatible with the input buffer; it copies memory
568 * without reinterpretation.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800569 *
570 * @param d the source data array
571 */
572 public void copyFromUnchecked(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700573 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
Jason Sams4fa3eed2011-01-19 15:44:38 -0800574 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700575 if (mCurrentDimZ > 0) {
576 copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
577 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800578 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
579 } else {
580 copy1DRangeFromUnchecked(0, mCurrentCount, d);
581 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700582 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800583 }
584
Tim Murray6d7a53c2013-05-23 16:59:23 -0700585
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700586 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700587 * Copy into this Allocation from an array. This variant is type checked
588 * and will generate exceptions if the Allocation's {@link
589 * android.renderscript.Element} is not a 32 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800590 *
591 * @param d the source data array
592 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800593 public void copyFrom(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700594 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800595 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700596 if (mCurrentDimZ > 0) {
597 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
598 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800599 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
600 } else {
601 copy1DRangeFrom(0, mCurrentCount, d);
602 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700603 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800604 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800605
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700606 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700607 * Copy into this Allocation from an array. This variant is type checked
608 * and will generate exceptions if the Allocation's {@link
609 * android.renderscript.Element} is not a 16 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800610 *
611 * @param d the source data array
612 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800613 public void copyFrom(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700614 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800615 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700616 if (mCurrentDimZ > 0) {
617 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
618 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800619 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
620 } else {
621 copy1DRangeFrom(0, mCurrentCount, d);
622 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700623 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800624 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800625
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700626 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700627 * Copy into this Allocation from an array. This variant is type checked
628 * and will generate exceptions if the Allocation's {@link
629 * android.renderscript.Element} is not an 8 bit integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800630 *
631 * @param d the source data array
632 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800633 public void copyFrom(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700634 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800635 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700636 if (mCurrentDimZ > 0) {
637 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
638 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800639 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
640 } else {
641 copy1DRangeFrom(0, mCurrentCount, d);
642 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700643 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800644 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800645
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700646 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700647 * Copy into this Allocation from an array. This variant is type checked
648 * and will generate exceptions if the Allocation's {@link
649 * android.renderscript.Element} is not a 32 bit float type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800650 *
651 * @param d the source data array
652 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800653 public void copyFrom(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700654 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800655 mRS.validate();
Jason Samsb05d6892013-04-09 15:59:24 -0700656 if (mCurrentDimZ > 0) {
657 copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
658 } else if (mCurrentDimY > 0) {
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800659 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
660 } else {
661 copy1DRangeFrom(0, mCurrentCount, d);
662 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700663 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800664 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800665
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700666 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700667 * Copy into an Allocation from a {@link android.graphics.Bitmap}. The
668 * height, width, and format of the bitmap must match the existing
669 * allocation.
670 *
671 * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
672 * android.graphics.Bitmap} used to create the Allocation with {@link
673 * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
674 * this will synchronize the Allocation with the latest data from the {@link
675 * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
Jason Sams4fa3eed2011-01-19 15:44:38 -0800676 *
677 * @param b the source bitmap
678 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800679 public void copyFrom(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700680 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Jason Samsfb9f82c2011-01-12 14:53:25 -0800681 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -0800682 if (b.getConfig() == null) {
683 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
684 Canvas c = new Canvas(newBitmap);
685 c.drawBitmap(b, 0, 0, null);
686 copyFrom(newBitmap);
687 return;
688 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800689 validateBitmapSize(b);
690 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700691 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700692 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700693 }
694
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700695 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700696 * Copy an Allocation from an Allocation. The types of both allocations
Tim Murrayf671fb02012-10-03 13:50:05 -0700697 * must be identical.
698 *
699 * @param a the source allocation
700 */
701 public void copyFrom(Allocation a) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700702 Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
Tim Murrayf671fb02012-10-03 13:50:05 -0700703 mRS.validate();
704 if (!mType.equals(a.getType())) {
705 throw new RSIllegalArgumentException("Types of allocations must match.");
706 }
707 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700708 Trace.traceEnd(RenderScript.TRACE_TAG);
Tim Murrayf671fb02012-10-03 13:50:05 -0700709 }
710
Tim Murrayf671fb02012-10-03 13:50:05 -0700711 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700712 * This is only intended to be used by auto-generated code reflected from
713 * the RenderScript script files and should not be used by developers.
Jason Samsfa445b92011-01-07 17:00:07 -0800714 *
715 * @param xoff
716 * @param fp
717 */
Jason Sams21b41032011-01-16 15:05:41 -0800718 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800719 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700720 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700721 final byte[] data = fp.getData();
722
723 int count = data.length / eSize;
724 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800725 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700726 " not divisible by element size " + eSize + ".");
727 }
Jason Samsba862d12011-07-07 15:24:42 -0700728 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700729 }
730
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700731 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700732 * This is only intended to be used by auto-generated code reflected from
733 * the RenderScript script files.
Jason Samsfa445b92011-01-07 17:00:07 -0800734 *
735 * @param xoff
736 * @param component_number
737 * @param fp
738 */
Jason Sams21b41032011-01-16 15:05:41 -0800739 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800740 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700741 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800742 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700743 }
744 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800745 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700746 }
747
748 final byte[] data = fp.getData();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700749 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800750 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700751
752 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800753 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700754 " does not match component size " + eSize + ".");
755 }
756
Jason Sams48fe5342011-07-08 13:52:30 -0700757 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Jason Samsba862d12011-07-07 15:24:42 -0700758 component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700759 }
760
Jason Sams768bc022009-09-21 19:41:04 -0700761 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800762 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700763 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800764 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700765 }
766 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800767 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700768 }
Jason Samsba862d12011-07-07 15:24:42 -0700769 if((off + count) > mCurrentCount) {
770 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700771 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700772 }
Jason Samsba862d12011-07-07 15:24:42 -0700773 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800774 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700775 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700776 }
777
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700778 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700779 * Generate a mipmap chain. This is only valid if the Type of the Allocation
780 * includes mipmaps.
Jason Samsf7086092011-01-12 13:28:37 -0800781 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700782 * <p>This function will generate a complete set of mipmaps from the top
783 * level LOD and place them into the script memory space.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800784 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700785 * <p>If the Allocation is also using other memory spaces, a call to {@link
786 * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
Jason Samsf7086092011-01-12 13:28:37 -0800787 */
788 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700789 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800790 }
791
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700792 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700793 * Copy an array into part of this Allocation. This method does not
794 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800795 *
796 * @param off The offset of the first element to be copied.
797 * @param count The number of elements to be copied.
798 * @param d the source data array
799 */
800 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700801 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700802 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700803 data1DChecks(off, count, d.length * 4, dataSize);
Jason Samse729a942013-11-06 11:22:02 -0800804 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize, Element.DataType.SIGNED_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700805 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams768bc022009-09-21 19:41:04 -0700806 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700807
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700808 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700809 * Copy an array into part of this Allocation. This method does not
810 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800811 *
812 * @param off The offset of the first element to be copied.
813 * @param count The number of elements to be copied.
814 * @param d the source data array
815 */
816 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700817 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700818 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700819 data1DChecks(off, count, d.length * 2, dataSize);
Jason Samse729a942013-11-06 11:22:02 -0800820 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize, Element.DataType.SIGNED_16);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700821 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams768bc022009-09-21 19:41:04 -0700822 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700823
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700824 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700825 * Copy an array into part of this Allocation. This method does not
826 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800827 *
828 * @param off The offset of the first element to be copied.
829 * @param count The number of elements to be copied.
830 * @param d the source data array
831 */
832 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700833 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700834 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700835 data1DChecks(off, count, d.length, dataSize);
Jason Samse729a942013-11-06 11:22:02 -0800836 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize, Element.DataType.SIGNED_8);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700837 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams768bc022009-09-21 19:41:04 -0700838 }
Tim Murray6d7a53c2013-05-23 16:59:23 -0700839
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700840 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700841 * Copy an array into part of this Allocation. This method does not
842 * guarantee that the Allocation is compatible with the input buffer.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800843 *
844 * @param off The offset of the first element to be copied.
845 * @param count The number of elements to be copied.
846 * @param d the source data array
847 */
848 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700849 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700850 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700851 data1DChecks(off, count, d.length * 4, dataSize);
Jason Samse729a942013-11-06 11:22:02 -0800852 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize, Element.DataType.FLOAT_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700853 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -0700854 }
855
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700856 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700857 * Copy an array into part of this Allocation. This variant is type checked
858 * and will generate exceptions if the Allocation type is not a 32 bit
859 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800860 *
861 * @param off The offset of the first element to be copied.
862 * @param count The number of elements to be copied.
863 * @param d the source data array
864 */
Jason Samsb97b2512011-01-16 15:04:08 -0800865 public void copy1DRangeFrom(int off, int count, int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700866 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800867 validateIsInt32();
868 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700869 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800870 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800871
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700872 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700873 * Copy an array into part of this Allocation. This variant is type checked
874 * and will generate exceptions if the Allocation type is not a 16 bit
875 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800876 *
877 * @param off The offset of the first element to be copied.
878 * @param count The number of elements to be copied.
879 * @param d the source data array
880 */
Jason Samsb97b2512011-01-16 15:04:08 -0800881 public void copy1DRangeFrom(int off, int count, short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700882 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800883 validateIsInt16();
884 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700885 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800886 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800887
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700888 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700889 * Copy an array into part of this Allocation. This variant is type checked
890 * and will generate exceptions if the Allocation type is not an 8 bit
891 * integer type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800892 *
893 * @param off The offset of the first element to be copied.
894 * @param count The number of elements to be copied.
895 * @param d the source data array
896 */
Jason Samsb97b2512011-01-16 15:04:08 -0800897 public void copy1DRangeFrom(int off, int count, byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700898 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800899 validateIsInt8();
900 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700901 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800902 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800903
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700904 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700905 * Copy an array into part of this Allocation. This variant is type checked
906 * and will generate exceptions if the Allocation type is not a 32 bit float
907 * type.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800908 *
909 * @param off The offset of the first element to be copied.
910 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700911 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800912 */
Jason Samsb97b2512011-01-16 15:04:08 -0800913 public void copy1DRangeFrom(int off, int count, float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700914 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Samsb97b2512011-01-16 15:04:08 -0800915 validateIsFloat32();
916 copy1DRangeFromUnchecked(off, count, d);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700917 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb97b2512011-01-16 15:04:08 -0800918 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700919 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700920 * Copy part of an Allocation into this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700921 *
922 * @param off The offset of the first element to be copied.
923 * @param count The number of elements to be copied.
924 * @param data the source data allocation.
925 * @param dataOff off The offset of the first element in data to
926 * be copied.
927 */
928 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700929 Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
Jason Sams48fe5342011-07-08 13:52:30 -0700930 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700931 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700932 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700933 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700934 }
935
Jason Samsfb9f82c2011-01-12 14:53:25 -0800936 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -0700937 if (mAdaptedAllocation != null) {
938
939 } else {
940
941 if (xoff < 0 || yoff < 0) {
942 throw new RSIllegalArgumentException("Offset cannot be negative.");
943 }
944 if (h < 0 || w < 0) {
945 throw new RSIllegalArgumentException("Height or width cannot be negative.");
946 }
947 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
948 throw new RSIllegalArgumentException("Updated region larger than allocation.");
949 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800950 }
951 }
Jason Sams768bc022009-09-21 19:41:04 -0700952
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800953 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, byte[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -0700954 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800955 mRS.validate();
956 validate2DRange(xoff, yoff, w, h);
957 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samse729a942013-11-06 11:22:02 -0800958 w, h, data, data.length, Element.DataType.SIGNED_8);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700959 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800960 }
961
962 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, short[] 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,
Jason Samse729a942013-11-06 11:22:02 -0800967 w, h, data, data.length * 2, Element.DataType.SIGNED_16);
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, int[] 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,
Jason Samse729a942013-11-06 11:22:02 -0800976 w, h, data, data.length * 4, Element.DataType.SIGNED_32);
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, float[] 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,
Jason Samse729a942013-11-06 11:22:02 -0800985 w, h, data, data.length * 4, Element.DataType.FLOAT_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -0700986 Trace.traceEnd(RenderScript.TRACE_TAG);
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800987 }
988
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700989 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700990 * Copy from an array into a rectangular region in this Allocation. The
991 * array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -0800992 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700993 * @param xoff X offset of the region to update in this Allocation
994 * @param yoff Y offset of the region to update in this Allocation
995 * @param w Width of the region to update
996 * @param h Height of the region to update
997 * @param data to be placed into the Allocation
Jason Samsf7086092011-01-12 13:28:37 -0800998 */
999 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001000 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001001 validateIsInt8();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001002 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001003 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001004 }
1005
Tim Murrayc11e25c2013-04-09 11:01:01 -07001006 /**
1007 * Copy from an array into a rectangular region in this Allocation. The
1008 * array is assumed to be tightly packed.
1009 *
1010 * @param xoff X offset of the region to update in this Allocation
1011 * @param yoff Y offset of the region to update in this Allocation
1012 * @param w Width of the region to update
1013 * @param h Height of the region to update
1014 * @param data to be placed into the Allocation
1015 */
Jason Samsf7086092011-01-12 13:28:37 -08001016 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001017 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001018 validateIsInt16();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001019 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001020 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001021 }
1022
Tim Murrayc11e25c2013-04-09 11:01:01 -07001023 /**
1024 * Copy from an array into a rectangular region in this Allocation. The
1025 * array is assumed to be tightly packed.
1026 *
1027 * @param xoff X offset of the region to update in this Allocation
1028 * @param yoff Y offset of the region to update in this Allocation
1029 * @param w Width of the region to update
1030 * @param h Height of the region to update
1031 * @param data to be placed into the Allocation
1032 */
Jason Samsf7086092011-01-12 13:28:37 -08001033 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001034 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001035 validateIsInt32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001036 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001037 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001038 }
1039
Tim Murrayc11e25c2013-04-09 11:01:01 -07001040 /**
1041 * Copy from an array into a rectangular region in this Allocation. The
1042 * array is assumed to be tightly packed.
1043 *
1044 * @param xoff X offset of the region to update in this Allocation
1045 * @param yoff Y offset of the region to update in this Allocation
1046 * @param w Width of the region to update
1047 * @param h Height of the region to update
1048 * @param data to be placed into the Allocation
1049 */
Jason Samsf7086092011-01-12 13:28:37 -08001050 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001051 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Stephen Hines5f528be2013-02-08 21:03:51 -08001052 validateIsFloat32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -08001053 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001054 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsb8c5a842009-07-31 20:40:47 -07001055 }
1056
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001057 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001058 * Copy a rectangular region from an Allocation into a rectangular region in
1059 * this Allocation.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001060 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001061 * @param xoff X offset of the region in this Allocation
1062 * @param yoff Y offset of the region in this Allocation
1063 * @param w Width of the region to update.
1064 * @param h Height of the region to update.
1065 * @param data source Allocation.
1066 * @param dataXoff X offset in source Allocation
1067 * @param dataYoff Y offset in source Allocation
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001068 */
1069 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1070 Allocation data, int dataXoff, int dataYoff) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001071 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001072 mRS.validate();
1073 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -07001074 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -07001075 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -07001076 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -07001077 data.mSelectedLOD, data.mSelectedFace.mID);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001078 Trace.traceEnd(RenderScript.TRACE_TAG);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001079 }
1080
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001081 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001082 * Copy a {@link android.graphics.Bitmap} into an Allocation. The height
1083 * and width of the update will use the height and width of the {@link
1084 * android.graphics.Bitmap}.
Jason Samsf7086092011-01-12 13:28:37 -08001085 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001086 * @param xoff X offset of the region to update in this Allocation
1087 * @param yoff Y offset of the region to update in this Allocation
1088 * @param data the Bitmap to be copied
Jason Samsf7086092011-01-12 13:28:37 -08001089 */
1090 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001091 Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
Jason Samsfa445b92011-01-07 17:00:07 -08001092 mRS.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001093 if (data.getConfig() == null) {
1094 Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1095 Canvas c = new Canvas(newBitmap);
1096 c.drawBitmap(data, 0, 0, null);
1097 copy2DRangeFrom(xoff, yoff, newBitmap);
Jason Samsb05d6892013-04-09 15:59:24 -07001098 return;
Tim Murrayabd5db92013-02-28 11:45:22 -08001099 }
Jason Samsfb9f82c2011-01-12 14:53:25 -08001100 validateBitmapFormat(data);
1101 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -07001102 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001103 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001104 }
1105
Jason Samsb05d6892013-04-09 15:59:24 -07001106 private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1107 if (mAdaptedAllocation != null) {
1108
1109 } else {
1110
1111 if (xoff < 0 || yoff < 0 || zoff < 0) {
1112 throw new RSIllegalArgumentException("Offset cannot be negative.");
1113 }
1114 if (h < 0 || w < 0 || d < 0) {
1115 throw new RSIllegalArgumentException("Height or width cannot be negative.");
1116 }
1117 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1118 throw new RSIllegalArgumentException("Updated region larger than allocation.");
1119 }
1120 }
1121 }
1122
1123 /**
1124 * @hide
1125 *
1126 */
1127 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1128 mRS.validate();
1129 validate3DRange(xoff, yoff, zoff, w, h, d);
1130 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Jason Samse729a942013-11-06 11:22:02 -08001131 w, h, d, data, data.length, Element.DataType.SIGNED_8);
Jason Samsb05d6892013-04-09 15:59:24 -07001132 }
1133
1134 /**
1135 * @hide
1136 *
1137 */
1138 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1139 mRS.validate();
1140 validate3DRange(xoff, yoff, zoff, w, h, d);
1141 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Jason Samse729a942013-11-06 11:22:02 -08001142 w, h, d, data, data.length * 2, Element.DataType.SIGNED_16);
Jason Samsb05d6892013-04-09 15:59:24 -07001143 }
1144
1145 /**
1146 * @hide
1147 *
1148 */
1149 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
1150 mRS.validate();
1151 validate3DRange(xoff, yoff, zoff, w, h, d);
1152 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Jason Samse729a942013-11-06 11:22:02 -08001153 w, h, d, data, data.length * 4, Element.DataType.SIGNED_32);
Jason Samsb05d6892013-04-09 15:59:24 -07001154 }
1155
1156 /**
1157 * @hide
1158 *
1159 */
1160 void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
1161 mRS.validate();
1162 validate3DRange(xoff, yoff, zoff, w, h, d);
1163 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
Jason Samse729a942013-11-06 11:22:02 -08001164 w, h, d, data, data.length * 4, Element.DataType.FLOAT_32);
Jason Samsb05d6892013-04-09 15:59:24 -07001165 }
1166
1167
1168 /**
1169 * @hide
1170 * Copy a rectangular region from the array into the allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001171 * The array is assumed to be tightly packed.
Jason Samsb05d6892013-04-09 15:59:24 -07001172 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001173 * @param xoff X offset of the region to update in this Allocation
1174 * @param yoff Y offset of the region to update in this Allocation
1175 * @param zoff Z offset of the region to update in this Allocation
1176 * @param w Width of the region to update
1177 * @param h Height of the region to update
1178 * @param d Depth of the region to update
Jason Samsb05d6892013-04-09 15:59:24 -07001179 * @param data to be placed into the allocation
1180 */
1181 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1182 validateIsInt8();
1183 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1184 }
1185
1186 /**
1187 * @hide
1188 *
1189 */
1190 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1191 validateIsInt16();
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, int[] data) {
1200 validateIsInt32();
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, float[] data) {
1209 validateIsFloat32();
1210 copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1211 }
1212
1213 /**
1214 * @hide
1215 * Copy a rectangular region into the allocation from another
1216 * allocation.
1217 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001218 * @param xoff X offset of the region to update in this Allocation
1219 * @param yoff Y offset of the region to update in this Allocation
1220 * @param zoff Z offset of the region to update in this Allocation
1221 * @param w Width of the region to update.
1222 * @param h Height of the region to update.
1223 * @param d Depth of the region to update.
Jason Samsb05d6892013-04-09 15:59:24 -07001224 * @param data source allocation.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001225 * @param dataXoff X offset of the region in the source Allocation
1226 * @param dataYoff Y offset of the region in the source Allocation
1227 * @param dataZoff Z offset of the region in the source Allocation
Jason Samsb05d6892013-04-09 15:59:24 -07001228 */
1229 public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1230 Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1231 mRS.validate();
1232 validate3DRange(xoff, yoff, zoff, w, h, d);
1233 mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1234 w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1235 data.mSelectedLOD);
1236 }
1237
Jason Samsfa445b92011-01-07 17:00:07 -08001238
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001239 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001240 * Copy from the Allocation into a {@link android.graphics.Bitmap}. The
1241 * bitmap must match the dimensions of the Allocation.
Jason Sams48fe5342011-07-08 13:52:30 -07001242 *
1243 * @param b The bitmap to be set from the Allocation.
1244 */
Jason Samsfa445b92011-01-07 17:00:07 -08001245 public void copyTo(Bitmap b) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001246 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsfb9f82c2011-01-12 14:53:25 -08001247 mRS.validate();
1248 validateBitmapFormat(b);
1249 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -07001250 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001251 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001252 }
1253
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001254 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001255 * Copy from the Allocation into a byte array. The array must be at least
1256 * as large as the Allocation. The allocation must be of an 8 bit integer
1257 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001258 *
1259 * @param d The array to be set from the Allocation.
1260 */
Jason Samsfa445b92011-01-07 17:00:07 -08001261 public void copyTo(byte[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001262 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001263 validateIsInt8();
Jason Sams771bebb2009-12-07 12:40:12 -08001264 mRS.validate();
Jason Sams21659ac2013-11-06 15:08:07 -08001265 mRS.nAllocationRead(getID(mRS), d, Element.DataType.SIGNED_8);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001266 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams40a29e82009-08-10 14:55:26 -07001267 }
1268
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001269 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001270 * Copy from the Allocation into a short array. The array must be at least
1271 * as large as the Allocation. The allocation must be of an 16 bit integer
1272 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001273 *
1274 * @param d The array to be set from the Allocation.
1275 */
Jason Samsfa445b92011-01-07 17:00:07 -08001276 public void copyTo(short[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001277 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001278 validateIsInt16();
Jason Samsfa445b92011-01-07 17:00:07 -08001279 mRS.validate();
Jason Sams21659ac2013-11-06 15:08:07 -08001280 mRS.nAllocationRead(getID(mRS), d, Element.DataType.SIGNED_16);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001281 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001282 }
1283
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001284 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001285 * Copy from the Allocation into a int array. The array must be at least as
1286 * large as the Allocation. The allocation must be of an 32 bit integer
1287 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001288 *
1289 * @param d The array to be set from the Allocation.
1290 */
Jason Samsfa445b92011-01-07 17:00:07 -08001291 public void copyTo(int[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001292 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001293 validateIsInt32();
Jason Samsfa445b92011-01-07 17:00:07 -08001294 mRS.validate();
Jason Sams21659ac2013-11-06 15:08:07 -08001295 mRS.nAllocationRead(getID(mRS), d, Element.DataType.SIGNED_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001296 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Samsfa445b92011-01-07 17:00:07 -08001297 }
1298
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001299 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001300 * Copy from the Allocation into a float array. The array must be at least
1301 * as large as the Allocation. The allocation must be of an 32 bit float
1302 * {@link android.renderscript.Element} type.
Jason Sams48fe5342011-07-08 13:52:30 -07001303 *
1304 * @param d The array to be set from the Allocation.
1305 */
Jason Samsfa445b92011-01-07 17:00:07 -08001306 public void copyTo(float[] d) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001307 Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
Jason Samsb97b2512011-01-16 15:04:08 -08001308 validateIsFloat32();
Jason Sams771bebb2009-12-07 12:40:12 -08001309 mRS.validate();
Jason Sams21659ac2013-11-06 15:08:07 -08001310 mRS.nAllocationRead(getID(mRS), d, Element.DataType.FLOAT_32);
Tim Murray6d7a53c2013-05-23 16:59:23 -07001311 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams40a29e82009-08-10 14:55:26 -07001312 }
1313
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001314 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001315 * Resize a 1D allocation. The contents of the allocation are preserved.
1316 * If new elements are allocated objects are created with null contents and
1317 * the new region is otherwise undefined.
Jason Samsf7086092011-01-12 13:28:37 -08001318 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001319 * <p>If the new region is smaller the references of any objects outside the
1320 * new region will be released.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001321 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001322 * <p>A new type will be created with the new dimension.</p>
Jason Samsf7086092011-01-12 13:28:37 -08001323 *
1324 * @param dimX The new size of the allocation.
Jason Samsb05d6892013-04-09 15:59:24 -07001325 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001326 * @deprecated RenderScript objects should be immutable once created. The
1327 * replacement is to create a new allocation and copy the contents.
Jason Samsf7086092011-01-12 13:28:37 -08001328 */
Jason Sams31a7e422010-10-26 13:09:17 -07001329 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001330 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001331 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001332 }
Jason Samse07694b2012-04-03 15:36:36 -07001333 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001334 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001335
Jason Samse07694b2012-04-03 15:36:36 -07001336 int typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001337 mType = new Type(typeID, mRS);
1338 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001339 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001340 }
1341
Jason Samsb8c5a842009-07-31 20:40:47 -07001342
1343 // creation
1344
Jason Sams49a05d72010-12-29 14:31:29 -08001345 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001346 static {
1347 mBitmapOptions.inScaled = false;
1348 }
1349
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001350 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001351 * Creates a new Allocation with the given {@link
1352 * android.renderscript.Type}, mipmap flag, and usage flags.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001353 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001354 * @param type RenderScript type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001355 * @param mips specifies desired mipmap behaviour for the
1356 * allocation
Tim Murrayc11e25c2013-04-09 11:01:01 -07001357 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001358 * utilized
1359 */
1360 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001361 Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
Jason Sams771bebb2009-12-07 12:40:12 -08001362 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001363 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001364 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001365 }
Jason Samse07694b2012-04-03 15:36:36 -07001366 int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001367 if (id == 0) {
1368 throw new RSRuntimeException("Allocation creation failed.");
1369 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001370 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams857d0c72011-11-23 15:02:15 -08001371 return new Allocation(id, rs, type, usage);
1372 }
1373
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001374 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001375 * Creates an Allocation with the size specified by the type and no mipmaps
1376 * generated by default
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001377 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001378 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001379 * @param type renderscript type describing data layout
1380 * @param usage bit field specifying how the allocation is
1381 * utilized
1382 *
1383 * @return allocation
1384 */
Jason Samse5d37122010-12-16 00:33:33 -08001385 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1386 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1387 }
1388
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001389 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001390 * Creates an Allocation for use by scripts with a given {@link
1391 * android.renderscript.Type} and no mipmaps
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001392 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001393 * @param rs Context to which the Allocation will belong.
1394 * @param type RenderScript Type describing data layout
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001395 *
1396 * @return allocation
1397 */
Jason Sams5476b452010-12-08 16:14:36 -08001398 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001399 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001400 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001401
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001402 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001403 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001404 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001405 * @param rs Context to which the Allocation will belong.
1406 * @param e Element to use in the Allocation
1407 * @param count the number of Elements in the Allocation
1408 * @param usage bit field specifying how the Allocation is
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001409 * utilized
1410 *
1411 * @return allocation
1412 */
Jason Sams5476b452010-12-08 16:14:36 -08001413 static public Allocation createSized(RenderScript rs, Element e,
1414 int count, int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001415 Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
Jason Sams771bebb2009-12-07 12:40:12 -08001416 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001417 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001418 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001419 Type t = b.create();
1420
Jason Samse07694b2012-04-03 15:36:36 -07001421 int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001422 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001423 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001424 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001425 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001426 return new Allocation(id, rs, t, usage);
1427 }
1428
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001429 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001430 * Creates an Allocation with a specified number of given elements
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001431 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001432 * @param rs Context to which the Allocation will belong.
1433 * @param e Element to use in the Allocation
1434 * @param count the number of Elements in the Allocation
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001435 *
1436 * @return allocation
1437 */
Jason Sams5476b452010-12-08 16:14:36 -08001438 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001439 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001440 }
1441
Jason Sams49a05d72010-12-29 14:31:29 -08001442 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001443 final Bitmap.Config bc = b.getConfig();
1444 if (bc == Bitmap.Config.ALPHA_8) {
1445 return Element.A_8(rs);
1446 }
1447 if (bc == Bitmap.Config.ARGB_4444) {
1448 return Element.RGBA_4444(rs);
1449 }
1450 if (bc == Bitmap.Config.ARGB_8888) {
1451 return Element.RGBA_8888(rs);
1452 }
1453 if (bc == Bitmap.Config.RGB_565) {
1454 return Element.RGB_565(rs);
1455 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001456 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001457 }
1458
Jason Sams49a05d72010-12-29 14:31:29 -08001459 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001460 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001461 Element e = elementFromBitmap(rs, b);
1462 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001463 tb.setX(b.getWidth());
1464 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001465 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001466 return tb.create();
1467 }
1468
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001469 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001470 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001471 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001472 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001473 * @param b Bitmap source for the allocation data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001474 * @param mips specifies desired mipmap behaviour for the
1475 * allocation
1476 * @param usage bit field specifying how the allocation is
1477 * utilized
1478 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001479 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001480 *
1481 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001482 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001483 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001484 int usage) {
Tim Murray6d7a53c2013-05-23 16:59:23 -07001485 Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
Jason Sams771bebb2009-12-07 12:40:12 -08001486 rs.validate();
Tim Murrayabd5db92013-02-28 11:45:22 -08001487
1488 // WAR undocumented color formats
1489 if (b.getConfig() == null) {
1490 if ((usage & USAGE_SHARED) != 0) {
1491 throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1492 }
1493 Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1494 Canvas c = new Canvas(newBitmap);
1495 c.drawBitmap(b, 0, 0, null);
1496 return createFromBitmap(rs, newBitmap, mips, usage);
1497 }
1498
Jason Sams5476b452010-12-08 16:14:36 -08001499 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001500
Tim Murraya3145512012-12-04 17:59:29 -08001501 // enable optimized bitmap path only with no mipmap and script-only usage
1502 if (mips == MipmapControl.MIPMAP_NONE &&
1503 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Tim Murray78e64942013-04-09 17:28:56 -07001504 usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
Tim Murraya3145512012-12-04 17:59:29 -08001505 int id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
1506 if (id == 0) {
1507 throw new RSRuntimeException("Load failed.");
1508 }
1509
1510 // keep a reference to the Bitmap around to prevent GC
1511 Allocation alloc = new Allocation(id, rs, t, usage);
1512 alloc.setBitmap(b);
1513 return alloc;
1514 }
1515
Jason Sams9bf18922013-04-13 19:48:36 -07001516
Jason Samse07694b2012-04-03 15:36:36 -07001517 int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001518 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001519 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001520 }
Tim Murray6d7a53c2013-05-23 16:59:23 -07001521 Trace.traceEnd(RenderScript.TRACE_TAG);
Jason Sams5476b452010-12-08 16:14:36 -08001522 return new Allocation(id, rs, t, usage);
1523 }
1524
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001525 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001526 * Returns the handle to a raw buffer that is being managed by the screen
1527 * compositor. This operation is only valid for Allocations with {@link
1528 * #USAGE_IO_INPUT}.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001529 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001530 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001531 *
1532 */
1533 public Surface getSurface() {
Jason Sams72226e02013-02-22 12:45:54 -08001534 if ((mUsage & USAGE_IO_INPUT) == 0) {
1535 throw new RSInvalidStateException("Allocation is not a surface texture.");
1536 }
1537 return mRS.nAllocationGetSurface(getID(mRS));
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001538 }
1539
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001540 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001541 * Associate a {@link android.view.Surface} with this Allocation. This
1542 * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001543 *
1544 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001545 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001546 public void setSurface(Surface sur) {
1547 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001548 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1549 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1550 }
1551
Jason Samse07694b2012-04-03 15:36:36 -07001552 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001553 }
1554
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001555 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001556 * Creates an Allocation from a {@link android.graphics.Bitmap}.
Tim Murray00bb4542012-12-17 16:35:06 -08001557 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001558 * <p>With target API version 18 or greater, this Allocation will be created
1559 * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
1560 * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
1561 * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001562 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001563 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001564 * @param b bitmap source for the allocation data
1565 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001566 * @return Allocation containing bitmap data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001567 *
1568 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001569 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08001570 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1571 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Tim Murray78e64942013-04-09 17:28:56 -07001572 USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Tim Murray00bb4542012-12-17 16:35:06 -08001573 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001574 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1575 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001576 }
1577
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001578 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001579 * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1580 * containing the horizontal list of cube faces. Each face must be a square,
1581 * have the same size as all other faces, and have a width that is a power
1582 * of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001583 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001584 * @param rs Context to which the allocation will belong.
Tim Murrayc11e25c2013-04-09 11:01:01 -07001585 * @param b Bitmap with cubemap faces layed out in the following
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001586 * format: right, left, top, bottom, front, back
1587 * @param mips specifies desired mipmap behaviour for the cubemap
1588 * @param usage bit field specifying how the cubemap is utilized
1589 *
1590 * @return allocation containing cubemap data
1591 *
1592 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001593 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001594 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001595 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001596 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001597
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001598 int height = b.getHeight();
1599 int width = b.getWidth();
1600
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001601 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001602 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1603 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001604 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001605 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001606 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001607 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001608 if (!isPow2) {
1609 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1610 }
1611
1612 Element e = elementFromBitmap(rs, b);
1613 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001614 tb.setX(height);
1615 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001616 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001617 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001618 Type t = tb.create();
1619
Jason Samse07694b2012-04-03 15:36:36 -07001620 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001621 if(id == 0) {
1622 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1623 }
Jason Sams5476b452010-12-08 16:14:36 -08001624 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001625 }
1626
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001627 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001628 * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
1629 * from a {@link android.graphics.Bitmap} containing the horizontal list of
1630 * cube faces. Each face must be a square, have the same size as all other
1631 * faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001632 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001633 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001634 * @param b bitmap with cubemap faces layed out in the following
1635 * format: right, left, top, bottom, front, back
1636 *
1637 * @return allocation containing cubemap data
1638 *
1639 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001640 static public Allocation createCubemapFromBitmap(RenderScript rs,
1641 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001642 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001643 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001644 }
1645
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001646 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001647 * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
1648 * objects containing the cube faces. Each face must be a square, have the
1649 * same size as all other faces, and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001650 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001651 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001652 * @param xpos cubemap face in the positive x direction
1653 * @param xneg cubemap face in the negative x direction
1654 * @param ypos cubemap face in the positive y direction
1655 * @param yneg cubemap face in the negative y direction
1656 * @param zpos cubemap face in the positive z direction
1657 * @param zneg cubemap face in the negative z direction
1658 * @param mips specifies desired mipmap behaviour for the cubemap
1659 * @param usage bit field specifying how the cubemap is utilized
1660 *
1661 * @return allocation containing cubemap data
1662 *
1663 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001664 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1665 Bitmap xpos,
1666 Bitmap xneg,
1667 Bitmap ypos,
1668 Bitmap yneg,
1669 Bitmap zpos,
1670 Bitmap zneg,
1671 MipmapControl mips,
1672 int usage) {
1673 int height = xpos.getHeight();
1674 if (xpos.getWidth() != height ||
1675 xneg.getWidth() != height || xneg.getHeight() != height ||
1676 ypos.getWidth() != height || ypos.getHeight() != height ||
1677 yneg.getWidth() != height || yneg.getHeight() != height ||
1678 zpos.getWidth() != height || zpos.getHeight() != height ||
1679 zneg.getWidth() != height || zneg.getHeight() != height) {
1680 throw new RSIllegalArgumentException("Only square cube map faces supported");
1681 }
1682 boolean isPow2 = (height & (height - 1)) == 0;
1683 if (!isPow2) {
1684 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1685 }
1686
1687 Element e = elementFromBitmap(rs, xpos);
1688 Type.Builder tb = new Type.Builder(rs, e);
1689 tb.setX(height);
1690 tb.setY(height);
1691 tb.setFaces(true);
1692 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1693 Type t = tb.create();
1694 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1695
1696 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001697 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001698 adapter.copyFrom(xpos);
1699 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1700 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001701 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001702 adapter.copyFrom(ypos);
1703 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1704 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001705 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001706 adapter.copyFrom(zpos);
1707 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1708 adapter.copyFrom(zneg);
1709
1710 return cubemap;
1711 }
1712
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001713 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001714 * Creates a non-mipmapped cubemap Allocation for use as a sampler input
1715 * from 6 {@link android.graphics.Bitmap} objects containing the cube
1716 * faces. Each face must be a square, have the same size as all other faces,
1717 * and have a width that is a power of 2.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001718 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001719 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001720 * @param xpos cubemap face in the positive x direction
1721 * @param xneg cubemap face in the negative x direction
1722 * @param ypos cubemap face in the positive y direction
1723 * @param yneg cubemap face in the negative y direction
1724 * @param zpos cubemap face in the positive z direction
1725 * @param zneg cubemap face in the negative z direction
1726 *
1727 * @return allocation containing cubemap data
1728 *
1729 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001730 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1731 Bitmap xpos,
1732 Bitmap xneg,
1733 Bitmap ypos,
1734 Bitmap yneg,
1735 Bitmap zpos,
1736 Bitmap zneg) {
1737 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1738 zpos, zneg, MipmapControl.MIPMAP_NONE,
1739 USAGE_GRAPHICS_TEXTURE);
1740 }
1741
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001742 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001743 * Creates an Allocation from the Bitmap referenced
1744 * by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001745 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001746 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001747 * @param res application resources
1748 * @param id resource id to load the data from
1749 * @param mips specifies desired mipmap behaviour for the
1750 * allocation
1751 * @param usage bit field specifying how the allocation is
1752 * utilized
1753 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001754 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001755 *
1756 */
Jason Sams5476b452010-12-08 16:14:36 -08001757 static public Allocation createFromBitmapResource(RenderScript rs,
1758 Resources res,
1759 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001760 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001761 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001762
Jason Sams771bebb2009-12-07 12:40:12 -08001763 rs.validate();
Jason Sams3ece2f32013-05-31 14:00:46 -07001764 if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
1765 throw new RSIllegalArgumentException("Unsupported usage specified.");
1766 }
Jason Sams5476b452010-12-08 16:14:36 -08001767 Bitmap b = BitmapFactory.decodeResource(res, id);
1768 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1769 b.recycle();
1770 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001771 }
1772
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001773 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001774 * Creates a non-mipmapped Allocation to use as a graphics texture from the
1775 * {@link android.graphics.Bitmap} referenced by resource ID.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001776 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001777 * <p>With target API version 18 or greater, this allocation will be created
1778 * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
1779 * target API version 17 or lower, this allocation will be created with
1780 * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
Jason Sams455d6442013-02-05 19:20:18 -08001781 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001782 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001783 * @param res application resources
1784 * @param id resource id to load the data from
1785 *
Tim Murrayc11e25c2013-04-09 11:01:01 -07001786 * @return Allocation containing resource data
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001787 *
1788 */
Jason Sams5476b452010-12-08 16:14:36 -08001789 static public Allocation createFromBitmapResource(RenderScript rs,
1790 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001791 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08001792 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1793 return createFromBitmapResource(rs, res, id,
1794 MipmapControl.MIPMAP_NONE,
Jason Sams3ece2f32013-05-31 14:00:46 -07001795 USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
Jason Sams455d6442013-02-05 19:20:18 -08001796 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001797 return createFromBitmapResource(rs, res, id,
1798 MipmapControl.MIPMAP_NONE,
1799 USAGE_GRAPHICS_TEXTURE);
1800 }
1801
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001802 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001803 * Creates an Allocation containing string data encoded in UTF-8 format.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001804 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001805 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001806 * @param str string to create the allocation from
1807 * @param usage bit field specifying how the allocaiton is
1808 * utilized
1809 *
1810 */
Jason Sams5476b452010-12-08 16:14:36 -08001811 static public Allocation createFromString(RenderScript rs,
1812 String str,
1813 int usage) {
1814 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001815 byte[] allocArray = null;
1816 try {
1817 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001818 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001819 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001820 return alloc;
1821 }
1822 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001823 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001824 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001825 }
Jason Sams739c8262013-04-11 18:07:52 -07001826
1827 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001828 * Interface to handle notification when new buffers are available via
1829 * {@link #USAGE_IO_INPUT}. An application will receive one notification
1830 * when a buffer is available. Additional buffers will not trigger new
1831 * notifications until a buffer is processed.
Jason Sams739c8262013-04-11 18:07:52 -07001832 */
Jason Sams42ef2382013-08-29 13:30:59 -07001833 public interface OnBufferAvailableListener {
Jason Sams739c8262013-04-11 18:07:52 -07001834 public void onBufferAvailable(Allocation a);
1835 }
1836
1837 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001838 * Set a notification handler for {@link #USAGE_IO_INPUT}.
Jason Sams739c8262013-04-11 18:07:52 -07001839 *
Jason Sams42ef2382013-08-29 13:30:59 -07001840 * @param callback instance of the OnBufferAvailableListener
1841 * class to be called when buffer arrive.
Jason Sams739c8262013-04-11 18:07:52 -07001842 */
Jason Sams42ef2382013-08-29 13:30:59 -07001843 public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
Jason Sams739c8262013-04-11 18:07:52 -07001844 synchronized(mAllocationMap) {
1845 mAllocationMap.put(new Integer(getID(mRS)), this);
1846 mBufferNotifier = callback;
1847 }
1848 }
1849
1850 static void sendBufferNotification(int id) {
1851 synchronized(mAllocationMap) {
1852 Allocation a = mAllocationMap.get(new Integer(id));
1853
1854 if ((a != null) && (a.mBufferNotifier != null)) {
1855 a.mBufferNotifier.onBufferAvailable(a);
1856 }
1857 }
1858 }
1859
Jason Samsb8c5a842009-07-31 20:40:47 -07001860}
1861