Add clipping for intrinsics.

Change-Id: Ic7e1da3c69aa3d5f990816178739120b6059e7a5
diff --git a/rs/java/android/renderscript/ScriptIntrinsic3DLUT.java b/rs/java/android/renderscript/ScriptIntrinsic3DLUT.java
index 33e64bd..ce149d9 100644
--- a/rs/java/android/renderscript/ScriptIntrinsic3DLUT.java
+++ b/rs/java/android/renderscript/ScriptIntrinsic3DLUT.java
@@ -84,10 +84,23 @@
      * @param aout Output allocation
      */
     public void forEach(Allocation ain, Allocation aout) {
-        forEach(0, ain, aout, null);
+        forEach(ain, aout, null);
     }
 
     /**
+     * Invoke the kernel and apply the lookup to each cell of ain
+     * and copy to aout.
+     *
+     * @param ain Input allocation
+     * @param aout Output allocation
+     * @param opt Launch options for kernel
+     */
+    public void forEach(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        forEach(0, ain, aout, null, opt);
+    }
+
+
+    /**
      * Get a KernelID for this intrinsic kernel.
      *
      * @return Script.KernelID The KernelID object.
diff --git a/rs/java/android/renderscript/ScriptIntrinsicBlend.java b/rs/java/android/renderscript/ScriptIntrinsicBlend.java
index 40f1a3e..d4038c2 100644
--- a/rs/java/android/renderscript/ScriptIntrinsicBlend.java
+++ b/rs/java/android/renderscript/ScriptIntrinsicBlend.java
@@ -40,14 +40,14 @@
 
     }
 
-    private void blend(int id, Allocation ain, Allocation aout) {
+    private void blend(int id, Allocation ain, Allocation aout, Script.LaunchOptions opt) {
         if (!ain.getElement().isCompatible(Element.U8_4(mRS))) {
             throw new RSIllegalArgumentException("Input is not of expected format.");
         }
         if (!aout.getElement().isCompatible(Element.U8_4(mRS))) {
             throw new RSIllegalArgumentException("Output is not of expected format.");
         }
-        forEach(id, ain, aout, null);
+        forEach(id, ain, aout, null, opt);
     }
 
     /**
@@ -57,7 +57,18 @@
      * @param aout The destination buffer
      */
     public void forEachClear(Allocation ain, Allocation aout) {
-        blend(0, ain, aout);
+        forEachClear(ain, aout, null);
+    }
+
+    /**
+     * Sets dst = {0, 0, 0, 0}
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachClear(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        blend(0, ain, aout, opt);
     }
 
     /**
@@ -77,7 +88,18 @@
      * @param aout The destination buffer
      */
     public void forEachSrc(Allocation ain, Allocation aout) {
-        blend(1, ain, aout);
+        forEachSrc(ain, aout, null);
+    }
+
+    /**
+     * Sets dst = src
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachSrc(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        forEachDst(ain, aout, null);
     }
 
     /**
@@ -102,6 +124,19 @@
     }
 
     /**
+     * Sets dst = dst
+     *
+     * This is a NOP.
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachDst(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        // N, optOP
+    }
+
+    /**
      * Get a KernelID for the Dst kernel.
      *
      * @return Script.KernelID The KernelID object.
@@ -117,7 +152,18 @@
      * @param aout The destination buffer
      */
     public void forEachSrcOver(Allocation ain, Allocation aout) {
-        blend(3, ain, aout);
+        forEachSrcOver(ain, aout, null);
+    }
+
+    /**
+     * Sets dst = src + dst * (1.0 - src.a)
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachSrcOver(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        blend(3, ain, aout, opt);
     }
 
     /**
@@ -136,7 +182,18 @@
      * @param aout The destination buffer
      */
     public void forEachDstOver(Allocation ain, Allocation aout) {
-        blend(4, ain, aout);
+        forEachDstOver(ain, aout, null);
+    }
+
+    /**
+     * Sets dst = dst + src * (1.0 - dst.a)
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachDstOver(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        blend(4, ain, aout, opt);
     }
 
     /**
@@ -155,7 +212,18 @@
      * @param aout The destination buffer
      */
     public void forEachSrcIn(Allocation ain, Allocation aout) {
-        blend(5, ain, aout);
+        forEachSrcIn(ain, aout, null);
+    }
+
+    /**
+     * Sets dst = src * dst.a
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachSrcIn(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        blend(5, ain, aout, opt);
     }
 
     /**
@@ -174,7 +242,18 @@
      * @param aout The destination buffer
      */
     public void forEachDstIn(Allocation ain, Allocation aout) {
-        blend(6, ain, aout);
+        forEachDstIn(ain, aout, null);
+    }
+
+    /**
+     * Sets dst = dst * src.a
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachDstIn(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        blend(6, ain, aout, opt);
     }
 
     /**
@@ -193,7 +272,18 @@
      * @param aout The destination buffer
      */
     public void forEachSrcOut(Allocation ain, Allocation aout) {
-        blend(7, ain, aout);
+        forEachSrcOut(ain, aout, null);
+    }
+
+    /**
+     * Sets dst = src * (1.0 - dst.a)
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachSrcOut(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        blend(7, ain, aout, opt);
     }
 
     /**
@@ -212,7 +302,18 @@
      * @param aout The destination buffer
      */
     public void forEachDstOut(Allocation ain, Allocation aout) {
-        blend(8, ain, aout);
+        forEachDstOut(ain, aout, null);
+    }
+
+    /**
+     * Sets dst = dst * (1.0 - src.a)
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachDstOut(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        blend(8, ain, aout, opt);
     }
 
     /**
@@ -232,7 +333,19 @@
      * @param aout The destination buffer
      */
     public void forEachSrcAtop(Allocation ain, Allocation aout) {
-        blend(9, ain, aout);
+        forEachSrcAtop(ain, aout, null);
+    }
+
+    /**
+     * dst.rgb = src.rgb * dst.a + (1.0 - src.a) * dst.rgb
+     * dst.a = dst.a
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachSrcAtop(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        blend(9, ain, aout, opt);
     }
 
     /**
@@ -252,7 +365,19 @@
      * @param aout The destination buffer
      */
     public void forEachDstAtop(Allocation ain, Allocation aout) {
-        blend(10, ain, aout);
+        forEachDstAtop(ain, aout, null);
+    }
+
+    /**
+     * dst = dst.rgb * src.a + (1.0 - dst.a) * src.rgb
+     * dst.a = src.a
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachDstAtop(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        blend(10, ain, aout, opt);
     }
 
     /**
@@ -271,7 +396,18 @@
      * @param aout The destination buffer
      */
     public void forEachXor(Allocation ain, Allocation aout) {
-        blend(11, ain, aout);
+        forEachXor(ain, aout, null);
+    }
+
+    /**
+     * Sets dst = {src.r ^ dst.r, src.g ^ dst.g, src.b ^ dst.b, src.a ^ dst.a}
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachXor(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        blend(11, ain, aout, opt);
     }
 
     /**
@@ -300,7 +436,18 @@
      * @param aout The destination buffer
      */
     public void forEachMultiply(Allocation ain, Allocation aout) {
-        blend(14, ain, aout);
+        forEachMultiply(ain, aout, null);
+    }
+
+    /**
+     * Sets dst = src * dst
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachMultiply(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        blend(14, ain, aout, opt);
     }
 
     /**
@@ -396,7 +543,18 @@
      * @param aout The destination buffer
      */
     public void forEachAdd(Allocation ain, Allocation aout) {
-        blend(34, ain, aout);
+        forEachAdd(ain, aout, null);
+    }
+
+    /**
+     * Sets dst = min(src + dst, 1.0)
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachAdd(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        blend(34, ain, aout, opt);
     }
 
     /**
@@ -415,7 +573,18 @@
      * @param aout The destination buffer
      */
     public void forEachSubtract(Allocation ain, Allocation aout) {
-        blend(35, ain, aout);
+        forEachSubtract(ain, aout, null);
+    }
+
+    /**
+     * Sets dst = max(dst - src, 0.0)
+     *
+     * @param ain The source buffer
+     * @param aout The destination buffer
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEachSubtract(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
+        blend(35, ain, aout, opt);
     }
 
     /**
diff --git a/rs/java/android/renderscript/ScriptIntrinsicBlur.java b/rs/java/android/renderscript/ScriptIntrinsicBlur.java
index 2b36d27..e7e33b8 100644
--- a/rs/java/android/renderscript/ScriptIntrinsicBlur.java
+++ b/rs/java/android/renderscript/ScriptIntrinsicBlur.java
@@ -84,10 +84,23 @@
      *             type.
      */
     public void forEach(Allocation aout) {
-        forEach(0, null, aout, null);
+        forEach(aout, null);
     }
 
     /**
+     * Apply the filter to the input and save to the specified
+     * allocation.
+     *
+     * @param aout Output allocation. Must match creation element
+     *             type.
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEach(Allocation aout, Script.LaunchOptions opt) {
+        forEach(0, null, aout, null, opt);
+    }
+
+
+    /**
      * Get a KernelID for this intrinsic kernel.
      *
      * @return Script.KernelID The KernelID object.
diff --git a/rs/java/android/renderscript/ScriptIntrinsicColorMatrix.java b/rs/java/android/renderscript/ScriptIntrinsicColorMatrix.java
index 4b0d507..57d917c 100644
--- a/rs/java/android/renderscript/ScriptIntrinsicColorMatrix.java
+++ b/rs/java/android/renderscript/ScriptIntrinsicColorMatrix.java
@@ -206,7 +206,6 @@
         setMatrix();
     }
 
-
     /**
      * Invoke the kernel and apply the matrix to each cell of input
      * {@link Allocation} and copy to the output {@link Allocation}.
@@ -223,6 +222,26 @@
      * @param aout Output allocation
      */
     public void forEach(Allocation ain, Allocation aout) {
+        forEach(ain, aout, null);
+    }
+
+    /**
+     * Invoke the kernel and apply the matrix to each cell of input
+     * {@link Allocation} and copy to the output {@link Allocation}.
+     *
+     * If the vector size of the input is less than four, the
+     * remaining components are treated as zero for the matrix
+     * multiply.
+     *
+     * If the output vector size is less than four, the unused
+     * vector components are discarded.
+     *
+     *
+     * @param ain Input allocation
+     * @param aout Output allocation
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEach(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
         if (!ain.getElement().isCompatible(Element.U8(mRS)) &&
             !ain.getElement().isCompatible(Element.U8_2(mRS)) &&
             !ain.getElement().isCompatible(Element.U8_3(mRS)) &&
@@ -247,7 +266,7 @@
             throw new RSIllegalArgumentException("Unsuported element type.");
         }
 
-        forEach(0, ain, aout, null);
+        forEach(0, ain, aout, null, opt);
     }
 
     /**
diff --git a/rs/java/android/renderscript/ScriptIntrinsicConvolve3x3.java b/rs/java/android/renderscript/ScriptIntrinsicConvolve3x3.java
index a1a1b7e..fb91fdc 100644
--- a/rs/java/android/renderscript/ScriptIntrinsicConvolve3x3.java
+++ b/rs/java/android/renderscript/ScriptIntrinsicConvolve3x3.java
@@ -106,7 +106,19 @@
      *             type.
      */
     public void forEach(Allocation aout) {
-        forEach(0, null, aout, null);
+        forEach(aout, null);
+    }
+
+    /**
+     * Apply the filter to the input and save to the specified
+     * allocation.
+     *
+     * @param aout Output allocation. Must match creation element
+     *             type.
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEach(Allocation aout, Script.LaunchOptions opt) {
+        forEach(0, null, aout, null, opt);
     }
 
     /**
diff --git a/rs/java/android/renderscript/ScriptIntrinsicConvolve5x5.java b/rs/java/android/renderscript/ScriptIntrinsicConvolve5x5.java
index 8b66896..0357560 100644
--- a/rs/java/android/renderscript/ScriptIntrinsicConvolve5x5.java
+++ b/rs/java/android/renderscript/ScriptIntrinsicConvolve5x5.java
@@ -107,10 +107,23 @@
      *             type.
      */
     public void forEach(Allocation aout) {
-        forEach(0, null, aout, null);
+        forEach(aout, null);
     }
 
     /**
+     * Apply the filter to the input and save to the specified
+     * allocation.
+     *
+     * @param aout Output allocation. Must match creation element
+     *             type.
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEach(Allocation aout, Script.LaunchOptions opt) {
+        forEach(0, null, aout, null, opt);
+    }
+
+
+    /**
      * Get a KernelID for this intrinsic kernel.
      *
      * @return Script.KernelID The KernelID object.
diff --git a/rs/java/android/renderscript/ScriptIntrinsicHistogram.java b/rs/java/android/renderscript/ScriptIntrinsicHistogram.java
index 536663d..95b610a 100644
--- a/rs/java/android/renderscript/ScriptIntrinsicHistogram.java
+++ b/rs/java/android/renderscript/ScriptIntrinsicHistogram.java
@@ -67,6 +67,24 @@
      * @param ain The input image
      */
     public void forEach(Allocation ain) {
+        forEach(ain, null);
+    }
+
+    /**
+     * Process an input buffer and place the histogram into the
+     * output allocation. The output allocation may be a narrower
+     * vector size than the input. In this case the vector size of
+     * the output is used to determine how many of the input
+     * channels are used in the computation. This is useful if you
+     * have an RGBA input buffer but only want the histogram for
+     * RGB.
+     *
+     * 1D and 2D input allocations are supported.
+     *
+     * @param ain The input image
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEach(Allocation ain, Script.LaunchOptions opt) {
         if (ain.getType().getElement().getVectorSize() <
             mOut.getType().getElement().getVectorSize()) {
 
@@ -78,9 +96,11 @@
             throw new RSIllegalArgumentException("Output type must be U32 or I32.");
         }
 
-        forEach(0, ain, null, null);
+        forEach(0, ain, null, null, opt);
     }
 
+
+
     /**
      * Set the coefficients used for the RGBA to Luminocity
      * calculation. The default is {0.299f, 0.587f, 0.114f, 0.f}.
@@ -137,6 +157,7 @@
         setVar(1, aout);
     }
 
+
     /**
      * Process an input buffer and place the histogram into the
      * output allocation. The dot product of the input channel and
@@ -148,6 +169,21 @@
      * @param ain The input image
      */
     public void forEach_Dot(Allocation ain) {
+        forEach_Dot(ain, null);
+    }
+
+    /**
+     * Process an input buffer and place the histogram into the
+     * output allocation. The dot product of the input channel and
+     * the coefficients from 'setDotCoefficients' are used to
+     * calculate the output values.
+     *
+     * 1D and 2D input allocations are supported.
+     *
+     * @param ain The input image
+     * @param opt LaunchOptions for clipping
+     */
+    public void forEach_Dot(Allocation ain, Script.LaunchOptions opt) {
         if (mOut.getType().getElement().getVectorSize() != 1) {
             throw new RSIllegalArgumentException("Output vector size must be one.");
         }
@@ -156,7 +192,7 @@
             throw new RSIllegalArgumentException("Output type must be U32 or I32.");
         }
 
-        forEach(1, ain, null, null);
+        forEach(1, ain, null, null, opt);
     }
 
 
diff --git a/rs/java/android/renderscript/ScriptIntrinsicLUT.java b/rs/java/android/renderscript/ScriptIntrinsicLUT.java
index 001095c..69ff64a 100644
--- a/rs/java/android/renderscript/ScriptIntrinsicLUT.java
+++ b/rs/java/android/renderscript/ScriptIntrinsicLUT.java
@@ -114,7 +114,6 @@
         mDirty = true;
     }
 
-
     /**
      * Invoke the kernel and apply the lookup to each cell of ain
      * and copy to aout.
@@ -123,11 +122,23 @@
      * @param aout Output allocation
      */
     public void forEach(Allocation ain, Allocation aout) {
+        forEach(ain, aout, null);
+    }
+
+    /**
+     * Invoke the kernel and apply the lookup to each cell of ain
+     * and copy to aout.
+     *
+     * @param ain Input allocation
+     * @param aout Output allocation
+     * @param opt Options for clipping
+     */
+    public void forEach(Allocation ain, Allocation aout, Script.LaunchOptions opt) {
         if (mDirty) {
             mDirty = false;
             mTables.copyFromUnchecked(mCache);
         }
-        forEach(0, ain, aout, null);
+        forEach(0, ain, aout, null, opt);
     }
 
     /**