Use ComposColorFilter to collaps hierarchy (when possible).

Clarify asColorFilter ...
1. Rename to isColorFilterNode for DAG reduction
2. Add asAColorFilter for removing the imagefilter entirely (future use-case)

Need layouttest rebaseline suppression before this can land in chrome...
https://codereview.chromium.org/984023004/

BUG=skia:

Review URL: https://codereview.chromium.org/982933002
diff --git a/include/core/SkImageFilter.h b/include/core/SkImageFilter.h
index a65a24c..cb18f7c 100644
--- a/include/core/SkImageFilter.h
+++ b/include/core/SkImageFilter.h
@@ -142,7 +142,25 @@
      *  If this returns true, then if filterPtr is not null, it must be set to a ref'd colorfitler
      *  (i.e. it may not be set to NULL).
      */
-    virtual bool asColorFilter(SkColorFilter** filterPtr) const;
+    bool isColorFilterNode(SkColorFilter** filterPtr) const {
+        return this->onIsColorFilterNode(filterPtr);
+    }
+
+    // DEPRECATED : use isColorFilterNode() instead
+    bool asColorFilter(SkColorFilter** filterPtr) const {
+        return this->isColorFilterNode(filterPtr);
+    }
+
+    /**
+     *  Returns true (and optionally returns a ref'd filter) if this imagefilter can be completely
+     *  replaced by the returned colorfilter. i.e. the two effects will affect drawing in the
+     *  same way.
+     */
+    bool asAColorFilter(SkColorFilter** filterPtr) const {
+        return this->countInputs() > 0 &&
+               NULL == this->getInput(0) &&
+               this->isColorFilterNode(filterPtr);
+    }
 
     /**
      *  Returns the number of inputs this filter will accept (some inputs can
@@ -271,6 +289,14 @@
     // no inputs.
     virtual bool onFilterBounds(const SkIRect&, const SkMatrix&, SkIRect*) const;
 
+    /**
+     *  Return true (and return a ref'd colorfilter) if this node in the DAG is just a
+     *  colorfilter w/o CropRect constraints.
+     */
+    virtual bool onIsColorFilterNode(SkColorFilter** /*filterPtr*/) const {
+        return false;
+    }
+
     /** Computes source bounds as the src bitmap bounds offset by srcOffset.
      *  Apply the transformed crop rect to the bounds if any of the
      *  corresponding edge flags are set. Intersects the result against the
diff --git a/include/effects/SkColorFilterImageFilter.h b/include/effects/SkColorFilterImageFilter.h
index 97203d8..c8b1661 100644
--- a/include/effects/SkColorFilterImageFilter.h
+++ b/include/effects/SkColorFilterImageFilter.h
@@ -29,7 +29,7 @@
     virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
                                SkBitmap* result, SkIPoint* loc) const SK_OVERRIDE;
 
-    bool asColorFilter(SkColorFilter**) const SK_OVERRIDE;
+    bool onIsColorFilterNode(SkColorFilter**) const SK_OVERRIDE;
 
 private:
     SkColorFilterImageFilter(SkColorFilter* cf,
diff --git a/src/core/SkImageFilter.cpp b/src/core/SkImageFilter.cpp
index 94b90d6..2837b7c 100644
--- a/src/core/SkImageFilter.cpp
+++ b/src/core/SkImageFilter.cpp
@@ -366,10 +366,6 @@
     return false;
 }
 
-bool SkImageFilter::asColorFilter(SkColorFilter**) const {
-    return false;
-}
-
 #if SK_SUPPORT_GPU
 
 void SkImageFilter::WrapTexture(GrTexture* texture, int width, int height, SkBitmap* result) {
diff --git a/src/effects/SkColorFilterImageFilter.cpp b/src/effects/SkColorFilterImageFilter.cpp
index fdf6de7..210fb57 100755
--- a/src/effects/SkColorFilterImageFilter.cpp
+++ b/src/effects/SkColorFilterImageFilter.cpp
@@ -21,10 +21,12 @@
         return NULL;
     }
 
-    SkColorFilter* inputColorFilter;
-    if (input && input->asColorFilter(&inputColorFilter)) {
-        SkAutoUnref autoUnref(inputColorFilter);
-        SkAutoTUnref<SkColorFilter> newCF(cf->newComposed(inputColorFilter));
+    SkColorFilter* inputCF;
+    if (input && input->isColorFilterNode(&inputCF)) {
+        // This is an optimization, as it collapses the hierarchy by just combining the two
+        // colorfilters into a single one, which the new imagefilter will wrap.
+        SkAutoUnref autoUnref(inputCF);
+        SkAutoTUnref<SkColorFilter> newCF(SkColorFilter::CreateComposeFilter(cf, inputCF));
         if (newCF) {
             return SkNEW_ARGS(SkColorFilterImageFilter, (newCF, input->getInput(0), cropRect, 0));
         }
@@ -85,11 +87,11 @@
     return true;
 }
 
-bool SkColorFilterImageFilter::asColorFilter(SkColorFilter** filter) const {
-    if (!cropRectIsSet()) {
+bool SkColorFilterImageFilter::onIsColorFilterNode(SkColorFilter** filter) const {
+    SkASSERT(1 == this->countInputs());
+    if (!this->cropRectIsSet()) {
         if (filter) {
-            *filter = fColorFilter;
-            fColorFilter->ref();
+            *filter = SkRef(fColorFilter);
         }
         return true;
     }
diff --git a/tests/ImageFilterTest.cpp b/tests/ImageFilterTest.cpp
index 430481f..5e92ee3 100644
--- a/tests/ImageFilterTest.cpp
+++ b/tests/ImageFilterTest.cpp
@@ -133,7 +133,7 @@
     return SkColorFilterImageFilter::Create(filter, input);
 }
 
-static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkImageFilter::CropRect* cropRect = NULL) {
+static SkImageFilter* make_grayscale(SkImageFilter* input, const SkImageFilter::CropRect* cropRect) {
     SkScalar matrix[20];
     memset(matrix, 0, 20 * sizeof(SkScalar));
     matrix[0] = matrix[5] = matrix[10] = 0.2126f;
@@ -144,27 +144,63 @@
     return SkColorFilterImageFilter::Create(filter, input, cropRect);
 }
 
+static SkImageFilter* make_blue(SkImageFilter* input, const SkImageFilter::CropRect* cropRect) {
+    SkAutoTUnref<SkColorFilter> filter(SkColorFilter::CreateModeFilter(SK_ColorBLUE,
+                                                                       SkXfermode::kSrcIn_Mode));
+    return SkColorFilterImageFilter::Create(filter, input, cropRect);
+}
+
 DEF_TEST(ImageFilter, reporter) {
     {
-        // Check that two non-clipping color matrices concatenate into a single filter.
+        // Check that two non-clipping color-matrice-filters concatenate into a single filter.
         SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f));
         SkAutoTUnref<SkImageFilter> quarterBrightness(make_scale(0.5f, halfBrightness));
         REPORTER_ASSERT(reporter, NULL == quarterBrightness->getInput(0));
+        SkColorFilter* cf;
+        REPORTER_ASSERT(reporter, quarterBrightness->asColorFilter(&cf));
+        REPORTER_ASSERT(reporter, cf->asColorMatrix(NULL));
+        cf->unref();
     }
 
     {
-        // Check that a clipping color matrix followed by a grayscale does not concatenate into a single filter.
+        // Check that a clipping color-matrice-filter followed by a color-matrice-filters
+        // concatenates into a single filter, but not a matrixfilter (due to clamping).
         SkAutoTUnref<SkImageFilter> doubleBrightness(make_scale(2.0f));
         SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f, doubleBrightness));
-        REPORTER_ASSERT(reporter, halfBrightness->getInput(0));
+        REPORTER_ASSERT(reporter, NULL == halfBrightness->getInput(0));
+        SkColorFilter* cf;
+        REPORTER_ASSERT(reporter, halfBrightness->asColorFilter(&cf));
+        REPORTER_ASSERT(reporter, !cf->asColorMatrix(NULL));
+        cf->unref();
     }
 
     {
         // Check that a color filter image filter without a crop rect can be
         // expressed as a color filter.
-        SkAutoTUnref<SkImageFilter> gray(make_grayscale());
+        SkAutoTUnref<SkImageFilter> gray(make_grayscale(NULL, NULL));
         REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL));
     }
+    
+    {
+        // Check that a colorfilterimage filter without a crop rect but with an input
+        // that is another colorfilterimage can be expressed as a colorfilter (composed).
+        SkAutoTUnref<SkImageFilter> mode(make_blue(NULL, NULL));
+        SkAutoTUnref<SkImageFilter> gray(make_grayscale(mode, NULL));
+        REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL));
+    }
+
+    {
+        // Test that if we exceed the limit of what ComposeColorFilter can combine, we still
+        // can build the DAG and won't assert if we call asColorFilter.
+        SkAutoTUnref<SkImageFilter> filter(make_blue(NULL, NULL));
+        const int kWayTooManyForComposeColorFilter = 100;
+        for (int i = 0; i < kWayTooManyForComposeColorFilter; ++i) {
+            filter.reset(make_blue(filter, NULL));
+            // the first few of these will succeed, but after we hit the internal limit,
+            // it will then return false.
+            (void)filter->asColorFilter(NULL);
+        }
+    }
 
     {
         // Check that a color filter image filter with a crop rect cannot