blob: 0c33a5f85eaeead9f2b015e80cc0aa4e5e0f7a24 [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 Sams4ef66502010-12-10 16:03:15 -0800165 private void validateBitmap(Bitmap b) {
166 mRS.validate();
167 if(mType.getX() != b.getWidth() ||
168 mType.getY() != b.getHeight()) {
169 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
170 }
Jason Sams252c0782011-01-11 17:42:52 -0800171 Bitmap.Config bc = b.getConfig();
172 switch (bc) {
173 case ALPHA_8:
174 if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
175 throw new RSIllegalArgumentException("Allocation kind is " +
176 mType.getElement().mKind + ", type " +
177 mType.getElement().mType +
178 " of " + mType.getElement().getSizeBytes() +
179 " bytes, passed bitmap was " + bc);
180 }
181 break;
182 case ARGB_8888:
183 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
184 (mType.getElement().getSizeBytes() != 4)) {
185 throw new RSIllegalArgumentException("Allocation kind is " +
186 mType.getElement().mKind + ", type " +
187 mType.getElement().mType +
188 " of " + mType.getElement().getSizeBytes() +
189 " bytes, passed bitmap was " + bc);
190 }
191 break;
192 case RGB_565:
193 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
194 (mType.getElement().getSizeBytes() != 2)) {
195 throw new RSIllegalArgumentException("Allocation kind is " +
196 mType.getElement().mKind + ", type " +
197 mType.getElement().mType +
198 " of " + mType.getElement().getSizeBytes() +
199 " bytes, passed bitmap was " + bc);
200 }
201 break;
202 case ARGB_4444:
203 if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
204 (mType.getElement().getSizeBytes() != 2)) {
205 throw new RSIllegalArgumentException("Allocation kind is " +
206 mType.getElement().mKind + ", type " +
207 mType.getElement().mType +
208 " of " + mType.getElement().getSizeBytes() +
209 " bytes, passed bitmap was " + bc);
210 }
211 break;
212
213 }
Jason Sams4ef66502010-12-10 16:03:15 -0800214 }
215
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800216 public void copyFrom(int[] d) {
217 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800218 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800219 }
220 public void copyFrom(short[] d) {
221 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800222 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800223 }
224 public void copyFrom(byte[] d) {
225 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800226 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800227 }
228 public void copyFrom(float[] d) {
229 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800230 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800231 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800232 public void copyFrom(Bitmap b) {
Jason Sams4ef66502010-12-10 16:03:15 -0800233 validateBitmap(b);
234 mRS.nAllocationCopyFromBitmap(getID(), b);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700235 }
236
Jason Samsfa445b92011-01-07 17:00:07 -0800237 /**
238 * @hide
239 *
240 * This is only intended to be used by auto-generate code reflected from the
241 * renderscript script files.
242 *
243 * @param xoff
244 * @param fp
245 */
246 public void setOneElement(int xoff, FieldPacker fp) {
Jason Samsa70f4162010-03-26 15:33:42 -0700247 int eSize = mType.mElement.getSizeBytes();
248 final byte[] data = fp.getData();
249
250 int count = data.length / eSize;
251 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800252 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700253 " not divisible by element size " + eSize + ".");
254 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700255 data1DChecks(xoff, count, data.length, data.length);
Jason Sams49a05d72010-12-29 14:31:29 -0800256 mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length);
Jason Sams49bdaf02010-08-31 13:50:42 -0700257 }
258
259
Jason Samsfa445b92011-01-07 17:00:07 -0800260 /**
261 * @hide
262 *
263 * 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 */
270 public void setOneComponent(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 Sams768bc022009-09-21 19:41:04 -0700341
Jason Samsf7086092011-01-12 13:28:37 -0800342 /**
343 * Copy a rectanglular region from the array into the
344 * allocation. The incoming array is assumed to be tightly
345 * packed.
346 *
347 * @param xoff X offset of the region to update
348 * @param yoff Y offset of the region to update
349 * @param w Width of the incoming region to update
350 * @param h Height of the incoming region to update
351 * @param data to be placed into the allocation
352 */
353 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800354 mRS.validate();
Jason Samsf7086092011-01-12 13:28:37 -0800355 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length);
Jason Samsfa445b92011-01-07 17:00:07 -0800356 }
357
Jason Samsf7086092011-01-12 13:28:37 -0800358 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800359 mRS.validate();
Jason Samsf7086092011-01-12 13:28:37 -0800360 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 2);
Jason Samsfa445b92011-01-07 17:00:07 -0800361 }
362
Jason Samsf7086092011-01-12 13:28:37 -0800363 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800364 mRS.validate();
Jason Samsf7086092011-01-12 13:28:37 -0800365 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700366 }
367
Jason Samsf7086092011-01-12 13:28:37 -0800368 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
Jason Sams771bebb2009-12-07 12:40:12 -0800369 mRS.validate();
Jason Samsf7086092011-01-12 13:28:37 -0800370 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700371 }
372
Jason Samsf7086092011-01-12 13:28:37 -0800373 /**
374 * Copy a bitmap into an allocation. The height and width of
375 * the update will use the height and width of the incoming
376 * bitmap.
377 *
378 * @param xoff X offset of the region to update
379 * @param yoff Y offset of the region to update
380 * @param data the bitmap to be copied
381 */
382 public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
Jason Samsfa445b92011-01-07 17:00:07 -0800383 mRS.validate();
Jason Samsf7086092011-01-12 13:28:37 -0800384 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, data);
Jason Samsfa445b92011-01-07 17:00:07 -0800385 }
386
387
388 public void copyTo(Bitmap b) {
389 validateBitmap(b);
390 mRS.nAllocationCopyToBitmap(getID(), b);
391 }
392
393 public void copyTo(byte[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800394 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800395 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700396 }
397
Jason Samsfa445b92011-01-07 17:00:07 -0800398 public void copyTo(short[] d) {
399 mRS.validate();
400 mRS.nAllocationRead(getID(), d);
401 }
402
403 public void copyTo(int[] d) {
404 mRS.validate();
405 mRS.nAllocationRead(getID(), d);
406 }
407
408 public void copyTo(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800409 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800410 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700411 }
412
Jason Samsf7086092011-01-12 13:28:37 -0800413 /**
414 * Resize a 1D allocation. The contents of the allocation are
415 * preserved. If new elements are allocated objects are created
416 * with null contents and the new region is otherwise undefined.
417 *
418 * If the new region is smaller the references of any objects
419 * outside the new region will be released.
420 *
421 * A new type will be created with the new dimension.
422 *
423 * @param dimX The new size of the allocation.
424 */
Jason Sams31a7e422010-10-26 13:09:17 -0700425 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800426 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800427 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700428 }
Jason Sams06d69de2010-11-09 17:11:40 -0800429 mRS.nAllocationResize1D(getID(), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -0700430 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -0700431
Jason Sams06d69de2010-11-09 17:11:40 -0800432 int typeID = mRS.nAllocationGetType(getID());
Jason Sams31a7e422010-10-26 13:09:17 -0700433 mType = new Type(typeID, mRS);
434 mType.updateFromNative();
Jason Sams5edc6082010-10-05 13:32:49 -0700435 }
436
437 /*
438 public void resize(int dimX, int dimY) {
439 if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800440 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700441 }
442 if (mType.getY() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800443 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700444 }
Jason Sams06d69de2010-11-09 17:11:40 -0800445 mRS.nAllocationResize2D(getID(), dimX, dimY);
Jason Sams5edc6082010-10-05 13:32:49 -0700446 }
447 */
Jason Sams40a29e82009-08-10 14:55:26 -0700448
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700449
Jason Samsb8c5a842009-07-31 20:40:47 -0700450
451 // creation
452
Jason Sams49a05d72010-12-29 14:31:29 -0800453 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -0700454 static {
455 mBitmapOptions.inScaled = false;
456 }
457
Jason Samsd4b23b52010-12-13 15:32:35 -0800458 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mc, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800459 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800460 if (type.getID() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800461 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -0700462 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800463 int id = rs.nAllocationCreateTyped(type.getID(), mc.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800464 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800465 throw new RSRuntimeException("Allocation creation failed.");
466 }
Jason Sams5476b452010-12-08 16:14:36 -0800467 return new Allocation(id, rs, type, usage);
Jason Samsb8c5a842009-07-31 20:40:47 -0700468 }
469
Jason Samse5d37122010-12-16 00:33:33 -0800470 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
471 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
472 }
473
Jason Sams5476b452010-12-08 16:14:36 -0800474 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800475 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -0800476 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700477
Jason Sams5476b452010-12-08 16:14:36 -0800478 static public Allocation createSized(RenderScript rs, Element e,
479 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800480 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700481 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800482 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -0700483 Type t = b.create();
484
Jason Samsd4b23b52010-12-13 15:32:35 -0800485 int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800486 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800487 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700488 }
Jason Sams5476b452010-12-08 16:14:36 -0800489 return new Allocation(id, rs, t, usage);
490 }
491
492 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800493 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -0700494 }
495
Jason Sams49a05d72010-12-29 14:31:29 -0800496 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -0800497 final Bitmap.Config bc = b.getConfig();
498 if (bc == Bitmap.Config.ALPHA_8) {
499 return Element.A_8(rs);
500 }
501 if (bc == Bitmap.Config.ARGB_4444) {
502 return Element.RGBA_4444(rs);
503 }
504 if (bc == Bitmap.Config.ARGB_8888) {
505 return Element.RGBA_8888(rs);
506 }
507 if (bc == Bitmap.Config.RGB_565) {
508 return Element.RGB_565(rs);
509 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -0800510 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -0800511 }
512
Jason Sams49a05d72010-12-29 14:31:29 -0800513 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800514 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -0800515 Element e = elementFromBitmap(rs, b);
516 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800517 tb.setX(b.getWidth());
518 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -0800519 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -0800520 return tb.create();
521 }
522
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800523 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800524 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800525 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800526 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800527 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -0800528
Jason Sams5476b452010-12-08 16:14:36 -0800529 int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
530 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800531 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -0800532 }
Jason Sams5476b452010-12-08 16:14:36 -0800533 return new Allocation(id, rs, t, usage);
534 }
535
Jason Sams6d8eb262010-12-15 01:41:00 -0800536 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
537 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
538 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -0800539 }
540
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800541 /**
542 * Creates a cubemap allocation from a bitmap containing the
543 * horizontal list of cube faces. Each individual face must be
544 * the same size and power of 2
545 *
546 * @param rs
547 * @param b bitmap with cubemap faces layed out in the following
548 * format: right, left, top, bottom, front, back
549 * @param mips specifies desired mipmap behaviour for the cubemap
550 * @param usage bitfield specifying how the cubemap is utilized
551 *
552 **/
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800553 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800554 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800555 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800556 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800557
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800558 int height = b.getHeight();
559 int width = b.getWidth();
560
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800561 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800562 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
563 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800564 if (width / 6 != height) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800565 throw new RSIllegalArgumentException("Only square cube map faces supported");
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800566 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800567 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800568 if (!isPow2) {
569 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
570 }
571
572 Element e = elementFromBitmap(rs, b);
573 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800574 tb.setX(height);
575 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800576 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -0800577 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800578 Type t = tb.create();
579
Jason Sams5476b452010-12-08 16:14:36 -0800580 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800581 if(id == 0) {
582 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
583 }
Jason Sams5476b452010-12-08 16:14:36 -0800584 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800585 }
586
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800587 static public Allocation createCubemapFromBitmap(RenderScript rs,
588 Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -0800589 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800590 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -0800591 }
592
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800593 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
594 Bitmap xpos,
595 Bitmap xneg,
596 Bitmap ypos,
597 Bitmap yneg,
598 Bitmap zpos,
599 Bitmap zneg,
600 MipmapControl mips,
601 int usage) {
602 int height = xpos.getHeight();
603 if (xpos.getWidth() != height ||
604 xneg.getWidth() != height || xneg.getHeight() != height ||
605 ypos.getWidth() != height || ypos.getHeight() != height ||
606 yneg.getWidth() != height || yneg.getHeight() != height ||
607 zpos.getWidth() != height || zpos.getHeight() != height ||
608 zneg.getWidth() != height || zneg.getHeight() != height) {
609 throw new RSIllegalArgumentException("Only square cube map faces supported");
610 }
611 boolean isPow2 = (height & (height - 1)) == 0;
612 if (!isPow2) {
613 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
614 }
615
616 Element e = elementFromBitmap(rs, xpos);
617 Type.Builder tb = new Type.Builder(rs, e);
618 tb.setX(height);
619 tb.setY(height);
620 tb.setFaces(true);
621 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
622 Type t = tb.create();
623 Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
624
625 AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
626 adapter.setFace(Type.CubemapFace.POSITVE_X);
627 adapter.copyFrom(xpos);
628 adapter.setFace(Type.CubemapFace.NEGATIVE_X);
629 adapter.copyFrom(xneg);
630 adapter.setFace(Type.CubemapFace.POSITVE_Y);
631 adapter.copyFrom(ypos);
632 adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
633 adapter.copyFrom(yneg);
634 adapter.setFace(Type.CubemapFace.POSITVE_Z);
635 adapter.copyFrom(zpos);
636 adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
637 adapter.copyFrom(zneg);
638
639 return cubemap;
640 }
641
642 static public Allocation createCubemapFromCubeFaces(RenderScript rs,
643 Bitmap xpos,
644 Bitmap xneg,
645 Bitmap ypos,
646 Bitmap yneg,
647 Bitmap zpos,
648 Bitmap zneg) {
649 return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
650 zpos, zneg, MipmapControl.MIPMAP_NONE,
651 USAGE_GRAPHICS_TEXTURE);
652 }
653
Jason Sams5476b452010-12-08 16:14:36 -0800654 static public Allocation createFromBitmapResource(RenderScript rs,
655 Resources res,
656 int id,
Jason Sams4ef66502010-12-10 16:03:15 -0800657 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800658 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -0700659
Jason Sams771bebb2009-12-07 12:40:12 -0800660 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800661 Bitmap b = BitmapFactory.decodeResource(res, id);
662 Allocation alloc = createFromBitmap(rs, b, mips, usage);
663 b.recycle();
664 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -0700665 }
666
Jason Sams5476b452010-12-08 16:14:36 -0800667 static public Allocation createFromBitmapResource(RenderScript rs,
668 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -0800669 int id) {
670 return createFromBitmapResource(rs, res, id,
671 MipmapControl.MIPMAP_NONE,
672 USAGE_GRAPHICS_TEXTURE);
673 }
674
Jason Sams5476b452010-12-08 16:14:36 -0800675 static public Allocation createFromString(RenderScript rs,
676 String str,
677 int usage) {
678 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700679 byte[] allocArray = null;
680 try {
681 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -0800682 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800683 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700684 return alloc;
685 }
686 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -0800687 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700688 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700689 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700690}
691
692