nsSVGInnerSVGFrame.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. // Main header first:
  6. #include "nsSVGInnerSVGFrame.h"
  7. // Keep others in (case-insensitive) order:
  8. #include "gfx2DGlue.h"
  9. #include "gfxContext.h"
  10. #include "nsIFrame.h"
  11. #include "nsISVGChildFrame.h"
  12. #include "nsSVGContainerFrame.h"
  13. #include "nsSVGIntegrationUtils.h"
  14. #include "mozilla/dom/SVGSVGElement.h"
  15. using namespace mozilla;
  16. using namespace mozilla::dom;
  17. using namespace mozilla::gfx;
  18. using namespace mozilla::image;
  19. nsIFrame*
  20. NS_NewSVGInnerSVGFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
  21. {
  22. return new (aPresShell) nsSVGInnerSVGFrame(aContext);
  23. }
  24. NS_IMPL_FRAMEARENA_HELPERS(nsSVGInnerSVGFrame)
  25. //----------------------------------------------------------------------
  26. // nsIFrame methods
  27. NS_QUERYFRAME_HEAD(nsSVGInnerSVGFrame)
  28. NS_QUERYFRAME_ENTRY(nsSVGInnerSVGFrame)
  29. NS_QUERYFRAME_ENTRY(nsISVGSVGFrame)
  30. NS_QUERYFRAME_TAIL_INHERITING(nsSVGDisplayContainerFrame)
  31. #ifdef DEBUG
  32. void
  33. nsSVGInnerSVGFrame::Init(nsIContent* aContent,
  34. nsContainerFrame* aParent,
  35. nsIFrame* aPrevInFlow)
  36. {
  37. NS_ASSERTION(aContent->IsSVGElement(nsGkAtoms::svg),
  38. "Content is not an SVG 'svg' element!");
  39. nsSVGDisplayContainerFrame::Init(aContent, aParent, aPrevInFlow);
  40. }
  41. #endif /* DEBUG */
  42. nsIAtom *
  43. nsSVGInnerSVGFrame::GetType() const
  44. {
  45. return nsGkAtoms::svgInnerSVGFrame;
  46. }
  47. //----------------------------------------------------------------------
  48. // nsISVGChildFrame methods
  49. DrawResult
  50. nsSVGInnerSVGFrame::PaintSVG(gfxContext& aContext,
  51. const gfxMatrix& aTransform,
  52. const nsIntRect *aDirtyRect)
  53. {
  54. NS_ASSERTION(!NS_SVGDisplayListPaintingEnabled() ||
  55. (mState & NS_FRAME_IS_NONDISPLAY),
  56. "If display lists are enabled, only painting of non-display "
  57. "SVG should take this code path");
  58. gfxContextAutoSaveRestore autoSR;
  59. if (StyleDisplay()->IsScrollableOverflow()) {
  60. float x, y, width, height;
  61. static_cast<SVGSVGElement*>(mContent)->
  62. GetAnimatedLengthValues(&x, &y, &width, &height, nullptr);
  63. if (width <= 0 || height <= 0) {
  64. return DrawResult::SUCCESS;
  65. }
  66. autoSR.SetContext(&aContext);
  67. gfxRect clipRect =
  68. nsSVGUtils::GetClipRectForFrame(this, x, y, width, height);
  69. nsSVGUtils::SetClipRect(&aContext, aTransform, clipRect);
  70. }
  71. return nsSVGDisplayContainerFrame::PaintSVG(aContext, aTransform, aDirtyRect);
  72. }
  73. nsRect
  74. nsSVGInnerSVGFrame::GetCoveredRegion()
  75. {
  76. float x, y, w, h;
  77. static_cast<SVGSVGElement*>(mContent)->
  78. GetAnimatedLengthValues(&x, &y, &w, &h, nullptr);
  79. if (w < 0.0f) w = 0.0f;
  80. if (h < 0.0f) h = 0.0f;
  81. // GetCanvasTM includes the x,y translation
  82. nsRect bounds = nsSVGUtils::ToCanvasBounds(gfxRect(0.0, 0.0, w, h),
  83. GetCanvasTM(),
  84. PresContext());
  85. if (!StyleDisplay()->IsScrollableOverflow()) {
  86. bounds.UnionRect(bounds, nsSVGUtils::GetCoveredRegion(mFrames));
  87. }
  88. return bounds;
  89. }
  90. void
  91. nsSVGInnerSVGFrame::ReflowSVG()
  92. {
  93. // mRect must be set before FinishAndStoreOverflow is called in order
  94. // for our overflow areas to be clipped correctly.
  95. float x, y, width, height;
  96. static_cast<SVGSVGElement*>(mContent)->
  97. GetAnimatedLengthValues(&x, &y, &width, &height, nullptr);
  98. mRect = nsLayoutUtils::RoundGfxRectToAppRect(
  99. gfxRect(x, y, width, height),
  100. PresContext()->AppUnitsPerCSSPixel());
  101. // If we have a filter, we need to invalidate ourselves because filter
  102. // output can change even if none of our descendants need repainting.
  103. if (StyleEffects()->HasFilters()) {
  104. InvalidateFrame();
  105. }
  106. nsSVGDisplayContainerFrame::ReflowSVG();
  107. }
  108. void
  109. nsSVGInnerSVGFrame::NotifySVGChanged(uint32_t aFlags)
  110. {
  111. MOZ_ASSERT(aFlags & (TRANSFORM_CHANGED | COORD_CONTEXT_CHANGED),
  112. "Invalidation logic may need adjusting");
  113. if (aFlags & COORD_CONTEXT_CHANGED) {
  114. SVGSVGElement *svg = static_cast<SVGSVGElement*>(mContent);
  115. bool xOrYIsPercentage =
  116. svg->mLengthAttributes[SVGSVGElement::ATTR_X].IsPercentage() ||
  117. svg->mLengthAttributes[SVGSVGElement::ATTR_Y].IsPercentage();
  118. bool widthOrHeightIsPercentage =
  119. svg->mLengthAttributes[SVGSVGElement::ATTR_WIDTH].IsPercentage() ||
  120. svg->mLengthAttributes[SVGSVGElement::ATTR_HEIGHT].IsPercentage();
  121. if (xOrYIsPercentage || widthOrHeightIsPercentage) {
  122. // Ancestor changes can't affect how we render from the perspective of
  123. // any rendering observers that we may have, so we don't need to
  124. // invalidate them. We also don't need to invalidate ourself, since our
  125. // changed ancestor will have invalidated its entire area, which includes
  126. // our area.
  127. // For perf reasons we call this before calling NotifySVGChanged() below.
  128. nsSVGUtils::ScheduleReflowSVG(this);
  129. }
  130. // Coordinate context changes affect mCanvasTM if we have a
  131. // percentage 'x' or 'y', or if we have a percentage 'width' or 'height' AND
  132. // a 'viewBox'.
  133. if (!(aFlags & TRANSFORM_CHANGED) &&
  134. (xOrYIsPercentage ||
  135. (widthOrHeightIsPercentage && svg->HasViewBoxRect()))) {
  136. aFlags |= TRANSFORM_CHANGED;
  137. }
  138. if (svg->HasViewBoxRect() || !widthOrHeightIsPercentage) {
  139. // Remove COORD_CONTEXT_CHANGED, since we establish the coordinate
  140. // context for our descendants and this notification won't change its
  141. // dimensions:
  142. aFlags &= ~COORD_CONTEXT_CHANGED;
  143. if (!aFlags) {
  144. return; // No notification flags left
  145. }
  146. }
  147. }
  148. if (aFlags & TRANSFORM_CHANGED) {
  149. // make sure our cached transform matrix gets (lazily) updated
  150. mCanvasTM = nullptr;
  151. }
  152. nsSVGDisplayContainerFrame::NotifySVGChanged(aFlags);
  153. }
  154. nsresult
  155. nsSVGInnerSVGFrame::AttributeChanged(int32_t aNameSpaceID,
  156. nsIAtom* aAttribute,
  157. int32_t aModType)
  158. {
  159. if (aNameSpaceID == kNameSpaceID_None &&
  160. !(GetStateBits() & NS_FRAME_IS_NONDISPLAY)) {
  161. SVGSVGElement* content = static_cast<SVGSVGElement*>(mContent);
  162. if (aAttribute == nsGkAtoms::width ||
  163. aAttribute == nsGkAtoms::height) {
  164. nsLayoutUtils::PostRestyleEvent(
  165. mContent->AsElement(), nsRestyleHint(0),
  166. nsChangeHint_InvalidateRenderingObservers);
  167. nsSVGUtils::ScheduleReflowSVG(this);
  168. if (content->HasViewBoxOrSyntheticViewBox()) {
  169. // make sure our cached transform matrix gets (lazily) updated
  170. mCanvasTM = nullptr;
  171. content->ChildrenOnlyTransformChanged();
  172. nsSVGUtils::NotifyChildrenOfSVGChange(this, TRANSFORM_CHANGED);
  173. } else {
  174. uint32_t flags = COORD_CONTEXT_CHANGED;
  175. if (mCanvasTM && mCanvasTM->IsSingular()) {
  176. mCanvasTM = nullptr;
  177. flags |= TRANSFORM_CHANGED;
  178. }
  179. nsSVGUtils::NotifyChildrenOfSVGChange(this, flags);
  180. }
  181. } else if (aAttribute == nsGkAtoms::transform ||
  182. aAttribute == nsGkAtoms::preserveAspectRatio ||
  183. aAttribute == nsGkAtoms::viewBox ||
  184. aAttribute == nsGkAtoms::x ||
  185. aAttribute == nsGkAtoms::y) {
  186. // make sure our cached transform matrix gets (lazily) updated
  187. mCanvasTM = nullptr;
  188. nsSVGUtils::NotifyChildrenOfSVGChange(
  189. this, aAttribute == nsGkAtoms::viewBox ?
  190. TRANSFORM_CHANGED | COORD_CONTEXT_CHANGED : TRANSFORM_CHANGED);
  191. // We don't invalidate for transform changes (the layers code does that).
  192. // Also note that SVGTransformableElement::GetAttributeChangeHint will
  193. // return nsChangeHint_UpdateOverflow for "transform" attribute changes
  194. // and cause DoApplyRenderingChangeToTree to make the SchedulePaint call.
  195. if (aAttribute == nsGkAtoms::x || aAttribute == nsGkAtoms::y) {
  196. nsLayoutUtils::PostRestyleEvent(
  197. mContent->AsElement(), nsRestyleHint(0),
  198. nsChangeHint_InvalidateRenderingObservers);
  199. nsSVGUtils::ScheduleReflowSVG(this);
  200. } else if (aAttribute == nsGkAtoms::viewBox ||
  201. (aAttribute == nsGkAtoms::preserveAspectRatio &&
  202. content->HasViewBoxOrSyntheticViewBox())) {
  203. content->ChildrenOnlyTransformChanged();
  204. // SchedulePaint sets a global state flag so we only need to call it once
  205. // (on ourself is fine), not once on each child (despite bug 828240).
  206. SchedulePaint();
  207. }
  208. }
  209. }
  210. return NS_OK;
  211. }
  212. nsIFrame*
  213. nsSVGInnerSVGFrame::GetFrameForPoint(const gfxPoint& aPoint)
  214. {
  215. NS_ASSERTION(!NS_SVGDisplayListHitTestingEnabled() ||
  216. (mState & NS_FRAME_IS_NONDISPLAY),
  217. "If display lists are enabled, only hit-testing of non-display "
  218. "SVG should take this code path");
  219. if (StyleDisplay()->IsScrollableOverflow()) {
  220. Rect clip;
  221. static_cast<nsSVGElement*>(mContent)->
  222. GetAnimatedLengthValues(&clip.x, &clip.y,
  223. &clip.width, &clip.height, nullptr);
  224. if (!clip.Contains(ToPoint(aPoint))) {
  225. return nullptr;
  226. }
  227. }
  228. return nsSVGDisplayContainerFrame::GetFrameForPoint(aPoint);
  229. }
  230. //----------------------------------------------------------------------
  231. // nsISVGSVGFrame methods:
  232. void
  233. nsSVGInnerSVGFrame::NotifyViewportOrTransformChanged(uint32_t aFlags)
  234. {
  235. // The dimensions of inner-<svg> frames are purely defined by their "width"
  236. // and "height" attributes, and transform changes can only occur as a result
  237. // of changes to their "width", "height", "viewBox" or "preserveAspectRatio"
  238. // attributes. Changes to all of these attributes are handled in
  239. // AttributeChanged(), so we should never be called.
  240. NS_ERROR("Not called for nsSVGInnerSVGFrame");
  241. }
  242. //----------------------------------------------------------------------
  243. // nsSVGContainerFrame methods:
  244. gfxMatrix
  245. nsSVGInnerSVGFrame::GetCanvasTM()
  246. {
  247. if (!mCanvasTM) {
  248. NS_ASSERTION(GetParent(), "null parent");
  249. nsSVGContainerFrame *parent = static_cast<nsSVGContainerFrame*>(GetParent());
  250. SVGSVGElement *content = static_cast<SVGSVGElement*>(mContent);
  251. gfxMatrix tm = content->PrependLocalTransformsTo(parent->GetCanvasTM());
  252. mCanvasTM = new gfxMatrix(tm);
  253. }
  254. return *mCanvasTM;
  255. }
  256. bool
  257. nsSVGInnerSVGFrame::HasChildrenOnlyTransform(gfx::Matrix *aTransform) const
  258. {
  259. SVGSVGElement *content = static_cast<SVGSVGElement*>(mContent);
  260. if (content->HasViewBoxOrSyntheticViewBox()) {
  261. // XXX Maybe return false if the transform is the identity transform?
  262. if (aTransform) {
  263. *aTransform = content->GetViewBoxTransform();
  264. }
  265. return true;
  266. }
  267. return false;
  268. }