blob: e5cf38ec5efa236ca3bbc60546bb065943f87bae [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
Jason Samsea87e962010-01-12 12:12:28 -080042 public Type getType() {
43 return mType;
44 }
45
Jason Samsb8c5a842009-07-31 20:40:47 -070046 public void uploadToTexture(int baseMipLevel) {
Jason Sams771bebb2009-12-07 12:40:12 -080047 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -070048 mRS.nAllocationUploadToTexture(mID, baseMipLevel);
49 }
50
Jason Sams07ae4062009-08-27 20:23:34 -070051 public void uploadToBufferObject() {
Jason Sams771bebb2009-12-07 12:40:12 -080052 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -070053 mRS.nAllocationUploadToBufferObject(mID);
54 }
55
Jason Samsb8c5a842009-07-31 20:40:47 -070056 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080057 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070058 subData1D(0, mType.getElementCount(), d);
59 }
60 public void data(short[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080061 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070062 subData1D(0, mType.getElementCount(), d);
63 }
64 public void data(byte[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080065 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070066 subData1D(0, mType.getElementCount(), d);
67 }
68 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -080069 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070070 subData1D(0, mType.getElementCount(), d);
Jason Samsb8c5a842009-07-31 20:40:47 -070071 }
72
Jason Sams768bc022009-09-21 19:41:04 -070073 private void data1DChecks(int off, int count, int len, int dataSize) {
Jason Sams771bebb2009-12-07 12:40:12 -080074 mRS.validate();
Jason Sams768bc022009-09-21 19:41:04 -070075 if((off < 0) || (count < 1) || ((off + count) > mType.getElementCount())) {
76 throw new IllegalArgumentException("Offset or Count out of bounds.");
Jason Sams07ae4062009-08-27 20:23:34 -070077 }
Jason Sams768bc022009-09-21 19:41:04 -070078 if((len) < dataSize) {
79 throw new IllegalArgumentException("Array too small for allocation type.");
80 }
Jason Samsb8c5a842009-07-31 20:40:47 -070081 }
82
83 public void subData1D(int off, int count, int[] d) {
Jason Sams768bc022009-09-21 19:41:04 -070084 int dataSize = mType.mElement.getSizeBytes() * count;
85 data1DChecks(off, count, d.length * 4, dataSize);
86 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
87 }
88 public void subData1D(int off, int count, short[] d) {
89 int dataSize = mType.mElement.getSizeBytes() * count;
90 data1DChecks(off, count, d.length * 2, dataSize);
91 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
92 }
93 public void subData1D(int off, int count, byte[] d) {
94 int dataSize = mType.mElement.getSizeBytes() * count;
95 data1DChecks(off, count, d.length, dataSize);
96 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
97 }
98 public void subData1D(int off, int count, float[] d) {
99 int dataSize = mType.mElement.getSizeBytes() * count;
100 data1DChecks(off, count, d.length * 4, dataSize);
101 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
Jason Samsb8c5a842009-07-31 20:40:47 -0700102 }
103
Jason Sams768bc022009-09-21 19:41:04 -0700104
Jason Samsb8c5a842009-07-31 20:40:47 -0700105
106 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800107 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700108 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700109 }
110
111 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800112 mRS.validate();
Jason Sams07ae4062009-08-27 20:23:34 -0700113 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700114 }
115
Jason Sams40a29e82009-08-10 14:55:26 -0700116 public void readData(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800117 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700118 mRS.nAllocationRead(mID, d);
119 }
120
121 public void readData(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800122 mRS.validate();
Jason Sams40a29e82009-08-10 14:55:26 -0700123 mRS.nAllocationRead(mID, d);
124 }
125
Jason Sams43ee06852009-08-12 17:54:11 -0700126 public void data(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800127 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700128 mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
Jason Sams43ee06852009-08-12 17:54:11 -0700129 }
130
Jason Sams5f43fd22009-09-15 12:39:22 -0700131 public void read(Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800132 mRS.validate();
Jason Sams5f43fd22009-09-15 12:39:22 -0700133 mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
134 }
135
Jason Sams2525a812009-09-03 15:43:13 -0700136 public void subData(int offset, Object o) {
Jason Sams771bebb2009-12-07 12:40:12 -0800137 mRS.validate();
Jason Sams2525a812009-09-03 15:43:13 -0700138 mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
139 }
Jason Sams40a29e82009-08-10 14:55:26 -0700140
Jason Samsb8c5a842009-07-31 20:40:47 -0700141 public class Adapter1D extends BaseObj {
142 Adapter1D(int id, RenderScript rs) {
143 super(rs);
144 mID = id;
145 }
146
Jason Samsb8c5a842009-07-31 20:40:47 -0700147 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800148 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700149 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
150 }
151
152 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800153 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700154 mRS.nAdapter1DData(mID, d);
155 }
156
Jason Samsb8c5a842009-07-31 20:40:47 -0700157 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800158 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700159 mRS.nAdapter1DData(mID, d);
160 }
161
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700162 public void subData(int off, int count, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800163 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700164 mRS.nAdapter1DSubData(mID, off, count, d);
165 }
166
Jason Samsb8c5a842009-07-31 20:40:47 -0700167 public void subData(int off, int count, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800168 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700169 mRS.nAdapter1DSubData(mID, off, count, d);
170 }
171 }
172
173 public Adapter1D createAdapter1D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800174 mRS.validate();
Jason Samsb8c5a842009-07-31 20:40:47 -0700175 int id = mRS.nAdapter1DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800176 if(id == 0) {
177 throw new IllegalStateException("allocation failed.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700178 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800179 mRS.nAdapter1DBindAllocation(id, mID);
Jason Samsb8c5a842009-07-31 20:40:47 -0700180 return new Adapter1D(id, mRS);
181 }
182
183
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700184 public class Adapter2D extends BaseObj {
185 Adapter2D(int id, RenderScript rs) {
186 super(rs);
187 mID = id;
188 }
189
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700190 public void setConstraint(Dimension dim, int value) {
Jason Sams771bebb2009-12-07 12:40:12 -0800191 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700192 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
193 }
194
195 public void data(int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800196 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700197 mRS.nAdapter2DData(mID, d);
198 }
199
200 public void data(float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800201 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700202 mRS.nAdapter2DData(mID, d);
203 }
204
205 public void subData(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800206 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700207 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
208 }
209
210 public void subData(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams771bebb2009-12-07 12:40:12 -0800211 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700212 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
213 }
214 }
215
216 public Adapter2D createAdapter2D() {
Jason Sams771bebb2009-12-07 12:40:12 -0800217 mRS.validate();
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700218 int id = mRS.nAdapter2DCreate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800219 if(id == 0) {
220 throw new IllegalStateException("allocation failed.");
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700221 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800222 mRS.nAdapter2DBindAllocation(id, mID);
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700223 return new Adapter2D(id, mRS);
224 }
225
Jason Samsb8c5a842009-07-31 20:40:47 -0700226
227 // creation
228
229 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
230 static {
231 mBitmapOptions.inScaled = false;
232 }
233
Jason Sams1bada8c2009-08-09 17:01:55 -0700234 static public Allocation createTyped(RenderScript rs, Type type)
235 throws IllegalArgumentException {
236
Jason Sams771bebb2009-12-07 12:40:12 -0800237 rs.validate();
Jason Sams1bada8c2009-08-09 17:01:55 -0700238 if(type.mID == 0) {
239 throw new IllegalStateException("Bad Type");
240 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700241 int id = rs.nAllocationCreateTyped(type.mID);
Jason Sams43ee06852009-08-12 17:54:11 -0700242 return new Allocation(id, rs, type);
Jason Samsb8c5a842009-07-31 20:40:47 -0700243 }
244
Jason Sams1bada8c2009-08-09 17:01:55 -0700245 static public Allocation createSized(RenderScript rs, Element e, int count)
246 throws IllegalArgumentException {
247
Jason Sams771bebb2009-12-07 12:40:12 -0800248 rs.validate();
Jason Sams768bc022009-09-21 19:41:04 -0700249 Type.Builder b = new Type.Builder(rs, e);
250 b.add(Dimension.X, count);
251 Type t = b.create();
252
253 int id = rs.nAllocationCreateTyped(t.mID);
Jason Samsea84a7c2009-09-04 14:42:41 -0700254 if(id == 0) {
255 throw new IllegalStateException("Bad element.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700256 }
Jason Sams768bc022009-09-21 19:41:04 -0700257 return new Allocation(id, rs, t);
Jason Samsb8c5a842009-07-31 20:40:47 -0700258 }
259
260 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
261 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700262
Jason Sams771bebb2009-12-07 12:40:12 -0800263 rs.validate();
Jason Samsea84a7c2009-09-04 14:42:41 -0700264 int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800265 if(id == 0) {
266 throw new IllegalStateException("Load failed.");
267 }
Jason Sams43ee06852009-08-12 17:54:11 -0700268 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700269 }
270
Jason Sams74e02ef2010-01-06 15:10:29 -0800271 static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
Jason Samsb8c5a842009-07-31 20:40:47 -0700272 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700273
Jason Sams771bebb2009-12-07 12:40:12 -0800274 rs.validate();
Jason Samsea84a7c2009-09-04 14:42:41 -0700275 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
Jason Sams718cd1f2009-12-23 14:35:29 -0800276 if(id == 0) {
277 throw new IllegalStateException("Load failed.");
278 }
Jason Sams43ee06852009-08-12 17:54:11 -0700279 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700280 }
281
282 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
283 throws IllegalArgumentException {
284
Jason Sams771bebb2009-12-07 12:40:12 -0800285 rs.validate();
Romain Guy650a3eb2009-08-31 14:06:43 -0700286 InputStream is = null;
287 try {
288 final TypedValue value = new TypedValue();
289 is = res.openRawResource(id, value);
290
291 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
Jason Samsea84a7c2009-09-04 14:42:41 -0700292 int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
Romain Guy650a3eb2009-08-31 14:06:43 -0700293 asset);
294
Jason Sams718cd1f2009-12-23 14:35:29 -0800295 if(allocationId == 0) {
296 throw new IllegalStateException("Load failed.");
297 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700298 return new Allocation(allocationId, rs, null);
Romain Guy650a3eb2009-08-31 14:06:43 -0700299 } catch (Exception e) {
300 // Ignore
301 } finally {
302 if (is != null) {
303 try {
304 is.close();
305 } catch (IOException e) {
306 // Ignore
307 }
308 }
309 }
310
311 return null;
Jason Samsb8c5a842009-07-31 20:40:47 -0700312 }
313
314 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
315 throws IllegalArgumentException {
316
317 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
318 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
319 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700320}
321
322