blob: b27c7f502e96c324238ab021e3d6bdf6627551a1 [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
Jason Samsea87e962010-01-12 12:12:28 -080043 public Type getType() {
44 return mType;
45 }
46
Jason Samsb8c5a842009-07-31 20:40:47 -070047 public void uploadToTexture(int baseMipLevel) {
Jason Sams771bebb2009-12-07 12:40:12 -080048 mRS.validate();
Jason Samsc2908e62010-02-23 17:44:28 -080049 mRS.nAllocationUploadToTexture(mID, false, baseMipLevel);
50 }
51
52 public void uploadToTexture(boolean genMips, int baseMipLevel) {
53 mRS.validate();
54 mRS.nAllocationUploadToTexture(mID, genMips, baseMipLevel);
Jason Samsb8c5a842009-07-31 20:40:47 -070055 }
56
Jason Sams07ae4062009-08-27 20:23:34 -070057 public void uploadToBufferObject() {
Jason Sams771bebb2009-12-07 12:40:12 -080058 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -070059 mRS.nAllocationUploadToBufferObject(mID);
60 }
61
Jason Samsb8c5a842009-07-31 20:40:47 -070062 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080063 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070064 subData1D(0, mType.getElementCount(), d);
65 }
66 public void data(short[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080067 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070068 subData1D(0, mType.getElementCount(), d);
69 }
70 public void data(byte[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080071 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070072 subData1D(0, mType.getElementCount(), d);
73 }
74 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080075 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070076 subData1D(0, mType.getElementCount(), d);
Jason Samsb8c5a842009-07-31 20:40:47 -070077 }
78
Jason Sams768bc022009-09-21 19:41:04 -070079 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -080080 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070081 if((off < 0) || (count < 1) || ((off + count) > mType.getElementCount())) {
82 throw new IllegalArgumentException("Offset or Count out of bounds.");
Jason Sams07ae4062009-08-27 20:23:34 -070083 }
Jason Sams768bc022009-09-21 19:41:04 -070084 if((len) < dataSize) {
85 throw new IllegalArgumentException("Array too small for allocation type.");
86 }
Jason Samsb8c5a842009-07-31 20:40:47 -070087 }
88
89 public void subData1D(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -070090 int dataSize = mType.mElement.getSizeBytes() * count;
91 data1DChecks(off, count, d.length * 4, dataSize);
92 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
93 }
94 public void subData1D(int off, int count, short[] d) {
95 int dataSize = mType.mElement.getSizeBytes() * count;
96 data1DChecks(off, count, d.length * 2, dataSize);
97 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
98 }
99 public void subData1D(int off, int count, byte[] d) {
100 int dataSize = mType.mElement.getSizeBytes() * count;
101 data1DChecks(off, count, d.length, dataSize);
102 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
103 }
104 public void subData1D(int off, int count, float[] d) {
105 int dataSize = mType.mElement.getSizeBytes() * count;
106 data1DChecks(off, count, d.length * 4, dataSize);
107 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700108 }
109
Jason Sams768bc022009-09-21 19:41:04 -0700110
Jason Samsb8c5a842009-07-31 20:40:47 -0700111
112 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800113 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700114 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700115 }
116
117 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800118 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700119 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700120 }
121
Jason Sams40a29e82009-08-10 14:55:26 -0700122 public void readData(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800123 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700124 mRS.nAllocationRead(mID, d);
125 }
126
127 public void readData(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800128 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700129 mRS.nAllocationRead(mID, d);
130 }
131
Jason Sams43ee06852009-08-12 17:54:11 -0700132 public void data(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800133 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700134 mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
Jason Sams43ee06852009-08-12 17:54:11 -0700135 }
136
Jason Sams5f43fd22009-09-15 12:39:22 -0700137 public void read(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800138 mRS.validate();
Jason Sams5f43fd22009-09-15 12:39:22 -0700139 mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
140 }
141
Jason Sams2525a812009-09-03 15:43:13 -0700142 public void subData(int offset, Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800143 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700144 mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
145 }
Jason Sams40a29e82009-08-10 14:55:26 -0700146
Jason Samsb8c5a842009-07-31 20:40:47 -0700147 public class Adapter1D extends BaseObj {
148 Adapter1D(int id, RenderScript rs) {
149 super(rs);
150 mID = id;
151 }
152
Jason Samsb8c5a842009-07-31 20:40:47 -0700153 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800154 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700155 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
156 }
157
158 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800159 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700160 mRS.nAdapter1DData(mID, d);
161 }
162
Jason Samsb8c5a842009-07-31 20:40:47 -0700163 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800164 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700165 mRS.nAdapter1DData(mID, d);
166 }
167
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700168 public void subData(int off, int count, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800169 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700170 mRS.nAdapter1DSubData(mID, off, count, d);
171 }
172
Jason Samsb8c5a842009-07-31 20:40:47 -0700173 public void subData(int off, int count, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800174 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700175 mRS.nAdapter1DSubData(mID, off, count, d);
176 }
177 }
178
179 public Adapter1D createAdapter1D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800180 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700181 int id = mRS.nAdapter1DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800182 if(id == 0) {
183 throw new IllegalStateException("allocation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700184 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800185 mRS.nAdapter1DBindAllocation(id, mID);
Jason Samsb8c5a842009-07-31 20:40:47 -0700186 return new Adapter1D(id, mRS);
187 }
188
189
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700190 public class Adapter2D extends BaseObj {
191 Adapter2D(int id, RenderScript rs) {
192 super(rs);
193 mID = id;
194 }
195
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700196 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800197 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700198 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
199 }
200
201 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800202 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700203 mRS.nAdapter2DData(mID, d);
204 }
205
206 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800207 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700208 mRS.nAdapter2DData(mID, d);
209 }
210
211 public void subData(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800212 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700213 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
214 }
215
216 public void subData(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800217 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700218 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
219 }
220 }
221
222 public Adapter2D createAdapter2D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800223 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700224 int id = mRS.nAdapter2DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800225 if(id == 0) {
226 throw new IllegalStateException("allocation failed.");
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700227 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800228 mRS.nAdapter2DBindAllocation(id, mID);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700229 return new Adapter2D(id, mRS);
230 }
231
Jason Samsb8c5a842009-07-31 20:40:47 -0700232
233 // creation
234
235 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
236 static {
237 mBitmapOptions.inScaled = false;
238 }
239
Jason Sams1bada8c2009-08-09 17:01:55 -0700240 static public Allocation createTyped(RenderScript rs, Type type)
241 throws IllegalArgumentException {
242
Jason Sams771bebb2009-12-07 12:40:12 -0800243 rs.validate();
Jason Sams1bada8c2009-08-09 17:01:55 -0700244 if(type.mID == 0) {
245 throw new IllegalStateException("Bad Type");
246 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700247 int id = rs.nAllocationCreateTyped(type.mID);
Jason Sams43ee06852009-08-12 17:54:11 -0700248 return new Allocation(id, rs, type);
Jason Samsb8c5a842009-07-31 20:40:47 -0700249 }
250
Jason Sams1bada8c2009-08-09 17:01:55 -0700251 static public Allocation createSized(RenderScript rs, Element e, int count)
252 throws IllegalArgumentException {
253
Jason Sams771bebb2009-12-07 12:40:12 -0800254 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700255 Type.Builder b = new Type.Builder(rs, e);
256 b.add(Dimension.X, count);
257 Type t = b.create();
258
259 int id = rs.nAllocationCreateTyped(t.mID);
Jason Samsea84a7c2009-09-04 14:42:41 -0700260 if(id == 0) {
261 throw new IllegalStateException("Bad element.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700262 }
Jason Sams768bc022009-09-21 19:41:04 -0700263 return new Allocation(id, rs, t);
Jason Samsb8c5a842009-07-31 20:40:47 -0700264 }
265
Jason Sams8a647432010-03-01 15:31:04 -0800266 static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
267 final Bitmap.Config bc = b.getConfig();
268 if (bc == Bitmap.Config.ALPHA_8) {
269 return Element.A_8(rs);
270 }
271 if (bc == Bitmap.Config.ARGB_4444) {
272 return Element.RGBA_4444(rs);
273 }
274 if (bc == Bitmap.Config.ARGB_8888) {
275 return Element.RGBA_8888(rs);
276 }
277 if (bc == Bitmap.Config.RGB_565) {
278 return Element.RGB_565(rs);
279 }
280 throw new IllegalStateException("Bad bitmap type.");
281 }
282
283 static private Type typeFromBitmap(RenderScript rs, Bitmap b) {
284 Element e = elementFromBitmap(rs, b);
285 Type.Builder tb = new Type.Builder(rs, e);
286 tb.add(Dimension.X, b.getWidth());
287 tb.add(Dimension.Y, b.getHeight());
288 return tb.create();
289 }
290
Jason Samsb8c5a842009-07-31 20:40:47 -0700291 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
292 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700293
Jason Sams771bebb2009-12-07 12:40:12 -0800294 rs.validate();
Jason Sams8a647432010-03-01 15:31:04 -0800295 Type t = typeFromBitmap(rs, b);
296
Jason Samsea84a7c2009-09-04 14:42:41 -0700297 int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800298 if(id == 0) {
299 throw new IllegalStateException("Load failed.");
300 }
Jason Sams8a647432010-03-01 15:31:04 -0800301 return new Allocation(id, rs, t);
302 }
303
304 static public Allocation createBitmapRef(RenderScript rs, Bitmap b)
305 throws IllegalArgumentException {
306
307 rs.validate();
308 Type t = typeFromBitmap(rs, b);
309
310 int id = rs.nAllocationCreateBitmapRef(t.getID(), b);
311 if(id == 0) {
312 throw new IllegalStateException("Load failed.");
313 }
314
315 Allocation a = new Allocation(id, rs, t);
316 a.mBitmap = b;
317 return a;
Jason Samsb8c5a842009-07-31 20:40:47 -0700318 }
319
Jason Sams74e02ef2010-01-06 15:10:29 -0800320 static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
Jason Samsb8c5a842009-07-31 20:40:47 -0700321 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700322
Jason Sams771bebb2009-12-07 12:40:12 -0800323 rs.validate();
Jason Samsea84a7c2009-09-04 14:42:41 -0700324 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800325 if(id == 0) {
326 throw new IllegalStateException("Load failed.");
327 }
Jason Sams43ee06852009-08-12 17:54:11 -0700328 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700329 }
330
331 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
332 throws IllegalArgumentException {
333
Jason Sams771bebb2009-12-07 12:40:12 -0800334 rs.validate();
Romain Guy650a3eb2009-08-31 14:06:43 -0700335 InputStream is = null;
336 try {
337 final TypedValue value = new TypedValue();
338 is = res.openRawResource(id, value);
339
340 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
Jason Samsea84a7c2009-09-04 14:42:41 -0700341 int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
Romain Guy650a3eb2009-08-31 14:06:43 -0700342 asset);
343
Jason Sams718cd1f2009-12-23 14:35:29 -0800344 if(allocationId == 0) {
345 throw new IllegalStateException("Load failed.");
346 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700347 return new Allocation(allocationId, rs, null);
Romain Guy650a3eb2009-08-31 14:06:43 -0700348 } catch (Exception e) {
349 // Ignore
350 } finally {
351 if (is != null) {
352 try {
353 is.close();
354 } catch (IOException e) {
355 // Ignore
356 }
357 }
358 }
359
360 return null;
Jason Samsb8c5a842009-07-31 20:40:47 -0700361 }
362
363 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
364 throws IllegalArgumentException {
365
Romain Guyf92a0a62010-08-20 15:43:52 -0700366 mBitmapOptions.inPreferredConfig = null;
367 if (dstFmt == rs.mElement_RGBA_8888) {
368 mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
369 } else if (dstFmt == rs.mElement_RGB_888) {
370 mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
371 } else if (dstFmt == rs.mElement_RGBA_4444) {
372 mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_4444;
373 } else if (dstFmt == rs.mElement_RGB_565) {
374 mBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
375 }
376
Jason Samsb8c5a842009-07-31 20:40:47 -0700377 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
378 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
379 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700380}
381
382