blob: 536f71c1de205ec7ae360bfbf8424fe11052ec22 [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) {
113 mRS.nAllocationDataFromObject(mID, mType, o);
114 }
115
Jason Sams40a29e82009-08-10 14:55:26 -0700116
Jason Samsb8c5a842009-07-31 20:40:47 -0700117 public class Adapter1D extends BaseObj {
118 Adapter1D(int id, RenderScript rs) {
119 super(rs);
120 mID = id;
121 }
122
Jason Samsb8c5a842009-07-31 20:40:47 -0700123 public void setConstraint(Dimension dim, int value) {
124 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
125 }
126
127 public void data(int[] d) {
128 mRS.nAdapter1DData(mID, d);
129 }
130
Jason Samsb8c5a842009-07-31 20:40:47 -0700131 public void data(float[] d) {
132 mRS.nAdapter1DData(mID, d);
133 }
134
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700135 public void subData(int off, int count, int[] d) {
136 mRS.nAdapter1DSubData(mID, off, count, d);
137 }
138
Jason Samsb8c5a842009-07-31 20:40:47 -0700139 public void subData(int off, int count, float[] d) {
140 mRS.nAdapter1DSubData(mID, off, count, d);
141 }
142 }
143
144 public Adapter1D createAdapter1D() {
145 int id = mRS.nAdapter1DCreate();
146 if (id != 0) {
147 mRS.nAdapter1DBindAllocation(id, mID);
148 }
149 return new Adapter1D(id, mRS);
150 }
151
152
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700153 public class Adapter2D extends BaseObj {
154 Adapter2D(int id, RenderScript rs) {
155 super(rs);
156 mID = id;
157 }
158
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700159 public void setConstraint(Dimension dim, int value) {
160 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
161 }
162
163 public void data(int[] d) {
164 mRS.nAdapter2DData(mID, d);
165 }
166
167 public void data(float[] d) {
168 mRS.nAdapter2DData(mID, d);
169 }
170
171 public void subData(int xoff, int yoff, int w, int h, int[] d) {
172 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
173 }
174
175 public void subData(int xoff, int yoff, int w, int h, float[] d) {
176 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
177 }
178 }
179
180 public Adapter2D createAdapter2D() {
181 int id = mRS.nAdapter2DCreate();
182 if (id != 0) {
183 mRS.nAdapter2DBindAllocation(id, mID);
184 }
185 return new Adapter2D(id, mRS);
186 }
187
Jason Samsb8c5a842009-07-31 20:40:47 -0700188
189 // creation
190
191 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
192 static {
193 mBitmapOptions.inScaled = false;
194 }
195
Jason Sams1bada8c2009-08-09 17:01:55 -0700196 static public Allocation createTyped(RenderScript rs, Type type)
197 throws IllegalArgumentException {
198
199 if(type.mID == 0) {
200 throw new IllegalStateException("Bad Type");
201 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700202 int id = rs.nAllocationCreateTyped(type.mID);
Jason Sams43ee06852009-08-12 17:54:11 -0700203 return new Allocation(id, rs, type);
Jason Samsb8c5a842009-07-31 20:40:47 -0700204 }
205
Jason Sams1bada8c2009-08-09 17:01:55 -0700206 static public Allocation createSized(RenderScript rs, Element e, int count)
207 throws IllegalArgumentException {
208
Jason Samsb8c5a842009-07-31 20:40:47 -0700209 int id;
210 if(e.mIsPredefined) {
211 id = rs.nAllocationCreatePredefSized(e.mPredefinedID, count);
212 } else {
213 id = rs.nAllocationCreateSized(e.mID, count);
Jason Sams1bada8c2009-08-09 17:01:55 -0700214 if(id == 0) {
215 throw new IllegalStateException("Bad element.");
216 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700217 }
Jason Sams43ee06852009-08-12 17:54:11 -0700218 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700219 }
220
221 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
222 throws IllegalArgumentException {
223 if(!dstFmt.mIsPredefined) {
224 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element.");
225 }
226
227 int id = rs.nAllocationCreateFromBitmap(dstFmt.mPredefinedID, genMips, b);
Jason Sams43ee06852009-08-12 17:54:11 -0700228 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700229 }
230
231 static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
232 throws IllegalArgumentException {
233 if(!dstFmt.mIsPredefined) {
234 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element.");
235 }
236
237 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mPredefinedID, genMips, b);
Jason Sams43ee06852009-08-12 17:54:11 -0700238 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700239 }
240
241 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
242 throws IllegalArgumentException {
243
Romain Guy650a3eb2009-08-31 14:06:43 -0700244 InputStream is = null;
245 try {
246 final TypedValue value = new TypedValue();
247 is = res.openRawResource(id, value);
248
249 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
250 int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mPredefinedID, genMips,
251 asset);
252
253 return new Allocation(allocationId, rs, null);
254 } catch (Exception e) {
255 // Ignore
256 } finally {
257 if (is != null) {
258 try {
259 is.close();
260 } catch (IOException e) {
261 // Ignore
262 }
263 }
264 }
265
266 return null;
Jason Samsb8c5a842009-07-31 20:40:47 -0700267 }
268
269 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
270 throws IllegalArgumentException {
271
272 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
273 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
274 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700275}
276
277