blob: 46f3eae907f6478bd9523ba8e2c7957d3c57030a [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();
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -070051 mName = mRS.nGetName(mID);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070052 int typeID = mRS.nAllocationGetType(mID);
53 if(typeID != 0) {
54 mType = new Type(typeID, mRS);
55 mType.updateFromNative();
56 }
57 }
58
Jason Samsea87e962010-01-12 12:12:28 -080059 public Type getType() {
60 return mType;
61 }
62
Jason Samsb8c5a842009-07-31 20:40:47 -070063 public void uploadToTexture(int baseMipLevel) {
Jason Sams771bebb2009-12-07 12:40:12 -080064 mRS.validate();
Jason Samsc2908e62010-02-23 17:44:28 -080065 mRS.nAllocationUploadToTexture(mID, false, baseMipLevel);
66 }
67
68 public void uploadToTexture(boolean genMips, int baseMipLevel) {
69 mRS.validate();
70 mRS.nAllocationUploadToTexture(mID, genMips, baseMipLevel);
Jason Samsb8c5a842009-07-31 20:40:47 -070071 }
72
Jason Sams07ae4062009-08-27 20:23:34 -070073 public void uploadToBufferObject() {
Jason Sams771bebb2009-12-07 12:40:12 -080074 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -070075 mRS.nAllocationUploadToBufferObject(mID);
76 }
77
Jason Samsb8c5a842009-07-31 20:40:47 -070078 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080079 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070080 subData1D(0, mType.getElementCount(), d);
81 }
82 public void data(short[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080083 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070084 subData1D(0, mType.getElementCount(), d);
85 }
86 public void data(byte[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080087 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070088 subData1D(0, mType.getElementCount(), d);
89 }
90 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080091 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070092 subData1D(0, mType.getElementCount(), d);
Jason Samsb8c5a842009-07-31 20:40:47 -070093 }
94
Jason Samsa70f4162010-03-26 15:33:42 -070095 public void subData(int off, FieldPacker fp) {
96 int eSize = mType.mElement.getSizeBytes();
97 final byte[] data = fp.getData();
98
99 int count = data.length / eSize;
100 if ((eSize * count) != data.length) {
101 throw new IllegalArgumentException("Field packer length " + data.length +
102 " not divisible by element size " + eSize + ".");
103 }
104 data1DChecks(off, count, data.length, data.length);
105 mRS.nAllocationSubData1D(mID, off, count, data, data.length);
106 }
107
Jason Sams768bc022009-09-21 19:41:04 -0700108 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800109 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700110 if(off < 0) {
111 throw new IllegalArgumentException("Offset must be >= 0.");
112 }
113 if(count < 1) {
114 throw new IllegalArgumentException("Count must be >= 1.");
115 }
116 if((off + count) > mType.getElementCount()) {
117 throw new IllegalArgumentException("Overflow, Available count " + mType.getElementCount() +
118 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700119 }
Jason Sams768bc022009-09-21 19:41:04 -0700120 if((len) < dataSize) {
121 throw new IllegalArgumentException("Array too small for allocation type.");
122 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700123 }
124
125 public void subData1D(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700126 int dataSize = mType.mElement.getSizeBytes() * count;
127 data1DChecks(off, count, d.length * 4, dataSize);
128 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
129 }
130 public void subData1D(int off, int count, short[] d) {
131 int dataSize = mType.mElement.getSizeBytes() * count;
132 data1DChecks(off, count, d.length * 2, dataSize);
133 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
134 }
135 public void subData1D(int off, int count, byte[] d) {
136 int dataSize = mType.mElement.getSizeBytes() * count;
137 data1DChecks(off, count, d.length, dataSize);
138 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
139 }
140 public void subData1D(int off, int count, float[] d) {
141 int dataSize = mType.mElement.getSizeBytes() * count;
142 data1DChecks(off, count, d.length * 4, dataSize);
143 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700144 }
145
Jason Sams768bc022009-09-21 19:41:04 -0700146
Jason Samsb8c5a842009-07-31 20:40:47 -0700147
148 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800149 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700150 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700151 }
152
153 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800154 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700155 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700156 }
157
Jason Sams40a29e82009-08-10 14:55:26 -0700158 public void readData(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800159 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700160 mRS.nAllocationRead(mID, d);
161 }
162
163 public void readData(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800164 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700165 mRS.nAllocationRead(mID, d);
166 }
167
Jason Sams43ee06852009-08-12 17:54:11 -0700168 public void data(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800169 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700170 mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
Jason Sams43ee06852009-08-12 17:54:11 -0700171 }
172
Jason Sams5f43fd22009-09-15 12:39:22 -0700173 public void read(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800174 mRS.validate();
Jason Sams5f43fd22009-09-15 12:39:22 -0700175 mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
176 }
177
Jason Sams2525a812009-09-03 15:43:13 -0700178 public void subData(int offset, Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800179 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700180 mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
181 }
Jason Sams40a29e82009-08-10 14:55:26 -0700182
Jason Samsb8c5a842009-07-31 20:40:47 -0700183 public class Adapter1D extends BaseObj {
184 Adapter1D(int id, RenderScript rs) {
185 super(rs);
186 mID = id;
187 }
188
Jason Samsb8c5a842009-07-31 20:40:47 -0700189 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800190 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700191 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
192 }
193
194 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800195 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700196 mRS.nAdapter1DData(mID, d);
197 }
198
Jason Samsb8c5a842009-07-31 20:40:47 -0700199 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800200 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700201 mRS.nAdapter1DData(mID, d);
202 }
203
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700204 public void subData(int off, int count, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800205 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700206 mRS.nAdapter1DSubData(mID, off, count, d);
207 }
208
Jason Samsb8c5a842009-07-31 20:40:47 -0700209 public void subData(int off, int count, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800210 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700211 mRS.nAdapter1DSubData(mID, off, count, d);
212 }
213 }
214
215 public Adapter1D createAdapter1D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800216 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700217 int id = mRS.nAdapter1DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800218 if(id == 0) {
219 throw new IllegalStateException("allocation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700220 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800221 mRS.nAdapter1DBindAllocation(id, mID);
Jason Samsb8c5a842009-07-31 20:40:47 -0700222 return new Adapter1D(id, mRS);
223 }
224
225
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700226 public class Adapter2D extends BaseObj {
227 Adapter2D(int id, RenderScript rs) {
228 super(rs);
229 mID = id;
230 }
231
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700232 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800233 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700234 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
235 }
236
237 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800238 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700239 mRS.nAdapter2DData(mID, d);
240 }
241
242 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800243 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700244 mRS.nAdapter2DData(mID, d);
245 }
246
247 public void subData(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800248 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700249 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
250 }
251
252 public void subData(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800253 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700254 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
255 }
256 }
257
258 public Adapter2D createAdapter2D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800259 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700260 int id = mRS.nAdapter2DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800261 if(id == 0) {
262 throw new IllegalStateException("allocation failed.");
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700263 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800264 mRS.nAdapter2DBindAllocation(id, mID);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700265 return new Adapter2D(id, mRS);
266 }
267
Jason Samsb8c5a842009-07-31 20:40:47 -0700268
269 // creation
270
271 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
272 static {
273 mBitmapOptions.inScaled = false;
274 }
275
Jason Sams1bada8c2009-08-09 17:01:55 -0700276 static public Allocation createTyped(RenderScript rs, Type type)
277 throws IllegalArgumentException {
278
Jason Sams771bebb2009-12-07 12:40:12 -0800279 rs.validate();
Jason Sams1bada8c2009-08-09 17:01:55 -0700280 if(type.mID == 0) {
281 throw new IllegalStateException("Bad Type");
282 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700283 int id = rs.nAllocationCreateTyped(type.mID);
Jason Sams43ee06852009-08-12 17:54:11 -0700284 return new Allocation(id, rs, type);
Jason Samsb8c5a842009-07-31 20:40:47 -0700285 }
286
Jason Sams1bada8c2009-08-09 17:01:55 -0700287 static public Allocation createSized(RenderScript rs, Element e, int count)
288 throws IllegalArgumentException {
289
Jason Sams771bebb2009-12-07 12:40:12 -0800290 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700291 Type.Builder b = new Type.Builder(rs, e);
292 b.add(Dimension.X, count);
293 Type t = b.create();
294
295 int id = rs.nAllocationCreateTyped(t.mID);
Jason Samsea84a7c2009-09-04 14:42:41 -0700296 if(id == 0) {
297 throw new IllegalStateException("Bad element.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700298 }
Jason Sams768bc022009-09-21 19:41:04 -0700299 return new Allocation(id, rs, t);
Jason Samsb8c5a842009-07-31 20:40:47 -0700300 }
301
Jason Sams8a647432010-03-01 15:31:04 -0800302 static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
303 final Bitmap.Config bc = b.getConfig();
304 if (bc == Bitmap.Config.ALPHA_8) {
305 return Element.A_8(rs);
306 }
307 if (bc == Bitmap.Config.ARGB_4444) {
308 return Element.RGBA_4444(rs);
309 }
310 if (bc == Bitmap.Config.ARGB_8888) {
311 return Element.RGBA_8888(rs);
312 }
313 if (bc == Bitmap.Config.RGB_565) {
314 return Element.RGB_565(rs);
315 }
316 throw new IllegalStateException("Bad bitmap type.");
317 }
318
319 static private Type typeFromBitmap(RenderScript rs, Bitmap b) {
320 Element e = elementFromBitmap(rs, b);
321 Type.Builder tb = new Type.Builder(rs, e);
322 tb.add(Dimension.X, b.getWidth());
323 tb.add(Dimension.Y, b.getHeight());
324 return tb.create();
325 }
326
Jason Samsb8c5a842009-07-31 20:40:47 -0700327 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
328 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700329
Jason Sams771bebb2009-12-07 12:40:12 -0800330 rs.validate();
Jason Sams8a647432010-03-01 15:31:04 -0800331 Type t = typeFromBitmap(rs, b);
332
Jason Samsea84a7c2009-09-04 14:42:41 -0700333 int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800334 if(id == 0) {
335 throw new IllegalStateException("Load failed.");
336 }
Jason Sams8a647432010-03-01 15:31:04 -0800337 return new Allocation(id, rs, t);
338 }
339
340 static public Allocation createBitmapRef(RenderScript rs, Bitmap b)
341 throws IllegalArgumentException {
342
343 rs.validate();
344 Type t = typeFromBitmap(rs, b);
345
346 int id = rs.nAllocationCreateBitmapRef(t.getID(), b);
347 if(id == 0) {
348 throw new IllegalStateException("Load failed.");
349 }
350
351 Allocation a = new Allocation(id, rs, t);
352 a.mBitmap = b;
353 return a;
Jason Samsb8c5a842009-07-31 20:40:47 -0700354 }
355
Jason Sams74e02ef2010-01-06 15:10:29 -0800356 static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
Jason Samsb8c5a842009-07-31 20:40:47 -0700357 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700358
Jason Sams771bebb2009-12-07 12:40:12 -0800359 rs.validate();
Jason Samsea84a7c2009-09-04 14:42:41 -0700360 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800361 if(id == 0) {
362 throw new IllegalStateException("Load failed.");
363 }
Jason Sams43ee06852009-08-12 17:54:11 -0700364 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700365 }
366
367 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
368 throws IllegalArgumentException {
369
Jason Sams771bebb2009-12-07 12:40:12 -0800370 rs.validate();
Romain Guy650a3eb2009-08-31 14:06:43 -0700371 InputStream is = null;
372 try {
373 final TypedValue value = new TypedValue();
374 is = res.openRawResource(id, value);
375
376 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
Jason Samsea84a7c2009-09-04 14:42:41 -0700377 int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
Romain Guy650a3eb2009-08-31 14:06:43 -0700378 asset);
379
Jason Sams718cd1f2009-12-23 14:35:29 -0800380 if(allocationId == 0) {
381 throw new IllegalStateException("Load failed.");
382 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700383 return new Allocation(allocationId, rs, null);
Romain Guy650a3eb2009-08-31 14:06:43 -0700384 } catch (Exception e) {
385 // Ignore
386 } finally {
387 if (is != null) {
388 try {
389 is.close();
390 } catch (IOException e) {
391 // Ignore
392 }
393 }
394 }
395
396 return null;
Jason Samsb8c5a842009-07-31 20:40:47 -0700397 }
398
399 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
400 throws IllegalArgumentException {
401
402 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
403 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
404 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700405
406 static public Allocation createFromString(RenderScript rs, String str)
407 throws IllegalArgumentException {
408 byte[] allocArray = null;
409 try {
410 allocArray = str.getBytes("UTF-8");
411 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length);
412 alloc.data(allocArray);
413 return alloc;
414 }
415 catch (Exception e) {
416 Log.e("rs", "could not convert string to utf-8");
417 }
418 return null;
419 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700420}
421
422