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        try {
61            return mN.getUsage();
62        } catch (android.renderscript.RSRuntimeException e) {
63            throw ExceptionThunker.convertException(e);
64        }
65    }
66
67    public int getBytesSize() {
68        try {
69            return mN.getBytesSize();
70        } catch (android.renderscript.RSRuntimeException e) {
71            throw ExceptionThunker.convertException(e);
72        }
73    }
74
75    AllocationThunker(RenderScript rs, Type t, int usage, android.renderscript.Allocation na) {
76        super(0, rs, t, usage);
77
78        mType = t;
79        mUsage = usage;
80        mN = na;
81    }
82
83    public void syncAll(int srcLocation) {
84        try {
85            mN.syncAll(srcLocation);
86        } catch (android.renderscript.RSRuntimeException e) {
87            throw ExceptionThunker.convertException(e);
88        }
89    }
90
91    public void ioSend() {
92        try {
93            mN.ioSend();
94        } catch (android.renderscript.RSRuntimeException e) {
95            throw ExceptionThunker.convertException(e);
96        }
97    }
98
99    public void ioReceive() {
100        try {
101            mN.ioReceive();
102        } catch (android.renderscript.RSRuntimeException e) {
103            throw ExceptionThunker.convertException(e);
104        }
105    }
106
107    public void copyFrom(BaseObj[] d) {
108        if (d == null) {
109            return;
110        }
111        android.renderscript.BaseObj[] dN = new android.renderscript.BaseObj[d.length];
112        for (int i = 0; i < d.length; i++) {
113            dN[i] = d[i].getNObj();
114        }
115        try {
116            mN.copyFrom(dN);
117        } catch (android.renderscript.RSRuntimeException e) {
118            throw ExceptionThunker.convertException(e);
119        }
120    }
121
122    public void copyFromUnchecked(int[] d) {
123        try {
124            mN.copyFromUnchecked(d);
125        } catch (android.renderscript.RSRuntimeException e) {
126            throw ExceptionThunker.convertException(e);
127        }
128    }
129    public void copyFromUnchecked(short[] d) {
130        try {
131            mN.copyFromUnchecked(d);
132        } catch (android.renderscript.RSRuntimeException e) {
133            throw ExceptionThunker.convertException(e);
134        }
135    }
136    public void copyFromUnchecked(byte[] d) {
137        try {
138            mN.copyFromUnchecked(d);
139        } catch (android.renderscript.RSRuntimeException e) {
140            throw ExceptionThunker.convertException(e);
141        }
142    }
143    public void copyFromUnchecked(float[] d) {
144        try {
145            mN.copyFromUnchecked(d);
146        } catch (android.renderscript.RSRuntimeException e) {
147            throw ExceptionThunker.convertException(e);
148        }
149    }
150
151    public void copyFrom(int[] d) {
152        try {
153            mN.copyFrom(d);
154        } catch (android.renderscript.RSRuntimeException e) {
155            throw ExceptionThunker.convertException(e);
156        }
157    }
158    public void copyFrom(short[] d) {
159        try {
160            mN.copyFrom(d);
161        } catch (android.renderscript.RSRuntimeException e) {
162            throw ExceptionThunker.convertException(e);
163        }
164    }
165    public void copyFrom(byte[] d) {
166        try {
167            mN.copyFrom(d);
168        } catch (android.renderscript.RSRuntimeException e) {
169            throw ExceptionThunker.convertException(e);
170        }
171    }
172    public void copyFrom(float[] d) {
173        try {
174            mN.copyFrom(d);
175        } catch (android.renderscript.RSRuntimeException e) {
176            throw ExceptionThunker.convertException(e);
177        }
178    }
179    public void copyFrom(Bitmap b) {
180        try {
181            mN.copyFrom(b);
182        } catch (android.renderscript.RSRuntimeException e) {
183            throw ExceptionThunker.convertException(e);
184        }
185    }
186    public void copyFrom(Allocation a) {
187        AllocationThunker at = (AllocationThunker)a;
188        try {
189            mN.copyFrom(at.mN);
190        } catch (android.renderscript.RSRuntimeException e) {
191            throw ExceptionThunker.convertException(e);
192        }
193    }
194
195
196    public void setFromFieldPacker(int xoff, FieldPacker fp) {
197        try {
198            // Must construct actual FieldPacker from scratch, since we don't
199            // know how many bytes were actually used.
200            byte[] data = fp.getData();
201            int fp_length = fp.getPos();
202            android.renderscript.FieldPacker nfp =
203                new android.renderscript.FieldPacker(fp_length);
204            for (int i = 0; i < fp_length; i++) {
205                nfp.addI8(data[i]);
206            }
207            mN.setFromFieldPacker(xoff, nfp);
208        } catch (android.renderscript.RSRuntimeException e) {
209            throw ExceptionThunker.convertException(e);
210        }
211    }
212    public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
213        try {
214            // Must construct actual FieldPacker from scratch, since we don't
215            // know how many bytes were actually used.
216            byte[] data = fp.getData();
217            int fp_length = fp.getPos();
218            android.renderscript.FieldPacker nfp =
219                new android.renderscript.FieldPacker(fp_length);
220            for (int i = 0; i < fp_length; i++) {
221                nfp.addI8(data[i]);
222            }
223            mN.setFromFieldPacker(xoff, component_number, nfp);
224        } catch (android.renderscript.RSRuntimeException e) {
225            throw ExceptionThunker.convertException(e);
226        }
227    }
228
229    public void generateMipmaps() {
230        try {
231            mN.generateMipmaps();
232        } catch (android.renderscript.RSRuntimeException e) {
233            throw ExceptionThunker.convertException(e);
234        }
235    }
236
237    public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
238        try {
239            mN.copy1DRangeFromUnchecked(off, count, d);
240        } catch (android.renderscript.RSRuntimeException e) {
241            throw ExceptionThunker.convertException(e);
242        }
243    }
244    public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
245        try {
246            mN.copy1DRangeFromUnchecked(off, count, d);
247        } catch (android.renderscript.RSRuntimeException e) {
248            throw ExceptionThunker.convertException(e);
249        }
250    }
251    public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
252        try {
253            mN.copy1DRangeFromUnchecked(off, count, d);
254        } catch (android.renderscript.RSRuntimeException e) {
255            throw ExceptionThunker.convertException(e);
256        }
257    }
258    public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
259        try {
260            mN.copy1DRangeFromUnchecked(off, count, d);
261        } catch (android.renderscript.RSRuntimeException e) {
262            throw ExceptionThunker.convertException(e);
263        }
264    }
265
266    public void copy1DRangeFrom(int off, int count, int[] d) {
267        try {
268            mN.copy1DRangeFrom(off, count, d);
269        } catch (android.renderscript.RSRuntimeException e) {
270            throw ExceptionThunker.convertException(e);
271        }
272    }
273    public void copy1DRangeFrom(int off, int count, short[] d) {
274        try {
275            mN.copy1DRangeFrom(off, count, d);
276        } catch (android.renderscript.RSRuntimeException e) {
277            throw ExceptionThunker.convertException(e);
278        }
279    }
280    public void copy1DRangeFrom(int off, int count, byte[] d) {
281        try {
282            mN.copy1DRangeFrom(off, count, d);
283        } catch (android.renderscript.RSRuntimeException e) {
284            throw ExceptionThunker.convertException(e);
285        }
286    }
287    public void copy1DRangeFrom(int off, int count, float[] d) {
288        try {
289            mN.copy1DRangeFrom(off, count, d);
290        } catch (android.renderscript.RSRuntimeException e) {
291            throw ExceptionThunker.convertException(e);
292        }
293    }
294
295    public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
296        try {
297            AllocationThunker at = (AllocationThunker)data;
298            mN.copy1DRangeFrom(off, count, at.mN, dataOff);
299        } catch (android.renderscript.RSRuntimeException e) {
300            throw ExceptionThunker.convertException(e);
301        }
302    }
303
304    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
305        try {
306            mN.copy2DRangeFrom(xoff, yoff, w, h, data);
307        } catch (android.renderscript.RSRuntimeException e) {
308            throw ExceptionThunker.convertException(e);
309        }
310    }
311    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
312        try {
313            mN.copy2DRangeFrom(xoff, yoff, w, h, data);
314        } catch (android.renderscript.RSRuntimeException e) {
315            throw ExceptionThunker.convertException(e);
316        }
317    }
318    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
319        try {
320            mN.copy2DRangeFrom(xoff, yoff, w, h, data);
321        } catch (android.renderscript.RSRuntimeException e) {
322            throw ExceptionThunker.convertException(e);
323        }
324    }
325    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
326        try {
327            mN.copy2DRangeFrom(xoff, yoff, w, h, data);
328        } catch (android.renderscript.RSRuntimeException e) {
329            throw ExceptionThunker.convertException(e);
330        }
331    }
332
333    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
334                                Allocation data, int dataXoff, int dataYoff) {
335        try {
336            AllocationThunker at = (AllocationThunker)data;
337            mN.copy2DRangeFrom(xoff, yoff, w, h, at.mN, dataXoff, dataYoff);
338        } catch (android.renderscript.RSRuntimeException e) {
339            throw ExceptionThunker.convertException(e);
340        }
341    }
342    public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
343        try {
344            mN.copy2DRangeFrom(xoff, yoff, data);
345        } catch (android.renderscript.RSRuntimeException e) {
346            throw ExceptionThunker.convertException(e);
347        }
348    }
349
350
351    public void copyTo(Bitmap b) {
352        try {
353            mN.copyTo(b);
354        } catch (android.renderscript.RSRuntimeException e) {
355            throw ExceptionThunker.convertException(e);
356        }
357    }
358    public void copyTo(byte[] d) {
359        try {
360            mN.copyTo(d);
361        } catch (android.renderscript.RSRuntimeException e) {
362            throw ExceptionThunker.convertException(e);
363        }
364    }
365    public void copyTo(short[] d) {
366        try {
367            mN.copyTo(d);
368        } catch (android.renderscript.RSRuntimeException e) {
369            throw ExceptionThunker.convertException(e);
370        }
371    }
372    public void copyTo(int[] d) {
373        try {
374            mN.copyTo(d);
375        } catch (android.renderscript.RSRuntimeException e) {
376            throw ExceptionThunker.convertException(e);
377        }
378    }
379    public void copyTo(float[] d) {
380        try {
381            mN.copyTo(d);
382        } catch (android.renderscript.RSRuntimeException e) {
383            throw ExceptionThunker.convertException(e);
384        }
385    }
386
387    // creation
388
389    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
390    static {
391        mBitmapOptions.inScaled = false;
392    }
393
394    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
395        RenderScriptThunker rst = (RenderScriptThunker)rs;
396        TypeThunker tt = (TypeThunker)type;
397
398        try {
399            android.renderscript.Allocation a =
400                android.renderscript.Allocation.createTyped(rst.mN, tt.mN,
401                                                            convertMipmapControl(mips),
402                                                            usage);
403            return new AllocationThunker(rs, type, usage, a);
404        } catch (android.renderscript.RSRuntimeException e) {
405            throw ExceptionThunker.convertException(e);
406        }
407    }
408
409    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
410                                              MipmapControl mips,
411                                              int usage) {
412
413        RenderScriptThunker rst = (RenderScriptThunker)rs;
414        try {
415            android.renderscript.Allocation a =
416                android.renderscript.Allocation.createFromBitmap(rst.mN, b,
417                                                                 convertMipmapControl(mips),
418                                                                 usage);
419            TypeThunker tt = new TypeThunker(rs, a.getType());
420            return new AllocationThunker(rs, tt, usage, a);
421
422        } catch (android.renderscript.RSRuntimeException e) {
423            throw ExceptionThunker.convertException(e);
424        }
425    }
426
427    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
428                                                     MipmapControl mips,
429                                                     int usage) {
430        RenderScriptThunker rst = (RenderScriptThunker)rs;
431        try {
432            android.renderscript.Allocation a =
433                    android.renderscript.Allocation.createCubemapFromBitmap(
434                    rst.mN, b, convertMipmapControl(mips), usage);
435            TypeThunker tt = new TypeThunker(rs, a.getType());
436            return new AllocationThunker(rs, tt, usage, a);
437        } catch (android.renderscript.RSRuntimeException e) {
438            throw ExceptionThunker.convertException(e);
439        }
440    }
441
442    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
443                                                        Bitmap xpos,
444                                                        Bitmap xneg,
445                                                        Bitmap ypos,
446                                                        Bitmap yneg,
447                                                        Bitmap zpos,
448                                                        Bitmap zneg,
449                                                        MipmapControl mips,
450                                                        int usage) {
451        RenderScriptThunker rst = (RenderScriptThunker)rs;
452        try {
453            android.renderscript.Allocation a =
454                    android.renderscript.Allocation.createCubemapFromCubeFaces(
455                    rst.mN, xpos, xneg, ypos, yneg, zpos, zneg,
456                    convertMipmapControl(mips), usage);
457            TypeThunker tt = new TypeThunker(rs, a.getType());
458            return new AllocationThunker(rs, tt, usage, a);
459        } catch (android.renderscript.RSRuntimeException e) {
460            throw ExceptionThunker.convertException(e);
461        }
462    }
463
464    static public Allocation createFromBitmapResource(RenderScript rs,
465                                                      Resources res,
466                                                      int id,
467                                                      MipmapControl mips,
468                                                      int usage) {
469
470        RenderScriptThunker rst = (RenderScriptThunker)rs;
471        try {
472            android.renderscript.Allocation a =
473                    android.renderscript.Allocation.createFromBitmapResource(
474                    rst.mN, res, id, convertMipmapControl(mips), usage);
475            TypeThunker tt = new TypeThunker(rs, a.getType());
476            return new AllocationThunker(rs, tt, usage, a);
477        } catch (android.renderscript.RSRuntimeException e) {
478            throw ExceptionThunker.convertException(e);
479        }
480    }
481
482    static public Allocation createFromString(RenderScript rs,
483                                              String str,
484                                              int usage) {
485        RenderScriptThunker rst = (RenderScriptThunker)rs;
486        try {
487            android.renderscript.Allocation a =
488                    android.renderscript.Allocation.createFromString(
489                    rst.mN, str, usage);
490            TypeThunker tt = new TypeThunker(rs, a.getType());
491            return new AllocationThunker(rs, tt, usage, a);
492        } catch (android.renderscript.RSRuntimeException e) {
493            throw ExceptionThunker.convertException(e);
494        }
495    }
496
497    static public Allocation createSized(RenderScript rs, Element e,
498                                         int count, int usage) {
499        RenderScriptThunker rst = (RenderScriptThunker)rs;
500        ElementThunker et = (ElementThunker) e;
501        try {
502            android.renderscript.Allocation a =
503                android.renderscript.Allocation.createSized
504                (rst.mN, (android.renderscript.Element)e.getNObj(), count, usage);
505            TypeThunker tt = new TypeThunker(rs, a.getType());
506            return new AllocationThunker(rs, tt, usage, a);
507        } catch (android.renderscript.RSRuntimeException exc) {
508            throw ExceptionThunker.convertException(exc);
509        }
510    }
511
512}
513