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 android.content.Context;
20import android.content.res.Resources;
21import android.util.Log;
22
23import java.io.File;
24import java.io.IOException;
25import java.io.InputStream;
26import java.util.Map.Entry;
27import java.util.HashMap;
28
29import java.lang.reflect.Field;
30import java.lang.reflect.Modifier;
31
32/**
33 *
34 **/
35class ScriptCThunker extends android.renderscript.ScriptC {
36    private static final String TAG = "ScriptC";
37
38    protected ScriptCThunker(RenderScriptThunker rs, Resources resources, int resourceID) {
39        super(rs.mN, resources, resourceID);
40    }
41
42    android.renderscript.Script.KernelID thunkCreateKernelID(
43            int slot, int sig, Element ein, Element eout) {
44
45        android.renderscript.Element nein = null;
46        android.renderscript.Element neout = null;
47        if (ein != null) {
48            nein = ((ElementThunker)ein).mN;
49        }
50        if (eout != null) {
51            neout = ((ElementThunker)eout).mN;
52        }
53        try {
54            android.renderscript.Script.KernelID kid = createKernelID(slot, sig, nein, neout);
55            return kid;
56        } catch (android.renderscript.RSRuntimeException e) {
57            throw ExceptionThunker.convertException(e);
58        }
59    }
60
61
62    void thunkInvoke(int slot) {
63        try {
64            invoke(slot);
65        } catch (android.renderscript.RSRuntimeException e) {
66            throw ExceptionThunker.convertException(e);
67        }
68    }
69
70    void thunkBindAllocation(Allocation va, int slot) {
71        android.renderscript.Allocation nva = null;
72        if (va != null) {
73            nva = ((AllocationThunker)va).mN;
74        }
75        try {
76            bindAllocation(nva, slot);
77        } catch (android.renderscript.RSRuntimeException e) {
78            throw ExceptionThunker.convertException(e);
79        }
80    }
81
82    void thunkSetTimeZone(String timeZone) {
83        try {
84            setTimeZone(timeZone);
85        } catch (android.renderscript.RSRuntimeException e) {
86            throw ExceptionThunker.convertException(e);
87        }
88    }
89
90    void thunkInvoke(int slot, FieldPacker v) {
91        try {
92            android.renderscript.FieldPacker nfp =
93                new android.renderscript.FieldPacker(v.getData());
94            invoke(slot, nfp);
95        } catch (android.renderscript.RSRuntimeException e) {
96            throw ExceptionThunker.convertException(e);
97        }
98    }
99
100    void thunkForEach(int slot, Allocation ain, Allocation aout, FieldPacker v) {
101        android.renderscript.Allocation nin = null;
102        android.renderscript.Allocation nout = null;
103        android.renderscript.FieldPacker nfp = null;
104        if (ain != null) {
105            nin = ((AllocationThunker)ain).mN;
106        }
107        if (aout != null) {
108            nout = ((AllocationThunker)aout).mN;
109        }
110        try {
111            if (v != null) {
112                nfp = new android.renderscript.FieldPacker(v.getData());
113            }
114            forEach(slot, nin, nout, nfp);
115        } catch (android.renderscript.RSRuntimeException e) {
116            throw ExceptionThunker.convertException(e);
117        }
118    }
119
120    void thunkForEach(int slot, Allocation ain, Allocation aout, FieldPacker v,
121                      android.support.v8.renderscript.Script.LaunchOptions sc) {
122        try {
123            android.renderscript.Script.LaunchOptions lo = null;
124            if (sc != null) {
125                lo = new android.renderscript.Script.LaunchOptions();
126                if (sc.getXEnd() > 0) lo.setX(sc.getXStart(), sc.getXEnd());
127                if (sc.getYEnd() > 0) lo.setY(sc.getYStart(), sc.getYEnd());
128                if (sc.getZEnd() > 0) lo.setZ(sc.getZStart(), sc.getZEnd());
129            }
130
131            android.renderscript.Allocation nin = null;
132            android.renderscript.Allocation nout = null;
133            android.renderscript.FieldPacker nfp = null;
134            if (ain != null) {
135                nin = ((AllocationThunker)ain).mN;
136            }
137            if (aout != null) {
138                nout = ((AllocationThunker)aout).mN;
139            }
140            if (v != null) {
141                nfp = new android.renderscript.FieldPacker(v.getData());
142            }
143            forEach(slot, nin, nout, nfp, lo);
144        } catch (android.renderscript.RSRuntimeException e) {
145            throw ExceptionThunker.convertException(e);
146        }
147    }
148
149    void thunkSetVar(int index, float v) {
150        try {
151            setVar(index, v);
152        } catch (android.renderscript.RSRuntimeException e) {
153            throw ExceptionThunker.convertException(e);
154        }
155    }
156    void thunkSetVar(int index, double v) {
157        try {
158            setVar(index, v);
159        } catch (android.renderscript.RSRuntimeException e) {
160            throw ExceptionThunker.convertException(e);
161        }
162    }
163    void thunkSetVar(int index, int v) {
164        try {
165            setVar(index, v);
166        } catch (android.renderscript.RSRuntimeException e) {
167            throw ExceptionThunker.convertException(e);
168        }
169    }
170    void thunkSetVar(int index, long v) {
171        try {
172            setVar(index, v);
173        } catch (android.renderscript.RSRuntimeException e) {
174            throw ExceptionThunker.convertException(e);
175        }
176    }
177    void thunkSetVar(int index, boolean v) {
178        try {
179            setVar(index, v);
180        } catch (android.renderscript.RSRuntimeException e) {
181            throw ExceptionThunker.convertException(e);
182        }
183    }
184
185    void thunkSetVar(int index, BaseObj o) {
186        if (o == null) {
187            try {
188                setVar(index, 0);
189            } catch (android.renderscript.RSRuntimeException e) {
190                throw ExceptionThunker.convertException(e);
191            }
192            return;
193        }
194        try {
195            setVar(index, o.getNObj());
196        } catch (android.renderscript.RSRuntimeException e) {
197            throw ExceptionThunker.convertException(e);
198        }
199    }
200    void thunkSetVar(int index, FieldPacker v) {
201        try {
202            android.renderscript.FieldPacker nfp =
203                new android.renderscript.FieldPacker(v.getData());
204            setVar(index, nfp);
205        } catch (android.renderscript.RSRuntimeException e) {
206            throw ExceptionThunker.convertException(e);
207        }
208    }
209
210    void thunkSetVar(int index, FieldPacker v, Element e, int[] dims) {
211        try {
212            android.renderscript.FieldPacker nfp =
213                new android.renderscript.FieldPacker(v.getData());
214            ElementThunker et = (ElementThunker)e;
215            setVar(index, nfp, et.mN, dims);
216        } catch (android.renderscript.RSRuntimeException exc) {
217            throw ExceptionThunker.convertException(exc);
218        }
219    }
220
221    android.renderscript.Script.FieldID thunkCreateFieldID(int slot, Element e) {
222        try {
223            ElementThunker et = (ElementThunker) e;
224            return createFieldID(slot, et.getNObj());
225        } catch (android.renderscript.RSRuntimeException exc) {
226            throw ExceptionThunker.convertException(exc);
227        }
228    }
229
230}
231