Bug 1276834 - Part 2. Draw nothing when any mask-image refer to an unresolvable URL draft
authorcku <cku@mozilla.com>
Sat, 04 Jun 2016 00:02:04 +0800
changeset 375171 b015d541b4bf142fc8c0ff278997c1e9f211d479
parent 375170 4b78d0628f3ee7f51bf1d4286f4a436a5ebda465
child 522779 4655498bc610afa483dc0529d95b6dc6ee204a25
push id20182
push usercku@mozilla.com
push dateFri, 03 Jun 2016 16:04:43 +0000
bugs1276834
milestone49.0a1
Bug 1276834 - Part 2. Draw nothing when any mask-image refer to an unresolvable URL MozReview-Commit-ID: 7gdKz0L1wmd
layout/svg/nsSVGIntegrationUtils.cpp
--- a/layout/svg/nsSVGIntegrationUtils.cpp
+++ b/layout/svg/nsSVGIntegrationUtils.cpp
@@ -513,16 +513,53 @@ GenerateMaskSurface(const nsSVGIntegrati
   if (!aOutMaskTransform.Invert()) {
     return nullptr;
   }
 
   RefPtr<SourceSurface> maskSurface = maskDT->Snapshot();
   return maskSurface.forget();
 }
 
+static bool
+ShouldCreateMaskSurface(const nsIFrame* aFrame,
+                        const nsStyleSVGReset* aSVGReset,
+                        const nsTArray<nsSVGMaskFrame *>& maskFrames,
+                        bool& aHasUnresovlableMaskLayer)
+{
+  aHasUnresovlableMaskLayer = false;
+
+  // For a HTML doc:
+  //   According to css-masking spec, always create a mask surface when we
+  //   have any item in maskFrame even if all of those items are
+  //   non-resolvable <mask-sources> or <images>, we still need to create a
+  //   transparent black mask layer under this condition.
+  // For a SVG doc:
+  //   SVG 1.1 say that  if we fail to resolve a mask, we should draw the
+  //   object unmasked.
+  nsIDocument* currentDoc = aFrame->PresContext()->Document();
+  bool shouldCreateMaskSurface = currentDoc->IsSVGDocument()
+                                 ? maskFrames.Length() == 1 && maskFrames[0]
+                                 : maskFrames.Length() > 0;
+
+  if (!shouldCreateMaskSurface) {
+    return false;
+  }
+
+  for (size_t i = 0; i < maskFrames.Length() ; i++) {
+    nsSVGMaskFrame *maskFrame = maskFrames[i];
+
+    if (!maskFrame && !aSVGReset->mMask.mLayers[i].mImage.IsComplete()) {
+      aHasUnresovlableMaskLayer = true;
+      break;
+    }
+  }
+
+  return true;
+}
+
 void
 nsSVGIntegrationUtils::PaintFramesWithEffects(const PaintFramesParams& aParams)
 {
 #ifdef DEBUG
   NS_ASSERTION(!(aParams.frame->GetStateBits() & NS_FRAME_SVG_LAYOUT) ||
                (NS_SVGDisplayListPaintingEnabled() &&
                 !(aParams.frame->GetStateBits() & NS_FRAME_IS_NONDISPLAY)),
                "Should not use nsSVGIntegrationUtils on this SVG frame");
@@ -612,51 +649,58 @@ nsSVGIntegrationUtils::PaintFramesWithEf
     nsLayoutUtils::PointToGfxPoint(offsetToUserSpace,
                                    frame->PresContext()->AppUnitsPerDevPixel());
   context.SetMatrix(context.CurrentMatrix().Translate(devPixelOffsetToUserSpace));
 
   gfxMatrix cssPxToDevPxMatrix = GetCSSPxToDevPxMatrix(frame);
 
   const nsStyleSVGReset *svgReset = firstFrame->StyleSVGReset();
   nsTArray<nsSVGMaskFrame *> maskFrames = effectProperties.GetMaskFrames();
-  // For a HTML doc:
-  //   According to css-masking spec, always create a mask surface when we
-  //   have any item in maskFrame even if all of those items are
-  //   non-resolvable <mask-sources> or <images>, we still need to create a
-  //   transparent black mask layer under this condition.
-  // For a SVG doc:
-  //   SVG 1.1 say that  if we fail to resolve a mask, we should draw the
-  //   object unmasked.
-  nsIDocument* currentDoc = frame->PresContext()->Document();
-  bool shouldGenerateMaskLayer = currentDoc->IsSVGDocument()
-                                 ? maskFrames.Length() == 1 && maskFrames[0]
-                                 : maskFrames.Length() > 0;
+  bool shouldCreateMaskSurface = false, hasUnresovlableMaskLayer = false;
+  shouldCreateMaskSurface = ShouldCreateMaskSurface(frame, svgReset,
+                                                    maskFrames,
+                                                    hasUnresovlableMaskLayer);
+
+  if (hasUnresovlableMaskLayer) {
+    MOZ_ASSERT(shouldCreateMaskSurface);
+    // We have at least one mask layer not resolvable. Clear clipped
+    // region as if we push a transparent black mask into context.
+    //
+    // With an unresolvable mask layer, there is no need to care about any
+    // other SVG effects, since a transparent mask basically filter out
+    // each pixel of the target surface.
+    gfxContextAutoSaveRestore saver(&context);
+
+    context.SetColor(Color(0.f, 0.f, 0.f, 0.f));
+    context.Paint();
+    return;
+  }
 
   // These are used if we require a temporary surface for a custom blend mode.
   RefPtr<gfxContext> target = &aParams.ctx;
   IntPoint targetOffset;
 
   bool complexEffects = false;
   /* Check if we need to do additional operations on this child's
    * rendering, which necessitates rendering into another surface. */
   if (opacity != 1.0f ||  (clipPathFrame && !isTrivialClip)
       || frame->StyleEffects()->mMixBlendMode != NS_STYLE_BLEND_NORMAL
-      || shouldGenerateMaskLayer) {
+      || shouldCreateMaskSurface) {
     complexEffects = true;
 
     context.Save();
     nsRect clipRect =
       frame->GetVisualOverflowRectRelativeToSelf() + toUserSpace;
     context.Clip(NSRectToSnappedRect(clipRect,
                                   frame->PresContext()->AppUnitsPerDevPixel(),
                                   *drawTarget));
     Matrix maskTransform;
     RefPtr<SourceSurface> maskSurface;
 
-    if (shouldGenerateMaskLayer) {
+    if (shouldCreateMaskSurface) {
       maskSurface = GenerateMaskSurface(aParams, opacity,
                                         firstFrame->StyleContext(),
                                         maskFrames, devPixelOffsetToUserSpace,
                                         maskTransform);
       if (!maskSurface) {
         // Entire surface is clipped out.
         context.Restore();
         return;
@@ -693,17 +737,17 @@ nsSVGIntegrationUtils::PaintFramesWithEf
                                                                          &clippedMaskTransform, maskSurface, maskTransform);
 
       if (clipMaskSurface) {
         maskSurface = clipMaskSurface;
         maskTransform = clippedMaskTransform;
       }
     }
 
-    if (opacity != 1.0f || shouldGenerateMaskLayer ||
+    if (opacity != 1.0f || shouldCreateMaskSurface ||
         (clipPathFrame && !isTrivialClip)) {
       target->PushGroupForBlendBack(gfxContentType::COLOR_ALPHA, opacity, maskSurface, maskTransform);
     }
   }
 
   /* If this frame has only a trivial clipPath, set up cairo's clipping now so
    * we can just do normal painting and get it clipped appropriately.
    */
@@ -739,17 +783,17 @@ nsSVGIntegrationUtils::PaintFramesWithEf
     context.Restore();
   }
 
   /* No more effects, we're done. */
   if (!complexEffects) {
     return;
   }
 
-  if (opacity != 1.0f || shouldGenerateMaskLayer ||
+  if (opacity != 1.0f || shouldCreateMaskSurface ||
       (clipPathFrame && !isTrivialClip)) {
     target->PopGroupAndBlend();
   }
 
   if (frame->StyleEffects()->mMixBlendMode != NS_STYLE_BLEND_NORMAL) {
     RefPtr<DrawTarget> targetDT = target->GetDrawTarget();
     target = nullptr;
     RefPtr<SourceSurface> targetSurf = targetDT->Snapshot();