Script.java revision c89ad4606ae29e103b85bdfc40c57e36c8877dba
1/*
2 * Copyright (C) 2012 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.util.SparseArray;
20
21/**
22 *
23 **/
24public class Script extends BaseObj {
25    ScriptCThunker mT;
26
27    android.renderscript.Script getNObj() {
28        return mT;
29    }
30
31
32    /**
33     * KernelID is an identifier for a Script + root function pair. It is used
34     * as an identifier for ScriptGroup creation.
35     *
36     * This class should not be directly created. Instead use the method in the
37     * reflected or intrinsic code "getKernelID_funcname()".
38     *
39     */
40    public static final class KernelID extends BaseObj {
41        android.renderscript.Script.KernelID mN;
42        Script mScript;
43        int mSlot;
44        int mSig;
45        KernelID(int id, RenderScript rs, Script s, int slot, int sig) {
46            super(id, rs);
47            mScript = s;
48            mSlot = slot;
49            mSig = sig;
50        }
51    }
52
53    private final SparseArray<KernelID> mKIDs = new SparseArray<KernelID>();
54    /**
55     * Only to be used by generated reflected classes.
56     *
57     *
58     * @param slot
59     * @param sig
60     * @param ein
61     * @param eout
62     *
63     * @return KernelID
64     */
65    protected KernelID createKernelID(int slot, int sig, Element ein, Element eout) {
66        KernelID k = mKIDs.get(slot);
67        if (k != null) {
68            return k;
69        }
70
71        // Any native callers to createKernelID must initialize their own native IDs
72        // excpet ScriptCThunker
73        if (mRS.isNative == true) {
74            k = new KernelID(0, mRS, this, slot, sig);
75            if (mT != null) {
76                k.mN = mT.thunkCreateKernelID(slot, sig, ein, eout);
77            }
78            mKIDs.put(slot, k);
79            return k;
80        }
81
82
83        int id = mRS.nScriptKernelIDCreate(getID(mRS), slot, sig);
84        if (id == 0) {
85            throw new RSDriverException("Failed to create KernelID");
86        }
87
88        k = new KernelID(id, mRS, this, slot, sig);
89
90        mKIDs.put(slot, k);
91        return k;
92    }
93
94    /**
95     * FieldID is an identifier for a Script + exported field pair. It is used
96     * as an identifier for ScriptGroup creation.
97     *
98     * This class should not be directly created. Instead use the method in the
99     * reflected or intrinsic code "getFieldID_funcname()".
100     *
101     */
102    public static final class FieldID extends BaseObj {
103        android.renderscript.Script.FieldID mN;
104        Script mScript;
105        int mSlot;
106        FieldID(int id, RenderScript rs, Script s, int slot) {
107            super(id, rs);
108            mScript = s;
109            mSlot = slot;
110        }
111    }
112
113    private final SparseArray<FieldID> mFIDs = new SparseArray();
114    /**
115     * Only to be used by generated reflected classes.
116     *
117     * @param slot
118     * @param e
119     *
120     * @return FieldID
121     */
122    protected FieldID createFieldID(int slot, Element e) {
123
124        // Any thunking caller to createFieldID must create its own native IDs
125        // except ScriptC
126        if (mRS.isNative == true) {
127            FieldID f = new FieldID(0, mRS, this, slot);
128            if (mT != null) {
129                f.mN = mT.thunkCreateFieldID(slot, e);
130            }
131            mFIDs.put(slot, f);
132            return f;
133        }
134        FieldID f = mFIDs.get(slot);
135        if (f != null) {
136            return f;
137        }
138
139        int id = mRS.nScriptFieldIDCreate(getID(mRS), slot);
140        if (id == 0) {
141            throw new RSDriverException("Failed to create FieldID");
142        }
143
144        f = new FieldID(id, mRS, this, slot);
145        mFIDs.put(slot, f);
146        return f;
147    }
148
149
150    /**
151     * Only intended for use by generated reflected code.
152     *
153     * @param slot
154     */
155    protected void invoke(int slot) {
156        if (mT != null) {
157            mT.thunkInvoke(slot);
158            return;
159        }
160
161        mRS.nScriptInvoke(getID(mRS), slot);
162    }
163
164    /**
165     * Only intended for use by generated reflected code.
166     *
167     * @param slot
168     * @param v
169     */
170    protected void invoke(int slot, FieldPacker v) {
171        if (mT != null) {
172            mT.thunkInvoke(slot, v);
173            return;
174        }
175
176        if (v != null) {
177            mRS.nScriptInvokeV(getID(mRS), slot, v.getData());
178        } else {
179            mRS.nScriptInvoke(getID(mRS), slot);
180        }
181    }
182
183    /**
184     * Only intended for use by generated reflected code.
185     *
186     * @param va
187     * @param slot
188     */
189    public void bindAllocation(Allocation va, int slot) {
190        if (mT != null) {
191            mT.thunkBindAllocation(va, slot);
192            return;
193        }
194
195        mRS.validate();
196        if (va != null) {
197            mRS.nScriptBindAllocation(getID(mRS), va.getID(mRS), slot);
198        } else {
199            mRS.nScriptBindAllocation(getID(mRS), 0, slot);
200        }
201    }
202
203    public void setTimeZone(String timeZone) {
204        if (mT != null) {
205            mT.thunkSetTimeZone(timeZone);
206            return;
207        }
208
209        mRS.validate();
210        try {
211            mRS.nScriptSetTimeZone(getID(mRS), timeZone.getBytes("UTF-8"));
212        } catch (java.io.UnsupportedEncodingException e) {
213            throw new RuntimeException(e);
214        }
215    }
216
217
218    /**
219     * Only intended for use by generated reflected code.
220     *
221     * @param slot
222     * @param ain
223     * @param aout
224     * @param v
225     */
226    protected void forEach(int slot, Allocation ain, Allocation aout, FieldPacker v) {
227        if (mT != null) {
228            mT.thunkForEach(slot, ain, aout, v);
229            return;
230        }
231
232        if (ain == null && aout == null) {
233            throw new RSIllegalArgumentException(
234                "At least one of ain or aout is required to be non-null.");
235        }
236        int in_id = 0;
237        if (ain != null) {
238            in_id = ain.getID(mRS);
239        }
240        int out_id = 0;
241        if (aout != null) {
242            out_id = aout.getID(mRS);
243        }
244        byte[] params = null;
245        if (v != null) {
246            params = v.getData();
247        }
248        mRS.nScriptForEach(getID(mRS), slot, in_id, out_id, params);
249    }
250
251    protected void forEach(int slot, Allocation ain, Allocation aout, FieldPacker v, LaunchOptions sc) {
252        if (mT != null) {
253            mT.thunkForEach(slot, ain, aout, v, sc);
254            return;
255        }
256
257        if (ain == null && aout == null) {
258            throw new RSIllegalArgumentException(
259                "At least one of ain or aout is required to be non-null.");
260        }
261
262        if (sc == null) {
263            forEach(slot, ain, aout, v);
264            return;
265        }
266        int in_id = 0;
267        if (ain != null) {
268            in_id = ain.getID(mRS);
269        }
270        int out_id = 0;
271        if (aout != null) {
272            out_id = aout.getID(mRS);
273        }
274        byte[] params = null;
275        if (v != null) {
276            params = v.getData();
277        }
278        mRS.nScriptForEachClipped(getID(mRS), slot, in_id, out_id, params, sc.xstart, sc.xend, sc.ystart, sc.yend, sc.zstart, sc.zend);
279    }
280
281    Script(int id, RenderScript rs) {
282        super(id, rs);
283    }
284
285    /**
286     * Only intended for use by generated reflected code.
287     *
288     * @param index
289     * @param v
290     */
291    public void setVar(int index, float v) {
292        if (mT != null) {
293            mT.thunkSetVar(index, v);
294            return;
295        }
296
297        mRS.nScriptSetVarF(getID(mRS), index, v);
298    }
299
300    /**
301     * Only intended for use by generated reflected code.
302     *
303     * @param index
304     * @param v
305     */
306    public void setVar(int index, double v) {
307        if (mT != null) {
308            mT.thunkSetVar(index, v);
309            return;
310        }
311
312        mRS.nScriptSetVarD(getID(mRS), index, v);
313    }
314
315    /**
316     * Only intended for use by generated reflected code.
317     *
318     * @param index
319     * @param v
320     */
321    public void setVar(int index, int v) {
322        if (mT != null) {
323            mT.thunkSetVar(index, v);
324            return;
325        }
326
327        mRS.nScriptSetVarI(getID(mRS), index, v);
328    }
329
330    /**
331     * Only intended for use by generated reflected code.
332     *
333     * @param index
334     * @param v
335     */
336    public void setVar(int index, long v) {
337        if (mT != null) {
338            mT.thunkSetVar(index, v);
339            return;
340        }
341
342        mRS.nScriptSetVarJ(getID(mRS), index, v);
343    }
344
345    /**
346     * Only intended for use by generated reflected code.
347     *
348     * @param index
349     * @param v
350     */
351    public void setVar(int index, boolean v) {
352        if (mT != null) {
353            mT.thunkSetVar(index, v);
354            return;
355        }
356
357        mRS.nScriptSetVarI(getID(mRS), index, v ? 1 : 0);
358    }
359
360    /**
361     * Only intended for use by generated reflected code.
362     *
363     * @param index
364     * @param o
365     */
366    public void setVar(int index, BaseObj o) {
367        if (mT != null) {
368            mT.thunkSetVar(index, o);
369            return;
370        }
371
372        mRS.nScriptSetVarObj(getID(mRS), index, (o == null) ? 0 : o.getID(mRS));
373    }
374
375    /**
376     * Only intended for use by generated reflected code.
377     *
378     * @param index
379     * @param v
380     */
381    public void setVar(int index, FieldPacker v) {
382        if (mT != null) {
383            mT.thunkSetVar(index, v);
384            return;
385        }
386
387        mRS.nScriptSetVarV(getID(mRS), index, v.getData());
388    }
389
390    /**
391     * Only intended for use by generated reflected code.
392     *
393     * @param index
394     * @param v
395     * @param e
396     * @param dims
397     */
398    public void setVar(int index, FieldPacker v, Element e, int[] dims) {
399        if (mT != null) {
400            mT.thunkSetVar(index, v, e, dims);
401            return;
402        }
403
404        mRS.nScriptSetVarVE(getID(mRS), index, v.getData(), e.getID(mRS), dims);
405    }
406
407    public static class Builder {
408        RenderScript mRS;
409
410        Builder(RenderScript rs) {
411            mRS = rs;
412        }
413    }
414
415
416    public static class FieldBase {
417        protected Element mElement;
418        protected Allocation mAllocation;
419
420        protected void init(RenderScript rs, int dimx) {
421            mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT);
422        }
423
424        protected void init(RenderScript rs, int dimx, int usages) {
425            mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT | usages);
426        }
427
428        protected FieldBase() {
429        }
430
431        public Element getElement() {
432            return mElement;
433        }
434
435        public Type getType() {
436            return mAllocation.getType();
437        }
438
439        public Allocation getAllocation() {
440            return mAllocation;
441        }
442
443        //@Override
444        public void updateAllocation() {
445        }
446    }
447
448    public static final class LaunchOptions {
449        protected int xstart = 0;
450        protected int ystart = 0;
451        protected int xend = 0;
452        protected int yend = 0;
453        protected int zstart = 0;
454        protected int zend = 0;
455
456        protected int strategy;
457
458        public LaunchOptions setX(int xstartArg, int xendArg) {
459            if (xstartArg < 0 || xendArg <= xstartArg) {
460                throw new RSIllegalArgumentException("Invalid dimensions");
461            }
462            xstart = xstartArg;
463            xend = xendArg;
464            return this;
465        }
466
467        public LaunchOptions setY(int ystartArg, int yendArg) {
468            if (ystartArg < 0 || yendArg <= ystartArg) {
469                throw new RSIllegalArgumentException("Invalid dimensions");
470            }
471            ystart = ystartArg;
472            yend = yendArg;
473            return this;
474        }
475
476        public LaunchOptions setZ(int zstartArg, int zendArg) {
477            if (zstartArg < 0 || zendArg <= zstartArg) {
478                throw new RSIllegalArgumentException("Invalid dimensions");
479            }
480            zstart = zstartArg;
481            zend = zendArg;
482            return this;
483        }
484
485
486        public int getXStart() {
487            return xstart;
488        }
489        public int getXEnd() {
490            return xend;
491        }
492        public int getYStart() {
493            return ystart;
494        }
495        public int getYEnd() {
496            return yend;
497        }
498        public int getZStart() {
499            return zstart;
500        }
501        public int getZEnd() {
502            return zend;
503        }
504
505    }
506}
507
508