Add Make functions for runtime blends.

This does basic plumbing work, but lacks a real implementation for the
custom blend subclass. That subclass is added in the followup CL of this
CL chain.

Change-Id: I8db0eb42737a739b49741ffd6f4a283a2c0a9d62
Bug: skia:12080
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/417005
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
diff --git a/tests/SkRuntimeEffectTest.cpp b/tests/SkRuntimeEffectTest.cpp
index 5e889ac..c2e24ff 100644
--- a/tests/SkRuntimeEffectTest.cpp
+++ b/tests/SkRuntimeEffectTest.cpp
@@ -138,7 +138,7 @@
     test_invalid("uniform shader child;"
                  "half4 main(half4 c) { return sample(child, c); }",
                  "no match for sample(shader, half4)");
-    // Coords and color in a differet order
+    // Coords and color in a different order
     test_invalid("uniform shader child;"
                  "half4 main(half4 c) { return sample(child, c, c.rg); }",
                  "no match for sample(shader, half4, half2)");
@@ -168,6 +168,56 @@
                  "sample(colorFilter, half2, half4)");
 }
 
+DEF_TEST(SkRuntimeEffectForBlender, r) {
+    // Tests that the blender factory rejects or accepts certain SkSL constructs
+    auto test_valid = [r](const char* sksl) {
+        auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(sksl));
+        REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
+    };
+
+    auto test_invalid = [r](const char* sksl, const char* expected) {
+        auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(sksl));
+        REPORTER_ASSERT(r, !effect);
+        REPORTER_ASSERT(r,
+                        errorText.contains(expected),
+                        "Expected error message to contain \"%s\". Actual message: \"%s\"",
+                        expected,
+                        errorText.c_str());
+    };
+
+    // Color filters must use the 'half4 main(half4, half4)' signature. Any mixture of
+    // float4/vec4/half4 is allowed.
+    test_valid("half4  main(half4  s, half4  d) { return s; }");
+    test_valid("float4 main(float4 s, float4 d) { return d; }");
+    test_valid("float4 main(half4  s, float4 d) { return s; }");
+    test_valid("half4  main(float4 s, half4  d) { return d; }");
+    test_valid("vec4   main(half4  s, half4  d) { return s; }");
+    test_valid("half4  main(vec4   s, vec4   d) { return d; }");
+    test_valid("vec4   main(vec4   s, vec4   d) { return s; }");
+
+    // Invalid return types
+    test_invalid("void  main(half4 s, half4 d) {}",                "'main' must return");
+    test_invalid("half3 main(half4 s, half4 d) { return s.rgb; }", "'main' must return");
+
+    // Invalid argument types (some are valid as shaders/color filters)
+    test_invalid("half4 main() { return half4(1); }",                    "'main' parameter");
+    test_invalid("half4 main(half4 c) { return c; }",                    "'main' parameter");
+    test_invalid("half4 main(float2 p) { return half4(1); }",            "'main' parameter");
+    test_invalid("half4 main(float2 p, half4 c) { return c; }",          "'main' parameter");
+    test_invalid("half4 main(float2 p, half4 a, half4 b) { return a; }", "'main' parameter");
+    test_invalid("half4 main(half4 a, half4 b, half4 c) { return a; }",  "'main' parameter");
+
+    // sk_FragCoord should not be available
+    test_invalid("half4 main(half4 s, half4 d) { return sk_FragCoord.xy01; }",
+                 "unknown identifier");
+
+    // Child shaders are currently unsupported in blends
+    test_invalid("uniform shader sh; half4 main(half4 s, half4 d) { return s; }",
+                 "'shader' is not allowed in runtime blend");
+    test_invalid("uniform shader sh; half4 main(half4 s, half4 d) { return sample(sh, s.rg); }",
+                 "unknown identifier 'sample'");
+}
+
 DEF_TEST(SkRuntimeEffectForShader, r) {
     // Tests that the shader factory rejects or accepts certain SkSL constructs
     auto test_valid = [r](const char* sksl) {