blob: a4ce947d49954bc2f9f78c88905500b64c30c51a [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;
21
22import android.content.res.Resources;
Romain Guy650a3eb2009-08-31 14:06:43 -070023import android.content.res.AssetManager;
Jason Samsb8c5a842009-07-31 20:40:47 -070024import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
Jason Samsb8c5a842009-07-31 20:40:47 -070026import android.util.Log;
Romain Guy650a3eb2009-08-31 14:06:43 -070027import android.util.TypedValue;
Jason Samsb8c5a842009-07-31 20:40:47 -070028
29/**
Jason Samsa23d4e72011-01-04 18:59:12 -080030 * Memory allocation class for renderscript. An allocation combines a Type with
31 * memory to provide storage for user data and objects.
32 *
33 * Allocations may exist in one or more memory spaces. Currently those are
34 * Script: accessable by RS scripts.
35 * Graphics Texture: accessable as a graphics texture.
36 * Graphics Vertex: accessable as graphical vertex data.
37 * Graphics Constants: Accessable as constants in user shaders
38 *
39 * By default java side updates are always applied to the script accessable
40 * memory. If this is not present they are then applied to the various HW
41 * memory types. A syncAll call is necessary after the script data is update to
42 * keep the other memory spaces in sync.
Jason Samsb8c5a842009-07-31 20:40:47 -070043 *
44 **/
45public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070046 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080047 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080048 int mUsage;
49
50 public static final int USAGE_SCRIPT = 0x0001;
51 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
52 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
53 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
54
Jason Sams43ee06852009-08-12 17:54:11 -070055
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080056 public enum CubemapLayout {
57 VERTICAL_FACE_LIST (0),
58 HORIZONTAL_FACE_LIST (1),
59 VERTICAL_CROSS (2),
60 HORIZONTAL_CROSS (3);
61
62 int mID;
63 CubemapLayout(int id) {
64 mID = id;
65 }
66 }
67
Jason Sams49a05d72010-12-29 14:31:29 -080068
Jason Sams4ef66502010-12-10 16:03:15 -080069 public enum MipmapControl {
Jason Sams5476b452010-12-08 16:14:36 -080070 MIPMAP_NONE(0),
71 MIPMAP_FULL(1),
72 MIPMAP_ON_SYNC_TO_TEXTURE(2);
73
74 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -080075 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -080076 mID = id;
77 }
Jason Samsb8c5a842009-07-31 20:40:47 -070078 }
79
Jason Sams5476b452010-12-08 16:14:36 -080080 Allocation(int id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070081 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -080082 if ((usage & ~(USAGE_SCRIPT |
83 USAGE_GRAPHICS_TEXTURE |
84 USAGE_GRAPHICS_VERTEX |
85 USAGE_GRAPHICS_CONSTANTS)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -080086 throw new RSIllegalArgumentException("Unknown usage specified.");
87 }
88 mType = t;
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -070089 }
90
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070091 @Override
92 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -080093 super.updateFromNative();
94 int typeID = mRS.nAllocationGetType(getID());
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070095 if(typeID != 0) {
96 mType = new Type(typeID, mRS);
97 mType.updateFromNative();
98 }
99 }
100
Jason Samsea87e962010-01-12 12:12:28 -0800101 public Type getType() {
102 return mType;
103 }
104
Jason Sams5476b452010-12-08 16:14:36 -0800105 public void syncAll(int srcLocation) {
106 switch (srcLocation) {
107 case USAGE_SCRIPT:
108 case USAGE_GRAPHICS_CONSTANTS:
109 case USAGE_GRAPHICS_TEXTURE:
110 case USAGE_GRAPHICS_VERTEX:
111 break;
112 default:
113 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
114 }
115 mRS.validate();
116 mRS.nAllocationSyncAll(getID(), srcLocation);
117 }
118
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800119 public void copyFrom(BaseObj[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800120 mRS.validate();
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800121 if (d.length != mType.getCount()) {
122 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
123 mType.getCount() + ", array length = " + d.length);
124 }
125 int i[] = new int[d.length];
126 for (int ct=0; ct < d.length; ct++) {
127 i[ct] = d[ct].getID();
128 }
129 subData1D(0, mType.getCount(), i);
Jason Samsb8c5a842009-07-31 20:40:47 -0700130 }
131
Jason Sams4ef66502010-12-10 16:03:15 -0800132 private void validateBitmap(Bitmap b) {
133 mRS.validate();
134 if(mType.getX() != b.getWidth() ||
135 mType.getY() != b.getHeight()) {
136 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
137 }
138 }
139
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800140 public void copyFrom(int[] d) {
141 mRS.validate();
142 subData1D(0, mType.getCount(), d);
143 }
144 public void copyFrom(short[] d) {
145 mRS.validate();
146 subData1D(0, mType.getCount(), d);
147 }
148 public void copyFrom(byte[] d) {
149 mRS.validate();
150 subData1D(0, mType.getCount(), d);
151 }
152 public void copyFrom(float[] d) {
153 mRS.validate();
154 subData1D(0, mType.getCount(), d);
155 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800156 public void copyFrom(Bitmap b) {
Jason Sams4ef66502010-12-10 16:03:15 -0800157 validateBitmap(b);
158 mRS.nAllocationCopyFromBitmap(getID(), b);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700159 }
160
Jason Sams4ef66502010-12-10 16:03:15 -0800161 public void copyTo(Bitmap b) {
162 validateBitmap(b);
163 mRS.nAllocationCopyToBitmap(getID(), b);
164 }
165
166
Jason Sams49bdaf02010-08-31 13:50:42 -0700167 public void subData(int xoff, FieldPacker fp) {
Jason Samsa70f4162010-03-26 15:33:42 -0700168 int eSize = mType.mElement.getSizeBytes();
169 final byte[] data = fp.getData();
170
171 int count = data.length / eSize;
172 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800173 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700174 " not divisible by element size " + eSize + ".");
175 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700176 data1DChecks(xoff, count, data.length, data.length);
Jason Sams49a05d72010-12-29 14:31:29 -0800177 mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length);
Jason Sams49bdaf02010-08-31 13:50:42 -0700178 }
179
180
181 public void subElementData(int xoff, int component_number, FieldPacker fp) {
182 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800183 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700184 }
185 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800186 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700187 }
188
189 final byte[] data = fp.getData();
190 int eSize = mType.mElement.mElements[component_number].getSizeBytes();
191
192 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800193 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700194 " does not match component size " + eSize + ".");
195 }
196
Jason Sams49a05d72010-12-29 14:31:29 -0800197 mRS.nAllocationElementData1D(getID(), xoff, 0, component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700198 }
199
Jason Sams768bc022009-09-21 19:41:04 -0700200 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800201 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700202 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800203 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700204 }
205 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800206 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700207 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800208 if((off + count) > mType.getCount()) {
209 throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() +
Jason Samsa70f4162010-03-26 15:33:42 -0700210 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700211 }
Jason Sams768bc022009-09-21 19:41:04 -0700212 if((len) < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800213 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700214 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700215 }
216
217 public void subData1D(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700218 int dataSize = mType.mElement.getSizeBytes() * count;
219 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams49a05d72010-12-29 14:31:29 -0800220 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700221 }
222 public void subData1D(int off, int count, short[] d) {
223 int dataSize = mType.mElement.getSizeBytes() * count;
224 data1DChecks(off, count, d.length * 2, dataSize);
Jason Sams49a05d72010-12-29 14:31:29 -0800225 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700226 }
227 public void subData1D(int off, int count, byte[] d) {
228 int dataSize = mType.mElement.getSizeBytes() * count;
229 data1DChecks(off, count, d.length, dataSize);
Jason Sams49a05d72010-12-29 14:31:29 -0800230 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700231 }
232 public void subData1D(int off, int count, float[] d) {
233 int dataSize = mType.mElement.getSizeBytes() * count;
234 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams49a05d72010-12-29 14:31:29 -0800235 mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700236 }
237
Jason Sams768bc022009-09-21 19:41:04 -0700238
Jason Samsb8c5a842009-07-31 20:40:47 -0700239 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800240 mRS.validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800241 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700242 }
243
244 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800245 mRS.validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800246 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700247 }
248
Jason Sams40a29e82009-08-10 14:55:26 -0700249 public void readData(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800250 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800251 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700252 }
253
254 public void readData(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800255 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800256 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700257 }
258
Jason Sams31a7e422010-10-26 13:09:17 -0700259 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800260 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800261 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700262 }
Jason Sams06d69de2010-11-09 17:11:40 -0800263 mRS.nAllocationResize1D(getID(), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -0700264 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -0700265
Jason Sams06d69de2010-11-09 17:11:40 -0800266 int typeID = mRS.nAllocationGetType(getID());
Jason Sams31a7e422010-10-26 13:09:17 -0700267 mType = new Type(typeID, mRS);
268 mType.updateFromNative();
Jason Sams5edc6082010-10-05 13:32:49 -0700269 }
270
271 /*
272 public void resize(int dimX, int dimY) {
273 if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800274 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700275 }
276 if (mType.getY() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800277 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700278 }
Jason Sams06d69de2010-11-09 17:11:40 -0800279 mRS.nAllocationResize2D(getID(), dimX, dimY);
Jason Sams5edc6082010-10-05 13:32:49 -0700280 }
281 */
Jason Sams40a29e82009-08-10 14:55:26 -0700282
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700283
Jason Samsb8c5a842009-07-31 20:40:47 -0700284
285 // creation
286
Jason Sams49a05d72010-12-29 14:31:29 -0800287 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -0700288 static {
289 mBitmapOptions.inScaled = false;
290 }
291
Jason Samsd4b23b52010-12-13 15:32:35 -0800292 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mc, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800293 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800294 if (type.getID() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800295 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -0700296 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800297 int id = rs.nAllocationCreateTyped(type.getID(), mc.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800298 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800299 throw new RSRuntimeException("Allocation creation failed.");
300 }
Jason Sams5476b452010-12-08 16:14:36 -0800301 return new Allocation(id, rs, type, usage);
Jason Samsb8c5a842009-07-31 20:40:47 -0700302 }
303
Jason Samse5d37122010-12-16 00:33:33 -0800304 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
305 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
306 }
307
Jason Sams5476b452010-12-08 16:14:36 -0800308 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800309 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -0800310 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700311
Jason Sams5476b452010-12-08 16:14:36 -0800312 static public Allocation createSized(RenderScript rs, Element e,
313 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800314 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700315 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800316 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -0700317 Type t = b.create();
318
Jason Samsd4b23b52010-12-13 15:32:35 -0800319 int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800320 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800321 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700322 }
Jason Sams5476b452010-12-08 16:14:36 -0800323 return new Allocation(id, rs, t, usage);
324 }
325
326 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800327 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -0700328 }
329
Jason Sams49a05d72010-12-29 14:31:29 -0800330 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -0800331 final Bitmap.Config bc = b.getConfig();
332 if (bc == Bitmap.Config.ALPHA_8) {
333 return Element.A_8(rs);
334 }
335 if (bc == Bitmap.Config.ARGB_4444) {
336 return Element.RGBA_4444(rs);
337 }
338 if (bc == Bitmap.Config.ARGB_8888) {
339 return Element.RGBA_8888(rs);
340 }
341 if (bc == Bitmap.Config.RGB_565) {
342 return Element.RGB_565(rs);
343 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -0800344 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -0800345 }
346
Jason Sams49a05d72010-12-29 14:31:29 -0800347 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800348 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -0800349 Element e = elementFromBitmap(rs, b);
350 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800351 tb.setX(b.getWidth());
352 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -0800353 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -0800354 return tb.create();
355 }
356
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800357 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800358 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800359 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800360 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800361 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -0800362
Jason Sams5476b452010-12-08 16:14:36 -0800363 int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
364 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800365 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -0800366 }
Jason Sams5476b452010-12-08 16:14:36 -0800367 return new Allocation(id, rs, t, usage);
368 }
369
Jason Sams6d8eb262010-12-15 01:41:00 -0800370 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
371 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
372 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -0800373 }
374
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800375 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800376 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800377 CubemapLayout layout,
378 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800379 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800380
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800381 int height = b.getHeight();
382 int width = b.getWidth();
383
384 if (layout != CubemapLayout.VERTICAL_FACE_LIST) {
385 throw new RSIllegalArgumentException("Only vertical face list supported");
386 }
387 if (height % 6 != 0) {
388 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
389 }
390 if (height / 6 != width) {
391 throw new RSIllegalArgumentException("Only square cobe map faces supported");
392 }
393 boolean isPow2 = (width & (width - 1)) == 0;
394 if (!isPow2) {
395 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
396 }
397
398 Element e = elementFromBitmap(rs, b);
399 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800400 tb.setX(width);
401 tb.setY(width);
402 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -0800403 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800404 Type t = tb.create();
405
Jason Sams5476b452010-12-08 16:14:36 -0800406 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800407 if(id == 0) {
408 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
409 }
Jason Sams5476b452010-12-08 16:14:36 -0800410 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800411 }
412
Jason Sams5476b452010-12-08 16:14:36 -0800413 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams5476b452010-12-08 16:14:36 -0800414 CubemapLayout layout) {
Jason Sams6d8eb262010-12-15 01:41:00 -0800415 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
416 layout, USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -0800417 }
418
Jason Sams5476b452010-12-08 16:14:36 -0800419 static public Allocation createFromBitmapResource(RenderScript rs,
420 Resources res,
421 int id,
Jason Sams4ef66502010-12-10 16:03:15 -0800422 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800423 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -0700424
Jason Sams771bebb2009-12-07 12:40:12 -0800425 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800426 Bitmap b = BitmapFactory.decodeResource(res, id);
427 Allocation alloc = createFromBitmap(rs, b, mips, usage);
428 b.recycle();
429 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -0700430 }
431
Jason Sams5476b452010-12-08 16:14:36 -0800432 static public Allocation createFromBitmapResource(RenderScript rs,
433 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -0800434 int id) {
435 return createFromBitmapResource(rs, res, id,
436 MipmapControl.MIPMAP_NONE,
437 USAGE_GRAPHICS_TEXTURE);
438 }
439
Jason Sams5476b452010-12-08 16:14:36 -0800440 static public Allocation createFromString(RenderScript rs,
441 String str,
442 int usage) {
443 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700444 byte[] allocArray = null;
445 try {
446 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -0800447 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800448 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700449 return alloc;
450 }
451 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -0800452 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700453 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700454 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700455}
456
457