srgb conversion needs to be in unpremul

Bug: skia:
Change-Id: Ic7dffb2cb30aae33a6c127ab10ea4282e1360114
Reviewed-on: https://skia-review.googlesource.com/21536
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
diff --git a/gm/srgb.cpp b/gm/srgb.cpp
new file mode 100644
index 0000000..bbf0abd
--- /dev/null
+++ b/gm/srgb.cpp
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "gm.h"
+#include "sk_tool_utils.h"
+#include "SkCanvas.h"
+#include "SkImage.h"
+#include "Resources.h"
+#include "SkColorFilter.h"
+
+DEF_SIMPLE_GM(srgb_colorfilter, canvas, 512, 256*3) {
+    auto img = GetResourceAsImage("mandrill_256.png");
+
+    const float array[] = {
+        1, 0, 0, 0, 0,
+        0, 1, 0, 0, 0,
+        0, 0, 1, 0, 0,
+        -1, 0, 0, 1, 0,
+    };
+    auto cf0 = SkColorFilter::MakeMatrixFilterRowMajor255(array);
+    auto cf1 = SkColorFilter::MakeLinearToSRGBGamma();
+    auto cf2 = SkColorFilter::MakeSRGBToLinearGamma();
+
+    SkPaint p;
+    p.setColorFilter(cf0);
+    canvas->drawImage(img, 0, 0, nullptr);
+    canvas->drawImage(img, 256, 0, &p);
+
+    p.setColorFilter(cf1);
+    canvas->drawImage(img, 0, 256, &p);
+    p.setColorFilter(SkColorFilter::MakeComposeFilter(cf1, cf0));
+    canvas->drawImage(img, 256, 256, &p);
+
+    p.setColorFilter(cf2);
+    canvas->drawImage(img, 0, 512, &p);
+    p.setColorFilter(SkColorFilter::MakeComposeFilter(cf2, cf0));
+    canvas->drawImage(img, 256, 512, &p);
+}
diff --git a/gn/gm.gni b/gn/gm.gni
index b9bfd4b..077b80a 100644
--- a/gn/gm.gni
+++ b/gn/gm.gni
@@ -274,6 +274,7 @@
   "$_gm/smallpaths.cpp",
   "$_gm/spritebitmap.cpp",
   "$_gm/srcmode.cpp",
+  "$_gm/srgb.cpp",
   "$_gm/stlouisarch.cpp",
   "$_gm/stringart.cpp",
   "$_gm/stroke_rect_shader.cpp",
diff --git a/src/core/SkColorFilter.cpp b/src/core/SkColorFilter.cpp
index 3da812a..74bad1a 100644
--- a/src/core/SkColorFilter.cpp
+++ b/src/core/SkColorFilter.cpp
@@ -228,14 +228,20 @@
 
     void onAppendStages(SkRasterPipeline* p, SkColorSpace*, SkArenaAlloc* alloc,
                         bool shaderIsOpaque) const override {
+        if (!shaderIsOpaque) {
+            p->append(SkRasterPipeline::unpremul);
+        }
         switch (fDir) {
             case Direction::kLinearToSRGB:
                 p->append(SkRasterPipeline::to_srgb);
                 break;
             case Direction::kSRGBToLinear:
-                p->append_from_srgb(shaderIsOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
+                p->append_from_srgb(shaderIsOpaque ? kOpaque_SkAlphaType : kUnpremul_SkAlphaType);
                 break;
         }
+        if (!shaderIsOpaque) {
+            p->append(SkRasterPipeline::premul);
+        }
     }
 
 protected: