blob: f0579ca24c2ba5285f03db2e9c87ebfdc49e2827 [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 Samsf64cca92013-04-19 12:56:37 -0700169 /**
170 * Only intended for use by generated reflected code.
171 *
172 * @param slot
173 * @param ain
174 * @param aout
175 * @param v
176 * @param sc
177 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800178 protected void forEach(int slot, Allocation ain, Allocation aout, FieldPacker v, LaunchOptions sc) {
179 if (ain == null && aout == null) {
180 throw new RSIllegalArgumentException(
181 "At least one of ain or aout is required to be non-null.");
182 }
Tim Murrayba9dd062013-02-12 16:22:34 -0800183
184 if (sc == null) {
185 forEach(slot, ain, aout, v);
186 return;
187 }
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800188 int in_id = 0;
189 if (ain != null) {
190 in_id = ain.getID(mRS);
191 }
192 int out_id = 0;
193 if (aout != null) {
194 out_id = aout.getID(mRS);
195 }
196 byte[] params = null;
197 if (v != null) {
198 params = v.getData();
199 }
200 mRS.nScriptForEachClipped(getID(mRS), slot, in_id, out_id, params, sc.xstart, sc.xend, sc.ystart, sc.yend, sc.zstart, sc.zend);
201 }
Jason Sams4d339932010-05-11 14:03:58 -0700202
Jason Sams69f0d312009-08-03 18:11:17 -0700203 Script(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700204 super(id, rs);
Jason Sams69f0d312009-08-03 18:11:17 -0700205 }
206
Jason Sams67e3d202011-01-09 13:49:01 -0800207
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700208 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800209 * Only intended for use by generated reflected code.
210 *
211 * @param va
212 * @param slot
213 */
Jason Sams69f0d312009-08-03 18:11:17 -0700214 public void bindAllocation(Allocation va, int slot) {
Jason Sams771bebb2009-12-07 12:40:12 -0800215 mRS.validate();
Jason Sams4d339932010-05-11 14:03:58 -0700216 if (va != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700217 mRS.nScriptBindAllocation(getID(mRS), va.getID(mRS), slot);
Jason Sams4d339932010-05-11 14:03:58 -0700218 } else {
Jason Samse07694b2012-04-03 15:36:36 -0700219 mRS.nScriptBindAllocation(getID(mRS), 0, slot);
Jason Sams4d339932010-05-11 14:03:58 -0700220 }
221 }
222
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700223 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800224 * Only intended for use by generated reflected code.
225 *
226 * @param index
227 * @param v
228 */
Jason Sams4d339932010-05-11 14:03:58 -0700229 public void setVar(int index, float v) {
Jason Samse07694b2012-04-03 15:36:36 -0700230 mRS.nScriptSetVarF(getID(mRS), index, v);
Jason Sams4d339932010-05-11 14:03:58 -0700231 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700232 public float getVarF(int index) {
233 return mRS.nScriptGetVarF(getID(mRS), index);
234 }
Jason Sams4d339932010-05-11 14:03:58 -0700235
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700236 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800237 * Only intended for use by generated reflected code.
238 *
239 * @param index
240 * @param v
241 */
Stephen Hinesca54ec32010-09-20 17:20:30 -0700242 public void setVar(int index, double v) {
Jason Samse07694b2012-04-03 15:36:36 -0700243 mRS.nScriptSetVarD(getID(mRS), index, v);
Stephen Hinesca54ec32010-09-20 17:20:30 -0700244 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700245 public double getVarD(int index) {
246 return mRS.nScriptGetVarD(getID(mRS), index);
247 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700248
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700249 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800250 * Only intended for use by generated reflected code.
251 *
252 * @param index
253 * @param v
254 */
Jason Sams4d339932010-05-11 14:03:58 -0700255 public void setVar(int index, int v) {
Jason Samse07694b2012-04-03 15:36:36 -0700256 mRS.nScriptSetVarI(getID(mRS), index, v);
Jason Sams4d339932010-05-11 14:03:58 -0700257 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700258 public int getVarI(int index) {
259 return mRS.nScriptGetVarI(getID(mRS), index);
260 }
261
Jason Sams4d339932010-05-11 14:03:58 -0700262
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700263 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800264 * Only intended for use by generated reflected code.
265 *
266 * @param index
267 * @param v
268 */
Stephen Hines031ec58c2010-10-11 10:54:21 -0700269 public void setVar(int index, long v) {
Jason Samse07694b2012-04-03 15:36:36 -0700270 mRS.nScriptSetVarJ(getID(mRS), index, v);
Stephen Hines031ec58c2010-10-11 10:54:21 -0700271 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700272 public long getVarJ(int index) {
273 return mRS.nScriptGetVarJ(getID(mRS), index);
274 }
275
Stephen Hines031ec58c2010-10-11 10:54:21 -0700276
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700277 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800278 * Only intended for use by generated reflected code.
279 *
280 * @param index
281 * @param v
282 */
Jason Sams0b9a22c2010-07-02 15:35:19 -0700283 public void setVar(int index, boolean v) {
Jason Samse07694b2012-04-03 15:36:36 -0700284 mRS.nScriptSetVarI(getID(mRS), index, v ? 1 : 0);
Jason Sams0b9a22c2010-07-02 15:35:19 -0700285 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700286 public boolean getVarB(int index) {
287 return mRS.nScriptGetVarI(getID(mRS), index) > 0 ? true : false;
288 }
Jason Sams0b9a22c2010-07-02 15:35:19 -0700289
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700290 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800291 * Only intended for use by generated reflected code.
292 *
293 * @param index
294 * @param o
295 */
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800296 public void setVar(int index, BaseObj o) {
Jason Samse07694b2012-04-03 15:36:36 -0700297 mRS.nScriptSetVarObj(getID(mRS), index, (o == null) ? 0 : o.getID(mRS));
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800298 }
299
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700300 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800301 * Only intended for use by generated reflected code.
302 *
303 * @param index
304 * @param v
305 */
Jason Sams4d339932010-05-11 14:03:58 -0700306 public void setVar(int index, FieldPacker v) {
Jason Samse07694b2012-04-03 15:36:36 -0700307 mRS.nScriptSetVarV(getID(mRS), index, v.getData());
Jason Sams69f0d312009-08-03 18:11:17 -0700308 }
309
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700310 /**
Stephen Hinesadeb8092012-04-20 14:26:06 -0700311 * Only intended for use by generated reflected code.
312 *
313 * @param index
314 * @param v
315 * @param e
316 * @param dims
317 */
318 public void setVar(int index, FieldPacker v, Element e, int[] dims) {
319 mRS.nScriptSetVarVE(getID(mRS), index, v.getData(), e.getID(mRS), dims);
320 }
321
Jason Samsf64cca92013-04-19 12:56:37 -0700322 /**
323 * Only intended for use by generated reflected code.
324 *
325 * @param index
326 * @param v
327 */
Tim Murray7c4caad2013-04-10 16:21:40 -0700328 public void getVarV(int index, FieldPacker v) {
329 mRS.nScriptGetVarV(getID(mRS), index, v.getData());
330 }
331
Jason Sams22534172009-08-04 16:58:20 -0700332 public void setTimeZone(String timeZone) {
Jason Sams771bebb2009-12-07 12:40:12 -0800333 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -0700334 try {
Jason Samse07694b2012-04-03 15:36:36 -0700335 mRS.nScriptSetTimeZone(getID(mRS), timeZone.getBytes("UTF-8"));
Jason Sams22534172009-08-04 16:58:20 -0700336 } catch (java.io.UnsupportedEncodingException e) {
337 throw new RuntimeException(e);
338 }
339 }
Jason Sams69f0d312009-08-03 18:11:17 -0700340
341 public static class Builder {
342 RenderScript mRS;
Jason Sams69f0d312009-08-03 18:11:17 -0700343
344 Builder(RenderScript rs) {
345 mRS = rs;
346 }
Jason Sams69f0d312009-08-03 18:11:17 -0700347 }
348
Jason Sams2d71bc72010-03-26 16:06:43 -0700349
Jason Samsf64cca92013-04-19 12:56:37 -0700350 /**
351 * Only intended for use by generated reflected code.
352 *
353 */
Jason Sams2d71bc72010-03-26 16:06:43 -0700354 public static class FieldBase {
355 protected Element mElement;
Jason Sams2d71bc72010-03-26 16:06:43 -0700356 protected Allocation mAllocation;
357
358 protected void init(RenderScript rs, int dimx) {
Jason Sams5476b452010-12-08 16:14:36 -0800359 mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT);
360 }
361
362 protected void init(RenderScript rs, int dimx, int usages) {
363 mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT | usages);
Jason Sams2d71bc72010-03-26 16:06:43 -0700364 }
365
366 protected FieldBase() {
367 }
368
369 public Element getElement() {
370 return mElement;
371 }
372
373 public Type getType() {
Jason Sams31a7e422010-10-26 13:09:17 -0700374 return mAllocation.getType();
Jason Sams2d71bc72010-03-26 16:06:43 -0700375 }
376
377 public Allocation getAllocation() {
378 return mAllocation;
379 }
380
381 //@Override
382 public void updateAllocation() {
383 }
Jason Sams2d71bc72010-03-26 16:06:43 -0700384 }
Tim Murrayfbfaa852012-12-14 16:01:58 -0800385
Jason Samsf64cca92013-04-19 12:56:37 -0700386
387 /**
388 * Class used to specify clipping for a kernel launch.
389 *
390 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800391 public static final class LaunchOptions {
Jason Samsf64cca92013-04-19 12:56:37 -0700392 private int xstart = 0;
393 private int ystart = 0;
394 private int xend = 0;
395 private int yend = 0;
396 private int zstart = 0;
397 private int zend = 0;
398 private int strategy;
Tim Murrayfbfaa852012-12-14 16:01:58 -0800399
Jason Samsf64cca92013-04-19 12:56:37 -0700400 /**
401 * Set the X range. If the end value is set to 0 the X dimension is not
402 * clipped.
403 *
404 * @param xstartArg Must be >= 0
405 * @param xendArg Must be >= xstartArg
406 *
407 * @return LaunchOptions
408 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800409 public LaunchOptions setX(int xstartArg, int xendArg) {
Tim Murrayfbfaa852012-12-14 16:01:58 -0800410 if (xstartArg < 0 || xendArg <= xstartArg) {
411 throw new RSIllegalArgumentException("Invalid dimensions");
412 }
413 xstart = xstartArg;
414 xend = xendArg;
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800415 return this;
Tim Murrayfbfaa852012-12-14 16:01:58 -0800416 }
417
Jason Samsf64cca92013-04-19 12:56:37 -0700418 /**
419 * Set the Y range. If the end value is set to 0 the Y dimension is not
420 * clipped.
421 *
422 * @param ystartArg Must be >= 0
423 * @param yendArg Must be >= ystartArg
424 *
425 * @return LaunchOptions
426 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800427 public LaunchOptions setY(int ystartArg, int yendArg) {
Tim Murrayfbfaa852012-12-14 16:01:58 -0800428 if (ystartArg < 0 || yendArg <= ystartArg) {
429 throw new RSIllegalArgumentException("Invalid dimensions");
430 }
431 ystart = ystartArg;
432 yend = yendArg;
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800433 return this;
Tim Murrayfbfaa852012-12-14 16:01:58 -0800434 }
435
Jason Samsf64cca92013-04-19 12:56:37 -0700436 /**
437 * Set the Z range. If the end value is set to 0 the Z dimension is not
438 * clipped.
439 *
440 * @param zstartArg Must be >= 0
441 * @param zendArg Must be >= zstartArg
442 *
443 * @return LaunchOptions
444 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800445 public LaunchOptions setZ(int zstartArg, int zendArg) {
446 if (zstartArg < 0 || zendArg <= zstartArg) {
447 throw new RSIllegalArgumentException("Invalid dimensions");
448 }
449 zstart = zstartArg;
450 zend = zendArg;
451 return this;
452 }
453
454
Jason Samsf64cca92013-04-19 12:56:37 -0700455 /**
456 * Returns the current X start
457 *
458 * @return int current value
459 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800460 public int getXStart() {
461 return xstart;
462 }
Jason Samsf64cca92013-04-19 12:56:37 -0700463 /**
464 * Returns the current X end
465 *
466 * @return int current value
467 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800468 public int getXEnd() {
469 return xend;
470 }
Jason Samsf64cca92013-04-19 12:56:37 -0700471 /**
472 * Returns the current Y start
473 *
474 * @return int current value
475 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800476 public int getYStart() {
477 return ystart;
478 }
Jason Samsf64cca92013-04-19 12:56:37 -0700479 /**
480 * Returns the current Y end
481 *
482 * @return int current value
483 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800484 public int getYEnd() {
485 return yend;
486 }
Jason Samsf64cca92013-04-19 12:56:37 -0700487 /**
488 * Returns the current Z start
489 *
490 * @return int current value
491 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800492 public int getZStart() {
493 return zstart;
494 }
Jason Samsf64cca92013-04-19 12:56:37 -0700495 /**
496 * Returns the current Z end
497 *
498 * @return int current value
499 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800500 public int getZEnd() {
501 return zend;
502 }
Tim Murrayfbfaa852012-12-14 16:01:58 -0800503
504 }
Jason Sams69f0d312009-08-03 18:11:17 -0700505}
506