Collapse consecutive SkTableColorFilters

BUG=skia:1366

For the added bench, the collapsing makes the bench take:
- 70% of the time for CPU rendering of 3 consecutive matrix filters
- almost no change in the GPU rendering of the matrix filters
- 50% of the time for CPU and GPU rendering of 3 consecutive table filters

Review URL: https://codereview.chromium.org/776673002
diff --git a/gm/tablecolorfilter.cpp b/gm/tablecolorfilter.cpp
index f806caa..a49859b 100644
--- a/gm/tablecolorfilter.cpp
+++ b/gm/tablecolorfilter.cpp
@@ -7,6 +7,7 @@
 
 #include "gm.h"
 #include "SkCanvas.h"
+#include "SkColorFilterImageFilter.h"
 #include "SkGradientShader.h"
 #include "SkTableColorFilter.h"
 
@@ -68,6 +69,10 @@
     }
 }
 
+static SkColorFilter* make_null_cf() {
+    return NULL;
+}
+
 static SkColorFilter* make_cf0() {
     uint8_t table[256]; make_table0(table);
     return SkTableColorFilter::Create(table);
@@ -97,33 +102,82 @@
     }
 
     virtual SkISize onISize() {
-        return SkISize::Make(700, 300);
+        return SkISize::Make(700, 1650);
     }
 
     virtual void onDraw(SkCanvas* canvas) {
         canvas->drawColor(0xFFDDDDDD);
         canvas->translate(20, 20);
 
+
+        static SkColorFilter* (*gColorFilterMakers[])() = { make_null_cf, make_cf0, make_cf1,
+                                                 make_cf2, make_cf3 };
+        static void (*gBitmapMakers[])(SkBitmap*) = { make_bm0, make_bm1 };
+
+        // This test will be done once for each bitmap with the results stacked vertically.
+        // For a single bitmap the resulting image will be the following:
+        //  - A first line with the original bitmap, followed by the image drawn once
+        //  with each of the N color filters
+        //  - N lines of the bitmap drawn N times, this will cover all N*N combinations of
+        //  pair of color filters in order to test the collpsing of consecutive table
+        //  color filters.
+        //
+        //  Here is a graphical representation of the result for 2 bitmaps and 2 filters
+        //  with the number corresponding to the number of filters the bitmap goes through:
+        //
+        //  --bitmap1
+        //  011
+        //  22
+        //  22
+        //  --bitmap2
+        //  011
+        //  22
+        //  22
+
         SkScalar x = 0, y = 0;
-
-        static void (*gMakers[])(SkBitmap*) = { make_bm0, make_bm1 };
-        for (size_t maker = 0; maker < SK_ARRAY_COUNT(gMakers); ++maker) {
+        for (size_t bitmapMaker = 0; bitmapMaker < SK_ARRAY_COUNT(gBitmapMakers); ++bitmapMaker) {
             SkBitmap bm;
-            gMakers[maker](&bm);
+            gBitmapMakers[bitmapMaker](&bm);
 
-            SkPaint paint;
+            SkScalar xOffset = SkScalar(bm.width() * 9 / 8);
+            SkScalar yOffset = SkScalar(bm.height() * 9 / 8);
+
+            // Draw the first element of the first line
             x = 0;
-            canvas->drawBitmap(bm, x, y, &paint);
-            paint.setColorFilter(make_cf0())->unref();  x += bm.width() * 9 / 8;
-            canvas->drawBitmap(bm, x, y, &paint);
-            paint.setColorFilter(make_cf1())->unref();  x += bm.width() * 9 / 8;
-            canvas->drawBitmap(bm, x, y, &paint);
-            paint.setColorFilter(make_cf2())->unref();  x += bm.width() * 9 / 8;
-            canvas->drawBitmap(bm, x, y, &paint);
-            paint.setColorFilter(make_cf3())->unref();  x += bm.width() * 9 / 8;
+            SkPaint paint;
             canvas->drawBitmap(bm, x, y, &paint);
 
-            y += bm.height() * 9 / 8;
+            // Draws the rest of the first line for this bitmap
+            // each draw being at xOffset of the previous one
+            for (unsigned i = 1; i < SK_ARRAY_COUNT(gColorFilterMakers); ++i) {
+                x += xOffset;
+                paint.setColorFilter(gColorFilterMakers[i]())->unref();
+                canvas->drawBitmap(bm, x, y, &paint);
+            }
+
+            paint.setColorFilter(NULL);
+
+            for (unsigned i = 0; i < SK_ARRAY_COUNT(gColorFilterMakers); ++i) {
+                SkAutoTUnref<SkColorFilter> colorFilter1(gColorFilterMakers[i]());
+                SkAutoTUnref<SkImageFilter> imageFilter1(SkColorFilterImageFilter::Create(
+                            colorFilter1, NULL, NULL, 0));
+
+                // Move down to the next line and draw it
+                // each draw being at xOffset of the previous one
+                y += yOffset;
+                x = 0;
+                for (unsigned j = 1; j < SK_ARRAY_COUNT(gColorFilterMakers); ++j) {
+                    SkAutoTUnref<SkColorFilter> colorFilter2(gColorFilterMakers[j]());
+                    SkAutoTUnref<SkImageFilter> imageFilter2(SkColorFilterImageFilter::Create(
+                                colorFilter2, imageFilter1, NULL, 0));
+                    paint.setImageFilter(imageFilter2);
+                    canvas->drawBitmap(bm, x, y, &paint);
+                    x += xOffset;
+                }
+            }
+
+            // Move down one line to the beginning of the block for next bitmap
+            y += yOffset;
         }
     }