2 * Copyright (C) 2008-2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.renderscript;
19 import android.util.SparseArray;
22 * The parent class for all executable scripts. This should not be used by
25 public class Script extends BaseObj {
28 * KernelID is an identifier for a Script + root function pair. It is used
29 * as an identifier for ScriptGroup creation.
31 * This class should not be directly created. Instead use the method in the
32 * reflected or intrinsic code "getKernelID_funcname()".
35 public static final class KernelID extends BaseObj {
39 KernelID(long id, RenderScript rs, Script s, int slot, int sig) {
47 private final SparseArray<KernelID> mKIDs = new SparseArray<KernelID>();
49 * Only to be used by generated reflected classes.
51 protected KernelID createKernelID(int slot, int sig, Element ein,
53 KernelID k = mKIDs.get(slot);
58 long id = mRS.nScriptKernelIDCreate(getID(mRS), slot, sig);
60 throw new RSDriverException("Failed to create KernelID");
63 k = new KernelID(id, mRS, this, slot, sig);
69 * InvokeID is an identifier for an invoke function. It is used
70 * as an identifier for ScriptGroup creation.
72 * This class should not be directly created. Instead use the method in the
73 * reflected or intrinsic code "getInvokeID_funcname()".
76 public static final class InvokeID extends BaseObj {
79 InvokeID(long id, RenderScript rs, Script s, int slot) {
86 private final SparseArray<InvokeID> mIIDs = new SparseArray<InvokeID>();
88 * Only to be used by generated reflected classes.
90 protected InvokeID createInvokeID(int slot) {
91 InvokeID i = mIIDs.get(slot);
96 long id = mRS.nScriptInvokeIDCreate(getID(mRS), slot);
98 throw new RSDriverException("Failed to create KernelID");
101 i = new InvokeID(id, mRS, this, slot);
107 * FieldID is an identifier for a Script + exported field pair. It is used
108 * as an identifier for ScriptGroup creation.
110 * This class should not be directly created. Instead use the method in the
111 * reflected or intrinsic code "getFieldID_funcname()".
114 public static final class FieldID extends BaseObj {
117 FieldID(long id, RenderScript rs, Script s, int slot) {
124 private final SparseArray<FieldID> mFIDs = new SparseArray();
126 * Only to be used by generated reflected classes.
128 protected FieldID createFieldID(int slot, Element e) {
129 FieldID f = mFIDs.get(slot);
134 long id = mRS.nScriptFieldIDCreate(getID(mRS), slot);
136 throw new RSDriverException("Failed to create FieldID");
139 f = new FieldID(id, mRS, this, slot);
146 * Only intended for use by generated reflected code.
149 protected void invoke(int slot) {
150 mRS.nScriptInvoke(getID(mRS), slot);
154 * Only intended for use by generated reflected code.
157 protected void invoke(int slot, FieldPacker v) {
159 mRS.nScriptInvokeV(getID(mRS), slot, v.getData());
161 mRS.nScriptInvoke(getID(mRS), slot);
166 * Only intended for use by generated reflected code.
169 protected void forEach(int slot, Allocation ain, Allocation aout,
171 forEach(slot, ain, aout, v, null);
175 * Only intended for use by generated reflected code.
178 protected void forEach(int slot, Allocation ain, Allocation aout,
179 FieldPacker v, LaunchOptions sc) {
180 // TODO: Is this necessary if nScriptForEach calls validate as well?
182 mRS.validateObject(ain);
183 mRS.validateObject(aout);
185 if (ain == null && aout == null && sc == null) {
186 throw new RSIllegalArgumentException(
187 "At least one of input allocation, output allocation, or LaunchOptions is required to be non-null.");
190 long[] in_ids = null;
192 in_ids = mInIdsBuffer;
193 in_ids[0] = ain.getID(mRS);
198 out_id = aout.getID(mRS);
201 byte[] params = null;
203 params = v.getData();
210 limits[0] = sc.xstart;
212 limits[2] = sc.ystart;
214 limits[4] = sc.zstart;
218 mRS.nScriptForEach(getID(mRS), slot, in_ids, out_id, params, limits);
222 * Only intended for use by generated reflected code.
224 protected void forEach(int slot, Allocation[] ains, Allocation aout,
227 // FieldPacker is kept here to support regular params in the future.
228 forEach(slot, ains, aout, v, null);
232 * Only intended for use by generated reflected code.
234 protected void forEach(int slot, Allocation[] ains, Allocation aout,
235 FieldPacker v, LaunchOptions sc) {
236 // TODO: Is this necessary if nScriptForEach calls validate as well?
237 // FieldPacker is kept here to support regular params in the future.
240 for (Allocation ain : ains) {
241 mRS.validateObject(ain);
244 mRS.validateObject(aout);
246 if (ains == null && aout == null) {
247 throw new RSIllegalArgumentException(
248 "At least one of ain or aout is required to be non-null.");
253 in_ids = new long[ains.length];
254 for (int index = 0; index < ains.length; ++index) {
255 in_ids[index] = ains[index].getID(mRS);
263 out_id = aout.getID(mRS);
266 byte[] params = null;
268 params = v.getData();
275 limits[0] = sc.xstart;
277 limits[2] = sc.ystart;
279 limits[4] = sc.zstart;
283 mRS.nScriptForEach(getID(mRS), slot, in_ids, out_id, params, limits);
287 * Only intended for use by generated reflected code.
291 protected void reduce(int slot, Allocation ain, Allocation aout, LaunchOptions sc) {
293 mRS.validateObject(ain);
294 mRS.validateObject(aout);
296 if (ain == null || aout == null) {
297 throw new RSIllegalArgumentException(
298 "Both ain and aout are required to be non-null.");
301 long in_id = ain.getID(mRS);
302 long out_id = aout.getID(mRS);
308 limits[0] = sc.xstart;
312 mRS.nScriptReduce(getID(mRS), slot, in_id, out_id, limits);
317 Script(long id, RenderScript rs) {
320 mInIdsBuffer = new long[1];
324 * Only intended for use by generated reflected code.
327 public void bindAllocation(Allocation va, int slot) {
329 mRS.validateObject(va);
332 android.content.Context context = mRS.getApplicationContext();
334 if (context.getApplicationInfo().targetSdkVersion >= 20) {
335 final Type t = va.mType;
336 if (t.hasMipmaps() || t.hasFaces() || (t.getY() != 0) ||
339 throw new RSIllegalArgumentException(
340 "API 20+ only allows simple 1D allocations to be " +
344 mRS.nScriptBindAllocation(getID(mRS), va.getID(mRS), slot);
346 mRS.nScriptBindAllocation(getID(mRS), 0, slot);
351 * Only intended for use by generated reflected code.
354 public void setVar(int index, float v) {
355 mRS.nScriptSetVarF(getID(mRS), index, v);
357 public float getVarF(int index) {
358 return mRS.nScriptGetVarF(getID(mRS), index);
362 * Only intended for use by generated reflected code.
365 public void setVar(int index, double v) {
366 mRS.nScriptSetVarD(getID(mRS), index, v);
368 public double getVarD(int index) {
369 return mRS.nScriptGetVarD(getID(mRS), index);
373 * Only intended for use by generated reflected code.
376 public void setVar(int index, int v) {
377 mRS.nScriptSetVarI(getID(mRS), index, v);
379 public int getVarI(int index) {
380 return mRS.nScriptGetVarI(getID(mRS), index);
385 * Only intended for use by generated reflected code.
388 public void setVar(int index, long v) {
389 mRS.nScriptSetVarJ(getID(mRS), index, v);
391 public long getVarJ(int index) {
392 return mRS.nScriptGetVarJ(getID(mRS), index);
397 * Only intended for use by generated reflected code.
400 public void setVar(int index, boolean v) {
401 mRS.nScriptSetVarI(getID(mRS), index, v ? 1 : 0);
403 public boolean getVarB(int index) {
404 return mRS.nScriptGetVarI(getID(mRS), index) > 0 ? true : false;
408 * Only intended for use by generated reflected code.
411 public void setVar(int index, BaseObj o) {
413 mRS.validateObject(o);
414 mRS.nScriptSetVarObj(getID(mRS), index, (o == null) ? 0 : o.getID(mRS));
418 * Only intended for use by generated reflected code.
421 public void setVar(int index, FieldPacker v) {
422 mRS.nScriptSetVarV(getID(mRS), index, v.getData());
426 * Only intended for use by generated reflected code.
429 public void setVar(int index, FieldPacker v, Element e, int[] dims) {
430 mRS.nScriptSetVarVE(getID(mRS), index, v.getData(), e.getID(mRS), dims);
434 * Only intended for use by generated reflected code.
437 public void getVarV(int index, FieldPacker v) {
438 mRS.nScriptGetVarV(getID(mRS), index, v.getData());
441 public void setTimeZone(String timeZone) {
444 mRS.nScriptSetTimeZone(getID(mRS), timeZone.getBytes("UTF-8"));
445 } catch (java.io.UnsupportedEncodingException e) {
446 throw new RuntimeException(e);
451 * Only intended for use by generated reflected code.
454 public static class Builder {
457 Builder(RenderScript rs) {
464 * Only intended for use by generated reflected code.
467 public static class FieldBase {
468 protected Element mElement;
469 protected Allocation mAllocation;
471 protected void init(RenderScript rs, int dimx) {
472 mAllocation = Allocation.createSized(rs, mElement, dimx,
473 Allocation.USAGE_SCRIPT);
476 protected void init(RenderScript rs, int dimx, int usages) {
478 Allocation.createSized(rs, mElement, dimx,
479 Allocation.USAGE_SCRIPT | usages);
482 protected FieldBase() {
485 public Element getElement() {
489 public Type getType() {
490 return mAllocation.getType();
493 public Allocation getAllocation() {
498 public void updateAllocation() {
504 * Class for specifying the specifics about how a kernel will be
507 * This class can specify a potential range of cells on which to
508 * run a kernel. If no set is called for a dimension then this
509 * class will have no impact on that dimension when the kernel
512 * The forEach launch will operate over the intersection of the
516 * LaunchOptions with setX(5, 15)
517 * Allocation with dimension X=10, Y=10
518 * The resulting forEach run would execute over x = 5 to 10 and
523 public static final class LaunchOptions {
524 private int xstart = 0;
525 private int ystart = 0;
526 private int xend = 0;
527 private int yend = 0;
528 private int zstart = 0;
529 private int zend = 0;
530 private int strategy;
533 * Set the X range. If the end value is set to 0 the X dimension is not
536 * @param xstartArg Must be >= 0
537 * @param xendArg Must be >= xstartArg
539 * @return LaunchOptions
541 public LaunchOptions setX(int xstartArg, int xendArg) {
542 if (xstartArg < 0 || xendArg <= xstartArg) {
543 throw new RSIllegalArgumentException("Invalid dimensions");
551 * Set the Y range. If the end value is set to 0 the Y dimension is not
554 * @param ystartArg Must be >= 0
555 * @param yendArg Must be >= ystartArg
557 * @return LaunchOptions
559 public LaunchOptions setY(int ystartArg, int yendArg) {
560 if (ystartArg < 0 || yendArg <= ystartArg) {
561 throw new RSIllegalArgumentException("Invalid dimensions");
569 * Set the Z range. If the end value is set to 0 the Z dimension is not
572 * @param zstartArg Must be >= 0
573 * @param zendArg Must be >= zstartArg
575 * @return LaunchOptions
577 public LaunchOptions setZ(int zstartArg, int zendArg) {
578 if (zstartArg < 0 || zendArg <= zstartArg) {
579 throw new RSIllegalArgumentException("Invalid dimensions");
588 * Returns the current X start
590 * @return int current value
592 public int getXStart() {
596 * Returns the current X end
598 * @return int current value
600 public int getXEnd() {
604 * Returns the current Y start
606 * @return int current value
608 public int getYStart() {
612 * Returns the current Y end
614 * @return int current value
616 public int getYEnd() {
620 * Returns the current Z start
622 * @return int current value
624 public int getZStart() {
628 * Returns the current Z end
630 * @return int current value
632 public int getZEnd() {