blob: fc3280be3ac77917780da4593b5dfd82f5b718e7 [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 Murray7a629fa2013-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;
Yang Ni6484b6b2016-03-24 09:40:32 -070044 guard.open("destroy");
Jason Sams08a81582012-09-18 12:32:10 -070045 }
46 }
47
48 private final SparseArray<KernelID> mKIDs = new SparseArray<KernelID>();
49 /**
50 * Only to be used by generated reflected classes.
Jason Sams08a81582012-09-18 12:32:10 -070051 */
Chris Wailesbe7b1de2014-07-15 10:56:14 -070052 protected KernelID createKernelID(int slot, int sig, Element ein,
53 Element eout) {
Jason Sams08a81582012-09-18 12:32:10 -070054 KernelID k = mKIDs.get(slot);
55 if (k != null) {
56 return k;
57 }
58
Tim Murray7a629fa2013-11-19 12:45:54 -080059 long id = mRS.nScriptKernelIDCreate(getID(mRS), slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -070060 if (id == 0) {
61 throw new RSDriverException("Failed to create KernelID");
62 }
63
64 k = new KernelID(id, mRS, this, slot, sig);
65 mKIDs.put(slot, k);
66 return k;
67 }
68
69 /**
Yang Nibe392ad2015-01-23 17:16:02 -080070 * InvokeID is an identifier for an invoke function. It is used
71 * as an identifier for ScriptGroup creation.
72 *
73 * This class should not be directly created. Instead use the method in the
74 * reflected or intrinsic code "getInvokeID_funcname()".
75 *
76 */
77 public static final class InvokeID extends BaseObj {
78 Script mScript;
79 int mSlot;
80 InvokeID(long id, RenderScript rs, Script s, int slot) {
81 super(id, rs);
82 mScript = s;
83 mSlot = slot;
84 }
85 }
86
87 private final SparseArray<InvokeID> mIIDs = new SparseArray<InvokeID>();
88 /**
Yang Nibe392ad2015-01-23 17:16:02 -080089 * Only to be used by generated reflected classes.
90 */
91 protected InvokeID createInvokeID(int slot) {
92 InvokeID i = mIIDs.get(slot);
93 if (i != null) {
94 return i;
95 }
96
97 long id = mRS.nScriptInvokeIDCreate(getID(mRS), slot);
98 if (id == 0) {
99 throw new RSDriverException("Failed to create KernelID");
100 }
101
102 i = new InvokeID(id, mRS, this, slot);
103 mIIDs.put(slot, i);
104 return i;
105 }
106
107 /**
Jason Sams08a81582012-09-18 12:32:10 -0700108 * FieldID is an identifier for a Script + exported field pair. It is used
109 * as an identifier for ScriptGroup creation.
110 *
111 * This class should not be directly created. Instead use the method in the
112 * reflected or intrinsic code "getFieldID_funcname()".
113 *
114 */
115 public static final class FieldID extends BaseObj {
116 Script mScript;
117 int mSlot;
Tim Murray7a629fa2013-11-19 12:45:54 -0800118 FieldID(long id, RenderScript rs, Script s, int slot) {
Jason Sams08a81582012-09-18 12:32:10 -0700119 super(id, rs);
120 mScript = s;
121 mSlot = slot;
Yang Ni6484b6b2016-03-24 09:40:32 -0700122 guard.open("destroy");
Jason Sams08a81582012-09-18 12:32:10 -0700123 }
124 }
125
126 private final SparseArray<FieldID> mFIDs = new SparseArray();
127 /**
128 * Only to be used by generated reflected classes.
Jason Sams08a81582012-09-18 12:32:10 -0700129 */
130 protected FieldID createFieldID(int slot, Element e) {
131 FieldID f = mFIDs.get(slot);
132 if (f != null) {
133 return f;
134 }
135
Tim Murray7a629fa2013-11-19 12:45:54 -0800136 long id = mRS.nScriptFieldIDCreate(getID(mRS), slot);
Jason Sams08a81582012-09-18 12:32:10 -0700137 if (id == 0) {
138 throw new RSDriverException("Failed to create FieldID");
139 }
140
141 f = new FieldID(id, mRS, this, slot);
142 mFIDs.put(slot, f);
143 return f;
144 }
145
146
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700147 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800148 * Only intended for use by generated reflected code.
149 *
Jason Sams67e3d202011-01-09 13:49:01 -0800150 */
Jason Sams2d71bc72010-03-26 16:06:43 -0700151 protected void invoke(int slot) {
Jason Samse07694b2012-04-03 15:36:36 -0700152 mRS.nScriptInvoke(getID(mRS), slot);
Jason Sams2d71bc72010-03-26 16:06:43 -0700153 }
154
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700155 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800156 * Only intended for use by generated reflected code.
157 *
Jason Sams67e3d202011-01-09 13:49:01 -0800158 */
Jason Sams96ed4cf2010-06-15 12:15:57 -0700159 protected void invoke(int slot, FieldPacker v) {
160 if (v != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700161 mRS.nScriptInvokeV(getID(mRS), slot, v.getData());
Jason Sams96ed4cf2010-06-15 12:15:57 -0700162 } else {
Jason Samse07694b2012-04-03 15:36:36 -0700163 mRS.nScriptInvoke(getID(mRS), slot);
Jason Sams96ed4cf2010-06-15 12:15:57 -0700164 }
Jason Sams4d339932010-05-11 14:03:58 -0700165 }
166
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700167 /**
Jason Sams6e494d32011-04-27 16:33:11 -0700168 * Only intended for use by generated reflected code.
169 *
Jason Sams6e494d32011-04-27 16:33:11 -0700170 */
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700171 protected void forEach(int slot, Allocation ain, Allocation aout,
172 FieldPacker v) {
173 forEach(slot, ain, aout, v, null);
Jason Sams6e494d32011-04-27 16:33:11 -0700174 }
175
Jason Samsf64cca92013-04-19 12:56:37 -0700176 /**
177 * Only intended for use by generated reflected code.
178 *
Jason Samsf64cca92013-04-19 12:56:37 -0700179 */
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700180 protected void forEach(int slot, Allocation ain, Allocation aout,
181 FieldPacker v, LaunchOptions sc) {
182 // TODO: Is this necessary if nScriptForEach calls validate as well?
Jason Sams678cc7f2014-03-05 16:09:02 -0800183 mRS.validate();
184 mRS.validateObject(ain);
185 mRS.validateObject(aout);
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700186
Jason Samsd1516df2015-05-05 18:00:34 -0700187 if (ain == null && aout == null && sc == null) {
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800188 throw new RSIllegalArgumentException(
Jason Samsd1516df2015-05-05 18:00:34 -0700189 "At least one of input allocation, output allocation, or LaunchOptions is required to be non-null.");
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800190 }
Tim Murrayba9dd062013-02-12 16:22:34 -0800191
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700192 long[] in_ids = null;
Stephen Hinesc9c7daf2014-08-13 17:32:19 +0000193 if (ain != null) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700194 in_ids = mInIdsBuffer;
195 in_ids[0] = ain.getID(mRS);
Stephen Hinesc9c7daf2014-08-13 17:32:19 +0000196 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700197
Tim Murray7a629fa2013-11-19 12:45:54 -0800198 long out_id = 0;
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800199 if (aout != null) {
200 out_id = aout.getID(mRS);
201 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700202
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800203 byte[] params = null;
204 if (v != null) {
205 params = v.getData();
206 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700207
208 int[] limits = null;
209 if (sc != null) {
210 limits = new int[6];
211
212 limits[0] = sc.xstart;
213 limits[1] = sc.xend;
214 limits[2] = sc.ystart;
215 limits[3] = sc.yend;
216 limits[4] = sc.zstart;
217 limits[5] = sc.zend;
218 }
219
220 mRS.nScriptForEach(getID(mRS), slot, in_ids, out_id, params, limits);
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800221 }
Jason Sams4d339932010-05-11 14:03:58 -0700222
Chris Wailes94961062014-06-11 12:01:28 -0700223 /**
224 * Only intended for use by generated reflected code.
Chris Wailes94961062014-06-11 12:01:28 -0700225 */
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700226 protected void forEach(int slot, Allocation[] ains, Allocation aout,
227 FieldPacker v) {
Jason Sams6a420b52015-03-30 15:31:26 -0700228
229 // FieldPacker is kept here to support regular params in the future.
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700230 forEach(slot, ains, aout, v, null);
Chris Wailes94961062014-06-11 12:01:28 -0700231 }
232
233 /**
234 * Only intended for use by generated reflected code.
Chris Wailes94961062014-06-11 12:01:28 -0700235 */
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700236 protected void forEach(int slot, Allocation[] ains, Allocation aout,
237 FieldPacker v, LaunchOptions sc) {
238 // TODO: Is this necessary if nScriptForEach calls validate as well?
Jason Sams6a420b52015-03-30 15:31:26 -0700239 // FieldPacker is kept here to support regular params in the future.
Chris Wailes94961062014-06-11 12:01:28 -0700240 mRS.validate();
Andreas Gampec8ddcdd2015-03-15 15:57:30 -0700241 if (ains != null) {
242 for (Allocation ain : ains) {
243 mRS.validateObject(ain);
244 }
Chris Wailes94961062014-06-11 12:01:28 -0700245 }
Stephen Hinesc9c7daf2014-08-13 17:32:19 +0000246 mRS.validateObject(aout);
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700247
Chris Wailes94961062014-06-11 12:01:28 -0700248 if (ains == null && aout == null) {
249 throw new RSIllegalArgumentException(
250 "At least one of ain or aout is required to be non-null.");
251 }
252
Andreas Gampead555f92015-03-17 20:05:46 -0700253 long[] in_ids;
254 if (ains != null) {
255 in_ids = new long[ains.length];
256 for (int index = 0; index < ains.length; ++index) {
257 in_ids[index] = ains[index].getID(mRS);
258 }
259 } else {
260 in_ids = null;
Chris Wailes94961062014-06-11 12:01:28 -0700261 }
262
263 long out_id = 0;
264 if (aout != null) {
265 out_id = aout.getID(mRS);
266 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700267
Chris Wailes94961062014-06-11 12:01:28 -0700268 byte[] params = null;
269 if (v != null) {
270 params = v.getData();
271 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700272
273 int[] limits = null;
274 if (sc != null) {
275 limits = new int[6];
276
277 limits[0] = sc.xstart;
278 limits[1] = sc.xend;
279 limits[2] = sc.ystart;
280 limits[3] = sc.yend;
281 limits[4] = sc.zstart;
282 limits[5] = sc.zend;
283 }
284
285 mRS.nScriptForEach(getID(mRS), slot, in_ids, out_id, params, limits);
Chris Wailes94961062014-06-11 12:01:28 -0700286 }
287
Matt Wala36eb1f72015-07-20 15:35:27 -0700288 /**
David Gross26ef7a732016-01-12 12:19:15 -0800289 * Only intended for use by generated reflected code. (Simple reduction)
Matt Wala36eb1f72015-07-20 15:35:27 -0700290 *
291 * @hide
292 */
293 protected void reduce(int slot, Allocation ain, Allocation aout, LaunchOptions sc) {
294 mRS.validate();
295 mRS.validateObject(ain);
296 mRS.validateObject(aout);
297
298 if (ain == null || aout == null) {
299 throw new RSIllegalArgumentException(
300 "Both ain and aout are required to be non-null.");
301 }
302
303 long in_id = ain.getID(mRS);
304 long out_id = aout.getID(mRS);
305
306 int[] limits = null;
307 if (sc != null) {
308 limits = new int[2];
309
310 limits[0] = sc.xstart;
311 limits[1] = sc.xend;
312 }
313
314 mRS.nScriptReduce(getID(mRS), slot, in_id, out_id, limits);
315 }
316
David Gross26ef7a732016-01-12 12:19:15 -0800317 /**
318 * Only intended for use by generated reflected code. (General reduction)
319 *
David Gross26ef7a732016-01-12 12:19:15 -0800320 */
321 protected void reduce(int slot, Allocation[] ains, Allocation aout, LaunchOptions sc) {
322 mRS.validate();
323 if (ains == null || ains.length < 1) {
324 throw new RSIllegalArgumentException(
325 "At least one input is required.");
326 }
327 if (aout == null) {
328 throw new RSIllegalArgumentException(
329 "aout is required to be non-null.");
330 }
331 for (Allocation ain : ains) {
332 mRS.validateObject(ain);
333 }
334
335 long[] in_ids = new long[ains.length];
336 for (int index = 0; index < ains.length; ++index) {
337 in_ids[index] = ains[index].getID(mRS);
338 }
339 long out_id = aout.getID(mRS);
340
341 int[] limits = null;
342 if (sc != null) {
343 limits = new int[6];
344
345 limits[0] = sc.xstart;
346 limits[1] = sc.xend;
347 limits[2] = sc.ystart;
348 limits[3] = sc.yend;
349 limits[4] = sc.zstart;
350 limits[5] = sc.zend;
351 }
352
353 mRS.nScriptReduceNew(getID(mRS), slot, in_ids, out_id, limits);
354 }
355
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700356 long[] mInIdsBuffer;
357
Tim Murray7a629fa2013-11-19 12:45:54 -0800358 Script(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700359 super(id, rs);
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700360
361 mInIdsBuffer = new long[1];
Yang Ni6484b6b2016-03-24 09:40:32 -0700362
363 /* The constructors for the derived classes (including ScriptIntrinsic
364 * derived classes and ScriptC derived classes generated by Slang
365 * reflection) seem to be simple enough, so we just put the guard.open()
366 * call here, rather than in the end of the constructor for the derived
367 * class. This, of course, assumes the derived constructor would not
368 * throw any exception after calling this constructor.
369 *
370 * If new derived classes are added with more complicated constructors
371 * that throw exceptions, this call has to be (duplicated and) moved
372 * to the end of each derived class constructor.
373 */
374 guard.open("destroy");
Jason Sams69f0d312009-08-03 18:11:17 -0700375 }
376
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700377 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800378 * Only intended for use by generated reflected code.
379 *
Jason Sams67e3d202011-01-09 13:49:01 -0800380 */
Jason Sams69f0d312009-08-03 18:11:17 -0700381 public void bindAllocation(Allocation va, int slot) {
Jason Sams771bebb2009-12-07 12:40:12 -0800382 mRS.validate();
Jason Sams678cc7f2014-03-05 16:09:02 -0800383 mRS.validateObject(va);
Jason Sams4d339932010-05-11 14:03:58 -0700384 if (va != null) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700385
386 android.content.Context context = mRS.getApplicationContext();
387
388 if (context.getApplicationInfo().targetSdkVersion >= 20) {
Jason Samscf9c8942014-01-14 16:18:14 -0800389 final Type t = va.mType;
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700390 if (t.hasMipmaps() || t.hasFaces() || (t.getY() != 0) ||
391 (t.getZ() != 0)) {
392
Jason Samscf9c8942014-01-14 16:18:14 -0800393 throw new RSIllegalArgumentException(
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700394 "API 20+ only allows simple 1D allocations to be " +
395 "used with bind.");
Jason Samscf9c8942014-01-14 16:18:14 -0800396 }
397 }
Jason Samse07694b2012-04-03 15:36:36 -0700398 mRS.nScriptBindAllocation(getID(mRS), va.getID(mRS), slot);
Jason Sams4d339932010-05-11 14:03:58 -0700399 } else {
Jason Samse07694b2012-04-03 15:36:36 -0700400 mRS.nScriptBindAllocation(getID(mRS), 0, slot);
Jason Sams4d339932010-05-11 14:03:58 -0700401 }
402 }
403
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700404 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800405 * Only intended for use by generated reflected code.
406 *
Jason Sams67e3d202011-01-09 13:49:01 -0800407 */
Jason Sams4d339932010-05-11 14:03:58 -0700408 public void setVar(int index, float v) {
Jason Samse07694b2012-04-03 15:36:36 -0700409 mRS.nScriptSetVarF(getID(mRS), index, v);
Jason Sams4d339932010-05-11 14:03:58 -0700410 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700411 public float getVarF(int index) {
412 return mRS.nScriptGetVarF(getID(mRS), index);
413 }
Jason Sams4d339932010-05-11 14:03:58 -0700414
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700415 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800416 * Only intended for use by generated reflected code.
417 *
Jason Sams67e3d202011-01-09 13:49:01 -0800418 */
Stephen Hinesca54ec32010-09-20 17:20:30 -0700419 public void setVar(int index, double v) {
Jason Samse07694b2012-04-03 15:36:36 -0700420 mRS.nScriptSetVarD(getID(mRS), index, v);
Stephen Hinesca54ec32010-09-20 17:20:30 -0700421 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700422 public double getVarD(int index) {
423 return mRS.nScriptGetVarD(getID(mRS), index);
424 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700425
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700426 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800427 * Only intended for use by generated reflected code.
428 *
Jason Sams67e3d202011-01-09 13:49:01 -0800429 */
Jason Sams4d339932010-05-11 14:03:58 -0700430 public void setVar(int index, int v) {
Jason Samse07694b2012-04-03 15:36:36 -0700431 mRS.nScriptSetVarI(getID(mRS), index, v);
Jason Sams4d339932010-05-11 14:03:58 -0700432 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700433 public int getVarI(int index) {
434 return mRS.nScriptGetVarI(getID(mRS), index);
435 }
436
Jason Sams4d339932010-05-11 14:03:58 -0700437
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700438 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800439 * Only intended for use by generated reflected code.
440 *
Jason Sams67e3d202011-01-09 13:49:01 -0800441 */
Stephen Hines031ec58c2010-10-11 10:54:21 -0700442 public void setVar(int index, long v) {
Jason Samse07694b2012-04-03 15:36:36 -0700443 mRS.nScriptSetVarJ(getID(mRS), index, v);
Stephen Hines031ec58c2010-10-11 10:54:21 -0700444 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700445 public long getVarJ(int index) {
446 return mRS.nScriptGetVarJ(getID(mRS), index);
447 }
448
Stephen Hines031ec58c2010-10-11 10:54:21 -0700449
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700450 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800451 * Only intended for use by generated reflected code.
452 *
Jason Sams67e3d202011-01-09 13:49:01 -0800453 */
Jason Sams0b9a22c2010-07-02 15:35:19 -0700454 public void setVar(int index, boolean v) {
Jason Samse07694b2012-04-03 15:36:36 -0700455 mRS.nScriptSetVarI(getID(mRS), index, v ? 1 : 0);
Jason Sams0b9a22c2010-07-02 15:35:19 -0700456 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700457 public boolean getVarB(int index) {
458 return mRS.nScriptGetVarI(getID(mRS), index) > 0 ? true : false;
459 }
Jason Sams0b9a22c2010-07-02 15:35:19 -0700460
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700461 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800462 * Only intended for use by generated reflected code.
463 *
Jason Sams67e3d202011-01-09 13:49:01 -0800464 */
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800465 public void setVar(int index, BaseObj o) {
Jason Sams678cc7f2014-03-05 16:09:02 -0800466 mRS.validate();
467 mRS.validateObject(o);
Jason Samse07694b2012-04-03 15:36:36 -0700468 mRS.nScriptSetVarObj(getID(mRS), index, (o == null) ? 0 : o.getID(mRS));
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800469 }
470
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700471 /**
Jason Sams67e3d202011-01-09 13:49:01 -0800472 * Only intended for use by generated reflected code.
473 *
Jason Sams67e3d202011-01-09 13:49:01 -0800474 */
Jason Sams4d339932010-05-11 14:03:58 -0700475 public void setVar(int index, FieldPacker v) {
Jason Samse07694b2012-04-03 15:36:36 -0700476 mRS.nScriptSetVarV(getID(mRS), index, v.getData());
Jason Sams69f0d312009-08-03 18:11:17 -0700477 }
478
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700479 /**
Stephen Hinesadeb8092012-04-20 14:26:06 -0700480 * Only intended for use by generated reflected code.
481 *
Stephen Hinesadeb8092012-04-20 14:26:06 -0700482 */
483 public void setVar(int index, FieldPacker v, Element e, int[] dims) {
484 mRS.nScriptSetVarVE(getID(mRS), index, v.getData(), e.getID(mRS), dims);
485 }
486
Jason Samsf64cca92013-04-19 12:56:37 -0700487 /**
488 * Only intended for use by generated reflected code.
489 *
Jason Samsf64cca92013-04-19 12:56:37 -0700490 */
Tim Murray7c4caad2013-04-10 16:21:40 -0700491 public void getVarV(int index, FieldPacker v) {
492 mRS.nScriptGetVarV(getID(mRS), index, v.getData());
493 }
494
Jason Sams22534172009-08-04 16:58:20 -0700495 public void setTimeZone(String timeZone) {
Jason Sams771bebb2009-12-07 12:40:12 -0800496 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -0700497 try {
Jason Samse07694b2012-04-03 15:36:36 -0700498 mRS.nScriptSetTimeZone(getID(mRS), timeZone.getBytes("UTF-8"));
Jason Sams22534172009-08-04 16:58:20 -0700499 } catch (java.io.UnsupportedEncodingException e) {
500 throw new RuntimeException(e);
501 }
502 }
Jason Sams69f0d312009-08-03 18:11:17 -0700503
Tim Murrayc11e25c2013-04-09 11:01:01 -0700504 /**
505 * Only intended for use by generated reflected code.
506 *
507 */
Jason Sams69f0d312009-08-03 18:11:17 -0700508 public static class Builder {
509 RenderScript mRS;
Jason Sams69f0d312009-08-03 18:11:17 -0700510
511 Builder(RenderScript rs) {
512 mRS = rs;
513 }
Jason Sams69f0d312009-08-03 18:11:17 -0700514 }
515
Jason Sams2d71bc72010-03-26 16:06:43 -0700516
Jason Samsf64cca92013-04-19 12:56:37 -0700517 /**
518 * Only intended for use by generated reflected code.
519 *
520 */
Jason Sams2d71bc72010-03-26 16:06:43 -0700521 public static class FieldBase {
522 protected Element mElement;
Jason Sams2d71bc72010-03-26 16:06:43 -0700523 protected Allocation mAllocation;
524
525 protected void init(RenderScript rs, int dimx) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700526 mAllocation = Allocation.createSized(rs, mElement, dimx,
527 Allocation.USAGE_SCRIPT);
Jason Sams5476b452010-12-08 16:14:36 -0800528 }
529
530 protected void init(RenderScript rs, int dimx, int usages) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700531 mAllocation =
532 Allocation.createSized(rs, mElement, dimx,
533 Allocation.USAGE_SCRIPT | usages);
Jason Sams2d71bc72010-03-26 16:06:43 -0700534 }
535
536 protected FieldBase() {
537 }
538
539 public Element getElement() {
540 return mElement;
541 }
542
543 public Type getType() {
Jason Sams31a7e422010-10-26 13:09:17 -0700544 return mAllocation.getType();
Jason Sams2d71bc72010-03-26 16:06:43 -0700545 }
546
547 public Allocation getAllocation() {
548 return mAllocation;
549 }
550
551 //@Override
552 public void updateAllocation() {
553 }
Jason Sams2d71bc72010-03-26 16:06:43 -0700554 }
Tim Murrayfbfaa852012-12-14 16:01:58 -0800555
Jason Samsf64cca92013-04-19 12:56:37 -0700556
557 /**
Jason Sams8610f832015-03-30 17:01:10 -0700558 * Class for specifying the specifics about how a kernel will be
Miao Wang53fdcfb2016-03-29 15:14:21 -0700559 * launched.
Jason Sams8610f832015-03-30 17:01:10 -0700560 *
561 * This class can specify a potential range of cells on which to
562 * run a kernel. If no set is called for a dimension then this
563 * class will have no impact on that dimension when the kernel
564 * is executed.
565 *
Miao Wang53fdcfb2016-03-29 15:14:21 -0700566 * The forEach kernel launch will operate over the intersection of
567 * the dimensions.
Jason Sams8610f832015-03-30 17:01:10 -0700568 *
569 * Example:
570 * LaunchOptions with setX(5, 15)
571 * Allocation with dimension X=10, Y=10
Miao Wang53fdcfb2016-03-29 15:14:21 -0700572 * The resulting forEach run would execute over:
573 * x = 5 to 9 (inclusive) and
574 * y = 0 to 9 (inclusive).
Jason Sams8610f832015-03-30 17:01:10 -0700575 *
Jason Samsf64cca92013-04-19 12:56:37 -0700576 *
577 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800578 public static final class LaunchOptions {
Jason Samsf64cca92013-04-19 12:56:37 -0700579 private int xstart = 0;
580 private int ystart = 0;
581 private int xend = 0;
582 private int yend = 0;
583 private int zstart = 0;
584 private int zend = 0;
585 private int strategy;
Tim Murrayfbfaa852012-12-14 16:01:58 -0800586
Jason Samsf64cca92013-04-19 12:56:37 -0700587 /**
Miao Wang53fdcfb2016-03-29 15:14:21 -0700588 * Set the X range. xstartArg is the lowest coordinate of the range,
589 * and xendArg-1 is the highest coordinate of the range.
Jason Samsf64cca92013-04-19 12:56:37 -0700590 *
591 * @param xstartArg Must be >= 0
Miao Wang53fdcfb2016-03-29 15:14:21 -0700592 * @param xendArg Must be > xstartArg
Jason Samsf64cca92013-04-19 12:56:37 -0700593 *
594 * @return LaunchOptions
595 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800596 public LaunchOptions setX(int xstartArg, int xendArg) {
Tim Murrayfbfaa852012-12-14 16:01:58 -0800597 if (xstartArg < 0 || xendArg <= xstartArg) {
598 throw new RSIllegalArgumentException("Invalid dimensions");
599 }
600 xstart = xstartArg;
601 xend = xendArg;
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800602 return this;
Tim Murrayfbfaa852012-12-14 16:01:58 -0800603 }
604
Jason Samsf64cca92013-04-19 12:56:37 -0700605 /**
Miao Wang53fdcfb2016-03-29 15:14:21 -0700606 * Set the Y range. ystartArg is the lowest coordinate of the range,
607 * and yendArg-1 is the highest coordinate of the range.
Jason Samsf64cca92013-04-19 12:56:37 -0700608 *
609 * @param ystartArg Must be >= 0
Miao Wang53fdcfb2016-03-29 15:14:21 -0700610 * @param yendArg Must be > ystartArg
Jason Samsf64cca92013-04-19 12:56:37 -0700611 *
612 * @return LaunchOptions
613 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800614 public LaunchOptions setY(int ystartArg, int yendArg) {
Tim Murrayfbfaa852012-12-14 16:01:58 -0800615 if (ystartArg < 0 || yendArg <= ystartArg) {
616 throw new RSIllegalArgumentException("Invalid dimensions");
617 }
618 ystart = ystartArg;
619 yend = yendArg;
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800620 return this;
Tim Murrayfbfaa852012-12-14 16:01:58 -0800621 }
622
Jason Samsf64cca92013-04-19 12:56:37 -0700623 /**
Miao Wang53fdcfb2016-03-29 15:14:21 -0700624 * Set the Z range. zstartArg is the lowest coordinate of the range,
625 * and zendArg-1 is the highest coordinate of the range.
Jason Samsf64cca92013-04-19 12:56:37 -0700626 *
627 * @param zstartArg Must be >= 0
Miao Wang53fdcfb2016-03-29 15:14:21 -0700628 * @param zendArg Must be > zstartArg
Jason Samsf64cca92013-04-19 12:56:37 -0700629 *
630 * @return LaunchOptions
631 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800632 public LaunchOptions setZ(int zstartArg, int zendArg) {
633 if (zstartArg < 0 || zendArg <= zstartArg) {
634 throw new RSIllegalArgumentException("Invalid dimensions");
635 }
636 zstart = zstartArg;
637 zend = zendArg;
638 return this;
639 }
640
641
Jason Samsf64cca92013-04-19 12:56:37 -0700642 /**
643 * Returns the current X start
644 *
645 * @return int current value
646 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800647 public int getXStart() {
648 return xstart;
649 }
Jason Samsf64cca92013-04-19 12:56:37 -0700650 /**
651 * Returns the current X end
652 *
653 * @return int current value
654 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800655 public int getXEnd() {
656 return xend;
657 }
Jason Samsf64cca92013-04-19 12:56:37 -0700658 /**
659 * Returns the current Y start
660 *
661 * @return int current value
662 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800663 public int getYStart() {
664 return ystart;
665 }
Jason Samsf64cca92013-04-19 12:56:37 -0700666 /**
667 * Returns the current Y end
668 *
669 * @return int current value
670 */
Tim Murrayfbfaa852012-12-14 16:01:58 -0800671 public int getYEnd() {
672 return yend;
673 }
Jason Samsf64cca92013-04-19 12:56:37 -0700674 /**
675 * Returns the current Z start
676 *
677 * @return int current value
678 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800679 public int getZStart() {
680 return zstart;
681 }
Jason Samsf64cca92013-04-19 12:56:37 -0700682 /**
683 * Returns the current Z end
684 *
685 * @return int current value
686 */
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800687 public int getZEnd() {
688 return zend;
689 }
Tim Murrayfbfaa852012-12-14 16:01:58 -0800690
691 }
Jason Sams69f0d312009-08-03 18:11:17 -0700692}