blob: 6b1d66f2afe1b26743577946723816aa348361a1 [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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070030/** @deprecated renderscript is deprecated in J
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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070096 /** @deprecated renderscript is deprecated in J
Jason Samsf7086092011-01-12 13:28:37 -080097 * The usage of the allocation. These signal to renderscript
98 * where to place the allocation in memory.
99 *
100 * SCRIPT The allocation will be bound to and accessed by
101 * scripts.
102 */
Jason Sams5476b452010-12-08 16:14:36 -0800103 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -0800104
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700105 /** @deprecated renderscript is deprecated in J
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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700112 /** @deprecated renderscript is deprecated in J
Jason Samsf7086092011-01-12 13:28:37 -0800113 * GRAPHICS_VERTEX The allocation will be used as a graphics
114 * mesh.
115 *
116 */
Jason Sams5476b452010-12-08 16:14:36 -0800117 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -0800118
119
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700120 /** @deprecated renderscript is deprecated in J
Jason Samsf7086092011-01-12 13:28:37 -0800121 * GRAPHICS_CONSTANTS The allocation will be used as the source
122 * of shader constants by one or more programs.
123 *
124 */
Jason Sams5476b452010-12-08 16:14:36 -0800125 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
126
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700127 /** @deprecated renderscript is deprecated in J
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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700134 /** @hide renderscript is deprecated in J
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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700142 /** @hide renderscript is deprecated in J
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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700150 /** @deprecated renderscript is deprecated in J
Jason Samsf7086092011-01-12 13:28:37 -0800151 * Controls mipmap behavior when using the bitmap creation and
152 * update functions.
153 */
Jason Sams4ef66502010-12-10 16:03:15 -0800154 public enum MipmapControl {
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700155 /** @deprecated renderscript is deprecated in J
Jason Samsf7086092011-01-12 13:28:37 -0800156 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700161 /** @deprecated renderscript is deprecated in J
Jason Samsf7086092011-01-12 13:28:37 -0800162 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700168 /** @deprecated renderscript is deprecated in J
Jason Samsf7086092011-01-12 13:28:37 -0800169 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700191 /** @hide renderscript is deprecated in J
Jason Sams03d2d002012-03-23 13:51:56 -0700192 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700202 /** @hide renderscript is deprecated in J
Jason Sams03d2d002012-03-23 13:51:56 -0700203 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700213 /** @hide renderscript is deprecated in J
Jason Sams36c0f642012-03-23 15:48:37 -0700214 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700329 /** @deprecated renderscript is deprecated in J
Jason Sams03d2d002012-03-23 13:51:56 -0700330 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700339 /** @deprecated renderscript is deprecated in J
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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700358 /** @hide renderscript is deprecated in J
Jason Sams163766c2012-02-15 12:04:24 -0800359 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700372 /** @deprecated renderscript is deprecated in J
Jason Samsc5f519c2012-03-29 17:58:15 -0700373 * Delete once code is updated.
374 * @hide
375 */
376 public void ioSendOutput() {
377 ioSend();
378 }
379
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700380 /** @hide renderscript is deprecated in J
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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700393 /** @deprecated renderscript is deprecated in J
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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700464 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800465 * 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 }
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700475 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800476 * 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 }
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700486 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800487 * 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 }
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700497 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800498 * Copy an allocation from an array. This variant is not type
499 * checked which allows an application to fill in structured
500 * data from an array.
501 *
502 * @param d the source data array
503 */
504 public void copyFromUnchecked(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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700509 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800510 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700521 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800522 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700533 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800534 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700545 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800546 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700557 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800558 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700570 /** @deprecated renderscript is deprecated in J
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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700590 /** @deprecated renderscript is deprecated in J
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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700637 /** @deprecated renderscript is deprecated in J
Jason Samsf7086092011-01-12 13:28:37 -0800638 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700651 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800652 * 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 }
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700665 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800666 * 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 }
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700679 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800680 * 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 }
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700693 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800694 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700708 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800709 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700722 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800723 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700736 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800737 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700750 /** @deprecated renderscript is deprecated in J
Jason Sams4fa3eed2011-01-19 15:44:38 -0800751 * 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 Sakhartchouka0c2eb22012-04-19 16:30:58 -0700764 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700765 * 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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700797 /** @deprecated renderscript is deprecated in J
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
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700814 /** @deprecated renderscript is deprecated in J
815 */
Jason Samsf7086092011-01-12 13:28:37 -0800816 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800817 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800818 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700819 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700820 w, h, data, data.length * 2);
Jason Samsfa445b92011-01-07 17:00:07 -0800821 }
822
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700823 /** @deprecated renderscript is deprecated in J
824 */
Jason Samsf7086092011-01-12 13:28:37 -0800825 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800826 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800827 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700828 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700829 w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700830 }
831
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700832 /** @deprecated renderscript is deprecated in J
833 */
Jason Samsf7086092011-01-12 13:28:37 -0800834 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800835 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800836 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700837 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
Jason Samsba862d12011-07-07 15:24:42 -0700838 w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700839 }
840
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700841 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700842 * Copy a rectangular region into the allocation from another
843 * allocation.
844 *
845 * @param xoff X offset of the region to update.
846 * @param yoff Y offset of the region to update.
847 * @param w Width of the incoming region to update.
848 * @param h Height of the incoming region to update.
849 * @param data source allocation.
850 * @param dataXoff X offset in data of the region to update.
851 * @param dataYoff Y offset in data of the region to update.
852 */
853 public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
854 Allocation data, int dataXoff, int dataYoff) {
855 mRS.validate();
856 validate2DRange(xoff, yoff, w, h);
Jason Sams48fe5342011-07-08 13:52:30 -0700857 mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
Jason Samsba862d12011-07-07 15:24:42 -0700858 mSelectedLOD, mSelectedFace.mID,
Jason Samse07694b2012-04-03 15:36:36 -0700859 w, h, data.getID(mRS), dataXoff, dataYoff,
Jason Samsba862d12011-07-07 15:24:42 -0700860 data.mSelectedLOD, data.mSelectedFace.mID);
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700861 }
862
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700863 /** @deprecated renderscript is deprecated in J
Jason Samsf7086092011-01-12 13:28:37 -0800864 * Copy a bitmap into an allocation. The height and width of
865 * the update will use the height and width of the incoming
866 * bitmap.
867 *
868 * @param xoff X offset of the region to update
869 * @param yoff Y offset of the region to update
870 * @param data the bitmap to be copied
871 */
872 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800873 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800874 validateBitmapFormat(data);
875 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Sams48fe5342011-07-08 13:52:30 -0700876 mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
Jason Samsfa445b92011-01-07 17:00:07 -0800877 }
878
879
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700880 /** @deprecated renderscript is deprecated in J
Jason Sams48fe5342011-07-08 13:52:30 -0700881 * Copy from the Allocation into a Bitmap. The bitmap must
882 * match the dimensions of the Allocation.
883 *
884 * @param b The bitmap to be set from the Allocation.
885 */
Jason Samsfa445b92011-01-07 17:00:07 -0800886 public void copyTo(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800887 mRS.validate();
888 validateBitmapFormat(b);
889 validateBitmapSize(b);
Jason Samse07694b2012-04-03 15:36:36 -0700890 mRS.nAllocationCopyToBitmap(getID(mRS), b);
Jason Samsfa445b92011-01-07 17:00:07 -0800891 }
892
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700893 /** @deprecated renderscript is deprecated in J
Jason Sams48fe5342011-07-08 13:52:30 -0700894 * Copy from the Allocation into a byte array. The array must
895 * be at least as large as the Allocation. The allocation must
896 * be of an 8 bit elemental type.
897 *
898 * @param d The array to be set from the Allocation.
899 */
Jason Samsfa445b92011-01-07 17:00:07 -0800900 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800901 validateIsInt8();
Jason Sams771bebb2009-12-07 12:40:12 -0800902 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700903 mRS.nAllocationRead(getID(mRS), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700904 }
905
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700906 /** @deprecated renderscript is deprecated in J
Jason Sams48fe5342011-07-08 13:52:30 -0700907 * Copy from the Allocation into a short array. The array must
908 * be at least as large as the Allocation. The allocation must
909 * be of an 16 bit elemental type.
910 *
911 * @param d The array to be set from the Allocation.
912 */
Jason Samsfa445b92011-01-07 17:00:07 -0800913 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800914 validateIsInt16();
Jason Samsfa445b92011-01-07 17:00:07 -0800915 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700916 mRS.nAllocationRead(getID(mRS), d);
Jason Samsfa445b92011-01-07 17:00:07 -0800917 }
918
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700919 /** @deprecated renderscript is deprecated in J
Jason Sams48fe5342011-07-08 13:52:30 -0700920 * Copy from the Allocation into a int array. The array must be
921 * at least as large as the Allocation. The allocation must be
922 * of an 32 bit elemental type.
923 *
924 * @param d The array to be set from the Allocation.
925 */
Jason Samsfa445b92011-01-07 17:00:07 -0800926 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800927 validateIsInt32();
Jason Samsfa445b92011-01-07 17:00:07 -0800928 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700929 mRS.nAllocationRead(getID(mRS), d);
Jason Samsfa445b92011-01-07 17:00:07 -0800930 }
931
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700932 /** @deprecated renderscript is deprecated in J
Jason Sams48fe5342011-07-08 13:52:30 -0700933 * Copy from the Allocation into a float array. The array must
934 * be at least as large as the Allocation. The allocation must
935 * be of an 32 bit float elemental type.
936 *
937 * @param d The array to be set from the Allocation.
938 */
Jason Samsfa445b92011-01-07 17:00:07 -0800939 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800940 validateIsFloat32();
Jason Sams771bebb2009-12-07 12:40:12 -0800941 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700942 mRS.nAllocationRead(getID(mRS), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700943 }
944
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700945 /** @deprecated renderscript is deprecated in J
Jason Samsf7086092011-01-12 13:28:37 -0800946 * Resize a 1D allocation. The contents of the allocation are
947 * preserved. If new elements are allocated objects are created
948 * with null contents and the new region is otherwise undefined.
949 *
950 * If the new region is smaller the references of any objects
951 * outside the new region will be released.
952 *
953 * A new type will be created with the new dimension.
954 *
955 * @param dimX The new size of the allocation.
956 */
Jason Sams31a7e422010-10-26 13:09:17 -0700957 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800958 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800959 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700960 }
Jason Samse07694b2012-04-03 15:36:36 -0700961 mRS.nAllocationResize1D(getID(mRS), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -0700962 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -0700963
Jason Samse07694b2012-04-03 15:36:36 -0700964 int typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams31a7e422010-10-26 13:09:17 -0700965 mType = new Type(typeID, mRS);
966 mType.updateFromNative();
Jason Sams452a7662011-07-07 16:05:18 -0700967 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -0700968 }
969
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700970 /** @deprecated renderscript is deprecated in J
Jason Sams163766c2012-02-15 12:04:24 -0800971 * Resize a 2D allocation. The contents of the allocation are
972 * preserved. If new elements are allocated objects are created
973 * with null contents and the new region is otherwise undefined.
974 *
975 * If the new region is smaller the references of any objects
976 * outside the new region will be released.
977 *
978 * A new type will be created with the new dimension.
979 *
980 * @hide
981 * @param dimX The new size of the allocation.
982 * @param dimY The new size of the allocation.
983 */
Jason Sams5edc6082010-10-05 13:32:49 -0700984 public void resize(int dimX, int dimY) {
Jason Sams163766c2012-02-15 12:04:24 -0800985 if ((mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
986 throw new RSInvalidStateException(
987 "Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700988 }
989 if (mType.getY() == 0) {
Jason Sams163766c2012-02-15 12:04:24 -0800990 throw new RSInvalidStateException(
991 "Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700992 }
Jason Samse07694b2012-04-03 15:36:36 -0700993 mRS.nAllocationResize2D(getID(mRS), dimX, dimY);
Jason Sams163766c2012-02-15 12:04:24 -0800994 mRS.finish(); // Necessary because resize is fifoed and update is async.
995
Jason Samse07694b2012-04-03 15:36:36 -0700996 int typeID = mRS.nAllocationGetType(getID(mRS));
Jason Sams163766c2012-02-15 12:04:24 -0800997 mType = new Type(typeID, mRS);
998 mType.updateFromNative();
999 updateCacheInfo(mType);
Jason Sams5edc6082010-10-05 13:32:49 -07001000 }
Jason Sams40a29e82009-08-10 14:55:26 -07001001
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001002
Jason Samsb8c5a842009-07-31 20:40:47 -07001003
1004 // creation
1005
Jason Sams49a05d72010-12-29 14:31:29 -08001006 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -07001007 static {
1008 mBitmapOptions.inScaled = false;
1009 }
1010
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001011 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001012 *
1013 * @param type renderscript type describing data layout
1014 * @param mips specifies desired mipmap behaviour for the
1015 * allocation
1016 * @param usage bit field specifying how the allocation is
1017 * utilized
1018 */
1019 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001020 rs.validate();
Jason Samse07694b2012-04-03 15:36:36 -07001021 if (type.getID(rs) == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001022 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -07001023 }
Jason Samse07694b2012-04-03 15:36:36 -07001024 int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
Jason Sams857d0c72011-11-23 15:02:15 -08001025 if (id == 0) {
1026 throw new RSRuntimeException("Allocation creation failed.");
1027 }
1028 return new Allocation(id, rs, type, usage);
1029 }
1030
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001031 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001032 * Creates a renderscript allocation with the size specified by
1033 * the type and no mipmaps generated by default
1034 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001035 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001036 * @param type renderscript type describing data layout
1037 * @param usage bit field specifying how the allocation is
1038 * utilized
1039 *
1040 * @return allocation
1041 */
Jason Samse5d37122010-12-16 00:33:33 -08001042 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1043 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1044 }
1045
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001046 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001047 * Creates a renderscript allocation for use by the script with
1048 * the size specified by the type and no mipmaps generated by
1049 * default
1050 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001051 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001052 * @param type renderscript type describing data layout
1053 *
1054 * @return allocation
1055 */
Jason Sams5476b452010-12-08 16:14:36 -08001056 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001057 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -08001058 }
Jason Sams1bada8c2009-08-09 17:01:55 -07001059
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001060 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001061 * Creates a renderscript allocation with a specified number of
1062 * given elements
1063 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001064 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001065 * @param e describes what each element of an allocation is
1066 * @param count specifies the number of element in the allocation
1067 * @param usage bit field specifying how the allocation is
1068 * utilized
1069 *
1070 * @return allocation
1071 */
Jason Sams5476b452010-12-08 16:14:36 -08001072 static public Allocation createSized(RenderScript rs, Element e,
1073 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001074 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -07001075 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001076 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -07001077 Type t = b.create();
1078
Jason Samse07694b2012-04-03 15:36:36 -07001079 int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
Jason Sams5476b452010-12-08 16:14:36 -08001080 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001081 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -07001082 }
Jason Sams5476b452010-12-08 16:14:36 -08001083 return new Allocation(id, rs, t, usage);
1084 }
1085
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001086 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001087 * Creates a renderscript allocation with a specified number of
1088 * given elements
1089 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001090 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001091 * @param e describes what each element of an allocation is
1092 * @param count specifies the number of element in the allocation
1093 *
1094 * @return allocation
1095 */
Jason Sams5476b452010-12-08 16:14:36 -08001096 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -08001097 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -07001098 }
1099
Jason Sams49a05d72010-12-29 14:31:29 -08001100 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -08001101 final Bitmap.Config bc = b.getConfig();
1102 if (bc == Bitmap.Config.ALPHA_8) {
1103 return Element.A_8(rs);
1104 }
1105 if (bc == Bitmap.Config.ARGB_4444) {
1106 return Element.RGBA_4444(rs);
1107 }
1108 if (bc == Bitmap.Config.ARGB_8888) {
1109 return Element.RGBA_8888(rs);
1110 }
1111 if (bc == Bitmap.Config.RGB_565) {
1112 return Element.RGB_565(rs);
1113 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -08001114 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -08001115 }
1116
Jason Sams49a05d72010-12-29 14:31:29 -08001117 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001118 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -08001119 Element e = elementFromBitmap(rs, b);
1120 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001121 tb.setX(b.getWidth());
1122 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -08001123 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -08001124 return tb.create();
1125 }
1126
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001127 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001128 * Creates a renderscript allocation from a bitmap
1129 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001130 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001131 * @param b bitmap source for the allocation data
1132 * @param mips specifies desired mipmap behaviour for the
1133 * allocation
1134 * @param usage bit field specifying how the allocation is
1135 * utilized
1136 *
1137 * @return renderscript allocation containing bitmap data
1138 *
1139 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001140 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001141 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001142 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -08001143 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001144 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -08001145
Jason Samse07694b2012-04-03 15:36:36 -07001146 int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Jason Sams5476b452010-12-08 16:14:36 -08001147 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -08001148 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -08001149 }
Jason Sams5476b452010-12-08 16:14:36 -08001150 return new Allocation(id, rs, t, usage);
1151 }
1152
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001153 /** @deprecated renderscript is deprecated in J
Jason Sams615e7ce2012-01-13 14:01:20 -08001154 *
1155 *
1156 * @hide
1157 *
1158 */
1159 public SurfaceTexture getSurfaceTexture() {
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001160 if ((mUsage & USAGE_IO_INPUT) == 0) {
Jason Sams615e7ce2012-01-13 14:01:20 -08001161 throw new RSInvalidStateException("Allocation is not a surface texture.");
1162 }
1163
Jason Samse07694b2012-04-03 15:36:36 -07001164 int id = mRS.nAllocationGetSurfaceTextureID(getID(mRS));
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001165 SurfaceTexture st = new SurfaceTexture(id);
Jason Samse07694b2012-04-03 15:36:36 -07001166 mRS.nAllocationGetSurfaceTextureID2(getID(mRS), st);
Jason Sams615e7ce2012-01-13 14:01:20 -08001167
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001168 return st;
Jason Sams615e7ce2012-01-13 14:01:20 -08001169 }
1170
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001171 /** @hide renderscript is deprecated in J
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001172 * For allocations used with io operations, returns the handle
1173 * onto a raw buffer that is being managed by the screen
1174 * compositor.
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001175 *
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001176 * @return Surface object associated with allocation
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001177 *
1178 */
1179 public Surface getSurface() {
1180 return new Surface(getSurfaceTexture());
1181 }
1182
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001183 /** @hide renderscript is deprecated in J
Alex Sakhartchouk918e8402012-04-11 14:04:23 -07001184 * Associate a surface for io output with this allocation
1185 *
1186 * @param sur Surface to associate with allocation
Jason Sams163766c2012-02-15 12:04:24 -08001187 */
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001188 public void setSurface(Surface sur) {
1189 mRS.validate();
Jason Sams163766c2012-02-15 12:04:24 -08001190 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1191 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1192 }
1193
Jason Samse07694b2012-04-03 15:36:36 -07001194 mRS.nAllocationSetSurface(getID(mRS), sur);
Jason Sams163766c2012-02-15 12:04:24 -08001195 }
1196
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001197 /** @deprecated renderscript is deprecated in J
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001198 * @hide
1199 */
1200 public void setSurfaceTexture(SurfaceTexture st) {
1201 mRS.validate();
1202 if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1203 throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1204 }
1205
1206 Surface s = new Surface(st);
Jason Samse07694b2012-04-03 15:36:36 -07001207 mRS.nAllocationSetSurface(getID(mRS), s);
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001208 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001209
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001210 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001211 * Creates a non-mipmapped renderscript allocation to use as a
1212 * graphics texture
1213 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001214 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001215 * @param b bitmap source for the allocation data
1216 *
1217 * @return renderscript allocation containing bitmap data
1218 *
1219 */
Jason Sams6d8eb262010-12-15 01:41:00 -08001220 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
1221 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1222 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -08001223 }
1224
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001225 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001226 * Creates a cubemap allocation from a bitmap containing the
1227 * horizontal list of cube faces. Each individual face must be
1228 * the same size and power of 2
1229 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001230 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001231 * @param b bitmap with cubemap faces layed out in the following
1232 * format: right, left, top, bottom, front, back
1233 * @param mips specifies desired mipmap behaviour for the cubemap
1234 * @param usage bit field specifying how the cubemap is utilized
1235 *
1236 * @return allocation containing cubemap data
1237 *
1238 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001239 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -08001240 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001241 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001242 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001243
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001244 int height = b.getHeight();
1245 int width = b.getWidth();
1246
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001247 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001248 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
1249 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001250 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001251 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001252 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001253 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001254 if (!isPow2) {
1255 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1256 }
1257
1258 Element e = elementFromBitmap(rs, b);
1259 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001260 tb.setX(height);
1261 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001262 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -08001263 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001264 Type t = tb.create();
1265
Jason Samse07694b2012-04-03 15:36:36 -07001266 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001267 if(id == 0) {
1268 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
1269 }
Jason Sams5476b452010-12-08 16:14:36 -08001270 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001271 }
1272
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001273 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001274 * Creates a non-mipmapped cubemap allocation for use as a
1275 * graphics texture from a bitmap containing the horizontal list
1276 * of cube faces. Each individual face must be the same size and
1277 * power of 2
1278 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001279 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001280 * @param b bitmap with cubemap faces layed out in the following
1281 * format: right, left, top, bottom, front, back
1282 *
1283 * @return allocation containing cubemap data
1284 *
1285 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001286 static public Allocation createCubemapFromBitmap(RenderScript rs,
1287 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -08001288 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -08001289 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -08001290 }
1291
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001292 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001293 * Creates a cubemap allocation from 6 bitmaps containing
1294 * the cube faces. All the faces must be the same size and
1295 * power of 2
1296 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001297 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001298 * @param xpos cubemap face in the positive x direction
1299 * @param xneg cubemap face in the negative x direction
1300 * @param ypos cubemap face in the positive y direction
1301 * @param yneg cubemap face in the negative y direction
1302 * @param zpos cubemap face in the positive z direction
1303 * @param zneg cubemap face in the negative z direction
1304 * @param mips specifies desired mipmap behaviour for the cubemap
1305 * @param usage bit field specifying how the cubemap is utilized
1306 *
1307 * @return allocation containing cubemap data
1308 *
1309 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001310 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1311 Bitmap xpos,
1312 Bitmap xneg,
1313 Bitmap ypos,
1314 Bitmap yneg,
1315 Bitmap zpos,
1316 Bitmap zneg,
1317 MipmapControl mips,
1318 int usage) {
1319 int height = xpos.getHeight();
1320 if (xpos.getWidth() != height ||
1321 xneg.getWidth() != height || xneg.getHeight() != height ||
1322 ypos.getWidth() != height || ypos.getHeight() != height ||
1323 yneg.getWidth() != height || yneg.getHeight() != height ||
1324 zpos.getWidth() != height || zpos.getHeight() != height ||
1325 zneg.getWidth() != height || zneg.getHeight() != height) {
1326 throw new RSIllegalArgumentException("Only square cube map faces supported");
1327 }
1328 boolean isPow2 = (height & (height - 1)) == 0;
1329 if (!isPow2) {
1330 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1331 }
1332
1333 Element e = elementFromBitmap(rs, xpos);
1334 Type.Builder tb = new Type.Builder(rs, e);
1335 tb.setX(height);
1336 tb.setY(height);
1337 tb.setFaces(true);
1338 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1339 Type t = tb.create();
1340 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1341
1342 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
Stephen Hines20fbd012011-06-16 17:44:53 -07001343 adapter.setFace(Type.CubemapFace.POSITIVE_X);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001344 adapter.copyFrom(xpos);
1345 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1346 adapter.copyFrom(xneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001347 adapter.setFace(Type.CubemapFace.POSITIVE_Y);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001348 adapter.copyFrom(ypos);
1349 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1350 adapter.copyFrom(yneg);
Stephen Hines20fbd012011-06-16 17:44:53 -07001351 adapter.setFace(Type.CubemapFace.POSITIVE_Z);
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001352 adapter.copyFrom(zpos);
1353 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1354 adapter.copyFrom(zneg);
1355
1356 return cubemap;
1357 }
1358
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001359 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001360 * Creates a non-mipmapped cubemap allocation for use as a
1361 * graphics texture from 6 bitmaps containing
1362 * the cube faces. All the faces must be the same size and
1363 * power of 2
1364 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001365 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001366 * @param xpos cubemap face in the positive x direction
1367 * @param xneg cubemap face in the negative x direction
1368 * @param ypos cubemap face in the positive y direction
1369 * @param yneg cubemap face in the negative y direction
1370 * @param zpos cubemap face in the positive z direction
1371 * @param zneg cubemap face in the negative z direction
1372 *
1373 * @return allocation containing cubemap data
1374 *
1375 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001376 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1377 Bitmap xpos,
1378 Bitmap xneg,
1379 Bitmap ypos,
1380 Bitmap yneg,
1381 Bitmap zpos,
1382 Bitmap zneg) {
1383 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1384 zpos, zneg, MipmapControl.MIPMAP_NONE,
1385 USAGE_GRAPHICS_TEXTURE);
1386 }
1387
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001388 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001389 * Creates a renderscript allocation from the bitmap referenced
1390 * by resource id
1391 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001392 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001393 * @param res application resources
1394 * @param id resource id to load the data from
1395 * @param mips specifies desired mipmap behaviour for the
1396 * allocation
1397 * @param usage bit field specifying how the allocation is
1398 * utilized
1399 *
1400 * @return renderscript allocation containing resource data
1401 *
1402 */
Jason Sams5476b452010-12-08 16:14:36 -08001403 static public Allocation createFromBitmapResource(RenderScript rs,
1404 Resources res,
1405 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001406 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001407 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001408
Jason Sams771bebb2009-12-07 12:40:12 -08001409 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001410 Bitmap b = BitmapFactory.decodeResource(res, id);
1411 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1412 b.recycle();
1413 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001414 }
1415
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001416 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001417 * Creates a non-mipmapped renderscript allocation to use as a
1418 * graphics texture from the bitmap referenced by resource id
1419 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001420 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001421 * @param res application resources
1422 * @param id resource id to load the data from
1423 *
1424 * @return renderscript allocation containing resource data
1425 *
1426 */
Jason Sams5476b452010-12-08 16:14:36 -08001427 static public Allocation createFromBitmapResource(RenderScript rs,
1428 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001429 int id) {
1430 return createFromBitmapResource(rs, res, id,
1431 MipmapControl.MIPMAP_NONE,
1432 USAGE_GRAPHICS_TEXTURE);
1433 }
1434
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -07001435 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001436 * Creates a renderscript allocation containing string data
1437 * encoded in UTF-8 format
1438 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001439 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001440 * @param str string to create the allocation from
1441 * @param usage bit field specifying how the allocaiton is
1442 * utilized
1443 *
1444 */
Jason Sams5476b452010-12-08 16:14:36 -08001445 static public Allocation createFromString(RenderScript rs,
1446 String str,
1447 int usage) {
1448 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001449 byte[] allocArray = null;
1450 try {
1451 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001452 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001453 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001454 return alloc;
1455 }
1456 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001457 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001458 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001459 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001460}
1461
1462