Allocation.java revision 771bebb94054d06f97284379c93a2620613513c3
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            mRS.nAdapter1DBindAllocation(id, mID);
176        }
177        return new Adapter1D(id, mRS);
178    }
179
180
181    public class Adapter2D extends BaseObj {
182        Adapter2D(int id, RenderScript rs) {
183            super(rs);
184            mID = id;
185        }
186
187        public void setConstraint(Dimension dim, int value) {
188            mRS.validate();
189            mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
190        }
191
192        public void data(int[] d) {
193            mRS.validate();
194            mRS.nAdapter2DData(mID, d);
195        }
196
197        public void data(float[] d) {
198            mRS.validate();
199            mRS.nAdapter2DData(mID, d);
200        }
201
202        public void subData(int xoff, int yoff, int w, int h, int[] d) {
203            mRS.validate();
204            mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
205        }
206
207        public void subData(int xoff, int yoff, int w, int h, float[] d) {
208            mRS.validate();
209            mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
210        }
211    }
212
213    public Adapter2D createAdapter2D() {
214        mRS.validate();
215        int id = mRS.nAdapter2DCreate();
216        if (id != 0) {
217            mRS.nAdapter2DBindAllocation(id, mID);
218        }
219        return new Adapter2D(id, mRS);
220    }
221
222
223    // creation
224
225    private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
226    static {
227        mBitmapOptions.inScaled = false;
228    }
229
230    static public Allocation createTyped(RenderScript rs, Type type)
231        throws IllegalArgumentException {
232
233        rs.validate();
234        if(type.mID == 0) {
235            throw new IllegalStateException("Bad Type");
236        }
237        int id = rs.nAllocationCreateTyped(type.mID);
238        return new Allocation(id, rs, type);
239    }
240
241    static public Allocation createSized(RenderScript rs, Element e, int count)
242        throws IllegalArgumentException {
243
244        rs.validate();
245        Type.Builder b = new Type.Builder(rs, e);
246        b.add(Dimension.X, count);
247        Type t = b.create();
248
249        int id = rs.nAllocationCreateTyped(t.mID);
250        if(id == 0) {
251            throw new IllegalStateException("Bad element.");
252        }
253        return new Allocation(id, rs, t);
254    }
255
256    static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
257        throws IllegalArgumentException {
258
259        rs.validate();
260        int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
261        return new Allocation(id, rs, null);
262    }
263
264    static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
265        throws IllegalArgumentException {
266
267        rs.validate();
268        int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
269        return new Allocation(id, rs, null);
270    }
271
272    static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
273        throws IllegalArgumentException {
274
275        rs.validate();
276        InputStream is = null;
277        try {
278            final TypedValue value = new TypedValue();
279            is = res.openRawResource(id, value);
280
281            int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
282            int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
283                    asset);
284
285            return new Allocation(allocationId, rs, null);
286        } catch (Exception e) {
287            // Ignore
288        } finally {
289            if (is != null) {
290                try {
291                    is.close();
292                } catch (IOException e) {
293                    // Ignore
294                }
295            }
296        }
297
298        return null;
299    }
300
301    static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
302        throws IllegalArgumentException {
303
304        Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
305        return createFromBitmapBoxed(rs, b, dstFmt, genMips);
306    }
307}
308
309
310