blob: 30475bd21d7adc1d846cffe26ebc7fe85cb586cf [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 Samsb8c5a842009-07-31 20:40:47 -0700105 public void uploadToTexture(int baseMipLevel) {
Jason Sams771bebb2009-12-07 12:40:12 -0800106 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800107 mRS.nAllocationUploadToTexture(getID(), false, baseMipLevel);
Jason Samsc2908e62010-02-23 17:44:28 -0800108 }
109
110 public void uploadToTexture(boolean genMips, int baseMipLevel) {
111 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800112 mRS.nAllocationUploadToTexture(getID(), genMips, baseMipLevel);
Jason Samsb8c5a842009-07-31 20:40:47 -0700113 }
114
Jason Sams07ae4062009-08-27 20:23:34 -0700115 public void uploadToBufferObject() {
Jason Sams771bebb2009-12-07 12:40:12 -0800116 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800117 mRS.nAllocationUploadToBufferObject(getID());
Jason Sams07ae4062009-08-27 20:23:34 -0700118 }
119
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800120
121 public void copyFrom(BaseObj[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800122 mRS.validate();
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800123 if (d.length != mType.getCount()) {
124 throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
125 mType.getCount() + ", array length = " + d.length);
126 }
127 int i[] = new int[d.length];
128 for (int ct=0; ct < d.length; ct++) {
129 i[ct] = d[ct].getID();
130 }
131 subData1D(0, mType.getCount(), i);
Jason Samsb8c5a842009-07-31 20:40:47 -0700132 }
133
Jason Sams4ef66502010-12-10 16:03:15 -0800134 private void validateBitmap(Bitmap b) {
135 mRS.validate();
136 if(mType.getX() != b.getWidth() ||
137 mType.getY() != b.getHeight()) {
138 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
139 }
140 }
141
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800142 public void copyFrom(int[] d) {
143 mRS.validate();
144 subData1D(0, mType.getCount(), d);
145 }
146 public void copyFrom(short[] d) {
147 mRS.validate();
148 subData1D(0, mType.getCount(), d);
149 }
150 public void copyFrom(byte[] d) {
151 mRS.validate();
152 subData1D(0, mType.getCount(), d);
153 }
154 public void copyFrom(float[] d) {
155 mRS.validate();
156 subData1D(0, mType.getCount(), d);
157 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800158 public void copyFrom(Bitmap b) {
Jason Sams4ef66502010-12-10 16:03:15 -0800159 validateBitmap(b);
160 mRS.nAllocationCopyFromBitmap(getID(), b);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700161 }
162
Jason Sams4ef66502010-12-10 16:03:15 -0800163 public void copyTo(Bitmap b) {
164 validateBitmap(b);
165 mRS.nAllocationCopyToBitmap(getID(), b);
166 }
167
168
Jason Sams49bdaf02010-08-31 13:50:42 -0700169 public void subData(int xoff, FieldPacker fp) {
Jason Samsa70f4162010-03-26 15:33:42 -0700170 int eSize = mType.mElement.getSizeBytes();
171 final byte[] data = fp.getData();
172
173 int count = data.length / eSize;
174 if ((eSize * count) != data.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800175 throw new RSIllegalArgumentException("Field packer length " + data.length +
Jason Samsa70f4162010-03-26 15:33:42 -0700176 " not divisible by element size " + eSize + ".");
177 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700178 data1DChecks(xoff, count, data.length, data.length);
Jason Sams06d69de2010-11-09 17:11:40 -0800179 mRS.nAllocationSubData1D(getID(), xoff, count, data, data.length);
Jason Sams49bdaf02010-08-31 13:50:42 -0700180 }
181
182
183 public void subElementData(int xoff, int component_number, FieldPacker fp) {
184 if (component_number >= mType.mElement.mElements.length) {
Jason Sams06d69de2010-11-09 17:11:40 -0800185 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700186 }
187 if(xoff < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800188 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Sams49bdaf02010-08-31 13:50:42 -0700189 }
190
191 final byte[] data = fp.getData();
192 int eSize = mType.mElement.mElements[component_number].getSizeBytes();
193
194 if (data.length != eSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800195 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
Jason Sams49bdaf02010-08-31 13:50:42 -0700196 " does not match component size " + eSize + ".");
197 }
198
Jason Sams06d69de2010-11-09 17:11:40 -0800199 mRS.nAllocationSubElementData1D(getID(), xoff, component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700200 }
201
Jason Sams768bc022009-09-21 19:41:04 -0700202 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800203 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700204 if(off < 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800205 throw new RSIllegalArgumentException("Offset must be >= 0.");
Jason Samsa70f4162010-03-26 15:33:42 -0700206 }
207 if(count < 1) {
Jason Sams06d69de2010-11-09 17:11:40 -0800208 throw new RSIllegalArgumentException("Count must be >= 1.");
Jason Samsa70f4162010-03-26 15:33:42 -0700209 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800210 if((off + count) > mType.getCount()) {
211 throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() +
Jason Samsa70f4162010-03-26 15:33:42 -0700212 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700213 }
Jason Sams768bc022009-09-21 19:41:04 -0700214 if((len) < dataSize) {
Jason Sams06d69de2010-11-09 17:11:40 -0800215 throw new RSIllegalArgumentException("Array too small for allocation type.");
Jason Sams768bc022009-09-21 19:41:04 -0700216 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700217 }
218
219 public void subData1D(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700220 int dataSize = mType.mElement.getSizeBytes() * count;
221 data1DChecks(off, count, d.length * 4, 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, short[] d) {
225 int dataSize = mType.mElement.getSizeBytes() * count;
226 data1DChecks(off, count, d.length * 2, dataSize);
Jason Sams06d69de2010-11-09 17:11:40 -0800227 mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700228 }
229 public void subData1D(int off, int count, byte[] d) {
230 int dataSize = mType.mElement.getSizeBytes() * count;
231 data1DChecks(off, count, d.length, dataSize);
Jason Sams06d69de2010-11-09 17:11:40 -0800232 mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
Jason Sams768bc022009-09-21 19:41:04 -0700233 }
234 public void subData1D(int off, int count, float[] d) {
235 int dataSize = mType.mElement.getSizeBytes() * count;
236 data1DChecks(off, count, d.length * 4, dataSize);
Jason Sams06d69de2010-11-09 17:11:40 -0800237 mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700238 }
239
Jason Sams768bc022009-09-21 19:41:04 -0700240
Jason Samsb8c5a842009-07-31 20:40:47 -0700241 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800242 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800243 mRS.nAllocationSubData2D(getID(), xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700244 }
245
246 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800247 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800248 mRS.nAllocationSubData2D(getID(), xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700249 }
250
Jason Sams40a29e82009-08-10 14:55:26 -0700251 public void readData(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800252 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800253 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700254 }
255
256 public void readData(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800257 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800258 mRS.nAllocationRead(getID(), d);
Jason Sams40a29e82009-08-10 14:55:26 -0700259 }
260
Jason Sams31a7e422010-10-26 13:09:17 -0700261 public synchronized void resize(int dimX) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800262 if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800263 throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700264 }
Jason Sams06d69de2010-11-09 17:11:40 -0800265 mRS.nAllocationResize1D(getID(), dimX);
Jason Samsd26297f2010-11-01 16:08:59 -0700266 mRS.finish(); // Necessary because resize is fifoed and update is async.
Jason Sams31a7e422010-10-26 13:09:17 -0700267
Jason Sams06d69de2010-11-09 17:11:40 -0800268 int typeID = mRS.nAllocationGetType(getID());
Jason Sams31a7e422010-10-26 13:09:17 -0700269 mType = new Type(typeID, mRS);
270 mType.updateFromNative();
Jason Sams5edc6082010-10-05 13:32:49 -0700271 }
272
273 /*
274 public void resize(int dimX, int dimY) {
275 if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
Jason Sams06d69de2010-11-09 17:11:40 -0800276 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700277 }
278 if (mType.getY() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800279 throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
Jason Sams5edc6082010-10-05 13:32:49 -0700280 }
Jason Sams06d69de2010-11-09 17:11:40 -0800281 mRS.nAllocationResize2D(getID(), dimX, dimY);
Jason Sams5edc6082010-10-05 13:32:49 -0700282 }
283 */
Jason Sams40a29e82009-08-10 14:55:26 -0700284
Jason Sams5476b452010-12-08 16:14:36 -0800285 /*
Jason Samsb8c5a842009-07-31 20:40:47 -0700286 public class Adapter1D extends BaseObj {
287 Adapter1D(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700288 super(id, rs);
Jason Samsb8c5a842009-07-31 20:40:47 -0700289 }
290
Jason Samsb8c5a842009-07-31 20:40:47 -0700291 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800292 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800293 mRS.nAdapter1DSetConstraint(getID(), dim.mID, value);
Jason Samsb8c5a842009-07-31 20:40:47 -0700294 }
295
296 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800297 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800298 mRS.nAdapter1DData(getID(), d);
Jason Samsb8c5a842009-07-31 20:40:47 -0700299 }
300
Jason Samsb8c5a842009-07-31 20:40:47 -0700301 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800302 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800303 mRS.nAdapter1DData(getID(), d);
Jason Samsb8c5a842009-07-31 20:40:47 -0700304 }
305
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700306 public void subData(int off, int count, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800307 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800308 mRS.nAdapter1DSubData(getID(), off, count, d);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700309 }
310
Jason Samsb8c5a842009-07-31 20:40:47 -0700311 public void subData(int off, int count, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800312 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800313 mRS.nAdapter1DSubData(getID(), off, count, d);
Jason Samsb8c5a842009-07-31 20:40:47 -0700314 }
315 }
316
317 public Adapter1D createAdapter1D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800318 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700319 int id = mRS.nAdapter1DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800320 if(id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800321 throw new RSRuntimeException("Adapter creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700322 }
Jason Sams06d69de2010-11-09 17:11:40 -0800323 mRS.nAdapter1DBindAllocation(id, getID());
Jason Samsb8c5a842009-07-31 20:40:47 -0700324 return new Adapter1D(id, mRS);
325 }
Jason Sams5476b452010-12-08 16:14:36 -0800326 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700327
328
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700329 public class Adapter2D extends BaseObj {
330 Adapter2D(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700331 super(id, rs);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700332 }
333
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700334 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800335 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800336 mRS.nAdapter2DSetConstraint(getID(), dim.mID, value);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700337 }
338
339 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800340 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800341 mRS.nAdapter2DData(getID(), d);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700342 }
343
344 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800345 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800346 mRS.nAdapter2DData(getID(), d);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700347 }
348
349 public void subData(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800350 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800351 mRS.nAdapter2DSubData(getID(), xoff, yoff, w, h, d);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700352 }
353
354 public void subData(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800355 mRS.validate();
Jason Sams06d69de2010-11-09 17:11:40 -0800356 mRS.nAdapter2DSubData(getID(), xoff, yoff, w, h, d);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700357 }
358 }
359
360 public Adapter2D createAdapter2D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800361 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700362 int id = mRS.nAdapter2DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800363 if(id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800364 throw new RSRuntimeException("allocation failed.");
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700365 }
Jason Sams06d69de2010-11-09 17:11:40 -0800366 mRS.nAdapter2DBindAllocation(id, getID());
367 if(id == 0) {
368 throw new RSRuntimeException("Adapter creation failed.");
369 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700370 return new Adapter2D(id, mRS);
371 }
372
Jason Samsb8c5a842009-07-31 20:40:47 -0700373
374 // creation
375
376 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
377 static {
378 mBitmapOptions.inScaled = false;
379 }
380
Jason Samsd4b23b52010-12-13 15:32:35 -0800381 static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mc, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800382 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800383 if (type.getID() == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800384 throw new RSInvalidStateException("Bad Type");
Jason Sams1bada8c2009-08-09 17:01:55 -0700385 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800386 int id = rs.nAllocationCreateTyped(type.getID(), mc.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800387 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800388 throw new RSRuntimeException("Allocation creation failed.");
389 }
Jason Sams5476b452010-12-08 16:14:36 -0800390 return new Allocation(id, rs, type, usage);
Jason Samsb8c5a842009-07-31 20:40:47 -0700391 }
392
Jason Sams5476b452010-12-08 16:14:36 -0800393 static public Allocation createTyped(RenderScript rs, Type type) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800394 return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -0800395 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700396
Jason Sams5476b452010-12-08 16:14:36 -0800397 static public Allocation createSized(RenderScript rs, Element e,
398 int count, int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800399 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700400 Type.Builder b = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800401 b.setX(count);
Jason Sams768bc022009-09-21 19:41:04 -0700402 Type t = b.create();
403
Jason Samsd4b23b52010-12-13 15:32:35 -0800404 int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
Jason Sams5476b452010-12-08 16:14:36 -0800405 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800406 throw new RSRuntimeException("Allocation creation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700407 }
Jason Sams5476b452010-12-08 16:14:36 -0800408 return new Allocation(id, rs, t, usage);
409 }
410
411 static public Allocation createSized(RenderScript rs, Element e, int count) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800412 return createSized(rs, e, count, USAGE_SCRIPT);
Jason Samsb8c5a842009-07-31 20:40:47 -0700413 }
414
Jason Sams8a647432010-03-01 15:31:04 -0800415 static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
416 final Bitmap.Config bc = b.getConfig();
417 if (bc == Bitmap.Config.ALPHA_8) {
418 return Element.A_8(rs);
419 }
420 if (bc == Bitmap.Config.ARGB_4444) {
421 return Element.RGBA_4444(rs);
422 }
423 if (bc == Bitmap.Config.ARGB_8888) {
424 return Element.RGBA_8888(rs);
425 }
426 if (bc == Bitmap.Config.RGB_565) {
427 return Element.RGB_565(rs);
428 }
Jeff Sharkey4bd1a3d2010-11-16 13:46:34 -0800429 throw new RSInvalidStateException("Bad bitmap type: " + bc);
Jason Sams8a647432010-03-01 15:31:04 -0800430 }
431
Jason Sams5476b452010-12-08 16:14:36 -0800432 static private Type typeFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800433 MipmapControl mip) {
Jason Sams8a647432010-03-01 15:31:04 -0800434 Element e = elementFromBitmap(rs, b);
435 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800436 tb.setX(b.getWidth());
437 tb.setY(b.getHeight());
Jason Sams4ef66502010-12-10 16:03:15 -0800438 tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
Jason Sams8a647432010-03-01 15:31:04 -0800439 return tb.create();
440 }
441
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800442 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800443 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800444 int usage) {
Jason Sams771bebb2009-12-07 12:40:12 -0800445 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800446 Type t = typeFromBitmap(rs, b, mips);
Jason Sams8a647432010-03-01 15:31:04 -0800447
Jason Sams5476b452010-12-08 16:14:36 -0800448 int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
449 if (id == 0) {
Jason Sams06d69de2010-11-09 17:11:40 -0800450 throw new RSRuntimeException("Load failed.");
Jason Sams718cd1f2009-12-23 14:35:29 -0800451 }
Jason Sams5476b452010-12-08 16:14:36 -0800452 return new Allocation(id, rs, t, usage);
453 }
454
455 static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
456 Element dstFmt, boolean genMips) {
Jason Sams4ef66502010-12-10 16:03:15 -0800457 MipmapControl mc = MipmapControl.MIPMAP_NONE;
Jason Sams5476b452010-12-08 16:14:36 -0800458 if (genMips) {
Jason Sams4ef66502010-12-10 16:03:15 -0800459 mc = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
Jason Sams5476b452010-12-08 16:14:36 -0800460 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800461 return createFromBitmap(rs, b, mc, USAGE_GRAPHICS_TEXTURE);
Jason Sams8a647432010-03-01 15:31:04 -0800462 }
463
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800464 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
Jason Sams4ef66502010-12-10 16:03:15 -0800465 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800466 CubemapLayout layout,
467 int usage) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800468 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800469
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800470 int height = b.getHeight();
471 int width = b.getWidth();
472
473 if (layout != CubemapLayout.VERTICAL_FACE_LIST) {
474 throw new RSIllegalArgumentException("Only vertical face list supported");
475 }
476 if (height % 6 != 0) {
477 throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
478 }
479 if (height / 6 != width) {
480 throw new RSIllegalArgumentException("Only square cobe map faces supported");
481 }
482 boolean isPow2 = (width & (width - 1)) == 0;
483 if (!isPow2) {
484 throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
485 }
486
487 Element e = elementFromBitmap(rs, b);
488 Type.Builder tb = new Type.Builder(rs, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800489 tb.setX(width);
490 tb.setY(width);
491 tb.setFaces(true);
Jason Sams4ef66502010-12-10 16:03:15 -0800492 tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800493 Type t = tb.create();
494
Jason Sams5476b452010-12-08 16:14:36 -0800495 int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800496 if(id == 0) {
497 throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
498 }
Jason Sams5476b452010-12-08 16:14:36 -0800499 return new Allocation(id, rs, t, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800500 }
501
Jason Sams5476b452010-12-08 16:14:36 -0800502 static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
503 Element dstFmt,
504 boolean genMips,
505 CubemapLayout layout) {
Jason Sams4ef66502010-12-10 16:03:15 -0800506 MipmapControl mc = MipmapControl.MIPMAP_NONE;
Jason Sams5476b452010-12-08 16:14:36 -0800507 if (genMips) {
Jason Sams4ef66502010-12-10 16:03:15 -0800508 mc = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
Jason Sams5476b452010-12-08 16:14:36 -0800509 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800510 return createCubemapFromBitmap(rs, b, mc, layout, USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -0800511 }
512
Jason Sams5476b452010-12-08 16:14:36 -0800513 static public Allocation createFromBitmapResource(RenderScript rs,
514 Resources res,
515 int id,
Jason Sams4ef66502010-12-10 16:03:15 -0800516 MipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800517 int usage) {
Jason Samsb8c5a842009-07-31 20:40:47 -0700518
Jason Sams771bebb2009-12-07 12:40:12 -0800519 rs.validate();
Jason Sams5476b452010-12-08 16:14:36 -0800520 Bitmap b = BitmapFactory.decodeResource(res, id);
521 Allocation alloc = createFromBitmap(rs, b, mips, usage);
522 b.recycle();
523 return alloc;
Jason Samsb8c5a842009-07-31 20:40:47 -0700524 }
525
Jason Sams5476b452010-12-08 16:14:36 -0800526 static public Allocation createFromBitmapResource(RenderScript rs,
527 Resources res,
528 int id,
529 Element dstFmt,
530 boolean genMips) {
Jason Sams4ef66502010-12-10 16:03:15 -0800531 MipmapControl mc = MipmapControl.MIPMAP_NONE;
Jason Sams5476b452010-12-08 16:14:36 -0800532 if (genMips) {
Jason Sams4ef66502010-12-10 16:03:15 -0800533 mc = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
Jason Sams5476b452010-12-08 16:14:36 -0800534 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800535 return createFromBitmapResource(rs, res, id, mc, USAGE_GRAPHICS_TEXTURE);
Jason Sams5476b452010-12-08 16:14:36 -0800536 }
537
538 static public Allocation createFromString(RenderScript rs,
539 String str,
540 int usage) {
541 rs.validate();
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700542 byte[] allocArray = null;
543 try {
544 allocArray = str.getBytes("UTF-8");
Jason Sams5476b452010-12-08 16:14:36 -0800545 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800546 alloc.copyFrom(allocArray);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700547 return alloc;
548 }
549 catch (Exception e) {
Jason Sams06d69de2010-11-09 17:11:40 -0800550 throw new RSRuntimeException("Could not convert string to utf-8.");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700551 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700552 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700553}
554
555