blob: 3dcfe88b4742d823e83dc8abd78c7f1fed60269b [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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 Samsb8c5a842009-07-31 20:40:47 -070025import android.util.Log;
Romain Guy650a3eb2009-08-31 14:06:43 -070026import android.util.TypedValue;
Jason Samsb8c5a842009-07-31 20:40:47 -070027
28/**
Jason Samsa23d4e72011-01-04 18:59:12 -080029 * Memory allocation class for renderscript. An allocation combines a Type with
30 * memory to provide storage for user data and objects.
31 *
32 * Allocations may exist in one or more memory spaces. Currently those are
33 * Script: accessable by RS scripts.
34 * Graphics Texture: accessable as a graphics texture.
35 * Graphics Vertex: accessable as graphical vertex data.
36 * Graphics Constants: Accessable as constants in user shaders
37 *
38 * By default java side updates are always applied to the script accessable
39 * memory. If this is not present they are then applied to the various HW
40 * memory types. A syncAll call is necessary after the script data is update to
41 * keep the other memory spaces in sync.
Jason Samsb8c5a842009-07-31 20:40:47 -070042 *
43 **/
44public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070045 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080046 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080047 int mUsage;
48
Jason Samsf7086092011-01-12 13:28:37 -080049 /**
50 * The usage of the allocation. These signal to renderscript
51 * where to place the allocation in memory.
52 *
53 * SCRIPT The allocation will be bound to and accessed by
54 * scripts.
55 */
Jason Sams5476b452010-12-08 16:14:36 -080056 public static final int USAGE_SCRIPT = 0x0001;
Jason Samsf7086092011-01-12 13:28:37 -080057
58 /**
59 * GRAPHICS_TEXTURE The allcation will be used as a texture
60 * source by one or more graphcics programs.
61 *
62 */
Jason Sams5476b452010-12-08 16:14:36 -080063 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
Jason Samsf7086092011-01-12 13:28:37 -080064
65 /**
66 * GRAPHICS_VERTEX The allocation will be used as a graphics
67 * mesh.
68 *
69 */
Jason Sams5476b452010-12-08 16:14:36 -080070 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
Jason Samsf7086092011-01-12 13:28:37 -080071
72
73 /**
74 * GRAPHICS_CONSTANTS The allocation will be used as the source
75 * of shader constants by one or more programs.
76 *
77 */
Jason Sams5476b452010-12-08 16:14:36 -080078 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
79
Jason Sams43ee06852009-08-12 17:54:11 -070080
Jason Samsf7086092011-01-12 13:28:37 -080081 /**
82 * Controls mipmap behavior when using the bitmap creation and
83 * update functions.
84 */
Jason Sams4ef66502010-12-10 16:03:15 -080085 public enum MipmapControl {
Jason Samsf7086092011-01-12 13:28:37 -080086 /**
87 * No mipmaps will be generated and the type generated from the
88 * incoming bitmap will not contain additional LODs.
89 */
Jason Sams5476b452010-12-08 16:14:36 -080090 MIPMAP_NONE(0),
Jason Samsf7086092011-01-12 13:28:37 -080091
92 /**
93 * A Full mipmap chain will be created in script memory. The
94 * type of the allocation will contain a full mipmap chain. On
95 * upload to graphics the full chain will be transfered.
96 */
Jason Sams5476b452010-12-08 16:14:36 -080097 MIPMAP_FULL(1),
Jason Samsf7086092011-01-12 13:28:37 -080098
99 /**
100 * The type of the allocation will be the same as MIPMAP_NONE.
101 * It will not contain mipmaps. On upload to graphics the
102 * graphics copy of the allocation data will contain a full
103 * mipmap chain generated from the top level in script memory.
104 */
Jason Sams5476b452010-12-08 16:14:36 -0800105 MIPMAP_ON_SYNC_TO_TEXTURE(2);
106
107 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -0800108 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -0800109 mID = id;
110 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700111 }
112
Jason Sams5476b452010-12-08 16:14:36 -0800113 Allocation(int id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700114 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -0800115 if ((usage & ~(USAGE_SCRIPT |
116 USAGE_GRAPHICS_TEXTURE |
117 USAGE_GRAPHICS_VERTEX |
118 USAGE_GRAPHICS_CONSTANTS)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -0800119 throw new RSIllegalArgumentException("Unknown usage specified.");
120 }
121 mType = t;
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700122 }
123
Jason Samsb97b2512011-01-16 15:04:08 -0800124 private void validateIsInt32() {
125 if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
126 (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
127 return;
128 }
129 throw new RSIllegalArgumentException(
130 "32 bit integer source does not match allocation type " + mType.mElement.mType);
131 }
132
133 private void validateIsInt16() {
134 if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
135 (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
136 return;
137 }
138 throw new RSIllegalArgumentException(
139 "16 bit integer source does not match allocation type " + mType.mElement.mType);
140 }
141
142 private void validateIsInt8() {
143 if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
144 (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
145 return;
146 }
147 throw new RSIllegalArgumentException(
148 "8 bit integer source does not match allocation type " + mType.mElement.mType);
149 }
150
151 private void validateIsFloat32() {
152 if (mType.mElement.mType == Element.DataType.FLOAT_32) {
153 return;
154 }
155 throw new RSIllegalArgumentException(
156 "32 bit float source does not match allocation type " + mType.mElement.mType);
157 }
158
159 private void validateIsObject() {
160 if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
161 (mType.mElement.mType == Element.DataType.RS_TYPE) ||
162 (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
163 (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
164 (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
165 (mType.mElement.mType == Element.DataType.RS_MESH) ||
166 (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
167 (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
168 (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
169 (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
170 return;
171 }
172 throw new RSIllegalArgumentException(
173 "Object source does not match allocation type " + mType.mElement.mType);
174 }
175
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700176 @Override
177 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800178 super.updateFromNative();
179 int typeID = mRS.nAllocationGetType(getID());
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700180 if(typeID != 0) {
181 mType = new Type(typeID, mRS);
182 mType.updateFromNative();
183 }
184 }
185
Jason Samsea87e962010-01-12 12:12:28 -0800186 public Type getType() {
187 return mType;
188 }
189
Jason Sams5476b452010-12-08 16:14:36 -0800190 public void syncAll(int srcLocation) {
191 switch (srcLocation) {
192 case USAGE_SCRIPT:
193 case USAGE_GRAPHICS_CONSTANTS:
194 case USAGE_GRAPHICS_TEXTURE:
195 case USAGE_GRAPHICS_VERTEX:
196 break;
197 default:
198 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
199 }
200 mRS.validate();
201 mRS.nAllocationSyncAll(getID(), srcLocation);
202 }
203
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800204 public void copyFrom(BaseObj[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800205 mRS.validate();
Jason Samsb97b2512011-01-16 15:04:08 -0800206 validateIsObject();
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800207 if (d.length != mType.getCount()) {
208 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
209 mType.getCount() + ", array length = " + d.length);
210 }
211 int i[] = new int[d.length];
212 for (int ct=0; ct < d.length; ct++) {
213 i[ct] = d[ct].getID();
214 }
Jason Samsed5bab92011-01-21 13:08:02 -0800215 copy1DRangeFromUnchecked(0, mType.getCount(), i);
Jason Samsb8c5a842009-07-31 20:40:47 -0700216 }
217
Jason Samsfb9f82c2011-01-12 14:53:25 -0800218 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800219 Bitmap.Config bc = b.getConfig();
220 switch (bc) {
221 case ALPHA_8:
222 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
223 throw new RSIllegalArgumentException("Allocation kind is " +
224 mType.getElement().mKind + ", type " +
225 mType.getElement().mType +
226 " of " + mType.getElement().getSizeBytes() +
227 " bytes, passed bitmap was " + bc);
228 }
229 break;
230 case ARGB_8888:
231 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
232 (mType.getElement().getSizeBytes() != 4)) {
233 throw new RSIllegalArgumentException("Allocation kind is " +
234 mType.getElement().mKind + ", type " +
235 mType.getElement().mType +
236 " of " + mType.getElement().getSizeBytes() +
237 " bytes, passed bitmap was " + bc);
238 }
239 break;
240 case RGB_565:
241 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
242 (mType.getElement().getSizeBytes() != 2)) {
243 throw new RSIllegalArgumentException("Allocation kind is " +
244 mType.getElement().mKind + ", type " +
245 mType.getElement().mType +
246 " of " + mType.getElement().getSizeBytes() +
247 " bytes, passed bitmap was " + bc);
248 }
249 break;
250 case ARGB_4444:
251 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
252 (mType.getElement().getSizeBytes() != 2)) {
253 throw new RSIllegalArgumentException("Allocation kind is " +
254 mType.getElement().mKind + ", type " +
255 mType.getElement().mType +
256 " of " + mType.getElement().getSizeBytes() +
257 " bytes, passed bitmap was " + bc);
258 }
259 break;
260
261 }
Jason Sams4ef66502010-12-10 16:03:15 -0800262 }
263
Jason Samsfb9f82c2011-01-12 14:53:25 -0800264 private void validateBitmapSize(Bitmap b) {
265 if(mType.getX() != b.getWidth() ||
266 mType.getY() != b.getHeight()) {
267 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
268 }
269 }
270
Jason Sams4fa3eed2011-01-19 15:44:38 -0800271 /**
272 * Copy an allocation from an array. This variant is not type
273 * checked which allows an application to fill in structured
274 * data from an array.
275 *
276 * @param d the source data array
277 */
278 public void copyFromUnchecked(int[] d) {
279 mRS.validate();
280 copy1DRangeFromUnchecked(0, mType.getCount(), d);
281 }
282 /**
283 * Copy an allocation from an array. This variant is not type
284 * checked which allows an application to fill in structured
285 * data from an array.
286 *
287 * @param d the source data array
288 */
289 public void copyFromUnchecked(short[] d) {
290 mRS.validate();
291 copy1DRangeFromUnchecked(0, mType.getCount(), d);
292 }
293 /**
294 * Copy an allocation from an array. This variant is not type
295 * checked which allows an application to fill in structured
296 * data from an array.
297 *
298 * @param d the source data array
299 */
300 public void copyFromUnchecked(byte[] d) {
301 mRS.validate();
302 copy1DRangeFromUnchecked(0, mType.getCount(), d);
303 }
304 /**
305 * Copy an allocation from an array. This variant is not type
306 * checked which allows an application to fill in structured
307 * data from an array.
308 *
309 * @param d the source data array
310 */
311 public void copyFromUnchecked(float[] d) {
312 mRS.validate();
313 copy1DRangeFromUnchecked(0, mType.getCount(), d);
314 }
315
316 /**
317 * Copy an allocation from an array. This variant is type
318 * checked and will generate exceptions if the Allocation type
319 * is not a 32 bit integer type.
320 *
321 * @param d the source data array
322 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800323 public void copyFrom(int[] d) {
324 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800325 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800326 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800327
328 /**
329 * Copy an allocation from an array. This variant is type
330 * checked and will generate exceptions if the Allocation type
331 * is not a 16 bit integer type.
332 *
333 * @param d the source data array
334 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800335 public void copyFrom(short[] d) {
336 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800337 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800338 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800339
340 /**
341 * Copy an allocation from an array. This variant is type
342 * checked and will generate exceptions if the Allocation type
343 * is not a 8 bit integer type.
344 *
345 * @param d the source data array
346 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800347 public void copyFrom(byte[] d) {
348 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800349 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800350 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800351
352 /**
353 * Copy an allocation from an array. This variant is type
354 * checked and will generate exceptions if the Allocation type
355 * is not a 32 bit float type.
356 *
357 * @param d the source data array
358 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800359 public void copyFrom(float[] d) {
360 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800361 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800362 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800363
364 /**
365 * Copy an allocation from a bitmap. The height, width, and
366 * format of the bitmap must match the existing allocation.
367 *
368 * @param b the source bitmap
369 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800370 public void copyFrom(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800371 mRS.validate();
372 validateBitmapSize(b);
373 validateBitmapFormat(b);
Jason Sams4ef66502010-12-10 16:03:15 -0800374 mRS.nAllocationCopyFromBitmap(getID(), b);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700375 }
376
Jason Samsfa445b92011-01-07 17:00:07 -0800377 /**
Jason Samsfa445b92011-01-07 17:00:07 -0800378 * This is only intended to be used by auto-generate code reflected from the
379 * renderscript script files.
380 *
381 * @param xoff
382 * @param fp
383 */
Jason Sams21b41032011-01-16 15:05:41 -0800384 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsa70f4162010-03-26 15:33:42 -0700385 int eSize = mType.mElement.getSizeBytes();
386 final byte[] data = fp.getData();
387
388 int count = data.length / eSize;
389 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800390 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700391 " not divisible by element size " + eSize + ".");
392 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700393 data1DChecks(xoff, count, data.length, data.length);
Jason Sams49a05d72010-12-29 14:31:29 -0800394 mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length);
Jason Sams49bdaf02010-08-31 13:50:42 -0700395 }
396
Jason Samsfa445b92011-01-07 17:00:07 -0800397 /**
Jason Samsfa445b92011-01-07 17:00:07 -0800398 * This is only intended to be used by auto-generate code reflected from the
399 * renderscript script files.
400 *
401 * @param xoff
402 * @param component_number
403 * @param fp
404 */
Jason Sams21b41032011-01-16 15:05:41 -0800405 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700406 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800407 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700408 }
409 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800410 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700411 }
412
413 final byte[] data = fp.getData();
414 int eSize = mType.mElement.mElements[component_number].getSizeBytes();
415
416 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800417 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700418 " does not match component size " + eSize + ".");
419 }
420
Jason Sams49a05d72010-12-29 14:31:29 -0800421 mRS.nAllocationElementData1D(getID(), xoff, 0, component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700422 }
423
Jason Sams768bc022009-09-21 19:41:04 -0700424 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800425 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700426 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800427 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700428 }
429 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800430 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700431 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800432 if((off + count) > mType.getCount()) {
433 throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() +
Jason Samsa70f4162010-03-26 15:33:42 -0700434 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700435 }
Jason Sams768bc022009-09-21 19:41:04 -0700436 if((len) < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800437 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700438 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700439 }
440
Jason Samsf7086092011-01-12 13:28:37 -0800441 /**
442 * Generate a mipmap chain. Requires the type of the allocation
443 * include mipmaps.
444 *
445 * This function will generate a complete set of mipmaps from
446 * the top level lod and place them into the script memoryspace.
447 *
448 * If the allocation is also using other memory spaces a
449 * followup sync will be required.
450 */
451 public void generateMipmaps() {
452 mRS.nAllocationGenerateMipmaps(getID());
453 }
454
Jason Sams4fa3eed2011-01-19 15:44:38 -0800455 /**
456 * Copy part of an allocation from an array. This variant is
457 * not type checked which allows an application to fill in
458 * structured data from an array.
459 *
460 * @param off The offset of the first element to be copied.
461 * @param count The number of elements to be copied.
462 * @param d the source data array
463 */
464 public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700465 int dataSize = mType.mElement.getSizeBytes() * count;
466 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams49a05d72010-12-29 14:31:29 -0800467 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700468 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800469 /**
470 * Copy part of an allocation from an array. This variant is
471 * not type checked which allows an application to fill in
472 * structured data from an array.
473 *
474 * @param off The offset of the first element to be copied.
475 * @param count The number of elements to be copied.
476 * @param d the source data array
477 */
478 public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700479 int dataSize = mType.mElement.getSizeBytes() * count;
480 data1DChecks(off, count, d.length * 2, dataSize);
Jason Sams49a05d72010-12-29 14:31:29 -0800481 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700482 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800483 /**
484 * Copy part of an allocation from an array. This variant is
485 * not type checked which allows an application to fill in
486 * structured data from an array.
487 *
488 * @param off The offset of the first element to be copied.
489 * @param count The number of elements to be copied.
490 * @param d the source data array
491 */
492 public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700493 int dataSize = mType.mElement.getSizeBytes() * count;
494 data1DChecks(off, count, d.length, dataSize);
Jason Sams49a05d72010-12-29 14:31:29 -0800495 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700496 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800497 /**
498 * Copy part of an allocation from an array. This variant is
499 * not type checked which allows an application to fill in
500 * structured data from an array.
501 *
502 * @param off The offset of the first element to be copied.
503 * @param count The number of elements to be copied.
504 * @param d the source data array
505 */
506 public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700507 int dataSize = mType.mElement.getSizeBytes() * count;
508 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams49a05d72010-12-29 14:31:29 -0800509 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700510 }
511
Jason Sams4fa3eed2011-01-19 15:44:38 -0800512 /**
513 * Copy part of an allocation from an array. This variant is
514 * type checked and will generate exceptions if the Allocation
515 * type is not a 32 bit integer type.
516 *
517 * @param off The offset of the first element to be copied.
518 * @param count The number of elements to be copied.
519 * @param d the source data array
520 */
Jason Samsb97b2512011-01-16 15:04:08 -0800521 public void copy1DRangeFrom(int off, int count, int[] d) {
522 validateIsInt32();
523 copy1DRangeFromUnchecked(off, count, d);
524 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800525
526 /**
527 * Copy part of an allocation from an array. This variant is
528 * type checked and will generate exceptions if the Allocation
529 * type is not a 16 bit integer type.
530 *
531 * @param off The offset of the first element to be copied.
532 * @param count The number of elements to be copied.
533 * @param d the source data array
534 */
Jason Samsb97b2512011-01-16 15:04:08 -0800535 public void copy1DRangeFrom(int off, int count, short[] d) {
536 validateIsInt16();
537 copy1DRangeFromUnchecked(off, count, d);
538 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800539
540 /**
541 * Copy part of an allocation from an array. This variant is
542 * type checked and will generate exceptions if the Allocation
543 * type is not a 8 bit integer type.
544 *
545 * @param off The offset of the first element to be copied.
546 * @param count The number of elements to be copied.
547 * @param d the source data array
548 */
Jason Samsb97b2512011-01-16 15:04:08 -0800549 public void copy1DRangeFrom(int off, int count, byte[] d) {
550 validateIsInt8();
551 copy1DRangeFromUnchecked(off, count, d);
552 }
Jason Sams4fa3eed2011-01-19 15:44:38 -0800553
554 /**
555 * Copy part of an allocation from an array. This variant is
556 * type checked and will generate exceptions if the Allocation
557 * type is not a 32 bit float type.
558 *
559 * @param off The offset of the first element to be copied.
560 * @param count The number of elements to be copied.
561 * @param d the source data array
562 */
Jason Samsb97b2512011-01-16 15:04:08 -0800563 public void copy1DRangeFrom(int off, int count, float[] d) {
564 validateIsFloat32();
565 copy1DRangeFromUnchecked(off, count, d);
566 }
567
Jason Samsfb9f82c2011-01-12 14:53:25 -0800568 private void validate2DRange(int xoff, int yoff, int w, int h) {
569 if (xoff < 0 || yoff < 0) {
570 throw new RSIllegalArgumentException("Offset cannot be negative.");
571 }
572 if (h < 0 || w < 0) {
573 throw new RSIllegalArgumentException("Height or width cannot be negative.");
574 }
575 if ((xoff + w) > mType.mDimX ||
576 (yoff + h) > mType.mDimY) {
577 throw new RSIllegalArgumentException("Updated region larger than allocation.");
578 }
579 }
Jason Sams768bc022009-09-21 19:41:04 -0700580
Jason Samsf7086092011-01-12 13:28:37 -0800581 /**
582 * Copy a rectanglular region from the array into the
583 * allocation. The incoming array is assumed to be tightly
584 * packed.
585 *
586 * @param xoff X offset of the region to update
587 * @param yoff Y offset of the region to update
588 * @param w Width of the incoming region to update
589 * @param h Height of the incoming region to update
590 * @param data to be placed into the allocation
591 */
592 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800593 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800594 validate2DRange(xoff, yoff, w, h);
Jason Samsf7086092011-01-12 13:28:37 -0800595 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -0800596 }
597
Jason Samsf7086092011-01-12 13:28:37 -0800598 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800599 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800600 validate2DRange(xoff, yoff, w, h);
Jason Samsf7086092011-01-12 13:28:37 -0800601 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 2);
Jason Samsfa445b92011-01-07 17:00:07 -0800602 }
603
Jason Samsf7086092011-01-12 13:28:37 -0800604 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800605 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800606 validate2DRange(xoff, yoff, w, h);
Jason Samsf7086092011-01-12 13:28:37 -0800607 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700608 }
609
Jason Samsf7086092011-01-12 13:28:37 -0800610 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800611 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800612 validate2DRange(xoff, yoff, w, h);
Jason Samsf7086092011-01-12 13:28:37 -0800613 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700614 }
615
Jason Samsf7086092011-01-12 13:28:37 -0800616 /**
617 * Copy a bitmap into an allocation. The height and width of
618 * the update will use the height and width of the incoming
619 * bitmap.
620 *
621 * @param xoff X offset of the region to update
622 * @param yoff Y offset of the region to update
623 * @param data the bitmap to be copied
624 */
625 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800626 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800627 validateBitmapFormat(data);
628 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Samsf7086092011-01-12 13:28:37 -0800629 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, data);
Jason Samsfa445b92011-01-07 17:00:07 -0800630 }
631
632
633 public void copyTo(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800634 mRS.validate();
635 validateBitmapFormat(b);
636 validateBitmapSize(b);
Jason Samsfa445b92011-01-07 17:00:07 -0800637 mRS.nAllocationCopyToBitmap(getID(), b);
638 }
639
640 public void copyTo(byte[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800641 validateIsInt8();
Jason Sams771bebb2009-12-07 12:40:12 -0800642 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800643 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700644 }
645
Jason Samsfa445b92011-01-07 17:00:07 -0800646 public void copyTo(short[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800647 validateIsInt16();
Jason Samsfa445b92011-01-07 17:00:07 -0800648 mRS.validate();
649 mRS.nAllocationRead(getID(), d);
650 }
651
652 public void copyTo(int[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800653 validateIsInt32();
Jason Samsfa445b92011-01-07 17:00:07 -0800654 mRS.validate();
655 mRS.nAllocationRead(getID(), d);
656 }
657
658 public void copyTo(float[] d) {
Jason Samsb97b2512011-01-16 15:04:08 -0800659 validateIsFloat32();
Jason Sams771bebb2009-12-07 12:40:12 -0800660 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800661 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700662 }
663
Jason Samsf7086092011-01-12 13:28:37 -0800664 /**
665 * Resize a 1D allocation. The contents of the allocation are
666 * preserved. If new elements are allocated objects are created
667 * with null contents and the new region is otherwise undefined.
668 *
669 * If the new region is smaller the references of any objects
670 * outside the new region will be released.
671 *
672 * A new type will be created with the new dimension.
673 *
674 * @param dimX The new size of the allocation.
675 */
Jason Sams31a7e422010-10-26 13:09:17 -0700676 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800677 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800678 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700679 }
Jason Sams06d69de2010-11-09 17:11:40 -0800680 mRS.nAllocationResize1D(getID(), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -0700681 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -0700682
Jason Sams06d69de2010-11-09 17:11:40 -0800683 int typeID = mRS.nAllocationGetType(getID());
Jason Sams31a7e422010-10-26 13:09:17 -0700684 mType = new Type(typeID, mRS);
685 mType.updateFromNative();
Jason Sams5edc6082010-10-05 13:32:49 -0700686 }
687
688 /*
689 public void resize(int dimX, int dimY) {
690 if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800691 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700692 }
693 if (mType.getY() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800694 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700695 }
Jason Sams06d69de2010-11-09 17:11:40 -0800696 mRS.nAllocationResize2D(getID(), dimX, dimY);
Jason Sams5edc6082010-10-05 13:32:49 -0700697 }
698 */
Jason Sams40a29e82009-08-10 14:55:26 -0700699
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700700
Jason Samsb8c5a842009-07-31 20:40:47 -0700701
702 // creation
703
Jason Sams49a05d72010-12-29 14:31:29 -0800704 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -0700705 static {
706 mBitmapOptions.inScaled = false;
707 }
708
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800709 /**
710 *
711 * @param type renderscript type describing data layout
712 * @param mips specifies desired mipmap behaviour for the
713 * allocation
714 * @param usage bit field specifying how the allocation is
715 * utilized
716 */
717 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800718 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800719 if (type.getID() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800720 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -0700721 }
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800722 int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800723 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800724 throw new RSRuntimeException("Allocation creation failed.");
725 }
Jason Sams5476b452010-12-08 16:14:36 -0800726 return new Allocation(id, rs, type, usage);
Jason Samsb8c5a842009-07-31 20:40:47 -0700727 }
728
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800729 /**
730 * Creates a renderscript allocation with the size specified by
731 * the type and no mipmaps generated by default
732 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800733 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800734 * @param type renderscript type describing data layout
735 * @param usage bit field specifying how the allocation is
736 * utilized
737 *
738 * @return allocation
739 */
Jason Samse5d37122010-12-16 00:33:33 -0800740 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
741 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
742 }
743
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800744 /**
745 * Creates a renderscript allocation for use by the script with
746 * the size specified by the type and no mipmaps generated by
747 * default
748 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800749 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800750 * @param type renderscript type describing data layout
751 *
752 * @return allocation
753 */
Jason Sams5476b452010-12-08 16:14:36 -0800754 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800755 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -0800756 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700757
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800758 /**
759 * Creates a renderscript allocation with a specified number of
760 * given elements
761 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800762 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800763 * @param e describes what each element of an allocation is
764 * @param count specifies the number of element in the allocation
765 * @param usage bit field specifying how the allocation is
766 * utilized
767 *
768 * @return allocation
769 */
Jason Sams5476b452010-12-08 16:14:36 -0800770 static public Allocation createSized(RenderScript rs, Element e,
771 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800772 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700773 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800774 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -0700775 Type t = b.create();
776
Jason Samsd4b23b52010-12-13 15:32:35 -0800777 int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800778 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800779 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700780 }
Jason Sams5476b452010-12-08 16:14:36 -0800781 return new Allocation(id, rs, t, usage);
782 }
783
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800784 /**
785 * Creates a renderscript allocation with a specified number of
786 * given elements
787 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800788 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800789 * @param e describes what each element of an allocation is
790 * @param count specifies the number of element in the allocation
791 *
792 * @return allocation
793 */
Jason Sams5476b452010-12-08 16:14:36 -0800794 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800795 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -0700796 }
797
Jason Sams49a05d72010-12-29 14:31:29 -0800798 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -0800799 final Bitmap.Config bc = b.getConfig();
800 if (bc == Bitmap.Config.ALPHA_8) {
801 return Element.A_8(rs);
802 }
803 if (bc == Bitmap.Config.ARGB_4444) {
804 return Element.RGBA_4444(rs);
805 }
806 if (bc == Bitmap.Config.ARGB_8888) {
807 return Element.RGBA_8888(rs);
808 }
809 if (bc == Bitmap.Config.RGB_565) {
810 return Element.RGB_565(rs);
811 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -0800812 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -0800813 }
814
Jason Sams49a05d72010-12-29 14:31:29 -0800815 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800816 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -0800817 Element e = elementFromBitmap(rs, b);
818 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800819 tb.setX(b.getWidth());
820 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -0800821 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -0800822 return tb.create();
823 }
824
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800825 /**
826 * Creates a renderscript allocation from a bitmap
827 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800828 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800829 * @param b bitmap source for the allocation data
830 * @param mips specifies desired mipmap behaviour for the
831 * allocation
832 * @param usage bit field specifying how the allocation is
833 * utilized
834 *
835 * @return renderscript allocation containing bitmap data
836 *
837 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800838 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800839 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800840 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800841 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800842 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -0800843
Jason Sams5476b452010-12-08 16:14:36 -0800844 int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
845 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800846 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -0800847 }
Jason Sams5476b452010-12-08 16:14:36 -0800848 return new Allocation(id, rs, t, usage);
849 }
850
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800851 /**
852 * Creates a non-mipmapped renderscript allocation to use as a
853 * graphics texture
854 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800855 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800856 * @param b bitmap source for the allocation data
857 *
858 * @return renderscript allocation containing bitmap data
859 *
860 */
Jason Sams6d8eb262010-12-15 01:41:00 -0800861 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
862 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
863 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -0800864 }
865
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800866 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800867 * Creates a cubemap allocation from a bitmap containing the
868 * horizontal list of cube faces. Each individual face must be
869 * the same size and power of 2
870 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800871 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800872 * @param b bitmap with cubemap faces layed out in the following
873 * format: right, left, top, bottom, front, back
874 * @param mips specifies desired mipmap behaviour for the cubemap
875 * @param usage bit field specifying how the cubemap is utilized
876 *
877 * @return allocation containing cubemap data
878 *
879 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800880 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800881 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800882 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800883 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800884
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800885 int height = b.getHeight();
886 int width = b.getWidth();
887
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800888 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800889 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
890 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800891 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800892 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800893 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800894 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800895 if (!isPow2) {
896 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
897 }
898
899 Element e = elementFromBitmap(rs, b);
900 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800901 tb.setX(height);
902 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800903 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -0800904 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800905 Type t = tb.create();
906
Jason Sams5476b452010-12-08 16:14:36 -0800907 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800908 if(id == 0) {
909 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
910 }
Jason Sams5476b452010-12-08 16:14:36 -0800911 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800912 }
913
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800914 /**
915 * Creates a non-mipmapped cubemap allocation for use as a
916 * graphics texture from a bitmap containing the horizontal list
917 * of cube faces. Each individual face must be the same size and
918 * power of 2
919 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800920 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800921 * @param b bitmap with cubemap faces layed out in the following
922 * format: right, left, top, bottom, front, back
923 *
924 * @return allocation containing cubemap data
925 *
926 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800927 static public Allocation createCubemapFromBitmap(RenderScript rs,
928 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -0800929 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800930 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -0800931 }
932
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800933 /**
934 * Creates a cubemap allocation from 6 bitmaps containing
935 * the cube faces. All the faces must be the same size and
936 * power of 2
937 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800938 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800939 * @param xpos cubemap face in the positive x direction
940 * @param xneg cubemap face in the negative x direction
941 * @param ypos cubemap face in the positive y direction
942 * @param yneg cubemap face in the negative y direction
943 * @param zpos cubemap face in the positive z direction
944 * @param zneg cubemap face in the negative z direction
945 * @param mips specifies desired mipmap behaviour for the cubemap
946 * @param usage bit field specifying how the cubemap is utilized
947 *
948 * @return allocation containing cubemap data
949 *
950 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800951 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
952 Bitmap xpos,
953 Bitmap xneg,
954 Bitmap ypos,
955 Bitmap yneg,
956 Bitmap zpos,
957 Bitmap zneg,
958 MipmapControl mips,
959 int usage) {
960 int height = xpos.getHeight();
961 if (xpos.getWidth() != height ||
962 xneg.getWidth() != height || xneg.getHeight() != height ||
963 ypos.getWidth() != height || ypos.getHeight() != height ||
964 yneg.getWidth() != height || yneg.getHeight() != height ||
965 zpos.getWidth() != height || zpos.getHeight() != height ||
966 zneg.getWidth() != height || zneg.getHeight() != height) {
967 throw new RSIllegalArgumentException("Only square cube map faces supported");
968 }
969 boolean isPow2 = (height & (height - 1)) == 0;
970 if (!isPow2) {
971 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
972 }
973
974 Element e = elementFromBitmap(rs, xpos);
975 Type.Builder tb = new Type.Builder(rs, e);
976 tb.setX(height);
977 tb.setY(height);
978 tb.setFaces(true);
979 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
980 Type t = tb.create();
981 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
982
983 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
984 adapter.setFace(Type.CubemapFace.POSITVE_X);
985 adapter.copyFrom(xpos);
986 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
987 adapter.copyFrom(xneg);
988 adapter.setFace(Type.CubemapFace.POSITVE_Y);
989 adapter.copyFrom(ypos);
990 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
991 adapter.copyFrom(yneg);
992 adapter.setFace(Type.CubemapFace.POSITVE_Z);
993 adapter.copyFrom(zpos);
994 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
995 adapter.copyFrom(zneg);
996
997 return cubemap;
998 }
999
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001000 /**
1001 * Creates a non-mipmapped cubemap allocation for use as a
1002 * graphics texture from 6 bitmaps containing
1003 * the cube faces. All the faces must be the same size and
1004 * power of 2
1005 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001006 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001007 * @param xpos cubemap face in the positive x direction
1008 * @param xneg cubemap face in the negative x direction
1009 * @param ypos cubemap face in the positive y direction
1010 * @param yneg cubemap face in the negative y direction
1011 * @param zpos cubemap face in the positive z direction
1012 * @param zneg cubemap face in the negative z direction
1013 *
1014 * @return allocation containing cubemap data
1015 *
1016 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -08001017 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1018 Bitmap xpos,
1019 Bitmap xneg,
1020 Bitmap ypos,
1021 Bitmap yneg,
1022 Bitmap zpos,
1023 Bitmap zneg) {
1024 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1025 zpos, zneg, MipmapControl.MIPMAP_NONE,
1026 USAGE_GRAPHICS_TEXTURE);
1027 }
1028
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001029 /**
1030 * Creates a renderscript allocation from the bitmap referenced
1031 * by resource id
1032 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001033 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001034 * @param res application resources
1035 * @param id resource id to load the data from
1036 * @param mips specifies desired mipmap behaviour for the
1037 * allocation
1038 * @param usage bit field specifying how the allocation is
1039 * utilized
1040 *
1041 * @return renderscript allocation containing resource data
1042 *
1043 */
Jason Sams5476b452010-12-08 16:14:36 -08001044 static public Allocation createFromBitmapResource(RenderScript rs,
1045 Resources res,
1046 int id,
Jason Sams4ef66502010-12-10 16:03:15 -08001047 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -08001048 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -07001049
Jason Sams771bebb2009-12-07 12:40:12 -08001050 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -08001051 Bitmap b = BitmapFactory.decodeResource(res, id);
1052 Allocation alloc = createFromBitmap(rs, b, mips, usage);
1053 b.recycle();
1054 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -07001055 }
1056
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001057 /**
1058 * Creates a non-mipmapped renderscript allocation to use as a
1059 * graphics texture from the bitmap referenced by resource id
1060 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001061 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001062 * @param res application resources
1063 * @param id resource id to load the data from
1064 *
1065 * @return renderscript allocation containing resource data
1066 *
1067 */
Jason Sams5476b452010-12-08 16:14:36 -08001068 static public Allocation createFromBitmapResource(RenderScript rs,
1069 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -08001070 int id) {
1071 return createFromBitmapResource(rs, res, id,
1072 MipmapControl.MIPMAP_NONE,
1073 USAGE_GRAPHICS_TEXTURE);
1074 }
1075
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001076 /**
1077 * Creates a renderscript allocation containing string data
1078 * encoded in UTF-8 format
1079 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -08001080 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -08001081 * @param str string to create the allocation from
1082 * @param usage bit field specifying how the allocaiton is
1083 * utilized
1084 *
1085 */
Jason Sams5476b452010-12-08 16:14:36 -08001086 static public Allocation createFromString(RenderScript rs,
1087 String str,
1088 int usage) {
1089 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001090 byte[] allocArray = null;
1091 try {
1092 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -08001093 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001094 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001095 return alloc;
1096 }
1097 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -08001098 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001099 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001100 }
Jason Samsb8c5a842009-07-31 20:40:47 -07001101}
1102
1103