blob: 1c83c51e377df34a7d5002d87decdab12819bf7a [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 Samsb8c5a842009-07-31 20:40:47 -070021import android.content.res.Resources;
Romain Guy650a3eb2009-08-31 14:06:43 -070022import android.content.res.AssetManager;
Jason Samsb8c5a842009-07-31 20:40:47 -070023import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
Jason Samsfb9aa9f2012-03-28 15:30:07 -070025import android.view.Surface;
Jason Sams615e7ce2012-01-13 14:01:20 -080026import android.graphics.SurfaceTexture;
Jason Samsb8c5a842009-07-31 20:40:47 -070027import android.util.Log;
Romain Guy650a3eb2009-08-31 14:06:43 -070028import android.util.TypedValue;
Jason Samsb8c5a842009-07-31 20:40:47 -070029
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070030/**
Robert Ly11518ac2011-02-09 13:57:06 -080031 * <p>
32 * Memory allocation class for renderscript. An allocation combines a
33 * {@link android.renderscript.Type} with the memory to provide storage for user data and objects.
34 * This implies that all memory in Renderscript is typed.
35 * </p>
Jason Samsa23d4e72011-01-04 18:59:12 -080036 *
Robert Ly11518ac2011-02-09 13:57:06 -080037 * <p>Allocations are the primary way data moves into and out of scripts. Memory is user
38 * synchronized and it's possible for allocations to exist in multiple memory spaces
39 * concurrently. Currently those spaces are:</p>
40 * <ul>
41 * <li>Script: accessable by RS scripts.</li>
42 * <li>Graphics Texture: accessable as a graphics texture.</li>
43 * <li>Graphics Vertex: accessable as graphical vertex data.</li>
44 * <li>Graphics Constants: Accessable as constants in user shaders</li>
45 * </ul>
46 * </p>
47 * <p>
48 * For example, when creating a allocation for a texture, the user can
49 * specify its memory spaces as both script and textures. This means that it can both
50 * be used as script binding and as a GPU texture for rendering. To maintain
51 * synchronization if a script modifies an allocation used by other targets it must
52 * call a synchronizing function to push the updates to the memory, otherwise the results
53 * are undefined.
54 * </p>
55 * <p>By default, Android system side updates are always applied to the script accessable
56 * memory. If this is not present, they are then applied to the various HW
57 * memory types. A {@link android.renderscript.Allocation#syncAll syncAll()}
58 * call is necessary after the script data is updated to
59 * keep the other memory spaces in sync.</p>
Jason Samsa23d4e72011-01-04 18:59:12 -080060 *
Robert Ly11518ac2011-02-09 13:57:06 -080061 * <p>Allocation data is uploaded in one of two primary ways. For simple
62 * arrays there are copyFrom() functions that take an array from the control code and
63 * copy it to the slave memory store. Both type checked and unchecked copies are provided.
64 * The unchecked variants exist to allow apps to copy over arrays of structures from a
65 * control language that does not support structures.</p>
Jason Samsb8c5a842009-07-31 20:40:47 -070066 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080067 * <div class="special reference">
68 * <h3>Developer Guides</h3>
69 * <p>For more information about creating an application that uses Renderscript, read the
Scott Mainb47fa162013-02-05 14:23:13 -080070 * <a href="{@docRoot}guide/topics/renderscript/index.html">Renderscript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080071 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070072 **/
73public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070074 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080075 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080076 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070077 Allocation mAdaptedAllocation;
78
79 boolean mConstrainedLOD;
80 boolean mConstrainedFace;
81 boolean mConstrainedY;
82 boolean mConstrainedZ;
Jason Sams615e7ce2012-01-13 14:01:20 -080083 boolean mReadAllowed = true;
84 boolean mWriteAllowed = true;
Jason Samsba862d12011-07-07 15:24:42 -070085 int mSelectedY;
86 int mSelectedZ;
87 int mSelectedLOD;
88 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
89
90 int mCurrentDimX;
91 int mCurrentDimY;
92 int mCurrentDimZ;
93 int mCurrentCount;
94
Jason Sams5476b452010-12-08 16:14:36 -080095
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070096 /**
Jason Samsf7086092011-01-12 13:28:37 -080097 * The usage of the allocation. These signal to renderscript
98 * where to place the allocation in memory.
99 *
100 * SCRIPT The allocation will be bound to and accessed by
101 * scripts.
102 */
Jason Sams5476b452010-12-08 16:14:36 -0800103 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800104
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700105 /**
Jason Sams163766c2012-02-15 12:04:24 -0800106 * GRAPHICS_TEXTURE The allocation will be used as a texture
Stephen Hines836c4a52011-06-01 14:38:10 -0700107 * source by one or more graphics programs.
Jason Samsf7086092011-01-12 13:28:37 -0800108 *
109 */
Jason Sams5476b452010-12-08 16:14:36 -0800110 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800111
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700112 /**
Jason Samsf7086092011-01-12 13:28:37 -0800113 * GRAPHICS_VERTEX The allocation will be used as a graphics
114 * mesh.
115 *
116 */
Jason Sams5476b452010-12-08 16:14:36 -0800117 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800118
119
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700120 /**
Jason Samsf7086092011-01-12 13:28:37 -0800121 * GRAPHICS_CONSTANTS The allocation will be used as the source
122 * of shader constants by one or more programs.
123 *
124 */
Jason Sams5476b452010-12-08 16:14:36 -0800125 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
126
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700127 /**
Jason Sams163766c2012-02-15 12:04:24 -0800128 * USAGE_GRAPHICS_RENDER_TARGET The allocation will be used as a
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700129 * target for offscreen rendering
130 *
131 */
132 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
133
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700134 /**
Jason Sams163766c2012-02-15 12:04:24 -0800135 * USAGE_IO_INPUT The allocation will be used as SurfaceTexture
136 * consumer. This usage will cause the allocation to be created
137 * read only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800138 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800139 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700140 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800141
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700142 /**
Jason Sams163766c2012-02-15 12:04:24 -0800143 * USAGE_IO_OUTPUT The allocation will be used as a
144 * SurfaceTexture producer. The dimensions and format of the
145 * SurfaceTexture will be forced to those of the allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800146 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800147 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700148 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700149
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700150 /**
Tim Murray00bb4542012-12-17 16:35:06 -0800151 * USAGE_SHARED The allocation's backing store will be inherited
152 * from another object (usually a Bitmap); calling appropriate
153 * copy methods will be significantly faster than if the entire
154 * allocation were copied every time.
155 *
156 * This is set by default for allocations created with
157 * CreateFromBitmap(RenderScript, Bitmap) in API version 18 and
158 * higher.
159 *
160 */
161 public static final int USAGE_SHARED = 0x0080;
162
163 /**
Jason Samsf7086092011-01-12 13:28:37 -0800164 * Controls mipmap behavior when using the bitmap creation and
165 * update functions.
166 */
Jason Sams4ef66502010-12-10 16:03:15 -0800167 public enum MipmapControl {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700168 /**
Jason Samsf7086092011-01-12 13:28:37 -0800169 * No mipmaps will be generated and the type generated from the
170 * incoming bitmap will not contain additional LODs.
171 */
Jason Sams5476b452010-12-08 16:14:36 -0800172 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800173
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700174 /**
Jason Samsf7086092011-01-12 13:28:37 -0800175 * A Full mipmap chain will be created in script memory. The
176 * type of the allocation will contain a full mipmap chain. On
177 * upload to graphics the full chain will be transfered.
178 */
Jason Sams5476b452010-12-08 16:14:36 -0800179 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800180
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700181 /**
Jason Samsf7086092011-01-12 13:28:37 -0800182 * The type of the allocation will be the same as MIPMAP_NONE.
183 * It will not contain mipmaps. On upload to graphics the
184 * graphics copy of the allocation data will contain a full
185 * mipmap chain generated from the top level in script memory.
186 */
Jason Sams5476b452010-12-08 16:14:36 -0800187 MIPMAP_ON_SYNC_TO_TEXTURE(2);
188
189 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800190 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800191 mID = id;
192 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700193 }
194
Jason Sams48fe5342011-07-08 13:52:30 -0700195
196 private int getIDSafe() {
197 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700198 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700199 }
Jason Samse07694b2012-04-03 15:36:36 -0700200 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700201 }
202
Jason Sams03d2d002012-03-23 13:51:56 -0700203
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700204 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700205 * Get the element of the type of the Allocation.
206 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700207 * @return Element that describes the structure of data in the
208 * allocation
Jason Sams03d2d002012-03-23 13:51:56 -0700209 *
210 */
211 public Element getElement() {
212 return mType.getElement();
213 }
214
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700215 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700216 * Get the usage flags of the Allocation.
217 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700218 * @return usage flags associated with the allocation. e.g.
219 * script, texture, etc.
Jason Sams03d2d002012-03-23 13:51:56 -0700220 *
221 */
222 public int getUsage() {
223 return mUsage;
224 }
225
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700226 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700227 * Get the size of the Allocation in bytes.
228 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700229 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700230 *
231 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700232 public int getBytesSize() {
233 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700234 }
235
Jason Sams452a7662011-07-07 16:05:18 -0700236 private void updateCacheInfo(Type t) {
237 mCurrentDimX = t.getX();
238 mCurrentDimY = t.getY();
239 mCurrentDimZ = t.getZ();
240 mCurrentCount = mCurrentDimX;
241 if (mCurrentDimY > 1) {
242 mCurrentCount *= mCurrentDimY;
243 }
244 if (mCurrentDimZ > 1) {
245 mCurrentCount *= mCurrentDimZ;
246 }
247 }
Jason Samsba862d12011-07-07 15:24:42 -0700248
Tim Murraya3145512012-12-04 17:59:29 -0800249 private void setBitmap(Bitmap b) {
250 mBitmap = b;
251 }
252
Jason Sams5476b452010-12-08 16:14:36 -0800253 Allocation(int id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700254 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800255 if ((usage & ~(USAGE_SCRIPT |
256 USAGE_GRAPHICS_TEXTURE |
257 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700258 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800259 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800260 USAGE_IO_INPUT |
Tim Murray00bb4542012-12-17 16:35:06 -0800261 USAGE_IO_OUTPUT |
262 USAGE_SHARED)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800263 throw new RSIllegalArgumentException("Unknown usage specified.");
264 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800265
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700266 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800267 mWriteAllowed = false;
268
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700269 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800270 USAGE_GRAPHICS_TEXTURE |
271 USAGE_SCRIPT)) != 0) {
272 throw new RSIllegalArgumentException("Invalid usage combination.");
273 }
274 }
275
Jason Sams5476b452010-12-08 16:14:36 -0800276 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800277 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700278
Jason Sams452a7662011-07-07 16:05:18 -0700279 if (t != null) {
280 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700281 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700282 }
283
Jason Samsb97b2512011-01-16 15:04:08 -0800284 private void validateIsInt32() {
285 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
286 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
287 return;
288 }
289 throw new RSIllegalArgumentException(
290 "32 bit integer source does not match allocation type " + mType.mElement.mType);
291 }
292
293 private void validateIsInt16() {
294 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
295 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
296 return;
297 }
298 throw new RSIllegalArgumentException(
299 "16 bit integer source does not match allocation type " + mType.mElement.mType);
300 }
301
302 private void validateIsInt8() {
303 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
304 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
305 return;
306 }
307 throw new RSIllegalArgumentException(
308 "8 bit integer source does not match allocation type " + mType.mElement.mType);
309 }
310
311 private void validateIsFloat32() {
312 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
313 return;
314 }
315 throw new RSIllegalArgumentException(
316 "32 bit float source does not match allocation type " + mType.mElement.mType);
317 }
318
319 private void validateIsObject() {
320 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
321 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
322 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
323 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
324 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
325 (mType.mElement.mType == Element.DataType.RS_MESH) ||
326 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
327 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
328 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
329 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
330 return;
331 }
332 throw new RSIllegalArgumentException(
333 "Object source does not match allocation type " + mType.mElement.mType);
334 }
335
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700336 @Override
337 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800338 super.updateFromNative();
Jason Samse07694b2012-04-03 15:36:36 -0700339 int typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700340 if(typeID != 0) {
341 mType = new Type(typeID, mRS);
342 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700343 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700344 }
345 }
346
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700347 /**
Jason Sams03d2d002012-03-23 13:51:56 -0700348 * Get the type of the Allocation.
349 *
350 * @return Type
351 *
352 */
Jason Samsea87e962010-01-12 12:12:28 -0800353 public Type getType() {
354 return mType;
355 }
356
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700357 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700358 * Propagate changes from one usage of the allocation to the
Jason Sams03d2d002012-03-23 13:51:56 -0700359 * remaining usages of the allocation.
360 *
361 */
Jason Sams5476b452010-12-08 16:14:36 -0800362 public void syncAll(int srcLocation) {
363 switch (srcLocation) {
364 case USAGE_SCRIPT:
365 case USAGE_GRAPHICS_CONSTANTS:
366 case USAGE_GRAPHICS_TEXTURE:
367 case USAGE_GRAPHICS_VERTEX:
368 break;
369 default:
370 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
371 }
372 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700373 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Jason Sams5476b452010-12-08 16:14:36 -0800374 }
375
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700376 /**
Jason Sams163766c2012-02-15 12:04:24 -0800377 * Send a buffer to the output stream. The contents of the
378 * Allocation will be undefined after this operation.
379 *
Jason Sams163766c2012-02-15 12:04:24 -0800380 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700381 public void ioSend() {
Jason Sams163766c2012-02-15 12:04:24 -0800382 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
383 throw new RSIllegalArgumentException(
384 "Can only send buffer if IO_OUTPUT usage specified.");
385 }
386 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700387 mRS.nAllocationIoSend(getID(mRS));
Jason Sams163766c2012-02-15 12:04:24 -0800388 }
389
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700390 /**
Jason Samsc5f519c2012-03-29 17:58:15 -0700391 * Delete once code is updated.
392 * @hide
393 */
394 public void ioSendOutput() {
395 ioSend();
396 }
397
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700398 /**
Jason Sams163766c2012-02-15 12:04:24 -0800399 * Receive the latest input into the Allocation.
400 *
Jason Sams163766c2012-02-15 12:04:24 -0800401 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700402 public void ioReceive() {
Jason Sams163766c2012-02-15 12:04:24 -0800403 if ((mUsage & USAGE_IO_INPUT) == 0) {
404 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700405 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800406 }
407 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700408 mRS.nAllocationIoReceive(getID(mRS));
Jason Sams163766c2012-02-15 12:04:24 -0800409 }
410
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700411 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700412 * Copy an array of RS objects to the allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700413 *
414 * @param d Source array.
415 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800416 public void copyFrom(BaseObj[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800417 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800418 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700419 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800420 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700421 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800422 }
423 int i[] = new int[d.length];
424 for (int ct=0; ct < d.length; ct++) {
Jason Samse07694b2012-04-03 15:36:36 -0700425 i[ct] = d[ct].getID(mRS);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800426 }
Jason Samsba862d12011-07-07 15:24:42 -0700427 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Jason Samsb8c5a842009-07-31 20:40:47 -0700428 }
429
Jason Samsfb9f82c2011-01-12 14:53:25 -0800430 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800431 Bitmap.Config bc = b.getConfig();
432 switch (bc) {
433 case ALPHA_8:
434 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
435 throw new RSIllegalArgumentException("Allocation kind is " +
436 mType.getElement().mKind + ", type " +
437 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700438 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800439 " bytes, passed bitmap was " + bc);
440 }
441 break;
442 case ARGB_8888:
443 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700444 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800445 throw new RSIllegalArgumentException("Allocation kind is " +
446 mType.getElement().mKind + ", type " +
447 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700448 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800449 " bytes, passed bitmap was " + bc);
450 }
451 break;
452 case RGB_565:
453 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700454 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800455 throw new RSIllegalArgumentException("Allocation kind is " +
456 mType.getElement().mKind + ", type " +
457 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700458 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800459 " bytes, passed bitmap was " + bc);
460 }
461 break;
462 case ARGB_4444:
463 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700464 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800465 throw new RSIllegalArgumentException("Allocation kind is " +
466 mType.getElement().mKind + ", type " +
467 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700468 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800469 " bytes, passed bitmap was " + bc);
470 }
471 break;
472
473 }
Jason Sams4ef66502010-12-10 16:03:15 -0800474 }
475
Jason Samsfb9f82c2011-01-12 14:53:25 -0800476 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700477 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800478 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
479 }
480 }
481
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700482 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800483 * Copy an allocation from an array. This variant is not type
484 * checked which allows an application to fill in structured
485 * data from an array.
486 *
487 * @param d the source data array
488 */
489 public void copyFromUnchecked(int[] d) {
490 mRS.validate();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800491 if (mCurrentDimY > 0) {
492 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
493 } else {
494 copy1DRangeFromUnchecked(0, mCurrentCount, d);
495 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800496 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700497 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800498 * Copy an allocation from an array. This variant is not type
499 * checked which allows an application to fill in structured
500 * data from an array.
501 *
502 * @param d the source data array
503 */
504 public void copyFromUnchecked(short[] d) {
505 mRS.validate();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800506 if (mCurrentDimY > 0) {
507 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
508 } else {
509 copy1DRangeFromUnchecked(0, mCurrentCount, d);
510 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800511 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700512 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800513 * Copy an allocation from an array. This variant is not type
514 * checked which allows an application to fill in structured
515 * data from an array.
516 *
517 * @param d the source data array
518 */
519 public void copyFromUnchecked(byte[] d) {
520 mRS.validate();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800521 if (mCurrentDimY > 0) {
522 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
523 } else {
524 copy1DRangeFromUnchecked(0, mCurrentCount, d);
525 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800526 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700527 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800528 * Copy an allocation from an array. This variant is not type
529 * checked which allows an application to fill in structured
530 * data from an array.
531 *
532 * @param d the source data array
533 */
534 public void copyFromUnchecked(float[] d) {
535 mRS.validate();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800536 if (mCurrentDimY > 0) {
537 copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
538 } else {
539 copy1DRangeFromUnchecked(0, mCurrentCount, d);
540 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800541 }
542
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700543 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800544 * Copy an allocation from an array. This variant is type
545 * checked and will generate exceptions if the Allocation type
546 * is not a 32 bit integer type.
547 *
548 * @param d the source data array
549 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800550 public void copyFrom(int[] d) {
551 mRS.validate();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800552 if (mCurrentDimY > 0) {
553 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
554 } else {
555 copy1DRangeFrom(0, mCurrentCount, d);
556 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800557 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800558
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700559 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800560 * Copy an allocation from an array. This variant is type
561 * checked and will generate exceptions if the Allocation type
562 * is not a 16 bit integer type.
563 *
564 * @param d the source data array
565 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800566 public void copyFrom(short[] d) {
567 mRS.validate();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800568 if (mCurrentDimY > 0) {
569 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
570 } else {
571 copy1DRangeFrom(0, mCurrentCount, d);
572 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800573 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800574
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700575 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800576 * Copy an allocation from an array. This variant is type
577 * checked and will generate exceptions if the Allocation type
578 * is not a 8 bit integer type.
579 *
580 * @param d the source data array
581 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800582 public void copyFrom(byte[] d) {
583 mRS.validate();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800584 if (mCurrentDimY > 0) {
585 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
586 } else {
587 copy1DRangeFrom(0, mCurrentCount, d);
588 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800589 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800590
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700591 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800592 * Copy an allocation from an array. This variant is type
593 * checked and will generate exceptions if the Allocation type
594 * is not a 32 bit float type.
595 *
596 * @param d the source data array
597 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800598 public void copyFrom(float[] d) {
599 mRS.validate();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800600 if (mCurrentDimY > 0) {
601 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
602 } else {
603 copy1DRangeFrom(0, mCurrentCount, d);
604 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800605 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800606
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700607 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800608 * Copy an allocation from a bitmap. The height, width, and
609 * format of the bitmap must match the existing allocation.
610 *
611 * @param b the source bitmap
612 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800613 public void copyFrom(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800614 mRS.validate();
615 validateBitmapSize(b);
616 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700617 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700618 }
619
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700620 /**
Tim Murrayf671fb02012-10-03 13:50:05 -0700621 * Copy an allocation from an allocation. The types of both allocations
622 * must be identical.
623 *
624 * @param a the source allocation
625 */
626 public void copyFrom(Allocation a) {
627 mRS.validate();
628 if (!mType.equals(a.getType())) {
629 throw new RSIllegalArgumentException("Types of allocations must match.");
630 }
631 copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
632 }
633
634
635 /**
Jason Samsfa445b92011-01-07 17:00:07 -0800636 * This is only intended to be used by auto-generate code reflected from the
637 * renderscript script files.
638 *
639 * @param xoff
640 * @param fp
641 */
Jason Sams21b41032011-01-16 15:05:41 -0800642 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800643 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700644 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700645 final byte[] data = fp.getData();
646
647 int count = data.length / eSize;
648 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800649 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700650 " not divisible by element size " + eSize + ".");
651 }
Jason Samsba862d12011-07-07 15:24:42 -0700652 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700653 }
654
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700655 /**
Jason Samsfa445b92011-01-07 17:00:07 -0800656 * This is only intended to be used by auto-generate code reflected from the
657 * renderscript script files.
658 *
659 * @param xoff
660 * @param component_number
661 * @param fp
662 */
Jason Sams21b41032011-01-16 15:05:41 -0800663 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800664 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700665 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800666 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700667 }
668 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800669 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700670 }
671
672 final byte[] data = fp.getData();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700673 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800674 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700675
676 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800677 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700678 " does not match component size " + eSize + ".");
679 }
680
Jason Sams48fe5342011-07-08 13:52:30 -0700681 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Jason Samsba862d12011-07-07 15:24:42 -0700682 component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700683 }
684
Jason Sams768bc022009-09-21 19:41:04 -0700685 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800686 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700687 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800688 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700689 }
690 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800691 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700692 }
Jason Samsba862d12011-07-07 15:24:42 -0700693 if((off + count) > mCurrentCount) {
694 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700695 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700696 }
Jason Samsba862d12011-07-07 15:24:42 -0700697 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800698 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700699 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700700 }
701
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700702 /**
Jason Samsf7086092011-01-12 13:28:37 -0800703 * Generate a mipmap chain. Requires the type of the allocation
704 * include mipmaps.
705 *
706 * This function will generate a complete set of mipmaps from
707 * the top level lod and place them into the script memoryspace.
708 *
709 * If the allocation is also using other memory spaces a
710 * followup sync will be required.
711 */
712 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700713 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800714 }
715
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700716 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800717 * Copy part of an allocation from an array. This variant is
718 * not type checked which allows an application to fill in
719 * structured data from an array.
720 *
721 * @param off The offset of the first element to be copied.
722 * @param count The number of elements to be copied.
723 * @param d the source data array
724 */
725 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700726 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700727 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700728 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700729 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700730 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800731 * Copy part of an allocation from an array. This variant is
732 * not type checked which allows an application to fill in
733 * structured data from an array.
734 *
735 * @param off The offset of the first element to be copied.
736 * @param count The number of elements to be copied.
737 * @param d the source data array
738 */
739 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700740 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700741 data1DChecks(off, count, d.length * 2, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700742 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700743 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700744 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800745 * Copy part of an allocation from an array. This variant is
746 * not type checked which allows an application to fill in
747 * structured data from an array.
748 *
749 * @param off The offset of the first element to be copied.
750 * @param count The number of elements to be copied.
751 * @param d the source data array
752 */
753 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700754 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700755 data1DChecks(off, count, d.length, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700756 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700757 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700758 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800759 * Copy part of an allocation from an array. This variant is
760 * not type checked which allows an application to fill in
761 * structured data from an array.
762 *
763 * @param off The offset of the first element to be copied.
764 * @param count The number of elements to be copied.
765 * @param d the source data array
766 */
767 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700768 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700769 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700770 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700771 }
772
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700773 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800774 * Copy part of an allocation from an array. This variant is
775 * type checked and will generate exceptions if the Allocation
776 * type is not a 32 bit integer type.
777 *
778 * @param off The offset of the first element to be copied.
779 * @param count The number of elements to be copied.
780 * @param d the source data array
781 */
Jason Samsb97b2512011-01-16 15:04:08 -0800782 public void copy1DRangeFrom(int off, int count, int[] d) {
783 validateIsInt32();
784 copy1DRangeFromUnchecked(off, count, d);
785 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800786
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700787 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800788 * Copy part of an allocation from an array. This variant is
789 * type checked and will generate exceptions if the Allocation
790 * type is not a 16 bit integer type.
791 *
792 * @param off The offset of the first element to be copied.
793 * @param count The number of elements to be copied.
794 * @param d the source data array
795 */
Jason Samsb97b2512011-01-16 15:04:08 -0800796 public void copy1DRangeFrom(int off, int count, short[] d) {
797 validateIsInt16();
798 copy1DRangeFromUnchecked(off, count, d);
799 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800800
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700801 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800802 * Copy part of an allocation from an array. This variant is
803 * type checked and will generate exceptions if the Allocation
804 * type is not a 8 bit integer type.
805 *
806 * @param off The offset of the first element to be copied.
807 * @param count The number of elements to be copied.
808 * @param d the source data array
809 */
Jason Samsb97b2512011-01-16 15:04:08 -0800810 public void copy1DRangeFrom(int off, int count, byte[] d) {
811 validateIsInt8();
812 copy1DRangeFromUnchecked(off, count, d);
813 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800814
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700815 /**
Jason Sams4fa3eed2011-01-19 15:44:38 -0800816 * Copy part of an allocation from an array. This variant is
817 * type checked and will generate exceptions if the Allocation
818 * type is not a 32 bit float type.
819 *
820 * @param off The offset of the first element to be copied.
821 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700822 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800823 */
Jason Samsb97b2512011-01-16 15:04:08 -0800824 public void copy1DRangeFrom(int off, int count, float[] d) {
825 validateIsFloat32();
826 copy1DRangeFromUnchecked(off, count, d);
827 }
828
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700829 /**
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700830 * Copy part of an allocation from another allocation.
831 *
832 * @param off The offset of the first element to be copied.
833 * @param count The number of elements to be copied.
834 * @param data the source data allocation.
835 * @param dataOff off The offset of the first element in data to
836 * be copied.
837 */
838 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Jason Sams48fe5342011-07-08 13:52:30 -0700839 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700840 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700841 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700842 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700843 }
844
Jason Samsfb9f82c2011-01-12 14:53:25 -0800845 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -0700846 if (mAdaptedAllocation != null) {
847
848 } else {
849
850 if (xoff < 0 || yoff < 0) {
851 throw new RSIllegalArgumentException("Offset cannot be negative.");
852 }
853 if (h < 0 || w < 0) {
854 throw new RSIllegalArgumentException("Height or width cannot be negative.");
855 }
856 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
857 throw new RSIllegalArgumentException("Updated region larger than allocation.");
858 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800859 }
860 }
Jason Sams768bc022009-09-21 19:41:04 -0700861
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800862 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, byte[] data) {
863 mRS.validate();
864 validate2DRange(xoff, yoff, w, h);
865 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
866 w, h, data, data.length);
867 }
868
869 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, short[] data) {
870 mRS.validate();
871 validate2DRange(xoff, yoff, w, h);
872 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
873 w, h, data, data.length * 2);
874 }
875
876 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, int[] data) {
877 mRS.validate();
878 validate2DRange(xoff, yoff, w, h);
879 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
880 w, h, data, data.length * 4);
881 }
882
883 void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, float[] data) {
884 mRS.validate();
885 validate2DRange(xoff, yoff, w, h);
886 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
887 w, h, data, data.length * 4);
888 }
889
890
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700891 /**
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700892 * Copy a rectangular region from the array into the allocation.
893 * The incoming array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -0800894 *
895 * @param xoff X offset of the region to update
896 * @param yoff Y offset of the region to update
897 * @param w Width of the incoming region to update
898 * @param h Height of the incoming region to update
899 * @param data to be placed into the allocation
900 */
901 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -0800902 validateIsInt8();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800903 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Jason Samsfa445b92011-01-07 17:00:07 -0800904 }
905
Jason Samsf7086092011-01-12 13:28:37 -0800906 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -0800907 validateIsInt16();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800908 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Jason Samsfa445b92011-01-07 17:00:07 -0800909 }
910
Jason Samsf7086092011-01-12 13:28:37 -0800911 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -0800912 validateIsInt32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800913 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Jason Samsb8c5a842009-07-31 20:40:47 -0700914 }
915
Jason Samsf7086092011-01-12 13:28:37 -0800916 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Stephen Hines5f528be2013-02-08 21:03:51 -0800917 validateIsFloat32();
Stephen Hinesa9a7b372013-02-08 17:11:31 -0800918 copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
Jason Samsb8c5a842009-07-31 20:40:47 -0700919 }
920
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700921 /**
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700922 * Copy a rectangular region into the allocation from another
923 * allocation.
924 *
925 * @param xoff X offset of the region to update.
926 * @param yoff Y offset of the region to update.
927 * @param w Width of the incoming region to update.
928 * @param h Height of the incoming region to update.
929 * @param data source allocation.
930 * @param dataXoff X offset in data of the region to update.
931 * @param dataYoff Y offset in data of the region to update.
932 */
933 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
934 Allocation data, int dataXoff, int dataYoff) {
935 mRS.validate();
936 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700937 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -0700938 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700939 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -0700940 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700941 }
942
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700943 /**
Jason Samsf7086092011-01-12 13:28:37 -0800944 * Copy a bitmap into an allocation. The height and width of
945 * the update will use the height and width of the incoming
946 * bitmap.
947 *
948 * @param xoff X offset of the region to update
949 * @param yoff Y offset of the region to update
950 * @param data the bitmap to be copied
951 */
952 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800953 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800954 validateBitmapFormat(data);
955 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -0700956 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Jason Samsfa445b92011-01-07 17:00:07 -0800957 }
958
959
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700960 /**
Jason Sams48fe5342011-07-08 13:52:30 -0700961 * Copy from the Allocation into a Bitmap. The bitmap must
962 * match the dimensions of the Allocation.
963 *
964 * @param b The bitmap to be set from the Allocation.
965 */
Jason Samsfa445b92011-01-07 17:00:07 -0800966 public void copyTo(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800967 mRS.validate();
968 validateBitmapFormat(b);
969 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -0700970 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Jason Samsfa445b92011-01-07 17:00:07 -0800971 }
972
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700973 /**
Jason Sams48fe5342011-07-08 13:52:30 -0700974 * Copy from the Allocation into a byte array. The array must
975 * be at least as large as the Allocation. The allocation must
976 * be of an 8 bit elemental type.
977 *
978 * @param d The array to be set from the Allocation.
979 */
Jason Samsfa445b92011-01-07 17:00:07 -0800980 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800981 validateIsInt8();
Jason Sams771bebb2009-12-07 12:40:12 -0800982 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700983 mRS.nAllocationRead(getID(mRS), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700984 }
985
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700986 /**
Jason Sams48fe5342011-07-08 13:52:30 -0700987 * Copy from the Allocation into a short array. The array must
988 * be at least as large as the Allocation. The allocation must
989 * be of an 16 bit elemental type.
990 *
991 * @param d The array to be set from the Allocation.
992 */
Jason Samsfa445b92011-01-07 17:00:07 -0800993 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800994 validateIsInt16();
Jason Samsfa445b92011-01-07 17:00:07 -0800995 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700996 mRS.nAllocationRead(getID(mRS), d);
Jason Samsfa445b92011-01-07 17:00:07 -0800997 }
998
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700999 /**
Jason Sams48fe5342011-07-08 13:52:30 -07001000 * Copy from the Allocation into a int array. The array must be
1001 * at least as large as the Allocation. The allocation must be
1002 * of an 32 bit elemental type.
1003 *
1004 * @param d The array to be set from the Allocation.
1005 */
Jason Samsfa445b92011-01-07 17:00:07 -08001006 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001007 validateIsInt32();
Jason Samsfa445b92011-01-07 17:00:07 -08001008 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001009 mRS.nAllocationRead(getID(mRS), d);
Jason Samsfa445b92011-01-07 17:00:07 -08001010 }
1011
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001012 /**
Jason Sams48fe5342011-07-08 13:52:30 -07001013 * Copy from the Allocation into a float array. The array must
1014 * be at least as large as the Allocation. The allocation must
1015 * be of an 32 bit float elemental type.
1016 *
1017 * @param d The array to be set from the Allocation.
1018 */
Jason Samsfa445b92011-01-07 17:00:07 -08001019 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -08001020 validateIsFloat32();
Jason Sams771bebb2009-12-07 12:40:12 -08001021 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001022 mRS.nAllocationRead(getID(mRS), d);
Jason Sams40a29e82009-08-10 14:55:26 -07001023 }
1024
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001025 /**
Jason Samsf7086092011-01-12 13:28:37 -08001026 * Resize a 1D allocation. The contents of the allocation are
1027 * preserved. If new elements are allocated objects are created
1028 * with null contents and the new region is otherwise undefined.
1029 *
1030 * If the new region is smaller the references of any objects
1031 * outside the new region will be released.
1032 *
1033 * A new type will be created with the new dimension.
1034 *
1035 * @param dimX The new size of the allocation.
1036 */
Jason Sams31a7e422010-10-26 13:09:17 -07001037 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001038 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -08001039 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001040 }
Jason Samse07694b2012-04-03 15:36:36 -07001041 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -07001042 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -07001043
Jason Samse07694b2012-04-03 15:36:36 -07001044 int typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -07001045 mType = new Type(typeID, mRS);
1046 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -07001047 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001048 }
1049
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001050 /**
Jason Sams163766c2012-02-15 12:04:24 -08001051 * Resize a 2D allocation. The contents of the allocation are
1052 * preserved. If new elements are allocated objects are created
1053 * with null contents and the new region is otherwise undefined.
1054 *
1055 * If the new region is smaller the references of any objects
1056 * outside the new region will be released.
1057 *
1058 * A new type will be created with the new dimension.
1059 *
Jason Sams163766c2012-02-15 12:04:24 -08001060 * @param dimX The new size of the allocation.
1061 * @param dimY The new size of the allocation.
1062 */
Tim Murraybc254b92012-10-05 15:00:45 -07001063 public synchronized void resize(int dimX, int dimY) {
Jason Sams163766c2012-02-15 12:04:24 -08001064 if ((mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
1065 throw new RSInvalidStateException(
1066 "Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001067 }
1068 if (mType.getY() == 0) {
Jason Sams163766c2012-02-15 12:04:24 -08001069 throw new RSInvalidStateException(
1070 "Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -07001071 }
Jason Samse07694b2012-04-03 15:36:36 -07001072 mRS.nAllocationResize2D(getID(mRS), dimX, dimY);
Jason Sams163766c2012-02-15 12:04:24 -08001073 mRS.finish(); // Necessary because resize is fifoed and update is async.
1074
Jason Samse07694b2012-04-03 15:36:36 -07001075 int typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams163766c2012-02-15 12:04:24 -08001076 mType = new Type(typeID, mRS);
1077 mType.updateFromNative();
1078 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001079 }
Jason Sams40a29e82009-08-10 14:55:26 -07001080
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001081
Jason Samsb8c5a842009-07-31 20:40:47 -07001082
1083 // creation
1084
Jason Sams49a05d72010-12-29 14:31:29 -08001085 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001086 static {
1087 mBitmapOptions.inScaled = false;
1088 }
1089
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001090 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001091 *
1092 * @param type renderscript type describing data layout
1093 * @param mips specifies desired mipmap behaviour for the
1094 * allocation
1095 * @param usage bit field specifying how the allocation is
1096 * utilized
1097 */
1098 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001099 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001100 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001101 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001102 }
Jason Samse07694b2012-04-03 15:36:36 -07001103 int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001104 if (id == 0) {
1105 throw new RSRuntimeException("Allocation creation failed.");
1106 }
1107 return new Allocation(id, rs, type, usage);
1108 }
1109
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001110 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001111 * Creates a renderscript allocation with the size specified by
1112 * the type and no mipmaps generated by default
1113 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001114 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001115 * @param type renderscript type describing data layout
1116 * @param usage bit field specifying how the allocation is
1117 * utilized
1118 *
1119 * @return allocation
1120 */
Jason Samse5d37122010-12-16 00:33:33 -08001121 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1122 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1123 }
1124
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001125 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001126 * Creates a renderscript allocation for use by the script with
1127 * the size specified by the type and no mipmaps generated by
1128 * default
1129 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001130 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001131 * @param type renderscript type describing data layout
1132 *
1133 * @return allocation
1134 */
Jason Sams5476b452010-12-08 16:14:36 -08001135 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001136 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001137 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001138
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001139 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001140 * Creates a renderscript allocation with a specified number of
1141 * given elements
1142 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001143 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001144 * @param e describes what each element of an allocation is
1145 * @param count specifies the number of element in the allocation
1146 * @param usage bit field specifying how the allocation is
1147 * utilized
1148 *
1149 * @return allocation
1150 */
Jason Sams5476b452010-12-08 16:14:36 -08001151 static public Allocation createSized(RenderScript rs, Element e,
1152 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001153 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001154 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001155 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001156 Type t = b.create();
1157
Jason Samse07694b2012-04-03 15:36:36 -07001158 int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001159 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001160 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001161 }
Jason Sams5476b452010-12-08 16:14:36 -08001162 return new Allocation(id, rs, t, usage);
1163 }
1164
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001165 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001166 * Creates a renderscript allocation with a specified number of
1167 * given elements
1168 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001169 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001170 * @param e describes what each element of an allocation is
1171 * @param count specifies the number of element in the allocation
1172 *
1173 * @return allocation
1174 */
Jason Sams5476b452010-12-08 16:14:36 -08001175 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001176 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001177 }
1178
Jason Sams49a05d72010-12-29 14:31:29 -08001179 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001180 final Bitmap.Config bc = b.getConfig();
1181 if (bc == Bitmap.Config.ALPHA_8) {
1182 return Element.A_8(rs);
1183 }
1184 if (bc == Bitmap.Config.ARGB_4444) {
1185 return Element.RGBA_4444(rs);
1186 }
1187 if (bc == Bitmap.Config.ARGB_8888) {
1188 return Element.RGBA_8888(rs);
1189 }
1190 if (bc == Bitmap.Config.RGB_565) {
1191 return Element.RGB_565(rs);
1192 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001193 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001194 }
1195
Jason Sams49a05d72010-12-29 14:31:29 -08001196 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001197 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001198 Element e = elementFromBitmap(rs, b);
1199 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001200 tb.setX(b.getWidth());
1201 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001202 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001203 return tb.create();
1204 }
1205
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001206 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001207 * Creates a renderscript allocation from a bitmap
1208 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001209 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001210 * @param b bitmap source for the allocation data
1211 * @param mips specifies desired mipmap behaviour for the
1212 * allocation
1213 * @param usage bit field specifying how the allocation is
1214 * utilized
1215 *
1216 * @return renderscript allocation containing bitmap data
1217 *
1218 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001219 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001220 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001221 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001222 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001223 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001224
Tim Murraya3145512012-12-04 17:59:29 -08001225 // enable optimized bitmap path only with no mipmap and script-only usage
1226 if (mips == MipmapControl.MIPMAP_NONE &&
1227 t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
Stephen Hinesd34dc852012-12-19 19:33:13 -08001228 usage == (USAGE_SHARED | USAGE_SCRIPT)) {
Tim Murraya3145512012-12-04 17:59:29 -08001229 int id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
1230 if (id == 0) {
1231 throw new RSRuntimeException("Load failed.");
1232 }
1233
1234 // keep a reference to the Bitmap around to prevent GC
1235 Allocation alloc = new Allocation(id, rs, t, usage);
1236 alloc.setBitmap(b);
1237 return alloc;
1238 }
1239
1240
Jason Samse07694b2012-04-03 15:36:36 -07001241 int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001242 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001243 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001244 }
Jason Sams5476b452010-12-08 16:14:36 -08001245 return new Allocation(id, rs, t, usage);
1246 }
1247
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001248 /**
Jason Sams615e7ce2012-01-13 14:01:20 -08001249 *
1250 *
1251 * @hide
1252 *
1253 */
1254 public SurfaceTexture getSurfaceTexture() {
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001255 if ((mUsage & USAGE_IO_INPUT) == 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -08001256 throw new RSInvalidStateException("Allocation is not a surface texture.");
1257 }
1258
Jason Samse07694b2012-04-03 15:36:36 -07001259 int id = mRS.nAllocationGetSurfaceTextureID(getID(mRS));
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001260 SurfaceTexture st = new SurfaceTexture(id);
Jason Samse07694b2012-04-03 15:36:36 -07001261 mRS.nAllocationGetSurfaceTextureID2(getID(mRS), st);
Jason Sams615e7ce2012-01-13 14:01:20 -08001262
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001263 return st;
Jason Sams615e7ce2012-01-13 14:01:20 -08001264 }
1265
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001266 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001267 * For allocations used with io operations, returns the handle
1268 * onto a raw buffer that is being managed by the screen
1269 * compositor.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001270 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001271 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001272 *
1273 */
1274 public Surface getSurface() {
1275 return new Surface(getSurfaceTexture());
1276 }
1277
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001278 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001279 * Associate a surface for io output with this allocation
1280 *
1281 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001282 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001283 public void setSurface(Surface sur) {
1284 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001285 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1286 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1287 }
1288
Jason Samse07694b2012-04-03 15:36:36 -07001289 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001290 }
1291
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001292 /**
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001293 * @hide
1294 */
1295 public void setSurfaceTexture(SurfaceTexture st) {
1296 mRS.validate();
1297 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1298 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1299 }
1300
1301 Surface s = new Surface(st);
Jason Samse07694b2012-04-03 15:36:36 -07001302 mRS.nAllocationSetSurface(getID(mRS), s);
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001303 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001304
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001305 /**
Tim Murray00bb4542012-12-17 16:35:06 -08001306 * Creates a RenderScript allocation from a bitmap.
1307 *
1308 * With target API version 18 or greater, this allocation will be
1309 * created with USAGE_SHARED. With target API version 17 or lower,
1310 * this allocation will be created with USAGE_GRAPHICS_TEXTURE.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001311 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001312 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001313 * @param b bitmap source for the allocation data
1314 *
1315 * @return renderscript allocation containing bitmap data
1316 *
1317 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001318 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
Tim Murray00bb4542012-12-17 16:35:06 -08001319 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1320 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1321 USAGE_SHARED | USAGE_SCRIPT);
1322 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001323 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1324 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001325 }
1326
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001327 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001328 * Creates a cubemap allocation from a bitmap containing the
1329 * horizontal list of cube faces. Each individual face must be
1330 * the same size and power of 2
1331 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001332 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001333 * @param b bitmap with cubemap faces layed out in the following
1334 * format: right, left, top, bottom, front, back
1335 * @param mips specifies desired mipmap behaviour for the cubemap
1336 * @param usage bit field specifying how the cubemap is utilized
1337 *
1338 * @return allocation containing cubemap data
1339 *
1340 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001341 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001342 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001343 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001344 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001345
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001346 int height = b.getHeight();
1347 int width = b.getWidth();
1348
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001349 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001350 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1351 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001352 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001353 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001354 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001355 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001356 if (!isPow2) {
1357 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1358 }
1359
1360 Element e = elementFromBitmap(rs, b);
1361 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001362 tb.setX(height);
1363 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001364 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001365 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001366 Type t = tb.create();
1367
Jason Samse07694b2012-04-03 15:36:36 -07001368 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001369 if(id == 0) {
1370 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1371 }
Jason Sams5476b452010-12-08 16:14:36 -08001372 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001373 }
1374
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001375 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001376 * Creates a non-mipmapped cubemap allocation for use as a
1377 * graphics texture from a bitmap containing the horizontal list
1378 * of cube faces. Each individual face must be the same size and
1379 * power of 2
1380 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001381 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001382 * @param b bitmap with cubemap faces layed out in the following
1383 * format: right, left, top, bottom, front, back
1384 *
1385 * @return allocation containing cubemap data
1386 *
1387 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001388 static public Allocation createCubemapFromBitmap(RenderScript rs,
1389 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001390 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001391 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001392 }
1393
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001394 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001395 * Creates a cubemap allocation from 6 bitmaps containing
1396 * the cube faces. All the faces must be the same size and
1397 * power of 2
1398 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001399 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001400 * @param xpos cubemap face in the positive x direction
1401 * @param xneg cubemap face in the negative x direction
1402 * @param ypos cubemap face in the positive y direction
1403 * @param yneg cubemap face in the negative y direction
1404 * @param zpos cubemap face in the positive z direction
1405 * @param zneg cubemap face in the negative z direction
1406 * @param mips specifies desired mipmap behaviour for the cubemap
1407 * @param usage bit field specifying how the cubemap is utilized
1408 *
1409 * @return allocation containing cubemap data
1410 *
1411 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001412 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1413 Bitmap xpos,
1414 Bitmap xneg,
1415 Bitmap ypos,
1416 Bitmap yneg,
1417 Bitmap zpos,
1418 Bitmap zneg,
1419 MipmapControl mips,
1420 int usage) {
1421 int height = xpos.getHeight();
1422 if (xpos.getWidth() != height ||
1423 xneg.getWidth() != height || xneg.getHeight() != height ||
1424 ypos.getWidth() != height || ypos.getHeight() != height ||
1425 yneg.getWidth() != height || yneg.getHeight() != height ||
1426 zpos.getWidth() != height || zpos.getHeight() != height ||
1427 zneg.getWidth() != height || zneg.getHeight() != height) {
1428 throw new RSIllegalArgumentException("Only square cube map faces supported");
1429 }
1430 boolean isPow2 = (height & (height - 1)) == 0;
1431 if (!isPow2) {
1432 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1433 }
1434
1435 Element e = elementFromBitmap(rs, xpos);
1436 Type.Builder tb = new Type.Builder(rs, e);
1437 tb.setX(height);
1438 tb.setY(height);
1439 tb.setFaces(true);
1440 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1441 Type t = tb.create();
1442 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1443
1444 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001445 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001446 adapter.copyFrom(xpos);
1447 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1448 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001449 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001450 adapter.copyFrom(ypos);
1451 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1452 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001453 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001454 adapter.copyFrom(zpos);
1455 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1456 adapter.copyFrom(zneg);
1457
1458 return cubemap;
1459 }
1460
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001461 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001462 * Creates a non-mipmapped cubemap allocation for use as a
1463 * graphics texture from 6 bitmaps containing
1464 * the cube faces. All the faces must be the same size and
1465 * power of 2
1466 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001467 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001468 * @param xpos cubemap face in the positive x direction
1469 * @param xneg cubemap face in the negative x direction
1470 * @param ypos cubemap face in the positive y direction
1471 * @param yneg cubemap face in the negative y direction
1472 * @param zpos cubemap face in the positive z direction
1473 * @param zneg cubemap face in the negative z direction
1474 *
1475 * @return allocation containing cubemap data
1476 *
1477 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001478 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1479 Bitmap xpos,
1480 Bitmap xneg,
1481 Bitmap ypos,
1482 Bitmap yneg,
1483 Bitmap zpos,
1484 Bitmap zneg) {
1485 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1486 zpos, zneg, MipmapControl.MIPMAP_NONE,
1487 USAGE_GRAPHICS_TEXTURE);
1488 }
1489
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001490 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001491 * Creates a renderscript allocation from the bitmap referenced
1492 * by resource id
1493 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001494 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001495 * @param res application resources
1496 * @param id resource id to load the data from
1497 * @param mips specifies desired mipmap behaviour for the
1498 * allocation
1499 * @param usage bit field specifying how the allocation is
1500 * utilized
1501 *
1502 * @return renderscript allocation containing resource data
1503 *
1504 */
Jason Sams5476b452010-12-08 16:14:36 -08001505 static public Allocation createFromBitmapResource(RenderScript rs,
1506 Resources res,
1507 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001508 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001509 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001510
Jason Sams771bebb2009-12-07 12:40:12 -08001511 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001512 Bitmap b = BitmapFactory.decodeResource(res, id);
1513 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1514 b.recycle();
1515 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001516 }
1517
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001518 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001519 * Creates a non-mipmapped renderscript allocation to use as a
1520 * graphics texture from the bitmap referenced by resource id
1521 *
Jason Sams455d6442013-02-05 19:20:18 -08001522 * With target API version 18 or greater, this allocation will be
1523 * created with USAGE_SHARED. With target API version 17 or lower,
1524 * this allocation will be created with USAGE_GRAPHICS_TEXTURE.
1525 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001526 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001527 * @param res application resources
1528 * @param id resource id to load the data from
1529 *
1530 * @return renderscript allocation containing resource data
1531 *
1532 */
Jason Sams5476b452010-12-08 16:14:36 -08001533 static public Allocation createFromBitmapResource(RenderScript rs,
1534 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001535 int id) {
Jason Sams455d6442013-02-05 19:20:18 -08001536 if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1537 return createFromBitmapResource(rs, res, id,
1538 MipmapControl.MIPMAP_NONE,
1539 USAGE_SHARED | USAGE_SCRIPT);
1540 }
Jason Sams6d8eb262010-12-15 01:41:00 -08001541 return createFromBitmapResource(rs, res, id,
1542 MipmapControl.MIPMAP_NONE,
1543 USAGE_GRAPHICS_TEXTURE);
1544 }
1545
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001546 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001547 * Creates a renderscript allocation containing string data
1548 * encoded in UTF-8 format
1549 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001550 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001551 * @param str string to create the allocation from
1552 * @param usage bit field specifying how the allocaiton is
1553 * utilized
1554 *
1555 */
Jason Sams5476b452010-12-08 16:14:36 -08001556 static public Allocation createFromString(RenderScript rs,
1557 String str,
1558 int usage) {
1559 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001560 byte[] allocArray = null;
1561 try {
1562 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001563 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001564 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001565 return alloc;
1566 }
1567 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001568 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001569 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001570 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001571}
1572
1573