blob: 852aabf5858f28f5e48607575e7bd2f54c81dffe [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
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700124 @Override
125 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800126 super.updateFromNative();
127 int typeID = mRS.nAllocationGetType(getID());
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700128 if(typeID != 0) {
129 mType = new Type(typeID, mRS);
130 mType.updateFromNative();
131 }
132 }
133
Jason Samsea87e962010-01-12 12:12:28 -0800134 public Type getType() {
135 return mType;
136 }
137
Jason Sams5476b452010-12-08 16:14:36 -0800138 public void syncAll(int srcLocation) {
139 switch (srcLocation) {
140 case USAGE_SCRIPT:
141 case USAGE_GRAPHICS_CONSTANTS:
142 case USAGE_GRAPHICS_TEXTURE:
143 case USAGE_GRAPHICS_VERTEX:
144 break;
145 default:
146 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
147 }
148 mRS.validate();
149 mRS.nAllocationSyncAll(getID(), srcLocation);
150 }
151
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800152 public void copyFrom(BaseObj[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800153 mRS.validate();
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800154 if (d.length != mType.getCount()) {
155 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
156 mType.getCount() + ", array length = " + d.length);
157 }
158 int i[] = new int[d.length];
159 for (int ct=0; ct < d.length; ct++) {
160 i[ct] = d[ct].getID();
161 }
Jason Samsfa445b92011-01-07 17:00:07 -0800162 copy1DRangeFrom(0, mType.getCount(), i);
Jason Samsb8c5a842009-07-31 20:40:47 -0700163 }
164
Jason Samsfb9f82c2011-01-12 14:53:25 -0800165 private void validateBitmapFormat(Bitmap b) {
Jason Sams252c0782011-01-11 17:42:52 -0800166 Bitmap.Config bc = b.getConfig();
167 switch (bc) {
168 case ALPHA_8:
169 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
170 throw new RSIllegalArgumentException("Allocation kind is " +
171 mType.getElement().mKind + ", type " +
172 mType.getElement().mType +
173 " of " + mType.getElement().getSizeBytes() +
174 " bytes, passed bitmap was " + bc);
175 }
176 break;
177 case ARGB_8888:
178 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
179 (mType.getElement().getSizeBytes() != 4)) {
180 throw new RSIllegalArgumentException("Allocation kind is " +
181 mType.getElement().mKind + ", type " +
182 mType.getElement().mType +
183 " of " + mType.getElement().getSizeBytes() +
184 " bytes, passed bitmap was " + bc);
185 }
186 break;
187 case RGB_565:
188 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
189 (mType.getElement().getSizeBytes() != 2)) {
190 throw new RSIllegalArgumentException("Allocation kind is " +
191 mType.getElement().mKind + ", type " +
192 mType.getElement().mType +
193 " of " + mType.getElement().getSizeBytes() +
194 " bytes, passed bitmap was " + bc);
195 }
196 break;
197 case ARGB_4444:
198 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
199 (mType.getElement().getSizeBytes() != 2)) {
200 throw new RSIllegalArgumentException("Allocation kind is " +
201 mType.getElement().mKind + ", type " +
202 mType.getElement().mType +
203 " of " + mType.getElement().getSizeBytes() +
204 " bytes, passed bitmap was " + bc);
205 }
206 break;
207
208 }
Jason Sams4ef66502010-12-10 16:03:15 -0800209 }
210
Jason Samsfb9f82c2011-01-12 14:53:25 -0800211 private void validateBitmapSize(Bitmap b) {
212 if(mType.getX() != b.getWidth() ||
213 mType.getY() != b.getHeight()) {
214 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
215 }
216 }
217
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800218 public void copyFrom(int[] d) {
219 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800220 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800221 }
222 public void copyFrom(short[] d) {
223 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800224 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800225 }
226 public void copyFrom(byte[] d) {
227 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800228 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800229 }
230 public void copyFrom(float[] d) {
231 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800232 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800233 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800234 public void copyFrom(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800235 mRS.validate();
236 validateBitmapSize(b);
237 validateBitmapFormat(b);
Jason Sams4ef66502010-12-10 16:03:15 -0800238 mRS.nAllocationCopyFromBitmap(getID(), b);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700239 }
240
Jason Samsfa445b92011-01-07 17:00:07 -0800241 /**
Jason Samsfa445b92011-01-07 17:00:07 -0800242 * This is only intended to be used by auto-generate code reflected from the
243 * renderscript script files.
244 *
245 * @param xoff
246 * @param fp
247 */
Jason Sams21b41032011-01-16 15:05:41 -0800248 public void setFromFieldPacker(int xoff, FieldPacker fp) {
Jason Samsa70f4162010-03-26 15:33:42 -0700249 int eSize = mType.mElement.getSizeBytes();
250 final byte[] data = fp.getData();
251
252 int count = data.length / eSize;
253 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800254 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700255 " not divisible by element size " + eSize + ".");
256 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700257 data1DChecks(xoff, count, data.length, data.length);
Jason Sams49a05d72010-12-29 14:31:29 -0800258 mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length);
Jason Sams49bdaf02010-08-31 13:50:42 -0700259 }
260
261
Jason Samsfa445b92011-01-07 17:00:07 -0800262 /**
Jason Samsfa445b92011-01-07 17:00:07 -0800263 * This is only intended to be used by auto-generate code reflected from the
264 * renderscript script files.
265 *
266 * @param xoff
267 * @param component_number
268 * @param fp
269 */
Jason Sams21b41032011-01-16 15:05:41 -0800270 public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700271 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800272 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700273 }
274 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800275 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700276 }
277
278 final byte[] data = fp.getData();
279 int eSize = mType.mElement.mElements[component_number].getSizeBytes();
280
281 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800282 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700283 " does not match component size " + eSize + ".");
284 }
285
Jason Sams49a05d72010-12-29 14:31:29 -0800286 mRS.nAllocationElementData1D(getID(), xoff, 0, component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700287 }
288
Jason Sams768bc022009-09-21 19:41:04 -0700289 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800290 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700291 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800292 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700293 }
294 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800295 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700296 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800297 if((off + count) > mType.getCount()) {
298 throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() +
Jason Samsa70f4162010-03-26 15:33:42 -0700299 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700300 }
Jason Sams768bc022009-09-21 19:41:04 -0700301 if((len) < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800302 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700303 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700304 }
305
Jason Samsf7086092011-01-12 13:28:37 -0800306 /**
307 * Generate a mipmap chain. Requires the type of the allocation
308 * include mipmaps.
309 *
310 * This function will generate a complete set of mipmaps from
311 * the top level lod and place them into the script memoryspace.
312 *
313 * If the allocation is also using other memory spaces a
314 * followup sync will be required.
315 */
316 public void generateMipmaps() {
317 mRS.nAllocationGenerateMipmaps(getID());
318 }
319
Jason Samsfa445b92011-01-07 17:00:07 -0800320 public void copy1DRangeFrom(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700321 int dataSize = mType.mElement.getSizeBytes() * count;
322 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams49a05d72010-12-29 14:31:29 -0800323 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700324 }
Jason Samsfa445b92011-01-07 17:00:07 -0800325 public void copy1DRangeFrom(int off, int count, short[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700326 int dataSize = mType.mElement.getSizeBytes() * count;
327 data1DChecks(off, count, d.length * 2, dataSize);
Jason Sams49a05d72010-12-29 14:31:29 -0800328 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700329 }
Jason Samsfa445b92011-01-07 17:00:07 -0800330 public void copy1DRangeFrom(int off, int count, byte[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700331 int dataSize = mType.mElement.getSizeBytes() * count;
332 data1DChecks(off, count, d.length, dataSize);
Jason Sams49a05d72010-12-29 14:31:29 -0800333 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700334 }
Jason Samsfa445b92011-01-07 17:00:07 -0800335 public void copy1DRangeFrom(int off, int count, float[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700336 int dataSize = mType.mElement.getSizeBytes() * count;
337 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams49a05d72010-12-29 14:31:29 -0800338 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700339 }
340
Jason Samsfb9f82c2011-01-12 14:53:25 -0800341 private void validate2DRange(int xoff, int yoff, int w, int h) {
342 if (xoff < 0 || yoff < 0) {
343 throw new RSIllegalArgumentException("Offset cannot be negative.");
344 }
345 if (h < 0 || w < 0) {
346 throw new RSIllegalArgumentException("Height or width cannot be negative.");
347 }
348 if ((xoff + w) > mType.mDimX ||
349 (yoff + h) > mType.mDimY) {
350 throw new RSIllegalArgumentException("Updated region larger than allocation.");
351 }
352 }
Jason Sams768bc022009-09-21 19:41:04 -0700353
Jason Samsf7086092011-01-12 13:28:37 -0800354 /**
355 * Copy a rectanglular region from the array into the
356 * allocation. The incoming array is assumed to be tightly
357 * packed.
358 *
359 * @param xoff X offset of the region to update
360 * @param yoff Y offset of the region to update
361 * @param w Width of the incoming region to update
362 * @param h Height of the incoming region to update
363 * @param data to be placed into the allocation
364 */
365 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800366 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800367 validate2DRange(xoff, yoff, w, h);
Jason Samsf7086092011-01-12 13:28:37 -0800368 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -0800369 }
370
Jason Samsf7086092011-01-12 13:28:37 -0800371 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800372 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800373 validate2DRange(xoff, yoff, w, h);
Jason Samsf7086092011-01-12 13:28:37 -0800374 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 2);
Jason Samsfa445b92011-01-07 17:00:07 -0800375 }
376
Jason Samsf7086092011-01-12 13:28:37 -0800377 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800378 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800379 validate2DRange(xoff, yoff, w, h);
Jason Samsf7086092011-01-12 13:28:37 -0800380 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700381 }
382
Jason Samsf7086092011-01-12 13:28:37 -0800383 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800384 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800385 validate2DRange(xoff, yoff, w, h);
Jason Samsf7086092011-01-12 13:28:37 -0800386 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700387 }
388
Jason Samsf7086092011-01-12 13:28:37 -0800389 /**
390 * Copy a bitmap into an allocation. The height and width of
391 * the update will use the height and width of the incoming
392 * bitmap.
393 *
394 * @param xoff X offset of the region to update
395 * @param yoff Y offset of the region to update
396 * @param data the bitmap to be copied
397 */
398 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800399 mRS.validate();
Jason Samsfb9f82c2011-01-12 14:53:25 -0800400 validateBitmapFormat(data);
401 validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
Jason Samsf7086092011-01-12 13:28:37 -0800402 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, data);
Jason Samsfa445b92011-01-07 17:00:07 -0800403 }
404
405
406 public void copyTo(Bitmap b) {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800407 mRS.validate();
408 validateBitmapFormat(b);
409 validateBitmapSize(b);
Jason Samsfa445b92011-01-07 17:00:07 -0800410 mRS.nAllocationCopyToBitmap(getID(), b);
411 }
412
413 public void copyTo(byte[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800414 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800415 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700416 }
417
Jason Samsfa445b92011-01-07 17:00:07 -0800418 public void copyTo(short[] d) {
419 mRS.validate();
420 mRS.nAllocationRead(getID(), d);
421 }
422
423 public void copyTo(int[] d) {
424 mRS.validate();
425 mRS.nAllocationRead(getID(), d);
426 }
427
428 public void copyTo(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800429 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800430 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700431 }
432
Jason Samsf7086092011-01-12 13:28:37 -0800433 /**
434 * Resize a 1D allocation. The contents of the allocation are
435 * preserved. If new elements are allocated objects are created
436 * with null contents and the new region is otherwise undefined.
437 *
438 * If the new region is smaller the references of any objects
439 * outside the new region will be released.
440 *
441 * A new type will be created with the new dimension.
442 *
443 * @param dimX The new size of the allocation.
444 */
Jason Sams31a7e422010-10-26 13:09:17 -0700445 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800446 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800447 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700448 }
Jason Sams06d69de2010-11-09 17:11:40 -0800449 mRS.nAllocationResize1D(getID(), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -0700450 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -0700451
Jason Sams06d69de2010-11-09 17:11:40 -0800452 int typeID = mRS.nAllocationGetType(getID());
Jason Sams31a7e422010-10-26 13:09:17 -0700453 mType = new Type(typeID, mRS);
454 mType.updateFromNative();
Jason Sams5edc6082010-10-05 13:32:49 -0700455 }
456
457 /*
458 public void resize(int dimX, int dimY) {
459 if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800460 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700461 }
462 if (mType.getY() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800463 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700464 }
Jason Sams06d69de2010-11-09 17:11:40 -0800465 mRS.nAllocationResize2D(getID(), dimX, dimY);
Jason Sams5edc6082010-10-05 13:32:49 -0700466 }
467 */
Jason Sams40a29e82009-08-10 14:55:26 -0700468
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700469
Jason Samsb8c5a842009-07-31 20:40:47 -0700470
471 // creation
472
Jason Sams49a05d72010-12-29 14:31:29 -0800473 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -0700474 static {
475 mBitmapOptions.inScaled = false;
476 }
477
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800478 /**
479 *
480 * @param type renderscript type describing data layout
481 * @param mips specifies desired mipmap behaviour for the
482 * allocation
483 * @param usage bit field specifying how the allocation is
484 * utilized
485 */
486 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800487 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800488 if (type.getID() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800489 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -0700490 }
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800491 int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800492 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800493 throw new RSRuntimeException("Allocation creation failed.");
494 }
Jason Sams5476b452010-12-08 16:14:36 -0800495 return new Allocation(id, rs, type, usage);
Jason Samsb8c5a842009-07-31 20:40:47 -0700496 }
497
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800498 /**
499 * Creates a renderscript allocation with the size specified by
500 * the type and no mipmaps generated by default
501 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800502 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800503 * @param type renderscript type describing data layout
504 * @param usage bit field specifying how the allocation is
505 * utilized
506 *
507 * @return allocation
508 */
Jason Samse5d37122010-12-16 00:33:33 -0800509 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
510 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
511 }
512
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800513 /**
514 * Creates a renderscript allocation for use by the script with
515 * the size specified by the type and no mipmaps generated by
516 * default
517 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800518 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800519 * @param type renderscript type describing data layout
520 *
521 * @return allocation
522 */
Jason Sams5476b452010-12-08 16:14:36 -0800523 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800524 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -0800525 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700526
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800527 /**
528 * Creates a renderscript allocation with a specified number of
529 * given elements
530 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800531 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800532 * @param e describes what each element of an allocation is
533 * @param count specifies the number of element in the allocation
534 * @param usage bit field specifying how the allocation is
535 * utilized
536 *
537 * @return allocation
538 */
Jason Sams5476b452010-12-08 16:14:36 -0800539 static public Allocation createSized(RenderScript rs, Element e,
540 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800541 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700542 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800543 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -0700544 Type t = b.create();
545
Jason Samsd4b23b52010-12-13 15:32:35 -0800546 int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800547 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800548 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700549 }
Jason Sams5476b452010-12-08 16:14:36 -0800550 return new Allocation(id, rs, t, usage);
551 }
552
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800553 /**
554 * Creates a renderscript allocation with a specified number of
555 * given elements
556 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800557 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800558 * @param e describes what each element of an allocation is
559 * @param count specifies the number of element in the allocation
560 *
561 * @return allocation
562 */
Jason Sams5476b452010-12-08 16:14:36 -0800563 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800564 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -0700565 }
566
Jason Sams49a05d72010-12-29 14:31:29 -0800567 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -0800568 final Bitmap.Config bc = b.getConfig();
569 if (bc == Bitmap.Config.ALPHA_8) {
570 return Element.A_8(rs);
571 }
572 if (bc == Bitmap.Config.ARGB_4444) {
573 return Element.RGBA_4444(rs);
574 }
575 if (bc == Bitmap.Config.ARGB_8888) {
576 return Element.RGBA_8888(rs);
577 }
578 if (bc == Bitmap.Config.RGB_565) {
579 return Element.RGB_565(rs);
580 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -0800581 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -0800582 }
583
Jason Sams49a05d72010-12-29 14:31:29 -0800584 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800585 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -0800586 Element e = elementFromBitmap(rs, b);
587 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800588 tb.setX(b.getWidth());
589 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -0800590 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -0800591 return tb.create();
592 }
593
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800594 /**
595 * Creates a renderscript allocation from a bitmap
596 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800597 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800598 * @param b bitmap source for the allocation data
599 * @param mips specifies desired mipmap behaviour for the
600 * allocation
601 * @param usage bit field specifying how the allocation is
602 * utilized
603 *
604 * @return renderscript allocation containing bitmap data
605 *
606 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800607 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800608 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800609 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800610 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800611 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -0800612
Jason Sams5476b452010-12-08 16:14:36 -0800613 int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
614 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800615 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -0800616 }
Jason Sams5476b452010-12-08 16:14:36 -0800617 return new Allocation(id, rs, t, usage);
618 }
619
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800620 /**
621 * Creates a non-mipmapped renderscript allocation to use as a
622 * graphics texture
623 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800624 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800625 * @param b bitmap source for the allocation data
626 *
627 * @return renderscript allocation containing bitmap data
628 *
629 */
Jason Sams6d8eb262010-12-15 01:41:00 -0800630 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
631 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
632 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -0800633 }
634
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800635 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800636 * Creates a cubemap allocation from a bitmap containing the
637 * horizontal list of cube faces. Each individual face must be
638 * the same size and power of 2
639 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800640 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800641 * @param b bitmap with cubemap faces layed out in the following
642 * format: right, left, top, bottom, front, back
643 * @param mips specifies desired mipmap behaviour for the cubemap
644 * @param usage bit field specifying how the cubemap is utilized
645 *
646 * @return allocation containing cubemap data
647 *
648 */
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800649 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800650 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800651 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800652 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800653
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800654 int height = b.getHeight();
655 int width = b.getWidth();
656
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800657 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800658 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
659 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800660 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800661 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800662 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800663 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800664 if (!isPow2) {
665 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
666 }
667
668 Element e = elementFromBitmap(rs, b);
669 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800670 tb.setX(height);
671 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800672 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -0800673 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800674 Type t = tb.create();
675
Jason Sams5476b452010-12-08 16:14:36 -0800676 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800677 if(id == 0) {
678 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
679 }
Jason Sams5476b452010-12-08 16:14:36 -0800680 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800681 }
682
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800683 /**
684 * Creates a non-mipmapped cubemap allocation for use as a
685 * graphics texture from a bitmap containing the horizontal list
686 * of cube faces. Each individual face must be the same size and
687 * power of 2
688 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800689 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800690 * @param b bitmap with cubemap faces layed out in the following
691 * format: right, left, top, bottom, front, back
692 *
693 * @return allocation containing cubemap data
694 *
695 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800696 static public Allocation createCubemapFromBitmap(RenderScript rs,
697 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -0800698 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800699 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -0800700 }
701
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800702 /**
703 * Creates a cubemap allocation from 6 bitmaps containing
704 * the cube faces. All the faces must be the same size and
705 * power of 2
706 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800707 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800708 * @param xpos cubemap face in the positive x direction
709 * @param xneg cubemap face in the negative x direction
710 * @param ypos cubemap face in the positive y direction
711 * @param yneg cubemap face in the negative y direction
712 * @param zpos cubemap face in the positive z direction
713 * @param zneg cubemap face in the negative z direction
714 * @param mips specifies desired mipmap behaviour for the cubemap
715 * @param usage bit field specifying how the cubemap is utilized
716 *
717 * @return allocation containing cubemap data
718 *
719 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800720 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
721 Bitmap xpos,
722 Bitmap xneg,
723 Bitmap ypos,
724 Bitmap yneg,
725 Bitmap zpos,
726 Bitmap zneg,
727 MipmapControl mips,
728 int usage) {
729 int height = xpos.getHeight();
730 if (xpos.getWidth() != height ||
731 xneg.getWidth() != height || xneg.getHeight() != height ||
732 ypos.getWidth() != height || ypos.getHeight() != height ||
733 yneg.getWidth() != height || yneg.getHeight() != height ||
734 zpos.getWidth() != height || zpos.getHeight() != height ||
735 zneg.getWidth() != height || zneg.getHeight() != height) {
736 throw new RSIllegalArgumentException("Only square cube map faces supported");
737 }
738 boolean isPow2 = (height & (height - 1)) == 0;
739 if (!isPow2) {
740 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
741 }
742
743 Element e = elementFromBitmap(rs, xpos);
744 Type.Builder tb = new Type.Builder(rs, e);
745 tb.setX(height);
746 tb.setY(height);
747 tb.setFaces(true);
748 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
749 Type t = tb.create();
750 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
751
752 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
753 adapter.setFace(Type.CubemapFace.POSITVE_X);
754 adapter.copyFrom(xpos);
755 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
756 adapter.copyFrom(xneg);
757 adapter.setFace(Type.CubemapFace.POSITVE_Y);
758 adapter.copyFrom(ypos);
759 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
760 adapter.copyFrom(yneg);
761 adapter.setFace(Type.CubemapFace.POSITVE_Z);
762 adapter.copyFrom(zpos);
763 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
764 adapter.copyFrom(zneg);
765
766 return cubemap;
767 }
768
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800769 /**
770 * Creates a non-mipmapped cubemap allocation for use as a
771 * graphics texture from 6 bitmaps containing
772 * the cube faces. All the faces must be the same size and
773 * power of 2
774 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800775 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800776 * @param xpos cubemap face in the positive x direction
777 * @param xneg cubemap face in the negative x direction
778 * @param ypos cubemap face in the positive y direction
779 * @param yneg cubemap face in the negative y direction
780 * @param zpos cubemap face in the positive z direction
781 * @param zneg cubemap face in the negative z direction
782 *
783 * @return allocation containing cubemap data
784 *
785 */
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800786 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
787 Bitmap xpos,
788 Bitmap xneg,
789 Bitmap ypos,
790 Bitmap yneg,
791 Bitmap zpos,
792 Bitmap zneg) {
793 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
794 zpos, zneg, MipmapControl.MIPMAP_NONE,
795 USAGE_GRAPHICS_TEXTURE);
796 }
797
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800798 /**
799 * Creates a renderscript allocation from the bitmap referenced
800 * by resource id
801 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800802 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800803 * @param res application resources
804 * @param id resource id to load the data from
805 * @param mips specifies desired mipmap behaviour for the
806 * allocation
807 * @param usage bit field specifying how the allocation is
808 * utilized
809 *
810 * @return renderscript allocation containing resource data
811 *
812 */
Jason Sams5476b452010-12-08 16:14:36 -0800813 static public Allocation createFromBitmapResource(RenderScript rs,
814 Resources res,
815 int id,
Jason Sams4ef66502010-12-10 16:03:15 -0800816 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800817 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -0700818
Jason Sams771bebb2009-12-07 12:40:12 -0800819 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800820 Bitmap b = BitmapFactory.decodeResource(res, id);
821 Allocation alloc = createFromBitmap(rs, b, mips, usage);
822 b.recycle();
823 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -0700824 }
825
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800826 /**
827 * Creates a non-mipmapped renderscript allocation to use as a
828 * graphics texture from the bitmap referenced by resource id
829 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800830 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800831 * @param res application resources
832 * @param id resource id to load the data from
833 *
834 * @return renderscript allocation containing resource data
835 *
836 */
Jason Sams5476b452010-12-08 16:14:36 -0800837 static public Allocation createFromBitmapResource(RenderScript rs,
838 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -0800839 int id) {
840 return createFromBitmapResource(rs, res, id,
841 MipmapControl.MIPMAP_NONE,
842 USAGE_GRAPHICS_TEXTURE);
843 }
844
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800845 /**
846 * Creates a renderscript allocation containing string data
847 * encoded in UTF-8 format
848 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800849 * @param rs Context to which the allocation will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800850 * @param str string to create the allocation from
851 * @param usage bit field specifying how the allocaiton is
852 * utilized
853 *
854 */
Jason Sams5476b452010-12-08 16:14:36 -0800855 static public Allocation createFromString(RenderScript rs,
856 String str,
857 int usage) {
858 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700859 byte[] allocArray = null;
860 try {
861 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -0800862 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800863 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700864 return alloc;
865 }
866 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -0800867 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700868 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700869 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700870}
871
872