blob: 81848b976ed95f33e10a7ab01ab01bc517f5b531 [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
Jason Samsb8c5a842009-07-31 20:40:47 -070050 public void data(int[] d) {
51 mRS.nAllocationData(mID, d);
52 }
53
54 public void data(float[] d) {
55 mRS.nAllocationData(mID, d);
56 }
57
58 public void subData1D(int off, int count, int[] d) {
59 mRS.nAllocationSubData1D(mID, off, count, d);
60 }
61
62 public void subData1D(int off, int count, float[] d) {
63 mRS.nAllocationSubData1D(mID, off, count, d);
64 }
65
66 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
67 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d);
68 }
69
70 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
71 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d);
72 }
73
Jason Sams40a29e82009-08-10 14:55:26 -070074 public void readData(int[] d) {
75 mRS.nAllocationRead(mID, d);
76 }
77
78 public void readData(float[] d) {
79 mRS.nAllocationRead(mID, d);
80 }
81
Jason Sams43ee06852009-08-12 17:54:11 -070082 public void data(Object o) {
83 mRS.nAllocationDataFromObject(mID, mType, o);
84 }
85
Jason Sams40a29e82009-08-10 14:55:26 -070086
Jason Samsb8c5a842009-07-31 20:40:47 -070087 public class Adapter1D extends BaseObj {
88 Adapter1D(int id, RenderScript rs) {
89 super(rs);
90 mID = id;
91 }
92
Jason Samsb8c5a842009-07-31 20:40:47 -070093 public void setConstraint(Dimension dim, int value) {
94 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
95 }
96
97 public void data(int[] d) {
98 mRS.nAdapter1DData(mID, d);
99 }
100
Jason Samsb8c5a842009-07-31 20:40:47 -0700101 public void data(float[] d) {
102 mRS.nAdapter1DData(mID, d);
103 }
104
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700105 public void subData(int off, int count, int[] d) {
106 mRS.nAdapter1DSubData(mID, off, count, d);
107 }
108
Jason Samsb8c5a842009-07-31 20:40:47 -0700109 public void subData(int off, int count, float[] d) {
110 mRS.nAdapter1DSubData(mID, off, count, d);
111 }
112 }
113
114 public Adapter1D createAdapter1D() {
115 int id = mRS.nAdapter1DCreate();
116 if (id != 0) {
117 mRS.nAdapter1DBindAllocation(id, mID);
118 }
119 return new Adapter1D(id, mRS);
120 }
121
122
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700123 public class Adapter2D extends BaseObj {
124 Adapter2D(int id, RenderScript rs) {
125 super(rs);
126 mID = id;
127 }
128
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700129 public void setConstraint(Dimension dim, int value) {
130 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
131 }
132
133 public void data(int[] d) {
134 mRS.nAdapter2DData(mID, d);
135 }
136
137 public void data(float[] d) {
138 mRS.nAdapter2DData(mID, d);
139 }
140
141 public void subData(int xoff, int yoff, int w, int h, int[] d) {
142 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
143 }
144
145 public void subData(int xoff, int yoff, int w, int h, float[] d) {
146 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
147 }
148 }
149
150 public Adapter2D createAdapter2D() {
151 int id = mRS.nAdapter2DCreate();
152 if (id != 0) {
153 mRS.nAdapter2DBindAllocation(id, mID);
154 }
155 return new Adapter2D(id, mRS);
156 }
157
Jason Samsb8c5a842009-07-31 20:40:47 -0700158
159 // creation
160
161 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
162 static {
163 mBitmapOptions.inScaled = false;
164 }
165
Jason Sams1bada8c2009-08-09 17:01:55 -0700166 static public Allocation createTyped(RenderScript rs, Type type)
167 throws IllegalArgumentException {
168
169 if(type.mID == 0) {
170 throw new IllegalStateException("Bad Type");
171 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700172 int id = rs.nAllocationCreateTyped(type.mID);
Jason Sams43ee06852009-08-12 17:54:11 -0700173 return new Allocation(id, rs, type);
Jason Samsb8c5a842009-07-31 20:40:47 -0700174 }
175
Jason Sams1bada8c2009-08-09 17:01:55 -0700176 static public Allocation createSized(RenderScript rs, Element e, int count)
177 throws IllegalArgumentException {
178
Jason Samsb8c5a842009-07-31 20:40:47 -0700179 int id;
180 if(e.mIsPredefined) {
181 id = rs.nAllocationCreatePredefSized(e.mPredefinedID, count);
182 } else {
183 id = rs.nAllocationCreateSized(e.mID, count);
Jason Sams1bada8c2009-08-09 17:01:55 -0700184 if(id == 0) {
185 throw new IllegalStateException("Bad element.");
186 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700187 }
Jason Sams43ee06852009-08-12 17:54:11 -0700188 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700189 }
190
191 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
192 throws IllegalArgumentException {
193 if(!dstFmt.mIsPredefined) {
194 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element.");
195 }
196
197 int id = rs.nAllocationCreateFromBitmap(dstFmt.mPredefinedID, genMips, b);
Jason Sams43ee06852009-08-12 17:54:11 -0700198 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700199 }
200
201 static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
202 throws IllegalArgumentException {
203 if(!dstFmt.mIsPredefined) {
204 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element.");
205 }
206
207 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mPredefinedID, genMips, b);
Jason Sams43ee06852009-08-12 17:54:11 -0700208 return new Allocation(id, rs, null);
Jason Samsb8c5a842009-07-31 20:40:47 -0700209 }
210
211 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
212 throws IllegalArgumentException {
213
214 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
215 return createFromBitmap(rs, b, dstFmt, genMips);
216 }
217
218 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
219 throws IllegalArgumentException {
220
221 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
222 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
223 }
Jason Sams43ee06852009-08-12 17:54:11 -0700224/*
225 public static Allocation createFromObject(RenderScript rs, Object o) {
226 Class c = o.getClass();
227 Type t;
228 if(c.isArray()) {
229 t = Type.createFromClass(rs, c, Array.getLength(o));
230 } else {
231 t = Type.createFromClass(rs, c, 1);
232 }
233 Allocation alloc = createTyped(rs, t);
234 t.destroy();
235 return alloc;
Jason Sams7ce033d2009-08-18 14:14:24 -0700236 }
Jason Sams43ee06852009-08-12 17:54:11 -0700237*/
Jason Samsb8c5a842009-07-31 20:40:47 -0700238}
239
240