split that new filter into smaller steps

Looks best to tackle the early return and if statements separately.

Change-Id: I4ad0d4c8e7707277d678e1219534d20e0e54ba1c
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/327017
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
diff --git a/gm/runtimecolorfilter.cpp b/gm/runtimecolorfilter.cpp
index dfa98b1..1fe2d52 100644
--- a/gm/runtimecolorfilter.cpp
+++ b/gm/runtimecolorfilter.cpp
@@ -20,6 +20,13 @@
 #include <stddef.h>
 #include <utility>
 
+const char* gNoop = R"(
+    in shader input;
+    half4 main() {
+        return sample(input);
+    }
+)";
+
 const char* gLumaSrc = R"(
     in shader input;
     half4 main() {
@@ -34,18 +41,50 @@
     }
 )";
 
-// A runtime effect with a small amount of control flow (if, else, etc., and
-// early return) that can in principle still be reduced to a single basic block.
-// Distilled from AOSP tone mapping shaders.
-const char* gComplex = R"(
+// Build up the same effect with increasingly complex control flow syntax.
+// All of these are semantically equivalent and can be reduced in principle to one basic block.
+
+// Simplest to run; hardest to write?
+const char* gTernary = R"(
     in shader input;
     half4 main() {
         half4 color = sample(input);
+        half luma = dot(color.rgb, half3(0.3, 0.6, 0.1));
 
+        half scale = luma < 0.33333 ? 0.5
+                   : luma < 0.66666 ? (0.166666 + 2.0 * (luma - 0.33333)) / luma
+                   :   /* else */     (0.833333 + 0.5 * (luma - 0.66666)) / luma;
+        return half4(color.rgb * scale, color.a);
+    }
+)";
+
+// Uses conditional if statements but no early return.
+const char* gIfs = R"(
+    in shader input;
+    half4 main() {
+        half4 color = sample(input);
         half luma = dot(color.rgb, half3(0.3, 0.6, 0.1));
 
         half scale = 0;
+        if (luma < 0.33333) {
+            scale = 0.5;
+        } else if (luma < 0.66666) {
+            scale = (0.166666 + 2.0 * (luma - 0.33333)) / luma;
+        } else {
+            scale = (0.833333 + 0.5 * (luma - 0.66666)) / luma;
+        }
+        return half4(color.rgb * scale, color.a);
+    }
+)";
 
+// Distilled from AOSP tone mapping shaders, more like what people tend to write.
+const char* gEarlyReturn = R"(
+    in shader input;
+    half4 main() {
+        half4 color = sample(input);
+        half luma = dot(color.rgb, half3(0.3, 0.6, 0.1));
+
+        half scale = 0;
         if (luma < 0.33333) {
             return half4(color.rgb * 0.5, color.a);
         } else if (luma < 0.66666) {
@@ -53,23 +92,32 @@
         } else {
             scale = 0.833333 + 0.5 * (luma - 0.66666);
         }
-
         return half4(color.rgb * (scale/luma), color.a);
     }
 )";
 
 
-DEF_SIMPLE_GM(runtimecolorfilter, canvas, 256 * 4, 256) {
-    auto img = GetResourceAsImage("images/mandrill_256.png");
-    canvas->drawImage(img, 0, 0, nullptr);
+DEF_SIMPLE_GM(runtimecolorfilter, canvas, 256 * 3, 256 * 2) {
+    sk_sp<SkImage> img = GetResourceAsImage("images/mandrill_256.png");
 
-    for (auto src : { gLumaSrc, gLumaSrcWithCoords, gComplex }) {
-        sk_sp<SkRuntimeEffect> effect = std::get<0>(SkRuntimeEffect::Make(SkString(src)));
+    auto draw_filter = [&](const char* src) {
+        auto [effect, err] = SkRuntimeEffect::Make(SkString(src));
+        if (!effect) {
+            SkDebugf("%s\n%s\n", src, err.c_str());
+        }
         SkASSERT(effect);
         SkPaint p;
         sk_sp<SkColorFilter> input = nullptr;
         p.setColorFilter(effect->makeColorFilter(nullptr, &input, 1));
-        canvas->translate(256, 0);
         canvas->drawImage(img, 0, 0, &p);
+        canvas->translate(256, 0);
+    };
+
+    for (const char* src : { gNoop, gLumaSrc, gLumaSrcWithCoords}) {
+        draw_filter(src);
+    }
+    canvas->translate(-256*3, 256);
+    for (const char* src : { gTernary, gIfs, gEarlyReturn}) {
+        draw_filter(src);
     }
 }