blob: 985d700147dc8008a20bff49f8d6766301492f44 [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 Sams43ee06852009-08-12 17:54:11 -070036
37 Allocation(int id, RenderScript rs, Type t) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070038 super(id, rs);
Jason Sams43ee06852009-08-12 17:54:11 -070039 mType = t;
Jason Samsb8c5a842009-07-31 20:40:47 -070040 }
41
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -070042 Allocation(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070043 super(id, rs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -070044 }
45
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070046 @Override
47 void updateFromNative() {
48 mRS.validate();
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -070049 mName = mRS.nGetName(mID);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070050 int typeID = mRS.nAllocationGetType(mID);
51 if(typeID != 0) {
52 mType = new Type(typeID, mRS);
53 mType.updateFromNative();
54 }
55 }
56
Jason Samsea87e962010-01-12 12:12:28 -080057 public Type getType() {
58 return mType;
59 }
60
Jason Samsb8c5a842009-07-31 20:40:47 -070061 public void uploadToTexture(int baseMipLevel) {
Jason Sams771bebb2009-12-07 12:40:12 -080062 mRS.validate();
Jason Samsc2908e62010-02-23 17:44:28 -080063 mRS.nAllocationUploadToTexture(mID, false, baseMipLevel);
64 }
65
66 public void uploadToTexture(boolean genMips, int baseMipLevel) {
67 mRS.validate();
68 mRS.nAllocationUploadToTexture(mID, genMips, baseMipLevel);
Jason Samsb8c5a842009-07-31 20:40:47 -070069 }
70
Jason Sams07ae4062009-08-27 20:23:34 -070071 public void uploadToBufferObject() {
Jason Sams771bebb2009-12-07 12:40:12 -080072 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -070073 mRS.nAllocationUploadToBufferObject(mID);
74 }
75
Jason Samsb8c5a842009-07-31 20:40:47 -070076 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080077 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070078 subData1D(0, mType.getElementCount(), d);
79 }
80 public void data(short[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080081 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070082 subData1D(0, mType.getElementCount(), d);
83 }
84 public void data(byte[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080085 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070086 subData1D(0, mType.getElementCount(), d);
87 }
88 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080089 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070090 subData1D(0, mType.getElementCount(), d);
Jason Samsb8c5a842009-07-31 20:40:47 -070091 }
92
Jason Sams49bdaf02010-08-31 13:50:42 -070093 public void subData(int xoff, FieldPacker fp) {
Jason Samsa70f4162010-03-26 15:33:42 -070094 int eSize = mType.mElement.getSizeBytes();
95 final byte[] data = fp.getData();
96
97 int count = data.length / eSize;
98 if ((eSize * count) != data.length) {
99 throw new IllegalArgumentException("Field packer length " + data.length +
100 " not divisible by element size " + eSize + ".");
101 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700102 data1DChecks(xoff, count, data.length, data.length);
103 mRS.nAllocationSubData1D(mID, xoff, count, data, data.length);
104 }
105
106
107 public void subElementData(int xoff, int component_number, FieldPacker fp) {
108 if (component_number >= mType.mElement.mElements.length) {
109 throw new IllegalArgumentException("Component_number " + component_number + " out of range.");
110 }
111 if(xoff < 0) {
112 throw new IllegalArgumentException("Offset must be >= 0.");
113 }
114
115 final byte[] data = fp.getData();
116 int eSize = mType.mElement.mElements[component_number].getSizeBytes();
117
118 if (data.length != eSize) {
119 throw new IllegalArgumentException("Field packer sizelength " + data.length +
120 " does not match component size " + eSize + ".");
121 }
122
123 mRS.nAllocationSubElementData1D(mID, xoff, component_number, data, data.length);
Jason Samsa70f4162010-03-26 15:33:42 -0700124 }
125
Jason Sams768bc022009-09-21 19:41:04 -0700126 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800127 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700128 if(off < 0) {
129 throw new IllegalArgumentException("Offset must be >= 0.");
130 }
131 if(count < 1) {
132 throw new IllegalArgumentException("Count must be >= 1.");
133 }
134 if((off + count) > mType.getElementCount()) {
135 throw new IllegalArgumentException("Overflow, Available count " + mType.getElementCount() +
136 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700137 }
Jason Sams768bc022009-09-21 19:41:04 -0700138 if((len) < dataSize) {
139 throw new IllegalArgumentException("Array too small for allocation type.");
140 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700141 }
142
143 public void subData1D(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700144 int dataSize = mType.mElement.getSizeBytes() * count;
145 data1DChecks(off, count, d.length * 4, dataSize);
146 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
147 }
148 public void subData1D(int off, int count, short[] d) {
149 int dataSize = mType.mElement.getSizeBytes() * count;
150 data1DChecks(off, count, d.length * 2, dataSize);
151 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
152 }
153 public void subData1D(int off, int count, byte[] d) {
154 int dataSize = mType.mElement.getSizeBytes() * count;
155 data1DChecks(off, count, d.length, dataSize);
156 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
157 }
158 public void subData1D(int off, int count, float[] d) {
159 int dataSize = mType.mElement.getSizeBytes() * count;
160 data1DChecks(off, count, d.length * 4, dataSize);
161 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700162 }
163
Jason Sams768bc022009-09-21 19:41:04 -0700164
Jason Samsb8c5a842009-07-31 20:40:47 -0700165 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800166 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700167 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700168 }
169
170 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800171 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700172 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700173 }
174
Jason Sams40a29e82009-08-10 14:55:26 -0700175 public void readData(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800176 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700177 mRS.nAllocationRead(mID, d);
178 }
179
180 public void readData(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800181 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700182 mRS.nAllocationRead(mID, d);
183 }
184
185
Jason Samsb8c5a842009-07-31 20:40:47 -0700186 public class Adapter1D extends BaseObj {
187 Adapter1D(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700188 super(id, rs);
Jason Samsb8c5a842009-07-31 20:40:47 -0700189 }
190
Jason Samsb8c5a842009-07-31 20:40:47 -0700191 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800192 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700193 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
194 }
195
196 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800197 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700198 mRS.nAdapter1DData(mID, d);
199 }
200
Jason Samsb8c5a842009-07-31 20:40:47 -0700201 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800202 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700203 mRS.nAdapter1DData(mID, d);
204 }
205
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700206 public void subData(int off, int count, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800207 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700208 mRS.nAdapter1DSubData(mID, off, count, d);
209 }
210
Jason Samsb8c5a842009-07-31 20:40:47 -0700211 public void subData(int off, int count, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800212 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700213 mRS.nAdapter1DSubData(mID, off, count, d);
214 }
215 }
216
217 public Adapter1D createAdapter1D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800218 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700219 int id = mRS.nAdapter1DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800220 if(id == 0) {
221 throw new IllegalStateException("allocation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700222 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800223 mRS.nAdapter1DBindAllocation(id, mID);
Jason Samsb8c5a842009-07-31 20:40:47 -0700224 return new Adapter1D(id, mRS);
225 }
226
227
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700228 public class Adapter2D extends BaseObj {
229 Adapter2D(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700230 super(id, rs);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700231 }
232
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700233 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800234 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700235 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
236 }
237
238 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800239 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700240 mRS.nAdapter2DData(mID, d);
241 }
242
243 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800244 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700245 mRS.nAdapter2DData(mID, d);
246 }
247
248 public void subData(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800249 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700250 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
251 }
252
253 public void subData(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800254 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700255 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
256 }
257 }
258
259 public Adapter2D createAdapter2D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800260 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700261 int id = mRS.nAdapter2DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800262 if(id == 0) {
263 throw new IllegalStateException("allocation failed.");
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700264 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800265 mRS.nAdapter2DBindAllocation(id, mID);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700266 return new Adapter2D(id, mRS);
267 }
268
Jason Samsb8c5a842009-07-31 20:40:47 -0700269
270 // creation
271
272 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
273 static {
274 mBitmapOptions.inScaled = false;
275 }
276
Jason Sams1bada8c2009-08-09 17:01:55 -0700277 static public Allocation createTyped(RenderScript rs, Type type)
278 throws IllegalArgumentException {
279
Jason Sams771bebb2009-12-07 12:40:12 -0800280 rs.validate();
Jason Sams1bada8c2009-08-09 17:01:55 -0700281 if(type.mID == 0) {
282 throw new IllegalStateException("Bad Type");
283 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700284 int id = rs.nAllocationCreateTyped(type.mID);
Jason Sams43ee06852009-08-12 17:54:11 -0700285 return new Allocation(id, rs, type);
Jason Samsb8c5a842009-07-31 20:40:47 -0700286 }
287
Jason Sams1bada8c2009-08-09 17:01:55 -0700288 static public Allocation createSized(RenderScript rs, Element e, int count)
289 throws IllegalArgumentException {
290
Jason Sams771bebb2009-12-07 12:40:12 -0800291 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700292 Type.Builder b = new Type.Builder(rs, e);
293 b.add(Dimension.X, count);
294 Type t = b.create();
295
296 int id = rs.nAllocationCreateTyped(t.mID);
Jason Samsea84a7c2009-09-04 14:42:41 -0700297 if(id == 0) {
298 throw new IllegalStateException("Bad element.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700299 }
Jason Sams768bc022009-09-21 19:41:04 -0700300 return new Allocation(id, rs, t);
Jason Samsb8c5a842009-07-31 20:40:47 -0700301 }
302
Jason Sams8a647432010-03-01 15:31:04 -0800303 static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
304 final Bitmap.Config bc = b.getConfig();
305 if (bc == Bitmap.Config.ALPHA_8) {
306 return Element.A_8(rs);
307 }
308 if (bc == Bitmap.Config.ARGB_4444) {
309 return Element.RGBA_4444(rs);
310 }
311 if (bc == Bitmap.Config.ARGB_8888) {
312 return Element.RGBA_8888(rs);
313 }
314 if (bc == Bitmap.Config.RGB_565) {
315 return Element.RGB_565(rs);
316 }
317 throw new IllegalStateException("Bad bitmap type.");
318 }
319
320 static private Type typeFromBitmap(RenderScript rs, Bitmap b) {
321 Element e = elementFromBitmap(rs, b);
322 Type.Builder tb = new Type.Builder(rs, e);
323 tb.add(Dimension.X, b.getWidth());
324 tb.add(Dimension.Y, b.getHeight());
325 return tb.create();
326 }
327
Jason Samsb8c5a842009-07-31 20:40:47 -0700328 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
329 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700330
Jason Sams771bebb2009-12-07 12:40:12 -0800331 rs.validate();
Jason Sams8a647432010-03-01 15:31:04 -0800332 Type t = typeFromBitmap(rs, b);
333
Jason Samsea84a7c2009-09-04 14:42:41 -0700334 int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800335 if(id == 0) {
336 throw new IllegalStateException("Load failed.");
337 }
Jason Sams8a647432010-03-01 15:31:04 -0800338 return new Allocation(id, rs, t);
339 }
340
341 static public Allocation createBitmapRef(RenderScript rs, Bitmap b)
342 throws IllegalArgumentException {
343
344 rs.validate();
345 Type t = typeFromBitmap(rs, b);
346
347 int id = rs.nAllocationCreateBitmapRef(t.getID(), b);
348 if(id == 0) {
349 throw new IllegalStateException("Load failed.");
350 }
351
352 Allocation a = new Allocation(id, rs, t);
353 a.mBitmap = b;
354 return a;
Jason Samsb8c5a842009-07-31 20:40:47 -0700355 }
356
Jason Sams74e02ef2010-01-06 15:10:29 -0800357 static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
Jason Samsb8c5a842009-07-31 20:40:47 -0700358 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700359
Jason Sams771bebb2009-12-07 12:40:12 -0800360 rs.validate();
Jason Samsea84a7c2009-09-04 14:42:41 -0700361 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800362 if(id == 0) {
363 throw new IllegalStateException("Load failed.");
364 }
Jason Sams43ee06852009-08-12 17:54:11 -0700365 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700366 }
367
368 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
369 throws IllegalArgumentException {
370
Jason Sams771bebb2009-12-07 12:40:12 -0800371 rs.validate();
Romain Guy650a3eb2009-08-31 14:06:43 -0700372 InputStream is = null;
373 try {
374 final TypedValue value = new TypedValue();
375 is = res.openRawResource(id, value);
376
377 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
Jason Samsea84a7c2009-09-04 14:42:41 -0700378 int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
Romain Guy650a3eb2009-08-31 14:06:43 -0700379 asset);
380
Jason Sams718cd1f2009-12-23 14:35:29 -0800381 if(allocationId == 0) {
382 throw new IllegalStateException("Load failed.");
383 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700384 return new Allocation(allocationId, rs, null);
Romain Guy650a3eb2009-08-31 14:06:43 -0700385 } catch (Exception e) {
386 // Ignore
387 } finally {
388 if (is != null) {
389 try {
390 is.close();
391 } catch (IOException e) {
392 // Ignore
393 }
394 }
395 }
396
397 return null;
Jason Samsb8c5a842009-07-31 20:40:47 -0700398 }
399
400 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
401 throws IllegalArgumentException {
402
Romain Guyf92a0a62010-08-20 15:43:52 -0700403 mBitmapOptions.inPreferredConfig = null;
404 if (dstFmt == rs.mElement_RGBA_8888) {
405 mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
406 } else if (dstFmt == rs.mElement_RGB_888) {
407 mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
408 } else if (dstFmt == rs.mElement_RGBA_4444) {
409 mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_4444;
410 } else if (dstFmt == rs.mElement_RGB_565) {
411 mBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
412 }
413
Jason Samsb8c5a842009-07-31 20:40:47 -0700414 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
415 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
416 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700417
418 static public Allocation createFromString(RenderScript rs, String str)
419 throws IllegalArgumentException {
420 byte[] allocArray = null;
421 try {
422 allocArray = str.getBytes("UTF-8");
423 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length);
424 alloc.data(allocArray);
425 return alloc;
426 }
427 catch (Exception e) {
428 Log.e("rs", "could not convert string to utf-8");
429 }
430 return null;
431 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700432}
433
434