blob: 6775c08019fa39a13c840d459954b4819bd96f0c [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 Samsa70f4162010-03-26 15:33:42 -070093 public void subData(int off, FieldPacker fp) {
94 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 }
102 data1DChecks(off, count, data.length, data.length);
103 mRS.nAllocationSubData1D(mID, off, count, data, data.length);
104 }
105
Jason Sams768bc022009-09-21 19:41:04 -0700106 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -0800107 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -0700108 if(off < 0) {
109 throw new IllegalArgumentException("Offset must be >= 0.");
110 }
111 if(count < 1) {
112 throw new IllegalArgumentException("Count must be >= 1.");
113 }
114 if((off + count) > mType.getElementCount()) {
115 throw new IllegalArgumentException("Overflow, Available count " + mType.getElementCount() +
116 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700117 }
Jason Sams768bc022009-09-21 19:41:04 -0700118 if((len) < dataSize) {
119 throw new IllegalArgumentException("Array too small for allocation type.");
120 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700121 }
122
123 public void subData1D(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700124 int dataSize = mType.mElement.getSizeBytes() * count;
125 data1DChecks(off, count, d.length * 4, dataSize);
126 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
127 }
128 public void subData1D(int off, int count, short[] d) {
129 int dataSize = mType.mElement.getSizeBytes() * count;
130 data1DChecks(off, count, d.length * 2, dataSize);
131 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
132 }
133 public void subData1D(int off, int count, byte[] d) {
134 int dataSize = mType.mElement.getSizeBytes() * count;
135 data1DChecks(off, count, d.length, dataSize);
136 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
137 }
138 public void subData1D(int off, int count, float[] d) {
139 int dataSize = mType.mElement.getSizeBytes() * count;
140 data1DChecks(off, count, d.length * 4, dataSize);
141 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700142 }
143
Jason Sams768bc022009-09-21 19:41:04 -0700144
Jason Samsb8c5a842009-07-31 20:40:47 -0700145
146 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800147 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700148 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700149 }
150
151 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800152 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700153 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700154 }
155
Jason Sams40a29e82009-08-10 14:55:26 -0700156 public void readData(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800157 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700158 mRS.nAllocationRead(mID, d);
159 }
160
161 public void readData(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800162 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700163 mRS.nAllocationRead(mID, d);
164 }
165
Jason Sams43ee06852009-08-12 17:54:11 -0700166 public void data(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800167 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700168 mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
Jason Sams43ee06852009-08-12 17:54:11 -0700169 }
170
Jason Sams5f43fd22009-09-15 12:39:22 -0700171 public void read(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800172 mRS.validate();
Jason Sams5f43fd22009-09-15 12:39:22 -0700173 mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
174 }
175
Jason Sams2525a812009-09-03 15:43:13 -0700176 public void subData(int offset, Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800177 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700178 mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
179 }
Jason Sams40a29e82009-08-10 14:55:26 -0700180
Jason Samsb8c5a842009-07-31 20:40:47 -0700181 public class Adapter1D extends BaseObj {
182 Adapter1D(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700183 super(id, rs);
Jason Samsb8c5a842009-07-31 20:40:47 -0700184 }
185
Jason Samsb8c5a842009-07-31 20:40:47 -0700186 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800187 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700188 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
189 }
190
191 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800192 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700193 mRS.nAdapter1DData(mID, d);
194 }
195
Jason Samsb8c5a842009-07-31 20:40:47 -0700196 public void data(float[] 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 Samsbd1c3ad2009-08-03 16:03:08 -0700201 public void subData(int off, int count, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800202 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700203 mRS.nAdapter1DSubData(mID, off, count, d);
204 }
205
Jason Samsb8c5a842009-07-31 20:40:47 -0700206 public void subData(int off, int count, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800207 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700208 mRS.nAdapter1DSubData(mID, off, count, d);
209 }
210 }
211
212 public Adapter1D createAdapter1D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800213 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700214 int id = mRS.nAdapter1DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800215 if(id == 0) {
216 throw new IllegalStateException("allocation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700217 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800218 mRS.nAdapter1DBindAllocation(id, mID);
Jason Samsb8c5a842009-07-31 20:40:47 -0700219 return new Adapter1D(id, mRS);
220 }
221
222
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700223 public class Adapter2D extends BaseObj {
224 Adapter2D(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700225 super(id, rs);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700226 }
227
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700228 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800229 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700230 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
231 }
232
233 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800234 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700235 mRS.nAdapter2DData(mID, d);
236 }
237
238 public void data(float[] 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 subData(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800244 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700245 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
246 }
247
248 public void subData(int xoff, int yoff, int w, int h, float[] 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
254 public Adapter2D createAdapter2D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800255 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700256 int id = mRS.nAdapter2DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800257 if(id == 0) {
258 throw new IllegalStateException("allocation failed.");
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700259 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800260 mRS.nAdapter2DBindAllocation(id, mID);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700261 return new Adapter2D(id, mRS);
262 }
263
Jason Samsb8c5a842009-07-31 20:40:47 -0700264
265 // creation
266
267 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
268 static {
269 mBitmapOptions.inScaled = false;
270 }
271
Jason Sams1bada8c2009-08-09 17:01:55 -0700272 static public Allocation createTyped(RenderScript rs, Type type)
273 throws IllegalArgumentException {
274
Jason Sams771bebb2009-12-07 12:40:12 -0800275 rs.validate();
Jason Sams1bada8c2009-08-09 17:01:55 -0700276 if(type.mID == 0) {
277 throw new IllegalStateException("Bad Type");
278 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700279 int id = rs.nAllocationCreateTyped(type.mID);
Jason Sams43ee06852009-08-12 17:54:11 -0700280 return new Allocation(id, rs, type);
Jason Samsb8c5a842009-07-31 20:40:47 -0700281 }
282
Jason Sams1bada8c2009-08-09 17:01:55 -0700283 static public Allocation createSized(RenderScript rs, Element e, int count)
284 throws IllegalArgumentException {
285
Jason Sams771bebb2009-12-07 12:40:12 -0800286 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700287 Type.Builder b = new Type.Builder(rs, e);
288 b.add(Dimension.X, count);
289 Type t = b.create();
290
291 int id = rs.nAllocationCreateTyped(t.mID);
Jason Samsea84a7c2009-09-04 14:42:41 -0700292 if(id == 0) {
293 throw new IllegalStateException("Bad element.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700294 }
Jason Sams768bc022009-09-21 19:41:04 -0700295 return new Allocation(id, rs, t);
Jason Samsb8c5a842009-07-31 20:40:47 -0700296 }
297
Jason Sams8a647432010-03-01 15:31:04 -0800298 static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
299 final Bitmap.Config bc = b.getConfig();
300 if (bc == Bitmap.Config.ALPHA_8) {
301 return Element.A_8(rs);
302 }
303 if (bc == Bitmap.Config.ARGB_4444) {
304 return Element.RGBA_4444(rs);
305 }
306 if (bc == Bitmap.Config.ARGB_8888) {
307 return Element.RGBA_8888(rs);
308 }
309 if (bc == Bitmap.Config.RGB_565) {
310 return Element.RGB_565(rs);
311 }
312 throw new IllegalStateException("Bad bitmap type.");
313 }
314
315 static private Type typeFromBitmap(RenderScript rs, Bitmap b) {
316 Element e = elementFromBitmap(rs, b);
317 Type.Builder tb = new Type.Builder(rs, e);
318 tb.add(Dimension.X, b.getWidth());
319 tb.add(Dimension.Y, b.getHeight());
320 return tb.create();
321 }
322
Jason Samsb8c5a842009-07-31 20:40:47 -0700323 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
324 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700325
Jason Sams771bebb2009-12-07 12:40:12 -0800326 rs.validate();
Jason Sams8a647432010-03-01 15:31:04 -0800327 Type t = typeFromBitmap(rs, b);
328
Jason Samsea84a7c2009-09-04 14:42:41 -0700329 int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800330 if(id == 0) {
331 throw new IllegalStateException("Load failed.");
332 }
Jason Sams8a647432010-03-01 15:31:04 -0800333 return new Allocation(id, rs, t);
334 }
335
336 static public Allocation createBitmapRef(RenderScript rs, Bitmap b)
337 throws IllegalArgumentException {
338
339 rs.validate();
340 Type t = typeFromBitmap(rs, b);
341
342 int id = rs.nAllocationCreateBitmapRef(t.getID(), b);
343 if(id == 0) {
344 throw new IllegalStateException("Load failed.");
345 }
346
347 Allocation a = new Allocation(id, rs, t);
348 a.mBitmap = b;
349 return a;
Jason Samsb8c5a842009-07-31 20:40:47 -0700350 }
351
Jason Sams74e02ef2010-01-06 15:10:29 -0800352 static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
Jason Samsb8c5a842009-07-31 20:40:47 -0700353 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700354
Jason Sams771bebb2009-12-07 12:40:12 -0800355 rs.validate();
Jason Samsea84a7c2009-09-04 14:42:41 -0700356 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800357 if(id == 0) {
358 throw new IllegalStateException("Load failed.");
359 }
Jason Sams43ee06852009-08-12 17:54:11 -0700360 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700361 }
362
363 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
364 throws IllegalArgumentException {
365
Jason Sams771bebb2009-12-07 12:40:12 -0800366 rs.validate();
Romain Guy650a3eb2009-08-31 14:06:43 -0700367 InputStream is = null;
368 try {
369 final TypedValue value = new TypedValue();
370 is = res.openRawResource(id, value);
371
372 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
Jason Samsea84a7c2009-09-04 14:42:41 -0700373 int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
Romain Guy650a3eb2009-08-31 14:06:43 -0700374 asset);
375
Jason Sams718cd1f2009-12-23 14:35:29 -0800376 if(allocationId == 0) {
377 throw new IllegalStateException("Load failed.");
378 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700379 return new Allocation(allocationId, rs, null);
Romain Guy650a3eb2009-08-31 14:06:43 -0700380 } catch (Exception e) {
381 // Ignore
382 } finally {
383 if (is != null) {
384 try {
385 is.close();
386 } catch (IOException e) {
387 // Ignore
388 }
389 }
390 }
391
392 return null;
Jason Samsb8c5a842009-07-31 20:40:47 -0700393 }
394
395 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
396 throws IllegalArgumentException {
397
Romain Guyf92a0a62010-08-20 15:43:52 -0700398 mBitmapOptions.inPreferredConfig = null;
399 if (dstFmt == rs.mElement_RGBA_8888) {
400 mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
401 } else if (dstFmt == rs.mElement_RGB_888) {
402 mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
403 } else if (dstFmt == rs.mElement_RGBA_4444) {
404 mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_4444;
405 } else if (dstFmt == rs.mElement_RGB_565) {
406 mBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
407 }
408
Jason Samsb8c5a842009-07-31 20:40:47 -0700409 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
410 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
411 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700412
413 static public Allocation createFromString(RenderScript rs, String str)
414 throws IllegalArgumentException {
415 byte[] allocArray = null;
416 try {
417 allocArray = str.getBytes("UTF-8");
418 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length);
419 alloc.data(allocArray);
420 return alloc;
421 }
422 catch (Exception e) {
423 Log.e("rs", "could not convert string to utf-8");
424 }
425 return null;
426 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700427}
428
429