layout/svg/nsSVGClipPathFrame.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/svg/nsSVGClipPathFrame.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,327 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +// Main header first:
    1.10 +#include "nsSVGClipPathFrame.h"
    1.11 +
    1.12 +// Keep others in (case-insensitive) order:
    1.13 +#include "gfxContext.h"
    1.14 +#include "nsGkAtoms.h"
    1.15 +#include "nsRenderingContext.h"
    1.16 +#include "mozilla/dom/SVGClipPathElement.h"
    1.17 +#include "nsSVGEffects.h"
    1.18 +#include "nsSVGUtils.h"
    1.19 +
    1.20 +using namespace mozilla::dom;
    1.21 +
    1.22 +//----------------------------------------------------------------------
    1.23 +// Implementation
    1.24 +
    1.25 +nsIFrame*
    1.26 +NS_NewSVGClipPathFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
    1.27 +{
    1.28 +  return new (aPresShell) nsSVGClipPathFrame(aContext);
    1.29 +}
    1.30 +
    1.31 +NS_IMPL_FRAMEARENA_HELPERS(nsSVGClipPathFrame)
    1.32 +
    1.33 +nsresult
    1.34 +nsSVGClipPathFrame::ClipPaint(nsRenderingContext* aContext,
    1.35 +                              nsIFrame* aParent,
    1.36 +                              const gfxMatrix &aMatrix)
    1.37 +{
    1.38 +  // If the flag is set when we get here, it means this clipPath frame
    1.39 +  // has already been used painting the current clip, and the document
    1.40 +  // has a clip reference loop.
    1.41 +  if (mInUse) {
    1.42 +    NS_WARNING("Clip loop detected!");
    1.43 +    return NS_OK;
    1.44 +  }
    1.45 +  AutoClipPathReferencer clipRef(this);
    1.46 +
    1.47 +  mClipParent = aParent;
    1.48 +  if (mClipParentMatrix) {
    1.49 +    *mClipParentMatrix = aMatrix;
    1.50 +  } else {
    1.51 +    mClipParentMatrix = new gfxMatrix(aMatrix);
    1.52 +  }
    1.53 +
    1.54 +  gfxContext *gfx = aContext->ThebesContext();
    1.55 +
    1.56 +  nsISVGChildFrame *singleClipPathChild = nullptr;
    1.57 +
    1.58 +  if (IsTrivial(&singleClipPathChild)) {
    1.59 +    // Notify our child that it's painting as part of a clipPath, and that
    1.60 +    // we only require it to draw its path (it should skip filling, etc.):
    1.61 +    SVGAutoRenderState mode(aContext, SVGAutoRenderState::CLIP);
    1.62 +
    1.63 +    if (!singleClipPathChild) {
    1.64 +      // We have no children - the spec says clip away everything:
    1.65 +      gfx->Rectangle(gfxRect());
    1.66 +    } else {
    1.67 +      singleClipPathChild->NotifySVGChanged(
    1.68 +                             nsISVGChildFrame::TRANSFORM_CHANGED);
    1.69 +      singleClipPathChild->PaintSVG(aContext, nullptr);
    1.70 +    }
    1.71 +    gfx->Clip();
    1.72 +    gfx->NewPath();
    1.73 +    return NS_OK;
    1.74 +  }
    1.75 +
    1.76 +  // Seems like this is a non-trivial clipPath, so we need to use a clip mask.
    1.77 +
    1.78 +  // Notify our children that they're painting into a clip mask:
    1.79 +  SVGAutoRenderState mode(aContext, SVGAutoRenderState::CLIP_MASK);
    1.80 +
    1.81 +  // Check if this clipPath is itself clipped by another clipPath:
    1.82 +  nsSVGClipPathFrame *clipPathFrame =
    1.83 +    nsSVGEffects::GetEffectProperties(this).GetClipPathFrame(nullptr);
    1.84 +  bool referencedClipIsTrivial;
    1.85 +  if (clipPathFrame) {
    1.86 +    referencedClipIsTrivial = clipPathFrame->IsTrivial();
    1.87 +    gfx->Save();
    1.88 +    if (referencedClipIsTrivial) {
    1.89 +      clipPathFrame->ClipPaint(aContext, aParent, aMatrix);
    1.90 +    } else {
    1.91 +      gfx->PushGroup(gfxContentType::ALPHA);
    1.92 +    }
    1.93 +  }
    1.94 +
    1.95 +  for (nsIFrame* kid = mFrames.FirstChild(); kid;
    1.96 +       kid = kid->GetNextSibling()) {
    1.97 +    nsISVGChildFrame* SVGFrame = do_QueryFrame(kid);
    1.98 +    if (SVGFrame) {
    1.99 +      // The CTM of each frame referencing us can be different.
   1.100 +      SVGFrame->NotifySVGChanged(nsISVGChildFrame::TRANSFORM_CHANGED);
   1.101 +
   1.102 +      bool isOK = true;
   1.103 +      nsSVGClipPathFrame *clipPathFrame =
   1.104 +        nsSVGEffects::GetEffectProperties(kid).GetClipPathFrame(&isOK);
   1.105 +      if (!isOK) {
   1.106 +        continue;
   1.107 +      }
   1.108 +
   1.109 +      bool isTrivial;
   1.110 +
   1.111 +      if (clipPathFrame) {
   1.112 +        isTrivial = clipPathFrame->IsTrivial();
   1.113 +        gfx->Save();
   1.114 +        if (isTrivial) {
   1.115 +          clipPathFrame->ClipPaint(aContext, aParent, aMatrix);
   1.116 +        } else {
   1.117 +          gfx->PushGroup(gfxContentType::ALPHA);
   1.118 +        }
   1.119 +      }
   1.120 +
   1.121 +      SVGFrame->PaintSVG(aContext, nullptr);
   1.122 +
   1.123 +      if (clipPathFrame) {
   1.124 +        if (!isTrivial) {
   1.125 +          gfx->PopGroupToSource();
   1.126 +
   1.127 +          nsRefPtr<gfxPattern> clipMaskSurface;
   1.128 +          gfx->PushGroup(gfxContentType::ALPHA);
   1.129 +
   1.130 +          clipPathFrame->ClipPaint(aContext, aParent, aMatrix);
   1.131 +          clipMaskSurface = gfx->PopGroup();
   1.132 +
   1.133 +          if (clipMaskSurface) {
   1.134 +            gfx->Mask(clipMaskSurface);
   1.135 +          }
   1.136 +        }
   1.137 +        gfx->Restore();
   1.138 +      }
   1.139 +    }
   1.140 +  }
   1.141 +
   1.142 +  if (clipPathFrame) {
   1.143 +    if (!referencedClipIsTrivial) {
   1.144 +      gfx->PopGroupToSource();
   1.145 +
   1.146 +      nsRefPtr<gfxPattern> clipMaskSurface;
   1.147 +      gfx->PushGroup(gfxContentType::ALPHA);
   1.148 +
   1.149 +      clipPathFrame->ClipPaint(aContext, aParent, aMatrix);
   1.150 +      clipMaskSurface = gfx->PopGroup();
   1.151 +
   1.152 +      if (clipMaskSurface) {
   1.153 +        gfx->Mask(clipMaskSurface);
   1.154 +      }
   1.155 +    }
   1.156 +    gfx->Restore();
   1.157 +  }
   1.158 +
   1.159 +  return NS_OK;
   1.160 +}
   1.161 +
   1.162 +bool
   1.163 +nsSVGClipPathFrame::ClipHitTest(nsIFrame* aParent,
   1.164 +                                const gfxMatrix &aMatrix,
   1.165 +                                const nsPoint &aPoint)
   1.166 +{
   1.167 +  // If the flag is set when we get here, it means this clipPath frame
   1.168 +  // has already been used in hit testing against the current clip,
   1.169 +  // and the document has a clip reference loop.
   1.170 +  if (mInUse) {
   1.171 +    NS_WARNING("Clip loop detected!");
   1.172 +    return false;
   1.173 +  }
   1.174 +  AutoClipPathReferencer clipRef(this);
   1.175 +
   1.176 +  mClipParent = aParent;
   1.177 +  if (mClipParentMatrix) {
   1.178 +    *mClipParentMatrix = aMatrix;
   1.179 +  } else {
   1.180 +    mClipParentMatrix = new gfxMatrix(aMatrix);
   1.181 +  }
   1.182 +
   1.183 +  nsSVGClipPathFrame *clipPathFrame =
   1.184 +    nsSVGEffects::GetEffectProperties(this).GetClipPathFrame(nullptr);
   1.185 +  if (clipPathFrame && !clipPathFrame->ClipHitTest(aParent, aMatrix, aPoint))
   1.186 +    return false;
   1.187 +
   1.188 +  for (nsIFrame* kid = mFrames.FirstChild(); kid;
   1.189 +       kid = kid->GetNextSibling()) {
   1.190 +    nsISVGChildFrame* SVGFrame = do_QueryFrame(kid);
   1.191 +    if (SVGFrame) {
   1.192 +      // Notify the child frame that we may be working with a
   1.193 +      // different transform, so it can update its covered region
   1.194 +      // (used to shortcut hit testing).
   1.195 +      SVGFrame->NotifySVGChanged(nsISVGChildFrame::TRANSFORM_CHANGED);
   1.196 +
   1.197 +      if (SVGFrame->GetFrameForPoint(aPoint))
   1.198 +        return true;
   1.199 +    }
   1.200 +  }
   1.201 +  return false;
   1.202 +}
   1.203 +
   1.204 +bool
   1.205 +nsSVGClipPathFrame::IsTrivial(nsISVGChildFrame **aSingleChild)
   1.206 +{
   1.207 +  // If the clip path is clipped then it's non-trivial
   1.208 +  if (nsSVGEffects::GetEffectProperties(this).GetClipPathFrame(nullptr))
   1.209 +    return false;
   1.210 +
   1.211 +  if (aSingleChild) {
   1.212 +    *aSingleChild = nullptr;
   1.213 +  }
   1.214 +
   1.215 +  nsISVGChildFrame *foundChild = nullptr;
   1.216 +
   1.217 +  for (nsIFrame* kid = mFrames.FirstChild(); kid;
   1.218 +       kid = kid->GetNextSibling()) {
   1.219 +    nsISVGChildFrame *svgChild = do_QueryFrame(kid);
   1.220 +    if (svgChild) {
   1.221 +      // We consider a non-trivial clipPath to be one containing
   1.222 +      // either more than one svg child and/or a svg container
   1.223 +      if (foundChild || svgChild->IsDisplayContainer())
   1.224 +        return false;
   1.225 +
   1.226 +      // or where the child is itself clipped
   1.227 +      if (nsSVGEffects::GetEffectProperties(kid).GetClipPathFrame(nullptr))
   1.228 +        return false;
   1.229 +
   1.230 +      foundChild = svgChild;
   1.231 +    }
   1.232 +  }
   1.233 +  if (aSingleChild) {
   1.234 +    *aSingleChild = foundChild;
   1.235 +  }
   1.236 +  return true;
   1.237 +}
   1.238 +
   1.239 +bool
   1.240 +nsSVGClipPathFrame::IsValid()
   1.241 +{
   1.242 +  if (mInUse) {
   1.243 +    NS_WARNING("Clip loop detected!");
   1.244 +    return false;
   1.245 +  }
   1.246 +  AutoClipPathReferencer clipRef(this);
   1.247 +
   1.248 +  bool isOK = true;
   1.249 +  nsSVGEffects::GetEffectProperties(this).GetClipPathFrame(&isOK);
   1.250 +  if (!isOK) {
   1.251 +    return false;
   1.252 +  }
   1.253 +
   1.254 +  for (nsIFrame* kid = mFrames.FirstChild(); kid;
   1.255 +       kid = kid->GetNextSibling()) {
   1.256 +
   1.257 +    nsIAtom *type = kid->GetType();
   1.258 +
   1.259 +    if (type == nsGkAtoms::svgUseFrame) {
   1.260 +      for (nsIFrame* grandKid = kid->GetFirstPrincipalChild(); grandKid;
   1.261 +           grandKid = grandKid->GetNextSibling()) {
   1.262 +
   1.263 +        nsIAtom *type = grandKid->GetType();
   1.264 +
   1.265 +        if (type != nsGkAtoms::svgPathGeometryFrame &&
   1.266 +            type != nsGkAtoms::svgTextFrame) {
   1.267 +          return false;
   1.268 +        }
   1.269 +      }
   1.270 +      continue;
   1.271 +    }
   1.272 +    if (type != nsGkAtoms::svgPathGeometryFrame &&
   1.273 +        type != nsGkAtoms::svgTextFrame) {
   1.274 +      return false;
   1.275 +    }
   1.276 +  }
   1.277 +  return true;
   1.278 +}
   1.279 +
   1.280 +nsresult
   1.281 +nsSVGClipPathFrame::AttributeChanged(int32_t         aNameSpaceID,
   1.282 +                                     nsIAtom*        aAttribute,
   1.283 +                                     int32_t         aModType)
   1.284 +{
   1.285 +  if (aNameSpaceID == kNameSpaceID_None) {
   1.286 +    if (aAttribute == nsGkAtoms::transform) {
   1.287 +      nsSVGEffects::InvalidateDirectRenderingObservers(this);
   1.288 +      nsSVGUtils::NotifyChildrenOfSVGChange(this,
   1.289 +                                            nsISVGChildFrame::TRANSFORM_CHANGED);
   1.290 +    }
   1.291 +    if (aAttribute == nsGkAtoms::clipPathUnits) {
   1.292 +      nsSVGEffects::InvalidateRenderingObservers(this);
   1.293 +    }
   1.294 +  }
   1.295 +
   1.296 +  return nsSVGClipPathFrameBase::AttributeChanged(aNameSpaceID,
   1.297 +                                                  aAttribute, aModType);
   1.298 +}
   1.299 +
   1.300 +void
   1.301 +nsSVGClipPathFrame::Init(nsIContent* aContent,
   1.302 +                         nsIFrame* aParent,
   1.303 +                         nsIFrame* aPrevInFlow)
   1.304 +{
   1.305 +  NS_ASSERTION(aContent->IsSVG(nsGkAtoms::clipPath),
   1.306 +               "Content is not an SVG clipPath!");
   1.307 +
   1.308 +  AddStateBits(NS_STATE_SVG_CLIPPATH_CHILD);
   1.309 +  nsSVGClipPathFrameBase::Init(aContent, aParent, aPrevInFlow);
   1.310 +}
   1.311 +
   1.312 +nsIAtom *
   1.313 +nsSVGClipPathFrame::GetType() const
   1.314 +{
   1.315 +  return nsGkAtoms::svgClipPathFrame;
   1.316 +}
   1.317 +
   1.318 +gfxMatrix
   1.319 +nsSVGClipPathFrame::GetCanvasTM(uint32_t aFor, nsIFrame* aTransformRoot)
   1.320 +{
   1.321 +  SVGClipPathElement *content = static_cast<SVGClipPathElement*>(mContent);
   1.322 +
   1.323 +  gfxMatrix tm =
   1.324 +    content->PrependLocalTransformsTo(mClipParentMatrix ?
   1.325 +                                      *mClipParentMatrix : gfxMatrix());
   1.326 +
   1.327 +  return nsSVGUtils::AdjustMatrixForUnits(tm,
   1.328 +                                          &content->mEnumAttributes[SVGClipPathElement::CLIPPATHUNITS],
   1.329 +                                          mClipParent);
   1.330 +}

mercurial