nsSVGClipPathFrame.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef __NS_SVGCLIPPATHFRAME_H__
  6. #define __NS_SVGCLIPPATHFRAME_H__
  7. #include "AutoReferenceLimiter.h"
  8. #include "gfxMatrix.h"
  9. #include "mozilla/Attributes.h"
  10. #include "nsSVGContainerFrame.h"
  11. #include "nsSVGUtils.h"
  12. class gfxContext;
  13. class nsISVGChildFrame;
  14. class nsSVGClipPathFrame : public nsSVGContainerFrame
  15. {
  16. friend nsIFrame*
  17. NS_NewSVGClipPathFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
  18. typedef mozilla::gfx::Matrix Matrix;
  19. typedef mozilla::gfx::SourceSurface SourceSurface;
  20. typedef mozilla::image::DrawResult DrawResult;
  21. protected:
  22. explicit nsSVGClipPathFrame(nsStyleContext* aContext)
  23. : nsSVGContainerFrame(aContext)
  24. , mReferencing(mozilla::AutoReferenceLimiter::notReferencing)
  25. {
  26. AddStateBits(NS_FRAME_IS_NONDISPLAY);
  27. }
  28. public:
  29. NS_DECL_FRAMEARENA_HELPERS
  30. // nsIFrame methods:
  31. virtual void BuildDisplayList(nsDisplayListBuilder* aBuilder,
  32. const nsDisplayListSet& aLists) override {}
  33. // nsSVGClipPathFrame methods:
  34. /**
  35. * Applies the clipPath by pushing a clip path onto the DrawTarget.
  36. *
  37. * This method must only be used if IsTrivial() returns true, otherwise use
  38. * GetClipMask.
  39. *
  40. * @param aContext The context that the clip path is to be applied to.
  41. * @param aClippedFrame The/an nsIFrame of the element that references this
  42. * clipPath that is currently being processed.
  43. * @param aMatrix The transform from aClippedFrame's user space to aContext's
  44. * current transform.
  45. */
  46. void ApplyClipPath(gfxContext& aContext,
  47. nsIFrame* aClippedFrame,
  48. const gfxMatrix &aMatrix);
  49. /**
  50. * Returns an alpha mask surface containing the clipping geometry.
  51. *
  52. * This method must only be used if IsTrivial() returns false, otherwise use
  53. * ApplyClipPath.
  54. *
  55. * @param aReferenceContext Used to determine the backend for and size of the
  56. * returned SourceSurface, the size being limited to the device space clip
  57. * extents on the context.
  58. * @param aClippedFrame The/an nsIFrame of the element that references this
  59. * clipPath that is currently being processed.
  60. * @param aMatrix The transform from aClippedFrame's user space to aContext's
  61. * current transform.
  62. * @param [out] aMaskTransform The transform to use with the returned
  63. * surface.
  64. * @param [in, optional] aExtraMask An extra surface that the returned
  65. * surface should be masked with.
  66. * @param [in, optional] aExtraMasksTransform The transform to use with
  67. * aExtraMask. Should be passed when aExtraMask is passed.
  68. * @param [out, optional] aResult returns the result of drawing action.
  69. */
  70. already_AddRefed<SourceSurface>
  71. GetClipMask(gfxContext& aReferenceContext, nsIFrame* aClippedFrame,
  72. const gfxMatrix& aMatrix, Matrix* aMaskTransform,
  73. SourceSurface* aExtraMask = nullptr,
  74. const Matrix& aExtraMasksTransform = Matrix(),
  75. DrawResult* aResult = nullptr);
  76. /**
  77. * aPoint is expected to be in aClippedFrame's SVG user space.
  78. */
  79. bool PointIsInsideClipPath(nsIFrame* aClippedFrame, const gfxPoint &aPoint);
  80. // Check if this clipPath is made up of more than one geometry object.
  81. // If so, the clipping API in cairo isn't enough and we need to use
  82. // mask based clipping.
  83. bool IsTrivial(nsISVGChildFrame **aSingleChild = nullptr);
  84. bool IsValid();
  85. // nsIFrame interface:
  86. virtual nsresult AttributeChanged(int32_t aNameSpaceID,
  87. nsIAtom* aAttribute,
  88. int32_t aModType) override;
  89. virtual void Init(nsIContent* aContent,
  90. nsContainerFrame* aParent,
  91. nsIFrame* aPrevInFlow) override;
  92. /**
  93. * Get the "type" of the frame
  94. *
  95. * @see nsGkAtoms::svgClipPathFrame
  96. */
  97. virtual nsIAtom* GetType() const override;
  98. #ifdef DEBUG_FRAME_DUMP
  99. virtual nsresult GetFrameName(nsAString& aResult) const override
  100. {
  101. return MakeFrameName(NS_LITERAL_STRING("SVGClipPath"), aResult);
  102. }
  103. #endif
  104. SVGBBox
  105. GetBBoxForClipPathFrame(const SVGBBox &aBBox, const gfxMatrix &aMatrix);
  106. /**
  107. * If the clipPath element transforms its children due to
  108. * clipPathUnits="objectBoundingBox" being set on it and/or due to the
  109. * 'transform' attribute being set on it, this function returns the resulting
  110. * transform.
  111. */
  112. gfxMatrix GetClipPathTransform(nsIFrame* aClippedFrame);
  113. private:
  114. // nsSVGContainerFrame methods:
  115. virtual gfxMatrix GetCanvasTM() override;
  116. // Set, during a GetClipMask() call, to the transform that still needs to be
  117. // concatenated to the transform of the DrawTarget that was passed to
  118. // GetClipMask in order to establish the coordinate space that the clipPath
  119. // establishes for its contents (i.e. including applying 'clipPathUnits' and
  120. // any 'transform' attribute set on the clipPath) specifically for clipping
  121. // the frame that was passed to GetClipMask at that moment in time. This is
  122. // set so that if our GetCanvasTM method is called while GetClipMask is
  123. // painting its children, the returned matrix will include the transforms
  124. // that should be used when creating the mask for the frame passed to
  125. // GetClipMask.
  126. //
  127. // Note: The removal of GetCanvasTM is nearly complete, so our GetCanvasTM
  128. // may not even be called soon/any more.
  129. gfxMatrix mMatrixForChildren;
  130. // Flag used by AutoReferenceLimiter while we're processing an instance of
  131. // this class to protect against (break) reference loops.
  132. int16_t mReferencing;
  133. };
  134. #endif