Allocation.java revision 43ee06857bb7f99446d1d84f8789016c5d105558
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.lang.reflect.Field;
20import java.lang.reflect.Array;
21
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 {
38    Type mType;
39
40    Allocation(int id, RenderScript rs, Type t) {
41        super(rs);
42        mID = id;
43        mType = t;
44    }
45
46    public void uploadToTexture(int baseMipLevel) {
47        mRS.nAllocationUploadToTexture(mID, baseMipLevel);
48    }
49
50    public void destroy() {
51        if(mDestroyed) {
52            throw new IllegalStateException("Object already destroyed.");
53        }
54        mDestroyed = true;
55        mRS.nAllocationDestroy(mID);
56    }
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
82    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
90    public void data(Object o) {
91        mRS.nAllocationDataFromObject(mID, mType, o);
92    }
93
94
95    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
114        public void data(float[] d) {
115            mRS.nAdapter1DData(mID, d);
116        }
117
118        public void subData(int off, int count, int[] d) {
119            mRS.nAdapter1DSubData(mID, off, count, d);
120        }
121
122        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
136    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
176
177    // creation
178
179    private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
180    static {
181        mBitmapOptions.inScaled = false;
182    }
183
184    static public Allocation createTyped(RenderScript rs, Type type)
185        throws IllegalArgumentException {
186
187        if(type.mID == 0) {
188            throw new IllegalStateException("Bad Type");
189        }
190        int id = rs.nAllocationCreateTyped(type.mID);
191        return new Allocation(id, rs, type);
192    }
193
194    static public Allocation createSized(RenderScript rs, Element e, int count)
195        throws IllegalArgumentException {
196
197        int id;
198        if(e.mIsPredefined) {
199            id = rs.nAllocationCreatePredefSized(e.mPredefinedID, count);
200        } else {
201            id = rs.nAllocationCreateSized(e.mID, count);
202            if(id == 0) {
203                throw new IllegalStateException("Bad element.");
204            }
205        }
206        return new Allocation(id, rs, null);
207    }
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);
216        return new Allocation(id, rs, null);
217    }
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);
226        return new Allocation(id, rs, null);
227    }
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    }
242/*
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*/
256}
257
258
259