blob: 87735b5856b68f728d9aa531f7928e43cb445297 [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) {
Jason Samsb8c5a842009-07-31 20:40:47 -070038 super(rs);
39 mID = id;
Jason Sams43ee06852009-08-12 17:54:11 -070040 mType = t;
Jason Samsb8c5a842009-07-31 20:40:47 -070041 }
42
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -070043 Allocation(int id, RenderScript rs) {
44 super(rs);
45 mID = id;
46 }
47
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070048 @Override
49 void updateFromNative() {
50 mRS.validate();
51 int typeID = mRS.nAllocationGetType(mID);
52 if(typeID != 0) {
53 mType = new Type(typeID, mRS);
54 mType.updateFromNative();
55 }
56 }
57
Jason Samsea87e962010-01-12 12:12:28 -080058 public Type getType() {
59 return mType;
60 }
61
Jason Samsb8c5a842009-07-31 20:40:47 -070062 public void uploadToTexture(int baseMipLevel) {
Jason Sams771bebb2009-12-07 12:40:12 -080063 mRS.validate();
Jason Samsc2908e62010-02-23 17:44:28 -080064 mRS.nAllocationUploadToTexture(mID, false, baseMipLevel);
65 }
66
67 public void uploadToTexture(boolean genMips, int baseMipLevel) {
68 mRS.validate();
69 mRS.nAllocationUploadToTexture(mID, genMips, baseMipLevel);
Jason Samsb8c5a842009-07-31 20:40:47 -070070 }
71
Jason Sams07ae4062009-08-27 20:23:34 -070072 public void uploadToBufferObject() {
Jason Sams771bebb2009-12-07 12:40:12 -080073 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -070074 mRS.nAllocationUploadToBufferObject(mID);
75 }
76
Jason Samsb8c5a842009-07-31 20:40:47 -070077 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080078 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070079 subData1D(0, mType.getElementCount(), d);
80 }
81 public void data(short[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080082 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070083 subData1D(0, mType.getElementCount(), d);
84 }
85 public void data(byte[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080086 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070087 subData1D(0, mType.getElementCount(), d);
88 }
89 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080090 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070091 subData1D(0, mType.getElementCount(), d);
Jason Samsb8c5a842009-07-31 20:40:47 -070092 }
93
Jason Samsa70f4162010-03-26 15:33:42 -070094 public void subData(int off, FieldPacker fp) {
95 int eSize = mType.mElement.getSizeBytes();
96 final byte[] data = fp.getData();
97
98 int count = data.length / eSize;
99 if ((eSize * count) != data.length) {
100 throw new IllegalArgumentException("Field packer length " + data.length +
101 " not divisible by element size " + eSize + ".");
102 }
103 data1DChecks(off, count, data.length, data.length);
104 mRS.nAllocationSubData1D(mID, off, count, data, data.length);
105 }
106
Jason Sams768bc022009-09-21 19:41:04 -0700107 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800108 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700109 if(off < 0) {
110 throw new IllegalArgumentException("Offset must be >= 0.");
111 }
112 if(count < 1) {
113 throw new IllegalArgumentException("Count must be >= 1.");
114 }
115 if((off + count) > mType.getElementCount()) {
116 throw new IllegalArgumentException("Overflow, Available count " + mType.getElementCount() +
117 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700118 }
Jason Sams768bc022009-09-21 19:41:04 -0700119 if((len) < dataSize) {
120 throw new IllegalArgumentException("Array too small for allocation type.");
121 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700122 }
123
124 public void subData1D(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700125 int dataSize = mType.mElement.getSizeBytes() * count;
126 data1DChecks(off, count, d.length * 4, dataSize);
127 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
128 }
129 public void subData1D(int off, int count, short[] d) {
130 int dataSize = mType.mElement.getSizeBytes() * count;
131 data1DChecks(off, count, d.length * 2, dataSize);
132 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
133 }
134 public void subData1D(int off, int count, byte[] d) {
135 int dataSize = mType.mElement.getSizeBytes() * count;
136 data1DChecks(off, count, d.length, dataSize);
137 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
138 }
139 public void subData1D(int off, int count, float[] d) {
140 int dataSize = mType.mElement.getSizeBytes() * count;
141 data1DChecks(off, count, d.length * 4, dataSize);
142 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700143 }
144
Jason Sams768bc022009-09-21 19:41:04 -0700145
Jason Samsb8c5a842009-07-31 20:40:47 -0700146
147 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800148 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700149 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700150 }
151
152 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800153 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700154 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700155 }
156
Jason Sams40a29e82009-08-10 14:55:26 -0700157 public void readData(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800158 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700159 mRS.nAllocationRead(mID, d);
160 }
161
162 public void readData(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800163 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700164 mRS.nAllocationRead(mID, d);
165 }
166
Jason Sams43ee06852009-08-12 17:54:11 -0700167 public void data(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800168 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700169 mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
Jason Sams43ee06852009-08-12 17:54:11 -0700170 }
171
Jason Sams5f43fd22009-09-15 12:39:22 -0700172 public void read(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800173 mRS.validate();
Jason Sams5f43fd22009-09-15 12:39:22 -0700174 mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
175 }
176
Jason Sams2525a812009-09-03 15:43:13 -0700177 public void subData(int offset, Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800178 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700179 mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
180 }
Jason Sams40a29e82009-08-10 14:55:26 -0700181
Jason Samsb8c5a842009-07-31 20:40:47 -0700182 public class Adapter1D extends BaseObj {
183 Adapter1D(int id, RenderScript rs) {
184 super(rs);
185 mID = id;
186 }
187
Jason Samsb8c5a842009-07-31 20:40:47 -0700188 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800189 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700190 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
191 }
192
193 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800194 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700195 mRS.nAdapter1DData(mID, d);
196 }
197
Jason Samsb8c5a842009-07-31 20:40:47 -0700198 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800199 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700200 mRS.nAdapter1DData(mID, d);
201 }
202
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700203 public void subData(int off, int count, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800204 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700205 mRS.nAdapter1DSubData(mID, off, count, d);
206 }
207
Jason Samsb8c5a842009-07-31 20:40:47 -0700208 public void subData(int off, int count, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800209 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700210 mRS.nAdapter1DSubData(mID, off, count, d);
211 }
212 }
213
214 public Adapter1D createAdapter1D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800215 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700216 int id = mRS.nAdapter1DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800217 if(id == 0) {
218 throw new IllegalStateException("allocation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700219 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800220 mRS.nAdapter1DBindAllocation(id, mID);
Jason Samsb8c5a842009-07-31 20:40:47 -0700221 return new Adapter1D(id, mRS);
222 }
223
224
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700225 public class Adapter2D extends BaseObj {
226 Adapter2D(int id, RenderScript rs) {
227 super(rs);
228 mID = id;
229 }
230
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700231 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800232 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700233 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
234 }
235
236 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800237 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700238 mRS.nAdapter2DData(mID, d);
239 }
240
241 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800242 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700243 mRS.nAdapter2DData(mID, d);
244 }
245
246 public void subData(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800247 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700248 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
249 }
250
251 public void subData(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800252 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700253 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
254 }
255 }
256
257 public Adapter2D createAdapter2D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800258 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700259 int id = mRS.nAdapter2DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800260 if(id == 0) {
261 throw new IllegalStateException("allocation failed.");
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700262 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800263 mRS.nAdapter2DBindAllocation(id, mID);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700264 return new Adapter2D(id, mRS);
265 }
266
Jason Samsb8c5a842009-07-31 20:40:47 -0700267
268 // creation
269
270 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
271 static {
272 mBitmapOptions.inScaled = false;
273 }
274
Jason Sams1bada8c2009-08-09 17:01:55 -0700275 static public Allocation createTyped(RenderScript rs, Type type)
276 throws IllegalArgumentException {
277
Jason Sams771bebb2009-12-07 12:40:12 -0800278 rs.validate();
Jason Sams1bada8c2009-08-09 17:01:55 -0700279 if(type.mID == 0) {
280 throw new IllegalStateException("Bad Type");
281 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700282 int id = rs.nAllocationCreateTyped(type.mID);
Jason Sams43ee06852009-08-12 17:54:11 -0700283 return new Allocation(id, rs, type);
Jason Samsb8c5a842009-07-31 20:40:47 -0700284 }
285
Jason Sams1bada8c2009-08-09 17:01:55 -0700286 static public Allocation createSized(RenderScript rs, Element e, int count)
287 throws IllegalArgumentException {
288
Jason Sams771bebb2009-12-07 12:40:12 -0800289 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700290 Type.Builder b = new Type.Builder(rs, e);
291 b.add(Dimension.X, count);
292 Type t = b.create();
293
294 int id = rs.nAllocationCreateTyped(t.mID);
Jason Samsea84a7c2009-09-04 14:42:41 -0700295 if(id == 0) {
296 throw new IllegalStateException("Bad element.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700297 }
Jason Sams768bc022009-09-21 19:41:04 -0700298 return new Allocation(id, rs, t);
Jason Samsb8c5a842009-07-31 20:40:47 -0700299 }
300
Jason Sams8a647432010-03-01 15:31:04 -0800301 static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
302 final Bitmap.Config bc = b.getConfig();
303 if (bc == Bitmap.Config.ALPHA_8) {
304 return Element.A_8(rs);
305 }
306 if (bc == Bitmap.Config.ARGB_4444) {
307 return Element.RGBA_4444(rs);
308 }
309 if (bc == Bitmap.Config.ARGB_8888) {
310 return Element.RGBA_8888(rs);
311 }
312 if (bc == Bitmap.Config.RGB_565) {
313 return Element.RGB_565(rs);
314 }
315 throw new IllegalStateException("Bad bitmap type.");
316 }
317
318 static private Type typeFromBitmap(RenderScript rs, Bitmap b) {
319 Element e = elementFromBitmap(rs, b);
320 Type.Builder tb = new Type.Builder(rs, e);
321 tb.add(Dimension.X, b.getWidth());
322 tb.add(Dimension.Y, b.getHeight());
323 return tb.create();
324 }
325
Jason Samsb8c5a842009-07-31 20:40:47 -0700326 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
327 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700328
Jason Sams771bebb2009-12-07 12:40:12 -0800329 rs.validate();
Jason Sams8a647432010-03-01 15:31:04 -0800330 Type t = typeFromBitmap(rs, b);
331
Jason Samsea84a7c2009-09-04 14:42:41 -0700332 int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800333 if(id == 0) {
334 throw new IllegalStateException("Load failed.");
335 }
Jason Sams8a647432010-03-01 15:31:04 -0800336 return new Allocation(id, rs, t);
337 }
338
339 static public Allocation createBitmapRef(RenderScript rs, Bitmap b)
340 throws IllegalArgumentException {
341
342 rs.validate();
343 Type t = typeFromBitmap(rs, b);
344
345 int id = rs.nAllocationCreateBitmapRef(t.getID(), b);
346 if(id == 0) {
347 throw new IllegalStateException("Load failed.");
348 }
349
350 Allocation a = new Allocation(id, rs, t);
351 a.mBitmap = b;
352 return a;
Jason Samsb8c5a842009-07-31 20:40:47 -0700353 }
354
Jason Sams74e02ef2010-01-06 15:10:29 -0800355 static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
Jason Samsb8c5a842009-07-31 20:40:47 -0700356 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700357
Jason Sams771bebb2009-12-07 12:40:12 -0800358 rs.validate();
Jason Samsea84a7c2009-09-04 14:42:41 -0700359 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800360 if(id == 0) {
361 throw new IllegalStateException("Load failed.");
362 }
Jason Sams43ee06852009-08-12 17:54:11 -0700363 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700364 }
365
366 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
367 throws IllegalArgumentException {
368
Jason Sams771bebb2009-12-07 12:40:12 -0800369 rs.validate();
Romain Guy650a3eb2009-08-31 14:06:43 -0700370 InputStream is = null;
371 try {
372 final TypedValue value = new TypedValue();
373 is = res.openRawResource(id, value);
374
375 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
Jason Samsea84a7c2009-09-04 14:42:41 -0700376 int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
Romain Guy650a3eb2009-08-31 14:06:43 -0700377 asset);
378
Jason Sams718cd1f2009-12-23 14:35:29 -0800379 if(allocationId == 0) {
380 throw new IllegalStateException("Load failed.");
381 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700382 return new Allocation(allocationId, rs, null);
Romain Guy650a3eb2009-08-31 14:06:43 -0700383 } catch (Exception e) {
384 // Ignore
385 } finally {
386 if (is != null) {
387 try {
388 is.close();
389 } catch (IOException e) {
390 // Ignore
391 }
392 }
393 }
394
395 return null;
Jason Samsb8c5a842009-07-31 20:40:47 -0700396 }
397
398 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
399 throws IllegalArgumentException {
400
401 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
402 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
403 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700404
405 static public Allocation createFromString(RenderScript rs, String str)
406 throws IllegalArgumentException {
407 byte[] allocArray = null;
408 try {
409 allocArray = str.getBytes("UTF-8");
410 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length);
411 alloc.data(allocArray);
412 return alloc;
413 }
414 catch (Exception e) {
415 Log.e("rs", "could not convert string to utf-8");
416 }
417 return null;
418 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700419}
420
421