1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/layout/svg/nsSVGSwitchFrame.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,253 @@ 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 +// Keep in (case-insensitive) order: 1.10 +#include "gfxRect.h" 1.11 +#include "nsSVGEffects.h" 1.12 +#include "nsSVGGFrame.h" 1.13 +#include "mozilla/dom/SVGSwitchElement.h" 1.14 +#include "nsSVGUtils.h" 1.15 + 1.16 +class nsRenderingContext; 1.17 + 1.18 +using namespace mozilla::gfx; 1.19 + 1.20 +typedef nsSVGGFrame nsSVGSwitchFrameBase; 1.21 + 1.22 +class nsSVGSwitchFrame : public nsSVGSwitchFrameBase 1.23 +{ 1.24 + friend nsIFrame* 1.25 + NS_NewSVGSwitchFrame(nsIPresShell* aPresShell, nsStyleContext* aContext); 1.26 +protected: 1.27 + nsSVGSwitchFrame(nsStyleContext* aContext) : 1.28 + nsSVGSwitchFrameBase(aContext) {} 1.29 + 1.30 +public: 1.31 + NS_DECL_FRAMEARENA_HELPERS 1.32 + 1.33 +#ifdef DEBUG 1.34 + virtual void Init(nsIContent* aContent, 1.35 + nsIFrame* aParent, 1.36 + nsIFrame* aPrevInFlow) MOZ_OVERRIDE; 1.37 +#endif 1.38 + 1.39 + /** 1.40 + * Get the "type" of the frame 1.41 + * 1.42 + * @see nsGkAtoms::svgSwitchFrame 1.43 + */ 1.44 + virtual nsIAtom* GetType() const MOZ_OVERRIDE; 1.45 + 1.46 +#ifdef DEBUG_FRAME_DUMP 1.47 + virtual nsresult GetFrameName(nsAString& aResult) const MOZ_OVERRIDE 1.48 + { 1.49 + return MakeFrameName(NS_LITERAL_STRING("SVGSwitch"), aResult); 1.50 + } 1.51 +#endif 1.52 + 1.53 + virtual void BuildDisplayList(nsDisplayListBuilder* aBuilder, 1.54 + const nsRect& aDirtyRect, 1.55 + const nsDisplayListSet& aLists) MOZ_OVERRIDE; 1.56 + 1.57 + // nsISVGChildFrame interface: 1.58 + virtual nsresult PaintSVG(nsRenderingContext* aContext, 1.59 + const nsIntRect *aDirtyRect, 1.60 + nsIFrame* aTransformRoot) MOZ_OVERRIDE; 1.61 + nsIFrame* GetFrameForPoint(const nsPoint &aPoint) MOZ_OVERRIDE; 1.62 + nsRect GetCoveredRegion() MOZ_OVERRIDE; 1.63 + virtual void ReflowSVG() MOZ_OVERRIDE; 1.64 + virtual SVGBBox GetBBoxContribution(const Matrix &aToBBoxUserspace, 1.65 + uint32_t aFlags) MOZ_OVERRIDE; 1.66 + 1.67 +private: 1.68 + nsIFrame *GetActiveChildFrame(); 1.69 +}; 1.70 + 1.71 +//---------------------------------------------------------------------- 1.72 +// Implementation 1.73 + 1.74 +nsIFrame* 1.75 +NS_NewSVGSwitchFrame(nsIPresShell* aPresShell, nsStyleContext* aContext) 1.76 +{ 1.77 + return new (aPresShell) nsSVGSwitchFrame(aContext); 1.78 +} 1.79 + 1.80 +NS_IMPL_FRAMEARENA_HELPERS(nsSVGSwitchFrame) 1.81 + 1.82 +#ifdef DEBUG 1.83 +void 1.84 +nsSVGSwitchFrame::Init(nsIContent* aContent, 1.85 + nsIFrame* aParent, 1.86 + nsIFrame* aPrevInFlow) 1.87 +{ 1.88 + NS_ASSERTION(aContent->IsSVG(nsGkAtoms::svgSwitch), 1.89 + "Content is not an SVG switch"); 1.90 + 1.91 + nsSVGSwitchFrameBase::Init(aContent, aParent, aPrevInFlow); 1.92 +} 1.93 +#endif /* DEBUG */ 1.94 + 1.95 +nsIAtom * 1.96 +nsSVGSwitchFrame::GetType() const 1.97 +{ 1.98 + return nsGkAtoms::svgSwitchFrame; 1.99 +} 1.100 + 1.101 +void 1.102 +nsSVGSwitchFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder, 1.103 + const nsRect& aDirtyRect, 1.104 + const nsDisplayListSet& aLists) 1.105 +{ 1.106 + nsIFrame* kid = GetActiveChildFrame(); 1.107 + if (kid) { 1.108 + BuildDisplayListForChild(aBuilder, kid, aDirtyRect, aLists); 1.109 + } 1.110 +} 1.111 + 1.112 +nsresult 1.113 +nsSVGSwitchFrame::PaintSVG(nsRenderingContext* aContext, 1.114 + const nsIntRect *aDirtyRect, 1.115 + nsIFrame* aTransformRoot) 1.116 +{ 1.117 + NS_ASSERTION(!NS_SVGDisplayListPaintingEnabled() || 1.118 + (mState & NS_FRAME_IS_NONDISPLAY), 1.119 + "If display lists are enabled, only painting of non-display " 1.120 + "SVG should take this code path"); 1.121 + 1.122 + if (StyleDisplay()->mOpacity == 0.0) 1.123 + return NS_OK; 1.124 + 1.125 + nsIFrame *kid = GetActiveChildFrame(); 1.126 + if (kid) { 1.127 + nsSVGUtils::PaintFrameWithEffects(aContext, aDirtyRect, kid, aTransformRoot); 1.128 + } 1.129 + return NS_OK; 1.130 +} 1.131 + 1.132 + 1.133 +nsIFrame* 1.134 +nsSVGSwitchFrame::GetFrameForPoint(const nsPoint &aPoint) 1.135 +{ 1.136 + NS_ASSERTION(!NS_SVGDisplayListHitTestingEnabled() || 1.137 + (mState & NS_FRAME_IS_NONDISPLAY), 1.138 + "If display lists are enabled, only hit-testing of non-display " 1.139 + "SVG should take this code path"); 1.140 + 1.141 + nsIFrame *kid = GetActiveChildFrame(); 1.142 + nsISVGChildFrame* svgFrame = do_QueryFrame(kid); 1.143 + if (svgFrame) { 1.144 + return svgFrame->GetFrameForPoint(aPoint); 1.145 + } 1.146 + 1.147 + return nullptr; 1.148 +} 1.149 + 1.150 +nsRect 1.151 +nsSVGSwitchFrame::GetCoveredRegion() 1.152 +{ 1.153 + nsRect rect; 1.154 + 1.155 + nsIFrame *kid = GetActiveChildFrame(); 1.156 + nsISVGChildFrame* child = do_QueryFrame(kid); 1.157 + if (child) { 1.158 + rect = child->GetCoveredRegion(); 1.159 + } 1.160 + return rect; 1.161 +} 1.162 + 1.163 +void 1.164 +nsSVGSwitchFrame::ReflowSVG() 1.165 +{ 1.166 + NS_ASSERTION(nsSVGUtils::OuterSVGIsCallingReflowSVG(this), 1.167 + "This call is probably a wasteful mistake"); 1.168 + 1.169 + NS_ABORT_IF_FALSE(!(GetStateBits() & NS_FRAME_IS_NONDISPLAY), 1.170 + "ReflowSVG mechanism not designed for this"); 1.171 + 1.172 + if (!nsSVGUtils::NeedsReflowSVG(this)) { 1.173 + return; 1.174 + } 1.175 + 1.176 + // If the NS_FRAME_FIRST_REFLOW bit has been removed from our parent frame, 1.177 + // then our outer-<svg> has previously had its initial reflow. In that case 1.178 + // we need to make sure that that bit has been removed from ourself _before_ 1.179 + // recursing over our children to ensure that they know too. Otherwise, we 1.180 + // need to remove it _after_ recursing over our children so that they know 1.181 + // the initial reflow is currently underway. 1.182 + 1.183 + bool isFirstReflow = (mState & NS_FRAME_FIRST_REFLOW); 1.184 + 1.185 + bool outerSVGHasHadFirstReflow = 1.186 + (GetParent()->GetStateBits() & NS_FRAME_FIRST_REFLOW) == 0; 1.187 + 1.188 + if (outerSVGHasHadFirstReflow) { 1.189 + mState &= ~NS_FRAME_FIRST_REFLOW; // tell our children 1.190 + } 1.191 + 1.192 + nsOverflowAreas overflowRects; 1.193 + 1.194 + nsIFrame *child = GetActiveChildFrame(); 1.195 + nsISVGChildFrame* svgChild = do_QueryFrame(child); 1.196 + if (svgChild) { 1.197 + NS_ABORT_IF_FALSE(!(child->GetStateBits() & NS_FRAME_IS_NONDISPLAY), 1.198 + "Check for this explicitly in the |if|, then"); 1.199 + svgChild->ReflowSVG(); 1.200 + 1.201 + // We build up our child frame overflows here instead of using 1.202 + // nsLayoutUtils::UnionChildOverflow since SVG frame's all use the same 1.203 + // frame list, and we're iterating over that list now anyway. 1.204 + ConsiderChildOverflow(overflowRects, child); 1.205 + } 1.206 + 1.207 + if (isFirstReflow) { 1.208 + // Make sure we have our filter property (if any) before calling 1.209 + // FinishAndStoreOverflow (subsequent filter changes are handled off 1.210 + // nsChangeHint_UpdateEffects): 1.211 + nsSVGEffects::UpdateEffects(this); 1.212 + } 1.213 + 1.214 + FinishAndStoreOverflow(overflowRects, mRect.Size()); 1.215 + 1.216 + // Remove state bits after FinishAndStoreOverflow so that it doesn't 1.217 + // invalidate on first reflow: 1.218 + mState &= ~(NS_FRAME_FIRST_REFLOW | NS_FRAME_IS_DIRTY | 1.219 + NS_FRAME_HAS_DIRTY_CHILDREN); 1.220 +} 1.221 + 1.222 +SVGBBox 1.223 +nsSVGSwitchFrame::GetBBoxContribution(const Matrix &aToBBoxUserspace, 1.224 + uint32_t aFlags) 1.225 +{ 1.226 + nsIFrame* kid = GetActiveChildFrame(); 1.227 + nsISVGChildFrame* svgKid = do_QueryFrame(kid); 1.228 + if (svgKid) { 1.229 + nsIContent *content = kid->GetContent(); 1.230 + gfxMatrix transform = ThebesMatrix(aToBBoxUserspace); 1.231 + if (content->IsSVG()) { 1.232 + transform = static_cast<nsSVGElement*>(content)-> 1.233 + PrependLocalTransformsTo(transform); 1.234 + } 1.235 + return svgKid->GetBBoxContribution(ToMatrix(transform), aFlags); 1.236 + } 1.237 + return SVGBBox(); 1.238 +} 1.239 + 1.240 +nsIFrame * 1.241 +nsSVGSwitchFrame::GetActiveChildFrame() 1.242 +{ 1.243 + nsIContent *activeChild = 1.244 + static_cast<mozilla::dom::SVGSwitchElement*>(mContent)->GetActiveChild(); 1.245 + 1.246 + if (activeChild) { 1.247 + for (nsIFrame* kid = mFrames.FirstChild(); kid; 1.248 + kid = kid->GetNextSibling()) { 1.249 + 1.250 + if (activeChild == kid->GetContent()) { 1.251 + return kid; 1.252 + } 1.253 + } 1.254 + } 1.255 + return nullptr; 1.256 +}