blob: bfa61f35cdbe744a91ecaf5d868365ea670c44dc [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
Jason Samsea87e962010-01-12 12:12:28 -080048 public Type getType() {
49 return mType;
50 }
51
Jason Samsb8c5a842009-07-31 20:40:47 -070052 public void uploadToTexture(int baseMipLevel) {
Jason Sams771bebb2009-12-07 12:40:12 -080053 mRS.validate();
Jason Samsc2908e62010-02-23 17:44:28 -080054 mRS.nAllocationUploadToTexture(mID, false, baseMipLevel);
55 }
56
57 public void uploadToTexture(boolean genMips, int baseMipLevel) {
58 mRS.validate();
59 mRS.nAllocationUploadToTexture(mID, genMips, baseMipLevel);
Jason Samsb8c5a842009-07-31 20:40:47 -070060 }
61
Jason Sams07ae4062009-08-27 20:23:34 -070062 public void uploadToBufferObject() {
Jason Sams771bebb2009-12-07 12:40:12 -080063 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -070064 mRS.nAllocationUploadToBufferObject(mID);
65 }
66
Jason Samsb8c5a842009-07-31 20:40:47 -070067 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080068 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070069 subData1D(0, mType.getElementCount(), d);
70 }
71 public void data(short[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080072 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070073 subData1D(0, mType.getElementCount(), d);
74 }
75 public void data(byte[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080076 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070077 subData1D(0, mType.getElementCount(), d);
78 }
79 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080080 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070081 subData1D(0, mType.getElementCount(), d);
Jason Samsb8c5a842009-07-31 20:40:47 -070082 }
83
Jason Samsa70f4162010-03-26 15:33:42 -070084 public void subData(int off, FieldPacker fp) {
85 int eSize = mType.mElement.getSizeBytes();
86 final byte[] data = fp.getData();
87
88 int count = data.length / eSize;
89 if ((eSize * count) != data.length) {
90 throw new IllegalArgumentException("Field packer length " + data.length +
91 " not divisible by element size " + eSize + ".");
92 }
93 data1DChecks(off, count, data.length, data.length);
94 mRS.nAllocationSubData1D(mID, off, count, data, data.length);
95 }
96
Jason Sams768bc022009-09-21 19:41:04 -070097 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -080098 mRS.validate();
Jason Samsa70f4162010-03-26 15:33:42 -070099 if(off < 0) {
100 throw new IllegalArgumentException("Offset must be >= 0.");
101 }
102 if(count < 1) {
103 throw new IllegalArgumentException("Count must be >= 1.");
104 }
105 if((off + count) > mType.getElementCount()) {
106 throw new IllegalArgumentException("Overflow, Available count " + mType.getElementCount() +
107 ", got " + count + " at offset " + off + ".");
Jason Sams07ae4062009-08-27 20:23:34 -0700108 }
Jason Sams768bc022009-09-21 19:41:04 -0700109 if((len) < dataSize) {
110 throw new IllegalArgumentException("Array too small for allocation type.");
111 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700112 }
113
114 public void subData1D(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -0700115 int dataSize = mType.mElement.getSizeBytes() * count;
116 data1DChecks(off, count, d.length * 4, dataSize);
117 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
118 }
119 public void subData1D(int off, int count, short[] d) {
120 int dataSize = mType.mElement.getSizeBytes() * count;
121 data1DChecks(off, count, d.length * 2, dataSize);
122 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
123 }
124 public void subData1D(int off, int count, byte[] d) {
125 int dataSize = mType.mElement.getSizeBytes() * count;
126 data1DChecks(off, count, d.length, dataSize);
127 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
128 }
129 public void subData1D(int off, int count, float[] d) {
130 int dataSize = mType.mElement.getSizeBytes() * count;
131 data1DChecks(off, count, d.length * 4, dataSize);
132 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700133 }
134
Jason Sams768bc022009-09-21 19:41:04 -0700135
Jason Samsb8c5a842009-07-31 20:40:47 -0700136
137 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800138 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700139 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700140 }
141
142 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800143 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700144 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700145 }
146
Jason Sams40a29e82009-08-10 14:55:26 -0700147 public void readData(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800148 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700149 mRS.nAllocationRead(mID, d);
150 }
151
152 public void readData(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800153 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700154 mRS.nAllocationRead(mID, d);
155 }
156
Jason Sams43ee06852009-08-12 17:54:11 -0700157 public void data(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800158 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700159 mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
Jason Sams43ee06852009-08-12 17:54:11 -0700160 }
161
Jason Sams5f43fd22009-09-15 12:39:22 -0700162 public void read(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800163 mRS.validate();
Jason Sams5f43fd22009-09-15 12:39:22 -0700164 mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
165 }
166
Jason Sams2525a812009-09-03 15:43:13 -0700167 public void subData(int offset, Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800168 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700169 mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
170 }
Jason Sams40a29e82009-08-10 14:55:26 -0700171
Jason Samsb8c5a842009-07-31 20:40:47 -0700172 public class Adapter1D extends BaseObj {
173 Adapter1D(int id, RenderScript rs) {
174 super(rs);
175 mID = id;
176 }
177
Jason Samsb8c5a842009-07-31 20:40:47 -0700178 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800179 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700180 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
181 }
182
183 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800184 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700185 mRS.nAdapter1DData(mID, d);
186 }
187
Jason Samsb8c5a842009-07-31 20:40:47 -0700188 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800189 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700190 mRS.nAdapter1DData(mID, d);
191 }
192
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700193 public void subData(int off, int count, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800194 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700195 mRS.nAdapter1DSubData(mID, off, count, d);
196 }
197
Jason Samsb8c5a842009-07-31 20:40:47 -0700198 public void subData(int off, int count, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800199 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700200 mRS.nAdapter1DSubData(mID, off, count, d);
201 }
202 }
203
204 public Adapter1D createAdapter1D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800205 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700206 int id = mRS.nAdapter1DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800207 if(id == 0) {
208 throw new IllegalStateException("allocation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700209 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800210 mRS.nAdapter1DBindAllocation(id, mID);
Jason Samsb8c5a842009-07-31 20:40:47 -0700211 return new Adapter1D(id, mRS);
212 }
213
214
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700215 public class Adapter2D extends BaseObj {
216 Adapter2D(int id, RenderScript rs) {
217 super(rs);
218 mID = id;
219 }
220
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700221 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800222 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700223 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
224 }
225
226 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800227 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700228 mRS.nAdapter2DData(mID, d);
229 }
230
231 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800232 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700233 mRS.nAdapter2DData(mID, d);
234 }
235
236 public void subData(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800237 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700238 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
239 }
240
241 public void subData(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800242 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700243 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
244 }
245 }
246
247 public Adapter2D createAdapter2D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800248 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700249 int id = mRS.nAdapter2DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800250 if(id == 0) {
251 throw new IllegalStateException("allocation failed.");
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700252 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800253 mRS.nAdapter2DBindAllocation(id, mID);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700254 return new Adapter2D(id, mRS);
255 }
256
Jason Samsb8c5a842009-07-31 20:40:47 -0700257
258 // creation
259
260 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
261 static {
262 mBitmapOptions.inScaled = false;
263 }
264
Jason Sams1bada8c2009-08-09 17:01:55 -0700265 static public Allocation createTyped(RenderScript rs, Type type)
266 throws IllegalArgumentException {
267
Jason Sams771bebb2009-12-07 12:40:12 -0800268 rs.validate();
Jason Sams1bada8c2009-08-09 17:01:55 -0700269 if(type.mID == 0) {
270 throw new IllegalStateException("Bad Type");
271 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700272 int id = rs.nAllocationCreateTyped(type.mID);
Jason Sams43ee06852009-08-12 17:54:11 -0700273 return new Allocation(id, rs, type);
Jason Samsb8c5a842009-07-31 20:40:47 -0700274 }
275
Jason Sams1bada8c2009-08-09 17:01:55 -0700276 static public Allocation createSized(RenderScript rs, Element e, int count)
277 throws IllegalArgumentException {
278
Jason Sams771bebb2009-12-07 12:40:12 -0800279 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700280 Type.Builder b = new Type.Builder(rs, e);
281 b.add(Dimension.X, count);
282 Type t = b.create();
283
284 int id = rs.nAllocationCreateTyped(t.mID);
Jason Samsea84a7c2009-09-04 14:42:41 -0700285 if(id == 0) {
286 throw new IllegalStateException("Bad element.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700287 }
Jason Sams768bc022009-09-21 19:41:04 -0700288 return new Allocation(id, rs, t);
Jason Samsb8c5a842009-07-31 20:40:47 -0700289 }
290
Jason Sams8a647432010-03-01 15:31:04 -0800291 static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
292 final Bitmap.Config bc = b.getConfig();
293 if (bc == Bitmap.Config.ALPHA_8) {
294 return Element.A_8(rs);
295 }
296 if (bc == Bitmap.Config.ARGB_4444) {
297 return Element.RGBA_4444(rs);
298 }
299 if (bc == Bitmap.Config.ARGB_8888) {
300 return Element.RGBA_8888(rs);
301 }
302 if (bc == Bitmap.Config.RGB_565) {
303 return Element.RGB_565(rs);
304 }
305 throw new IllegalStateException("Bad bitmap type.");
306 }
307
308 static private Type typeFromBitmap(RenderScript rs, Bitmap b) {
309 Element e = elementFromBitmap(rs, b);
310 Type.Builder tb = new Type.Builder(rs, e);
311 tb.add(Dimension.X, b.getWidth());
312 tb.add(Dimension.Y, b.getHeight());
313 return tb.create();
314 }
315
Jason Samsb8c5a842009-07-31 20:40:47 -0700316 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
317 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700318
Jason Sams771bebb2009-12-07 12:40:12 -0800319 rs.validate();
Jason Sams8a647432010-03-01 15:31:04 -0800320 Type t = typeFromBitmap(rs, b);
321
Jason Samsea84a7c2009-09-04 14:42:41 -0700322 int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800323 if(id == 0) {
324 throw new IllegalStateException("Load failed.");
325 }
Jason Sams8a647432010-03-01 15:31:04 -0800326 return new Allocation(id, rs, t);
327 }
328
329 static public Allocation createBitmapRef(RenderScript rs, Bitmap b)
330 throws IllegalArgumentException {
331
332 rs.validate();
333 Type t = typeFromBitmap(rs, b);
334
335 int id = rs.nAllocationCreateBitmapRef(t.getID(), b);
336 if(id == 0) {
337 throw new IllegalStateException("Load failed.");
338 }
339
340 Allocation a = new Allocation(id, rs, t);
341 a.mBitmap = b;
342 return a;
Jason Samsb8c5a842009-07-31 20:40:47 -0700343 }
344
Jason Sams74e02ef2010-01-06 15:10:29 -0800345 static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
Jason Samsb8c5a842009-07-31 20:40:47 -0700346 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700347
Jason Sams771bebb2009-12-07 12:40:12 -0800348 rs.validate();
Jason Samsea84a7c2009-09-04 14:42:41 -0700349 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800350 if(id == 0) {
351 throw new IllegalStateException("Load failed.");
352 }
Jason Sams43ee06852009-08-12 17:54:11 -0700353 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700354 }
355
356 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
357 throws IllegalArgumentException {
358
Jason Sams771bebb2009-12-07 12:40:12 -0800359 rs.validate();
Romain Guy650a3eb2009-08-31 14:06:43 -0700360 InputStream is = null;
361 try {
362 final TypedValue value = new TypedValue();
363 is = res.openRawResource(id, value);
364
365 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
Jason Samsea84a7c2009-09-04 14:42:41 -0700366 int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
Romain Guy650a3eb2009-08-31 14:06:43 -0700367 asset);
368
Jason Sams718cd1f2009-12-23 14:35:29 -0800369 if(allocationId == 0) {
370 throw new IllegalStateException("Load failed.");
371 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700372 return new Allocation(allocationId, rs, null);
Romain Guy650a3eb2009-08-31 14:06:43 -0700373 } catch (Exception e) {
374 // Ignore
375 } finally {
376 if (is != null) {
377 try {
378 is.close();
379 } catch (IOException e) {
380 // Ignore
381 }
382 }
383 }
384
385 return null;
Jason Samsb8c5a842009-07-31 20:40:47 -0700386 }
387
388 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
389 throws IllegalArgumentException {
390
391 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
392 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
393 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700394
395 static public Allocation createFromString(RenderScript rs, String str)
396 throws IllegalArgumentException {
397 byte[] allocArray = null;
398 try {
399 allocArray = str.getBytes("UTF-8");
400 Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length);
401 alloc.data(allocArray);
402 return alloc;
403 }
404 catch (Exception e) {
405 Log.e("rs", "could not convert string to utf-8");
406 }
407 return null;
408 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700409}
410
411