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