Don't apply (-webkit-)mask-composite on the bottom mask layer

 "If there is no further mask layer, the compositing operator must
  be ignored."

  (https://drafts.fxtf.org/css-masking/#the-mask-composite)

Bug: 915847
Change-Id: Ia42f188160cf84648e364829c7733dbb0895936b
Reviewed-on: https://chromium-review.googlesource.com/c/1384251
Reviewed-by: Stephen Chenney <schenney@chromium.org>
Commit-Queue: Fredrik Söderquist <fs@opera.com>
Cr-Commit-Position: refs/heads/master@{#617865}
diff --git a/third_party/blink/renderer/core/paint/box_painter_base.cc b/third_party/blink/renderer/core/paint/box_painter_base.cc
index 4f5c3e5..37dc1aa 100644
--- a/third_party/blink/renderer/core/paint/box_painter_base.cc
+++ b/third_party/blink/renderer/core/paint/box_painter_base.cc
@@ -580,6 +580,12 @@
   return adjusted;
 }
 
+bool ShouldApplyBlendOperation(const BoxPainterBase::FillLayerInfo& info,
+                               const FillLayer& layer) {
+  // For a mask layer, don't use the operator if this is the bottom layer.
+  return !info.is_bottom_layer || layer.GetType() != EFillLayerType::kMask;
+}
+
 }  // anonymous namespace
 
 LayoutRectOutsets BoxPainterBase::AdjustedBorderOutsets(
@@ -612,7 +618,7 @@
   const auto did_adjust_paint_rect = scrolled_paint_rect != rect;
 
   scoped_refptr<Image> image;
-  SkBlendMode composite_op = op;
+  SkBlendMode composite_op = SkBlendMode::kSrcOver;
   base::Optional<ScopedInterpolationQuality> interpolation_quality_context;
   if (info.should_paint_image) {
     geometry.Calculate(paint_info.PaintContainer(), paint_info.phase,
@@ -627,10 +633,10 @@
     if (bg_layer.MaskSourceType() == EMaskSourceType::kLuminance)
       context.SetColorFilter(kColorFilterLuminanceToAlpha);
 
-    // If op != SkBlendMode::kSrcOver, a mask is being painted.
-    SkBlendMode bg_op = WebCoreCompositeToSkiaComposite(
-        bg_layer.Composite(), bg_layer.GetBlendMode());
-    composite_op = (op == SkBlendMode::kSrcOver) ? bg_op : op;
+    if (ShouldApplyBlendOperation(info, bg_layer)) {
+      composite_op = WebCoreCompositeToSkiaComposite(bg_layer.Composite(),
+                                                     bg_layer.GetBlendMode());
+    }
   }
 
   LayoutRectOutsets border = ComputeBorders();
diff --git a/third_party/blink/web_tests/css3/masking/mask-composite-source-in-single-layer-expected.html b/third_party/blink/web_tests/css3/masking/mask-composite-source-in-single-layer-expected.html
new file mode 100644
index 0000000..f718ea6
--- /dev/null
+++ b/third_party/blink/web_tests/css3/masking/mask-composite-source-in-single-layer-expected.html
@@ -0,0 +1,2 @@
+<!DOCTYPE html>
+<div style="width: 100px; height: 100px; background-color: green"></div>
diff --git a/third_party/blink/web_tests/css3/masking/mask-composite-source-in-single-layer.html b/third_party/blink/web_tests/css3/masking/mask-composite-source-in-single-layer.html
new file mode 100644
index 0000000..b856650
--- /dev/null
+++ b/third_party/blink/web_tests/css3/masking/mask-composite-source-in-single-layer.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<style>
+#target {
+  width: 100px;
+  height: 100px;
+  background-color: green;
+  -webkit-mask-image: linear-gradient(white, white);
+  -webkit-mask-composite: source-in;
+}
+</style>
+<div id="target"></div>