blob: 6539ff37472458e3b0d0b6da713a6d8e102c4e28 [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 Samsf70b0fc82012-02-22 15:22:41 -0800537 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700538 int eSize = mType.mElement.getSizeBytes();
539 final byte[] data = fp.getData();
540
541 int count = data.length / eSize;
542 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800543 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700544 " not divisible by element size " + eSize + ".");
545 }
Jason Samsba862d12011-07-07 15:24:42 -0700546 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700547 }
548
Jason Samsfa445b92011-01-07 17:00:07 -0800549 /**
Jason Samsfa445b92011-01-07 17:00:07 -0800550 * This is only intended to be used by auto-generate code reflected from the
551 * renderscript script files.
552 *
553 * @param xoff
554 * @param component_number
555 * @param fp
556 */
Jason Sams21b41032011-01-16 15:05:41 -0800557 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800558 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700559 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800560 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700561 }
562 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800563 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700564 }
565
566 final byte[] data = fp.getData();
567 int eSize = mType.mElement.mElements[component_number].getSizeBytes();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800568 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700569
570 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800571 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700572 " does not match component size " + eSize + ".");
573 }
574
Jason Sams48fe5342011-07-08 13:52:30 -0700575 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Jason Samsba862d12011-07-07 15:24:42 -0700576 component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700577 }
578
Jason Sams768bc022009-09-21 19:41:04 -0700579 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800580 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700581 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800582 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700583 }
584 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800585 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700586 }
Jason Samsba862d12011-07-07 15:24:42 -0700587 if((off + count) > mCurrentCount) {
588 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700589 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700590 }
Jason Samsba862d12011-07-07 15:24:42 -0700591 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800592 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700593 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700594 }
595
Jason Samsf7086092011-01-12 13:28:37 -0800596 /**
597 * Generate a mipmap chain. Requires the type of the allocation
598 * include mipmaps.
599 *
600 * This function will generate a complete set of mipmaps from
601 * the top level lod and place them into the script memoryspace.
602 *
603 * If the allocation is also using other memory spaces a
604 * followup sync will be required.
605 */
606 public void generateMipmaps() {
607 mRS.nAllocationGenerateMipmaps(getID());
608 }
609
Jason Sams4fa3eed2011-01-19 15:44:38 -0800610 /**
611 * Copy part of an allocation from an array. This variant is
612 * not type checked which allows an application to fill in
613 * structured data from an array.
614 *
615 * @param off The offset of the first element to be copied.
616 * @param count The number of elements to be copied.
617 * @param d the source data array
618 */
619 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700620 int dataSize = mType.mElement.getSizeBytes() * count;
621 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700622 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700623 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800624 /**
625 * Copy part of an allocation from an array. This variant is
626 * not type checked which allows an application to fill in
627 * structured data from an array.
628 *
629 * @param off The offset of the first element to be copied.
630 * @param count The number of elements to be copied.
631 * @param d the source data array
632 */
633 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700634 int dataSize = mType.mElement.getSizeBytes() * count;
635 data1DChecks(off, count, d.length * 2, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700636 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700637 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800638 /**
639 * Copy part of an allocation from an array. This variant is
640 * not type checked which allows an application to fill in
641 * structured data from an array.
642 *
643 * @param off The offset of the first element to be copied.
644 * @param count The number of elements to be copied.
645 * @param d the source data array
646 */
647 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700648 int dataSize = mType.mElement.getSizeBytes() * count;
649 data1DChecks(off, count, d.length, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700650 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700651 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800652 /**
653 * Copy part of an allocation from an array. This variant is
654 * not type checked which allows an application to fill in
655 * structured data from an array.
656 *
657 * @param off The offset of the first element to be copied.
658 * @param count The number of elements to be copied.
659 * @param d the source data array
660 */
661 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700662 int dataSize = mType.mElement.getSizeBytes() * count;
663 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700664 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700665 }
666
Jason Sams4fa3eed2011-01-19 15:44:38 -0800667 /**
668 * Copy part of an allocation from an array. This variant is
669 * type checked and will generate exceptions if the Allocation
670 * type is not a 32 bit integer type.
671 *
672 * @param off The offset of the first element to be copied.
673 * @param count The number of elements to be copied.
674 * @param d the source data array
675 */
Jason Samsb97b2512011-01-16 15:04:08 -0800676 public void copy1DRangeFrom(int off, int count, int[] d) {
677 validateIsInt32();
678 copy1DRangeFromUnchecked(off, count, d);
679 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800680
681 /**
682 * Copy part of an allocation from an array. This variant is
683 * type checked and will generate exceptions if the Allocation
684 * type is not a 16 bit integer type.
685 *
686 * @param off The offset of the first element to be copied.
687 * @param count The number of elements to be copied.
688 * @param d the source data array
689 */
Jason Samsb97b2512011-01-16 15:04:08 -0800690 public void copy1DRangeFrom(int off, int count, short[] d) {
691 validateIsInt16();
692 copy1DRangeFromUnchecked(off, count, d);
693 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800694
695 /**
696 * Copy part of an allocation from an array. This variant is
697 * type checked and will generate exceptions if the Allocation
698 * type is not a 8 bit integer type.
699 *
700 * @param off The offset of the first element to be copied.
701 * @param count The number of elements to be copied.
702 * @param d the source data array
703 */
Jason Samsb97b2512011-01-16 15:04:08 -0800704 public void copy1DRangeFrom(int off, int count, byte[] d) {
705 validateIsInt8();
706 copy1DRangeFromUnchecked(off, count, d);
707 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800708
709 /**
710 * Copy part of an allocation from an array. This variant is
711 * type checked and will generate exceptions if the Allocation
712 * type is not a 32 bit float type.
713 *
714 * @param off The offset of the first element to be copied.
715 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700716 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800717 */
Jason Samsb97b2512011-01-16 15:04:08 -0800718 public void copy1DRangeFrom(int off, int count, float[] d) {
719 validateIsFloat32();
720 copy1DRangeFromUnchecked(off, count, d);
721 }
722
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700723 /**
724 * Copy part of an allocation from another allocation.
725 *
726 * @param off The offset of the first element to be copied.
727 * @param count The number of elements to be copied.
728 * @param data the source data allocation.
729 * @param dataOff off The offset of the first element in data to
730 * be copied.
731 */
732 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Jason Sams48fe5342011-07-08 13:52:30 -0700733 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700734 mSelectedLOD, mSelectedFace.mID,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700735 count, 1, data.getID(), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700736 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700737 }
738
Jason Samsfb9f82c2011-01-12 14:53:25 -0800739 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -0700740 if (mAdaptedAllocation != null) {
741
742 } else {
743
744 if (xoff < 0 || yoff < 0) {
745 throw new RSIllegalArgumentException("Offset cannot be negative.");
746 }
747 if (h < 0 || w < 0) {
748 throw new RSIllegalArgumentException("Height or width cannot be negative.");
749 }
750 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
751 throw new RSIllegalArgumentException("Updated region larger than allocation.");
752 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800753 }
754 }
Jason Sams768bc022009-09-21 19:41:04 -0700755
Jason Samsf7086092011-01-12 13:28:37 -0800756 /**
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700757 * Copy a rectangular region from the array into the allocation.
758 * The incoming array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -0800759 *
760 * @param xoff X offset of the region to update
761 * @param yoff Y offset of the region to update
762 * @param w Width of the incoming region to update
763 * @param h Height of the incoming region to update
764 * @param data to be placed into the allocation
765 */
766 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800767 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800768 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700769 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700770 w, h, data, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -0800771 }
772
Jason Samsf7086092011-01-12 13:28:37 -0800773 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800774 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800775 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700776 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700777 w, h, data, data.length * 2);
Jason Samsfa445b92011-01-07 17:00:07 -0800778 }
779
Jason Samsf7086092011-01-12 13:28:37 -0800780 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800781 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800782 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700783 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700784 w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700785 }
786
Jason Samsf7086092011-01-12 13:28:37 -0800787 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800788 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800789 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700790 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700791 w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700792 }
793
Jason Samsf7086092011-01-12 13:28:37 -0800794 /**
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700795 * Copy a rectangular region into the allocation from another
796 * allocation.
797 *
798 * @param xoff X offset of the region to update.
799 * @param yoff Y offset of the region to update.
800 * @param w Width of the incoming region to update.
801 * @param h Height of the incoming region to update.
802 * @param data source allocation.
803 * @param dataXoff X offset in data of the region to update.
804 * @param dataYoff Y offset in data of the region to update.
805 */
806 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
807 Allocation data, int dataXoff, int dataYoff) {
808 mRS.validate();
809 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700810 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -0700811 mSelectedLOD, mSelectedFace.mID,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700812 w, h, data.getID(), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -0700813 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700814 }
815
816 /**
Jason Samsf7086092011-01-12 13:28:37 -0800817 * Copy a bitmap into an allocation. The height and width of
818 * the update will use the height and width of the incoming
819 * bitmap.
820 *
821 * @param xoff X offset of the region to update
822 * @param yoff Y offset of the region to update
823 * @param data the bitmap to be copied
824 */
825 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800826 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800827 validateBitmapFormat(data);
828 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -0700829 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Jason Samsfa445b92011-01-07 17:00:07 -0800830 }
831
832
Jason Sams48fe5342011-07-08 13:52:30 -0700833 /**
834 * Copy from the Allocation into a Bitmap. The bitmap must
835 * match the dimensions of the Allocation.
836 *
837 * @param b The bitmap to be set from the Allocation.
838 */
Jason Samsfa445b92011-01-07 17:00:07 -0800839 public void copyTo(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800840 mRS.validate();
841 validateBitmapFormat(b);
842 validateBitmapSize(b);
Jason Samsfa445b92011-01-07 17:00:07 -0800843 mRS.nAllocationCopyToBitmap(getID(), b);
844 }
845
Jason Sams48fe5342011-07-08 13:52:30 -0700846 /**
847 * Copy from the Allocation into a byte array. The array must
848 * be at least as large as the Allocation. The allocation must
849 * be of an 8 bit elemental type.
850 *
851 * @param d The array to be set from the Allocation.
852 */
Jason Samsfa445b92011-01-07 17:00:07 -0800853 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800854 validateIsInt8();
Jason Sams771bebb2009-12-07 12:40:12 -0800855 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800856 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700857 }
858
Jason Sams48fe5342011-07-08 13:52:30 -0700859 /**
860 * Copy from the Allocation into a short array. The array must
861 * be at least as large as the Allocation. The allocation must
862 * be of an 16 bit elemental type.
863 *
864 * @param d The array to be set from the Allocation.
865 */
Jason Samsfa445b92011-01-07 17:00:07 -0800866 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800867 validateIsInt16();
Jason Samsfa445b92011-01-07 17:00:07 -0800868 mRS.validate();
869 mRS.nAllocationRead(getID(), d);
870 }
871
Jason Sams48fe5342011-07-08 13:52:30 -0700872 /**
873 * Copy from the Allocation into a int array. The array must be
874 * at least as large as the Allocation. The allocation must be
875 * of an 32 bit elemental type.
876 *
877 * @param d The array to be set from the Allocation.
878 */
Jason Samsfa445b92011-01-07 17:00:07 -0800879 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800880 validateIsInt32();
Jason Samsfa445b92011-01-07 17:00:07 -0800881 mRS.validate();
882 mRS.nAllocationRead(getID(), d);
883 }
884
Jason Sams48fe5342011-07-08 13:52:30 -0700885 /**
886 * Copy from the Allocation into a float array. The array must
887 * be at least as large as the Allocation. The allocation must
888 * be of an 32 bit float elemental type.
889 *
890 * @param d The array to be set from the Allocation.
891 */
Jason Samsfa445b92011-01-07 17:00:07 -0800892 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800893 validateIsFloat32();
Jason Sams771bebb2009-12-07 12:40:12 -0800894 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800895 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700896 }
897
Jason Samsf7086092011-01-12 13:28:37 -0800898 /**
899 * Resize a 1D allocation. The contents of the allocation are
900 * preserved. If new elements are allocated objects are created
901 * with null contents and the new region is otherwise undefined.
902 *
903 * If the new region is smaller the references of any objects
904 * outside the new region will be released.
905 *
906 * A new type will be created with the new dimension.
907 *
908 * @param dimX The new size of the allocation.
909 */
Jason Sams31a7e422010-10-26 13:09:17 -0700910 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800911 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800912 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700913 }
Jason Sams06d69de2010-11-09 17:11:40 -0800914 mRS.nAllocationResize1D(getID(), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -0700915 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -0700916
Jason Sams06d69de2010-11-09 17:11:40 -0800917 int typeID = mRS.nAllocationGetType(getID());
Jason Sams31a7e422010-10-26 13:09:17 -0700918 mType = new Type(typeID, mRS);
919 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -0700920 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -0700921 }
922
Jason Sams163766c2012-02-15 12:04:24 -0800923 /**
924 * Resize a 2D allocation. The contents of the allocation are
925 * preserved. If new elements are allocated objects are created
926 * with null contents and the new region is otherwise undefined.
927 *
928 * If the new region is smaller the references of any objects
929 * outside the new region will be released.
930 *
931 * A new type will be created with the new dimension.
932 *
933 * @hide
934 * @param dimX The new size of the allocation.
935 * @param dimY The new size of the allocation.
936 */
Jason Sams5edc6082010-10-05 13:32:49 -0700937 public void resize(int dimX, int dimY) {
Jason Sams163766c2012-02-15 12:04:24 -0800938 if ((mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
939 throw new RSInvalidStateException(
940 "Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700941 }
942 if (mType.getY() == 0) {
Jason Sams163766c2012-02-15 12:04:24 -0800943 throw new RSInvalidStateException(
944 "Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700945 }
Jason Sams06d69de2010-11-09 17:11:40 -0800946 mRS.nAllocationResize2D(getID(), dimX, dimY);
Jason Sams163766c2012-02-15 12:04:24 -0800947 mRS.finish(); // Necessary because resize is fifoed and update is async.
948
949 int typeID = mRS.nAllocationGetType(getID());
950 mType = new Type(typeID, mRS);
951 mType.updateFromNative();
952 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -0700953 }
Jason Sams40a29e82009-08-10 14:55:26 -0700954
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700955
Jason Samsb8c5a842009-07-31 20:40:47 -0700956
957 // creation
958
Jason Sams49a05d72010-12-29 14:31:29 -0800959 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -0700960 static {
961 mBitmapOptions.inScaled = false;
962 }
963
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800964 /**
965 *
966 * @param type renderscript type describing data layout
967 * @param mips specifies desired mipmap behaviour for the
968 * allocation
969 * @param usage bit field specifying how the allocation is
970 * utilized
971 */
972 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800973 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800974 if (type.getID() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800975 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -0700976 }
Jason Sams857d0c72011-11-23 15:02:15 -0800977 int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage, 0);
978 if (id == 0) {
979 throw new RSRuntimeException("Allocation creation failed.");
980 }
981 return new Allocation(id, rs, type, usage);
982 }
983
984 /**
985 * @hide
986 * This API is hidden and only intended to be used for
987 * transitional purposes.
988 *
989 * @param type renderscript type describing data layout
990 * @param mips specifies desired mipmap behaviour for the
991 * allocation
992 * @param usage bit field specifying how the allocation is
993 * utilized
994 */
995 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips,
996 int usage, int pointer) {
997 rs.validate();
998 if (type.getID() == 0) {
999 throw new RSInvalidStateException("Bad Type");
1000 }
1001 int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage, pointer);
Jason Sams5476b452010-12-08 16:14:36 -08001002 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001003 throw new RSRuntimeException("Allocation creation failed.");
1004 }
Jason Sams5476b452010-12-08 16:14:36 -08001005 return new Allocation(id, rs, type, usage);
Jason Samsb8c5a842009-07-31 20:40:47 -07001006 }
1007
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001008 /**
1009 * Creates a renderscript allocation with the size specified by
1010 * the type and no mipmaps generated by default
1011 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001012 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001013 * @param type renderscript type describing data layout
1014 * @param usage bit field specifying how the allocation is
1015 * utilized
1016 *
1017 * @return allocation
1018 */
Jason Samse5d37122010-12-16 00:33:33 -08001019 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1020 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1021 }
1022
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001023 /**
1024 * Creates a renderscript allocation for use by the script with
1025 * the size specified by the type and no mipmaps generated by
1026 * default
1027 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001028 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001029 * @param type renderscript type describing data layout
1030 *
1031 * @return allocation
1032 */
Jason Sams5476b452010-12-08 16:14:36 -08001033 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001034 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001035 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001036
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001037 /**
1038 * Creates a renderscript allocation with a specified number of
1039 * given elements
1040 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001041 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001042 * @param e describes what each element of an allocation is
1043 * @param count specifies the number of element in the allocation
1044 * @param usage bit field specifying how the allocation is
1045 * utilized
1046 *
1047 * @return allocation
1048 */
Jason Sams5476b452010-12-08 16:14:36 -08001049 static public Allocation createSized(RenderScript rs, Element e,
1050 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001051 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001052 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001053 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001054 Type t = b.create();
1055
Jason Sams857d0c72011-11-23 15:02:15 -08001056 int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001057 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001058 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001059 }
Jason Sams5476b452010-12-08 16:14:36 -08001060 return new Allocation(id, rs, t, usage);
1061 }
1062
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001063 /**
1064 * Creates a renderscript allocation with a specified number of
1065 * given elements
1066 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001067 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001068 * @param e describes what each element of an allocation is
1069 * @param count specifies the number of element in the allocation
1070 *
1071 * @return allocation
1072 */
Jason Sams5476b452010-12-08 16:14:36 -08001073 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001074 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001075 }
1076
Jason Sams49a05d72010-12-29 14:31:29 -08001077 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001078 final Bitmap.Config bc = b.getConfig();
1079 if (bc == Bitmap.Config.ALPHA_8) {
1080 return Element.A_8(rs);
1081 }
1082 if (bc == Bitmap.Config.ARGB_4444) {
1083 return Element.RGBA_4444(rs);
1084 }
1085 if (bc == Bitmap.Config.ARGB_8888) {
1086 return Element.RGBA_8888(rs);
1087 }
1088 if (bc == Bitmap.Config.RGB_565) {
1089 return Element.RGB_565(rs);
1090 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001091 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001092 }
1093
Jason Sams49a05d72010-12-29 14:31:29 -08001094 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001095 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001096 Element e = elementFromBitmap(rs, b);
1097 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001098 tb.setX(b.getWidth());
1099 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001100 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001101 return tb.create();
1102 }
1103
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001104 /**
1105 * Creates a renderscript allocation from a bitmap
1106 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001107 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001108 * @param b bitmap source for the allocation data
1109 * @param mips specifies desired mipmap behaviour for the
1110 * allocation
1111 * @param usage bit field specifying how the allocation is
1112 * utilized
1113 *
1114 * @return renderscript allocation containing bitmap data
1115 *
1116 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001117 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001118 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001119 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001120 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001121 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001122
Jason Sams5476b452010-12-08 16:14:36 -08001123 int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
1124 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001125 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001126 }
Jason Sams5476b452010-12-08 16:14:36 -08001127 return new Allocation(id, rs, t, usage);
1128 }
1129
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001130 /**
Jason Sams615e7ce2012-01-13 14:01:20 -08001131 *
1132 *
1133 * @hide
1134 *
1135 */
1136 public SurfaceTexture getSurfaceTexture() {
1137 if ((mUsage & USAGE_GRAPHICS_SURFACE_TEXTURE_INPUT_OPAQUE) == 0) {
1138 throw new RSInvalidStateException("Allocation is not a surface texture.");
1139 }
1140
1141 int id = mRS.nAllocationGetSurfaceTextureID(getID());
1142 return new SurfaceTexture(id);
1143
1144 }
1145
Jason Sams163766c2012-02-15 12:04:24 -08001146 /**
1147 * @hide
1148 */
1149 public void setSurfaceTexture(SurfaceTexture sur) {
1150 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1151 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1152 }
1153
1154 mRS.validate();
1155 mRS.nAllocationSetSurfaceTexture(getID(), sur);
1156 }
1157
Jason Sams615e7ce2012-01-13 14:01:20 -08001158
1159 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001160 * Creates a non-mipmapped renderscript allocation to use as a
1161 * graphics texture
1162 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001163 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001164 * @param b bitmap source for the allocation data
1165 *
1166 * @return renderscript allocation containing bitmap data
1167 *
1168 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001169 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
1170 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1171 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001172 }
1173
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001174 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001175 * Creates a cubemap allocation from a bitmap containing the
1176 * horizontal list of cube faces. Each individual face must be
1177 * the same size and power of 2
1178 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001179 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001180 * @param b bitmap with cubemap faces layed out in the following
1181 * format: right, left, top, bottom, front, back
1182 * @param mips specifies desired mipmap behaviour for the cubemap
1183 * @param usage bit field specifying how the cubemap is utilized
1184 *
1185 * @return allocation containing cubemap data
1186 *
1187 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001188 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001189 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001190 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001191 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001192
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001193 int height = b.getHeight();
1194 int width = b.getWidth();
1195
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001196 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001197 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1198 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001199 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001200 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001201 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001202 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001203 if (!isPow2) {
1204 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1205 }
1206
1207 Element e = elementFromBitmap(rs, b);
1208 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001209 tb.setX(height);
1210 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001211 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001212 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001213 Type t = tb.create();
1214
Jason Sams5476b452010-12-08 16:14:36 -08001215 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001216 if(id == 0) {
1217 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1218 }
Jason Sams5476b452010-12-08 16:14:36 -08001219 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001220 }
1221
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001222 /**
1223 * Creates a non-mipmapped cubemap allocation for use as a
1224 * graphics texture from a bitmap containing the horizontal list
1225 * of cube faces. Each individual face must be the same size and
1226 * power of 2
1227 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001228 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001229 * @param b bitmap with cubemap faces layed out in the following
1230 * format: right, left, top, bottom, front, back
1231 *
1232 * @return allocation containing cubemap data
1233 *
1234 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001235 static public Allocation createCubemapFromBitmap(RenderScript rs,
1236 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001237 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001238 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001239 }
1240
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001241 /**
1242 * Creates a cubemap allocation from 6 bitmaps containing
1243 * the cube faces. All the faces must be the same size and
1244 * power of 2
1245 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001246 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001247 * @param xpos cubemap face in the positive x direction
1248 * @param xneg cubemap face in the negative x direction
1249 * @param ypos cubemap face in the positive y direction
1250 * @param yneg cubemap face in the negative y direction
1251 * @param zpos cubemap face in the positive z direction
1252 * @param zneg cubemap face in the negative z direction
1253 * @param mips specifies desired mipmap behaviour for the cubemap
1254 * @param usage bit field specifying how the cubemap is utilized
1255 *
1256 * @return allocation containing cubemap data
1257 *
1258 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001259 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1260 Bitmap xpos,
1261 Bitmap xneg,
1262 Bitmap ypos,
1263 Bitmap yneg,
1264 Bitmap zpos,
1265 Bitmap zneg,
1266 MipmapControl mips,
1267 int usage) {
1268 int height = xpos.getHeight();
1269 if (xpos.getWidth() != height ||
1270 xneg.getWidth() != height || xneg.getHeight() != height ||
1271 ypos.getWidth() != height || ypos.getHeight() != height ||
1272 yneg.getWidth() != height || yneg.getHeight() != height ||
1273 zpos.getWidth() != height || zpos.getHeight() != height ||
1274 zneg.getWidth() != height || zneg.getHeight() != height) {
1275 throw new RSIllegalArgumentException("Only square cube map faces supported");
1276 }
1277 boolean isPow2 = (height & (height - 1)) == 0;
1278 if (!isPow2) {
1279 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1280 }
1281
1282 Element e = elementFromBitmap(rs, xpos);
1283 Type.Builder tb = new Type.Builder(rs, e);
1284 tb.setX(height);
1285 tb.setY(height);
1286 tb.setFaces(true);
1287 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1288 Type t = tb.create();
1289 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1290
1291 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001292 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001293 adapter.copyFrom(xpos);
1294 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1295 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001296 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001297 adapter.copyFrom(ypos);
1298 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1299 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001300 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001301 adapter.copyFrom(zpos);
1302 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1303 adapter.copyFrom(zneg);
1304
1305 return cubemap;
1306 }
1307
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001308 /**
1309 * Creates a non-mipmapped cubemap allocation for use as a
1310 * graphics texture from 6 bitmaps containing
1311 * the cube faces. All the faces must be the same size and
1312 * power of 2
1313 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001314 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001315 * @param xpos cubemap face in the positive x direction
1316 * @param xneg cubemap face in the negative x direction
1317 * @param ypos cubemap face in the positive y direction
1318 * @param yneg cubemap face in the negative y direction
1319 * @param zpos cubemap face in the positive z direction
1320 * @param zneg cubemap face in the negative z direction
1321 *
1322 * @return allocation containing cubemap data
1323 *
1324 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001325 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1326 Bitmap xpos,
1327 Bitmap xneg,
1328 Bitmap ypos,
1329 Bitmap yneg,
1330 Bitmap zpos,
1331 Bitmap zneg) {
1332 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1333 zpos, zneg, MipmapControl.MIPMAP_NONE,
1334 USAGE_GRAPHICS_TEXTURE);
1335 }
1336
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001337 /**
1338 * Creates a renderscript allocation from the bitmap referenced
1339 * by resource id
1340 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001341 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001342 * @param res application resources
1343 * @param id resource id to load the data from
1344 * @param mips specifies desired mipmap behaviour for the
1345 * allocation
1346 * @param usage bit field specifying how the allocation is
1347 * utilized
1348 *
1349 * @return renderscript allocation containing resource data
1350 *
1351 */
Jason Sams5476b452010-12-08 16:14:36 -08001352 static public Allocation createFromBitmapResource(RenderScript rs,
1353 Resources res,
1354 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001355 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001356 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001357
Jason Sams771bebb2009-12-07 12:40:12 -08001358 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001359 Bitmap b = BitmapFactory.decodeResource(res, id);
1360 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1361 b.recycle();
1362 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001363 }
1364
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001365 /**
1366 * Creates a non-mipmapped renderscript allocation to use as a
1367 * graphics texture from the bitmap referenced by resource id
1368 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001369 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001370 * @param res application resources
1371 * @param id resource id to load the data from
1372 *
1373 * @return renderscript allocation containing resource data
1374 *
1375 */
Jason Sams5476b452010-12-08 16:14:36 -08001376 static public Allocation createFromBitmapResource(RenderScript rs,
1377 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001378 int id) {
1379 return createFromBitmapResource(rs, res, id,
1380 MipmapControl.MIPMAP_NONE,
1381 USAGE_GRAPHICS_TEXTURE);
1382 }
1383
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001384 /**
1385 * Creates a renderscript allocation containing string data
1386 * encoded in UTF-8 format
1387 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001388 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001389 * @param str string to create the allocation from
1390 * @param usage bit field specifying how the allocaiton is
1391 * utilized
1392 *
1393 */
Jason Sams5476b452010-12-08 16:14:36 -08001394 static public Allocation createFromString(RenderScript rs,
1395 String str,
1396 int usage) {
1397 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001398 byte[] allocArray = null;
1399 try {
1400 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001401 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001402 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001403 return alloc;
1404 }
1405 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001406 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001407 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001408 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001409}
1410
1411