Allocation.java revision 74e02ef2000ff2783e526d6916e2f0b5d517593e
1/*
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
19import java.io.IOException;
20import java.io.InputStream;
21
22import android.content.res.Resources;
23import android.content.res.AssetManager;
24import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
26import android.util.Log;
27import android.util.TypedValue;
28
29/**
30 * @hide
31 *
32 **/
33public class Allocation extends BaseObj {
34    Type mType;
35
36    Allocation(int id, RenderScript rs, Type t) {
37        super(rs);
38        mID = id;
39        mType = t;
40    }
41
42    public void uploadToTexture(int baseMipLevel) {
43        mRS.validate();
44        mRS.validateSurface();
45        mRS.nAllocationUploadToTexture(mID, baseMipLevel);
46    }
47
48    public void uploadToBufferObject() {
49        mRS.validate();
50        mRS.validateSurface();
51        mRS.nAllocationUploadToBufferObject(mID);
52    }
53
54    public void data(int[] d) {
55        mRS.validate();
56        subData1D(0, mType.getElementCount(), d);
57    }
58    public void data(short[] d) {
59        mRS.validate();
60        subData1D(0, mType.getElementCount(), d);
61    }
62    public void data(byte[] d) {
63        mRS.validate();
64        subData1D(0, mType.getElementCount(), d);
65    }
66    public void data(float[] d) {
67        mRS.validate();
68        subData1D(0, mType.getElementCount(), d);
69    }
70
71    private void data1DChecks(int off, int count, int len, int dataSize) {
72        mRS.validate();
73        if((off < 0) || (count < 1) || ((off + count) > mType.getElementCount())) {
74            throw new IllegalArgumentException("Offset or Count out of bounds.");
75        }
76        if((len) < dataSize) {
77            throw new IllegalArgumentException("Array too small for allocation type.");
78        }
79    }
80
81    public void subData1D(int off, int count, int[] d) {
82        int dataSize = mType.mElement.getSizeBytes() * count;
83        data1DChecks(off, count, d.length * 4, dataSize);
84        mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
85    }
86    public void subData1D(int off, int count, short[] d) {
87        int dataSize = mType.mElement.getSizeBytes() * count;
88        data1DChecks(off, count, d.length * 2, dataSize);
89        mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
90    }
91    public void subData1D(int off, int count, byte[] d) {
92        int dataSize = mType.mElement.getSizeBytes() * count;
93        data1DChecks(off, count, d.length, dataSize);
94        mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
95    }
96    public void subData1D(int off, int count, float[] d) {
97        int dataSize = mType.mElement.getSizeBytes() * count;
98        data1DChecks(off, count, d.length * 4, dataSize);
99        mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
100    }
101
102
103
104    public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
105        mRS.validate();
106        mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
107    }
108
109    public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
110        mRS.validate();
111        mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
112    }
113
114    public void readData(int[] d) {
115        mRS.validate();
116        mRS.nAllocationRead(mID, d);
117    }
118
119    public void readData(float[] d) {
120        mRS.validate();
121        mRS.nAllocationRead(mID, d);
122    }
123
124    public void data(Object o) {
125        mRS.validate();
126        mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
127    }
128
129    public void read(Object o) {
130        mRS.validate();
131        mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
132    }
133
134    public void subData(int offset, Object o) {
135        mRS.validate();
136        mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
137    }
138
139    public class Adapter1D extends BaseObj {
140        Adapter1D(int id, RenderScript rs) {
141            super(rs);
142            mID = id;
143        }
144
145        public void setConstraint(Dimension dim, int value) {
146            mRS.validate();
147            mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
148        }
149
150        public void data(int[] d) {
151            mRS.validate();
152            mRS.nAdapter1DData(mID, d);
153        }
154
155        public void data(float[] d) {
156            mRS.validate();
157            mRS.nAdapter1DData(mID, d);
158        }
159
160        public void subData(int off, int count, int[] d) {
161            mRS.validate();
162            mRS.nAdapter1DSubData(mID, off, count, d);
163        }
164
165        public void subData(int off, int count, float[] d) {
166            mRS.validate();
167            mRS.nAdapter1DSubData(mID, off, count, d);
168        }
169    }
170
171    public Adapter1D createAdapter1D() {
172        mRS.validate();
173        int id = mRS.nAdapter1DCreate();
174        if(id == 0) {
175            throw new IllegalStateException("allocation failed.");
176        }
177        mRS.nAdapter1DBindAllocation(id, mID);
178        return new Adapter1D(id, mRS);
179    }
180
181
182    public class Adapter2D extends BaseObj {
183        Adapter2D(int id, RenderScript rs) {
184            super(rs);
185            mID = id;
186        }
187
188        public void setConstraint(Dimension dim, int value) {
189            mRS.validate();
190            mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
191        }
192
193        public void data(int[] d) {
194            mRS.validate();
195            mRS.nAdapter2DData(mID, d);
196        }
197
198        public void data(float[] d) {
199            mRS.validate();
200            mRS.nAdapter2DData(mID, d);
201        }
202
203        public void subData(int xoff, int yoff, int w, int h, int[] d) {
204            mRS.validate();
205            mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
206        }
207
208        public void subData(int xoff, int yoff, int w, int h, float[] d) {
209            mRS.validate();
210            mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
211        }
212    }
213
214    public Adapter2D createAdapter2D() {
215        mRS.validate();
216        int id = mRS.nAdapter2DCreate();
217        if(id == 0) {
218            throw new IllegalStateException("allocation failed.");
219        }
220        mRS.nAdapter2DBindAllocation(id, mID);
221        return new Adapter2D(id, mRS);
222    }
223
224
225    // creation
226
227    private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
228    static {
229        mBitmapOptions.inScaled = false;
230    }
231
232    static public Allocation createTyped(RenderScript rs, Type type)
233        throws IllegalArgumentException {
234
235        rs.validate();
236        if(type.mID == 0) {
237            throw new IllegalStateException("Bad Type");
238        }
239        int id = rs.nAllocationCreateTyped(type.mID);
240        return new Allocation(id, rs, type);
241    }
242
243    static public Allocation createSized(RenderScript rs, Element e, int count)
244        throws IllegalArgumentException {
245
246        rs.validate();
247        Type.Builder b = new Type.Builder(rs, e);
248        b.add(Dimension.X, count);
249        Type t = b.create();
250
251        int id = rs.nAllocationCreateTyped(t.mID);
252        if(id == 0) {
253            throw new IllegalStateException("Bad element.");
254        }
255        return new Allocation(id, rs, t);
256    }
257
258    static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
259        throws IllegalArgumentException {
260
261        rs.validate();
262        int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
263        if(id == 0) {
264            throw new IllegalStateException("Load failed.");
265        }
266        return new Allocation(id, rs, null);
267    }
268
269    static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
270        throws IllegalArgumentException {
271
272        rs.validate();
273        int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
274        if(id == 0) {
275            throw new IllegalStateException("Load failed.");
276        }
277        return new Allocation(id, rs, null);
278    }
279
280    static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
281        throws IllegalArgumentException {
282
283        rs.validate();
284        InputStream is = null;
285        try {
286            final TypedValue value = new TypedValue();
287            is = res.openRawResource(id, value);
288
289            int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
290            int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
291                    asset);
292
293            if(allocationId == 0) {
294                throw new IllegalStateException("Load failed.");
295            }
296            return new Allocation(allocationId, rs, null);
297        } catch (Exception e) {
298            // Ignore
299        } finally {
300            if (is != null) {
301                try {
302                    is.close();
303                } catch (IOException e) {
304                    // Ignore
305                }
306            }
307        }
308
309        return null;
310    }
311
312    static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
313        throws IllegalArgumentException {
314
315        Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
316        return createFromBitmapBoxed(rs, b, dstFmt, genMips);
317    }
318}
319
320
321