blob: cd5300d87e5c9e1ecce43a11fab9c47aa0e71428 [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
Stephen Hines9069ee82012-02-13 18:25:54 -08002 * Copyright (C) 2008-2012 The Android Open Source Project
Jason Samsb8c5a842009-07-31 20:40:47 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
Jason Samsb8c5a842009-07-31 20:40:47 -070019import java.io.IOException;
20import java.io.InputStream;
Jason Samsb8c5a842009-07-31 20:40:47 -070021import android.content.res.Resources;
Romain Guy650a3eb2009-08-31 14:06:43 -070022import android.content.res.AssetManager;
Jason Samsb8c5a842009-07-31 20:40:47 -070023import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
Jason Samsfb9aa9f2012-03-28 15:30:07 -070025import android.view.Surface;
Jason Sams615e7ce2012-01-13 14:01:20 -080026import android.graphics.SurfaceTexture;
Jason Samsb8c5a842009-07-31 20:40:47 -070027import android.util.Log;
Romain Guy650a3eb2009-08-31 14:06:43 -070028import android.util.TypedValue;
Jason Samsb8c5a842009-07-31 20:40:47 -070029
30/**
Robert Ly11518ac2011-02-09 13:57:06 -080031 * <p>
32 * Memory allocation class for renderscript. An allocation combines a
33 * {@link android.renderscript.Type} with the memory to provide storage for user data and objects.
34 * This implies that all memory in Renderscript is typed.
35 * </p>
Jason Samsa23d4e72011-01-04 18:59:12 -080036 *
Robert Ly11518ac2011-02-09 13:57:06 -080037 * <p>Allocations are the primary way data moves into and out of scripts. Memory is user
38 * synchronized and it's possible for allocations to exist in multiple memory spaces
39 * concurrently. Currently those spaces are:</p>
40 * <ul>
41 * <li>Script: accessable by RS scripts.</li>
42 * <li>Graphics Texture: accessable as a graphics texture.</li>
43 * <li>Graphics Vertex: accessable as graphical vertex data.</li>
44 * <li>Graphics Constants: Accessable as constants in user shaders</li>
45 * </ul>
46 * </p>
47 * <p>
48 * For example, when creating a allocation for a texture, the user can
49 * specify its memory spaces as both script and textures. This means that it can both
50 * be used as script binding and as a GPU texture for rendering. To maintain
51 * synchronization if a script modifies an allocation used by other targets it must
52 * call a synchronizing function to push the updates to the memory, otherwise the results
53 * are undefined.
54 * </p>
55 * <p>By default, Android system side updates are always applied to the script accessable
56 * memory. If this is not present, they are then applied to the various HW
57 * memory types. A {@link android.renderscript.Allocation#syncAll syncAll()}
58 * call is necessary after the script data is updated to
59 * keep the other memory spaces in sync.</p>
Jason Samsa23d4e72011-01-04 18:59:12 -080060 *
Robert Ly11518ac2011-02-09 13:57:06 -080061 * <p>Allocation data is uploaded in one of two primary ways. For simple
62 * arrays there are copyFrom() functions that take an array from the control code and
63 * copy it to the slave memory store. Both type checked and unchecked copies are provided.
64 * The unchecked variants exist to allow apps to copy over arrays of structures from a
65 * control language that does not support structures.</p>
Jason Samsb8c5a842009-07-31 20:40:47 -070066 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080067 * <div class="special reference">
68 * <h3>Developer Guides</h3>
69 * <p>For more information about creating an application that uses Renderscript, read the
70 * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p>
71 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070072 **/
73public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070074 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080075 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080076 int mUsage;
Jason Samsba862d12011-07-07 15:24:42 -070077 Allocation mAdaptedAllocation;
78
79 boolean mConstrainedLOD;
80 boolean mConstrainedFace;
81 boolean mConstrainedY;
82 boolean mConstrainedZ;
Jason Sams615e7ce2012-01-13 14:01:20 -080083 boolean mReadAllowed = true;
84 boolean mWriteAllowed = true;
Jason Samsba862d12011-07-07 15:24:42 -070085 int mSelectedY;
86 int mSelectedZ;
87 int mSelectedLOD;
88 Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
89
90 int mCurrentDimX;
91 int mCurrentDimY;
92 int mCurrentDimZ;
93 int mCurrentCount;
94
Jason Sams5476b452010-12-08 16:14:36 -080095
Jason Samsf7086092011-01-12 13:28:37 -080096 /**
97 * The usage of the allocation. These signal to renderscript
98 * where to place the allocation in memory.
99 *
100 * SCRIPT The allocation will be bound to and accessed by
101 * scripts.
102 */
Jason Sams5476b452010-12-08 16:14:36 -0800103 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800104
105 /**
Jason Sams163766c2012-02-15 12:04:24 -0800106 * GRAPHICS_TEXTURE The allocation will be used as a texture
Stephen Hines836c4a52011-06-01 14:38:10 -0700107 * source by one or more graphics programs.
Jason Samsf7086092011-01-12 13:28:37 -0800108 *
109 */
Jason Sams5476b452010-12-08 16:14:36 -0800110 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -0800111
112 /**
113 * GRAPHICS_VERTEX The allocation will be used as a graphics
114 * mesh.
115 *
116 */
Jason Sams5476b452010-12-08 16:14:36 -0800117 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800118
119
120 /**
121 * GRAPHICS_CONSTANTS The allocation will be used as the source
122 * of shader constants by one or more programs.
123 *
124 */
Jason Sams5476b452010-12-08 16:14:36 -0800125 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
126
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700127 /**
Jason Sams163766c2012-02-15 12:04:24 -0800128 * USAGE_GRAPHICS_RENDER_TARGET The allocation will be used as a
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700129 * target for offscreen rendering
130 *
131 */
132 public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
133
Jason Sams615e7ce2012-01-13 14:01:20 -0800134 /**
Jason Sams163766c2012-02-15 12:04:24 -0800135 * USAGE_IO_INPUT The allocation will be used as SurfaceTexture
136 * consumer. This usage will cause the allocation to be created
137 * read only.
Jason Sams615e7ce2012-01-13 14:01:20 -0800138 *
139 * @hide
140 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700141 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800142
Jason Sams615e7ce2012-01-13 14:01:20 -0800143 /**
Jason Sams163766c2012-02-15 12:04:24 -0800144 * USAGE_IO_OUTPUT The allocation will be used as a
145 * SurfaceTexture producer. The dimensions and format of the
146 * SurfaceTexture will be forced to those of the allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800147 *
148 * @hide
149 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700150 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700151
Jason Samsf7086092011-01-12 13:28:37 -0800152 /**
153 * Controls mipmap behavior when using the bitmap creation and
154 * update functions.
155 */
Jason Sams4ef66502010-12-10 16:03:15 -0800156 public enum MipmapControl {
Jason Samsf7086092011-01-12 13:28:37 -0800157 /**
158 * No mipmaps will be generated and the type generated from the
159 * incoming bitmap will not contain additional LODs.
160 */
Jason Sams5476b452010-12-08 16:14:36 -0800161 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800162
163 /**
164 * A Full mipmap chain will be created in script memory. The
165 * type of the allocation will contain a full mipmap chain. On
166 * upload to graphics the full chain will be transfered.
167 */
Jason Sams5476b452010-12-08 16:14:36 -0800168 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800169
170 /**
171 * The type of the allocation will be the same as MIPMAP_NONE.
172 * It will not contain mipmaps. On upload to graphics the
173 * graphics copy of the allocation data will contain a full
174 * mipmap chain generated from the top level in script memory.
175 */
Jason Sams5476b452010-12-08 16:14:36 -0800176 MIPMAP_ON_SYNC_TO_TEXTURE(2);
177
178 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800179 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800180 mID = id;
181 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700182 }
183
Jason Sams48fe5342011-07-08 13:52:30 -0700184
185 private int getIDSafe() {
186 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700187 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700188 }
Jason Samse07694b2012-04-03 15:36:36 -0700189 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700190 }
191
Jason Sams03d2d002012-03-23 13:51:56 -0700192
193 /**
194 * Get the element of the type of the Allocation.
195 *
196 * @hide
197 * @return Element
198 *
199 */
200 public Element getElement() {
201 return mType.getElement();
202 }
203
204 /**
205 * Get the usage flags of the Allocation.
206 *
207 * @hide
208 * @return usage
209 *
210 */
211 public int getUsage() {
212 return mUsage;
213 }
214
Jason Sams36c0f642012-03-23 15:48:37 -0700215 /**
216 * Get the size of the Allocation in bytes.
217 *
218 * @hide
219 * @return sizeInBytes
220 *
221 */
222 public int getSizeBytes() {
223 return mType.getCount() * mType.getElement().getSizeBytes();
224 }
225
Jason Sams452a7662011-07-07 16:05:18 -0700226 private void updateCacheInfo(Type t) {
227 mCurrentDimX = t.getX();
228 mCurrentDimY = t.getY();
229 mCurrentDimZ = t.getZ();
230 mCurrentCount = mCurrentDimX;
231 if (mCurrentDimY > 1) {
232 mCurrentCount *= mCurrentDimY;
233 }
234 if (mCurrentDimZ > 1) {
235 mCurrentCount *= mCurrentDimZ;
236 }
237 }
Jason Samsba862d12011-07-07 15:24:42 -0700238
Jason Sams5476b452010-12-08 16:14:36 -0800239 Allocation(int id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700240 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800241 if ((usage & ~(USAGE_SCRIPT |
242 USAGE_GRAPHICS_TEXTURE |
243 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700244 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800245 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800246 USAGE_IO_INPUT |
247 USAGE_IO_OUTPUT)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800248 throw new RSIllegalArgumentException("Unknown usage specified.");
249 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800250
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700251 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800252 mWriteAllowed = false;
253
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700254 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800255 USAGE_GRAPHICS_TEXTURE |
256 USAGE_SCRIPT)) != 0) {
257 throw new RSIllegalArgumentException("Invalid usage combination.");
258 }
259 }
260
Jason Sams5476b452010-12-08 16:14:36 -0800261 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800262 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700263
Jason Sams452a7662011-07-07 16:05:18 -0700264 if (t != null) {
265 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700266 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700267 }
268
Jason Samsb97b2512011-01-16 15:04:08 -0800269 private void validateIsInt32() {
270 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
271 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
272 return;
273 }
274 throw new RSIllegalArgumentException(
275 "32 bit integer source does not match allocation type " + mType.mElement.mType);
276 }
277
278 private void validateIsInt16() {
279 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
280 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
281 return;
282 }
283 throw new RSIllegalArgumentException(
284 "16 bit integer source does not match allocation type " + mType.mElement.mType);
285 }
286
287 private void validateIsInt8() {
288 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
289 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
290 return;
291 }
292 throw new RSIllegalArgumentException(
293 "8 bit integer source does not match allocation type " + mType.mElement.mType);
294 }
295
296 private void validateIsFloat32() {
297 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
298 return;
299 }
300 throw new RSIllegalArgumentException(
301 "32 bit float source does not match allocation type " + mType.mElement.mType);
302 }
303
304 private void validateIsObject() {
305 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
306 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
307 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
308 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
309 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
310 (mType.mElement.mType == Element.DataType.RS_MESH) ||
311 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
312 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
313 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
314 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
315 return;
316 }
317 throw new RSIllegalArgumentException(
318 "Object source does not match allocation type " + mType.mElement.mType);
319 }
320
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700321 @Override
322 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800323 super.updateFromNative();
Jason Samse07694b2012-04-03 15:36:36 -0700324 int typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700325 if(typeID != 0) {
326 mType = new Type(typeID, mRS);
327 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700328 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700329 }
330 }
331
Jason Sams03d2d002012-03-23 13:51:56 -0700332 /**
333 * Get the type of the Allocation.
334 *
335 * @return Type
336 *
337 */
Jason Samsea87e962010-01-12 12:12:28 -0800338 public Type getType() {
339 return mType;
340 }
341
Jason Sams03d2d002012-03-23 13:51:56 -0700342 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700343 * Propagate changes from one usage of the allocation to the
Jason Sams03d2d002012-03-23 13:51:56 -0700344 * remaining usages of the allocation.
345 *
346 */
Jason Sams5476b452010-12-08 16:14:36 -0800347 public void syncAll(int srcLocation) {
348 switch (srcLocation) {
349 case USAGE_SCRIPT:
350 case USAGE_GRAPHICS_CONSTANTS:
351 case USAGE_GRAPHICS_TEXTURE:
352 case USAGE_GRAPHICS_VERTEX:
353 break;
354 default:
355 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
356 }
357 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700358 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Jason Sams5476b452010-12-08 16:14:36 -0800359 }
360
Jason Sams163766c2012-02-15 12:04:24 -0800361 /**
362 * Send a buffer to the output stream. The contents of the
363 * Allocation will be undefined after this operation.
364 *
365 * @hide
366 *
367 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700368 public void ioSend() {
Jason Sams163766c2012-02-15 12:04:24 -0800369 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
370 throw new RSIllegalArgumentException(
371 "Can only send buffer if IO_OUTPUT usage specified.");
372 }
373 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700374 mRS.nAllocationIoSend(getID(mRS));
Jason Sams163766c2012-02-15 12:04:24 -0800375 }
376
377 /**
Jason Samsc5f519c2012-03-29 17:58:15 -0700378 * Delete once code is updated.
379 * @hide
380 */
381 public void ioSendOutput() {
382 ioSend();
383 }
384
385 /**
Jason Sams163766c2012-02-15 12:04:24 -0800386 * Receive the latest input into the Allocation.
387 *
388 * @hide
389 *
390 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700391 public void ioReceive() {
Jason Sams163766c2012-02-15 12:04:24 -0800392 if ((mUsage & USAGE_IO_INPUT) == 0) {
393 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700394 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800395 }
396 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700397 mRS.nAllocationIoReceive(getID(mRS));
Jason Sams163766c2012-02-15 12:04:24 -0800398 }
399
Jason Sams03d2d002012-03-23 13:51:56 -0700400 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700401 * Copy an array of RS objects to the allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700402 *
403 * @param d Source array.
404 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800405 public void copyFrom(BaseObj[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800406 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800407 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700408 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800409 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700410 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800411 }
412 int i[] = new int[d.length];
413 for (int ct=0; ct < d.length; ct++) {
Jason Samse07694b2012-04-03 15:36:36 -0700414 i[ct] = d[ct].getID(mRS);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800415 }
Jason Samsba862d12011-07-07 15:24:42 -0700416 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Jason Samsb8c5a842009-07-31 20:40:47 -0700417 }
418
Jason Samsfb9f82c2011-01-12 14:53:25 -0800419 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800420 Bitmap.Config bc = b.getConfig();
421 switch (bc) {
422 case ALPHA_8:
423 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
424 throw new RSIllegalArgumentException("Allocation kind is " +
425 mType.getElement().mKind + ", type " +
426 mType.getElement().mType +
427 " of " + mType.getElement().getSizeBytes() +
428 " bytes, passed bitmap was " + bc);
429 }
430 break;
431 case ARGB_8888:
432 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
433 (mType.getElement().getSizeBytes() != 4)) {
434 throw new RSIllegalArgumentException("Allocation kind is " +
435 mType.getElement().mKind + ", type " +
436 mType.getElement().mType +
437 " of " + mType.getElement().getSizeBytes() +
438 " bytes, passed bitmap was " + bc);
439 }
440 break;
441 case RGB_565:
442 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
443 (mType.getElement().getSizeBytes() != 2)) {
444 throw new RSIllegalArgumentException("Allocation kind is " +
445 mType.getElement().mKind + ", type " +
446 mType.getElement().mType +
447 " of " + mType.getElement().getSizeBytes() +
448 " bytes, passed bitmap was " + bc);
449 }
450 break;
451 case ARGB_4444:
452 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
453 (mType.getElement().getSizeBytes() != 2)) {
454 throw new RSIllegalArgumentException("Allocation kind is " +
455 mType.getElement().mKind + ", type " +
456 mType.getElement().mType +
457 " of " + mType.getElement().getSizeBytes() +
458 " bytes, passed bitmap was " + bc);
459 }
460 break;
461
462 }
Jason Sams4ef66502010-12-10 16:03:15 -0800463 }
464
Jason Samsfb9f82c2011-01-12 14:53:25 -0800465 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700466 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800467 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
468 }
469 }
470
Jason Sams4fa3eed2011-01-19 15:44:38 -0800471 /**
472 * Copy an allocation from an array. This variant is not type
473 * checked which allows an application to fill in structured
474 * data from an array.
475 *
476 * @param d the source data array
477 */
478 public void copyFromUnchecked(int[] d) {
479 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700480 copy1DRangeFromUnchecked(0, mCurrentCount, d);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800481 }
482 /**
483 * Copy an allocation from an array. This variant is not type
484 * checked which allows an application to fill in structured
485 * data from an array.
486 *
487 * @param d the source data array
488 */
489 public void copyFromUnchecked(short[] d) {
490 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700491 copy1DRangeFromUnchecked(0, mCurrentCount, d);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800492 }
493 /**
494 * Copy an allocation from an array. This variant is not type
495 * checked which allows an application to fill in structured
496 * data from an array.
497 *
498 * @param d the source data array
499 */
500 public void copyFromUnchecked(byte[] d) {
501 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700502 copy1DRangeFromUnchecked(0, mCurrentCount, d);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800503 }
504 /**
505 * Copy an allocation from an array. This variant is not type
506 * checked which allows an application to fill in structured
507 * data from an array.
508 *
509 * @param d the source data array
510 */
511 public void copyFromUnchecked(float[] d) {
512 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700513 copy1DRangeFromUnchecked(0, mCurrentCount, d);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800514 }
515
516 /**
517 * Copy an allocation from an array. This variant is type
518 * checked and will generate exceptions if the Allocation type
519 * is not a 32 bit integer type.
520 *
521 * @param d the source data array
522 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800523 public void copyFrom(int[] d) {
524 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700525 copy1DRangeFrom(0, mCurrentCount, d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800526 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800527
528 /**
529 * Copy an allocation from an array. This variant is type
530 * checked and will generate exceptions if the Allocation type
531 * is not a 16 bit integer type.
532 *
533 * @param d the source data array
534 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800535 public void copyFrom(short[] d) {
536 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700537 copy1DRangeFrom(0, mCurrentCount, d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800538 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800539
540 /**
541 * Copy an allocation from an array. This variant is type
542 * checked and will generate exceptions if the Allocation type
543 * is not a 8 bit integer type.
544 *
545 * @param d the source data array
546 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800547 public void copyFrom(byte[] d) {
548 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700549 copy1DRangeFrom(0, mCurrentCount, d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800550 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800551
552 /**
553 * Copy an allocation from an array. This variant is type
554 * checked and will generate exceptions if the Allocation type
555 * is not a 32 bit float type.
556 *
557 * @param d the source data array
558 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800559 public void copyFrom(float[] d) {
560 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700561 copy1DRangeFrom(0, mCurrentCount, d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800562 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800563
564 /**
565 * Copy an allocation from a bitmap. The height, width, and
566 * format of the bitmap must match the existing allocation.
567 *
568 * @param b the source bitmap
569 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800570 public void copyFrom(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800571 mRS.validate();
572 validateBitmapSize(b);
573 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700574 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700575 }
576
Jason Samsfa445b92011-01-07 17:00:07 -0800577 /**
Jason Samsfa445b92011-01-07 17:00:07 -0800578 * This is only intended to be used by auto-generate code reflected from the
579 * renderscript script files.
580 *
581 * @param xoff
582 * @param fp
583 */
Jason Sams21b41032011-01-16 15:05:41 -0800584 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800585 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700586 int eSize = mType.mElement.getSizeBytes();
587 final byte[] data = fp.getData();
588
589 int count = data.length / eSize;
590 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800591 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700592 " not divisible by element size " + eSize + ".");
593 }
Jason Samsba862d12011-07-07 15:24:42 -0700594 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700595 }
596
Jason Samsfa445b92011-01-07 17:00:07 -0800597 /**
Jason Samsfa445b92011-01-07 17:00:07 -0800598 * This is only intended to be used by auto-generate code reflected from the
599 * renderscript script files.
600 *
601 * @param xoff
602 * @param component_number
603 * @param fp
604 */
Jason Sams21b41032011-01-16 15:05:41 -0800605 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800606 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700607 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800608 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700609 }
610 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800611 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700612 }
613
614 final byte[] data = fp.getData();
615 int eSize = mType.mElement.mElements[component_number].getSizeBytes();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800616 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700617
618 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800619 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700620 " does not match component size " + eSize + ".");
621 }
622
Jason Sams48fe5342011-07-08 13:52:30 -0700623 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Jason Samsba862d12011-07-07 15:24:42 -0700624 component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700625 }
626
Jason Sams768bc022009-09-21 19:41:04 -0700627 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800628 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700629 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800630 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700631 }
632 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800633 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700634 }
Jason Samsba862d12011-07-07 15:24:42 -0700635 if((off + count) > mCurrentCount) {
636 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700637 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700638 }
Jason Samsba862d12011-07-07 15:24:42 -0700639 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800640 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700641 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700642 }
643
Jason Samsf7086092011-01-12 13:28:37 -0800644 /**
645 * Generate a mipmap chain. Requires the type of the allocation
646 * include mipmaps.
647 *
648 * This function will generate a complete set of mipmaps from
649 * the top level lod and place them into the script memoryspace.
650 *
651 * If the allocation is also using other memory spaces a
652 * followup sync will be required.
653 */
654 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700655 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800656 }
657
Jason Sams4fa3eed2011-01-19 15:44:38 -0800658 /**
659 * Copy part of an allocation from an array. This variant is
660 * not type checked which allows an application to fill in
661 * structured data from an array.
662 *
663 * @param off The offset of the first element to be copied.
664 * @param count The number of elements to be copied.
665 * @param d the source data array
666 */
667 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700668 int dataSize = mType.mElement.getSizeBytes() * count;
669 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700670 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700671 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800672 /**
673 * Copy part of an allocation from an array. This variant is
674 * not type checked which allows an application to fill in
675 * structured data from an array.
676 *
677 * @param off The offset of the first element to be copied.
678 * @param count The number of elements to be copied.
679 * @param d the source data array
680 */
681 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700682 int dataSize = mType.mElement.getSizeBytes() * count;
683 data1DChecks(off, count, d.length * 2, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700684 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700685 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800686 /**
687 * Copy part of an allocation from an array. This variant is
688 * not type checked which allows an application to fill in
689 * structured data from an array.
690 *
691 * @param off The offset of the first element to be copied.
692 * @param count The number of elements to be copied.
693 * @param d the source data array
694 */
695 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700696 int dataSize = mType.mElement.getSizeBytes() * count;
697 data1DChecks(off, count, d.length, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700698 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700699 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800700 /**
701 * Copy part of an allocation from an array. This variant is
702 * not type checked which allows an application to fill in
703 * structured data from an array.
704 *
705 * @param off The offset of the first element to be copied.
706 * @param count The number of elements to be copied.
707 * @param d the source data array
708 */
709 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700710 int dataSize = mType.mElement.getSizeBytes() * count;
711 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700712 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700713 }
714
Jason Sams4fa3eed2011-01-19 15:44:38 -0800715 /**
716 * Copy part of an allocation from an array. This variant is
717 * type checked and will generate exceptions if the Allocation
718 * type is not a 32 bit integer type.
719 *
720 * @param off The offset of the first element to be copied.
721 * @param count The number of elements to be copied.
722 * @param d the source data array
723 */
Jason Samsb97b2512011-01-16 15:04:08 -0800724 public void copy1DRangeFrom(int off, int count, int[] d) {
725 validateIsInt32();
726 copy1DRangeFromUnchecked(off, count, d);
727 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800728
729 /**
730 * Copy part of an allocation from an array. This variant is
731 * type checked and will generate exceptions if the Allocation
732 * type is not a 16 bit integer type.
733 *
734 * @param off The offset of the first element to be copied.
735 * @param count The number of elements to be copied.
736 * @param d the source data array
737 */
Jason Samsb97b2512011-01-16 15:04:08 -0800738 public void copy1DRangeFrom(int off, int count, short[] d) {
739 validateIsInt16();
740 copy1DRangeFromUnchecked(off, count, d);
741 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800742
743 /**
744 * Copy part of an allocation from an array. This variant is
745 * type checked and will generate exceptions if the Allocation
746 * type is not a 8 bit integer type.
747 *
748 * @param off The offset of the first element to be copied.
749 * @param count The number of elements to be copied.
750 * @param d the source data array
751 */
Jason Samsb97b2512011-01-16 15:04:08 -0800752 public void copy1DRangeFrom(int off, int count, byte[] d) {
753 validateIsInt8();
754 copy1DRangeFromUnchecked(off, count, d);
755 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800756
757 /**
758 * Copy part of an allocation from an array. This variant is
759 * type checked and will generate exceptions if the Allocation
760 * type is not a 32 bit float type.
761 *
762 * @param off The offset of the first element to be copied.
763 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700764 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800765 */
Jason Samsb97b2512011-01-16 15:04:08 -0800766 public void copy1DRangeFrom(int off, int count, float[] d) {
767 validateIsFloat32();
768 copy1DRangeFromUnchecked(off, count, d);
769 }
770
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700771 /**
772 * Copy part of an allocation from another allocation.
773 *
774 * @param off The offset of the first element to be copied.
775 * @param count The number of elements to be copied.
776 * @param data the source data allocation.
777 * @param dataOff off The offset of the first element in data to
778 * be copied.
779 */
780 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Jason Sams48fe5342011-07-08 13:52:30 -0700781 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700782 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700783 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700784 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700785 }
786
Jason Samsfb9f82c2011-01-12 14:53:25 -0800787 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -0700788 if (mAdaptedAllocation != null) {
789
790 } else {
791
792 if (xoff < 0 || yoff < 0) {
793 throw new RSIllegalArgumentException("Offset cannot be negative.");
794 }
795 if (h < 0 || w < 0) {
796 throw new RSIllegalArgumentException("Height or width cannot be negative.");
797 }
798 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
799 throw new RSIllegalArgumentException("Updated region larger than allocation.");
800 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800801 }
802 }
Jason Sams768bc022009-09-21 19:41:04 -0700803
Jason Samsf7086092011-01-12 13:28:37 -0800804 /**
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700805 * Copy a rectangular region from the array into the allocation.
806 * The incoming array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -0800807 *
808 * @param xoff X offset of the region to update
809 * @param yoff Y offset of the region to update
810 * @param w Width of the incoming region to update
811 * @param h Height of the incoming region to update
812 * @param data to be placed into the allocation
813 */
814 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800815 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800816 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700817 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700818 w, h, data, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -0800819 }
820
Jason Samsf7086092011-01-12 13:28:37 -0800821 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800822 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800823 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700824 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700825 w, h, data, data.length * 2);
Jason Samsfa445b92011-01-07 17:00:07 -0800826 }
827
Jason Samsf7086092011-01-12 13:28:37 -0800828 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800829 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800830 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700831 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700832 w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700833 }
834
Jason Samsf7086092011-01-12 13:28:37 -0800835 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800836 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800837 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700838 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700839 w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700840 }
841
Jason Samsf7086092011-01-12 13:28:37 -0800842 /**
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700843 * Copy a rectangular region into the allocation from another
844 * allocation.
845 *
846 * @param xoff X offset of the region to update.
847 * @param yoff Y offset of the region to update.
848 * @param w Width of the incoming region to update.
849 * @param h Height of the incoming region to update.
850 * @param data source allocation.
851 * @param dataXoff X offset in data of the region to update.
852 * @param dataYoff Y offset in data of the region to update.
853 */
854 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
855 Allocation data, int dataXoff, int dataYoff) {
856 mRS.validate();
857 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700858 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -0700859 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700860 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -0700861 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700862 }
863
864 /**
Jason Samsf7086092011-01-12 13:28:37 -0800865 * Copy a bitmap into an allocation. The height and width of
866 * the update will use the height and width of the incoming
867 * bitmap.
868 *
869 * @param xoff X offset of the region to update
870 * @param yoff Y offset of the region to update
871 * @param data the bitmap to be copied
872 */
873 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800874 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800875 validateBitmapFormat(data);
876 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -0700877 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Jason Samsfa445b92011-01-07 17:00:07 -0800878 }
879
880
Jason Sams48fe5342011-07-08 13:52:30 -0700881 /**
882 * Copy from the Allocation into a Bitmap. The bitmap must
883 * match the dimensions of the Allocation.
884 *
885 * @param b The bitmap to be set from the Allocation.
886 */
Jason Samsfa445b92011-01-07 17:00:07 -0800887 public void copyTo(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800888 mRS.validate();
889 validateBitmapFormat(b);
890 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -0700891 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Jason Samsfa445b92011-01-07 17:00:07 -0800892 }
893
Jason Sams48fe5342011-07-08 13:52:30 -0700894 /**
895 * Copy from the Allocation into a byte array. The array must
896 * be at least as large as the Allocation. The allocation must
897 * be of an 8 bit elemental type.
898 *
899 * @param d The array to be set from the Allocation.
900 */
Jason Samsfa445b92011-01-07 17:00:07 -0800901 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800902 validateIsInt8();
Jason Sams771bebb2009-12-07 12:40:12 -0800903 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700904 mRS.nAllocationRead(getID(mRS), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700905 }
906
Jason Sams48fe5342011-07-08 13:52:30 -0700907 /**
908 * Copy from the Allocation into a short array. The array must
909 * be at least as large as the Allocation. The allocation must
910 * be of an 16 bit elemental type.
911 *
912 * @param d The array to be set from the Allocation.
913 */
Jason Samsfa445b92011-01-07 17:00:07 -0800914 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800915 validateIsInt16();
Jason Samsfa445b92011-01-07 17:00:07 -0800916 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700917 mRS.nAllocationRead(getID(mRS), d);
Jason Samsfa445b92011-01-07 17:00:07 -0800918 }
919
Jason Sams48fe5342011-07-08 13:52:30 -0700920 /**
921 * Copy from the Allocation into a int array. The array must be
922 * at least as large as the Allocation. The allocation must be
923 * of an 32 bit elemental type.
924 *
925 * @param d The array to be set from the Allocation.
926 */
Jason Samsfa445b92011-01-07 17:00:07 -0800927 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800928 validateIsInt32();
Jason Samsfa445b92011-01-07 17:00:07 -0800929 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700930 mRS.nAllocationRead(getID(mRS), d);
Jason Samsfa445b92011-01-07 17:00:07 -0800931 }
932
Jason Sams48fe5342011-07-08 13:52:30 -0700933 /**
934 * Copy from the Allocation into a float array. The array must
935 * be at least as large as the Allocation. The allocation must
936 * be of an 32 bit float elemental type.
937 *
938 * @param d The array to be set from the Allocation.
939 */
Jason Samsfa445b92011-01-07 17:00:07 -0800940 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800941 validateIsFloat32();
Jason Sams771bebb2009-12-07 12:40:12 -0800942 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700943 mRS.nAllocationRead(getID(mRS), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700944 }
945
Jason Samsf7086092011-01-12 13:28:37 -0800946 /**
947 * Resize a 1D allocation. The contents of the allocation are
948 * preserved. If new elements are allocated objects are created
949 * with null contents and the new region is otherwise undefined.
950 *
951 * If the new region is smaller the references of any objects
952 * outside the new region will be released.
953 *
954 * A new type will be created with the new dimension.
955 *
956 * @param dimX The new size of the allocation.
957 */
Jason Sams31a7e422010-10-26 13:09:17 -0700958 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800959 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800960 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700961 }
Jason Samse07694b2012-04-03 15:36:36 -0700962 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -0700963 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -0700964
Jason Samse07694b2012-04-03 15:36:36 -0700965 int typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -0700966 mType = new Type(typeID, mRS);
967 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -0700968 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -0700969 }
970
Jason Sams163766c2012-02-15 12:04:24 -0800971 /**
972 * Resize a 2D allocation. The contents of the allocation are
973 * preserved. If new elements are allocated objects are created
974 * with null contents and the new region is otherwise undefined.
975 *
976 * If the new region is smaller the references of any objects
977 * outside the new region will be released.
978 *
979 * A new type will be created with the new dimension.
980 *
981 * @hide
982 * @param dimX The new size of the allocation.
983 * @param dimY The new size of the allocation.
984 */
Jason Sams5edc6082010-10-05 13:32:49 -0700985 public void resize(int dimX, int dimY) {
Jason Sams163766c2012-02-15 12:04:24 -0800986 if ((mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
987 throw new RSInvalidStateException(
988 "Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700989 }
990 if (mType.getY() == 0) {
Jason Sams163766c2012-02-15 12:04:24 -0800991 throw new RSInvalidStateException(
992 "Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700993 }
Jason Samse07694b2012-04-03 15:36:36 -0700994 mRS.nAllocationResize2D(getID(mRS), dimX, dimY);
Jason Sams163766c2012-02-15 12:04:24 -0800995 mRS.finish(); // Necessary because resize is fifoed and update is async.
996
Jason Samse07694b2012-04-03 15:36:36 -0700997 int typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams163766c2012-02-15 12:04:24 -0800998 mType = new Type(typeID, mRS);
999 mType.updateFromNative();
1000 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001001 }
Jason Sams40a29e82009-08-10 14:55:26 -07001002
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001003
Jason Samsb8c5a842009-07-31 20:40:47 -07001004
1005 // creation
1006
Jason Sams49a05d72010-12-29 14:31:29 -08001007 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001008 static {
1009 mBitmapOptions.inScaled = false;
1010 }
1011
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001012 /**
1013 *
1014 * @param type renderscript type describing data layout
1015 * @param mips specifies desired mipmap behaviour for the
1016 * allocation
1017 * @param usage bit field specifying how the allocation is
1018 * utilized
1019 */
1020 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001021 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001022 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001023 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001024 }
Jason Samse07694b2012-04-03 15:36:36 -07001025 int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001026 if (id == 0) {
1027 throw new RSRuntimeException("Allocation creation failed.");
1028 }
1029 return new Allocation(id, rs, type, usage);
1030 }
1031
1032 /**
1033 * @hide
1034 * This API is hidden and only intended to be used for
1035 * transitional purposes.
1036 *
1037 * @param type renderscript type describing data layout
1038 * @param mips specifies desired mipmap behaviour for the
1039 * allocation
1040 * @param usage bit field specifying how the allocation is
1041 * utilized
1042 */
1043 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips,
1044 int usage, int pointer) {
1045 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001046 if (type.getID(rs) == 0) {
Jason Sams857d0c72011-11-23 15:02:15 -08001047 throw new RSInvalidStateException("Bad Type");
1048 }
Jason Samse07694b2012-04-03 15:36:36 -07001049 int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, pointer);
Jason Sams5476b452010-12-08 16:14:36 -08001050 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001051 throw new RSRuntimeException("Allocation creation failed.");
1052 }
Jason Sams5476b452010-12-08 16:14:36 -08001053 return new Allocation(id, rs, type, usage);
Jason Samsb8c5a842009-07-31 20:40:47 -07001054 }
1055
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001056 /**
1057 * Creates a renderscript allocation with the size specified by
1058 * the type and no mipmaps generated by default
1059 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001060 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001061 * @param type renderscript type describing data layout
1062 * @param usage bit field specifying how the allocation is
1063 * utilized
1064 *
1065 * @return allocation
1066 */
Jason Samse5d37122010-12-16 00:33:33 -08001067 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1068 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1069 }
1070
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001071 /**
1072 * Creates a renderscript allocation for use by the script with
1073 * the size specified by the type and no mipmaps generated by
1074 * default
1075 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001076 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001077 * @param type renderscript type describing data layout
1078 *
1079 * @return allocation
1080 */
Jason Sams5476b452010-12-08 16:14:36 -08001081 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001082 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001083 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001084
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001085 /**
1086 * Creates a renderscript allocation with a specified number of
1087 * given elements
1088 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001089 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001090 * @param e describes what each element of an allocation is
1091 * @param count specifies the number of element in the allocation
1092 * @param usage bit field specifying how the allocation is
1093 * utilized
1094 *
1095 * @return allocation
1096 */
Jason Sams5476b452010-12-08 16:14:36 -08001097 static public Allocation createSized(RenderScript rs, Element e,
1098 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001099 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001100 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001101 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001102 Type t = b.create();
1103
Jason Samse07694b2012-04-03 15:36:36 -07001104 int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001105 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001106 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001107 }
Jason Sams5476b452010-12-08 16:14:36 -08001108 return new Allocation(id, rs, t, usage);
1109 }
1110
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001111 /**
1112 * Creates a renderscript allocation with a specified number of
1113 * given elements
1114 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001115 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001116 * @param e describes what each element of an allocation is
1117 * @param count specifies the number of element in the allocation
1118 *
1119 * @return allocation
1120 */
Jason Sams5476b452010-12-08 16:14:36 -08001121 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001122 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001123 }
1124
Jason Sams49a05d72010-12-29 14:31:29 -08001125 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001126 final Bitmap.Config bc = b.getConfig();
1127 if (bc == Bitmap.Config.ALPHA_8) {
1128 return Element.A_8(rs);
1129 }
1130 if (bc == Bitmap.Config.ARGB_4444) {
1131 return Element.RGBA_4444(rs);
1132 }
1133 if (bc == Bitmap.Config.ARGB_8888) {
1134 return Element.RGBA_8888(rs);
1135 }
1136 if (bc == Bitmap.Config.RGB_565) {
1137 return Element.RGB_565(rs);
1138 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001139 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001140 }
1141
Jason Sams49a05d72010-12-29 14:31:29 -08001142 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001143 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001144 Element e = elementFromBitmap(rs, b);
1145 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001146 tb.setX(b.getWidth());
1147 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001148 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001149 return tb.create();
1150 }
1151
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001152 /**
1153 * Creates a renderscript allocation from a bitmap
1154 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001155 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001156 * @param b bitmap source for the allocation data
1157 * @param mips specifies desired mipmap behaviour for the
1158 * allocation
1159 * @param usage bit field specifying how the allocation is
1160 * utilized
1161 *
1162 * @return renderscript allocation containing bitmap data
1163 *
1164 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001165 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001166 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001167 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001168 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001169 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001170
Jason Samse07694b2012-04-03 15:36:36 -07001171 int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001172 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001173 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001174 }
Jason Sams5476b452010-12-08 16:14:36 -08001175 return new Allocation(id, rs, t, usage);
1176 }
1177
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001178 /**
Jason Sams615e7ce2012-01-13 14:01:20 -08001179 *
1180 *
1181 * @hide
1182 *
1183 */
1184 public SurfaceTexture getSurfaceTexture() {
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001185 if ((mUsage & USAGE_IO_INPUT) == 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -08001186 throw new RSInvalidStateException("Allocation is not a surface texture.");
1187 }
1188
Jason Samse07694b2012-04-03 15:36:36 -07001189 int id = mRS.nAllocationGetSurfaceTextureID(getID(mRS));
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001190 SurfaceTexture st = new SurfaceTexture(id);
Jason Samse07694b2012-04-03 15:36:36 -07001191 mRS.nAllocationGetSurfaceTextureID2(getID(mRS), st);
Jason Sams615e7ce2012-01-13 14:01:20 -08001192
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001193 return st;
Jason Sams615e7ce2012-01-13 14:01:20 -08001194 }
1195
Jason Sams163766c2012-02-15 12:04:24 -08001196 /**
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001197 *
1198 * @hide
1199 *
1200 */
1201 public Surface getSurface() {
1202 return new Surface(getSurfaceTexture());
1203 }
1204
1205 /**
Jason Sams163766c2012-02-15 12:04:24 -08001206 * @hide
1207 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001208 public void setSurface(Surface sur) {
1209 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001210 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1211 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1212 }
1213
Jason Samse07694b2012-04-03 15:36:36 -07001214 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001215 }
1216
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001217 /**
1218 * @hide
1219 */
1220 public void setSurfaceTexture(SurfaceTexture st) {
1221 mRS.validate();
1222 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1223 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1224 }
1225
1226 Surface s = new Surface(st);
Jason Samse07694b2012-04-03 15:36:36 -07001227 mRS.nAllocationSetSurface(getID(mRS), s);
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001228 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001229
1230 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001231 * Creates a non-mipmapped renderscript allocation to use as a
1232 * graphics texture
1233 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001234 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001235 * @param b bitmap source for the allocation data
1236 *
1237 * @return renderscript allocation containing bitmap data
1238 *
1239 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001240 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
1241 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1242 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001243 }
1244
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001245 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001246 * Creates a cubemap allocation from a bitmap containing the
1247 * horizontal list of cube faces. Each individual face must be
1248 * the same size and power of 2
1249 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001250 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001251 * @param b bitmap with cubemap faces layed out in the following
1252 * format: right, left, top, bottom, front, back
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 Sakhartchouk67f2e442010-11-18 15:22:43 -08001259 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001260 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001261 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001262 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001263
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001264 int height = b.getHeight();
1265 int width = b.getWidth();
1266
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001267 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001268 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1269 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001270 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001271 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001272 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001273 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001274 if (!isPow2) {
1275 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1276 }
1277
1278 Element e = elementFromBitmap(rs, b);
1279 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001280 tb.setX(height);
1281 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001282 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001283 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001284 Type t = tb.create();
1285
Jason Samse07694b2012-04-03 15:36:36 -07001286 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001287 if(id == 0) {
1288 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1289 }
Jason Sams5476b452010-12-08 16:14:36 -08001290 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001291 }
1292
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001293 /**
1294 * Creates a non-mipmapped cubemap allocation for use as a
1295 * graphics texture from a bitmap containing the horizontal list
1296 * of cube faces. Each individual face must be the same size and
1297 * power of 2
1298 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001299 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001300 * @param b bitmap with cubemap faces layed out in the following
1301 * format: right, left, top, bottom, front, back
1302 *
1303 * @return allocation containing cubemap data
1304 *
1305 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001306 static public Allocation createCubemapFromBitmap(RenderScript rs,
1307 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001308 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001309 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001310 }
1311
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001312 /**
1313 * Creates a cubemap allocation from 6 bitmaps containing
1314 * the cube faces. All the faces must be the same size and
1315 * power of 2
1316 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001317 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001318 * @param xpos cubemap face in the positive x direction
1319 * @param xneg cubemap face in the negative x direction
1320 * @param ypos cubemap face in the positive y direction
1321 * @param yneg cubemap face in the negative y direction
1322 * @param zpos cubemap face in the positive z direction
1323 * @param zneg cubemap face in the negative z direction
1324 * @param mips specifies desired mipmap behaviour for the cubemap
1325 * @param usage bit field specifying how the cubemap is utilized
1326 *
1327 * @return allocation containing cubemap data
1328 *
1329 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001330 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1331 Bitmap xpos,
1332 Bitmap xneg,
1333 Bitmap ypos,
1334 Bitmap yneg,
1335 Bitmap zpos,
1336 Bitmap zneg,
1337 MipmapControl mips,
1338 int usage) {
1339 int height = xpos.getHeight();
1340 if (xpos.getWidth() != height ||
1341 xneg.getWidth() != height || xneg.getHeight() != height ||
1342 ypos.getWidth() != height || ypos.getHeight() != height ||
1343 yneg.getWidth() != height || yneg.getHeight() != height ||
1344 zpos.getWidth() != height || zpos.getHeight() != height ||
1345 zneg.getWidth() != height || zneg.getHeight() != height) {
1346 throw new RSIllegalArgumentException("Only square cube map faces supported");
1347 }
1348 boolean isPow2 = (height & (height - 1)) == 0;
1349 if (!isPow2) {
1350 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1351 }
1352
1353 Element e = elementFromBitmap(rs, xpos);
1354 Type.Builder tb = new Type.Builder(rs, e);
1355 tb.setX(height);
1356 tb.setY(height);
1357 tb.setFaces(true);
1358 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1359 Type t = tb.create();
1360 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1361
1362 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001363 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001364 adapter.copyFrom(xpos);
1365 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1366 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001367 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001368 adapter.copyFrom(ypos);
1369 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1370 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001371 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001372 adapter.copyFrom(zpos);
1373 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1374 adapter.copyFrom(zneg);
1375
1376 return cubemap;
1377 }
1378
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001379 /**
1380 * Creates a non-mipmapped cubemap allocation for use as a
1381 * graphics texture from 6 bitmaps containing
1382 * the cube faces. All the faces must be the same size and
1383 * power of 2
1384 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001385 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001386 * @param xpos cubemap face in the positive x direction
1387 * @param xneg cubemap face in the negative x direction
1388 * @param ypos cubemap face in the positive y direction
1389 * @param yneg cubemap face in the negative y direction
1390 * @param zpos cubemap face in the positive z direction
1391 * @param zneg cubemap face in the negative z direction
1392 *
1393 * @return allocation containing cubemap data
1394 *
1395 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001396 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1397 Bitmap xpos,
1398 Bitmap xneg,
1399 Bitmap ypos,
1400 Bitmap yneg,
1401 Bitmap zpos,
1402 Bitmap zneg) {
1403 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1404 zpos, zneg, MipmapControl.MIPMAP_NONE,
1405 USAGE_GRAPHICS_TEXTURE);
1406 }
1407
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001408 /**
1409 * Creates a renderscript allocation from the bitmap referenced
1410 * by resource id
1411 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001412 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001413 * @param res application resources
1414 * @param id resource id to load the data from
1415 * @param mips specifies desired mipmap behaviour for the
1416 * allocation
1417 * @param usage bit field specifying how the allocation is
1418 * utilized
1419 *
1420 * @return renderscript allocation containing resource data
1421 *
1422 */
Jason Sams5476b452010-12-08 16:14:36 -08001423 static public Allocation createFromBitmapResource(RenderScript rs,
1424 Resources res,
1425 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001426 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001427 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001428
Jason Sams771bebb2009-12-07 12:40:12 -08001429 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001430 Bitmap b = BitmapFactory.decodeResource(res, id);
1431 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1432 b.recycle();
1433 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001434 }
1435
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001436 /**
1437 * Creates a non-mipmapped renderscript allocation to use as a
1438 * graphics texture from the bitmap referenced by resource id
1439 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001440 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001441 * @param res application resources
1442 * @param id resource id to load the data from
1443 *
1444 * @return renderscript allocation containing resource data
1445 *
1446 */
Jason Sams5476b452010-12-08 16:14:36 -08001447 static public Allocation createFromBitmapResource(RenderScript rs,
1448 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001449 int id) {
1450 return createFromBitmapResource(rs, res, id,
1451 MipmapControl.MIPMAP_NONE,
1452 USAGE_GRAPHICS_TEXTURE);
1453 }
1454
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001455 /**
1456 * Creates a renderscript allocation containing string data
1457 * encoded in UTF-8 format
1458 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001459 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001460 * @param str string to create the allocation from
1461 * @param usage bit field specifying how the allocaiton is
1462 * utilized
1463 *
1464 */
Jason Sams5476b452010-12-08 16:14:36 -08001465 static public Allocation createFromString(RenderScript rs,
1466 String str,
1467 int usage) {
1468 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001469 byte[] allocArray = null;
1470 try {
1471 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001472 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001473 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001474 return alloc;
1475 }
1476 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001477 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001478 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001479 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001480}
1481
1482