blob: 7a47c3bc88de3a3c007e58d5572a15b2298c8a47 [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
Jason Sams4ef66502010-12-10 16:03:15 -080056 public enum MipmapControl {
Jason Sams5476b452010-12-08 16:14:36 -080057 MIPMAP_NONE(0),
58 MIPMAP_FULL(1),
59 MIPMAP_ON_SYNC_TO_TEXTURE(2);
60
61 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -080062 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -080063 mID = id;
64 }
Jason Samsb8c5a842009-07-31 20:40:47 -070065 }
66
Jason Sams5476b452010-12-08 16:14:36 -080067 Allocation(int id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070068 super(id, rs);
Jason Sams49a05d72010-12-29 14:31:29 -080069 if ((usage & ~(USAGE_SCRIPT |
70 USAGE_GRAPHICS_TEXTURE |
71 USAGE_GRAPHICS_VERTEX |
72 USAGE_GRAPHICS_CONSTANTS)) != 0) {
Jason Sams5476b452010-12-08 16:14:36 -080073 throw new RSIllegalArgumentException("Unknown usage specified.");
74 }
75 mType = t;
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -070076 }
77
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070078 @Override
79 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -080080 super.updateFromNative();
81 int typeID = mRS.nAllocationGetType(getID());
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070082 if(typeID != 0) {
83 mType = new Type(typeID, mRS);
84 mType.updateFromNative();
85 }
86 }
87
Jason Samsea87e962010-01-12 12:12:28 -080088 public Type getType() {
89 return mType;
90 }
91
Jason Sams5476b452010-12-08 16:14:36 -080092 public void syncAll(int srcLocation) {
93 switch (srcLocation) {
94 case USAGE_SCRIPT:
95 case USAGE_GRAPHICS_CONSTANTS:
96 case USAGE_GRAPHICS_TEXTURE:
97 case USAGE_GRAPHICS_VERTEX:
98 break;
99 default:
100 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
101 }
102 mRS.validate();
103 mRS.nAllocationSyncAll(getID(), srcLocation);
104 }
105
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800106 public void copyFrom(BaseObj[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800107 mRS.validate();
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800108 if (d.length != mType.getCount()) {
109 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
110 mType.getCount() + ", array length = " + d.length);
111 }
112 int i[] = new int[d.length];
113 for (int ct=0; ct < d.length; ct++) {
114 i[ct] = d[ct].getID();
115 }
Jason Samsfa445b92011-01-07 17:00:07 -0800116 copy1DRangeFrom(0, mType.getCount(), i);
Jason Samsb8c5a842009-07-31 20:40:47 -0700117 }
118
Jason Sams4ef66502010-12-10 16:03:15 -0800119 private void validateBitmap(Bitmap b) {
120 mRS.validate();
121 if(mType.getX() != b.getWidth() ||
122 mType.getY() != b.getHeight()) {
123 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
124 }
125 }
126
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800127 public void copyFrom(int[] d) {
128 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800129 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800130 }
131 public void copyFrom(short[] d) {
132 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800133 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800134 }
135 public void copyFrom(byte[] d) {
136 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800137 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800138 }
139 public void copyFrom(float[] d) {
140 mRS.validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800141 copy1DRangeFrom(0, mType.getCount(), d);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800142 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800143 public void copyFrom(Bitmap b) {
Jason Sams4ef66502010-12-10 16:03:15 -0800144 validateBitmap(b);
145 mRS.nAllocationCopyFromBitmap(getID(), b);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700146 }
147
Jason Samsfa445b92011-01-07 17:00:07 -0800148 /**
149 * @hide
150 *
151 * This is only intended to be used by auto-generate code reflected from the
152 * renderscript script files.
153 *
154 * @param xoff
155 * @param fp
156 */
157 public void setOneElement(int xoff, FieldPacker fp) {
Jason Samsa70f4162010-03-26 15:33:42 -0700158 int eSize = mType.mElement.getSizeBytes();
159 final byte[] data = fp.getData();
160
161 int count = data.length / eSize;
162 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800163 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700164 " not divisible by element size " + eSize + ".");
165 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700166 data1DChecks(xoff, count, data.length, data.length);
Jason Sams49a05d72010-12-29 14:31:29 -0800167 mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length);
Jason Sams49bdaf02010-08-31 13:50:42 -0700168 }
169
170
Jason Samsfa445b92011-01-07 17:00:07 -0800171 /**
172 * @hide
173 *
174 * This is only intended to be used by auto-generate code reflected from the
175 * renderscript script files.
176 *
177 * @param xoff
178 * @param component_number
179 * @param fp
180 */
181 public void setOneComponent(int xoff, int component_number, FieldPacker fp) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700182 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
Jason Samsfa445b92011-01-07 17:00:07 -0800217 public void copy1DRangeFrom(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 }
Jason Samsfa445b92011-01-07 17:00:07 -0800222 public void copy1DRangeFrom(int off, int count, short[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700223 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 }
Jason Samsfa445b92011-01-07 17:00:07 -0800227 public void copy1DRangeFrom(int off, int count, byte[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700228 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 }
Jason Samsfa445b92011-01-07 17:00:07 -0800232 public void copy1DRangeFrom(int off, int count, float[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700233 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 Samsfa445b92011-01-07 17:00:07 -0800239 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] d) {
240 mRS.validate();
241 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length);
242 }
243
244 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] d) {
245 mRS.validate();
246 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 2);
247 }
248
249 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800250 mRS.validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800251 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700252 }
253
Jason Samsfa445b92011-01-07 17:00:07 -0800254 public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800255 mRS.validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800256 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700257 }
258
Jason Samsfa445b92011-01-07 17:00:07 -0800259 public void copy2DRangeFrom(int xoff, int yoff, Bitmap b) {
260 mRS.validate();
261 mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, b);
262 }
263
264
265 public void copyTo(Bitmap b) {
266 validateBitmap(b);
267 mRS.nAllocationCopyToBitmap(getID(), b);
268 }
269
270 public void copyTo(byte[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800271 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800272 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700273 }
274
Jason Samsfa445b92011-01-07 17:00:07 -0800275 public void copyTo(short[] d) {
276 mRS.validate();
277 mRS.nAllocationRead(getID(), d);
278 }
279
280 public void copyTo(int[] d) {
281 mRS.validate();
282 mRS.nAllocationRead(getID(), d);
283 }
284
285 public void copyTo(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800286 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800287 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700288 }
289
Jason Sams31a7e422010-10-26 13:09:17 -0700290 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800291 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800292 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700293 }
Jason Sams06d69de2010-11-09 17:11:40 -0800294 mRS.nAllocationResize1D(getID(), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -0700295 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -0700296
Jason Sams06d69de2010-11-09 17:11:40 -0800297 int typeID = mRS.nAllocationGetType(getID());
Jason Sams31a7e422010-10-26 13:09:17 -0700298 mType = new Type(typeID, mRS);
299 mType.updateFromNative();
Jason Sams5edc6082010-10-05 13:32:49 -0700300 }
301
302 /*
303 public void resize(int dimX, int dimY) {
304 if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800305 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700306 }
307 if (mType.getY() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800308 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700309 }
Jason Sams06d69de2010-11-09 17:11:40 -0800310 mRS.nAllocationResize2D(getID(), dimX, dimY);
Jason Sams5edc6082010-10-05 13:32:49 -0700311 }
312 */
Jason Sams40a29e82009-08-10 14:55:26 -0700313
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700314
Jason Samsb8c5a842009-07-31 20:40:47 -0700315
316 // creation
317
Jason Sams49a05d72010-12-29 14:31:29 -0800318 static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
Jason Samsb8c5a842009-07-31 20:40:47 -0700319 static {
320 mBitmapOptions.inScaled = false;
321 }
322
Jason Samsd4b23b52010-12-13 15:32:35 -0800323 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mc, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800324 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800325 if (type.getID() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800326 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -0700327 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800328 int id = rs.nAllocationCreateTyped(type.getID(), mc.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800329 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800330 throw new RSRuntimeException("Allocation creation failed.");
331 }
Jason Sams5476b452010-12-08 16:14:36 -0800332 return new Allocation(id, rs, type, usage);
Jason Samsb8c5a842009-07-31 20:40:47 -0700333 }
334
Jason Samse5d37122010-12-16 00:33:33 -0800335 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
336 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
337 }
338
Jason Sams5476b452010-12-08 16:14:36 -0800339 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800340 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -0800341 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700342
Jason Sams5476b452010-12-08 16:14:36 -0800343 static public Allocation createSized(RenderScript rs, Element e,
344 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800345 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700346 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800347 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -0700348 Type t = b.create();
349
Jason Samsd4b23b52010-12-13 15:32:35 -0800350 int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800351 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800352 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700353 }
Jason Sams5476b452010-12-08 16:14:36 -0800354 return new Allocation(id, rs, t, usage);
355 }
356
357 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800358 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -0700359 }
360
Jason Sams49a05d72010-12-29 14:31:29 -0800361 static Element elementFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams8a647432010-03-01 15:31:04 -0800362 final Bitmap.Config bc = b.getConfig();
363 if (bc == Bitmap.Config.ALPHA_8) {
364 return Element.A_8(rs);
365 }
366 if (bc == Bitmap.Config.ARGB_4444) {
367 return Element.RGBA_4444(rs);
368 }
369 if (bc == Bitmap.Config.ARGB_8888) {
370 return Element.RGBA_8888(rs);
371 }
372 if (bc == Bitmap.Config.RGB_565) {
373 return Element.RGB_565(rs);
374 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -0800375 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -0800376 }
377
Jason Sams49a05d72010-12-29 14:31:29 -0800378 static Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800379 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -0800380 Element e = elementFromBitmap(rs, b);
381 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800382 tb.setX(b.getWidth());
383 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -0800384 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -0800385 return tb.create();
386 }
387
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800388 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800389 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800390 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800391 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800392 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -0800393
Jason Sams5476b452010-12-08 16:14:36 -0800394 int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
395 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800396 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -0800397 }
Jason Sams5476b452010-12-08 16:14:36 -0800398 return new Allocation(id, rs, t, usage);
399 }
400
Jason Sams6d8eb262010-12-15 01:41:00 -0800401 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
402 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
403 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -0800404 }
405
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800406 /**
407 * Creates a cubemap allocation from a bitmap containing the
408 * horizontal list of cube faces. Each individual face must be
409 * the same size and power of 2
410 *
411 * @param rs
412 * @param b bitmap with cubemap faces layed out in the following
413 * format: right, left, top, bottom, front, back
414 * @param mips specifies desired mipmap behaviour for the cubemap
415 * @param usage bitfield specifying how the cubemap is utilized
416 *
417 **/
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800418 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800419 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800420 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800421 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800422
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800423 int height = b.getHeight();
424 int width = b.getWidth();
425
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800426 if (width % 6 != 0) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800427 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
428 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800429 if (width / 6 != height) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800430 throw new RSIllegalArgumentException("Only square cobe map faces supported");
431 }
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800432 boolean isPow2 = (height & (height - 1)) == 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800433 if (!isPow2) {
434 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
435 }
436
437 Element e = elementFromBitmap(rs, b);
438 Type.Builder tb = new Type.Builder(rs, e);
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800439 tb.setX(height);
440 tb.setY(height);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800441 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -0800442 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800443 Type t = tb.create();
444
Jason Sams5476b452010-12-08 16:14:36 -0800445 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800446 if(id == 0) {
447 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
448 }
Jason Sams5476b452010-12-08 16:14:36 -0800449 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800450 }
451
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800452 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b) {
Jason Sams6d8eb262010-12-15 01:41:00 -0800453 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800454 USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -0800455 }
456
Jason Sams5476b452010-12-08 16:14:36 -0800457 static public Allocation createFromBitmapResource(RenderScript rs,
458 Resources res,
459 int id,
Jason Sams4ef66502010-12-10 16:03:15 -0800460 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800461 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -0700462
Jason Sams771bebb2009-12-07 12:40:12 -0800463 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800464 Bitmap b = BitmapFactory.decodeResource(res, id);
465 Allocation alloc = createFromBitmap(rs, b, mips, usage);
466 b.recycle();
467 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -0700468 }
469
Jason Sams5476b452010-12-08 16:14:36 -0800470 static public Allocation createFromBitmapResource(RenderScript rs,
471 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -0800472 int id) {
473 return createFromBitmapResource(rs, res, id,
474 MipmapControl.MIPMAP_NONE,
475 USAGE_GRAPHICS_TEXTURE);
476 }
477
Jason Sams5476b452010-12-08 16:14:36 -0800478 static public Allocation createFromString(RenderScript rs,
479 String str,
480 int usage) {
481 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700482 byte[] allocArray = null;
483 try {
484 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -0800485 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800486 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700487 return alloc;
488 }
489 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -0800490 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700491 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700492 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700493}
494
495