AllocationAdapter.java revision ba862d1544a06528151550be1784a926ee986580
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 android.content.res.Resources;
20import android.graphics.Bitmap;
21import android.graphics.BitmapFactory;
22import android.util.TypedValue;
23
24/**
25 *
26 **/
27public class AllocationAdapter extends Allocation {
28    AllocationAdapter(int id, RenderScript rs, Allocation alloc) {
29        super(id, rs, alloc.mType, alloc.mUsage);
30        mAdaptedAllocation = alloc;
31    }
32
33    int getID() {
34        return mAdaptedAllocation.getID();
35    }
36
37    /**
38     * @hide
39     */
40    public void subData(int xoff, FieldPacker fp) {
41        super.setFromFieldPacker(xoff, fp);
42    }
43    /**
44     * @hide
45     */
46    public void subElementData(int xoff, int component_number, FieldPacker fp) {
47        super.setFromFieldPacker(xoff, component_number, fp);
48    }
49    /**
50     * @hide
51     */
52    public void subData1D(int off, int count, int[] d) {
53        super.copy1DRangeFrom(off, count, d);
54    }
55    /**
56     * @hide
57     */
58    public void subData1D(int off, int count, short[] d) {
59        super.copy1DRangeFrom(off, count, d);
60    }
61    /**
62     * @hide
63     */
64    public void subData1D(int off, int count, byte[] d) {
65        super.copy1DRangeFrom(off, count, d);
66    }
67    /**
68     * @hide
69     */
70    public void subData1D(int off, int count, float[] d) {
71        super.copy1DRangeFrom(off, count, d);
72    }
73    /**
74     * @hide
75     */
76    public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
77        super.copy2DRangeFrom(xoff, yoff, w, h, d);
78    }
79    /**
80     * @hide
81     */
82    public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
83        super.copy2DRangeFrom(xoff, yoff, w, h, d);
84    }
85    /**
86     * @hide
87     */
88    public void readData(int[] d) {
89        super.copyTo(d);
90    }
91    /**
92     * @hide
93     */
94    public void readData(float[] d) {
95        super.copyTo(d);
96    }
97
98    void initLOD(int lod) {
99        if (lod < 0) {
100            throw new RSIllegalArgumentException("Attempting to set negative lod (" + lod + ").");
101        }
102
103        int tx = mAdaptedAllocation.mType.getX();
104        int ty = mAdaptedAllocation.mType.getY();
105        int tz = mAdaptedAllocation.mType.getZ();
106
107        for (int ct=0; ct < lod; ct++) {
108            if ((tx==1) && (ty == 1) && (tz == 1)) {
109                throw new RSIllegalArgumentException("Attempting to set lod (" + lod + ") out of range.");
110            }
111
112            if (tx > 1) tx >>= 1;
113            if (ty > 1) ty >>= 1;
114            if (tz > 1) tz >>= 1;
115        }
116
117        mCurrentDimX = tx;
118        mCurrentDimY = ty;
119        mCurrentDimZ = tz;
120        mCurrentCount = mCurrentDimX;
121        if (mCurrentDimY > 1) {
122            mCurrentCount *= mCurrentDimY;
123        }
124        if (mCurrentDimZ > 1) {
125            mCurrentCount *= mCurrentDimZ;
126        }
127        mSelectedY = 0;
128        mSelectedZ = 0;
129    }
130
131    /**
132     * Set the active LOD.  The LOD must be within the range for the
133     * type being adapted.  The base allocation must have mipmaps.
134     *
135     * Because this changes the dimensions of the adapter the
136     * current Y and Z will be reset.
137     *
138     * @param lod The LOD to make active.
139     */
140    public void setLOD(int lod) {
141        if (!mAdaptedAllocation.getType().hasMipmaps()) {
142            throw new RSInvalidStateException("Cannot set LOD when the allocation type does not include mipmaps.");
143        }
144        if (!mConstrainedLOD) {
145            throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps.");
146        }
147
148        initLOD(lod);
149    }
150
151    /**
152     * Set the active Face.  The base allocation must be of a type
153     * that includes faces.
154     *
155     * @param cf The face to make active.
156     */
157    public void setFace(Type.CubemapFace cf) {
158        if (!mAdaptedAllocation.getType().hasFaces()) {
159            throw new RSInvalidStateException("Cannot set Face when the allocation type does not include faces.");
160        }
161        if (!mConstrainedFace) {
162            throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps.");
163        }
164        if (cf == null) {
165            throw new RSIllegalArgumentException("Cannot set null face.");
166        }
167
168        mSelectedFace = cf;
169    }
170
171    /**
172     * Set the active Y.  The y value must be within the range for
173     * the allocation being adapted.  The base allocation must
174     * contain the Y dimension.
175     *
176     * @param y The y to make active.
177     */
178    public void setY(int y) {
179        if (mAdaptedAllocation.getType().getY() == 0) {
180            throw new RSInvalidStateException("Cannot set Y when the allocation type does not include Y dim.");
181        }
182        if (mAdaptedAllocation.getType().getY() <= y) {
183            throw new RSInvalidStateException("Cannot set Y greater than dimension of allocation.");
184        }
185        if (!mConstrainedY) {
186            throw new RSInvalidStateException("Cannot set Y when the adapter includes Y.");
187        }
188
189        mSelectedY = y;
190    }
191
192    /**
193     * Set the active Z.  The z value must be within the range for
194     * the allocation being adapted.  The base allocation must
195     * contain the Z dimension.
196     *
197     * @param z The z to make active.
198     */
199    public void setZ(int z) {
200        if (mAdaptedAllocation.getType().getZ() == 0) {
201            throw new RSInvalidStateException("Cannot set Z when the allocation type does not include Z dim.");
202        }
203        if (mAdaptedAllocation.getType().getZ() <= z) {
204            throw new RSInvalidStateException("Cannot set Z greater than dimension of allocation.");
205        }
206        if (!mConstrainedZ) {
207            throw new RSInvalidStateException("Cannot set Z when the adapter includes Z.");
208        }
209
210        mSelectedZ = z;
211    }
212
213    static public AllocationAdapter create1D(RenderScript rs, Allocation a) {
214        rs.validate();
215        AllocationAdapter aa = new AllocationAdapter(0, rs, a);
216        aa.mConstrainedLOD = true;
217        aa.mConstrainedFace = true;
218        aa.mConstrainedY = true;
219        aa.mConstrainedZ = true;
220        aa.initLOD(0);
221        return aa;
222    }
223
224    static public AllocationAdapter create2D(RenderScript rs, Allocation a) {
225        android.util.Log.e("rs", "create2d " + a);
226        rs.validate();
227        AllocationAdapter aa = new AllocationAdapter(0, rs, a);
228        aa.mConstrainedLOD = true;
229        aa.mConstrainedFace = true;
230        aa.mConstrainedY = false;
231        aa.mConstrainedZ = true;
232        aa.initLOD(0);
233        return aa;
234    }
235
236
237    /**
238     * Override the Allocation resize.  Resizing adapters is not
239     * allowed and will throw a RSInvalidStateException.
240     *
241     * @param dimX ignored.
242     */
243    public synchronized void resize(int dimX) {
244        throw new RSInvalidStateException("Resize not allowed for Adapters.");
245    }
246
247}
248
249
250