Observe gTreatSkColorAsSRGB for 4f gradients.
authorfmalita <fmalita@chromium.org>
Fri, 15 Apr 2016 20:03:07 +0000 (13:03 -0700)
committerCommit bot <commit-bot@chromium.org>
Fri, 15 Apr 2016 20:03:07 +0000 (13:03 -0700)
Use SkColor4f::FromColor() in the color packer to convert srgb->linear
when needed.

R=reed@google.com,herb@google.com
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1890253002

Review URL: https://codereview.chromium.org/1890253002

src/effects/gradients/Sk4fGradientBase.cpp
src/effects/gradients/Sk4fGradientBase.h

index e09ba77..a221368 100644 (file)
 
 namespace {
 
-SkPMColor pack_color(SkColor c, bool premul) {
-    return premul
-        ? SkPreMultiplyColor(c)
-        : SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), SkColorGetB(c));
+Sk4f pack_color(SkColor c, bool premul, const Sk4f& component_scale) {
+    const SkColor4f c4f = SkColor4f::FromColor(c);
+    const Sk4f pm4f = premul
+        ? c4f.premul().to4f()
+        : Sk4f{c4f.fR, c4f.fG, c4f.fB, c4f.fA};
+
+    return pm4f * component_scale;
 }
 
 template<SkShader::TileMode>
@@ -117,20 +120,17 @@ private:
 } // anonymous namespace
 
 SkGradientShaderBase::GradientShaderBase4fContext::
-Interval::Interval(SkPMColor c0, SkScalar p0,
-                   SkPMColor c1, SkScalar p1,
-                   const Sk4f& componentScale)
+Interval::Interval(const Sk4f& c0, SkScalar p0,
+                   const Sk4f& c1, SkScalar p1)
     : fP0(p0)
     , fP1(p1)
-    , fZeroRamp(c0 == c1) {
-    SkASSERT(p0 != p1);
+    , fZeroRamp((c0 == c1).allTrue()) {
 
-    const Sk4f c4f0 = SkPM4f::FromPMColor(c0).to4f() * componentScale;
-    const Sk4f c4f1 = SkPM4f::FromPMColor(c1).to4f() * componentScale;
-    const Sk4f dc4f = (c4f1 - c4f0) / (p1 - p0);
+    SkASSERT(p0 != p1);
+    const Sk4f dc = (c1 - c0) / (p1 - p0);
 
-    c4f0.store(&fC0.fVec);
-    dc4f.store(&fDc.fVec);
+    c0.store(&fC0.fVec);
+    dc.store(&fDc.fVec);
 }
 
 SkGradientShaderBase::
@@ -216,12 +216,11 @@ GradientShaderBase4fContext::buildIntervals(const SkGradientShaderBase& shader,
 
     if (shader.fTileMode == SkShader::kClamp_TileMode) {
         // synthetic edge interval: -/+inf .. P0
-        const SkPMColor clamp_color = pack_color(shader.fOrigColors[first_index],
-                                                 fColorsArePremul);
+        const Sk4f clamp_color = pack_color(shader.fOrigColors[first_index],
+                                            fColorsArePremul, componentScale);
         const SkScalar clamp_pos = reverse ? SK_ScalarMax : SK_ScalarMin;
         fIntervals.emplace_back(clamp_color, clamp_pos,
-                                clamp_color, first_pos,
-                                componentScale);
+                                clamp_color, first_pos);
     } else if (shader.fTileMode == SkShader::kMirror_TileMode && reverse) {
         // synthetic mirror intervals injected before main intervals: (2 .. 1]
         addMirrorIntervals(shader, componentScale, false);
@@ -234,21 +233,19 @@ GradientShaderBase4fContext::buildIntervals(const SkGradientShaderBase& shader,
     iter.iterate([this, &componentScale] (SkColor c0, SkColor c1, SkScalar p0, SkScalar p1) {
         SkASSERT(fIntervals.empty() || fIntervals.back().fP1 == p0);
 
-        fIntervals.emplace_back(pack_color(c0, fColorsArePremul),
+        fIntervals.emplace_back(pack_color(c0, fColorsArePremul, componentScale),
                                 p0,
-                                pack_color(c1, fColorsArePremul),
-                                p1,
-                                componentScale);
+                                pack_color(c1, fColorsArePremul, componentScale),
+                                p1);
     });
 
     if (shader.fTileMode == SkShader::kClamp_TileMode) {
         // synthetic edge interval: Pn .. +/-inf
-        const SkPMColor clamp_color =
-            pack_color(shader.fOrigColors[last_index], fColorsArePremul);
+        const Sk4f clamp_color = pack_color(shader.fOrigColors[last_index],
+                                            fColorsArePremul, componentScale);
         const SkScalar clamp_pos = reverse ? SK_ScalarMin : SK_ScalarMax;
         fIntervals.emplace_back(clamp_color, last_pos,
-                                clamp_color, clamp_pos,
-                                componentScale);
+                                clamp_color, clamp_pos);
     } else if (shader.fTileMode == SkShader::kMirror_TileMode && !reverse) {
         // synthetic mirror intervals injected after main intervals: [1 .. 2)
         addMirrorIntervals(shader, componentScale, true);
@@ -265,11 +262,10 @@ GradientShaderBase4fContext::addMirrorIntervals(const SkGradientShaderBase& shad
     iter.iterate([this, &componentScale] (SkColor c0, SkColor c1, SkScalar p0, SkScalar p1) {
         SkASSERT(fIntervals.empty() || fIntervals.back().fP1 == 2 - p0);
 
-        fIntervals.emplace_back(pack_color(c0, fColorsArePremul),
+        fIntervals.emplace_back(pack_color(c0, fColorsArePremul, componentScale),
                                 2 - p0,
-                                pack_color(c1, fColorsArePremul),
-                                2 - p1,
-                                componentScale);
+                                pack_color(c1, fColorsArePremul, componentScale),
+                                2 - p1);
     });
 }
 
index ddd0b2b..17f9563 100644 (file)
@@ -30,9 +30,8 @@ public:
 
 protected:
     struct Interval {
-        Interval(SkPMColor c0, SkScalar p0,
-                 SkPMColor c1, SkScalar p1,
-                 const Sk4f& componentScale);
+        Interval(const Sk4f& c0, SkScalar p0,
+                 const Sk4f& c1, SkScalar p1);
 
         bool isZeroRamp() const { return fZeroRamp; }