blob: 37a270e9c10d4fc5bb22e604394dd937f6c4afa0 [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 Sams615e7ce2012-01-13 14:01:20 -080025import android.graphics.SurfaceTexture;
Jason Samsb8c5a842009-07-31 20:40:47 -070026import android.util.Log;
Romain Guy650a3eb2009-08-31 14:06:43 -070027import android.util.TypedValue;
Jason Samsb8c5a842009-07-31 20:40:47 -070028
29/**
Robert Ly11518ac2011-02-09 13:57:06 -080030 * <p>
31 * Memory allocation class for renderscript. An allocation combines a
32 * {@link android.renderscript.Type} with the memory to provide storage for user data and objects.
33 * This implies that all memory in Renderscript is typed.
34 * </p>
Jason Samsa23d4e72011-01-04 18:59:12 -080035 *
Robert Ly11518ac2011-02-09 13:57:06 -080036 * <p>Allocations are the primary way data moves into and out of scripts. Memory is user
37 * synchronized and it's possible for allocations to exist in multiple memory spaces
38 * concurrently. Currently those spaces are:</p>
39 * <ul>
40 * <li>Script: accessable by RS scripts.</li>
41 * <li>Graphics Texture: accessable as a graphics texture.</li>
42 * <li>Graphics Vertex: accessable as graphical vertex data.</li>
43 * <li>Graphics Constants: Accessable as constants in user shaders</li>
44 * </ul>
45 * </p>
46 * <p>
47 * For example, when creating a allocation for a texture, the user can
48 * specify its memory spaces as both script and textures. This means that it can both
49 * be used as script binding and as a GPU texture for rendering. To maintain
50 * synchronization if a script modifies an allocation used by other targets it must
51 * call a synchronizing function to push the updates to the memory, otherwise the results
52 * are undefined.
53 * </p>
54 * <p>By default, Android system side updates are always applied to the script accessable
55 * memory. If this is not present, they are then applied to the various HW
56 * memory types. A {@link android.renderscript.Allocation#syncAll syncAll()}
57 * call is necessary after the script data is updated to
58 * keep the other memory spaces in sync.</p>
Jason Samsa23d4e72011-01-04 18:59:12 -080059 *
Robert Ly11518ac2011-02-09 13:57:06 -080060 * <p>Allocation data is uploaded in one of two primary ways. For simple
61 * arrays there are copyFrom() functions that take an array from the control code and
62 * copy it to the slave memory store. Both type checked and unchecked copies are provided.
63 * The unchecked variants exist to allow apps to copy over arrays of structures from a
64 * control language that does not support structures.</p>
Jason Samsb8c5a842009-07-31 20:40:47 -070065 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080066 * <div class="special reference">
67 * <h3>Developer Guides</h3>
68 * <p>For more information about creating an application that uses Renderscript, read the
69 * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p>
70 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070071 **/
72public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070073 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080074 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080075 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070076 Allocation mAdaptedAllocation;
77
78 boolean mConstrainedLOD;
79 boolean mConstrainedFace;
80 boolean mConstrainedY;
81 boolean mConstrainedZ;
Jason Sams615e7ce2012-01-13 14:01:20 -080082 boolean mReadAllowed = true;
83 boolean mWriteAllowed = true;
Jason Samsba862d12011-07-07 15:24:42 -070084 int mSelectedY;
85 int mSelectedZ;
86 int mSelectedLOD;
87 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
88
89 int mCurrentDimX;
90 int mCurrentDimY;
91 int mCurrentDimZ;
92 int mCurrentCount;
93
Jason Sams5476b452010-12-08 16:14:36 -080094
Jason Samsf7086092011-01-12 13:28:37 -080095 /**
96 * The usage of the allocation. These signal to renderscript
97 * where to place the allocation in memory.
98 *
99 * SCRIPT The allocation will be bound to and accessed by
100 * scripts.
101 */
Jason Sams5476b452010-12-08 16:14:36 -0800102 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800103
104 /**
Jason Sams163766c2012-02-15 12:04:24 -0800105 * GRAPHICS_TEXTURE The allocation will be used as a texture
Stephen Hines836c4a52011-06-01 14:38:10 -0700106 * source by one or more graphics programs.
Jason Samsf7086092011-01-12 13:28:37 -0800107 *
108 */
Jason Sams5476b452010-12-08 16:14:36 -0800109 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800110
111 /**
112 * GRAPHICS_VERTEX The allocation will be used as a graphics
113 * mesh.
114 *
115 */
Jason Sams5476b452010-12-08 16:14:36 -0800116 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800117
118
119 /**
120 * GRAPHICS_CONSTANTS The allocation will be used as the source
121 * of shader constants by one or more programs.
122 *
123 */
Jason Sams5476b452010-12-08 16:14:36 -0800124 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
125
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700126 /**
Jason Sams163766c2012-02-15 12:04:24 -0800127 * USAGE_GRAPHICS_RENDER_TARGET The allocation will be used as a
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700128 * target for offscreen rendering
129 *
130 */
131 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
132
Jason Sams615e7ce2012-01-13 14:01:20 -0800133 /**
Jason Sams163766c2012-02-15 12:04:24 -0800134 * USAGE_GRAPHICS_SURFACE_TEXTURE_INPUT_OPAQUE The allocation
135 * will be used as a SurfaceTexture graphics consumer. This
136 * usage may only be used with USAGE_GRAPHICS_TEXTURE.
Jason Sams615e7ce2012-01-13 14:01:20 -0800137 *
138 * @hide
139 */
140 public static final int USAGE_GRAPHICS_SURFACE_TEXTURE_INPUT_OPAQUE = 0x0020;
141
142 /**
Jason Sams163766c2012-02-15 12:04:24 -0800143 * USAGE_IO_INPUT The allocation will be used as SurfaceTexture
144 * consumer. This usage will cause the allocation to be created
145 * read only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800146 *
147 * @hide
148 */
Jason Sams615e7ce2012-01-13 14:01:20 -0800149 public static final int USAGE_IO_INPUT = 0x0040;
Stephen Hines9069ee82012-02-13 18:25:54 -0800150
Jason Sams615e7ce2012-01-13 14:01:20 -0800151 /**
Jason Sams163766c2012-02-15 12:04:24 -0800152 * USAGE_IO_OUTPUT The allocation will be used as a
153 * SurfaceTexture producer. The dimensions and format of the
154 * SurfaceTexture will be forced to those of the allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800155 *
156 * @hide
157 */
158 public static final int USAGE_IO_OUTPUT = 0x0080;
Jason Sams43ee06852009-08-12 17:54:11 -0700159
Jason Samsf7086092011-01-12 13:28:37 -0800160 /**
161 * Controls mipmap behavior when using the bitmap creation and
162 * update functions.
163 */
Jason Sams4ef66502010-12-10 16:03:15 -0800164 public enum MipmapControl {
Jason Samsf7086092011-01-12 13:28:37 -0800165 /**
166 * No mipmaps will be generated and the type generated from the
167 * incoming bitmap will not contain additional LODs.
168 */
Jason Sams5476b452010-12-08 16:14:36 -0800169 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800170
171 /**
172 * A Full mipmap chain will be created in script memory. The
173 * type of the allocation will contain a full mipmap chain. On
174 * upload to graphics the full chain will be transfered.
175 */
Jason Sams5476b452010-12-08 16:14:36 -0800176 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800177
178 /**
179 * The type of the allocation will be the same as MIPMAP_NONE.
180 * It will not contain mipmaps. On upload to graphics the
181 * graphics copy of the allocation data will contain a full
182 * mipmap chain generated from the top level in script memory.
183 */
Jason Sams5476b452010-12-08 16:14:36 -0800184 MIPMAP_ON_SYNC_TO_TEXTURE(2);
185
186 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800187 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800188 mID = id;
189 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700190 }
191
Jason Sams48fe5342011-07-08 13:52:30 -0700192
193 private int getIDSafe() {
194 if (mAdaptedAllocation != null) {
195 return mAdaptedAllocation.getID();
196 }
197 return getID();
198 }
199
Jason Sams452a7662011-07-07 16:05:18 -0700200 private void updateCacheInfo(Type t) {
201 mCurrentDimX = t.getX();
202 mCurrentDimY = t.getY();
203 mCurrentDimZ = t.getZ();
204 mCurrentCount = mCurrentDimX;
205 if (mCurrentDimY > 1) {
206 mCurrentCount *= mCurrentDimY;
207 }
208 if (mCurrentDimZ > 1) {
209 mCurrentCount *= mCurrentDimZ;
210 }
211 }
Jason Samsba862d12011-07-07 15:24:42 -0700212
Jason Sams5476b452010-12-08 16:14:36 -0800213 Allocation(int id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700214 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800215 if ((usage & ~(USAGE_SCRIPT |
216 USAGE_GRAPHICS_TEXTURE |
217 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700218 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800219 USAGE_GRAPHICS_RENDER_TARGET |
220 USAGE_GRAPHICS_SURFACE_TEXTURE_INPUT_OPAQUE |
221 USAGE_IO_INPUT |
222 USAGE_IO_OUTPUT)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800223 throw new RSIllegalArgumentException("Unknown usage specified.");
224 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800225
226 if ((usage & (USAGE_GRAPHICS_SURFACE_TEXTURE_INPUT_OPAQUE | USAGE_IO_INPUT)) != 0) {
227 mWriteAllowed = false;
228
229 if ((usage & ~(USAGE_GRAPHICS_SURFACE_TEXTURE_INPUT_OPAQUE |
230 USAGE_IO_INPUT |
231 USAGE_GRAPHICS_TEXTURE |
232 USAGE_SCRIPT)) != 0) {
233 throw new RSIllegalArgumentException("Invalid usage combination.");
234 }
235 }
236
Jason Sams5476b452010-12-08 16:14:36 -0800237 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800238 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700239
Jason Sams452a7662011-07-07 16:05:18 -0700240 if (t != null) {
241 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700242 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700243 }
244
Jason Samsb97b2512011-01-16 15:04:08 -0800245 private void validateIsInt32() {
246 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
247 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
248 return;
249 }
250 throw new RSIllegalArgumentException(
251 "32 bit integer source does not match allocation type " + mType.mElement.mType);
252 }
253
254 private void validateIsInt16() {
255 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
256 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
257 return;
258 }
259 throw new RSIllegalArgumentException(
260 "16 bit integer source does not match allocation type " + mType.mElement.mType);
261 }
262
263 private void validateIsInt8() {
264 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
265 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
266 return;
267 }
268 throw new RSIllegalArgumentException(
269 "8 bit integer source does not match allocation type " + mType.mElement.mType);
270 }
271
272 private void validateIsFloat32() {
273 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
274 return;
275 }
276 throw new RSIllegalArgumentException(
277 "32 bit float source does not match allocation type " + mType.mElement.mType);
278 }
279
280 private void validateIsObject() {
281 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
282 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
283 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
284 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
285 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
286 (mType.mElement.mType == Element.DataType.RS_MESH) ||
287 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
288 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
289 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
290 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
291 return;
292 }
293 throw new RSIllegalArgumentException(
294 "Object source does not match allocation type " + mType.mElement.mType);
295 }
296
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700297 @Override
298 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800299 super.updateFromNative();
300 int typeID = mRS.nAllocationGetType(getID());
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700301 if(typeID != 0) {
302 mType = new Type(typeID, mRS);
303 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700304 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700305 }
306 }
307
Jason Samsea87e962010-01-12 12:12:28 -0800308 public Type getType() {
309 return mType;
310 }
311
Jason Sams5476b452010-12-08 16:14:36 -0800312 public void syncAll(int srcLocation) {
313 switch (srcLocation) {
314 case USAGE_SCRIPT:
315 case USAGE_GRAPHICS_CONSTANTS:
316 case USAGE_GRAPHICS_TEXTURE:
317 case USAGE_GRAPHICS_VERTEX:
318 break;
319 default:
320 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
321 }
322 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700323 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Jason Sams5476b452010-12-08 16:14:36 -0800324 }
325
Jason Sams163766c2012-02-15 12:04:24 -0800326 /**
327 * Send a buffer to the output stream. The contents of the
328 * Allocation will be undefined after this operation.
329 *
330 * @hide
331 *
332 */
333 public void ioSendOutput() {
334 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
335 throw new RSIllegalArgumentException(
336 "Can only send buffer if IO_OUTPUT usage specified.");
337 }
338 mRS.validate();
339 mRS.nAllocationIoSend(getID());
340 }
341
342 /**
343 * Receive the latest input into the Allocation.
344 *
345 * @hide
346 *
347 */
348 public void ioGetInput() {
349 if ((mUsage & USAGE_IO_INPUT) == 0) {
350 throw new RSIllegalArgumentException(
351 "Can only send buffer if IO_OUTPUT usage specified.");
352 }
353 mRS.validate();
354 mRS.nAllocationIoReceive(getID());
355 }
356
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800357 public void copyFrom(BaseObj[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800358 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800359 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700360 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800361 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700362 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800363 }
364 int i[] = new int[d.length];
365 for (int ct=0; ct < d.length; ct++) {
366 i[ct] = d[ct].getID();
367 }
Jason Samsba862d12011-07-07 15:24:42 -0700368 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Jason Samsb8c5a842009-07-31 20:40:47 -0700369 }
370
Jason Samsfb9f82c2011-01-12 14:53:25 -0800371 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800372 Bitmap.Config bc = b.getConfig();
373 switch (bc) {
374 case ALPHA_8:
375 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
376 throw new RSIllegalArgumentException("Allocation kind is " +
377 mType.getElement().mKind + ", type " +
378 mType.getElement().mType +
379 " of " + mType.getElement().getSizeBytes() +
380 " bytes, passed bitmap was " + bc);
381 }
382 break;
383 case ARGB_8888:
384 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
385 (mType.getElement().getSizeBytes() != 4)) {
386 throw new RSIllegalArgumentException("Allocation kind is " +
387 mType.getElement().mKind + ", type " +
388 mType.getElement().mType +
389 " of " + mType.getElement().getSizeBytes() +
390 " bytes, passed bitmap was " + bc);
391 }
392 break;
393 case RGB_565:
394 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
395 (mType.getElement().getSizeBytes() != 2)) {
396 throw new RSIllegalArgumentException("Allocation kind is " +
397 mType.getElement().mKind + ", type " +
398 mType.getElement().mType +
399 " of " + mType.getElement().getSizeBytes() +
400 " bytes, passed bitmap was " + bc);
401 }
402 break;
403 case ARGB_4444:
404 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
405 (mType.getElement().getSizeBytes() != 2)) {
406 throw new RSIllegalArgumentException("Allocation kind is " +
407 mType.getElement().mKind + ", type " +
408 mType.getElement().mType +
409 " of " + mType.getElement().getSizeBytes() +
410 " bytes, passed bitmap was " + bc);
411 }
412 break;
413
414 }
Jason Sams4ef66502010-12-10 16:03:15 -0800415 }
416
Jason Samsfb9f82c2011-01-12 14:53:25 -0800417 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700418 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800419 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
420 }
421 }
422
Jason Sams4fa3eed2011-01-19 15:44:38 -0800423 /**
424 * Copy an allocation from an array. This variant is not type
425 * checked which allows an application to fill in structured
426 * data from an array.
427 *
428 * @param d the source data array
429 */
430 public void copyFromUnchecked(int[] d) {
431 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700432 copy1DRangeFromUnchecked(0, mCurrentCount, d);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800433 }
434 /**
435 * Copy an allocation from an array. This variant is not type
436 * checked which allows an application to fill in structured
437 * data from an array.
438 *
439 * @param d the source data array
440 */
441 public void copyFromUnchecked(short[] d) {
442 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700443 copy1DRangeFromUnchecked(0, mCurrentCount, d);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800444 }
445 /**
446 * Copy an allocation from an array. This variant is not type
447 * checked which allows an application to fill in structured
448 * data from an array.
449 *
450 * @param d the source data array
451 */
452 public void copyFromUnchecked(byte[] d) {
453 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700454 copy1DRangeFromUnchecked(0, mCurrentCount, d);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800455 }
456 /**
457 * Copy an allocation from an array. This variant is not type
458 * checked which allows an application to fill in structured
459 * data from an array.
460 *
461 * @param d the source data array
462 */
463 public void copyFromUnchecked(float[] d) {
464 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700465 copy1DRangeFromUnchecked(0, mCurrentCount, d);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800466 }
467
468 /**
469 * Copy an allocation from an array. This variant is type
470 * checked and will generate exceptions if the Allocation type
471 * is not a 32 bit integer type.
472 *
473 * @param d the source data array
474 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800475 public void copyFrom(int[] d) {
476 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700477 copy1DRangeFrom(0, mCurrentCount, d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800478 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800479
480 /**
481 * Copy an allocation from an array. This variant is type
482 * checked and will generate exceptions if the Allocation type
483 * is not a 16 bit integer type.
484 *
485 * @param d the source data array
486 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800487 public void copyFrom(short[] d) {
488 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700489 copy1DRangeFrom(0, mCurrentCount, d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800490 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800491
492 /**
493 * Copy an allocation from an array. This variant is type
494 * checked and will generate exceptions if the Allocation type
495 * is not a 8 bit integer type.
496 *
497 * @param d the source data array
498 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800499 public void copyFrom(byte[] d) {
500 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700501 copy1DRangeFrom(0, mCurrentCount, d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800502 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800503
504 /**
505 * Copy an allocation from an array. This variant is type
506 * checked and will generate exceptions if the Allocation type
507 * is not a 32 bit float type.
508 *
509 * @param d the source data array
510 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800511 public void copyFrom(float[] d) {
512 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700513 copy1DRangeFrom(0, mCurrentCount, d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800514 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800515
516 /**
517 * Copy an allocation from a bitmap. The height, width, and
518 * format of the bitmap must match the existing allocation.
519 *
520 * @param b the source bitmap
521 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800522 public void copyFrom(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800523 mRS.validate();
524 validateBitmapSize(b);
525 validateBitmapFormat(b);
Jason Sams4ef66502010-12-10 16:03:15 -0800526 mRS.nAllocationCopyFromBitmap(getID(), b);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700527 }
528
Jason Samsfa445b92011-01-07 17:00:07 -0800529 /**
Jason Samsfa445b92011-01-07 17:00:07 -0800530 * This is only intended to be used by auto-generate code reflected from the
531 * renderscript script files.
532 *
533 * @param xoff
534 * @param fp
535 */
Jason Sams21b41032011-01-16 15:05:41 -0800536 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsa70f4162010-03-26 15:33:42 -0700537 int eSize = mType.mElement.getSizeBytes();
538 final byte[] data = fp.getData();
539
540 int count = data.length / eSize;
541 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800542 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700543 " not divisible by element size " + eSize + ".");
544 }
Jason Samsba862d12011-07-07 15:24:42 -0700545 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700546 }
547
Jason Samsfa445b92011-01-07 17:00:07 -0800548 /**
Jason Samsfa445b92011-01-07 17:00:07 -0800549 * This is only intended to be used by auto-generate code reflected from the
550 * renderscript script files.
551 *
552 * @param xoff
553 * @param component_number
554 * @param fp
555 */
Jason Sams21b41032011-01-16 15:05:41 -0800556 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700557 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800558 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700559 }
560 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800561 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700562 }
563
564 final byte[] data = fp.getData();
565 int eSize = mType.mElement.mElements[component_number].getSizeBytes();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800566 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700567
568 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800569 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700570 " does not match component size " + eSize + ".");
571 }
572
Jason Sams48fe5342011-07-08 13:52:30 -0700573 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Jason Samsba862d12011-07-07 15:24:42 -0700574 component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700575 }
576
Jason Sams768bc022009-09-21 19:41:04 -0700577 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800578 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700579 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800580 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700581 }
582 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800583 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700584 }
Jason Samsba862d12011-07-07 15:24:42 -0700585 if((off + count) > mCurrentCount) {
586 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700587 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700588 }
Jason Samsba862d12011-07-07 15:24:42 -0700589 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800590 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700591 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700592 }
593
Jason Samsf7086092011-01-12 13:28:37 -0800594 /**
595 * Generate a mipmap chain. Requires the type of the allocation
596 * include mipmaps.
597 *
598 * This function will generate a complete set of mipmaps from
599 * the top level lod and place them into the script memoryspace.
600 *
601 * If the allocation is also using other memory spaces a
602 * followup sync will be required.
603 */
604 public void generateMipmaps() {
605 mRS.nAllocationGenerateMipmaps(getID());
606 }
607
Jason Sams4fa3eed2011-01-19 15:44:38 -0800608 /**
609 * Copy part of an allocation from an array. This variant is
610 * not type checked which allows an application to fill in
611 * structured data from an array.
612 *
613 * @param off The offset of the first element to be copied.
614 * @param count The number of elements to be copied.
615 * @param d the source data array
616 */
617 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700618 int dataSize = mType.mElement.getSizeBytes() * count;
619 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700620 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700621 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800622 /**
623 * Copy part of an allocation from an array. This variant is
624 * not type checked which allows an application to fill in
625 * structured data from an array.
626 *
627 * @param off The offset of the first element to be copied.
628 * @param count The number of elements to be copied.
629 * @param d the source data array
630 */
631 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700632 int dataSize = mType.mElement.getSizeBytes() * count;
633 data1DChecks(off, count, d.length * 2, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700634 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700635 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800636 /**
637 * Copy part of an allocation from an array. This variant is
638 * not type checked which allows an application to fill in
639 * structured data from an array.
640 *
641 * @param off The offset of the first element to be copied.
642 * @param count The number of elements to be copied.
643 * @param d the source data array
644 */
645 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700646 int dataSize = mType.mElement.getSizeBytes() * count;
647 data1DChecks(off, count, d.length, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700648 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700649 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800650 /**
651 * Copy part of an allocation from an array. This variant is
652 * not type checked which allows an application to fill in
653 * structured data from an array.
654 *
655 * @param off The offset of the first element to be copied.
656 * @param count The number of elements to be copied.
657 * @param d the source data array
658 */
659 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700660 int dataSize = mType.mElement.getSizeBytes() * count;
661 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700662 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700663 }
664
Jason Sams4fa3eed2011-01-19 15:44:38 -0800665 /**
666 * Copy part of an allocation from an array. This variant is
667 * type checked and will generate exceptions if the Allocation
668 * type is not a 32 bit integer type.
669 *
670 * @param off The offset of the first element to be copied.
671 * @param count The number of elements to be copied.
672 * @param d the source data array
673 */
Jason Samsb97b2512011-01-16 15:04:08 -0800674 public void copy1DRangeFrom(int off, int count, int[] d) {
675 validateIsInt32();
676 copy1DRangeFromUnchecked(off, count, d);
677 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800678
679 /**
680 * Copy part of an allocation from an array. This variant is
681 * type checked and will generate exceptions if the Allocation
682 * type is not a 16 bit integer type.
683 *
684 * @param off The offset of the first element to be copied.
685 * @param count The number of elements to be copied.
686 * @param d the source data array
687 */
Jason Samsb97b2512011-01-16 15:04:08 -0800688 public void copy1DRangeFrom(int off, int count, short[] d) {
689 validateIsInt16();
690 copy1DRangeFromUnchecked(off, count, d);
691 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800692
693 /**
694 * Copy part of an allocation from an array. This variant is
695 * type checked and will generate exceptions if the Allocation
696 * type is not a 8 bit integer type.
697 *
698 * @param off The offset of the first element to be copied.
699 * @param count The number of elements to be copied.
700 * @param d the source data array
701 */
Jason Samsb97b2512011-01-16 15:04:08 -0800702 public void copy1DRangeFrom(int off, int count, byte[] d) {
703 validateIsInt8();
704 copy1DRangeFromUnchecked(off, count, d);
705 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800706
707 /**
708 * Copy part of an allocation from an array. This variant is
709 * type checked and will generate exceptions if the Allocation
710 * type is not a 32 bit float type.
711 *
712 * @param off The offset of the first element to be copied.
713 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700714 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800715 */
Jason Samsb97b2512011-01-16 15:04:08 -0800716 public void copy1DRangeFrom(int off, int count, float[] d) {
717 validateIsFloat32();
718 copy1DRangeFromUnchecked(off, count, d);
719 }
720
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700721 /**
722 * Copy part of an allocation from another allocation.
723 *
724 * @param off The offset of the first element to be copied.
725 * @param count The number of elements to be copied.
726 * @param data the source data allocation.
727 * @param dataOff off The offset of the first element in data to
728 * be copied.
729 */
730 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Jason Sams48fe5342011-07-08 13:52:30 -0700731 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700732 mSelectedLOD, mSelectedFace.mID,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700733 count, 1, data.getID(), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700734 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700735 }
736
Jason Samsfb9f82c2011-01-12 14:53:25 -0800737 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -0700738 if (mAdaptedAllocation != null) {
739
740 } else {
741
742 if (xoff < 0 || yoff < 0) {
743 throw new RSIllegalArgumentException("Offset cannot be negative.");
744 }
745 if (h < 0 || w < 0) {
746 throw new RSIllegalArgumentException("Height or width cannot be negative.");
747 }
748 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
749 throw new RSIllegalArgumentException("Updated region larger than allocation.");
750 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800751 }
752 }
Jason Sams768bc022009-09-21 19:41:04 -0700753
Jason Samsf7086092011-01-12 13:28:37 -0800754 /**
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700755 * Copy a rectangular region from the array into the allocation.
756 * The incoming array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -0800757 *
758 * @param xoff X offset of the region to update
759 * @param yoff Y offset of the region to update
760 * @param w Width of the incoming region to update
761 * @param h Height of the incoming region to update
762 * @param data to be placed into the allocation
763 */
764 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800765 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800766 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700767 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700768 w, h, data, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -0800769 }
770
Jason Samsf7086092011-01-12 13:28:37 -0800771 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800772 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800773 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700774 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700775 w, h, data, data.length * 2);
Jason Samsfa445b92011-01-07 17:00:07 -0800776 }
777
Jason Samsf7086092011-01-12 13:28:37 -0800778 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800779 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800780 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700781 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700782 w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700783 }
784
Jason Samsf7086092011-01-12 13:28:37 -0800785 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800786 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800787 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700788 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700789 w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700790 }
791
Jason Samsf7086092011-01-12 13:28:37 -0800792 /**
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700793 * Copy a rectangular region into the allocation from another
794 * allocation.
795 *
796 * @param xoff X offset of the region to update.
797 * @param yoff Y offset of the region to update.
798 * @param w Width of the incoming region to update.
799 * @param h Height of the incoming region to update.
800 * @param data source allocation.
801 * @param dataXoff X offset in data of the region to update.
802 * @param dataYoff Y offset in data of the region to update.
803 */
804 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
805 Allocation data, int dataXoff, int dataYoff) {
806 mRS.validate();
807 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700808 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -0700809 mSelectedLOD, mSelectedFace.mID,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700810 w, h, data.getID(), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -0700811 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700812 }
813
814 /**
Jason Samsf7086092011-01-12 13:28:37 -0800815 * Copy a bitmap into an allocation. The height and width of
816 * the update will use the height and width of the incoming
817 * bitmap.
818 *
819 * @param xoff X offset of the region to update
820 * @param yoff Y offset of the region to update
821 * @param data the bitmap to be copied
822 */
823 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800824 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800825 validateBitmapFormat(data);
826 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -0700827 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Jason Samsfa445b92011-01-07 17:00:07 -0800828 }
829
830
Jason Sams48fe5342011-07-08 13:52:30 -0700831 /**
832 * Copy from the Allocation into a Bitmap. The bitmap must
833 * match the dimensions of the Allocation.
834 *
835 * @param b The bitmap to be set from the Allocation.
836 */
Jason Samsfa445b92011-01-07 17:00:07 -0800837 public void copyTo(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800838 mRS.validate();
839 validateBitmapFormat(b);
840 validateBitmapSize(b);
Jason Samsfa445b92011-01-07 17:00:07 -0800841 mRS.nAllocationCopyToBitmap(getID(), b);
842 }
843
Jason Sams48fe5342011-07-08 13:52:30 -0700844 /**
845 * Copy from the Allocation into a byte array. The array must
846 * be at least as large as the Allocation. The allocation must
847 * be of an 8 bit elemental type.
848 *
849 * @param d The array to be set from the Allocation.
850 */
Jason Samsfa445b92011-01-07 17:00:07 -0800851 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800852 validateIsInt8();
Jason Sams771bebb2009-12-07 12:40:12 -0800853 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800854 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700855 }
856
Jason Sams48fe5342011-07-08 13:52:30 -0700857 /**
858 * Copy from the Allocation into a short array. The array must
859 * be at least as large as the Allocation. The allocation must
860 * be of an 16 bit elemental type.
861 *
862 * @param d The array to be set from the Allocation.
863 */
Jason Samsfa445b92011-01-07 17:00:07 -0800864 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800865 validateIsInt16();
Jason Samsfa445b92011-01-07 17:00:07 -0800866 mRS.validate();
867 mRS.nAllocationRead(getID(), d);
868 }
869
Jason Sams48fe5342011-07-08 13:52:30 -0700870 /**
871 * Copy from the Allocation into a int array. The array must be
872 * at least as large as the Allocation. The allocation must be
873 * of an 32 bit elemental type.
874 *
875 * @param d The array to be set from the Allocation.
876 */
Jason Samsfa445b92011-01-07 17:00:07 -0800877 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800878 validateIsInt32();
Jason Samsfa445b92011-01-07 17:00:07 -0800879 mRS.validate();
880 mRS.nAllocationRead(getID(), d);
881 }
882
Jason Sams48fe5342011-07-08 13:52:30 -0700883 /**
884 * Copy from the Allocation into a float array. The array must
885 * be at least as large as the Allocation. The allocation must
886 * be of an 32 bit float elemental type.
887 *
888 * @param d The array to be set from the Allocation.
889 */
Jason Samsfa445b92011-01-07 17:00:07 -0800890 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800891 validateIsFloat32();
Jason Sams771bebb2009-12-07 12:40:12 -0800892 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800893 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700894 }
895
Jason Samsf7086092011-01-12 13:28:37 -0800896 /**
897 * Resize a 1D allocation. The contents of the allocation are
898 * preserved. If new elements are allocated objects are created
899 * with null contents and the new region is otherwise undefined.
900 *
901 * If the new region is smaller the references of any objects
902 * outside the new region will be released.
903 *
904 * A new type will be created with the new dimension.
905 *
906 * @param dimX The new size of the allocation.
907 */
Jason Sams31a7e422010-10-26 13:09:17 -0700908 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800909 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800910 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700911 }
Jason Sams06d69de2010-11-09 17:11:40 -0800912 mRS.nAllocationResize1D(getID(), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -0700913 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -0700914
Jason Sams06d69de2010-11-09 17:11:40 -0800915 int typeID = mRS.nAllocationGetType(getID());
Jason Sams31a7e422010-10-26 13:09:17 -0700916 mType = new Type(typeID, mRS);
917 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -0700918 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -0700919 }
920
Jason Sams163766c2012-02-15 12:04:24 -0800921 /**
922 * Resize a 2D allocation. The contents of the allocation are
923 * preserved. If new elements are allocated objects are created
924 * with null contents and the new region is otherwise undefined.
925 *
926 * If the new region is smaller the references of any objects
927 * outside the new region will be released.
928 *
929 * A new type will be created with the new dimension.
930 *
931 * @hide
932 * @param dimX The new size of the allocation.
933 * @param dimY The new size of the allocation.
934 */
Jason Sams5edc6082010-10-05 13:32:49 -0700935 public void resize(int dimX, int dimY) {
Jason Sams163766c2012-02-15 12:04:24 -0800936 if ((mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
937 throw new RSInvalidStateException(
938 "Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700939 }
940 if (mType.getY() == 0) {
Jason Sams163766c2012-02-15 12:04:24 -0800941 throw new RSInvalidStateException(
942 "Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700943 }
Jason Sams06d69de2010-11-09 17:11:40 -0800944 mRS.nAllocationResize2D(getID(), dimX, dimY);
Jason Sams163766c2012-02-15 12:04:24 -0800945 mRS.finish(); // Necessary because resize is fifoed and update is async.
946
947 int typeID = mRS.nAllocationGetType(getID());
948 mType = new Type(typeID, mRS);
949 mType.updateFromNative();
950 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -0700951 }
Jason Sams40a29e82009-08-10 14:55:26 -0700952
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700953
Jason Samsb8c5a842009-07-31 20:40:47 -0700954
955 // creation
956
Jason Sams49a05d72010-12-29 14:31:29 -0800957 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -0700958 static {
959 mBitmapOptions.inScaled = false;
960 }
961
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800962 /**
963 *
964 * @param type renderscript type describing data layout
965 * @param mips specifies desired mipmap behaviour for the
966 * allocation
967 * @param usage bit field specifying how the allocation is
968 * utilized
969 */
970 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800971 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800972 if (type.getID() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800973 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -0700974 }
Jason Sams857d0c72011-11-23 15:02:15 -0800975 int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage, 0);
976 if (id == 0) {
977 throw new RSRuntimeException("Allocation creation failed.");
978 }
979 return new Allocation(id, rs, type, usage);
980 }
981
982 /**
983 * @hide
984 * This API is hidden and only intended to be used for
985 * transitional purposes.
986 *
987 * @param type renderscript type describing data layout
988 * @param mips specifies desired mipmap behaviour for the
989 * allocation
990 * @param usage bit field specifying how the allocation is
991 * utilized
992 */
993 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips,
994 int usage, int pointer) {
995 rs.validate();
996 if (type.getID() == 0) {
997 throw new RSInvalidStateException("Bad Type");
998 }
999 int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage, pointer);
Jason Sams5476b452010-12-08 16:14:36 -08001000 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001001 throw new RSRuntimeException("Allocation creation failed.");
1002 }
Jason Sams5476b452010-12-08 16:14:36 -08001003 return new Allocation(id, rs, type, usage);
Jason Samsb8c5a842009-07-31 20:40:47 -07001004 }
1005
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001006 /**
1007 * Creates a renderscript allocation with the size specified by
1008 * the type and no mipmaps generated by default
1009 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001010 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001011 * @param type renderscript type describing data layout
1012 * @param usage bit field specifying how the allocation is
1013 * utilized
1014 *
1015 * @return allocation
1016 */
Jason Samse5d37122010-12-16 00:33:33 -08001017 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1018 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1019 }
1020
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001021 /**
1022 * Creates a renderscript allocation for use by the script with
1023 * the size specified by the type and no mipmaps generated by
1024 * default
1025 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001026 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001027 * @param type renderscript type describing data layout
1028 *
1029 * @return allocation
1030 */
Jason Sams5476b452010-12-08 16:14:36 -08001031 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001032 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001033 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001034
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001035 /**
1036 * Creates a renderscript allocation with a specified number of
1037 * given elements
1038 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001039 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001040 * @param e describes what each element of an allocation is
1041 * @param count specifies the number of element in the allocation
1042 * @param usage bit field specifying how the allocation is
1043 * utilized
1044 *
1045 * @return allocation
1046 */
Jason Sams5476b452010-12-08 16:14:36 -08001047 static public Allocation createSized(RenderScript rs, Element e,
1048 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001049 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001050 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001051 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001052 Type t = b.create();
1053
Jason Sams857d0c72011-11-23 15:02:15 -08001054 int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001055 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001056 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001057 }
Jason Sams5476b452010-12-08 16:14:36 -08001058 return new Allocation(id, rs, t, usage);
1059 }
1060
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001061 /**
1062 * Creates a renderscript allocation with a specified number of
1063 * given elements
1064 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001065 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001066 * @param e describes what each element of an allocation is
1067 * @param count specifies the number of element in the allocation
1068 *
1069 * @return allocation
1070 */
Jason Sams5476b452010-12-08 16:14:36 -08001071 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001072 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001073 }
1074
Jason Sams49a05d72010-12-29 14:31:29 -08001075 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001076 final Bitmap.Config bc = b.getConfig();
1077 if (bc == Bitmap.Config.ALPHA_8) {
1078 return Element.A_8(rs);
1079 }
1080 if (bc == Bitmap.Config.ARGB_4444) {
1081 return Element.RGBA_4444(rs);
1082 }
1083 if (bc == Bitmap.Config.ARGB_8888) {
1084 return Element.RGBA_8888(rs);
1085 }
1086 if (bc == Bitmap.Config.RGB_565) {
1087 return Element.RGB_565(rs);
1088 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001089 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001090 }
1091
Jason Sams49a05d72010-12-29 14:31:29 -08001092 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001093 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001094 Element e = elementFromBitmap(rs, b);
1095 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001096 tb.setX(b.getWidth());
1097 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001098 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001099 return tb.create();
1100 }
1101
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001102 /**
1103 * Creates a renderscript allocation from a bitmap
1104 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001105 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001106 * @param b bitmap source for the allocation data
1107 * @param mips specifies desired mipmap behaviour for the
1108 * allocation
1109 * @param usage bit field specifying how the allocation is
1110 * utilized
1111 *
1112 * @return renderscript allocation containing bitmap data
1113 *
1114 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001115 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001116 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001117 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001118 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001119 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001120
Jason Sams5476b452010-12-08 16:14:36 -08001121 int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
1122 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001123 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001124 }
Jason Sams5476b452010-12-08 16:14:36 -08001125 return new Allocation(id, rs, t, usage);
1126 }
1127
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001128 /**
Jason Sams615e7ce2012-01-13 14:01:20 -08001129 *
1130 *
1131 * @hide
1132 *
1133 */
1134 public SurfaceTexture getSurfaceTexture() {
1135 if ((mUsage & USAGE_GRAPHICS_SURFACE_TEXTURE_INPUT_OPAQUE) == 0) {
1136 throw new RSInvalidStateException("Allocation is not a surface texture.");
1137 }
1138
1139 int id = mRS.nAllocationGetSurfaceTextureID(getID());
1140 return new SurfaceTexture(id);
1141
1142 }
1143
Jason Sams163766c2012-02-15 12:04:24 -08001144 /**
1145 * @hide
1146 */
1147 public void setSurfaceTexture(SurfaceTexture sur) {
1148 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1149 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1150 }
1151
1152 mRS.validate();
1153 mRS.nAllocationSetSurfaceTexture(getID(), sur);
1154 }
1155
Jason Sams615e7ce2012-01-13 14:01:20 -08001156
1157 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001158 * Creates a non-mipmapped renderscript allocation to use as a
1159 * graphics texture
1160 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001161 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001162 * @param b bitmap source for the allocation data
1163 *
1164 * @return renderscript allocation containing bitmap data
1165 *
1166 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001167 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
1168 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1169 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001170 }
1171
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001172 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001173 * Creates a cubemap allocation from a bitmap containing the
1174 * horizontal list of cube faces. Each individual face must be
1175 * the same size and power of 2
1176 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001177 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001178 * @param b bitmap with cubemap faces layed out in the following
1179 * format: right, left, top, bottom, front, back
1180 * @param mips specifies desired mipmap behaviour for the cubemap
1181 * @param usage bit field specifying how the cubemap is utilized
1182 *
1183 * @return allocation containing cubemap data
1184 *
1185 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001186 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001187 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001188 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001189 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001190
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001191 int height = b.getHeight();
1192 int width = b.getWidth();
1193
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001194 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001195 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1196 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001197 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001198 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001199 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001200 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001201 if (!isPow2) {
1202 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1203 }
1204
1205 Element e = elementFromBitmap(rs, b);
1206 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001207 tb.setX(height);
1208 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001209 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001210 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001211 Type t = tb.create();
1212
Jason Sams5476b452010-12-08 16:14:36 -08001213 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001214 if(id == 0) {
1215 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1216 }
Jason Sams5476b452010-12-08 16:14:36 -08001217 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001218 }
1219
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001220 /**
1221 * Creates a non-mipmapped cubemap allocation for use as a
1222 * graphics texture from a bitmap containing the horizontal list
1223 * of cube faces. Each individual face must be the same size and
1224 * power of 2
1225 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001226 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001227 * @param b bitmap with cubemap faces layed out in the following
1228 * format: right, left, top, bottom, front, back
1229 *
1230 * @return allocation containing cubemap data
1231 *
1232 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001233 static public Allocation createCubemapFromBitmap(RenderScript rs,
1234 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001235 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001236 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001237 }
1238
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001239 /**
1240 * Creates a cubemap allocation from 6 bitmaps containing
1241 * the cube faces. All the faces must be the same size and
1242 * power of 2
1243 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001244 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001245 * @param xpos cubemap face in the positive x direction
1246 * @param xneg cubemap face in the negative x direction
1247 * @param ypos cubemap face in the positive y direction
1248 * @param yneg cubemap face in the negative y direction
1249 * @param zpos cubemap face in the positive z direction
1250 * @param zneg cubemap face in the negative z direction
1251 * @param mips specifies desired mipmap behaviour for the cubemap
1252 * @param usage bit field specifying how the cubemap is utilized
1253 *
1254 * @return allocation containing cubemap data
1255 *
1256 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001257 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1258 Bitmap xpos,
1259 Bitmap xneg,
1260 Bitmap ypos,
1261 Bitmap yneg,
1262 Bitmap zpos,
1263 Bitmap zneg,
1264 MipmapControl mips,
1265 int usage) {
1266 int height = xpos.getHeight();
1267 if (xpos.getWidth() != height ||
1268 xneg.getWidth() != height || xneg.getHeight() != height ||
1269 ypos.getWidth() != height || ypos.getHeight() != height ||
1270 yneg.getWidth() != height || yneg.getHeight() != height ||
1271 zpos.getWidth() != height || zpos.getHeight() != height ||
1272 zneg.getWidth() != height || zneg.getHeight() != height) {
1273 throw new RSIllegalArgumentException("Only square cube map faces supported");
1274 }
1275 boolean isPow2 = (height & (height - 1)) == 0;
1276 if (!isPow2) {
1277 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1278 }
1279
1280 Element e = elementFromBitmap(rs, xpos);
1281 Type.Builder tb = new Type.Builder(rs, e);
1282 tb.setX(height);
1283 tb.setY(height);
1284 tb.setFaces(true);
1285 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1286 Type t = tb.create();
1287 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1288
1289 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001290 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001291 adapter.copyFrom(xpos);
1292 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1293 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001294 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001295 adapter.copyFrom(ypos);
1296 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1297 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001298 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001299 adapter.copyFrom(zpos);
1300 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1301 adapter.copyFrom(zneg);
1302
1303 return cubemap;
1304 }
1305
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001306 /**
1307 * Creates a non-mipmapped cubemap allocation for use as a
1308 * graphics texture from 6 bitmaps containing
1309 * the cube faces. All the faces must be the same size and
1310 * power of 2
1311 *
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 xpos cubemap face in the positive x direction
1314 * @param xneg cubemap face in the negative x direction
1315 * @param ypos cubemap face in the positive y direction
1316 * @param yneg cubemap face in the negative y direction
1317 * @param zpos cubemap face in the positive z direction
1318 * @param zneg cubemap face in the negative z direction
1319 *
1320 * @return allocation containing cubemap data
1321 *
1322 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001323 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1324 Bitmap xpos,
1325 Bitmap xneg,
1326 Bitmap ypos,
1327 Bitmap yneg,
1328 Bitmap zpos,
1329 Bitmap zneg) {
1330 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1331 zpos, zneg, MipmapControl.MIPMAP_NONE,
1332 USAGE_GRAPHICS_TEXTURE);
1333 }
1334
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001335 /**
1336 * Creates a renderscript allocation from the bitmap referenced
1337 * by resource id
1338 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001339 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001340 * @param res application resources
1341 * @param id resource id to load the data from
1342 * @param mips specifies desired mipmap behaviour for the
1343 * allocation
1344 * @param usage bit field specifying how the allocation is
1345 * utilized
1346 *
1347 * @return renderscript allocation containing resource data
1348 *
1349 */
Jason Sams5476b452010-12-08 16:14:36 -08001350 static public Allocation createFromBitmapResource(RenderScript rs,
1351 Resources res,
1352 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001353 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001354 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001355
Jason Sams771bebb2009-12-07 12:40:12 -08001356 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001357 Bitmap b = BitmapFactory.decodeResource(res, id);
1358 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1359 b.recycle();
1360 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001361 }
1362
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001363 /**
1364 * Creates a non-mipmapped renderscript allocation to use as a
1365 * graphics texture from the bitmap referenced by resource id
1366 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001367 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001368 * @param res application resources
1369 * @param id resource id to load the data from
1370 *
1371 * @return renderscript allocation containing resource data
1372 *
1373 */
Jason Sams5476b452010-12-08 16:14:36 -08001374 static public Allocation createFromBitmapResource(RenderScript rs,
1375 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001376 int id) {
1377 return createFromBitmapResource(rs, res, id,
1378 MipmapControl.MIPMAP_NONE,
1379 USAGE_GRAPHICS_TEXTURE);
1380 }
1381
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001382 /**
1383 * Creates a renderscript allocation containing string data
1384 * encoded in UTF-8 format
1385 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001386 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001387 * @param str string to create the allocation from
1388 * @param usage bit field specifying how the allocaiton is
1389 * utilized
1390 *
1391 */
Jason Sams5476b452010-12-08 16:14:36 -08001392 static public Allocation createFromString(RenderScript rs,
1393 String str,
1394 int usage) {
1395 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001396 byte[] allocArray = null;
1397 try {
1398 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001399 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001400 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001401 return alloc;
1402 }
1403 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001404 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001405 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001406 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001407}
1408
1409