Allocation.java revision 5f43fd289abbd5380b6068766daf721b555d0053
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.nAllocationUploadToTexture(mID, baseMipLevel);
44    }
45
46    public void uploadToBufferObject() {
47        mRS.nAllocationUploadToBufferObject(mID);
48    }
49
50    public void data(int[] d) {
51        int size;
52        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);
67    }
68
69    public void data(float[] d) {
70        int size;
71        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);
86    }
87
88    public void subData1D(int off, int count, int[] d) {
89        mRS.nAllocationSubData1D(mID, off, count, d, count * 4);
90    }
91
92    public void subData1D(int off, int count, float[] d) {
93        mRS.nAllocationSubData1D(mID, off, count, d, d.length * 4);
94    }
95
96    public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
97        mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
98    }
99
100    public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
101        mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
102    }
103
104    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
112    public void data(Object o) {
113        mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
114    }
115
116    public void read(Object o) {
117        mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
118    }
119
120    public void subData(int offset, Object o) {
121        mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
122    }
123
124    public class Adapter1D extends BaseObj {
125        Adapter1D(int id, RenderScript rs) {
126            super(rs);
127            mID = id;
128        }
129
130        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
138        public void data(float[] d) {
139            mRS.nAdapter1DData(mID, d);
140        }
141
142        public void subData(int off, int count, int[] d) {
143            mRS.nAdapter1DSubData(mID, off, count, d);
144        }
145
146        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
160    public class Adapter2D extends BaseObj {
161        Adapter2D(int id, RenderScript rs) {
162            super(rs);
163            mID = id;
164        }
165
166        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
195
196    // creation
197
198    private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
199    static {
200        mBitmapOptions.inScaled = false;
201    }
202
203    static public Allocation createTyped(RenderScript rs, Type type)
204        throws IllegalArgumentException {
205
206        if(type.mID == 0) {
207            throw new IllegalStateException("Bad Type");
208        }
209        int id = rs.nAllocationCreateTyped(type.mID);
210        return new Allocation(id, rs, type);
211    }
212
213    static public Allocation createSized(RenderScript rs, Element e, int count)
214        throws IllegalArgumentException {
215
216        int id = rs.nAllocationCreateSized(e.mID, count);
217        if(id == 0) {
218            throw new IllegalStateException("Bad element.");
219        }
220        return new Allocation(id, rs, null);
221    }
222
223    static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
224        throws IllegalArgumentException {
225
226        int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
227        return new Allocation(id, rs, null);
228    }
229
230    static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
231        throws IllegalArgumentException {
232
233        int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
234        return new Allocation(id, rs, null);
235    }
236
237    static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
238        throws IllegalArgumentException {
239
240        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();
246            int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
247                    asset);
248
249            return new Allocation(allocationId, rs, null);
250        } 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;
263    }
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    }
271}
272
273
274