[graphite] Use X-macro to get better UniqueKey printouts

Also add a Precompilation version of the Luma ColorFilter to test it out.

Bug: b/259548724
Bug: b/238759147
Change-Id: I6ca45ae3edcd38c17f604bda96220bc935b75b01
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/833300
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Jim Van Verth <jvanverth@google.com>
diff --git a/src/core/SkKnownRuntimeEffects.h b/src/core/SkKnownRuntimeEffects.h
index 9587588..170f5b6 100644
--- a/src/core/SkKnownRuntimeEffects.h
+++ b/src/core/SkKnownRuntimeEffects.h
@@ -44,55 +44,57 @@
 
 static constexpr int kUnknownRuntimeEffectIDStart = kChromeKnownRuntimeEffectsEnd;
 
+// All six 1DBlur* stable keys must be consecutive after 1DBlurBase and
+// there is no 1DBlur24 bc for large kernels we bin by a multiple of eight.
+// Similarly, all six 2DBlur* stable keys must be consecutive after 2DBlurBase and
+// there is no 2DBlur24 bc for large kernels we bin by a multiple of eight.
+// As for the macros:
+//   M(X) is for standard entries
+//   M1(X) is for helper values that should be skipped in a switch statement
+//   M2(X,Y) is for entries that have an initializer (Y)
+#define SK_ALL_STABLEKEYS(M, M1, M2) \
+    M2(Invalid, Start)      \
+    M1(1DBlurBase)          \
+    M2(1DBlur4, 1DBlurBase) \
+    M(1DBlur8)              \
+    M(1DBlur12)             \
+    M(1DBlur16)             \
+    M(1DBlur20)             \
+    M(1DBlur28)             \
+    M1(2DBlurBase)          \
+    M2(2DBlur4, 2DBlurBase) \
+    M(2DBlur8)              \
+    M(2DBlur12)             \
+    M(2DBlur16)             \
+    M(2DBlur20)             \
+    M(2DBlur28)             \
+    M(Blend)                \
+    M(Decal)                \
+    M(Displacement)         \
+    M(Lighting)             \
+    M(LinearMorphology)     \
+    M(Magnifier)            \
+    M(Normal)               \
+    M(SparseMorphology)     \
+    M(Arithmetic)           \
+    M(HighContrast)         \
+    M(Lerp)                 \
+    M(Luma)                 \
+    M(Overdraw)
 
 // WARNING: If any of the existing values are changed, UniqueKeys that have stably-keyed effects
 // will need to be invalidated. (Adding new values to the end of the enum should be fine though.)
 // TODO(b/238759147): add a revision number that can drive the invalidation
-// TODO(b/238759147): use an X macro to stringize this list and then known runtime effects could
-// have a real name in the SkSL instead of all being named "KnownRuntimeEffect."
 enum class StableKey : uint32_t {
     kStart =   kSkiaKnownRuntimeEffectsStart,
 
-    kInvalid = kStart,
-
-    // shaders
-    // Binned 1D Blurs
-    k1DBlurBase,
-    k1DBlur4 = k1DBlurBase, // all six 1DBlur stable keys must be consecutive after the Base
-    k1DBlur8,
-    k1DBlur12,
-    k1DBlur16,
-    k1DBlur20,
-    // For large kernels we bin by a multiple of eight (so no k1DBlur24)
-    k1DBlur28,
-
-    // Binned 2D Blurs
-    k2DBlurBase,
-    k2DBlur4 = k2DBlurBase, // all six 2DBlur stable keys must be consecutive after the Base
-    k2DBlur8,
-    k2DBlur12,
-    k2DBlur16,
-    k2DBlur20,
-    // For large kernels we bin by a multiple of eight (so no k2DBlur24)
-    k2DBlur28,
-
-    kBlend,
-    kDecal,
-    kDisplacement,
-    kLighting,
-    kLinearMorphology,
-    kMagnifier,
-    kNormal,
-    kSparseMorphology,
-
-    // blenders
-    kArithmetic,
-
-    // color filters
-    kHighContrast,
-    kLerp,
-    kLuma,
-    kOverdraw,
+#define M(type) k##type,
+#define M1(type) k##type,
+#define M2(type, initializer) k##type = k##initializer,
+    SK_ALL_STABLEKEYS(M, M1, M2)
+#undef M2
+#undef M1
+#undef M
 
     kLast =    kOverdraw,
 };
@@ -104,6 +106,8 @@
 
 const SkRuntimeEffect* GetKnownRuntimeEffect(StableKey);
 
+static_assert(static_cast<int>(StableKey::kInvalid)  == static_cast<int>(StableKey::kStart));
+
 static_assert(static_cast<int>(StableKey::k1DBlur4)  == static_cast<int>(StableKey::k1DBlurBase));
 static_assert(static_cast<int>(StableKey::k1DBlur8)  == static_cast<int>(StableKey::k1DBlurBase)+1);
 static_assert(static_cast<int>(StableKey::k1DBlur12) == static_cast<int>(StableKey::k1DBlurBase)+2);
diff --git a/src/gpu/graphite/FactoryFunctions.cpp b/src/gpu/graphite/FactoryFunctions.cpp
index 9e3195f..63b4d0b 100644
--- a/src/gpu/graphite/FactoryFunctions.cpp
+++ b/src/gpu/graphite/FactoryFunctions.cpp
@@ -905,6 +905,14 @@
 }
 
 //--------------------------------------------------------------------------------------------------
+sk_sp<PrecompileColorFilter> PrecompileColorFilters::Luma() {
+    const SkRuntimeEffect* lumaEffect =
+            GetKnownRuntimeEffect(SkKnownRuntimeEffects::StableKey::kLuma);
+
+    return MakePrecompileColorFilter(sk_ref_sp(lumaEffect));
+}
+
+//--------------------------------------------------------------------------------------------------
 class PrecompileComposeColorFilter : public PrecompileColorFilter {
 public:
     PrecompileComposeColorFilter(SkSpan<const sk_sp<PrecompileColorFilter>> outerOptions,
diff --git a/src/gpu/graphite/FactoryFunctions.h b/src/gpu/graphite/FactoryFunctions.h
index de839ee..c3cf2bc 100644
--- a/src/gpu/graphite/FactoryFunctions.h
+++ b/src/gpu/graphite/FactoryFunctions.h
@@ -135,6 +135,9 @@
     SK_API sk_sp<PrecompileColorFilter> LinearToSRGBGamma();
     SK_API sk_sp<PrecompileColorFilter> SRGBToLinearGamma();
 
+    // This matches the main API's factory in include/effects/SkLumaColorFilter.h
+    static sk_sp<PrecompileColorFilter> Luma();
+
     // This encompases both variants of SkColorFilters::Table and TableARGB
     SK_API sk_sp<PrecompileColorFilter> Table();
 
diff --git a/src/gpu/graphite/ShaderCodeDictionary.cpp b/src/gpu/graphite/ShaderCodeDictionary.cpp
index 43b2cab..8645b81 100644
--- a/src/gpu/graphite/ShaderCodeDictionary.cpp
+++ b/src/gpu/graphite/ShaderCodeDictionary.cpp
@@ -41,6 +41,20 @@
 
 namespace {
 
+const char* get_known_rte_name(StableKey key) {
+    switch (key) {
+#define M(type) case StableKey::k##type : return "KnownRuntimeEffect_" #type;
+#define M1(type)
+#define M2(type, initializer) case StableKey::k##type : return "KnownRuntimeEffect_" #type;
+        SK_ALL_STABLEKEYS(M, M1, M2)
+#undef M2
+#undef M1
+#undef M
+    }
+
+    SkUNREACHABLE;
+}
+
 std::string get_mangled_name(const std::string& baseName, int manglingSuffix) {
     return baseName + "_" + std::to_string(manglingSuffix);
 }
@@ -1487,12 +1501,13 @@
         int index = stableKey - kSkiaKnownRuntimeEffectsStart;
 
         if (!fKnownRuntimeEffectCodeSnippets[index].fExpressionGenerator) {
+            const char* name = get_known_rte_name(static_cast<StableKey>(stableKey));
             fKnownRuntimeEffectCodeSnippets[index] = ShaderSnippet(
-                    "KnownRuntimeEffect",
+                    name,
                     this->convertUniforms(effect),
                     snippetFlags,
                     /* texturesAndSamplers= */ {},
-                    "KnownRuntimeEffect",
+                    name,
                     GenerateRuntimeShaderExpression,
                     GenerateRuntimeShaderPreamble,
                     (int)effect->children().size());
diff --git a/tests/graphite/PaintParamsKeyTest.cpp b/tests/graphite/PaintParamsKeyTest.cpp
index c529340..6e5b879 100644
--- a/tests/graphite/PaintParamsKeyTest.cpp
+++ b/tests/graphite/PaintParamsKeyTest.cpp
@@ -21,6 +21,7 @@
 #include "include/core/SkVertices.h"
 #include "include/effects/SkColorMatrix.h"
 #include "include/effects/SkGradientShader.h"
+#include "include/effects/SkLumaColorFilter.h"
 #include "include/effects/SkPerlinNoiseShader.h"
 #include "include/effects/SkRuntimeEffect.h"
 #include "include/gpu/graphite/Image.h"
@@ -143,6 +144,7 @@
     M(HSLAMatrix)      \
     M(Lighting)        \
     M(LinearToSRGB)    \
+    M(Luma)            \
     M(Matrix)          \
     M(Runtime)         \
     M(SRGBToLinear)    \
@@ -851,6 +853,10 @@
     return { SkColorFilters::SRGBToLinearGamma(), PrecompileColorFilters::SRGBToLinearGamma() };
 }
 
+std::pair<sk_sp<SkColorFilter>, sk_sp<PrecompileColorFilter>> create_luma_colorfilter() {
+    return { SkLumaColorFilter::Make(), PrecompileColorFilters::Luma() };
+}
+
 std::pair<sk_sp<SkColorFilter>, sk_sp<PrecompileColorFilter>> create_compose_colorfilter(
         SkRandom* rand) {
     auto [outerCF, outerO] = create_random_colorfilter(rand);
@@ -923,6 +929,8 @@
             return create_lighting_colorfilter();
         case ColorFilterType::kLinearToSRGB:
             return create_linear_to_srgb_colorfilter();
+        case ColorFilterType::kLuma:
+            return create_luma_colorfilter();
         case ColorFilterType::kMatrix:
             return create_matrix_colorfilter();
         case ColorFilterType::kRuntime:
@@ -1216,6 +1224,7 @@
             ColorFilterType::kHSLAMatrix,
             ColorFilterType::kLighting,
             ColorFilterType::kLinearToSRGB,
+            ColorFilterType::kLuma,
             ColorFilterType::kRuntime,
             ColorFilterType::kSRGBToLinear,
             ColorFilterType::kTable,