blob: 30c81ab4c897428f7d8a37cecf090a0c732292da [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 Sams5f43fd22009-09-15 12:39:22 -0700116 public void read(Object o) {
117 mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
118 }
119
Jason Sams2525a812009-09-03 15:43:13 -0700120 public void subData(int offset, Object o) {
121 mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
122 }
Jason Sams40a29e82009-08-10 14:55:26 -0700123
Jason Samsb8c5a842009-07-31 20:40:47 -0700124 public class Adapter1D extends BaseObj {
125 Adapter1D(int id, RenderScript rs) {
126 super(rs);
127 mID = id;
128 }
129
Jason Samsb8c5a842009-07-31 20:40:47 -0700130 public void setConstraint(Dimension dim, int value) {
131 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
132 }
133
134 public void data(int[] d) {
135 mRS.nAdapter1DData(mID, d);
136 }
137
Jason Samsb8c5a842009-07-31 20:40:47 -0700138 public void data(float[] d) {
139 mRS.nAdapter1DData(mID, d);
140 }
141
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700142 public void subData(int off, int count, int[] d) {
143 mRS.nAdapter1DSubData(mID, off, count, d);
144 }
145
Jason Samsb8c5a842009-07-31 20:40:47 -0700146 public void subData(int off, int count, float[] d) {
147 mRS.nAdapter1DSubData(mID, off, count, d);
148 }
149 }
150
151 public Adapter1D createAdapter1D() {
152 int id = mRS.nAdapter1DCreate();
153 if (id != 0) {
154 mRS.nAdapter1DBindAllocation(id, mID);
155 }
156 return new Adapter1D(id, mRS);
157 }
158
159
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700160 public class Adapter2D extends BaseObj {
161 Adapter2D(int id, RenderScript rs) {
162 super(rs);
163 mID = id;
164 }
165
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700166 public void setConstraint(Dimension dim, int value) {
167 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
168 }
169
170 public void data(int[] d) {
171 mRS.nAdapter2DData(mID, d);
172 }
173
174 public void data(float[] d) {
175 mRS.nAdapter2DData(mID, d);
176 }
177
178 public void subData(int xoff, int yoff, int w, int h, int[] d) {
179 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
180 }
181
182 public void subData(int xoff, int yoff, int w, int h, float[] d) {
183 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
184 }
185 }
186
187 public Adapter2D createAdapter2D() {
188 int id = mRS.nAdapter2DCreate();
189 if (id != 0) {
190 mRS.nAdapter2DBindAllocation(id, mID);
191 }
192 return new Adapter2D(id, mRS);
193 }
194
Jason Samsb8c5a842009-07-31 20:40:47 -0700195
196 // creation
197
198 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
199 static {
200 mBitmapOptions.inScaled = false;
201 }
202
Jason Sams1bada8c2009-08-09 17:01:55 -0700203 static public Allocation createTyped(RenderScript rs, Type type)
204 throws IllegalArgumentException {
205
206 if(type.mID == 0) {
207 throw new IllegalStateException("Bad Type");
208 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700209 int id = rs.nAllocationCreateTyped(type.mID);
Jason Sams43ee06852009-08-12 17:54:11 -0700210 return new Allocation(id, rs, type);
Jason Samsb8c5a842009-07-31 20:40:47 -0700211 }
212
Jason Sams1bada8c2009-08-09 17:01:55 -0700213 static public Allocation createSized(RenderScript rs, Element e, int count)
214 throws IllegalArgumentException {
215
Jason Samsea84a7c2009-09-04 14:42:41 -0700216 int id = rs.nAllocationCreateSized(e.mID, count);
217 if(id == 0) {
218 throw new IllegalStateException("Bad element.");
Jason Samsb8c5a842009-07-31 20:40:47 -0700219 }
Jason Sams43ee06852009-08-12 17:54:11 -0700220 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700221 }
222
223 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
224 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700225
Jason Samsea84a7c2009-09-04 14:42:41 -0700226 int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
Jason Sams43ee06852009-08-12 17:54:11 -0700227 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700228 }
229
230 static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
231 throws IllegalArgumentException {
Jason Samsb8c5a842009-07-31 20:40:47 -0700232
Jason Samsea84a7c2009-09-04 14:42:41 -0700233 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
Jason Sams43ee06852009-08-12 17:54:11 -0700234 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700235 }
236
237 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
238 throws IllegalArgumentException {
239
Romain Guy650a3eb2009-08-31 14:06:43 -0700240 InputStream is = null;
241 try {
242 final TypedValue value = new TypedValue();
243 is = res.openRawResource(id, value);
244
245 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
Jason Samsea84a7c2009-09-04 14:42:41 -0700246 int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
Romain Guy650a3eb2009-08-31 14:06:43 -0700247 asset);
248
Jason Samsea84a7c2009-09-04 14:42:41 -0700249 return new Allocation(allocationId, rs, null);
Romain Guy650a3eb2009-08-31 14:06:43 -0700250 } catch (Exception e) {
251 // Ignore
252 } finally {
253 if (is != null) {
254 try {
255 is.close();
256 } catch (IOException e) {
257 // Ignore
258 }
259 }
260 }
261
262 return null;
Jason Samsb8c5a842009-07-31 20:40:47 -0700263 }
264
265 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
266 throws IllegalArgumentException {
267
268 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
269 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
270 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700271}
272
273