blob: a1f228718436317058346930d9323fdf456fc283 [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/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070022 * The parent class for all executable scripts. This should not be used by
23 * applications.
Jason Sams69f0d312009-08-03 18:11:17 -070024 **/
25public class Script extends BaseObj {
Jason Sams08a81582012-09-18 12:32:10 -070026
27 /**
28 * KernelID is an identifier for a Script + root function pair. It is used
29 * as an identifier for ScriptGroup creation.
30 *
31 * This class should not be directly created. Instead use the method in the
32 * reflected or intrinsic code "getKernelID_funcname()".
33 *
34 */
35 public static final class KernelID extends BaseObj {
36 Script mScript;
37 int mSlot;
38 int mSig;
Tim Murray460a0492013-11-19 12:45:54 -080039 KernelID(long id, RenderScript rs, Script s, int slot, int sig) {
Jason Sams08a81582012-09-18 12:32:10 -070040 super(id, rs);
41 mScript = s;
42 mSlot = slot;
43 mSig = sig;
44 }
45 }
46
47 private final SparseArray<KernelID> mKIDs = new SparseArray<KernelID>();
48 /**
49 * Only to be used by generated reflected classes.
Jason Sams08a81582012-09-18 12:32:10 -070050 */
51 protected KernelID createKernelID(int slot, int sig, Element ein, Element eout) {
52 KernelID k = mKIDs.get(slot);
53 if (k != null) {
54 return k;
55 }
56
Tim Murray460a0492013-11-19 12:45:54 -080057 long id = mRS.nScriptKernelIDCreate(getID(mRS), slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -070058 if (id == 0) {
59 throw new RSDriverException("Failed to create KernelID");
60 }
61
62 k = new KernelID(id, mRS, this, slot, sig);
63 mKIDs.put(slot, k);
64 return k;
65 }
66
67 /**
68 * FieldID is an identifier for a Script + exported field pair. It is used
69 * as an identifier for ScriptGroup creation.
70 *
71 * This class should not be directly created. Instead use the method in the
72 * reflected or intrinsic code "getFieldID_funcname()".
73 *
74 */
75 public static final class FieldID extends BaseObj {
76 Script mScript;
77 int mSlot;
Tim Murray460a0492013-11-19 12:45:54 -080078 FieldID(long id, RenderScript rs, Script s, int slot) {
Jason Sams08a81582012-09-18 12:32:10 -070079 super(id, rs);
80 mScript = s;
81 mSlot = slot;
82 }
83 }
84
85 private final SparseArray<FieldID> mFIDs = new SparseArray();
86 /**
87 * Only to be used by generated reflected classes.
Jason Sams08a81582012-09-18 12:32:10 -070088 */
89 protected FieldID createFieldID(int slot, Element e) {
90 FieldID f = mFIDs.get(slot);
91 if (f != null) {
92 return f;
93 }
94
Tim Murray460a0492013-11-19 12:45:54 -080095 long id = mRS.nScriptFieldIDCreate(getID(mRS), slot);
Jason Sams08a81582012-09-18 12:32:10 -070096 if (id == 0) {
97 throw new RSDriverException("Failed to create FieldID");
98 }
99
100 f = new FieldID(id, mRS, this, slot);
101 mFIDs.put(slot, f);
102 return f;
103 }
104
105
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700106 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800107 * Only intended for use by generated reflected code.
108 *
Jason Sams67e3d202011-01-09 13:49:01 -0800109 */
Jason Sams2d71bc72010-03-26 16:06:43 -0700110 protected void invoke(int slot) {
Jason Samse07694b2012-04-03 15:36:36 -0700111 mRS.nScriptInvoke(getID(mRS), slot);
Jason Sams2d71bc72010-03-26 16:06:43 -0700112 }
113
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700114 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800115 * Only intended for use by generated reflected code.
116 *
Jason Sams67e3d202011-01-09 13:49:01 -0800117 */
Jason Sams96ed4cf2010-06-15 12:15:57 -0700118 protected void invoke(int slot, FieldPacker v) {
119 if (v != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700120 mRS.nScriptInvokeV(getID(mRS), slot, v.getData());
Jason Sams96ed4cf2010-06-15 12:15:57 -0700121 } else {
Jason Samse07694b2012-04-03 15:36:36 -0700122 mRS.nScriptInvoke(getID(mRS), slot);
Jason Sams96ed4cf2010-06-15 12:15:57 -0700123 }
Jason Sams4d339932010-05-11 14:03:58 -0700124 }
125
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700126 /**
Jason Sams6e494d32011-04-27 16:33:11 -0700127 * Only intended for use by generated reflected code.
128 *
Jason Sams6e494d32011-04-27 16:33:11 -0700129 */
130 protected void forEach(int slot, Allocation ain, Allocation aout, FieldPacker v) {
131 if (ain == null && aout == null) {
132 throw new RSIllegalArgumentException(
133 "At least one of ain or aout is required to be non-null.");
134 }
Tim Murray460a0492013-11-19 12:45:54 -0800135 long in_id = 0;
Jason Sams6e494d32011-04-27 16:33:11 -0700136 if (ain != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700137 in_id = ain.getID(mRS);
Jason Sams6e494d32011-04-27 16:33:11 -0700138 }
Tim Murray460a0492013-11-19 12:45:54 -0800139 long out_id = 0;
Jason Sams6e494d32011-04-27 16:33:11 -0700140 if (aout != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700141 out_id = aout.getID(mRS);
Jason Sams6e494d32011-04-27 16:33:11 -0700142 }
143 byte[] params = null;
144 if (v != null) {
145 params = v.getData();
146 }
Jason Samse07694b2012-04-03 15:36:36 -0700147 mRS.nScriptForEach(getID(mRS), slot, in_id, out_id, params);
Jason Sams6e494d32011-04-27 16:33:11 -0700148 }
149
Jason Samsf64cca92013-04-19 12:56:37 -0700150 /**
151 * Only intended for use by generated reflected code.
152 *
Jason Samsf64cca92013-04-19 12:56:37 -0700153 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800154 protected void forEach(int slot, Allocation ain, Allocation aout, FieldPacker v, LaunchOptions sc) {
155 if (ain == null && aout == null) {
156 throw new RSIllegalArgumentException(
157 "At least one of ain or aout is required to be non-null.");
158 }
Tim Murrayba9dd062013-02-12 16:22:34 -0800159
160 if (sc == null) {
161 forEach(slot, ain, aout, v);
162 return;
163 }
Tim Murray460a0492013-11-19 12:45:54 -0800164 long in_id = 0;
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800165 if (ain != null) {
166 in_id = ain.getID(mRS);
167 }
Tim Murray460a0492013-11-19 12:45:54 -0800168 long out_id = 0;
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800169 if (aout != null) {
170 out_id = aout.getID(mRS);
171 }
172 byte[] params = null;
173 if (v != null) {
174 params = v.getData();
175 }
176 mRS.nScriptForEachClipped(getID(mRS), slot, in_id, out_id, params, sc.xstart, sc.xend, sc.ystart, sc.yend, sc.zstart, sc.zend);
177 }
Jason Sams4d339932010-05-11 14:03:58 -0700178
Tim Murray460a0492013-11-19 12:45:54 -0800179 Script(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700180 super(id, rs);
Jason Sams69f0d312009-08-03 18:11:17 -0700181 }
182
Jason Sams67e3d202011-01-09 13:49:01 -0800183
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700184 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800185 * Only intended for use by generated reflected code.
186 *
Jason Sams67e3d202011-01-09 13:49:01 -0800187 */
Jason Sams69f0d312009-08-03 18:11:17 -0700188 public void bindAllocation(Allocation va, int slot) {
Jason Sams771bebb2009-12-07 12:40:12 -0800189 mRS.validate();
Jason Sams4d339932010-05-11 14:03:58 -0700190 if (va != null) {
Jason Sams9eb8b3a2014-01-14 16:18:14 -0800191 if (mRS.getApplicationContext().getApplicationInfo().targetSdkVersion >= 20) {
192 final Type t = va.mType;
193 if (t.hasMipmaps() || t.hasFaces() || (t.getY() != 0) || (t.getZ() != 0)) {
194 throw new RSIllegalArgumentException(
195 "API 20+ only allows simple 1D allocations to be used with bind.");
196 }
197 }
Jason Samse07694b2012-04-03 15:36:36 -0700198 mRS.nScriptBindAllocation(getID(mRS), va.getID(mRS), slot);
Jason Sams4d339932010-05-11 14:03:58 -0700199 } else {
Jason Samse07694b2012-04-03 15:36:36 -0700200 mRS.nScriptBindAllocation(getID(mRS), 0, slot);
Jason Sams4d339932010-05-11 14:03:58 -0700201 }
202 }
203
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700204 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800205 * Only intended for use by generated reflected code.
206 *
Jason Sams67e3d202011-01-09 13:49:01 -0800207 */
Jason Sams4d339932010-05-11 14:03:58 -0700208 public void setVar(int index, float v) {
Jason Samse07694b2012-04-03 15:36:36 -0700209 mRS.nScriptSetVarF(getID(mRS), index, v);
Jason Sams4d339932010-05-11 14:03:58 -0700210 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700211 public float getVarF(int index) {
212 return mRS.nScriptGetVarF(getID(mRS), index);
213 }
Jason Sams4d339932010-05-11 14:03:58 -0700214
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700215 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800216 * Only intended for use by generated reflected code.
217 *
Jason Sams67e3d202011-01-09 13:49:01 -0800218 */
Stephen Hinesca54ec32010-09-20 17:20:30 -0700219 public void setVar(int index, double v) {
Jason Samse07694b2012-04-03 15:36:36 -0700220 mRS.nScriptSetVarD(getID(mRS), index, v);
Stephen Hinesca54ec32010-09-20 17:20:30 -0700221 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700222 public double getVarD(int index) {
223 return mRS.nScriptGetVarD(getID(mRS), index);
224 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700225
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700226 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800227 * Only intended for use by generated reflected code.
228 *
Jason Sams67e3d202011-01-09 13:49:01 -0800229 */
Jason Sams4d339932010-05-11 14:03:58 -0700230 public void setVar(int index, int v) {
Jason Samse07694b2012-04-03 15:36:36 -0700231 mRS.nScriptSetVarI(getID(mRS), index, v);
Jason Sams4d339932010-05-11 14:03:58 -0700232 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700233 public int getVarI(int index) {
234 return mRS.nScriptGetVarI(getID(mRS), index);
235 }
236
Jason Sams4d339932010-05-11 14:03:58 -0700237
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700238 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800239 * Only intended for use by generated reflected code.
240 *
Jason Sams67e3d202011-01-09 13:49:01 -0800241 */
Stephen Hines031ec58c2010-10-11 10:54:21 -0700242 public void setVar(int index, long v) {
Jason Samse07694b2012-04-03 15:36:36 -0700243 mRS.nScriptSetVarJ(getID(mRS), index, v);
Stephen Hines031ec58c2010-10-11 10:54:21 -0700244 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700245 public long getVarJ(int index) {
246 return mRS.nScriptGetVarJ(getID(mRS), index);
247 }
248
Stephen Hines031ec58c2010-10-11 10:54:21 -0700249
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 *
Jason Sams67e3d202011-01-09 13:49:01 -0800253 */
Jason Sams0b9a22c2010-07-02 15:35:19 -0700254 public void setVar(int index, boolean v) {
Jason Samse07694b2012-04-03 15:36:36 -0700255 mRS.nScriptSetVarI(getID(mRS), index, v ? 1 : 0);
Jason Sams0b9a22c2010-07-02 15:35:19 -0700256 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700257 public boolean getVarB(int index) {
258 return mRS.nScriptGetVarI(getID(mRS), index) > 0 ? true : false;
259 }
Jason Sams0b9a22c2010-07-02 15:35:19 -0700260
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700261 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800262 * Only intended for use by generated reflected code.
263 *
Jason Sams67e3d202011-01-09 13:49:01 -0800264 */
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800265 public void setVar(int index, BaseObj o) {
Jason Samse07694b2012-04-03 15:36:36 -0700266 mRS.nScriptSetVarObj(getID(mRS), index, (o == null) ? 0 : o.getID(mRS));
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800267 }
268
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700269 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800270 * Only intended for use by generated reflected code.
271 *
Jason Sams67e3d202011-01-09 13:49:01 -0800272 */
Jason Sams4d339932010-05-11 14:03:58 -0700273 public void setVar(int index, FieldPacker v) {
Jason Samse07694b2012-04-03 15:36:36 -0700274 mRS.nScriptSetVarV(getID(mRS), index, v.getData());
Jason Sams69f0d312009-08-03 18:11:17 -0700275 }
276
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700277 /**
Stephen Hinesadeb8092012-04-20 14:26:06 -0700278 * Only intended for use by generated reflected code.
279 *
Stephen Hinesadeb8092012-04-20 14:26:06 -0700280 */
281 public void setVar(int index, FieldPacker v, Element e, int[] dims) {
282 mRS.nScriptSetVarVE(getID(mRS), index, v.getData(), e.getID(mRS), dims);
283 }
284
Jason Samsf64cca92013-04-19 12:56:37 -0700285 /**
286 * Only intended for use by generated reflected code.
287 *
Jason Samsf64cca92013-04-19 12:56:37 -0700288 */
Tim Murray7c4caad2013-04-10 16:21:40 -0700289 public void getVarV(int index, FieldPacker v) {
290 mRS.nScriptGetVarV(getID(mRS), index, v.getData());
291 }
292
Jason Sams22534172009-08-04 16:58:20 -0700293 public void setTimeZone(String timeZone) {
Jason Sams771bebb2009-12-07 12:40:12 -0800294 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -0700295 try {
Jason Samse07694b2012-04-03 15:36:36 -0700296 mRS.nScriptSetTimeZone(getID(mRS), timeZone.getBytes("UTF-8"));
Jason Sams22534172009-08-04 16:58:20 -0700297 } catch (java.io.UnsupportedEncodingException e) {
298 throw new RuntimeException(e);
299 }
300 }
Jason Sams69f0d312009-08-03 18:11:17 -0700301
Tim Murrayc11e25c2013-04-09 11:01:01 -0700302 /**
303 * Only intended for use by generated reflected code.
304 *
305 */
Jason Sams69f0d312009-08-03 18:11:17 -0700306 public static class Builder {
307 RenderScript mRS;
Jason Sams69f0d312009-08-03 18:11:17 -0700308
309 Builder(RenderScript rs) {
310 mRS = rs;
311 }
Jason Sams69f0d312009-08-03 18:11:17 -0700312 }
313
Jason Sams2d71bc72010-03-26 16:06:43 -0700314
Jason Samsf64cca92013-04-19 12:56:37 -0700315 /**
316 * Only intended for use by generated reflected code.
317 *
318 */
Jason Sams2d71bc72010-03-26 16:06:43 -0700319 public static class FieldBase {
320 protected Element mElement;
Jason Sams2d71bc72010-03-26 16:06:43 -0700321 protected Allocation mAllocation;
322
323 protected void init(RenderScript rs, int dimx) {
Jason Sams5476b452010-12-08 16:14:36 -0800324 mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT);
325 }
326
327 protected void init(RenderScript rs, int dimx, int usages) {
328 mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT | usages);
Jason Sams2d71bc72010-03-26 16:06:43 -0700329 }
330
331 protected FieldBase() {
332 }
333
334 public Element getElement() {
335 return mElement;
336 }
337
338 public Type getType() {
Jason Sams31a7e422010-10-26 13:09:17 -0700339 return mAllocation.getType();
Jason Sams2d71bc72010-03-26 16:06:43 -0700340 }
341
342 public Allocation getAllocation() {
343 return mAllocation;
344 }
345
346 //@Override
347 public void updateAllocation() {
348 }
Jason Sams2d71bc72010-03-26 16:06:43 -0700349 }
Tim Murrayfbfaa852012-12-14 16:01:58 -0800350
Jason Samsf64cca92013-04-19 12:56:37 -0700351
352 /**
353 * Class used to specify clipping for a kernel launch.
354 *
355 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800356 public static final class LaunchOptions {
Jason Samsf64cca92013-04-19 12:56:37 -0700357 private int xstart = 0;
358 private int ystart = 0;
359 private int xend = 0;
360 private int yend = 0;
361 private int zstart = 0;
362 private int zend = 0;
363 private int strategy;
Tim Murrayfbfaa852012-12-14 16:01:58 -0800364
Jason Samsf64cca92013-04-19 12:56:37 -0700365 /**
366 * Set the X range. If the end value is set to 0 the X dimension is not
367 * clipped.
368 *
369 * @param xstartArg Must be >= 0
370 * @param xendArg Must be >= xstartArg
371 *
372 * @return LaunchOptions
373 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800374 public LaunchOptions setX(int xstartArg, int xendArg) {
Tim Murrayfbfaa852012-12-14 16:01:58 -0800375 if (xstartArg < 0 || xendArg <= xstartArg) {
376 throw new RSIllegalArgumentException("Invalid dimensions");
377 }
378 xstart = xstartArg;
379 xend = xendArg;
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800380 return this;
Tim Murrayfbfaa852012-12-14 16:01:58 -0800381 }
382
Jason Samsf64cca92013-04-19 12:56:37 -0700383 /**
384 * Set the Y range. If the end value is set to 0 the Y dimension is not
385 * clipped.
386 *
387 * @param ystartArg Must be >= 0
388 * @param yendArg Must be >= ystartArg
389 *
390 * @return LaunchOptions
391 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800392 public LaunchOptions setY(int ystartArg, int yendArg) {
Tim Murrayfbfaa852012-12-14 16:01:58 -0800393 if (ystartArg < 0 || yendArg <= ystartArg) {
394 throw new RSIllegalArgumentException("Invalid dimensions");
395 }
396 ystart = ystartArg;
397 yend = yendArg;
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800398 return this;
Tim Murrayfbfaa852012-12-14 16:01:58 -0800399 }
400
Jason Samsf64cca92013-04-19 12:56:37 -0700401 /**
402 * Set the Z range. If the end value is set to 0 the Z dimension is not
403 * clipped.
404 *
405 * @param zstartArg Must be >= 0
406 * @param zendArg Must be >= zstartArg
407 *
408 * @return LaunchOptions
409 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800410 public LaunchOptions setZ(int zstartArg, int zendArg) {
411 if (zstartArg < 0 || zendArg <= zstartArg) {
412 throw new RSIllegalArgumentException("Invalid dimensions");
413 }
414 zstart = zstartArg;
415 zend = zendArg;
416 return this;
417 }
418
419
Jason Samsf64cca92013-04-19 12:56:37 -0700420 /**
421 * Returns the current X start
422 *
423 * @return int current value
424 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800425 public int getXStart() {
426 return xstart;
427 }
Jason Samsf64cca92013-04-19 12:56:37 -0700428 /**
429 * Returns the current X end
430 *
431 * @return int current value
432 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800433 public int getXEnd() {
434 return xend;
435 }
Jason Samsf64cca92013-04-19 12:56:37 -0700436 /**
437 * Returns the current Y start
438 *
439 * @return int current value
440 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800441 public int getYStart() {
442 return ystart;
443 }
Jason Samsf64cca92013-04-19 12:56:37 -0700444 /**
445 * Returns the current Y end
446 *
447 * @return int current value
448 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800449 public int getYEnd() {
450 return yend;
451 }
Jason Samsf64cca92013-04-19 12:56:37 -0700452 /**
453 * Returns the current Z start
454 *
455 * @return int current value
456 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800457 public int getZStart() {
458 return zstart;
459 }
Jason Samsf64cca92013-04-19 12:56:37 -0700460 /**
461 * Returns the current Z end
462 *
463 * @return int current value
464 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800465 public int getZEnd() {
466 return zend;
467 }
Tim Murrayfbfaa852012-12-14 16:01:58 -0800468
469 }
Jason Sams69f0d312009-08-03 18:11:17 -0700470}
471