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/. */
7 #ifndef mozilla_RestyleManager_h
8 #define mozilla_RestyleManager_h
10 #include "mozilla/AutoRestore.h"
11 #include "mozilla/Maybe.h"
12 #include "mozilla/OverflowChangedTracker.h"
13 #include "mozilla/ServoElementSnapshot.h"
14 #include "mozilla/ServoElementSnapshotTable.h"
15 #include "nsChangeHint.h"
16 #include "nsPresContext.h"
17 #include "nsPresContextInlines.h" // XXX Shouldn't be included by header though
18 #include "nsStringFwd.h"
19 #include "nsTHashSet.h"
24 class nsStyleChangeList
;
25 class nsStyleChangeList
;
37 * A stack class used to pass some common restyle state in a slightly more
38 * comfortable way than a bunch of individual arguments, and that also checks
39 * that the change hint used for optimization is correctly used in debug mode.
41 class ServoRestyleState
{
44 ServoStyleSet
& aStyleSet
, nsStyleChangeList
& aChangeList
,
45 nsTArray
<nsIFrame
*>& aPendingWrapperRestyles
,
46 nsTArray
<RefPtr
<dom::Element
>>& aPendingScrollAnchorSuppressions
)
47 : mStyleSet(aStyleSet
),
48 mChangeList(aChangeList
),
49 mPendingWrapperRestyles(aPendingWrapperRestyles
),
50 mPendingScrollAnchorSuppressions(aPendingScrollAnchorSuppressions
),
51 mPendingWrapperRestyleOffset(aPendingWrapperRestyles
.Length()),
52 mChangesHandled(nsChangeHint(0))
54 // If !mOwner, then we wouldn't have processed our wrapper restyles,
55 // because we only process those when handling an element with a frame.
56 // But that's OK, because if we started our traversal at an element with
57 // no frame (e.g. it's display:contents), that means the wrapper frames
58 // in our list actually inherit from one of its ancestors, not from it,
59 // and hence not restyling them is OK.
61 mAssertWrapperRestyleLength(false)
66 // We shouldn't assume that changes handled from our parent are handled for
67 // our children too if we're out of flow since they aren't necessarily
68 // parented in DOM order, and thus a change handled by a DOM ancestor doesn't
69 // necessarily mean that it's handled for an ancestor frame.
70 enum class CanUseHandledHints
: bool { No
= false, Yes
};
72 ServoRestyleState(const nsIFrame
& aOwner
, ServoRestyleState
& aParentState
,
73 nsChangeHint aHintForThisFrame
,
74 CanUseHandledHints aCanUseHandledHints
,
75 bool aAssertWrapperRestyleLength
= true)
76 : mStyleSet(aParentState
.mStyleSet
),
77 mChangeList(aParentState
.mChangeList
),
78 mPendingWrapperRestyles(aParentState
.mPendingWrapperRestyles
),
79 mPendingScrollAnchorSuppressions(
80 aParentState
.mPendingScrollAnchorSuppressions
),
81 mPendingWrapperRestyleOffset(
82 aParentState
.mPendingWrapperRestyles
.Length()),
83 mChangesHandled(bool(aCanUseHandledHints
)
84 ? aParentState
.mChangesHandled
| aHintForThisFrame
89 mAssertWrapperRestyleLength(aAssertWrapperRestyleLength
)
92 if (bool(aCanUseHandledHints
)) {
93 AssertOwner(aParentState
);
97 ~ServoRestyleState() {
99 !mAssertWrapperRestyleLength
||
100 mPendingWrapperRestyles
.Length() == mPendingWrapperRestyleOffset
,
101 "Someone forgot to call ProcessWrapperRestyles!");
104 nsStyleChangeList
& ChangeList() { return mChangeList
; }
105 ServoStyleSet
& StyleSet() { return mStyleSet
; }
108 void AssertOwner(const ServoRestyleState
& aParentState
) const;
109 nsChangeHint
ChangesHandledFor(const nsIFrame
*) const;
111 void AssertOwner(const ServoRestyleState
&) const {}
112 nsChangeHint
ChangesHandledFor(const nsIFrame
*) const {
113 return mChangesHandled
;
117 // Add a pending wrapper restyle. We don't have to do anything if the thing
118 // being added is already last in the list, but otherwise we do want to add
119 // it, in order for ProcessWrapperRestyles to work correctly.
120 void AddPendingWrapperRestyle(nsIFrame
* aWrapperFrame
);
122 // Process wrapper restyles for this restyle state. This should be done
123 // before it comes off the stack.
124 void ProcessWrapperRestyles(nsIFrame
* aParentFrame
);
126 // Get the table-aware parent for the given child. This will walk through
127 // outer table and cellcontent frames.
128 static nsIFrame
* TableAwareParentFor(const nsIFrame
* aChild
);
130 // When the value of the position property changes such as we stop or start
131 // being absolutely or fixed positioned, we need to suppress scroll anchoring
132 // adjustments to avoid breaking websites.
134 // We do need to process all this once we're done with all our reframes,
135 // to handle correctly the cases where we reconstruct an ancestor, like when
136 // you reframe an ib-split (see bug 1559627 for example).
138 // This doesn't handle nested reframes. We'd need to rework quite some code to
139 // do that, and so far it doesn't seem to be a problem in practice.
140 void AddPendingScrollAnchorSuppression(dom::Element
* aElement
) {
141 mPendingScrollAnchorSuppressions
.AppendElement(aElement
);
145 // Process a wrapper restyle at the given index, and restyles for any
146 // wrappers nested in it. Returns the number of entries from
147 // mPendingWrapperRestyles that we processed. The return value is always at
149 size_t ProcessMaybeNestedWrapperRestyle(nsIFrame
* aParent
, size_t aIndex
);
151 ServoStyleSet
& mStyleSet
;
152 nsStyleChangeList
& mChangeList
;
154 // A list of pending wrapper restyles. Anonymous box wrapper frames that need
155 // restyling are added to this list when their non-anonymous kids are
156 // restyled. This avoids us having to do linear searches along the frame tree
157 // for these anonymous boxes. The problem then becomes that we can have
158 // multiple kids all with the same anonymous parent, and we don't want to
159 // restyle it more than once. We use mPendingWrapperRestyles to track which
160 // anonymous wrapper boxes we've requested be restyled and which of them have
161 // already been restyled. We use a single array propagated through
162 // ServoRestyleStates by reference, because in a situation like this:
164 // <div style="display: table"><span></span></div>
166 // We have multiple wrappers to restyle (cell, row, table-row-group) and we
167 // want to add them in to the list all at once but restyle them using
168 // different ServoRestyleStates with different owners. When this situation
169 // occurs, the relevant frames will be placed in the array with ancestors
170 // before descendants.
171 nsTArray
<nsIFrame
*>& mPendingWrapperRestyles
;
173 nsTArray
<RefPtr
<dom::Element
>>& mPendingScrollAnchorSuppressions
;
175 // Since we're given a possibly-nonempty mPendingWrapperRestyles to start
176 // with, we need to keep track of where the part of it we're responsible for
178 size_t mPendingWrapperRestyleOffset
;
180 const nsChangeHint mChangesHandled
;
182 // We track the "owner" frame of this restyle state, that is, the frame that
183 // generated the last change that is stored in mChangesHandled, in order to
184 // verify that we only use mChangesHandled for actual descendants of that
185 // frame (given DOM order isn't always frame order, and that there are a few
186 // special cases for stuff like wrapper frames, ::backdrop, and so on).
188 const nsIFrame
* mOwner
{nullptr};
191 // Whether we should assert in our destructor that we've processed all of the
192 // relevant wrapper restyles.
194 const bool mAssertWrapperRestyleLength
;
198 enum class ServoPostTraversalFlags
: uint32_t;
200 class RestyleManager
{
201 friend class dom::Document
;
202 friend class ServoStyleSet
;
205 typedef ServoElementSnapshotTable SnapshotTable
;
206 typedef mozilla::dom::Element Element
;
208 // Get an integer that increments every time we process pending restyles.
209 // The value is never 0.
210 uint64_t GetRestyleGeneration() const { return mRestyleGeneration
; }
211 // Unlike GetRestyleGeneration, which means the actual restyling count,
212 // GetUndisplayedRestyleGeneration represents any possible DOM changes that
213 // can cause restyling. This is needed for getComputedStyle to work with
214 // non-styled (e.g. display: none) elements.
215 uint64_t GetUndisplayedRestyleGeneration() const {
216 return mUndisplayedRestyleGeneration
;
219 void Disconnect() { mPresContext
= nullptr; }
222 MOZ_ASSERT(!mAnimationsWithDestroyedFrame
,
223 "leaving dangling pointers from AnimationsWithDestroyedFrame");
224 MOZ_ASSERT(!mReentrantChanges
);
228 static nsCString
ChangeHintToString(nsChangeHint aHint
);
231 * DEBUG ONLY method to verify integrity of style tree versus frame tree
233 void DebugVerifyStyleTree(nsIFrame
* aFrame
);
236 void FlushOverflowChangedTracker() { mOverflowChangedTracker
.Flush(); }
238 // Should be called when a frame is going to be destroyed and
239 // WillDestroyFrameTree hasn't been called yet.
240 void NotifyDestroyingFrame(nsIFrame
* aFrame
) {
241 mOverflowChangedTracker
.RemoveFrame(aFrame
);
242 // If ProcessRestyledFrames is tracking frames which have been
243 // destroyed (to avoid re-visiting them), add this one to its set.
244 if (mDestroyedFrames
) {
245 mDestroyedFrames
->Insert(aFrame
);
249 // Note: It's the caller's responsibility to make sure to wrap a
250 // ProcessRestyledFrames call in a view update batch and a script blocker.
251 // This function does not call ProcessAttachedQueue() on the binding manager.
252 // If the caller wants that to happen synchronously, it needs to handle that
254 void ProcessRestyledFrames(nsStyleChangeList
& aChangeList
);
256 bool IsInStyleRefresh() const { return mInStyleRefresh
; }
258 // AnimationsWithDestroyedFrame is used to stop animations and transitions
259 // on elements that have no frame at the end of the restyling process.
260 // It only lives during the restyling process.
261 class MOZ_STACK_CLASS AnimationsWithDestroyedFrame final
{
263 // Construct a AnimationsWithDestroyedFrame object. The caller must
264 // ensure that aRestyleManager lives at least as long as the
265 // object. (This is generally easy since the caller is typically a
266 // method of RestyleManager.)
267 explicit AnimationsWithDestroyedFrame(RestyleManager
* aRestyleManager
);
269 // This method takes the content node for the generated content for
270 // animation/transition on ::before and ::after, rather than the
271 // content node for the real element.
272 void Put(nsIContent
* aContent
, ComputedStyle
* aComputedStyle
) {
273 MOZ_ASSERT(aContent
);
274 PseudoStyleType pseudoType
= aComputedStyle
->GetPseudoType();
275 if (pseudoType
== PseudoStyleType::NotPseudo
||
276 PseudoStyle::IsViewTransitionPseudoElement(pseudoType
)) {
277 mContents
.AppendElement(aContent
->AsElement());
278 } else if (pseudoType
== PseudoStyleType::before
) {
279 MOZ_ASSERT(aContent
->NodeInfo()->NameAtom() ==
280 nsGkAtoms::mozgeneratedcontentbefore
);
281 mBeforeContents
.AppendElement(aContent
->GetParent()->AsElement());
282 } else if (pseudoType
== PseudoStyleType::after
) {
283 MOZ_ASSERT(aContent
->NodeInfo()->NameAtom() ==
284 nsGkAtoms::mozgeneratedcontentafter
);
285 mAfterContents
.AppendElement(aContent
->GetParent()->AsElement());
286 } else if (pseudoType
== PseudoStyleType::marker
) {
287 MOZ_ASSERT(aContent
->NodeInfo()->NameAtom() ==
288 nsGkAtoms::mozgeneratedcontentmarker
);
289 mMarkerContents
.AppendElement(aContent
->GetParent()->AsElement());
293 void StopAnimationsForElementsWithoutFrames();
296 void StopAnimationsWithoutFrame(nsTArray
<RefPtr
<Element
>>& aArray
,
297 const PseudoStyleRequest
& aPseudoRequest
);
299 RestyleManager
* mRestyleManager
;
300 AutoRestore
<AnimationsWithDestroyedFrame
*> mRestorePointer
;
302 // Below four arrays might include elements that have already had their
303 // animations or transitions stopped.
305 // mBeforeContents, mAfterContents and mMarkerContents hold the real element
306 // rather than the content node for the generated content (which might
307 // change during a reframe).
308 nsTArray
<RefPtr
<Element
>> mContents
;
309 nsTArray
<RefPtr
<Element
>> mBeforeContents
;
310 nsTArray
<RefPtr
<Element
>> mAfterContents
;
311 nsTArray
<RefPtr
<Element
>> mMarkerContents
;
315 * Return the current AnimationsWithDestroyedFrame struct, or null if we're
316 * not currently in a restyling operation.
318 AnimationsWithDestroyedFrame
* GetAnimationsWithDestroyedFrame() {
319 return mAnimationsWithDestroyedFrame
;
322 void ContentInserted(nsIContent
* aChild
);
323 void ContentAppended(nsIContent
* aFirstNewContent
);
325 // Restyling for a content removal that is about to happen.
326 void ContentWillBeRemoved(nsIContent
* aOldChild
);
328 // Restyling for a ContentInserted (notification after insertion) or
329 // for some CharacterDataChanged.
330 void RestyleForInsertOrChange(nsIContent
* aChild
);
332 // Restyle for a CharacterDataChanged notification. In practice this can only
333 // affect :empty / :-moz-only-whitespace / :-moz-first-node / :-moz-last-node.
334 void CharacterDataChanged(nsIContent
*, const CharacterDataChangeInfo
&);
336 void PostRestyleEvent(dom::Element
*, RestyleHint
,
337 nsChangeHint aMinChangeHint
);
340 * Posts restyle hints for animations.
341 * This is only called for the second traversal for CSS animations during
342 * updating CSS animations in a SequentialTask.
343 * This function does neither register a refresh observer nor flag that a
344 * style flush is needed since this function is supposed to be called during
345 * restyling process and this restyle event will be processed in the second
346 * traversal of the same restyling process.
348 void PostRestyleEventForAnimations(dom::Element
*, const PseudoStyleRequest
&,
351 void NextRestyleIsForCSSRuleChanges() { mRestyleForCSSRuleChanges
= true; }
353 void RebuildAllStyleData(nsChangeHint aExtraHint
, RestyleHint
);
355 void ProcessPendingRestyles();
356 void ProcessAllPendingAttributeAndStateInvalidations();
358 void ElementStateChanged(Element
*, dom::ElementState
);
360 void CustomStatesWillChange(Element
&);
361 void CustomStateChanged(Element
&, nsAtom
* aState
);
362 void MaybeRestyleForNthOfCustomState(ServoStyleSet
&, Element
&,
366 * Posts restyle hints for siblings of an element and their descendants if the
367 * element's parent has NODE_HAS_SLOW_SELECTOR_NTH_OF and the element has a
368 * relevant state dependency.
370 void MaybeRestyleForNthOfState(ServoStyleSet
& aStyleSet
, dom::Element
* aChild
,
371 dom::ElementState aChangedBits
);
373 void AttributeWillChange(Element
* aElement
, int32_t aNameSpaceID
,
374 nsAtom
* aAttribute
, int32_t aModType
);
375 void ClassAttributeWillBeChangedBySMIL(dom::Element
* aElement
);
376 void AttributeChanged(dom::Element
* aElement
, int32_t aNameSpaceID
,
377 nsAtom
* aAttribute
, int32_t aModType
,
378 const nsAttrValue
* aOldValue
);
381 * Restyle an element's previous and/or next siblings.
383 void RestyleSiblingsForNthOf(dom::Element
* aChild
,
384 NodeSelectorFlags aParentFlags
);
387 * Posts restyle hints for siblings of an element and their descendants if the
388 * element's parent has NODE_HAS_SLOW_SELECTOR_NTH_OF and the element has a
389 * relevant attribute dependency.
391 void MaybeRestyleForNthOfAttribute(dom::Element
* aChild
, int32_t aNameSpaceID
,
393 const nsAttrValue
* aOldValue
);
395 void MaybeRestyleForRelativeSelectorAttribute(dom::Element
* aElement
,
396 int32_t aNameSpaceID
,
398 const nsAttrValue
* aOldValue
);
399 void MaybeRestyleForRelativeSelectorState(ServoStyleSet
& aStyleSet
,
400 dom::Element
* aElement
,
401 dom::ElementState aChangedBits
);
403 // This is only used to reparent things when moving them in/out of the
405 void ReparentComputedStyleForFirstLine(nsIFrame
*);
408 * Performs a Servo animation-only traversal to compute style for all nodes
409 * with the animation-only dirty bit in the document.
411 * This processes just the traversal for animation-only restyles and skips the
412 * normal traversal for other restyles unrelated to animations.
413 * This is used to bring throttled animations up-to-date such as when we need
414 * to get correct position for transform animations that are throttled because
415 * they are running on the compositor.
417 * This will traverse all of the document's style roots (that is, its document
418 * element, and the roots of the document-level native anonymous content).
420 void UpdateOnlyAnimationStyles();
422 // Get a counter that increments on every style change, that we use to
423 // track whether off-main-thread animations are up-to-date.
424 uint64_t GetAnimationGeneration() const { return mAnimationGeneration
; }
426 // Typically only style frames have animations associated with them so this
427 // will likely return zero for anything that is not a style frame.
428 static uint64_t GetAnimationGenerationForFrame(nsIFrame
* aStyleFrame
);
430 // Update the animation generation count to mark that animation state
433 // This is normally performed automatically by ProcessPendingRestyles
434 // but it is also called when we have out-of-band changes to animations
435 // such as changes made through the Web Animations API.
436 void IncrementAnimationGeneration();
438 // Apply change hints for animations on the compositor.
440 // There are some cases where we forcibly apply change hints for animations
441 // even if there is no change hint produced in order to synchronize with
442 // animations running on the compositor.
446 // a) Pausing animations via the Web Animations API
447 // b) When the style before sending the animation to the compositor exactly
448 // the same as the current style
449 static void AddLayerChangesForAnimation(
450 nsIFrame
* aStyleFrame
, nsIFrame
* aPrimaryFrame
, Element
* aElement
,
451 nsChangeHint aHintForThisFrame
, nsStyleChangeList
& aChangeListToProcess
);
454 * Whether to clear all the style data (including the element itself), or just
455 * the descendants' data.
457 enum class IncludeRoot
{
463 * Clears the ServoElementData and HasDirtyDescendants from all elements
464 * in the subtree rooted at aElement.
466 static void ClearServoDataFromSubtree(Element
*,
467 IncludeRoot
= IncludeRoot::Yes
);
470 * Clears HasDirtyDescendants and RestyleData from all elements in the
471 * subtree rooted at aElement.
473 static void ClearRestyleStateFromSubtree(Element
* aElement
);
475 explicit RestyleManager(nsPresContext
* aPresContext
);
479 * Reparent the descendants of aFrame. This is used by ReparentComputedStyle
480 * and shouldn't be called by anyone else. aProviderChild, if non-null, is a
481 * child that was the style parent for aFrame and hence shouldn't be
484 void ReparentFrameDescendants(nsIFrame
* aFrame
, nsIFrame
* aProviderChild
,
485 ServoStyleSet
& aStyleSet
);
488 * Performs post-Servo-traversal processing on this element and its
491 * Returns whether any style did actually change. There may be cases where we
492 * didn't need to change any style after all, for example, when a content
493 * attribute changes that happens not to have any effect on the style of that
494 * element or any descendant or sibling.
496 bool ProcessPostTraversal(Element
* aElement
, ServoRestyleState
& aRestyleState
,
497 ServoPostTraversalFlags aFlags
);
499 struct TextPostTraversalState
;
500 bool ProcessPostTraversalForText(nsIContent
* aTextNode
,
501 TextPostTraversalState
& aState
,
502 ServoRestyleState
& aRestyleState
,
503 ServoPostTraversalFlags aFlags
);
505 ServoStyleSet
* StyleSet() const { return PresContext()->StyleSet(); }
507 void RestyleWholeContainer(nsINode
* aContainer
, NodeSelectorFlags
);
508 void RestylePreviousSiblings(nsIContent
* aStartingSibling
);
509 void RestyleSiblingsStartingWith(nsIContent
* aStartingSibling
);
511 void RestyleForEmptyChange(Element
* aContainer
);
512 void MaybeRestyleForEdgeChildChange(nsINode
* aContainer
,
513 nsIContent
* aChangedChild
);
515 bool IsDisconnected() const { return !mPresContext
; }
517 void IncrementRestyleGeneration() {
518 if (++mRestyleGeneration
== 0) {
519 // Keep mRestyleGeneration from being 0, since that's what
520 // nsPresContext::GetRestyleGeneration returns when it no
521 // longer has a RestyleManager.
522 ++mRestyleGeneration
;
524 IncrementUndisplayedRestyleGeneration();
527 void IncrementUndisplayedRestyleGeneration() {
528 if (++mUndisplayedRestyleGeneration
== 0) {
529 // Ensure mUndisplayedRestyleGeneration > 0, for the same reason as
530 // IncrementRestyleGeneration.
531 ++mUndisplayedRestyleGeneration
;
535 nsPresContext
* PresContext() const {
536 MOZ_ASSERT(mPresContext
);
541 nsPresContext
* mPresContext
; // weak, can be null after Disconnect().
542 uint64_t mRestyleGeneration
;
543 uint64_t mUndisplayedRestyleGeneration
;
545 // Used to keep track of frames that have been destroyed during
546 // ProcessRestyledFrames, so we don't try to touch them again even if
547 // they're referenced again later in the changelist.
548 mozilla::UniquePtr
<nsTHashSet
<const nsIFrame
*>> mDestroyedFrames
;
550 // Containers we've already fully restyled / invalidated.
551 nsTHashSet
<RefPtr
<nsINode
>> mRestyledAsWholeContainer
;
554 // True if we're in the middle of a nsRefreshDriver refresh
555 bool mInStyleRefresh
;
557 // The total number of animation flushes by this frame constructor.
558 // Used to keep the layer and animation manager in sync.
559 uint64_t mAnimationGeneration
;
561 OverflowChangedTracker mOverflowChangedTracker
;
563 AnimationsWithDestroyedFrame
* mAnimationsWithDestroyedFrame
= nullptr;
565 const SnapshotTable
& Snapshots() const { return mSnapshots
; }
566 void ClearSnapshots();
567 ServoElementSnapshot
& SnapshotFor(Element
&);
568 void TakeSnapshotForAttributeChange(Element
&, int32_t aNameSpaceID
,
571 void DoProcessPendingRestyles(ServoTraversalFlags aFlags
);
573 // Function to do the actual (recursive) work of
574 // ReparentComputedStyleForFirstLine, once we have asserted the invariants
575 // that only hold on the initial call.
576 void DoReparentComputedStyleForFirstLine(nsIFrame
*, ServoStyleSet
&);
578 // We use a separate data structure from nsStyleChangeList because we need a
579 // frame to create nsStyleChangeList entries, and the primary frame may not be
581 struct ReentrantChange
{
582 nsCOMPtr
<nsIContent
> mContent
;
585 typedef AutoTArray
<ReentrantChange
, 10> ReentrantChangeList
;
587 // Only non-null while processing change hints. See the comment in
588 // ProcessPendingRestyles.
589 ReentrantChangeList
* mReentrantChanges
= nullptr;
591 // We use this flag to track if the current restyle contains any non-animation
592 // update, which triggers a normal restyle, and so there might be any new
593 // transition created later. Therefore, if this flag is true, we need to
594 // increase mAnimationGeneration before creating new transitions, so their
595 // creation sequence will be correct.
596 bool mHaveNonAnimationRestyles
= false;
598 // Set to true when posting restyle events triggered by CSS rule changes.
599 // This flag is cleared once ProcessPendingRestyles has completed.
600 // When we process a traversal all descendants elements of the document
601 // triggered by CSS rule changes, we will need to update all elements with
602 // CSS animations. We propagate TraversalRestyleBehavior::ForCSSRuleChanges
603 // to traversal function if this flag is set.
604 bool mRestyleForCSSRuleChanges
= false;
606 // A hashtable with the elements that have changed state or attributes, in
607 // order to calculate restyle hints during the traversal.
608 SnapshotTable mSnapshots
;
611 } // namespace mozilla