blob: fe80967e6f35d2419d293150951e3ba6a74a0e4a [file] [log] [blame]
Jason Sams69f0d312009-08-03 18:11:17 -07001/*
Stephen Hinesadeb8092012-04-20 14:26:06 -07002 * Copyright (C) 2008-2012 The Android Open Source Project
Jason Sams69f0d312009-08-03 18:11:17 -07003 *
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
Jason Sams08a81582012-09-18 12:32:10 -070019import android.util.SparseArray;
20
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070021/**
Jason Samsa23d4e72011-01-04 18:59:12 -080022 *
Jason Sams69f0d312009-08-03 18:11:17 -070023 **/
24public class Script extends BaseObj {
Jason Sams08a81582012-09-18 12:32:10 -070025
26 /**
27 * KernelID is an identifier for a Script + root function pair. It is used
28 * as an identifier for ScriptGroup creation.
29 *
30 * This class should not be directly created. Instead use the method in the
31 * reflected or intrinsic code "getKernelID_funcname()".
32 *
33 */
34 public static final class KernelID extends BaseObj {
35 Script mScript;
36 int mSlot;
37 int mSig;
38 KernelID(int id, RenderScript rs, Script s, int slot, int sig) {
39 super(id, rs);
40 mScript = s;
41 mSlot = slot;
42 mSig = sig;
43 }
44 }
45
46 private final SparseArray<KernelID> mKIDs = new SparseArray<KernelID>();
47 /**
48 * Only to be used by generated reflected classes.
49 *
50 *
51 * @param slot
52 * @param sig
53 * @param ein
54 * @param eout
55 *
56 * @return KernelID
57 */
58 protected KernelID createKernelID(int slot, int sig, Element ein, Element eout) {
59 KernelID k = mKIDs.get(slot);
60 if (k != null) {
61 return k;
62 }
63
64 int id = mRS.nScriptKernelIDCreate(getID(mRS), slot, sig);
65 if (id == 0) {
66 throw new RSDriverException("Failed to create KernelID");
67 }
68
69 k = new KernelID(id, mRS, this, slot, sig);
70 mKIDs.put(slot, k);
71 return k;
72 }
73
74 /**
75 * FieldID is an identifier for a Script + exported field pair. It is used
76 * as an identifier for ScriptGroup creation.
77 *
78 * This class should not be directly created. Instead use the method in the
79 * reflected or intrinsic code "getFieldID_funcname()".
80 *
81 */
82 public static final class FieldID extends BaseObj {
83 Script mScript;
84 int mSlot;
85 FieldID(int id, RenderScript rs, Script s, int slot) {
86 super(id, rs);
87 mScript = s;
88 mSlot = slot;
89 }
90 }
91
92 private final SparseArray<FieldID> mFIDs = new SparseArray();
93 /**
94 * Only to be used by generated reflected classes.
95 *
96 * @param slot
97 * @param e
98 *
99 * @return FieldID
100 */
101 protected FieldID createFieldID(int slot, Element e) {
102 FieldID f = mFIDs.get(slot);
103 if (f != null) {
104 return f;
105 }
106
107 int id = mRS.nScriptFieldIDCreate(getID(mRS), slot);
108 if (id == 0) {
109 throw new RSDriverException("Failed to create FieldID");
110 }
111
112 f = new FieldID(id, mRS, this, slot);
113 mFIDs.put(slot, f);
114 return f;
115 }
116
117
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700118 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800119 * Only intended for use by generated reflected code.
120 *
121 * @param slot
122 */
Jason Sams2d71bc72010-03-26 16:06:43 -0700123 protected void invoke(int slot) {
Jason Samse07694b2012-04-03 15:36:36 -0700124 mRS.nScriptInvoke(getID(mRS), slot);
Jason Sams2d71bc72010-03-26 16:06:43 -0700125 }
126
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700127 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800128 * Only intended for use by generated reflected code.
129 *
130 * @param slot
131 * @param v
132 */
Jason Sams96ed4cf2010-06-15 12:15:57 -0700133 protected void invoke(int slot, FieldPacker v) {
134 if (v != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700135 mRS.nScriptInvokeV(getID(mRS), slot, v.getData());
Jason Sams96ed4cf2010-06-15 12:15:57 -0700136 } else {
Jason Samse07694b2012-04-03 15:36:36 -0700137 mRS.nScriptInvoke(getID(mRS), slot);
Jason Sams96ed4cf2010-06-15 12:15:57 -0700138 }
Jason Sams4d339932010-05-11 14:03:58 -0700139 }
140
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700141 /**
Jason Sams6e494d32011-04-27 16:33:11 -0700142 * Only intended for use by generated reflected code.
143 *
144 * @param slot
145 * @param ain
146 * @param aout
147 * @param v
148 */
149 protected void forEach(int slot, Allocation ain, Allocation aout, FieldPacker v) {
150 if (ain == null && aout == null) {
151 throw new RSIllegalArgumentException(
152 "At least one of ain or aout is required to be non-null.");
153 }
154 int in_id = 0;
155 if (ain != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700156 in_id = ain.getID(mRS);
Jason Sams6e494d32011-04-27 16:33:11 -0700157 }
158 int out_id = 0;
159 if (aout != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700160 out_id = aout.getID(mRS);
Jason Sams6e494d32011-04-27 16:33:11 -0700161 }
162 byte[] params = null;
163 if (v != null) {
164 params = v.getData();
165 }
Jason Samse07694b2012-04-03 15:36:36 -0700166 mRS.nScriptForEach(getID(mRS), slot, in_id, out_id, params);
Jason Sams6e494d32011-04-27 16:33:11 -0700167 }
168
Jason Sams4d339932010-05-11 14:03:58 -0700169
Jason Sams69f0d312009-08-03 18:11:17 -0700170 Script(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700171 super(id, rs);
Jason Sams69f0d312009-08-03 18:11:17 -0700172 }
173
Jason Sams67e3d202011-01-09 13:49:01 -0800174
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700175 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800176 * Only intended for use by generated reflected code.
177 *
178 * @param va
179 * @param slot
180 */
Jason Sams69f0d312009-08-03 18:11:17 -0700181 public void bindAllocation(Allocation va, int slot) {
Jason Sams771bebb2009-12-07 12:40:12 -0800182 mRS.validate();
Jason Sams4d339932010-05-11 14:03:58 -0700183 if (va != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700184 mRS.nScriptBindAllocation(getID(mRS), va.getID(mRS), slot);
Jason Sams4d339932010-05-11 14:03:58 -0700185 } else {
Jason Samse07694b2012-04-03 15:36:36 -0700186 mRS.nScriptBindAllocation(getID(mRS), 0, slot);
Jason Sams4d339932010-05-11 14:03:58 -0700187 }
188 }
189
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700190 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800191 * Only intended for use by generated reflected code.
192 *
193 * @param index
194 * @param v
195 */
Jason Sams4d339932010-05-11 14:03:58 -0700196 public void setVar(int index, float v) {
Jason Samse07694b2012-04-03 15:36:36 -0700197 mRS.nScriptSetVarF(getID(mRS), index, v);
Jason Sams4d339932010-05-11 14:03:58 -0700198 }
199
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700200 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800201 * Only intended for use by generated reflected code.
202 *
203 * @param index
204 * @param v
205 */
Stephen Hinesca54ec32010-09-20 17:20:30 -0700206 public void setVar(int index, double v) {
Jason Samse07694b2012-04-03 15:36:36 -0700207 mRS.nScriptSetVarD(getID(mRS), index, v);
Stephen Hinesca54ec32010-09-20 17:20:30 -0700208 }
209
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700210 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800211 * Only intended for use by generated reflected code.
212 *
213 * @param index
214 * @param v
215 */
Jason Sams4d339932010-05-11 14:03:58 -0700216 public void setVar(int index, int v) {
Jason Samse07694b2012-04-03 15:36:36 -0700217 mRS.nScriptSetVarI(getID(mRS), index, v);
Jason Sams4d339932010-05-11 14:03:58 -0700218 }
219
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700220 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800221 * Only intended for use by generated reflected code.
222 *
223 * @param index
224 * @param v
225 */
Stephen Hines031ec58c2010-10-11 10:54:21 -0700226 public void setVar(int index, long v) {
Jason Samse07694b2012-04-03 15:36:36 -0700227 mRS.nScriptSetVarJ(getID(mRS), index, v);
Stephen Hines031ec58c2010-10-11 10:54:21 -0700228 }
229
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700230 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800231 * Only intended for use by generated reflected code.
232 *
233 * @param index
234 * @param v
235 */
Jason Sams0b9a22c2010-07-02 15:35:19 -0700236 public void setVar(int index, boolean v) {
Jason Samse07694b2012-04-03 15:36:36 -0700237 mRS.nScriptSetVarI(getID(mRS), index, v ? 1 : 0);
Jason Sams0b9a22c2010-07-02 15:35:19 -0700238 }
239
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700240 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800241 * Only intended for use by generated reflected code.
242 *
243 * @param index
244 * @param o
245 */
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800246 public void setVar(int index, BaseObj o) {
Jason Samse07694b2012-04-03 15:36:36 -0700247 mRS.nScriptSetVarObj(getID(mRS), index, (o == null) ? 0 : o.getID(mRS));
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800248 }
249
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700250 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800251 * Only intended for use by generated reflected code.
252 *
253 * @param index
254 * @param v
255 */
Jason Sams4d339932010-05-11 14:03:58 -0700256 public void setVar(int index, FieldPacker v) {
Jason Samse07694b2012-04-03 15:36:36 -0700257 mRS.nScriptSetVarV(getID(mRS), index, v.getData());
Jason Sams69f0d312009-08-03 18:11:17 -0700258 }
259
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700260 /**
Stephen Hinesadeb8092012-04-20 14:26:06 -0700261 * Only intended for use by generated reflected code.
262 *
263 * @param index
264 * @param v
265 * @param e
266 * @param dims
267 */
268 public void setVar(int index, FieldPacker v, Element e, int[] dims) {
269 mRS.nScriptSetVarVE(getID(mRS), index, v.getData(), e.getID(mRS), dims);
270 }
271
Jason Sams22534172009-08-04 16:58:20 -0700272 public void setTimeZone(String timeZone) {
Jason Sams771bebb2009-12-07 12:40:12 -0800273 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -0700274 try {
Jason Samse07694b2012-04-03 15:36:36 -0700275 mRS.nScriptSetTimeZone(getID(mRS), timeZone.getBytes("UTF-8"));
Jason Sams22534172009-08-04 16:58:20 -0700276 } catch (java.io.UnsupportedEncodingException e) {
277 throw new RuntimeException(e);
278 }
279 }
Jason Sams69f0d312009-08-03 18:11:17 -0700280
281 public static class Builder {
282 RenderScript mRS;
Jason Sams69f0d312009-08-03 18:11:17 -0700283
284 Builder(RenderScript rs) {
285 mRS = rs;
286 }
Jason Sams69f0d312009-08-03 18:11:17 -0700287 }
288
Jason Sams2d71bc72010-03-26 16:06:43 -0700289
290 public static class FieldBase {
291 protected Element mElement;
Jason Sams2d71bc72010-03-26 16:06:43 -0700292 protected Allocation mAllocation;
293
294 protected void init(RenderScript rs, int dimx) {
Jason Sams5476b452010-12-08 16:14:36 -0800295 mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT);
296 }
297
298 protected void init(RenderScript rs, int dimx, int usages) {
299 mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT | usages);
Jason Sams2d71bc72010-03-26 16:06:43 -0700300 }
301
302 protected FieldBase() {
303 }
304
305 public Element getElement() {
306 return mElement;
307 }
308
309 public Type getType() {
Jason Sams31a7e422010-10-26 13:09:17 -0700310 return mAllocation.getType();
Jason Sams2d71bc72010-03-26 16:06:43 -0700311 }
312
313 public Allocation getAllocation() {
314 return mAllocation;
315 }
316
317 //@Override
318 public void updateAllocation() {
319 }
Jason Sams2d71bc72010-03-26 16:06:43 -0700320 }
Tim Murrayfbfaa852012-12-14 16:01:58 -0800321
322 public static final class LaunchOptions {
323 protected int xstart = -1;
324 protected int ystart = -1;
325 protected int xend = -1 ;
326 protected int yend = -1;
327
328 protected int strategy;
329
330 public void setX(int xstartArg, int xendArg) {
331 if (xstartArg < 0 || xendArg <= xstartArg) {
332 throw new RSIllegalArgumentException("Invalid dimensions");
333 }
334 xstart = xstartArg;
335 xend = xendArg;
336 }
337
338 public void setY(int ystartArg, int yendArg) {
339 if (ystartArg < 0 || yendArg <= ystartArg) {
340 throw new RSIllegalArgumentException("Invalid dimensions");
341 }
342 ystart = ystartArg;
343 yend = yendArg;
344 }
345
346 public int getXStart() {
347 return xstart;
348 }
349 public int getXEnd() {
350 return xend;
351 }
352 public int getYStart() {
353 return ystart;
354 }
355 public int getYEnd() {
356 return yend;
357 }
358
359 }
Jason Sams69f0d312009-08-03 18:11:17 -0700360}
361