1/*
2 * Copyright (C) 2013 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.support.v8.renderscript;
18
19import java.io.IOException;
20import java.io.InputStream;
21import android.content.res.Resources;
22import android.content.res.AssetManager;
23import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
25import android.view.Surface;
26import android.util.Log;
27import android.util.TypedValue;
28
29class AllocationThunker extends Allocation {
30    android.renderscript.Allocation mN;
31    //Allocation mAdaptedAllocation;
32
33    android.renderscript.Allocation getNObj() {
34        return mN;
35    }
36
37    static android.renderscript.Allocation.MipmapControl
38        convertMipmapControl(MipmapControl mc) {
39
40        switch(mc) {
41        case MIPMAP_NONE:
42            return android.renderscript.Allocation.MipmapControl.MIPMAP_NONE;
43        case MIPMAP_FULL:
44            return android.renderscript.Allocation.MipmapControl.MIPMAP_FULL;
45        case MIPMAP_ON_SYNC_TO_TEXTURE:
46            return android.renderscript.Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
47        }
48        return null;
49    }
50
51    public Type getType() {
52        return TypeThunker.find(mN.getType());
53    }
54
55    public Element getElement() {
56        return getType().getElement();
57    }
58
59    public int getUsage() {
60        return mN.getUsage();
61    }
62
63    public int getBytesSize() {
64        return mN.getBytesSize();
65    }
66
67    AllocationThunker(RenderScript rs, Type t, int usage, android.renderscript.Allocation na) {
68        super(0, rs, t, usage);
69
70        mType = t;
71        mUsage = usage;
72        mN = na;
73    }
74
75    public void syncAll(int srcLocation) {
76        mN.syncAll(srcLocation);
77    }
78
79    public void ioSend() {
80        mN.ioSend();
81    }
82
83    public void ioReceive() {
84        mN.ioReceive();
85    }
86
87    public void copyFrom(BaseObj[] d) {
88        if (d == null) {
89            return;
90        }
91        android.renderscript.BaseObj[] dN = new android.renderscript.BaseObj[d.length];
92        for (int i = 0; i < d.length; i++) {
93            dN[i] = d[i].getNObj();
94        }
95        mN.copyFrom(dN);
96    }
97
98    public void copyFromUnchecked(int[] d) {
99        mN.copyFromUnchecked(d);
100    }
101    public void copyFromUnchecked(short[] d) {
102        mN.copyFromUnchecked(d);
103    }
104    public void copyFromUnchecked(byte[] d) {
105        mN.copyFromUnchecked(d);
106    }
107    public void copyFromUnchecked(float[] d) {
108        mN.copyFromUnchecked(d);
109    }
110
111    public void copyFrom(int[] d) {
112        mN.copyFrom(d);
113    }
114    public void copyFrom(short[] d) {
115        mN.copyFrom(d);
116    }
117    public void copyFrom(byte[] d) {
118        mN.copyFrom(d);
119    }
120    public void copyFrom(float[] d) {
121        mN.copyFrom(d);
122    }
123    public void copyFrom(Bitmap b) {
124        mN.copyFrom(b);
125    }
126    public void copyFrom(Allocation a) {
127        AllocationThunker at = (AllocationThunker)a;
128        mN.copyFrom(at.mN);
129    }
130
131
132    public void setFromFieldPacker(int xoff, FieldPacker fp) {
133        android.renderscript.FieldPacker nfp =
134            new android.renderscript.FieldPacker(fp.getData());
135        mN.setFromFieldPacker(xoff, nfp);
136    }
137    public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
138        android.renderscript.FieldPacker nfp =
139            new android.renderscript.FieldPacker(fp.getData());
140        mN.setFromFieldPacker(xoff, component_number, nfp);
141    }
142
143    public void generateMipmaps() {
144        mN.generateMipmaps();
145    }
146
147    public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
148        mN.copy1DRangeFromUnchecked(off, count, d);
149    }
150    public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
151        mN.copy1DRangeFromUnchecked(off, count, d);
152    }
153    public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
154        mN.copy1DRangeFromUnchecked(off, count, d);
155    }
156    public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
157        mN.copy1DRangeFromUnchecked(off, count, d);
158    }
159
160    public void copy1DRangeFrom(int off, int count, int[] d) {
161        mN.copy1DRangeFrom(off, count, d);
162    }
163    public void copy1DRangeFrom(int off, int count, short[] d) {
164        mN.copy1DRangeFrom(off, count, d);
165    }
166    public void copy1DRangeFrom(int off, int count, byte[] d) {
167        mN.copy1DRangeFrom(off, count, d);
168    }
169    public void copy1DRangeFrom(int off, int count, float[] d) {
170        mN.copy1DRangeFrom(off, count, d);
171    }
172
173    public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
174        AllocationThunker at = (AllocationThunker)data;
175        mN.copy1DRangeFrom(off, count, at.mN, dataOff);
176    }
177
178    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
179        mN.copy2DRangeFrom(xoff, yoff, w, h, data);
180    }
181    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
182        mN.copy2DRangeFrom(xoff, yoff, w, h, data);
183    }
184    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
185        mN.copy2DRangeFrom(xoff, yoff, w, h, data);
186    }
187    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
188        mN.copy2DRangeFrom(xoff, yoff, w, h, data);
189    }
190
191    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
192                                Allocation data, int dataXoff, int dataYoff) {
193        AllocationThunker at = (AllocationThunker)data;
194        mN.copy2DRangeFrom(xoff, yoff, w, h, at.mN, dataXoff, dataYoff);
195    }
196    public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
197        mN.copy2DRangeFrom(xoff, yoff, data);
198    }
199
200
201    public void copyTo(Bitmap b) {
202        mN.copyTo(b);
203    }
204    public void copyTo(byte[] d) {
205        mN.copyTo(d);
206    }
207    public void copyTo(short[] d) {
208        mN.copyTo(d);
209    }
210    public void copyTo(int[] d) {
211        mN.copyTo(d);
212    }
213    public void copyTo(float[] d) {
214        mN.copyTo(d);
215    }
216
217    // creation
218
219    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
220    static {
221        mBitmapOptions.inScaled = false;
222    }
223
224    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
225        RenderScriptThunker rst = (RenderScriptThunker)rs;
226        TypeThunker tt = (TypeThunker)type;
227
228        android.renderscript.Allocation a =
229            android.renderscript.Allocation.createTyped(rst.mN, tt.mN,
230                                                        convertMipmapControl(mips), usage);
231
232        return new AllocationThunker(rs, type, usage, a);
233    }
234
235    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
236                                              MipmapControl mips,
237                                              int usage) {
238
239        RenderScriptThunker rst = (RenderScriptThunker)rs;
240        android.renderscript.Allocation a =
241                android.renderscript.Allocation.createFromBitmap(
242                rst.mN, b, convertMipmapControl(mips), usage);
243        TypeThunker tt = new TypeThunker(rs, a.getType());
244
245        return new AllocationThunker(rs, tt, usage, a);
246    }
247
248    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
249                                                     MipmapControl mips,
250                                                     int usage) {
251        RenderScriptThunker rst = (RenderScriptThunker)rs;
252        android.renderscript.Allocation a =
253                android.renderscript.Allocation.createCubemapFromBitmap(
254                rst.mN, b, convertMipmapControl(mips), usage);
255        TypeThunker tt = new TypeThunker(rs, a.getType());
256        return new AllocationThunker(rs, tt, usage, a);
257    }
258
259    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
260                                                        Bitmap xpos,
261                                                        Bitmap xneg,
262                                                        Bitmap ypos,
263                                                        Bitmap yneg,
264                                                        Bitmap zpos,
265                                                        Bitmap zneg,
266                                                        MipmapControl mips,
267                                                        int usage) {
268        RenderScriptThunker rst = (RenderScriptThunker)rs;
269        android.renderscript.Allocation a =
270                android.renderscript.Allocation.createCubemapFromCubeFaces(
271                rst.mN, xpos, xneg, ypos, yneg, zpos, zneg,
272                convertMipmapControl(mips), usage);
273        TypeThunker tt = new TypeThunker(rs, a.getType());
274        return new AllocationThunker(rs, tt, usage, a);
275    }
276
277    static public Allocation createFromBitmapResource(RenderScript rs,
278                                                      Resources res,
279                                                      int id,
280                                                      MipmapControl mips,
281                                                      int usage) {
282
283        RenderScriptThunker rst = (RenderScriptThunker)rs;
284        android.renderscript.Allocation a =
285                android.renderscript.Allocation.createFromBitmapResource(
286                rst.mN, res, id, convertMipmapControl(mips), usage);
287        TypeThunker tt = new TypeThunker(rs, a.getType());
288        return new AllocationThunker(rs, tt, usage, a);
289    }
290
291    static public Allocation createFromString(RenderScript rs,
292                                              String str,
293                                              int usage) {
294        RenderScriptThunker rst = (RenderScriptThunker)rs;
295        android.renderscript.Allocation a =
296                android.renderscript.Allocation.createFromString(
297                rst.mN, str, usage);
298        TypeThunker tt = new TypeThunker(rs, a.getType());
299        return new AllocationThunker(rs, tt, usage, a);
300    }
301
302    static public Allocation createSized(RenderScript rs, Element e,
303                                         int count, int usage) {
304        RenderScriptThunker rst = (RenderScriptThunker)rs;
305        ElementThunker et = (ElementThunker) e;
306        android.renderscript.Allocation a =
307            android.renderscript.Allocation.createSized(rst.mN, (android.renderscript.Element)e.getNObj(), count, usage);
308        TypeThunker tt = new TypeThunker(rs, a.getType());
309        return new AllocationThunker(rs, tt, usage, a);
310    }
311
312}