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 383253 17a4c061ded551156fd36835c84acacef8f396e4
parent 383252 1c79b8e51098831201a26cf33e87990337cee6bf
child 524423 ddd5446f8a1207195305fb3ed0e16c22b137617d
push id21967
push usercku@mozilla.com
push dateFri, 01 Jul 2016 21:02:32 +0000
bugs1276834
milestone50.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
@@ -449,23 +449,17 @@ ComputeMaskGeometry(const nsSVGIntegrati
 
   ctx.Save();
 
   // Clip ctx by both frame's visual overflow rect and mask union.
   gfxRect frameVisualOverflowRect =
     nsLayoutUtils::RectToGfxRect(frame->GetVisualOverflowRectRelativeToSelf(),
                                  appUnitsPerDevPixel);
   ctx.Clip(frameVisualOverflowRect);
-  // maskInUserSpace might be empty if all mask references are not resolvable
-  // or the size of them are empty. We still need to create a transparent mask
-  // before bug 1276834 fixed, so don't clip ctx by an empty rectangle for for
-  // now.
-  if (!maskInUserSpace.IsEmpty()) {
-    ctx.Clip(maskInUserSpace);
-  }
+  ctx.Clip(maskInUserSpace);
 
   // Get the clip extents in device space.
   ctx.SetMatrix(gfxMatrix());
   gfxRect clippedFrameSurfaceRect = ctx.GetClipExtents();
   clippedFrameSurfaceRect.RoundOut();
 
   ctx.Restore();
 
@@ -585,16 +579,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& aHasUnresolvableMaskLayer)
+{
+  aHasUnresolvableMaskLayer = 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()) {
+      aHasUnresolvableMaskLayer = 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");
@@ -687,57 +718,51 @@ 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 hasUnresolvableMaskLayer = false;
+  bool shouldCreateMaskSurface =
+    ShouldCreateMaskSurface(frame, svgReset, maskFrames,
+                            hasUnresolvableMaskLayer);
 
   // 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, offsetToUserSpace,
                                         maskTransform);
-      if (!maskSurface) {
-        // Entire surface is clipped out.
+      if (!maskSurface || hasUnresolvableMaskLayer) {
+        // Entire surface is clipped out or we have non-resolvable masks.
+        // Check hasUnresolvableMaskLayer after calling GenerateMaskSurface
+        // to ensure we get invalidated for loads of the mask.
         context.Restore();
         return;
       }
     }
 
     if (frame->StyleEffects()->mMixBlendMode != NS_STYLE_BLEND_NORMAL) {
       // Create a temporary context to draw to so we can blend it back with
       // another operator.
@@ -768,17 +793,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.
    */
@@ -814,17 +839,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();