Helper functions to do SkColor -> GrColor4f

I started fixing more effects and realized I needed something like this.
Wanted to land it separately. After this, I'll add the DC's cached xform
from sRGB to AsFPArgs, so that we can easily leverage this code in more
places (mostly GrConstColorProcessor, or any effect that falls back to
that based on invariants, etc...)

BUG=skia:

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2844

Change-Id: I335546f02a6c49620494d736140a72c14441b35d
Reviewed-on: https://skia-review.googlesource.com/2844
Reviewed-by: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/include/gpu/SkGr.h b/include/gpu/SkGr.h
index 70db775..43d61e2 100644
--- a/include/gpu/SkGr.h
+++ b/include/gpu/SkGr.h
@@ -16,6 +16,7 @@
 #include "SkImageInfo.h"
 
 class GrCaps;
+class GrColorSpaceXform;
 class GrContext;
 class GrTexture;
 class GrTextureParams;
@@ -41,6 +42,9 @@
     return GrColorPackRGBA(r, g, b, a);
 }
 
+GrColor4f SkColorToPremulGrColor4f(SkColor c, bool gammaCorrect, GrColorSpaceXform* gamutXform);
+GrColor4f SkColorToUnpremulGrColor4f(SkColor c, bool gammaCorrect, GrColorSpaceXform* gamutXform);
+
 static inline GrColor SkColorToOpaqueGrColor(SkColor c) {
     unsigned r = SkColorGetR(c);
     unsigned g = SkColorGetG(c);
diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp
index 6d1e735..d4db461 100644
--- a/src/gpu/SkGr.cpp
+++ b/src/gpu/SkGr.cpp
@@ -437,6 +437,33 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 
+GrColor4f SkColorToPremulGrColor4f(SkColor c, bool gammaCorrect, GrColorSpaceXform* gamutXform) {
+    // We want to premultiply after linearizing, so this is easy:
+    return SkColorToUnpremulGrColor4f(c, gammaCorrect, gamutXform).premul();
+}
+
+GrColor4f SkColorToUnpremulGrColor4f(SkColor c, bool gammaCorrect, GrColorSpaceXform* gamutXform) {
+    // You can't be color-space aware in legacy mode
+    SkASSERT(gammaCorrect || !gamutXform);
+
+    GrColor4f color;
+    if (gammaCorrect) {
+        // SkColor4f::FromColor does sRGB -> Linear
+        color = GrColor4f::FromSkColor4f(SkColor4f::FromColor(c));
+    } else {
+        // GrColor4f::FromGrColor just multiplies by 1/255
+        color = GrColor4f::FromGrColor(SkColorToUnpremulGrColor(c));
+    }
+
+    if (gamutXform) {
+        color = gamutXform->apply(color);
+    }
+
+    return color;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
 // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
 // alpha info, that will be considered.
 GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, const SkColorSpace* cs,
@@ -542,19 +569,9 @@
     grPaint->setAntiAlias(skPaint.isAntiAlias());
     grPaint->setAllowSRGBInputs(dc->isGammaCorrect());
 
-    // Raw translation of the SkPaint color to our 4f format:
-    GrColor4f origColor = GrColor4f::FromGrColor(SkColorToUnpremulGrColor(skPaint.getColor()));
-
-    // Linearize, if the color is meant to be in sRGB gamma:
-    if (dc->isGammaCorrect()) {
-        origColor.fRGBA[0] = exact_srgb_to_linear(origColor.fRGBA[0]);
-        origColor.fRGBA[1] = exact_srgb_to_linear(origColor.fRGBA[1]);
-        origColor.fRGBA[2] = exact_srgb_to_linear(origColor.fRGBA[2]);
-
-        if (dc->getColorXformFromSRGB()) {
-            origColor = dc->getColorXformFromSRGB()->apply(origColor);
-        }
-    }
+    // Convert SkPaint color to 4f format, including optional linearizing and gamut conversion.
+    GrColor4f origColor = SkColorToUnpremulGrColor4f(skPaint.getColor(), dc->isGammaCorrect(),
+                                                     dc->getColorXformFromSRGB());
 
     // Setup the initial color considering the shader, the SkPaint color, and the presence or not
     // of per-vertex colors.