SVGClipPathFrame.h (6586B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef LAYOUT_SVG_SVGCLIPPATHFRAME_H_ 8 #define LAYOUT_SVG_SVGCLIPPATHFRAME_H_ 9 10 #include "gfxMatrix.h" 11 #include "mozilla/SVGContainerFrame.h" 12 13 class gfxContext; 14 15 namespace mozilla { 16 class ISVGDisplayableFrame; 17 class PresShell; 18 } // namespace mozilla 19 20 nsIFrame* NS_NewSVGClipPathFrame(mozilla::PresShell* aPresShell, 21 mozilla::ComputedStyle* aStyle); 22 23 namespace mozilla { 24 25 class SVGClipPathFrame final : public SVGContainerFrame { 26 friend nsIFrame* ::NS_NewSVGClipPathFrame(mozilla::PresShell* aPresShell, 27 ComputedStyle* aStyle); 28 29 using Matrix = gfx::Matrix; 30 using SourceSurface = gfx::SourceSurface; 31 using imgDrawingParams = image::imgDrawingParams; 32 33 protected: 34 explicit SVGClipPathFrame(ComputedStyle* aStyle, nsPresContext* aPresContext) 35 : SVGContainerFrame(aStyle, aPresContext, kClassID), 36 mIsBeingProcessed(false) { 37 AddStateBits(NS_FRAME_IS_NONDISPLAY | NS_STATE_SVG_CLIPPATH_CHILD | 38 NS_FRAME_MAY_BE_TRANSFORMED); 39 } 40 41 public: 42 NS_DECL_FRAMEARENA_HELPERS(SVGClipPathFrame) 43 44 // nsIFrame methods: 45 void BuildDisplayList(nsDisplayListBuilder* aBuilder, 46 const nsDisplayListSet& aLists) override {} 47 48 // SVGClipPathFrame methods: 49 50 /** 51 * Applies the clipPath by pushing a clip path onto the DrawTarget. 52 * 53 * This method must only be used if IsTrivial() returns true, otherwise use 54 * GetClipMask. 55 * 56 * @param aContext The context that the clip path is to be applied to. 57 * @param aClippedFrame The/an nsIFrame of the element that references this 58 * clipPath that is currently being processed. 59 * @param aMatrix The transform from aClippedFrame's user space to aContext's 60 * current transform. 61 */ 62 void ApplyClipPath(gfxContext& aContext, nsIFrame* aClippedFrame, 63 const gfxMatrix& aMatrix); 64 65 /** 66 * Returns an alpha mask surface containing the clipping geometry. 67 * 68 * This method must only be used if IsTrivial() returns false, otherwise use 69 * ApplyClipPath. 70 * 71 * @param aReferenceContext Used to determine the backend for and size of the 72 * returned SourceSurface, the size being limited to the device space clip 73 * extents on the context. 74 * @param aClippedFrame The/an nsIFrame of the element that references this 75 * clipPath that is currently being processed. 76 * @param aMatrix The transform from aClippedFrame's user space to aContext's 77 * current transform. 78 * @param [in, optional] aExtraMask An extra surface that the returned 79 * surface should be masked with. 80 */ 81 already_AddRefed<SourceSurface> GetClipMask( 82 gfxContext& aReferenceContext, nsIFrame* aClippedFrame, 83 const gfxMatrix& aMatrix, SourceSurface* aExtraMask = nullptr); 84 85 /** 86 * Paint mask directly onto a given context(aMaskContext). 87 * 88 * @param aMaskContext The target of mask been painting on. 89 * @param aClippedFrame The/an nsIFrame of the element that references this 90 * clipPath that is currently being processed. 91 * @param aMatrix The transform from aClippedFrame's user space to 92 * current transform. 93 * @param [in, optional] aExtraMask An extra surface that the returned 94 * surface should be masked with. 95 */ 96 void PaintClipMask(gfxContext& aMaskContext, nsIFrame* aClippedFrame, 97 const gfxMatrix& aMatrix, SourceSurface* aExtraMask); 98 99 /** 100 * aPoint is expected to be in aClippedFrame's SVG user space. 101 */ 102 bool PointIsInsideClipPath(nsIFrame* aClippedFrame, const gfxPoint& aPoint); 103 104 // Check if this clipPath is made up of more than one geometry object. 105 // If so, the clipping API in cairo isn't enough and we need to use 106 // mask based clipping. 107 bool IsTrivial(nsIFrame** aSingleChild = nullptr); 108 109 // nsIFrame interface: 110 nsresult AttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, 111 AttrModType aModType) override; 112 113 #ifdef DEBUG 114 void Init(nsIContent* aContent, nsContainerFrame* aParent, 115 nsIFrame* aPrevInFlow) override; 116 #endif 117 118 #ifdef DEBUG_FRAME_DUMP 119 nsresult GetFrameName(nsAString& aResult) const override { 120 return MakeFrameName(u"SVGClipPath"_ns, aResult); 121 } 122 #endif 123 124 SVGBBox GetBBoxForClipPathFrame(const SVGBBox& aBBox, 125 const gfxMatrix& aMatrix, uint32_t aFlags); 126 127 /** 128 * If the clipPath element transforms its children due to 129 * clipPathUnits="objectBoundingBox" being set on it and/or due to the 130 * 'transform' attribute being set on it, this function returns the resulting 131 * transform. 132 */ 133 gfxMatrix GetClipPathTransform(nsIFrame* aClippedFrame); 134 135 private: 136 // SVGContainerFrame methods: 137 gfxMatrix GetCanvasTM() override; 138 139 already_AddRefed<DrawTarget> CreateClipMask(gfxContext& aReferenceContext, 140 gfx::IntPoint& aOffset); 141 142 void PaintFrameIntoMask(nsIFrame* aFrame, nsIFrame* aClippedFrame, 143 gfxContext& aTarget); 144 145 void PaintChildren(gfxContext& aMaskContext, nsIFrame* aClippedFrame, 146 const gfxMatrix& aMatrix); 147 148 bool IsValid(); 149 150 // Set, during a GetClipMask() call, to the transform that still needs to be 151 // concatenated to the transform of the DrawTarget that was passed to 152 // GetClipMask in order to establish the coordinate space that the clipPath 153 // establishes for its contents (i.e. including applying 'clipPathUnits' and 154 // any 'transform' attribute set on the clipPath) specifically for clipping 155 // the frame that was passed to GetClipMask at that moment in time. This is 156 // set so that if our GetCanvasTM method is called while GetClipMask is 157 // painting its children, the returned matrix will include the transforms 158 // that should be used when creating the mask for the frame passed to 159 // GetClipMask. 160 // 161 // Note: The removal of GetCanvasTM is nearly complete, so our GetCanvasTM 162 // may not even be called soon/any more. 163 gfxMatrix mMatrixForChildren; 164 165 // Flag used to indicate whether a methods that may reenter due to 166 // following a reference to another instance is currently executing. 167 bool mIsBeingProcessed; 168 }; 169 170 } // namespace mozilla 171 172 #endif // LAYOUT_SVG_SVGCLIPPATHFRAME_H_