Script.java revision 9c9ad3f8c218954e46aab81f9af7834cea5675ca
1/*
2 * Copyright (C) 2008-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.renderscript;
18
19/**
20 *
21 **/
22public class Script extends BaseObj {
23    /**
24     * Only intended for use by generated reflected code.
25     *
26     * @param slot
27     */
28    protected void invoke(int slot) {
29        mRS.nScriptInvoke(getID(mRS), slot);
30    }
31
32    /**
33     * Only intended for use by generated reflected code.
34     *
35     * @param slot
36     * @param v
37     */
38    protected void invoke(int slot, FieldPacker v) {
39        if (v != null) {
40            mRS.nScriptInvokeV(getID(mRS), slot, v.getData());
41        } else {
42            mRS.nScriptInvoke(getID(mRS), slot);
43        }
44    }
45
46    /**
47     * Only intended for use by generated reflected code.
48     *
49     * @param slot
50     * @param ain
51     * @param aout
52     * @param v
53     */
54    protected void forEach(int slot, Allocation ain, Allocation aout, FieldPacker v) {
55        if (ain == null && aout == null) {
56            throw new RSIllegalArgumentException(
57                "At least one of ain or aout is required to be non-null.");
58        }
59        int in_id = 0;
60        if (ain != null) {
61            in_id = ain.getID(mRS);
62        }
63        int out_id = 0;
64        if (aout != null) {
65            out_id = aout.getID(mRS);
66        }
67        byte[] params = null;
68        if (v != null) {
69            params = v.getData();
70        }
71        mRS.nScriptForEach(getID(mRS), slot, in_id, out_id, params);
72    }
73
74
75    Script(int id, RenderScript rs) {
76        super(id, rs);
77    }
78
79
80    /**
81     * Only intended for use by generated reflected code.
82     *
83     * @param va
84     * @param slot
85     */
86    public void bindAllocation(Allocation va, int slot) {
87        mRS.validate();
88        if (va != null) {
89            mRS.nScriptBindAllocation(getID(mRS), va.getID(mRS), slot);
90        } else {
91            mRS.nScriptBindAllocation(getID(mRS), 0, slot);
92        }
93    }
94
95    /**
96     * Only intended for use by generated reflected code.
97     *
98     * @param index
99     * @param v
100     */
101    public void setVar(int index, float v) {
102        mRS.nScriptSetVarF(getID(mRS), index, v);
103    }
104
105    /**
106     * Only intended for use by generated reflected code.
107     *
108     * @param index
109     * @param v
110     */
111    public void setVar(int index, double v) {
112        mRS.nScriptSetVarD(getID(mRS), index, v);
113    }
114
115    /**
116     * Only intended for use by generated reflected code.
117     *
118     * @param index
119     * @param v
120     */
121    public void setVar(int index, int v) {
122        mRS.nScriptSetVarI(getID(mRS), index, v);
123    }
124
125    /**
126     * Only intended for use by generated reflected code.
127     *
128     * @param index
129     * @param v
130     */
131    public void setVar(int index, long v) {
132        mRS.nScriptSetVarJ(getID(mRS), index, v);
133    }
134
135    /**
136     * Only intended for use by generated reflected code.
137     *
138     * @param index
139     * @param v
140     */
141    public void setVar(int index, boolean v) {
142        mRS.nScriptSetVarI(getID(mRS), index, v ? 1 : 0);
143    }
144
145    /**
146     * Only intended for use by generated reflected code.
147     *
148     * @param index
149     * @param o
150     */
151    public void setVar(int index, BaseObj o) {
152        mRS.nScriptSetVarObj(getID(mRS), index, (o == null) ? 0 : o.getID(mRS));
153    }
154
155    /**
156     * Only intended for use by generated reflected code.
157     *
158     * @param index
159     * @param v
160     */
161    public void setVar(int index, FieldPacker v) {
162        mRS.nScriptSetVarV(getID(mRS), index, v.getData());
163    }
164
165    /**
166     * Only intended for use by generated reflected code.
167     *
168     * @param index
169     * @param v
170     * @param e
171     * @param dims
172     */
173    public void setVar(int index, FieldPacker v, Element e, int[] dims) {
174        mRS.nScriptSetVarVE(getID(mRS), index, v.getData(), e.getID(mRS), dims);
175    }
176
177    public void setTimeZone(String timeZone) {
178        mRS.validate();
179        try {
180            mRS.nScriptSetTimeZone(getID(mRS), timeZone.getBytes("UTF-8"));
181        } catch (java.io.UnsupportedEncodingException e) {
182            throw new RuntimeException(e);
183        }
184    }
185
186    public static class Builder {
187        RenderScript mRS;
188
189        Builder(RenderScript rs) {
190            mRS = rs;
191        }
192    }
193
194
195    public static class FieldBase {
196        protected Element mElement;
197        protected Allocation mAllocation;
198
199        protected void init(RenderScript rs, int dimx) {
200            mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT);
201        }
202
203        protected void init(RenderScript rs, int dimx, int usages) {
204            mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT | usages);
205        }
206
207        protected FieldBase() {
208        }
209
210        public Element getElement() {
211            return mElement;
212        }
213
214        public Type getType() {
215            return mAllocation.getType();
216        }
217
218        public Allocation getAllocation() {
219            return mAllocation;
220        }
221
222        //@Override
223        public void updateAllocation() {
224        }
225    }
226}
227
228