blob: 10ccb87407ad7c01ba1be8e19c077515d70f3bc6 [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 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800139 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700140 public static final int USAGE_IO_INPUT = 0x0020;
Stephen Hines9069ee82012-02-13 18:25:54 -0800141
Jason Sams615e7ce2012-01-13 14:01:20 -0800142 /**
Jason Sams163766c2012-02-15 12:04:24 -0800143 * USAGE_IO_OUTPUT The allocation will be used as a
144 * SurfaceTexture producer. The dimensions and format of the
145 * SurfaceTexture will be forced to those of the allocation.
Jason Sams615e7ce2012-01-13 14:01:20 -0800146 *
Jason Sams615e7ce2012-01-13 14:01:20 -0800147 */
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700148 public static final int USAGE_IO_OUTPUT = 0x0040;
Jason Sams43ee06852009-08-12 17:54:11 -0700149
Jason Samsf7086092011-01-12 13:28:37 -0800150 /**
151 * Controls mipmap behavior when using the bitmap creation and
152 * update functions.
153 */
Jason Sams4ef66502010-12-10 16:03:15 -0800154 public enum MipmapControl {
Jason Samsf7086092011-01-12 13:28:37 -0800155 /**
156 * No mipmaps will be generated and the type generated from the
157 * incoming bitmap will not contain additional LODs.
158 */
Jason Sams5476b452010-12-08 16:14:36 -0800159 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -0800160
161 /**
162 * A Full mipmap chain will be created in script memory. The
163 * type of the allocation will contain a full mipmap chain. On
164 * upload to graphics the full chain will be transfered.
165 */
Jason Sams5476b452010-12-08 16:14:36 -0800166 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -0800167
168 /**
169 * The type of the allocation will be the same as MIPMAP_NONE.
170 * It will not contain mipmaps. On upload to graphics the
171 * graphics copy of the allocation data will contain a full
172 * mipmap chain generated from the top level in script memory.
173 */
Jason Sams5476b452010-12-08 16:14:36 -0800174 MIPMAP_ON_SYNC_TO_TEXTURE(2);
175
176 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800177 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800178 mID = id;
179 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700180 }
181
Jason Sams48fe5342011-07-08 13:52:30 -0700182
183 private int getIDSafe() {
184 if (mAdaptedAllocation != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700185 return mAdaptedAllocation.getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700186 }
Jason Samse07694b2012-04-03 15:36:36 -0700187 return getID(mRS);
Jason Sams48fe5342011-07-08 13:52:30 -0700188 }
189
Jason Sams03d2d002012-03-23 13:51:56 -0700190
191 /**
192 * Get the element of the type of the Allocation.
193 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700194 * @return Element that describes the structure of data in the
195 * allocation
Jason Sams03d2d002012-03-23 13:51:56 -0700196 *
197 */
198 public Element getElement() {
199 return mType.getElement();
200 }
201
202 /**
203 * Get the usage flags of the Allocation.
204 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700205 * @return usage flags associated with the allocation. e.g.
206 * script, texture, etc.
Jason Sams03d2d002012-03-23 13:51:56 -0700207 *
208 */
209 public int getUsage() {
210 return mUsage;
211 }
212
Jason Sams36c0f642012-03-23 15:48:37 -0700213 /**
214 * Get the size of the Allocation in bytes.
215 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700216 * @return size of the Allocation in bytes.
Jason Sams36c0f642012-03-23 15:48:37 -0700217 *
218 */
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700219 public int getBytesSize() {
220 return mType.getCount() * mType.getElement().getBytesSize();
Jason Sams36c0f642012-03-23 15:48:37 -0700221 }
222
Jason Sams452a7662011-07-07 16:05:18 -0700223 private void updateCacheInfo(Type t) {
224 mCurrentDimX = t.getX();
225 mCurrentDimY = t.getY();
226 mCurrentDimZ = t.getZ();
227 mCurrentCount = mCurrentDimX;
228 if (mCurrentDimY > 1) {
229 mCurrentCount *= mCurrentDimY;
230 }
231 if (mCurrentDimZ > 1) {
232 mCurrentCount *= mCurrentDimZ;
233 }
234 }
Jason Samsba862d12011-07-07 15:24:42 -0700235
Jason Sams5476b452010-12-08 16:14:36 -0800236 Allocation(int id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700237 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800238 if ((usage & ~(USAGE_SCRIPT |
239 USAGE_GRAPHICS_TEXTURE |
240 USAGE_GRAPHICS_VERTEX |
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700241 USAGE_GRAPHICS_CONSTANTS |
Jason Sams615e7ce2012-01-13 14:01:20 -0800242 USAGE_GRAPHICS_RENDER_TARGET |
Jason Sams615e7ce2012-01-13 14:01:20 -0800243 USAGE_IO_INPUT |
244 USAGE_IO_OUTPUT)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800245 throw new RSIllegalArgumentException("Unknown usage specified.");
246 }
Jason Sams615e7ce2012-01-13 14:01:20 -0800247
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700248 if ((usage & USAGE_IO_INPUT) != 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800249 mWriteAllowed = false;
250
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700251 if ((usage & ~(USAGE_IO_INPUT |
Jason Sams615e7ce2012-01-13 14:01:20 -0800252 USAGE_GRAPHICS_TEXTURE |
253 USAGE_SCRIPT)) != 0) {
254 throw new RSIllegalArgumentException("Invalid usage combination.");
255 }
256 }
257
Jason Sams5476b452010-12-08 16:14:36 -0800258 mType = t;
Jason Sams615e7ce2012-01-13 14:01:20 -0800259 mUsage = usage;
Jason Samsba862d12011-07-07 15:24:42 -0700260
Jason Sams452a7662011-07-07 16:05:18 -0700261 if (t != null) {
262 updateCacheInfo(t);
Jason Samsba862d12011-07-07 15:24:42 -0700263 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700264 }
265
Jason Samsb97b2512011-01-16 15:04:08 -0800266 private void validateIsInt32() {
267 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
268 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
269 return;
270 }
271 throw new RSIllegalArgumentException(
272 "32 bit integer source does not match allocation type " + mType.mElement.mType);
273 }
274
275 private void validateIsInt16() {
276 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
277 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
278 return;
279 }
280 throw new RSIllegalArgumentException(
281 "16 bit integer source does not match allocation type " + mType.mElement.mType);
282 }
283
284 private void validateIsInt8() {
285 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
286 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
287 return;
288 }
289 throw new RSIllegalArgumentException(
290 "8 bit integer source does not match allocation type " + mType.mElement.mType);
291 }
292
293 private void validateIsFloat32() {
294 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
295 return;
296 }
297 throw new RSIllegalArgumentException(
298 "32 bit float source does not match allocation type " + mType.mElement.mType);
299 }
300
301 private void validateIsObject() {
302 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
303 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
304 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
305 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
306 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
307 (mType.mElement.mType == Element.DataType.RS_MESH) ||
308 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
309 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
310 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
311 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
312 return;
313 }
314 throw new RSIllegalArgumentException(
315 "Object source does not match allocation type " + mType.mElement.mType);
316 }
317
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700318 @Override
319 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800320 super.updateFromNative();
Jason Samse07694b2012-04-03 15:36:36 -0700321 int typeID = mRS.nAllocationGetType(getID(mRS));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700322 if(typeID != 0) {
323 mType = new Type(typeID, mRS);
324 mType.updateFromNative();
Jason Samsad37cb22011-07-07 16:17:36 -0700325 updateCacheInfo(mType);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700326 }
327 }
328
Jason Sams03d2d002012-03-23 13:51:56 -0700329 /**
330 * Get the type of the Allocation.
331 *
332 * @return Type
333 *
334 */
Jason Samsea87e962010-01-12 12:12:28 -0800335 public Type getType() {
336 return mType;
337 }
338
Jason Sams03d2d002012-03-23 13:51:56 -0700339 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700340 * Propagate changes from one usage of the allocation to the
Jason Sams03d2d002012-03-23 13:51:56 -0700341 * remaining usages of the allocation.
342 *
343 */
Jason Sams5476b452010-12-08 16:14:36 -0800344 public void syncAll(int srcLocation) {
345 switch (srcLocation) {
346 case USAGE_SCRIPT:
347 case USAGE_GRAPHICS_CONSTANTS:
348 case USAGE_GRAPHICS_TEXTURE:
349 case USAGE_GRAPHICS_VERTEX:
350 break;
351 default:
352 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
353 }
354 mRS.validate();
Jason Sams48fe5342011-07-08 13:52:30 -0700355 mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
Jason Sams5476b452010-12-08 16:14:36 -0800356 }
357
Jason Sams163766c2012-02-15 12:04:24 -0800358 /**
359 * Send a buffer to the output stream. The contents of the
360 * Allocation will be undefined after this operation.
361 *
Jason Sams163766c2012-02-15 12:04:24 -0800362 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700363 public void ioSend() {
Jason Sams163766c2012-02-15 12:04:24 -0800364 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
365 throw new RSIllegalArgumentException(
366 "Can only send buffer if IO_OUTPUT usage specified.");
367 }
368 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700369 mRS.nAllocationIoSend(getID(mRS));
Jason Sams163766c2012-02-15 12:04:24 -0800370 }
371
372 /**
Jason Samsc5f519c2012-03-29 17:58:15 -0700373 * Delete once code is updated.
374 * @hide
375 */
376 public void ioSendOutput() {
377 ioSend();
378 }
379
380 /**
Jason Sams163766c2012-02-15 12:04:24 -0800381 * Receive the latest input into the Allocation.
382 *
Jason Sams163766c2012-02-15 12:04:24 -0800383 */
Jason Samsc5f519c2012-03-29 17:58:15 -0700384 public void ioReceive() {
Jason Sams163766c2012-02-15 12:04:24 -0800385 if ((mUsage & USAGE_IO_INPUT) == 0) {
386 throw new RSIllegalArgumentException(
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700387 "Can only receive if IO_INPUT usage specified.");
Jason Sams163766c2012-02-15 12:04:24 -0800388 }
389 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700390 mRS.nAllocationIoReceive(getID(mRS));
Jason Sams163766c2012-02-15 12:04:24 -0800391 }
392
Jason Sams03d2d002012-03-23 13:51:56 -0700393 /**
Jason Sams36c0f642012-03-23 15:48:37 -0700394 * Copy an array of RS objects to the allocation.
Jason Sams03d2d002012-03-23 13:51:56 -0700395 *
396 * @param d Source array.
397 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800398 public void copyFrom(BaseObj[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800399 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800400 validateIsObject();
Jason Samsba862d12011-07-07 15:24:42 -0700401 if (d.length != mCurrentCount) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800402 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
Jason Samsba862d12011-07-07 15:24:42 -0700403 mCurrentCount + ", array length = " + d.length);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800404 }
405 int i[] = new int[d.length];
406 for (int ct=0; ct < d.length; ct++) {
Jason Samse07694b2012-04-03 15:36:36 -0700407 i[ct] = d[ct].getID(mRS);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800408 }
Jason Samsba862d12011-07-07 15:24:42 -0700409 copy1DRangeFromUnchecked(0, mCurrentCount, i);
Jason Samsb8c5a842009-07-31 20:40:47 -0700410 }
411
Jason Samsfb9f82c2011-01-12 14:53:25 -0800412 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800413 Bitmap.Config bc = b.getConfig();
414 switch (bc) {
415 case ALPHA_8:
416 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
417 throw new RSIllegalArgumentException("Allocation kind is " +
418 mType.getElement().mKind + ", type " +
419 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700420 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800421 " bytes, passed bitmap was " + bc);
422 }
423 break;
424 case ARGB_8888:
425 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700426 (mType.getElement().getBytesSize() != 4)) {
Jason Sams252c0782011-01-11 17:42:52 -0800427 throw new RSIllegalArgumentException("Allocation kind is " +
428 mType.getElement().mKind + ", type " +
429 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700430 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800431 " bytes, passed bitmap was " + bc);
432 }
433 break;
434 case RGB_565:
435 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700436 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800437 throw new RSIllegalArgumentException("Allocation kind is " +
438 mType.getElement().mKind + ", type " +
439 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700440 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800441 " bytes, passed bitmap was " + bc);
442 }
443 break;
444 case ARGB_4444:
445 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700446 (mType.getElement().getBytesSize() != 2)) {
Jason Sams252c0782011-01-11 17:42:52 -0800447 throw new RSIllegalArgumentException("Allocation kind is " +
448 mType.getElement().mKind + ", type " +
449 mType.getElement().mType +
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700450 " of " + mType.getElement().getBytesSize() +
Jason Sams252c0782011-01-11 17:42:52 -0800451 " bytes, passed bitmap was " + bc);
452 }
453 break;
454
455 }
Jason Sams4ef66502010-12-10 16:03:15 -0800456 }
457
Jason Samsfb9f82c2011-01-12 14:53:25 -0800458 private void validateBitmapSize(Bitmap b) {
Jason Samsba862d12011-07-07 15:24:42 -0700459 if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800460 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
461 }
462 }
463
Jason Sams4fa3eed2011-01-19 15:44:38 -0800464 /**
465 * Copy an allocation from an array. This variant is not type
466 * checked which allows an application to fill in structured
467 * data from an array.
468 *
469 * @param d the source data array
470 */
471 public void copyFromUnchecked(int[] d) {
472 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700473 copy1DRangeFromUnchecked(0, mCurrentCount, d);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800474 }
475 /**
476 * Copy an allocation from an array. This variant is not type
477 * checked which allows an application to fill in structured
478 * data from an array.
479 *
480 * @param d the source data array
481 */
482 public void copyFromUnchecked(short[] d) {
483 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700484 copy1DRangeFromUnchecked(0, mCurrentCount, d);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800485 }
486 /**
487 * Copy an allocation from an array. This variant is not type
488 * checked which allows an application to fill in structured
489 * data from an array.
490 *
491 * @param d the source data array
492 */
493 public void copyFromUnchecked(byte[] d) {
494 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700495 copy1DRangeFromUnchecked(0, mCurrentCount, d);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800496 }
497 /**
498 * Copy an allocation from an array. This variant is not type
499 * checked which allows an application to fill in structured
500 * data from an array.
501 *
502 * @param d the source data array
503 */
504 public void copyFromUnchecked(float[] d) {
505 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700506 copy1DRangeFromUnchecked(0, mCurrentCount, d);
Jason Sams4fa3eed2011-01-19 15:44:38 -0800507 }
508
509 /**
510 * Copy an allocation from an array. This variant is type
511 * checked and will generate exceptions if the Allocation type
512 * is not a 32 bit integer type.
513 *
514 * @param d the source data array
515 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800516 public void copyFrom(int[] d) {
517 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700518 copy1DRangeFrom(0, mCurrentCount, d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800519 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800520
521 /**
522 * Copy an allocation from an array. This variant is type
523 * checked and will generate exceptions if the Allocation type
524 * is not a 16 bit integer type.
525 *
526 * @param d the source data array
527 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800528 public void copyFrom(short[] d) {
529 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700530 copy1DRangeFrom(0, mCurrentCount, d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800531 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800532
533 /**
534 * Copy an allocation from an array. This variant is type
535 * checked and will generate exceptions if the Allocation type
536 * is not a 8 bit integer type.
537 *
538 * @param d the source data array
539 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800540 public void copyFrom(byte[] d) {
541 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700542 copy1DRangeFrom(0, mCurrentCount, d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800543 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800544
545 /**
546 * Copy an allocation from an array. This variant is type
547 * checked and will generate exceptions if the Allocation type
548 * is not a 32 bit float type.
549 *
550 * @param d the source data array
551 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800552 public void copyFrom(float[] d) {
553 mRS.validate();
Jason Samsba862d12011-07-07 15:24:42 -0700554 copy1DRangeFrom(0, mCurrentCount, d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800555 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800556
557 /**
558 * Copy an allocation from a bitmap. The height, width, and
559 * format of the bitmap must match the existing allocation.
560 *
561 * @param b the source bitmap
562 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800563 public void copyFrom(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800564 mRS.validate();
565 validateBitmapSize(b);
566 validateBitmapFormat(b);
Jason Samse07694b2012-04-03 15:36:36 -0700567 mRS.nAllocationCopyFromBitmap(getID(mRS), b);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700568 }
569
Jason Samsfa445b92011-01-07 17:00:07 -0800570 /**
Jason Samsfa445b92011-01-07 17:00:07 -0800571 * This is only intended to be used by auto-generate code reflected from the
572 * renderscript script files.
573 *
574 * @param xoff
575 * @param fp
576 */
Jason Sams21b41032011-01-16 15:05:41 -0800577 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800578 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700579 int eSize = mType.mElement.getBytesSize();
Jason Samsa70f4162010-03-26 15:33:42 -0700580 final byte[] data = fp.getData();
581
582 int count = data.length / eSize;
583 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800584 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700585 " not divisible by element size " + eSize + ".");
586 }
Jason Samsba862d12011-07-07 15:24:42 -0700587 copy1DRangeFromUnchecked(xoff, count, data);
Jason Sams49bdaf02010-08-31 13:50:42 -0700588 }
589
Jason Samsfa445b92011-01-07 17:00:07 -0800590 /**
Jason Samsfa445b92011-01-07 17:00:07 -0800591 * This is only intended to be used by auto-generate code reflected from the
592 * renderscript script files.
593 *
594 * @param xoff
595 * @param component_number
596 * @param fp
597 */
Jason Sams21b41032011-01-16 15:05:41 -0800598 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Samsf70b0fc82012-02-22 15:22:41 -0800599 mRS.validate();
Jason Sams49bdaf02010-08-31 13:50:42 -0700600 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800601 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700602 }
603 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800604 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700605 }
606
607 final byte[] data = fp.getData();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700608 int eSize = mType.mElement.mElements[component_number].getBytesSize();
Alex Sakhartchoukbf3c3f22012-02-02 09:47:26 -0800609 eSize *= mType.mElement.mArraySizes[component_number];
Jason Sams49bdaf02010-08-31 13:50:42 -0700610
611 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800612 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700613 " does not match component size " + eSize + ".");
614 }
615
Jason Sams48fe5342011-07-08 13:52:30 -0700616 mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
Jason Samsba862d12011-07-07 15:24:42 -0700617 component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700618 }
619
Jason Sams768bc022009-09-21 19:41:04 -0700620 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800621 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700622 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800623 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700624 }
625 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800626 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700627 }
Jason Samsba862d12011-07-07 15:24:42 -0700628 if((off + count) > mCurrentCount) {
629 throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
Jason Samsa70f4162010-03-26 15:33:42 -0700630 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700631 }
Jason Samsba862d12011-07-07 15:24:42 -0700632 if(len < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800633 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700634 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700635 }
636
Jason Samsf7086092011-01-12 13:28:37 -0800637 /**
638 * Generate a mipmap chain. Requires the type of the allocation
639 * include mipmaps.
640 *
641 * This function will generate a complete set of mipmaps from
642 * the top level lod and place them into the script memoryspace.
643 *
644 * If the allocation is also using other memory spaces a
645 * followup sync will be required.
646 */
647 public void generateMipmaps() {
Jason Samse07694b2012-04-03 15:36:36 -0700648 mRS.nAllocationGenerateMipmaps(getID(mRS));
Jason Samsf7086092011-01-12 13:28:37 -0800649 }
650
Jason Sams4fa3eed2011-01-19 15:44:38 -0800651 /**
652 * Copy part of an allocation from an array. This variant is
653 * not type checked which allows an application to fill in
654 * structured data from an array.
655 *
656 * @param off The offset of the first element to be copied.
657 * @param count The number of elements to be copied.
658 * @param d the source data array
659 */
660 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700661 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700662 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700663 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700664 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800665 /**
666 * Copy part of an allocation from an array. This variant is
667 * not type checked which allows an application to fill in
668 * structured data from an array.
669 *
670 * @param off The offset of the first element to be copied.
671 * @param count The number of elements to be copied.
672 * @param d the source data array
673 */
674 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700675 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700676 data1DChecks(off, count, d.length * 2, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700677 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700678 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800679 /**
680 * Copy part of an allocation from an array. This variant is
681 * not type checked which allows an application to fill in
682 * structured data from an array.
683 *
684 * @param off The offset of the first element to be copied.
685 * @param count The number of elements to be copied.
686 * @param d the source data array
687 */
688 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700689 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700690 data1DChecks(off, count, d.length, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700691 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700692 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800693 /**
694 * Copy part of an allocation from an array. This variant is
695 * not type checked which allows an application to fill in
696 * structured data from an array.
697 *
698 * @param off The offset of the first element to be copied.
699 * @param count The number of elements to be copied.
700 * @param d the source data array
701 */
702 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700703 int dataSize = mType.mElement.getBytesSize() * count;
Jason Sams768bc022009-09-21 19:41:04 -0700704 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams48fe5342011-07-08 13:52:30 -0700705 mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700706 }
707
Jason Sams4fa3eed2011-01-19 15:44:38 -0800708 /**
709 * Copy part of an allocation from an array. This variant is
710 * type checked and will generate exceptions if the Allocation
711 * type is not a 32 bit integer type.
712 *
713 * @param off The offset of the first element to be copied.
714 * @param count The number of elements to be copied.
715 * @param d the source data array
716 */
Jason Samsb97b2512011-01-16 15:04:08 -0800717 public void copy1DRangeFrom(int off, int count, int[] d) {
718 validateIsInt32();
719 copy1DRangeFromUnchecked(off, count, d);
720 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800721
722 /**
723 * Copy part of an allocation from an array. This variant is
724 * type checked and will generate exceptions if the Allocation
725 * type is not a 16 bit integer type.
726 *
727 * @param off The offset of the first element to be copied.
728 * @param count The number of elements to be copied.
729 * @param d the source data array
730 */
Jason Samsb97b2512011-01-16 15:04:08 -0800731 public void copy1DRangeFrom(int off, int count, short[] d) {
732 validateIsInt16();
733 copy1DRangeFromUnchecked(off, count, d);
734 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800735
736 /**
737 * Copy part of an allocation from an array. This variant is
738 * type checked and will generate exceptions if the Allocation
739 * type is not a 8 bit integer type.
740 *
741 * @param off The offset of the first element to be copied.
742 * @param count The number of elements to be copied.
743 * @param d the source data array
744 */
Jason Samsb97b2512011-01-16 15:04:08 -0800745 public void copy1DRangeFrom(int off, int count, byte[] d) {
746 validateIsInt8();
747 copy1DRangeFromUnchecked(off, count, d);
748 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800749
750 /**
751 * Copy part of an allocation from an array. This variant is
752 * type checked and will generate exceptions if the Allocation
753 * type is not a 32 bit float type.
754 *
755 * @param off The offset of the first element to be copied.
756 * @param count The number of elements to be copied.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700757 * @param d the source data array.
Jason Sams4fa3eed2011-01-19 15:44:38 -0800758 */
Jason Samsb97b2512011-01-16 15:04:08 -0800759 public void copy1DRangeFrom(int off, int count, float[] d) {
760 validateIsFloat32();
761 copy1DRangeFromUnchecked(off, count, d);
762 }
763
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700764 /**
765 * Copy part of an allocation from another allocation.
766 *
767 * @param off The offset of the first element to be copied.
768 * @param count The number of elements to be copied.
769 * @param data the source data allocation.
770 * @param dataOff off The offset of the first element in data to
771 * be copied.
772 */
773 public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
Jason Sams48fe5342011-07-08 13:52:30 -0700774 mRS.nAllocationData2D(getIDSafe(), off, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700775 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700776 count, 1, data.getID(mRS), dataOff, 0,
Jason Samsba862d12011-07-07 15:24:42 -0700777 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700778 }
779
Jason Samsfb9f82c2011-01-12 14:53:25 -0800780 private void validate2DRange(int xoff, int yoff, int w, int h) {
Jason Samsba862d12011-07-07 15:24:42 -0700781 if (mAdaptedAllocation != null) {
782
783 } else {
784
785 if (xoff < 0 || yoff < 0) {
786 throw new RSIllegalArgumentException("Offset cannot be negative.");
787 }
788 if (h < 0 || w < 0) {
789 throw new RSIllegalArgumentException("Height or width cannot be negative.");
790 }
791 if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
792 throw new RSIllegalArgumentException("Updated region larger than allocation.");
793 }
Jason Samsfb9f82c2011-01-12 14:53:25 -0800794 }
795 }
Jason Sams768bc022009-09-21 19:41:04 -0700796
Jason Samsf7086092011-01-12 13:28:37 -0800797 /**
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700798 * Copy a rectangular region from the array into the allocation.
799 * The incoming array is assumed to be tightly packed.
Jason Samsf7086092011-01-12 13:28:37 -0800800 *
801 * @param xoff X offset of the region to update
802 * @param yoff Y offset of the region to update
803 * @param w Width of the incoming region to update
804 * @param h Height of the incoming region to update
805 * @param data to be placed into the allocation
806 */
807 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800808 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800809 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700810 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700811 w, h, data, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -0800812 }
813
Jason Samsf7086092011-01-12 13:28:37 -0800814 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] 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 * 2);
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, int[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -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 * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700826 }
827
Jason Samsf7086092011-01-12 13:28:37 -0800828 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] 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 /**
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700836 * Copy a rectangular region into the allocation from another
837 * allocation.
838 *
839 * @param xoff X offset of the region to update.
840 * @param yoff Y offset of the region to update.
841 * @param w Width of the incoming region to update.
842 * @param h Height of the incoming region to update.
843 * @param data source allocation.
844 * @param dataXoff X offset in data of the region to update.
845 * @param dataYoff Y offset in data of the region to update.
846 */
847 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
848 Allocation data, int dataXoff, int dataYoff) {
849 mRS.validate();
850 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700851 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -0700852 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700853 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -0700854 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700855 }
856
857 /**
Jason Samsf7086092011-01-12 13:28:37 -0800858 * Copy a bitmap into an allocation. The height and width of
859 * the update will use the height and width of the incoming
860 * bitmap.
861 *
862 * @param xoff X offset of the region to update
863 * @param yoff Y offset of the region to update
864 * @param data the bitmap to be copied
865 */
866 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800867 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800868 validateBitmapFormat(data);
869 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -0700870 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Jason Samsfa445b92011-01-07 17:00:07 -0800871 }
872
873
Jason Sams48fe5342011-07-08 13:52:30 -0700874 /**
875 * Copy from the Allocation into a Bitmap. The bitmap must
876 * match the dimensions of the Allocation.
877 *
878 * @param b The bitmap to be set from the Allocation.
879 */
Jason Samsfa445b92011-01-07 17:00:07 -0800880 public void copyTo(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800881 mRS.validate();
882 validateBitmapFormat(b);
883 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -0700884 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Jason Samsfa445b92011-01-07 17:00:07 -0800885 }
886
Jason Sams48fe5342011-07-08 13:52:30 -0700887 /**
888 * Copy from the Allocation into a byte array. The array must
889 * be at least as large as the Allocation. The allocation must
890 * be of an 8 bit elemental type.
891 *
892 * @param d The array to be set from the Allocation.
893 */
Jason Samsfa445b92011-01-07 17:00:07 -0800894 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800895 validateIsInt8();
Jason Sams771bebb2009-12-07 12:40:12 -0800896 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700897 mRS.nAllocationRead(getID(mRS), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700898 }
899
Jason Sams48fe5342011-07-08 13:52:30 -0700900 /**
901 * Copy from the Allocation into a short array. The array must
902 * be at least as large as the Allocation. The allocation must
903 * be of an 16 bit elemental type.
904 *
905 * @param d The array to be set from the Allocation.
906 */
Jason Samsfa445b92011-01-07 17:00:07 -0800907 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800908 validateIsInt16();
Jason Samsfa445b92011-01-07 17:00:07 -0800909 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700910 mRS.nAllocationRead(getID(mRS), d);
Jason Samsfa445b92011-01-07 17:00:07 -0800911 }
912
Jason Sams48fe5342011-07-08 13:52:30 -0700913 /**
914 * Copy from the Allocation into a int array. The array must be
915 * at least as large as the Allocation. The allocation must be
916 * of an 32 bit elemental type.
917 *
918 * @param d The array to be set from the Allocation.
919 */
Jason Samsfa445b92011-01-07 17:00:07 -0800920 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800921 validateIsInt32();
Jason Samsfa445b92011-01-07 17:00:07 -0800922 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700923 mRS.nAllocationRead(getID(mRS), d);
Jason Samsfa445b92011-01-07 17:00:07 -0800924 }
925
Jason Sams48fe5342011-07-08 13:52:30 -0700926 /**
927 * Copy from the Allocation into a float array. The array must
928 * be at least as large as the Allocation. The allocation must
929 * be of an 32 bit float elemental type.
930 *
931 * @param d The array to be set from the Allocation.
932 */
Jason Samsfa445b92011-01-07 17:00:07 -0800933 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800934 validateIsFloat32();
Jason Sams771bebb2009-12-07 12:40:12 -0800935 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700936 mRS.nAllocationRead(getID(mRS), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700937 }
938
Jason Samsf7086092011-01-12 13:28:37 -0800939 /**
940 * Resize a 1D allocation. The contents of the allocation are
941 * preserved. If new elements are allocated objects are created
942 * with null contents and the new region is otherwise undefined.
943 *
944 * If the new region is smaller the references of any objects
945 * outside the new region will be released.
946 *
947 * A new type will be created with the new dimension.
948 *
949 * @param dimX The new size of the allocation.
950 */
Jason Sams31a7e422010-10-26 13:09:17 -0700951 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800952 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800953 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700954 }
Jason Samse07694b2012-04-03 15:36:36 -0700955 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -0700956 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -0700957
Jason Samse07694b2012-04-03 15:36:36 -0700958 int typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -0700959 mType = new Type(typeID, mRS);
960 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -0700961 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -0700962 }
963
Jason Sams163766c2012-02-15 12:04:24 -0800964 /**
965 * Resize a 2D allocation. The contents of the allocation are
966 * preserved. If new elements are allocated objects are created
967 * with null contents and the new region is otherwise undefined.
968 *
969 * If the new region is smaller the references of any objects
970 * outside the new region will be released.
971 *
972 * A new type will be created with the new dimension.
973 *
974 * @hide
975 * @param dimX The new size of the allocation.
976 * @param dimY The new size of the allocation.
977 */
Jason Sams5edc6082010-10-05 13:32:49 -0700978 public void resize(int dimX, int dimY) {
Jason Sams163766c2012-02-15 12:04:24 -0800979 if ((mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
980 throw new RSInvalidStateException(
981 "Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700982 }
983 if (mType.getY() == 0) {
Jason Sams163766c2012-02-15 12:04:24 -0800984 throw new RSInvalidStateException(
985 "Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700986 }
Jason Samse07694b2012-04-03 15:36:36 -0700987 mRS.nAllocationResize2D(getID(mRS), dimX, dimY);
Jason Sams163766c2012-02-15 12:04:24 -0800988 mRS.finish(); // Necessary because resize is fifoed and update is async.
989
Jason Samse07694b2012-04-03 15:36:36 -0700990 int typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams163766c2012-02-15 12:04:24 -0800991 mType = new Type(typeID, mRS);
992 mType.updateFromNative();
993 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -0700994 }
Jason Sams40a29e82009-08-10 14:55:26 -0700995
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700996
Jason Samsb8c5a842009-07-31 20:40:47 -0700997
998 // creation
999
Jason Sams49a05d72010-12-29 14:31:29 -08001000 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001001 static {
1002 mBitmapOptions.inScaled = false;
1003 }
1004
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001005 /**
1006 *
1007 * @param type renderscript type describing data layout
1008 * @param mips specifies desired mipmap behaviour for the
1009 * allocation
1010 * @param usage bit field specifying how the allocation is
1011 * utilized
1012 */
1013 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001014 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001015 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001016 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001017 }
Jason Samse07694b2012-04-03 15:36:36 -07001018 int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001019 if (id == 0) {
1020 throw new RSRuntimeException("Allocation creation failed.");
1021 }
1022 return new Allocation(id, rs, type, usage);
1023 }
1024
1025 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001026 * Creates a renderscript allocation with the size specified by
1027 * the type and no mipmaps generated by default
1028 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001029 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001030 * @param type renderscript type describing data layout
1031 * @param usage bit field specifying how the allocation is
1032 * utilized
1033 *
1034 * @return allocation
1035 */
Jason Samse5d37122010-12-16 00:33:33 -08001036 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1037 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1038 }
1039
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001040 /**
1041 * Creates a renderscript allocation for use by the script with
1042 * the size specified by the type and no mipmaps generated by
1043 * default
1044 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001045 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001046 * @param type renderscript type describing data layout
1047 *
1048 * @return allocation
1049 */
Jason Sams5476b452010-12-08 16:14:36 -08001050 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001051 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001052 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001053
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001054 /**
1055 * Creates a renderscript allocation with a specified number of
1056 * given elements
1057 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001058 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001059 * @param e describes what each element of an allocation is
1060 * @param count specifies the number of element in the allocation
1061 * @param usage bit field specifying how the allocation is
1062 * utilized
1063 *
1064 * @return allocation
1065 */
Jason Sams5476b452010-12-08 16:14:36 -08001066 static public Allocation createSized(RenderScript rs, Element e,
1067 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001068 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001069 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001070 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001071 Type t = b.create();
1072
Jason Samse07694b2012-04-03 15:36:36 -07001073 int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001074 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001075 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001076 }
Jason Sams5476b452010-12-08 16:14:36 -08001077 return new Allocation(id, rs, t, usage);
1078 }
1079
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001080 /**
1081 * Creates a renderscript allocation with a specified number of
1082 * given elements
1083 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001084 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001085 * @param e describes what each element of an allocation is
1086 * @param count specifies the number of element in the allocation
1087 *
1088 * @return allocation
1089 */
Jason Sams5476b452010-12-08 16:14:36 -08001090 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001091 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001092 }
1093
Jason Sams49a05d72010-12-29 14:31:29 -08001094 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001095 final Bitmap.Config bc = b.getConfig();
1096 if (bc == Bitmap.Config.ALPHA_8) {
1097 return Element.A_8(rs);
1098 }
1099 if (bc == Bitmap.Config.ARGB_4444) {
1100 return Element.RGBA_4444(rs);
1101 }
1102 if (bc == Bitmap.Config.ARGB_8888) {
1103 return Element.RGBA_8888(rs);
1104 }
1105 if (bc == Bitmap.Config.RGB_565) {
1106 return Element.RGB_565(rs);
1107 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001108 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001109 }
1110
Jason Sams49a05d72010-12-29 14:31:29 -08001111 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001112 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001113 Element e = elementFromBitmap(rs, b);
1114 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001115 tb.setX(b.getWidth());
1116 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001117 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001118 return tb.create();
1119 }
1120
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001121 /**
1122 * Creates a renderscript allocation from a bitmap
1123 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001124 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001125 * @param b bitmap source for the allocation data
1126 * @param mips specifies desired mipmap behaviour for the
1127 * allocation
1128 * @param usage bit field specifying how the allocation is
1129 * utilized
1130 *
1131 * @return renderscript allocation containing bitmap data
1132 *
1133 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001134 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001135 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001136 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001137 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001138 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001139
Jason Samse07694b2012-04-03 15:36:36 -07001140 int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001141 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001142 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001143 }
Jason Sams5476b452010-12-08 16:14:36 -08001144 return new Allocation(id, rs, t, usage);
1145 }
1146
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001147 /**
Jason Sams615e7ce2012-01-13 14:01:20 -08001148 *
1149 *
1150 * @hide
1151 *
1152 */
1153 public SurfaceTexture getSurfaceTexture() {
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001154 if ((mUsage & USAGE_IO_INPUT) == 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -08001155 throw new RSInvalidStateException("Allocation is not a surface texture.");
1156 }
1157
Jason Samse07694b2012-04-03 15:36:36 -07001158 int id = mRS.nAllocationGetSurfaceTextureID(getID(mRS));
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001159 SurfaceTexture st = new SurfaceTexture(id);
Jason Samse07694b2012-04-03 15:36:36 -07001160 mRS.nAllocationGetSurfaceTextureID2(getID(mRS), st);
Jason Sams615e7ce2012-01-13 14:01:20 -08001161
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001162 return st;
Jason Sams615e7ce2012-01-13 14:01:20 -08001163 }
1164
Jason Sams163766c2012-02-15 12:04:24 -08001165 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001166 * For allocations used with io operations, returns the handle
1167 * onto a raw buffer that is being managed by the screen
1168 * compositor.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001169 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001170 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001171 *
1172 */
1173 public Surface getSurface() {
1174 return new Surface(getSurfaceTexture());
1175 }
1176
1177 /**
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001178 * Associate a surface for io output with this allocation
1179 *
1180 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001181 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001182 public void setSurface(Surface sur) {
1183 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001184 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1185 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1186 }
1187
Jason Samse07694b2012-04-03 15:36:36 -07001188 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001189 }
1190
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001191 /**
1192 * @hide
1193 */
1194 public void setSurfaceTexture(SurfaceTexture st) {
1195 mRS.validate();
1196 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1197 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1198 }
1199
1200 Surface s = new Surface(st);
Jason Samse07694b2012-04-03 15:36:36 -07001201 mRS.nAllocationSetSurface(getID(mRS), s);
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001202 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001203
1204 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001205 * Creates a non-mipmapped renderscript allocation to use as a
1206 * graphics texture
1207 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001208 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001209 * @param b bitmap source for the allocation data
1210 *
1211 * @return renderscript allocation containing bitmap data
1212 *
1213 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001214 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
1215 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1216 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001217 }
1218
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001219 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001220 * Creates a cubemap allocation from a bitmap containing the
1221 * horizontal list of cube faces. Each individual face must be
1222 * the same size and power of 2
1223 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001224 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001225 * @param b bitmap with cubemap faces layed out in the following
1226 * format: right, left, top, bottom, front, back
1227 * @param mips specifies desired mipmap behaviour for the cubemap
1228 * @param usage bit field specifying how the cubemap is utilized
1229 *
1230 * @return allocation containing cubemap data
1231 *
1232 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001233 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001234 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001235 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001236 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001237
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001238 int height = b.getHeight();
1239 int width = b.getWidth();
1240
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001241 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001242 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1243 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001244 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001245 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001246 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001247 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001248 if (!isPow2) {
1249 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1250 }
1251
1252 Element e = elementFromBitmap(rs, b);
1253 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001254 tb.setX(height);
1255 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001256 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001257 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001258 Type t = tb.create();
1259
Jason Samse07694b2012-04-03 15:36:36 -07001260 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001261 if(id == 0) {
1262 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1263 }
Jason Sams5476b452010-12-08 16:14:36 -08001264 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001265 }
1266
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001267 /**
1268 * Creates a non-mipmapped cubemap allocation for use as a
1269 * graphics texture from a bitmap containing the horizontal list
1270 * of cube faces. Each individual face must be the same size and
1271 * power of 2
1272 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001273 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001274 * @param b bitmap with cubemap faces layed out in the following
1275 * format: right, left, top, bottom, front, back
1276 *
1277 * @return allocation containing cubemap data
1278 *
1279 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001280 static public Allocation createCubemapFromBitmap(RenderScript rs,
1281 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001282 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001283 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001284 }
1285
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001286 /**
1287 * Creates a cubemap allocation from 6 bitmaps containing
1288 * the cube faces. All the faces must be the same size and
1289 * power of 2
1290 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001291 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001292 * @param xpos cubemap face in the positive x direction
1293 * @param xneg cubemap face in the negative x direction
1294 * @param ypos cubemap face in the positive y direction
1295 * @param yneg cubemap face in the negative y direction
1296 * @param zpos cubemap face in the positive z direction
1297 * @param zneg cubemap face in the negative z direction
1298 * @param mips specifies desired mipmap behaviour for the cubemap
1299 * @param usage bit field specifying how the cubemap is utilized
1300 *
1301 * @return allocation containing cubemap data
1302 *
1303 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001304 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1305 Bitmap xpos,
1306 Bitmap xneg,
1307 Bitmap ypos,
1308 Bitmap yneg,
1309 Bitmap zpos,
1310 Bitmap zneg,
1311 MipmapControl mips,
1312 int usage) {
1313 int height = xpos.getHeight();
1314 if (xpos.getWidth() != height ||
1315 xneg.getWidth() != height || xneg.getHeight() != height ||
1316 ypos.getWidth() != height || ypos.getHeight() != height ||
1317 yneg.getWidth() != height || yneg.getHeight() != height ||
1318 zpos.getWidth() != height || zpos.getHeight() != height ||
1319 zneg.getWidth() != height || zneg.getHeight() != height) {
1320 throw new RSIllegalArgumentException("Only square cube map faces supported");
1321 }
1322 boolean isPow2 = (height & (height - 1)) == 0;
1323 if (!isPow2) {
1324 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1325 }
1326
1327 Element e = elementFromBitmap(rs, xpos);
1328 Type.Builder tb = new Type.Builder(rs, e);
1329 tb.setX(height);
1330 tb.setY(height);
1331 tb.setFaces(true);
1332 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1333 Type t = tb.create();
1334 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1335
1336 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001337 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001338 adapter.copyFrom(xpos);
1339 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1340 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001341 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001342 adapter.copyFrom(ypos);
1343 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1344 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001345 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001346 adapter.copyFrom(zpos);
1347 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1348 adapter.copyFrom(zneg);
1349
1350 return cubemap;
1351 }
1352
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001353 /**
1354 * Creates a non-mipmapped cubemap allocation for use as a
1355 * graphics texture from 6 bitmaps containing
1356 * the cube faces. All the faces must be the same size and
1357 * power of 2
1358 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001359 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001360 * @param xpos cubemap face in the positive x direction
1361 * @param xneg cubemap face in the negative x direction
1362 * @param ypos cubemap face in the positive y direction
1363 * @param yneg cubemap face in the negative y direction
1364 * @param zpos cubemap face in the positive z direction
1365 * @param zneg cubemap face in the negative z direction
1366 *
1367 * @return allocation containing cubemap data
1368 *
1369 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001370 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1371 Bitmap xpos,
1372 Bitmap xneg,
1373 Bitmap ypos,
1374 Bitmap yneg,
1375 Bitmap zpos,
1376 Bitmap zneg) {
1377 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1378 zpos, zneg, MipmapControl.MIPMAP_NONE,
1379 USAGE_GRAPHICS_TEXTURE);
1380 }
1381
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001382 /**
1383 * Creates a renderscript allocation from the bitmap referenced
1384 * by resource id
1385 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001386 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001387 * @param res application resources
1388 * @param id resource id to load the data from
1389 * @param mips specifies desired mipmap behaviour for the
1390 * allocation
1391 * @param usage bit field specifying how the allocation is
1392 * utilized
1393 *
1394 * @return renderscript allocation containing resource data
1395 *
1396 */
Jason Sams5476b452010-12-08 16:14:36 -08001397 static public Allocation createFromBitmapResource(RenderScript rs,
1398 Resources res,
1399 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001400 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001401 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001402
Jason Sams771bebb2009-12-07 12:40:12 -08001403 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001404 Bitmap b = BitmapFactory.decodeResource(res, id);
1405 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1406 b.recycle();
1407 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001408 }
1409
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001410 /**
1411 * Creates a non-mipmapped renderscript allocation to use as a
1412 * graphics texture from the bitmap referenced by resource id
1413 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001414 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001415 * @param res application resources
1416 * @param id resource id to load the data from
1417 *
1418 * @return renderscript allocation containing resource data
1419 *
1420 */
Jason Sams5476b452010-12-08 16:14:36 -08001421 static public Allocation createFromBitmapResource(RenderScript rs,
1422 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001423 int id) {
1424 return createFromBitmapResource(rs, res, id,
1425 MipmapControl.MIPMAP_NONE,
1426 USAGE_GRAPHICS_TEXTURE);
1427 }
1428
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001429 /**
1430 * Creates a renderscript allocation containing string data
1431 * encoded in UTF-8 format
1432 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001433 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001434 * @param str string to create the allocation from
1435 * @param usage bit field specifying how the allocaiton is
1436 * utilized
1437 *
1438 */
Jason Sams5476b452010-12-08 16:14:36 -08001439 static public Allocation createFromString(RenderScript rs,
1440 String str,
1441 int usage) {
1442 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001443 byte[] allocArray = null;
1444 try {
1445 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001446 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001447 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001448 return alloc;
1449 }
1450 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001451 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001452 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001453 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001454}
1455
1456