blob: 3ff483d19de448c6f20618a157e36884f94d3025 [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/**
30 * @hide
31 *
32 **/
33public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070034 Type mType;
Jason Sams8a647432010-03-01 15:31:04 -080035 Bitmap mBitmap;
Jason Sams5476b452010-12-08 16:14:36 -080036 int mUsage;
37
38 public static final int USAGE_SCRIPT = 0x0001;
39 public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
40 public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
41 public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
42
43 private static final int USAGE_ALL = 0x000F;
44
Jason Sams43ee06852009-08-12 17:54:11 -070045
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080046 public enum CubemapLayout {
47 VERTICAL_FACE_LIST (0),
48 HORIZONTAL_FACE_LIST (1),
49 VERTICAL_CROSS (2),
50 HORIZONTAL_CROSS (3);
51
52 int mID;
53 CubemapLayout(int id) {
54 mID = id;
55 }
56 }
57
Jason Sams4ef66502010-12-10 16:03:15 -080058 public enum MipmapControl {
Jason Sams5476b452010-12-08 16:14:36 -080059 MIPMAP_NONE(0),
60 MIPMAP_FULL(1),
61 MIPMAP_ON_SYNC_TO_TEXTURE(2);
62
63 int mID;
Jason Sams4ef66502010-12-10 16:03:15 -080064 MipmapControl(int id) {
Jason Sams5476b452010-12-08 16:14:36 -080065 mID = id;
66 }
Jason Samsb8c5a842009-07-31 20:40:47 -070067 }
68
Jason Sams5476b452010-12-08 16:14:36 -080069 Allocation(int id, RenderScript rs, Type t, int usage) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070070 super(id, rs);
Jason Sams5476b452010-12-08 16:14:36 -080071 if (usage > USAGE_ALL) {
72 throw new RSIllegalArgumentException("Unknown usage specified.");
73 }
74 mType = t;
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -070075 }
76
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070077 @Override
78 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -080079 super.updateFromNative();
80 int typeID = mRS.nAllocationGetType(getID());
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070081 if(typeID != 0) {
82 mType = new Type(typeID, mRS);
83 mType.updateFromNative();
84 }
85 }
86
Jason Samsea87e962010-01-12 12:12:28 -080087 public Type getType() {
88 return mType;
89 }
90
Jason Sams5476b452010-12-08 16:14:36 -080091 public void syncAll(int srcLocation) {
92 switch (srcLocation) {
93 case USAGE_SCRIPT:
94 case USAGE_GRAPHICS_CONSTANTS:
95 case USAGE_GRAPHICS_TEXTURE:
96 case USAGE_GRAPHICS_VERTEX:
97 break;
98 default:
99 throw new RSIllegalArgumentException("Source must be exactly one usage type.");
100 }
101 mRS.validate();
102 mRS.nAllocationSyncAll(getID(), srcLocation);
103 }
104
Jason Sams07ae4062009-08-27 20:23:34 -0700105 public void uploadToBufferObject() {
Jason Sams771bebb2009-12-07 12:40:12 -0800106 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800107 mRS.nAllocationUploadToBufferObject(getID());
Jason Sams07ae4062009-08-27 20:23:34 -0700108 }
109
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800110
111 public void copyFrom(BaseObj[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800112 mRS.validate();
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800113 if (d.length != mType.getCount()) {
114 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
115 mType.getCount() + ", array length = " + d.length);
116 }
117 int i[] = new int[d.length];
118 for (int ct=0; ct < d.length; ct++) {
119 i[ct] = d[ct].getID();
120 }
121 subData1D(0, mType.getCount(), i);
Jason Samsb8c5a842009-07-31 20:40:47 -0700122 }
123
Jason Sams4ef66502010-12-10 16:03:15 -0800124 private void validateBitmap(Bitmap b) {
125 mRS.validate();
126 if(mType.getX() != b.getWidth() ||
127 mType.getY() != b.getHeight()) {
128 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
129 }
130 }
131
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800132 public void copyFrom(int[] d) {
133 mRS.validate();
134 subData1D(0, mType.getCount(), d);
135 }
136 public void copyFrom(short[] d) {
137 mRS.validate();
138 subData1D(0, mType.getCount(), d);
139 }
140 public void copyFrom(byte[] d) {
141 mRS.validate();
142 subData1D(0, mType.getCount(), d);
143 }
144 public void copyFrom(float[] d) {
145 mRS.validate();
146 subData1D(0, mType.getCount(), d);
147 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800148 public void copyFrom(Bitmap b) {
Jason Sams4ef66502010-12-10 16:03:15 -0800149 validateBitmap(b);
150 mRS.nAllocationCopyFromBitmap(getID(), b);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700151 }
152
Jason Sams4ef66502010-12-10 16:03:15 -0800153 public void copyTo(Bitmap b) {
154 validateBitmap(b);
155 mRS.nAllocationCopyToBitmap(getID(), b);
156 }
157
158
Jason Sams49bdaf02010-08-31 13:50:42 -0700159 public void subData(int xoff, FieldPacker fp) {
Jason Samsa70f4162010-03-26 15:33:42 -0700160 int eSize = mType.mElement.getSizeBytes();
161 final byte[] data = fp.getData();
162
163 int count = data.length / eSize;
164 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800165 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700166 " not divisible by element size " + eSize + ".");
167 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700168 data1DChecks(xoff, count, data.length, data.length);
Jason Sams06d69de2010-11-09 17:11:40 -0800169 mRS.nAllocationSubData1D(getID(), xoff, count, data, data.length);
Jason Sams49bdaf02010-08-31 13:50:42 -0700170 }
171
172
173 public void subElementData(int xoff, int component_number, FieldPacker fp) {
174 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800175 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700176 }
177 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800178 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700179 }
180
181 final byte[] data = fp.getData();
182 int eSize = mType.mElement.mElements[component_number].getSizeBytes();
183
184 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800185 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700186 " does not match component size " + eSize + ".");
187 }
188
Jason Sams06d69de2010-11-09 17:11:40 -0800189 mRS.nAllocationSubElementData1D(getID(), xoff, component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700190 }
191
Jason Sams768bc022009-09-21 19:41:04 -0700192 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800193 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700194 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800195 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700196 }
197 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800198 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700199 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800200 if((off + count) > mType.getCount()) {
201 throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() +
Jason Samsa70f4162010-03-26 15:33:42 -0700202 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700203 }
Jason Sams768bc022009-09-21 19:41:04 -0700204 if((len) < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800205 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700206 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700207 }
208
209 public void subData1D(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700210 int dataSize = mType.mElement.getSizeBytes() * count;
211 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams06d69de2010-11-09 17:11:40 -0800212 mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700213 }
214 public void subData1D(int off, int count, short[] d) {
215 int dataSize = mType.mElement.getSizeBytes() * count;
216 data1DChecks(off, count, d.length * 2, dataSize);
Jason Sams06d69de2010-11-09 17:11:40 -0800217 mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700218 }
219 public void subData1D(int off, int count, byte[] d) {
220 int dataSize = mType.mElement.getSizeBytes() * count;
221 data1DChecks(off, count, d.length, dataSize);
Jason Sams06d69de2010-11-09 17:11:40 -0800222 mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700223 }
224 public void subData1D(int off, int count, float[] d) {
225 int dataSize = mType.mElement.getSizeBytes() * count;
226 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams06d69de2010-11-09 17:11:40 -0800227 mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700228 }
229
Jason Sams768bc022009-09-21 19:41:04 -0700230
Jason Samsb8c5a842009-07-31 20:40:47 -0700231 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800232 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800233 mRS.nAllocationSubData2D(getID(), xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700234 }
235
236 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800237 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800238 mRS.nAllocationSubData2D(getID(), xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700239 }
240
Jason Sams40a29e82009-08-10 14:55:26 -0700241 public void readData(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800242 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800243 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700244 }
245
246 public void readData(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800247 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800248 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700249 }
250
Jason Sams31a7e422010-10-26 13:09:17 -0700251 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800252 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800253 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700254 }
Jason Sams06d69de2010-11-09 17:11:40 -0800255 mRS.nAllocationResize1D(getID(), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -0700256 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -0700257
Jason Sams06d69de2010-11-09 17:11:40 -0800258 int typeID = mRS.nAllocationGetType(getID());
Jason Sams31a7e422010-10-26 13:09:17 -0700259 mType = new Type(typeID, mRS);
260 mType.updateFromNative();
Jason Sams5edc6082010-10-05 13:32:49 -0700261 }
262
263 /*
264 public void resize(int dimX, int dimY) {
265 if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800266 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700267 }
268 if (mType.getY() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800269 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700270 }
Jason Sams06d69de2010-11-09 17:11:40 -0800271 mRS.nAllocationResize2D(getID(), dimX, dimY);
Jason Sams5edc6082010-10-05 13:32:49 -0700272 }
273 */
Jason Sams40a29e82009-08-10 14:55:26 -0700274
Jason Sams5476b452010-12-08 16:14:36 -0800275 /*
Jason Samsb8c5a842009-07-31 20:40:47 -0700276 public class Adapter1D extends BaseObj {
277 Adapter1D(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700278 super(id, rs);
Jason Samsb8c5a842009-07-31 20:40:47 -0700279 }
280
Jason Samsb8c5a842009-07-31 20:40:47 -0700281 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800282 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800283 mRS.nAdapter1DSetConstraint(getID(), dim.mID, value);
Jason Samsb8c5a842009-07-31 20:40:47 -0700284 }
285
286 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800287 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800288 mRS.nAdapter1DData(getID(), d);
Jason Samsb8c5a842009-07-31 20:40:47 -0700289 }
290
Jason Samsb8c5a842009-07-31 20:40:47 -0700291 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800292 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800293 mRS.nAdapter1DData(getID(), d);
Jason Samsb8c5a842009-07-31 20:40:47 -0700294 }
295
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700296 public void subData(int off, int count, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800297 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800298 mRS.nAdapter1DSubData(getID(), off, count, d);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700299 }
300
Jason Samsb8c5a842009-07-31 20:40:47 -0700301 public void subData(int off, int count, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800302 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800303 mRS.nAdapter1DSubData(getID(), off, count, d);
Jason Samsb8c5a842009-07-31 20:40:47 -0700304 }
305 }
306
307 public Adapter1D createAdapter1D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800308 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700309 int id = mRS.nAdapter1DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800310 if(id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800311 throw new RSRuntimeException("Adapter creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700312 }
Jason Sams06d69de2010-11-09 17:11:40 -0800313 mRS.nAdapter1DBindAllocation(id, getID());
Jason Samsb8c5a842009-07-31 20:40:47 -0700314 return new Adapter1D(id, mRS);
315 }
Jason Sams5476b452010-12-08 16:14:36 -0800316 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700317
318
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700319 public class Adapter2D extends BaseObj {
320 Adapter2D(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700321 super(id, rs);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700322 }
323
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700324 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800325 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800326 mRS.nAdapter2DSetConstraint(getID(), dim.mID, value);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700327 }
328
329 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800330 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800331 mRS.nAdapter2DData(getID(), d);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700332 }
333
334 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800335 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800336 mRS.nAdapter2DData(getID(), d);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700337 }
338
339 public void subData(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800340 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800341 mRS.nAdapter2DSubData(getID(), xoff, yoff, w, h, d);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700342 }
343
344 public void subData(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800345 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800346 mRS.nAdapter2DSubData(getID(), xoff, yoff, w, h, d);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700347 }
348 }
349
350 public Adapter2D createAdapter2D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800351 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700352 int id = mRS.nAdapter2DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800353 if(id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800354 throw new RSRuntimeException("allocation failed.");
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700355 }
Jason Sams06d69de2010-11-09 17:11:40 -0800356 mRS.nAdapter2DBindAllocation(id, getID());
357 if(id == 0) {
358 throw new RSRuntimeException("Adapter creation failed.");
359 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700360 return new Adapter2D(id, mRS);
361 }
362
Jason Samsb8c5a842009-07-31 20:40:47 -0700363
364 // creation
365
366 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
367 static {
368 mBitmapOptions.inScaled = false;
369 }
370
Jason Samsd4b23b52010-12-13 15:32:35 -0800371 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mc, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800372 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800373 if (type.getID() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800374 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -0700375 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800376 int id = rs.nAllocationCreateTyped(type.getID(), mc.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800377 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800378 throw new RSRuntimeException("Allocation creation failed.");
379 }
Jason Sams5476b452010-12-08 16:14:36 -0800380 return new Allocation(id, rs, type, usage);
Jason Samsb8c5a842009-07-31 20:40:47 -0700381 }
382
Jason Samse5d37122010-12-16 00:33:33 -0800383 static public Allocation createTyped(RenderScript rs, Type type, int usage) {
384 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
385 }
386
Jason Sams5476b452010-12-08 16:14:36 -0800387 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800388 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -0800389 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700390
Jason Sams5476b452010-12-08 16:14:36 -0800391 static public Allocation createSized(RenderScript rs, Element e,
392 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800393 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700394 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800395 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -0700396 Type t = b.create();
397
Jason Samsd4b23b52010-12-13 15:32:35 -0800398 int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800399 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800400 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700401 }
Jason Sams5476b452010-12-08 16:14:36 -0800402 return new Allocation(id, rs, t, usage);
403 }
404
405 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800406 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -0700407 }
408
Jason Sams8a647432010-03-01 15:31:04 -0800409 static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
410 final Bitmap.Config bc = b.getConfig();
411 if (bc == Bitmap.Config.ALPHA_8) {
412 return Element.A_8(rs);
413 }
414 if (bc == Bitmap.Config.ARGB_4444) {
415 return Element.RGBA_4444(rs);
416 }
417 if (bc == Bitmap.Config.ARGB_8888) {
418 return Element.RGBA_8888(rs);
419 }
420 if (bc == Bitmap.Config.RGB_565) {
421 return Element.RGB_565(rs);
422 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -0800423 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -0800424 }
425
Jason Sams5476b452010-12-08 16:14:36 -0800426 static private Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800427 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -0800428 Element e = elementFromBitmap(rs, b);
429 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800430 tb.setX(b.getWidth());
431 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -0800432 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -0800433 return tb.create();
434 }
435
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800436 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800437 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800438 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800439 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800440 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -0800441
Jason Sams5476b452010-12-08 16:14:36 -0800442 int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
443 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800444 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -0800445 }
Jason Sams5476b452010-12-08 16:14:36 -0800446 return new Allocation(id, rs, t, usage);
447 }
448
Jason Sams6d8eb262010-12-15 01:41:00 -0800449 static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
450 return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
451 USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -0800452 }
453
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800454 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800455 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800456 CubemapLayout layout,
457 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800458 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800459
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800460 int height = b.getHeight();
461 int width = b.getWidth();
462
463 if (layout != CubemapLayout.VERTICAL_FACE_LIST) {
464 throw new RSIllegalArgumentException("Only vertical face list supported");
465 }
466 if (height % 6 != 0) {
467 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
468 }
469 if (height / 6 != width) {
470 throw new RSIllegalArgumentException("Only square cobe map faces supported");
471 }
472 boolean isPow2 = (width & (width - 1)) == 0;
473 if (!isPow2) {
474 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
475 }
476
477 Element e = elementFromBitmap(rs, b);
478 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800479 tb.setX(width);
480 tb.setY(width);
481 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -0800482 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800483 Type t = tb.create();
484
Jason Sams5476b452010-12-08 16:14:36 -0800485 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800486 if(id == 0) {
487 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
488 }
Jason Sams5476b452010-12-08 16:14:36 -0800489 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800490 }
491
Jason Sams5476b452010-12-08 16:14:36 -0800492 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams5476b452010-12-08 16:14:36 -0800493 CubemapLayout layout) {
Jason Sams6d8eb262010-12-15 01:41:00 -0800494 return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
495 layout, USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -0800496 }
497
Jason Sams5476b452010-12-08 16:14:36 -0800498 static public Allocation createFromBitmapResource(RenderScript rs,
499 Resources res,
500 int id,
Jason Sams4ef66502010-12-10 16:03:15 -0800501 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800502 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -0700503
Jason Sams771bebb2009-12-07 12:40:12 -0800504 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800505 Bitmap b = BitmapFactory.decodeResource(res, id);
506 Allocation alloc = createFromBitmap(rs, b, mips, usage);
507 b.recycle();
508 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -0700509 }
510
Jason Sams5476b452010-12-08 16:14:36 -0800511 static public Allocation createFromBitmapResource(RenderScript rs,
512 Resources res,
Jason Sams6d8eb262010-12-15 01:41:00 -0800513 int id) {
514 return createFromBitmapResource(rs, res, id,
515 MipmapControl.MIPMAP_NONE,
516 USAGE_GRAPHICS_TEXTURE);
517 }
518
519/*
520 static public Allocation createFromBitmapResource(RenderScript rs,
521 Resources res,
Jason Sams5476b452010-12-08 16:14:36 -0800522 int id,
523 Element dstFmt,
524 boolean genMips) {
Jason Sams4ef66502010-12-10 16:03:15 -0800525 MipmapControl mc = MipmapControl.MIPMAP_NONE;
Jason Sams5476b452010-12-08 16:14:36 -0800526 if (genMips) {
Jason Sams4ef66502010-12-10 16:03:15 -0800527 mc = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
Jason Sams5476b452010-12-08 16:14:36 -0800528 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800529 return createFromBitmapResource(rs, res, id, mc, USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -0800530 }
Jason Sams6d8eb262010-12-15 01:41:00 -0800531*/
Jason Sams5476b452010-12-08 16:14:36 -0800532 static public Allocation createFromString(RenderScript rs,
533 String str,
534 int usage) {
535 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700536 byte[] allocArray = null;
537 try {
538 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -0800539 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800540 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700541 return alloc;
542 }
543 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -0800544 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700545 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700546 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700547}
548
549