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