[graphite] Apply colorSpace to the gradients' color stops

Bug: b/239959460
Change-Id: Iab65e2b793ae6f865e0629b06a844e803f480ddf
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/639077
Reviewed-by: James Godfrey-Kittle <jamesgk@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/graphite/KeyHelpers.cpp b/src/gpu/graphite/KeyHelpers.cpp
index be00321..56ac6c5 100644
--- a/src/gpu/graphite/KeyHelpers.cpp
+++ b/src/gpu/graphite/KeyHelpers.cpp
@@ -88,7 +88,7 @@
     VALIDATE_UNIFORMS(gatherer, dict, codeSnippetID)
     size_t stops = codeSnippetID == BuiltInCodeSnippetID::kLinearGradientShader4 ? 4 : 8;
 
-    gatherer->writeArray({gradData.fColor4fs, stops});
+    gatherer->writeArray({gradData.fColors, stops});
     gatherer->writeArray({gradData.fOffsets, stops});
     gatherer->write(gradData.fPoints[0]);
     gatherer->write(gradData.fPoints[1]);
@@ -104,7 +104,7 @@
     VALIDATE_UNIFORMS(gatherer, dict, codeSnippetID)
     size_t stops = codeSnippetID == BuiltInCodeSnippetID::kRadialGradientShader4 ? 4 : 8;
 
-    gatherer->writeArray({gradData.fColor4fs, stops});
+    gatherer->writeArray({gradData.fColors, stops});
     gatherer->writeArray({gradData.fOffsets, stops});
     gatherer->write(gradData.fPoints[0]);
     gatherer->write(gradData.fRadii[0]);
@@ -120,7 +120,7 @@
     VALIDATE_UNIFORMS(gatherer, dict, codeSnippetID)
     size_t stops = codeSnippetID == BuiltInCodeSnippetID::kSweepGradientShader4 ? 4 : 8;
 
-    gatherer->writeArray({gradData.fColor4fs, stops});
+    gatherer->writeArray({gradData.fColors, stops});
     gatherer->writeArray({gradData.fOffsets, stops});
     gatherer->write(gradData.fPoints[0]);
     gatherer->write(gradData.fBias);
@@ -137,7 +137,7 @@
     VALIDATE_UNIFORMS(gatherer, dict, codeSnippetID)
     size_t stops = codeSnippetID == BuiltInCodeSnippetID::kConicalGradientShader4 ? 4 : 8;
 
-    gatherer->writeArray({gradData.fColor4fs, stops});
+    gatherer->writeArray({gradData.fColors, stops});
     gatherer->writeArray({gradData.fOffsets, stops});
     gatherer->write(gradData.fPoints[0]);
     gatherer->write(gradData.fPoints[1]);
@@ -158,7 +158,7 @@
         , fScale(0.0f)
         , fTM(SkTileMode::kClamp)
         , fNumStops(numStops) {
-    sk_bzero(fColor4fs, sizeof(fColor4fs));
+    sk_bzero(fColors, sizeof(fColors));
     sk_bzero(fOffsets, sizeof(fOffsets));
 }
 
@@ -168,7 +168,7 @@
                                                  float bias, float scale,
                                                  SkTileMode tm,
                                                  int numStops,
-                                                 SkColor4f* color4fs,
+                                                 const SkPMColor4f* colors,
                                                  float* offsets)
         : fType(type)
         , fBias(bias)
@@ -181,7 +181,7 @@
     fPoints[1] = point1;
     fRadii[0] = radius0;
     fRadii[1] = radius1;
-    memcpy(fColor4fs, color4fs, fNumStops * sizeof(SkColor4f));
+    memcpy(fColors, colors, fNumStops * sizeof(SkColor4f));
     if (offsets) {
         memcpy(fOffsets, offsets, fNumStops * sizeof(float));
     } else {
@@ -193,7 +193,7 @@
     // Extend the colors and offset, if necessary, to fill out the arrays
     // TODO: this should be done later when the actual code snippet has been selected!!
     for (int i = fNumStops ; i < kMaxStops; ++i) {
-        fColor4fs[i] = fColor4fs[fNumStops-1];
+        fColors[i] = fColors[fNumStops-1];
         fOffsets[i] = fOffsets[fNumStops-1];
     }
 }
diff --git a/src/gpu/graphite/KeyHelpers.h b/src/gpu/graphite/KeyHelpers.h
index 4199753..6264b95 100644
--- a/src/gpu/graphite/KeyHelpers.h
+++ b/src/gpu/graphite/KeyHelpers.h
@@ -82,7 +82,7 @@
                      float bias, float scale,
                      SkTileMode,
                      int numStops,
-                     SkColor4f* colors,
+                     const SkPMColor4f* colors,
                      float* offsets);
 
         bool operator==(const GradientData& rhs) const {
@@ -95,7 +95,7 @@
                    fScale == rhs.fScale &&
                    fTM == rhs.fTM &&
                    fNumStops == rhs.fNumStops &&
-                   !memcmp(fColor4fs, rhs.fColor4fs, sizeof(fColor4fs)) &&
+                   !memcmp(fColors, rhs.fColors, sizeof(fColors)) &&
                    !memcmp(fOffsets, rhs.fOffsets, sizeof(fOffsets));
         }
         bool operator!=(const GradientData& rhs) const { return !(*this == rhs); }
@@ -111,7 +111,7 @@
 
         SkTileMode             fTM;
         int                    fNumStops;
-        SkColor4f              fColor4fs[kMaxStops];
+        SkPMColor4f            fColors[kMaxStops];
         float                  fOffsets[kMaxStops];
     };
 
diff --git a/src/gpu/graphite/PipelineData.h b/src/gpu/graphite/PipelineData.h
index e477d22..224cf8a 100644
--- a/src/gpu/graphite/PipelineData.h
+++ b/src/gpu/graphite/PipelineData.h
@@ -123,6 +123,7 @@
     void write(const Uniform& u, const uint8_t* data) { fUniformManager.write(u, data); }
 
     void writeArray(SkSpan<const SkColor4f> colors) { fUniformManager.writeArray(colors); }
+    void writeArray(SkSpan<const SkPMColor4f> colors) { fUniformManager.writeArray(colors); }
     void writeArray(SkSpan<const float> floats) { fUniformManager.writeArray(floats); }
 
     void writeHalf(const SkMatrix& mat) { fUniformManager.writeHalf(mat); }
diff --git a/src/shaders/gradients/SkLinearGradient.cpp b/src/shaders/gradients/SkLinearGradient.cpp
index d856f4c..84d351e 100644
--- a/src/shaders/gradients/SkLinearGradient.cpp
+++ b/src/shaders/gradients/SkLinearGradient.cpp
@@ -12,6 +12,7 @@
 #include "src/shaders/SkLocalMatrixShader.h"
 
 #ifdef SK_GRAPHITE_ENABLED
+#include "src/gpu/graphite/KeyContext.h"
 #include "src/gpu/graphite/KeyHelpers.h"
 #include "src/gpu/graphite/PaintParamsKey.h"
 #endif
@@ -105,13 +106,17 @@
                                 skgpu::graphite::PipelineDataGatherer* gatherer) const {
     using namespace skgpu::graphite;
 
+    // TODO: respect the interpolateInPremul setting
+    SkColor4fXformer xformedColors(this, keyContext.dstColorInfo().colorSpace());
+    const SkPMColor4f* colors = xformedColors.fColors.begin();
+
     GradientShaderBlocks::GradientData data(GradientType::kLinear,
                                             fStart, fEnd,
                                             0.0f, 0.0f,
                                             0.0f, 0.0f,
                                             fTileMode,
                                             fColorCount,
-                                            fColors,
+                                            colors,
                                             fPositions);
 
     GradientShaderBlocks::BeginBlock(keyContext, builder, gatherer, data);
diff --git a/src/shaders/gradients/SkRadialGradient.cpp b/src/shaders/gradients/SkRadialGradient.cpp
index ef7f0c0..ebe2dd3 100644
--- a/src/shaders/gradients/SkRadialGradient.cpp
+++ b/src/shaders/gradients/SkRadialGradient.cpp
@@ -11,6 +11,7 @@
 #include "src/shaders/SkLocalMatrixShader.h"
 
 #ifdef SK_GRAPHITE_ENABLED
+#include "src/gpu/graphite/KeyContext.h"
 #include "src/gpu/graphite/KeyHelpers.h"
 #include "src/gpu/graphite/PaintParamsKey.h"
 #endif
@@ -146,13 +147,17 @@
                                 skgpu::graphite::PipelineDataGatherer* gatherer) const {
     using namespace skgpu::graphite;
 
+    // TODO: respect the interpolateInPremul setting
+    SkColor4fXformer xformedColors(this, keyContext.dstColorInfo().colorSpace());
+    const SkPMColor4f* colors = xformedColors.fColors.begin();
+
     GradientShaderBlocks::GradientData data(GradientType::kRadial,
                                             fCenter, { 0.0f, 0.0f },
                                             fRadius, 0.0f,
                                             0.0f, 0.0f,
                                             fTileMode,
                                             fColorCount,
-                                            fColors,
+                                            colors,
                                             fPositions);
 
     GradientShaderBlocks::BeginBlock(keyContext, builder, gatherer, data);
diff --git a/src/shaders/gradients/SkSweepGradient.cpp b/src/shaders/gradients/SkSweepGradient.cpp
index efbfa66..c85f3de 100644
--- a/src/shaders/gradients/SkSweepGradient.cpp
+++ b/src/shaders/gradients/SkSweepGradient.cpp
@@ -12,6 +12,7 @@
 #include "src/shaders/SkLocalMatrixShader.h"
 
 #ifdef SK_GRAPHITE_ENABLED
+#include "src/gpu/graphite/KeyContext.h"
 #include "src/gpu/graphite/KeyHelpers.h"
 #include "src/gpu/graphite/PaintParamsKey.h"
 #endif
@@ -194,13 +195,17 @@
                                skgpu::graphite::PipelineDataGatherer* gatherer) const {
     using namespace skgpu::graphite;
 
+    // TODO: respect the interpolateInPremul setting
+    SkColor4fXformer xformedColors(this, keyContext.dstColorInfo().colorSpace());
+    const SkPMColor4f* colors = xformedColors.fColors.begin();
+
     GradientShaderBlocks::GradientData data(SkShaderBase::GradientType::kSweep,
                                             fCenter, { 0.0f, 0.0f },
                                             0.0, 0.0f,
                                             fTBias, fTScale,
                                             fTileMode,
                                             fColorCount,
-                                            fColors,
+                                            colors,
                                             fPositions);
 
     GradientShaderBlocks::BeginBlock(keyContext, builder, gatherer, data);
diff --git a/src/shaders/gradients/SkTwoPointConicalGradient.cpp b/src/shaders/gradients/SkTwoPointConicalGradient.cpp
index 6e3c7b9..7e771fb 100644
--- a/src/shaders/gradients/SkTwoPointConicalGradient.cpp
+++ b/src/shaders/gradients/SkTwoPointConicalGradient.cpp
@@ -15,6 +15,7 @@
 #include <utility>
 
 #ifdef SK_GRAPHITE_ENABLED
+#include "src/gpu/graphite/KeyContext.h"
 #include "src/gpu/graphite/KeyHelpers.h"
 #include "src/gpu/graphite/PaintParamsKey.h"
 #endif
@@ -536,13 +537,17 @@
                                          skgpu::graphite::PipelineDataGatherer* gatherer) const {
     using namespace skgpu::graphite;
 
+    // TODO: respect the interpolateInPremul setting
+    SkColor4fXformer xformedColors(this, keyContext.dstColorInfo().colorSpace());
+    const SkPMColor4f* colors = xformedColors.fColors.begin();
+
     GradientShaderBlocks::GradientData data(GradientType::kConical,
                                             fCenter1, fCenter2,
                                             fRadius1, fRadius2,
                                             0.0f, 0.0f,
                                             fTileMode,
                                             fColorCount,
-                                            fColors,
+                                            colors,
                                             fPositions);
 
     GradientShaderBlocks::BeginBlock(keyContext, builder, gatherer, data);