blob: e6cb395773981601372aa2d235f15518698de306 [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) {
43 mRS.nAllocationUploadToTexture(mID, baseMipLevel);
44 }
45
Jason Sams07ae4062009-08-27 20:23:34 -070046 public void uploadToBufferObject() {
47 mRS.nAllocationUploadToBufferObject(mID);
48 }
49
Jason Samsb8c5a842009-07-31 20:40:47 -070050 public void data(int[] d) {
Romain Guy650a3eb2009-08-31 14:06:43 -070051 int size;
Jason Sams07ae4062009-08-27 20:23:34 -070052 if(mType != null && mType.mElement != null) {
53 size = mType.mElement.mSize;
54 for(int ct=0; ct < mType.mValues.length; ct++) {
55 if(mType.mValues[ct] != 0) {
56 size *= mType.mValues[ct];
57 }
58 }
59 if((d.length * 4) < size) {
60 throw new IllegalArgumentException("Array too small for allocation type.");
61 }
62 Log.e("rs", "Alloc data size=" + size);
63 mRS.nAllocationData(mID, d, size);
64 return;
65 }
66 mRS.nAllocationData(mID, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -070067 }
68
69 public void data(float[] d) {
Romain Guy650a3eb2009-08-31 14:06:43 -070070 int size;
Jason Sams07ae4062009-08-27 20:23:34 -070071 if(mType != null && mType.mElement != null) {
72 size = mType.mElement.mSize;
73 for(int ct=0; ct < mType.mValues.length; ct++) {
74 if(mType.mValues[ct] != 0) {
75 size *= mType.mValues[ct];
76 }
77 }
78 if((d.length * 4) < size) {
79 throw new IllegalArgumentException("Array too small for allocation type.");
80 }
81 Log.e("rs", "Alloc data size=" + size);
82 mRS.nAllocationData(mID, d, size);
83 return;
84 }
85 mRS.nAllocationData(mID, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -070086 }
87
88 public void subData1D(int off, int count, int[] d) {
Jason Sams07ae4062009-08-27 20:23:34 -070089 mRS.nAllocationSubData1D(mID, off, count, d, count * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -070090 }
91
92 public void subData1D(int off, int count, float[] d) {
Jason Sams07ae4062009-08-27 20:23:34 -070093 mRS.nAllocationSubData1D(mID, off, count, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -070094 }
95
96 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Sams07ae4062009-08-27 20:23:34 -070097 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -070098 }
99
100 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
Jason Sams07ae4062009-08-27 20:23:34 -0700101 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
Jason Samsb8c5a842009-07-31 20:40:47 -0700102 }
103
Jason Sams40a29e82009-08-10 14:55:26 -0700104 public void readData(int[] d) {
105 mRS.nAllocationRead(mID, d);
106 }
107
108 public void readData(float[] d) {
109 mRS.nAllocationRead(mID, d);
110 }
111
Jason Sams43ee06852009-08-12 17:54:11 -0700112 public void data(Object o) {
Jason Sams2525a812009-09-03 15:43:13 -0700113 mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
Jason Sams43ee06852009-08-12 17:54:11 -0700114 }
115
Jason Sams2525a812009-09-03 15:43:13 -0700116 public void subData(int offset, Object o) {
117 mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
118 }
Jason Sams40a29e82009-08-10 14:55:26 -0700119
Jason Samsb8c5a842009-07-31 20:40:47 -0700120 public class Adapter1D extends BaseObj {
121 Adapter1D(int id, RenderScript rs) {
122 super(rs);
123 mID = id;
124 }
125
Jason Samsb8c5a842009-07-31 20:40:47 -0700126 public void setConstraint(Dimension dim, int value) {
127 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
128 }
129
130 public void data(int[] d) {
131 mRS.nAdapter1DData(mID, d);
132 }
133
Jason Samsb8c5a842009-07-31 20:40:47 -0700134 public void data(float[] d) {
135 mRS.nAdapter1DData(mID, d);
136 }
137
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700138 public void subData(int off, int count, int[] d) {
139 mRS.nAdapter1DSubData(mID, off, count, d);
140 }
141
Jason Samsb8c5a842009-07-31 20:40:47 -0700142 public void subData(int off, int count, float[] d) {
143 mRS.nAdapter1DSubData(mID, off, count, d);
144 }
145 }
146
147 public Adapter1D createAdapter1D() {
148 int id = mRS.nAdapter1DCreate();
149 if (id != 0) {
150 mRS.nAdapter1DBindAllocation(id, mID);
151 }
152 return new Adapter1D(id, mRS);
153 }
154
155
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700156 public class Adapter2D extends BaseObj {
157 Adapter2D(int id, RenderScript rs) {
158 super(rs);
159 mID = id;
160 }
161
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700162 public void setConstraint(Dimension dim, int value) {
163 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
164 }
165
166 public void data(int[] d) {
167 mRS.nAdapter2DData(mID, d);
168 }
169
170 public void data(float[] d) {
171 mRS.nAdapter2DData(mID, d);
172 }
173
174 public void subData(int xoff, int yoff, int w, int h, int[] d) {
175 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
176 }
177
178 public void subData(int xoff, int yoff, int w, int h, float[] d) {
179 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
180 }
181 }
182
183 public Adapter2D createAdapter2D() {
184 int id = mRS.nAdapter2DCreate();
185 if (id != 0) {
186 mRS.nAdapter2DBindAllocation(id, mID);
187 }
188 return new Adapter2D(id, mRS);
189 }
190
Jason Samsb8c5a842009-07-31 20:40:47 -0700191
192 // creation
193
194 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
195 static {
196 mBitmapOptions.inScaled = false;
197 }
198
Jason Sams1bada8c2009-08-09 17:01:55 -0700199 static public Allocation createTyped(RenderScript rs, Type type)
200 throws IllegalArgumentException {
201
202 if(type.mID == 0) {
203 throw new IllegalStateException("Bad Type");
204 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700205 int id = rs.nAllocationCreateTyped(type.mID);
Jason Sams43ee06852009-08-12 17:54:11 -0700206 return new Allocation(id, rs, type);
Jason Samsb8c5a842009-07-31 20:40:47 -0700207 }
208
Jason Sams1bada8c2009-08-09 17:01:55 -0700209 static public Allocation createSized(RenderScript rs, Element e, int count)
210 throws IllegalArgumentException {
211
Jason Samsb8c5a842009-07-31 20:40:47 -0700212 int id;
213 if(e.mIsPredefined) {
214 id = rs.nAllocationCreatePredefSized(e.mPredefinedID, count);
215 } else {
216 id = rs.nAllocationCreateSized(e.mID, count);
Jason Sams1bada8c2009-08-09 17:01:55 -0700217 if(id == 0) {
218 throw new IllegalStateException("Bad element.");
219 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700220 }
Jason Sams43ee06852009-08-12 17:54:11 -0700221 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700222 }
223
224 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
225 throws IllegalArgumentException {
226 if(!dstFmt.mIsPredefined) {
227 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element.");
228 }
229
230 int id = rs.nAllocationCreateFromBitmap(dstFmt.mPredefinedID, genMips, b);
Jason Sams43ee06852009-08-12 17:54:11 -0700231 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700232 }
233
234 static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
235 throws IllegalArgumentException {
236 if(!dstFmt.mIsPredefined) {
237 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element.");
238 }
239
240 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mPredefinedID, genMips, b);
Jason Sams43ee06852009-08-12 17:54:11 -0700241 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700242 }
243
244 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
245 throws IllegalArgumentException {
246
Romain Guy650a3eb2009-08-31 14:06:43 -0700247 InputStream is = null;
248 try {
249 final TypedValue value = new TypedValue();
250 is = res.openRawResource(id, value);
251
252 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
253 int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mPredefinedID, genMips,
254 asset);
255
256 return new Allocation(allocationId, rs, null);
257 } catch (Exception e) {
258 // Ignore
259 } finally {
260 if (is != null) {
261 try {
262 is.close();
263 } catch (IOException e) {
264 // Ignore
265 }
266 }
267 }
268
269 return null;
Jason Samsb8c5a842009-07-31 20:40:47 -0700270 }
271
272 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
273 throws IllegalArgumentException {
274
275 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
276 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
277 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700278}
279
280