blob: 50d39b7ca09fdd29250bd37cadbf50272cf9525f [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 Sams43ee06852009-08-12 17:54:11 -070019import java.lang.reflect.Field;
20import java.lang.reflect.Array;
Jason Samsb8c5a842009-07-31 20:40:47 -070021
22import java.io.IOException;
23import java.io.InputStream;
24
25import android.content.res.Resources;
26import android.graphics.Bitmap;
27import android.graphics.BitmapFactory;
28import android.os.Bundle;
29import android.renderscript.Type;
30import android.util.Config;
31import android.util.Log;
32
33/**
34 * @hide
35 *
36 **/
37public class Allocation extends BaseObj {
Jason Sams43ee06852009-08-12 17:54:11 -070038 Type mType;
39
40 Allocation(int id, RenderScript rs, Type t) {
Jason Samsb8c5a842009-07-31 20:40:47 -070041 super(rs);
42 mID = id;
Jason Sams43ee06852009-08-12 17:54:11 -070043 mType = t;
Jason Samsb8c5a842009-07-31 20:40:47 -070044 }
45
46 public void uploadToTexture(int baseMipLevel) {
47 mRS.nAllocationUploadToTexture(mID, baseMipLevel);
48 }
49
50 public void destroy() {
Jason Sams1bada8c2009-08-09 17:01:55 -070051 if(mDestroyed) {
52 throw new IllegalStateException("Object already destroyed.");
53 }
54 mDestroyed = true;
Jason Samsb8c5a842009-07-31 20:40:47 -070055 mRS.nAllocationDestroy(mID);
Jason Samsb8c5a842009-07-31 20:40:47 -070056 }
57
58 public void data(int[] d) {
59 mRS.nAllocationData(mID, d);
60 }
61
62 public void data(float[] d) {
63 mRS.nAllocationData(mID, d);
64 }
65
66 public void subData1D(int off, int count, int[] d) {
67 mRS.nAllocationSubData1D(mID, off, count, d);
68 }
69
70 public void subData1D(int off, int count, float[] d) {
71 mRS.nAllocationSubData1D(mID, off, count, d);
72 }
73
74 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
75 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d);
76 }
77
78 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
79 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d);
80 }
81
Jason Sams40a29e82009-08-10 14:55:26 -070082 public void readData(int[] d) {
83 mRS.nAllocationRead(mID, d);
84 }
85
86 public void readData(float[] d) {
87 mRS.nAllocationRead(mID, d);
88 }
89
Jason Sams43ee06852009-08-12 17:54:11 -070090 public void data(Object o) {
91 mRS.nAllocationDataFromObject(mID, mType, o);
92 }
93
Jason Sams40a29e82009-08-10 14:55:26 -070094
Jason Samsb8c5a842009-07-31 20:40:47 -070095 public class Adapter1D extends BaseObj {
96 Adapter1D(int id, RenderScript rs) {
97 super(rs);
98 mID = id;
99 }
100
101 public void destroy() {
102 mRS.nAdapter1DDestroy(mID);
103 mID = 0;
104 }
105
106 public void setConstraint(Dimension dim, int value) {
107 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
108 }
109
110 public void data(int[] d) {
111 mRS.nAdapter1DData(mID, d);
112 }
113
Jason Samsb8c5a842009-07-31 20:40:47 -0700114 public void data(float[] d) {
115 mRS.nAdapter1DData(mID, d);
116 }
117
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700118 public void subData(int off, int count, int[] d) {
119 mRS.nAdapter1DSubData(mID, off, count, d);
120 }
121
Jason Samsb8c5a842009-07-31 20:40:47 -0700122 public void subData(int off, int count, float[] d) {
123 mRS.nAdapter1DSubData(mID, off, count, d);
124 }
125 }
126
127 public Adapter1D createAdapter1D() {
128 int id = mRS.nAdapter1DCreate();
129 if (id != 0) {
130 mRS.nAdapter1DBindAllocation(id, mID);
131 }
132 return new Adapter1D(id, mRS);
133 }
134
135
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700136 public class Adapter2D extends BaseObj {
137 Adapter2D(int id, RenderScript rs) {
138 super(rs);
139 mID = id;
140 }
141
142 public void destroy() {
143 mRS.nAdapter2DDestroy(mID);
144 mID = 0;
145 }
146
147 public void setConstraint(Dimension dim, int value) {
148 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
149 }
150
151 public void data(int[] d) {
152 mRS.nAdapter2DData(mID, d);
153 }
154
155 public void data(float[] d) {
156 mRS.nAdapter2DData(mID, d);
157 }
158
159 public void subData(int xoff, int yoff, int w, int h, int[] d) {
160 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
161 }
162
163 public void subData(int xoff, int yoff, int w, int h, float[] d) {
164 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
165 }
166 }
167
168 public Adapter2D createAdapter2D() {
169 int id = mRS.nAdapter2DCreate();
170 if (id != 0) {
171 mRS.nAdapter2DBindAllocation(id, mID);
172 }
173 return new Adapter2D(id, mRS);
174 }
175
Jason Samsb8c5a842009-07-31 20:40:47 -0700176
177 // creation
178
179 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
180 static {
181 mBitmapOptions.inScaled = false;
182 }
183
Jason Sams1bada8c2009-08-09 17:01:55 -0700184 static public Allocation createTyped(RenderScript rs, Type type)
185 throws IllegalArgumentException {
186
187 if(type.mID == 0) {
188 throw new IllegalStateException("Bad Type");
189 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700190 int id = rs.nAllocationCreateTyped(type.mID);
Jason Sams43ee06852009-08-12 17:54:11 -0700191 return new Allocation(id, rs, type);
Jason Samsb8c5a842009-07-31 20:40:47 -0700192 }
193
Jason Sams1bada8c2009-08-09 17:01:55 -0700194 static public Allocation createSized(RenderScript rs, Element e, int count)
195 throws IllegalArgumentException {
196
Jason Samsb8c5a842009-07-31 20:40:47 -0700197 int id;
198 if(e.mIsPredefined) {
199 id = rs.nAllocationCreatePredefSized(e.mPredefinedID, count);
200 } else {
201 id = rs.nAllocationCreateSized(e.mID, count);
Jason Sams1bada8c2009-08-09 17:01:55 -0700202 if(id == 0) {
203 throw new IllegalStateException("Bad element.");
204 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700205 }
Jason Sams43ee06852009-08-12 17:54:11 -0700206 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700207 }
208
209 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
210 throws IllegalArgumentException {
211 if(!dstFmt.mIsPredefined) {
212 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element.");
213 }
214
215 int id = rs.nAllocationCreateFromBitmap(dstFmt.mPredefinedID, genMips, b);
Jason Sams43ee06852009-08-12 17:54:11 -0700216 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700217 }
218
219 static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
220 throws IllegalArgumentException {
221 if(!dstFmt.mIsPredefined) {
222 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element.");
223 }
224
225 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mPredefinedID, genMips, b);
Jason Sams43ee06852009-08-12 17:54:11 -0700226 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700227 }
228
229 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
230 throws IllegalArgumentException {
231
232 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
233 return createFromBitmap(rs, b, dstFmt, genMips);
234 }
235
236 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
237 throws IllegalArgumentException {
238
239 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
240 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
241 }
Jason Sams43ee06852009-08-12 17:54:11 -0700242/*
243 public static Allocation createFromObject(RenderScript rs, Object o) {
244 Class c = o.getClass();
245 Type t;
246 if(c.isArray()) {
247 t = Type.createFromClass(rs, c, Array.getLength(o));
248 } else {
249 t = Type.createFromClass(rs, c, 1);
250 }
251 Allocation alloc = createTyped(rs, t);
252 t.destroy();
253 return alloc;
254 }
255*/
Jason Samsb8c5a842009-07-31 20:40:47 -0700256}
257
258