blob: b6ac14aa4c2921765922585dcb33a38929e3dcbe [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;
35
36 Allocation(int id, RenderScript rs, Type t) {
Jason Samsb8c5a842009-07-31 20:40:47 -070037 super(rs);
38 mID = id;
Jason Sams43ee06852009-08-12 17:54:11 -070039 mType = t;
Jason Samsb8c5a842009-07-31 20:40:47 -070040 }
41
42 public void uploadToTexture(int baseMipLevel) {
Jason Sams771bebb2009-12-07 12:40:12 -080043 mRS.validate();
44 mRS.validateSurface();
Jason Samsb8c5a842009-07-31 20:40:47 -070045 mRS.nAllocationUploadToTexture(mID, baseMipLevel);
46 }
47
Jason Sams07ae4062009-08-27 20:23:34 -070048 public void uploadToBufferObject() {
Jason Sams771bebb2009-12-07 12:40:12 -080049 mRS.validate();
50 mRS.validateSurface();
Jason Sams07ae4062009-08-27 20:23:34 -070051 mRS.nAllocationUploadToBufferObject(mID);
52 }
53
Jason Samsb8c5a842009-07-31 20:40:47 -070054 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080055 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070056 subData1D(0, mType.getElementCount(), d);
57 }
58 public void data(short[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080059 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070060 subData1D(0, mType.getElementCount(), d);
61 }
62 public void data(byte[] 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(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080067 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070068 subData1D(0, mType.getElementCount(), d);
Jason Samsb8c5a842009-07-31 20:40:47 -070069 }
70
Jason Sams768bc022009-09-21 19:41:04 -070071 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -080072 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070073 if((off < 0) || (count < 1) || ((off + count) > mType.getElementCount())) {
74 throw new IllegalArgumentException("Offset or Count out of bounds.");
Jason Sams07ae4062009-08-27 20:23:34 -070075 }
Jason Sams768bc022009-09-21 19:41:04 -070076 if((len) < dataSize) {
77 throw new IllegalArgumentException("Array too small for allocation type.");
78 }
Jason Samsb8c5a842009-07-31 20:40:47 -070079 }
80
81 public void subData1D(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -070082 int dataSize = mType.mElement.getSizeBytes() * count;
83 data1DChecks(off, count, d.length * 4, dataSize);
84 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
85 }
86 public void subData1D(int off, int count, short[] d) {
87 int dataSize = mType.mElement.getSizeBytes() * count;
88 data1DChecks(off, count, d.length * 2, dataSize);
89 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
90 }
91 public void subData1D(int off, int count, byte[] d) {
92 int dataSize = mType.mElement.getSizeBytes() * count;
93 data1DChecks(off, count, d.length, dataSize);
94 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
95 }
96 public void subData1D(int off, int count, float[] d) {
97 int dataSize = mType.mElement.getSizeBytes() * count;
98 data1DChecks(off, count, d.length * 4, dataSize);
99 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700100 }
101
Jason Sams768bc022009-09-21 19:41:04 -0700102
Jason Samsb8c5a842009-07-31 20:40:47 -0700103
104 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800105 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700106 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700107 }
108
109 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800110 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700111 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700112 }
113
Jason Sams40a29e82009-08-10 14:55:26 -0700114 public void readData(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800115 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700116 mRS.nAllocationRead(mID, d);
117 }
118
119 public void readData(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800120 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700121 mRS.nAllocationRead(mID, d);
122 }
123
Jason Sams43ee06852009-08-12 17:54:11 -0700124 public void data(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800125 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700126 mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
Jason Sams43ee06852009-08-12 17:54:11 -0700127 }
128
Jason Sams5f43fd22009-09-15 12:39:22 -0700129 public void read(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800130 mRS.validate();
Jason Sams5f43fd22009-09-15 12:39:22 -0700131 mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
132 }
133
Jason Sams2525a812009-09-03 15:43:13 -0700134 public void subData(int offset, Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800135 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700136 mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
137 }
Jason Sams40a29e82009-08-10 14:55:26 -0700138
Jason Samsb8c5a842009-07-31 20:40:47 -0700139 public class Adapter1D extends BaseObj {
140 Adapter1D(int id, RenderScript rs) {
141 super(rs);
142 mID = id;
143 }
144
Jason Samsb8c5a842009-07-31 20:40:47 -0700145 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800146 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700147 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
148 }
149
150 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800151 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700152 mRS.nAdapter1DData(mID, d);
153 }
154
Jason Samsb8c5a842009-07-31 20:40:47 -0700155 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800156 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700157 mRS.nAdapter1DData(mID, d);
158 }
159
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700160 public void subData(int off, int count, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800161 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700162 mRS.nAdapter1DSubData(mID, off, count, d);
163 }
164
Jason Samsb8c5a842009-07-31 20:40:47 -0700165 public void subData(int off, int count, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800166 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700167 mRS.nAdapter1DSubData(mID, off, count, d);
168 }
169 }
170
171 public Adapter1D createAdapter1D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800172 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700173 int id = mRS.nAdapter1DCreate();
174 if (id != 0) {
175 mRS.nAdapter1DBindAllocation(id, mID);
176 }
177 return new Adapter1D(id, mRS);
178 }
179
180
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700181 public class Adapter2D extends BaseObj {
182 Adapter2D(int id, RenderScript rs) {
183 super(rs);
184 mID = id;
185 }
186
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700187 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800188 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700189 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
190 }
191
192 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800193 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700194 mRS.nAdapter2DData(mID, d);
195 }
196
197 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800198 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700199 mRS.nAdapter2DData(mID, d);
200 }
201
202 public void subData(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800203 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700204 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
205 }
206
207 public void subData(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800208 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700209 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
210 }
211 }
212
213 public Adapter2D createAdapter2D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800214 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700215 int id = mRS.nAdapter2DCreate();
216 if (id != 0) {
217 mRS.nAdapter2DBindAllocation(id, mID);
218 }
219 return new Adapter2D(id, mRS);
220 }
221
Jason Samsb8c5a842009-07-31 20:40:47 -0700222
223 // creation
224
225 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
226 static {
227 mBitmapOptions.inScaled = false;
228 }
229
Jason Sams1bada8c2009-08-09 17:01:55 -0700230 static public Allocation createTyped(RenderScript rs, Type type)
231 throws IllegalArgumentException {
232
Jason Sams771bebb2009-12-07 12:40:12 -0800233 rs.validate();
Jason Sams1bada8c2009-08-09 17:01:55 -0700234 if(type.mID == 0) {
235 throw new IllegalStateException("Bad Type");
236 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700237 int id = rs.nAllocationCreateTyped(type.mID);
Jason Sams43ee06852009-08-12 17:54:11 -0700238 return new Allocation(id, rs, type);
Jason Samsb8c5a842009-07-31 20:40:47 -0700239 }
240
Jason Sams1bada8c2009-08-09 17:01:55 -0700241 static public Allocation createSized(RenderScript rs, Element e, int count)
242 throws IllegalArgumentException {
243
Jason Sams771bebb2009-12-07 12:40:12 -0800244 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700245 Type.Builder b = new Type.Builder(rs, e);
246 b.add(Dimension.X, count);
247 Type t = b.create();
248
249 int id = rs.nAllocationCreateTyped(t.mID);
Jason Samsea84a7c2009-09-04 14:42:41 -0700250 if(id == 0) {
251 throw new IllegalStateException("Bad element.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700252 }
Jason Sams768bc022009-09-21 19:41:04 -0700253 return new Allocation(id, rs, t);
Jason Samsb8c5a842009-07-31 20:40:47 -0700254 }
255
256 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
257 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700258
Jason Sams771bebb2009-12-07 12:40:12 -0800259 rs.validate();
Jason Samsea84a7c2009-09-04 14:42:41 -0700260 int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
Jason Sams43ee06852009-08-12 17:54:11 -0700261 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700262 }
263
264 static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
265 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700266
Jason Sams771bebb2009-12-07 12:40:12 -0800267 rs.validate();
Jason Samsea84a7c2009-09-04 14:42:41 -0700268 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
Jason Sams43ee06852009-08-12 17:54:11 -0700269 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700270 }
271
272 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
273 throws IllegalArgumentException {
274
Jason Sams771bebb2009-12-07 12:40:12 -0800275 rs.validate();
Romain Guy650a3eb2009-08-31 14:06:43 -0700276 InputStream is = null;
277 try {
278 final TypedValue value = new TypedValue();
279 is = res.openRawResource(id, value);
280
281 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
Jason Samsea84a7c2009-09-04 14:42:41 -0700282 int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
Romain Guy650a3eb2009-08-31 14:06:43 -0700283 asset);
284
Jason Samsea84a7c2009-09-04 14:42:41 -0700285 return new Allocation(allocationId, rs, null);
Romain Guy650a3eb2009-08-31 14:06:43 -0700286 } catch (Exception e) {
287 // Ignore
288 } finally {
289 if (is != null) {
290 try {
291 is.close();
292 } catch (IOException e) {
293 // Ignore
294 }
295 }
296 }
297
298 return null;
Jason Samsb8c5a842009-07-31 20:40:47 -0700299 }
300
301 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
302 throws IllegalArgumentException {
303
304 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
305 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
306 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700307}
308
309