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
;
36 * A stack class used to pass some common restyle state in a slightly more
37 * comfortable way than a bunch of individual arguments, and that also checks
38 * that the change hint used for optimization is correctly used in debug mode.
40 class ServoRestyleState
{
43 ServoStyleSet
& aStyleSet
, nsStyleChangeList
& aChangeList
,
44 nsTArray
<nsIFrame
*>& aPendingWrapperRestyles
,
45 nsTArray
<RefPtr
<dom::Element
>>& aPendingScrollAnchorSuppressions
)
46 : mStyleSet(aStyleSet
),
47 mChangeList(aChangeList
),
48 mPendingWrapperRestyles(aPendingWrapperRestyles
),
49 mPendingScrollAnchorSuppressions(aPendingScrollAnchorSuppressions
),
50 mPendingWrapperRestyleOffset(aPendingWrapperRestyles
.Length()),
51 mChangesHandled(nsChangeHint(0))
53 // If !mOwner, then we wouldn't have processed our wrapper restyles,
54 // because we only process those when handling an element with a frame.
55 // But that's OK, because if we started our traversal at an element with
56 // no frame (e.g. it's display:contents), that means the wrapper frames
57 // in our list actually inherit from one of its ancestors, not from it,
58 // and hence not restyling them is OK.
60 mAssertWrapperRestyleLength(false)
65 // We shouldn't assume that changes handled from our parent are handled for
66 // our children too if we're out of flow since they aren't necessarily
67 // parented in DOM order, and thus a change handled by a DOM ancestor doesn't
68 // necessarily mean that it's handled for an ancestor frame.
74 ServoRestyleState(const nsIFrame
& aOwner
, ServoRestyleState
& aParentState
,
75 nsChangeHint aHintForThisFrame
, Type aType
,
76 bool aAssertWrapperRestyleLength
= true)
77 : mStyleSet(aParentState
.mStyleSet
),
78 mChangeList(aParentState
.mChangeList
),
79 mPendingWrapperRestyles(aParentState
.mPendingWrapperRestyles
),
80 mPendingScrollAnchorSuppressions(
81 aParentState
.mPendingScrollAnchorSuppressions
),
82 mPendingWrapperRestyleOffset(
83 aParentState
.mPendingWrapperRestyles
.Length()),
84 mChangesHandled(aType
== Type::InFlow
85 ? aParentState
.mChangesHandled
| aHintForThisFrame
90 mAssertWrapperRestyleLength(aAssertWrapperRestyleLength
)
93 if (aType
== Type::InFlow
) {
94 AssertOwner(aParentState
);
98 ~ServoRestyleState() {
100 !mAssertWrapperRestyleLength
||
101 mPendingWrapperRestyles
.Length() == mPendingWrapperRestyleOffset
,
102 "Someone forgot to call ProcessWrapperRestyles!");
105 nsStyleChangeList
& ChangeList() { return mChangeList
; }
106 ServoStyleSet
& StyleSet() { return mStyleSet
; }
109 void AssertOwner(const ServoRestyleState
& aParentState
) const;
110 nsChangeHint
ChangesHandledFor(const nsIFrame
*) const;
112 void AssertOwner(const ServoRestyleState
&) const {}
113 nsChangeHint
ChangesHandledFor(const nsIFrame
*) const {
114 return mChangesHandled
;
118 // Add a pending wrapper restyle. We don't have to do anything if the thing
119 // being added is already last in the list, but otherwise we do want to add
120 // it, in order for ProcessWrapperRestyles to work correctly.
121 void AddPendingWrapperRestyle(nsIFrame
* aWrapperFrame
);
123 // Process wrapper restyles for this restyle state. This should be done
124 // before it comes off the stack.
125 void ProcessWrapperRestyles(nsIFrame
* aParentFrame
);
127 // Get the table-aware parent for the given child. This will walk through
128 // outer table and cellcontent frames.
129 static nsIFrame
* TableAwareParentFor(const nsIFrame
* aChild
);
131 // When the value of the position property changes such as we stop or start
132 // being absolutely or fixed positioned, we need to suppress scroll anchoring
133 // adjustments to avoid breaking websites.
135 // We do need to process all this once we're done with all our reframes,
136 // to handle correctly the cases where we reconstruct an ancestor, like when
137 // you reframe an ib-split (see bug 1559627 for example).
139 // This doesn't handle nested reframes. We'd need to rework quite some code to
140 // do that, and so far it doesn't seem to be a problem in practice.
141 void AddPendingScrollAnchorSuppression(dom::Element
* aElement
) {
142 mPendingScrollAnchorSuppressions
.AppendElement(aElement
);
146 // Process a wrapper restyle at the given index, and restyles for any
147 // wrappers nested in it. Returns the number of entries from
148 // mPendingWrapperRestyles that we processed. The return value is always at
150 size_t ProcessMaybeNestedWrapperRestyle(nsIFrame
* aParent
, size_t aIndex
);
152 ServoStyleSet
& mStyleSet
;
153 nsStyleChangeList
& mChangeList
;
155 // A list of pending wrapper restyles. Anonymous box wrapper frames that need
156 // restyling are added to this list when their non-anonymous kids are
157 // restyled. This avoids us having to do linear searches along the frame tree
158 // for these anonymous boxes. The problem then becomes that we can have
159 // multiple kids all with the same anonymous parent, and we don't want to
160 // restyle it more than once. We use mPendingWrapperRestyles to track which
161 // anonymous wrapper boxes we've requested be restyled and which of them have
162 // already been restyled. We use a single array propagated through
163 // ServoRestyleStates by reference, because in a situation like this:
165 // <div style="display: table"><span></span></div>
167 // We have multiple wrappers to restyle (cell, row, table-row-group) and we
168 // want to add them in to the list all at once but restyle them using
169 // different ServoRestyleStates with different owners. When this situation
170 // occurs, the relevant frames will be placed in the array with ancestors
171 // before descendants.
172 nsTArray
<nsIFrame
*>& mPendingWrapperRestyles
;
174 nsTArray
<RefPtr
<dom::Element
>>& mPendingScrollAnchorSuppressions
;
176 // Since we're given a possibly-nonempty mPendingWrapperRestyles to start
177 // with, we need to keep track of where the part of it we're responsible for
179 size_t mPendingWrapperRestyleOffset
;
181 const nsChangeHint mChangesHandled
;
183 // We track the "owner" frame of this restyle state, that is, the frame that
184 // generated the last change that is stored in mChangesHandled, in order to
185 // verify that we only use mChangesHandled for actual descendants of that
186 // frame (given DOM order isn't always frame order, and that there are a few
187 // special cases for stuff like wrapper frames, ::backdrop, and so on).
189 const nsIFrame
* mOwner
{nullptr};
192 // Whether we should assert in our destructor that we've processed all of the
193 // relevant wrapper restyles.
195 const bool mAssertWrapperRestyleLength
;
199 enum class ServoPostTraversalFlags
: uint32_t;
201 class RestyleManager
{
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 mContents
.AppendElement(aContent
);
277 } else if (pseudoType
== PseudoStyleType::before
) {
278 MOZ_ASSERT(aContent
->NodeInfo()->NameAtom() ==
279 nsGkAtoms::mozgeneratedcontentbefore
);
280 mBeforeContents
.AppendElement(aContent
->GetParent());
281 } else if (pseudoType
== PseudoStyleType::after
) {
282 MOZ_ASSERT(aContent
->NodeInfo()->NameAtom() ==
283 nsGkAtoms::mozgeneratedcontentafter
);
284 mAfterContents
.AppendElement(aContent
->GetParent());
285 } else if (pseudoType
== PseudoStyleType::marker
) {
286 MOZ_ASSERT(aContent
->NodeInfo()->NameAtom() ==
287 nsGkAtoms::mozgeneratedcontentmarker
);
288 mMarkerContents
.AppendElement(aContent
->GetParent());
292 void StopAnimationsForElementsWithoutFrames();
295 void StopAnimationsWithoutFrame(nsTArray
<RefPtr
<nsIContent
>>& aArray
,
296 PseudoStyleType aPseudoType
);
298 RestyleManager
* mRestyleManager
;
299 AutoRestore
<AnimationsWithDestroyedFrame
*> mRestorePointer
;
301 // Below three arrays might include elements that have already had their
302 // animations or transitions stopped.
304 // mBeforeContents, mAfterContents and mMarkerContents hold the real element
305 // rather than the content node for the generated content (which might
306 // change during a reframe)
307 nsTArray
<RefPtr
<nsIContent
>> mContents
;
308 nsTArray
<RefPtr
<nsIContent
>> mBeforeContents
;
309 nsTArray
<RefPtr
<nsIContent
>> mAfterContents
;
310 nsTArray
<RefPtr
<nsIContent
>> mMarkerContents
;
314 * Return the current AnimationsWithDestroyedFrame struct, or null if we're
315 * not currently in a restyling operation.
317 AnimationsWithDestroyedFrame
* GetAnimationsWithDestroyedFrame() {
318 return mAnimationsWithDestroyedFrame
;
321 void ContentInserted(nsIContent
* aChild
);
322 void ContentAppended(nsIContent
* aFirstNewContent
);
324 // This would be have the same logic as RestyleForInsertOrChange if we got the
325 // notification before the removal. However, we get it after, so we need the
326 // following sibling in addition to the old child.
328 // aFollowingSibling is the sibling that used to come after aOldChild before
330 void ContentRemoved(nsIContent
* aOldChild
, nsIContent
* aFollowingSibling
);
332 // Restyling for a ContentInserted (notification after insertion) or
333 // for some CharacterDataChanged.
334 void RestyleForInsertOrChange(nsIContent
* aChild
);
336 // Restyle for a CharacterDataChanged notification. In practice this can only
337 // affect :empty / :-moz-only-whitespace / :-moz-first-node / :-moz-last-node.
338 void CharacterDataChanged(nsIContent
*, const CharacterDataChangeInfo
&);
340 void PostRestyleEvent(dom::Element
*, RestyleHint
,
341 nsChangeHint aMinChangeHint
);
344 * Posts restyle hints for animations.
345 * This is only called for the second traversal for CSS animations during
346 * updating CSS animations in a SequentialTask.
347 * This function does neither register a refresh observer nor flag that a
348 * style flush is needed since this function is supposed to be called during
349 * restyling process and this restyle event will be processed in the second
350 * traversal of the same restyling process.
352 void PostRestyleEventForAnimations(dom::Element
*, PseudoStyleType
,
355 void NextRestyleIsForCSSRuleChanges() { mRestyleForCSSRuleChanges
= true; }
357 void RebuildAllStyleData(nsChangeHint aExtraHint
, RestyleHint
);
359 void ProcessPendingRestyles();
360 void ProcessAllPendingAttributeAndStateInvalidations();
362 void ElementStateChanged(Element
*, dom::ElementState
);
365 * Posts restyle hints for siblings of an element and their descendants if the
366 * element's parent has NODE_HAS_SLOW_SELECTOR_NTH_OF and the element has a
367 * relevant state dependency.
369 void MaybeRestyleForNthOfState(ServoStyleSet
& aStyleSet
, dom::Element
* aChild
,
370 dom::ElementState aChangedBits
);
372 void AttributeWillChange(Element
* aElement
, int32_t aNameSpaceID
,
373 nsAtom
* aAttribute
, int32_t aModType
);
374 void ClassAttributeWillBeChangedBySMIL(dom::Element
* aElement
);
375 void AttributeChanged(dom::Element
* aElement
, int32_t aNameSpaceID
,
376 nsAtom
* aAttribute
, int32_t aModType
,
377 const nsAttrValue
* aOldValue
);
380 * Restyle an element's previous and/or next siblings.
382 void RestyleSiblingsForNthOf(dom::Element
* aChild
,
383 NodeSelectorFlags aParentFlags
);
386 * Posts restyle hints for siblings of an element and their descendants if the
387 * element's parent has NODE_HAS_SLOW_SELECTOR_NTH_OF and the element has a
388 * relevant attribute dependency.
390 void MaybeRestyleForNthOfAttribute(dom::Element
* aChild
, nsAtom
* aAttribute
,
391 const nsAttrValue
* aOldValue
);
393 void MaybeRestyleForRelativeSelectorAttribute(dom::Element
* aElement
,
395 const nsAttrValue
* aOldValue
);
396 void MaybeRestyleForRelativeSelectorState(ServoStyleSet
& aStyleSet
,
397 dom::Element
* aElement
,
398 dom::ElementState aChangedBits
);
400 // This is only used to reparent things when moving them in/out of the
402 void ReparentComputedStyleForFirstLine(nsIFrame
*);
405 * Performs a Servo animation-only traversal to compute style for all nodes
406 * with the animation-only dirty bit in the document.
408 * This processes just the traversal for animation-only restyles and skips the
409 * normal traversal for other restyles unrelated to animations.
410 * This is used to bring throttled animations up-to-date such as when we need
411 * to get correct position for transform animations that are throttled because
412 * they are running on the compositor.
414 * This will traverse all of the document's style roots (that is, its document
415 * element, and the roots of the document-level native anonymous content).
417 void UpdateOnlyAnimationStyles();
419 // Get a counter that increments on every style change, that we use to
420 // track whether off-main-thread animations are up-to-date.
421 uint64_t GetAnimationGeneration() const { return mAnimationGeneration
; }
423 // Typically only style frames have animations associated with them so this
424 // will likely return zero for anything that is not a style frame.
425 static uint64_t GetAnimationGenerationForFrame(nsIFrame
* aStyleFrame
);
427 // Update the animation generation count to mark that animation state
430 // This is normally performed automatically by ProcessPendingRestyles
431 // but it is also called when we have out-of-band changes to animations
432 // such as changes made through the Web Animations API.
433 void IncrementAnimationGeneration();
435 // Apply change hints for animations on the compositor.
437 // There are some cases where we forcibly apply change hints for animations
438 // even if there is no change hint produced in order to synchronize with
439 // animations running on the compositor.
443 // a) Pausing animations via the Web Animations API
444 // b) When the style before sending the animation to the compositor exactly
445 // the same as the current style
446 static void AddLayerChangesForAnimation(
447 nsIFrame
* aStyleFrame
, nsIFrame
* aPrimaryFrame
, Element
* aElement
,
448 nsChangeHint aHintForThisFrame
, nsStyleChangeList
& aChangeListToProcess
);
451 * Whether to clear all the style data (including the element itself), or just
452 * the descendants' data.
454 enum class IncludeRoot
{
460 * Clears the ServoElementData and HasDirtyDescendants from all elements
461 * in the subtree rooted at aElement.
463 static void ClearServoDataFromSubtree(Element
*,
464 IncludeRoot
= IncludeRoot::Yes
);
467 * Clears HasDirtyDescendants and RestyleData from all elements in the
468 * subtree rooted at aElement.
470 static void ClearRestyleStateFromSubtree(Element
* aElement
);
472 explicit RestyleManager(nsPresContext
* aPresContext
);
476 * Reparent the descendants of aFrame. This is used by ReparentComputedStyle
477 * and shouldn't be called by anyone else. aProviderChild, if non-null, is a
478 * child that was the style parent for aFrame and hence shouldn't be
481 void ReparentFrameDescendants(nsIFrame
* aFrame
, nsIFrame
* aProviderChild
,
482 ServoStyleSet
& aStyleSet
);
485 * Performs post-Servo-traversal processing on this element and its
488 * Returns whether any style did actually change. There may be cases where we
489 * didn't need to change any style after all, for example, when a content
490 * attribute changes that happens not to have any effect on the style of that
491 * element or any descendant or sibling.
493 bool ProcessPostTraversal(Element
* aElement
, ServoRestyleState
& aRestyleState
,
494 ServoPostTraversalFlags aFlags
);
496 struct TextPostTraversalState
;
497 bool ProcessPostTraversalForText(nsIContent
* aTextNode
,
498 TextPostTraversalState
& aState
,
499 ServoRestyleState
& aRestyleState
,
500 ServoPostTraversalFlags aFlags
);
502 ServoStyleSet
* StyleSet() const { return PresContext()->StyleSet(); }
504 void RestylePreviousSiblings(nsIContent
* aStartingSibling
);
505 void RestyleSiblingsStartingWith(nsIContent
* aStartingSibling
);
507 void RestyleForEmptyChange(Element
* aContainer
);
508 void MaybeRestyleForEdgeChildChange(nsINode
* aContainer
,
509 nsIContent
* aChangedChild
);
511 bool IsDisconnected() const { return !mPresContext
; }
513 void IncrementRestyleGeneration() {
514 if (++mRestyleGeneration
== 0) {
515 // Keep mRestyleGeneration from being 0, since that's what
516 // nsPresContext::GetRestyleGeneration returns when it no
517 // longer has a RestyleManager.
518 ++mRestyleGeneration
;
520 IncrementUndisplayedRestyleGeneration();
523 void IncrementUndisplayedRestyleGeneration() {
524 if (++mUndisplayedRestyleGeneration
== 0) {
525 // Ensure mUndisplayedRestyleGeneration > 0, for the same reason as
526 // IncrementRestyleGeneration.
527 ++mUndisplayedRestyleGeneration
;
531 nsPresContext
* PresContext() const {
532 MOZ_ASSERT(mPresContext
);
537 nsPresContext
* mPresContext
; // weak, can be null after Disconnect().
538 uint64_t mRestyleGeneration
;
539 uint64_t mUndisplayedRestyleGeneration
;
541 // Used to keep track of frames that have been destroyed during
542 // ProcessRestyledFrames, so we don't try to touch them again even if
543 // they're referenced again later in the changelist.
544 mozilla::UniquePtr
<nsTHashSet
<const nsIFrame
*>> mDestroyedFrames
;
547 // True if we're in the middle of a nsRefreshDriver refresh
548 bool mInStyleRefresh
;
550 // The total number of animation flushes by this frame constructor.
551 // Used to keep the layer and animation manager in sync.
552 uint64_t mAnimationGeneration
;
554 OverflowChangedTracker mOverflowChangedTracker
;
556 AnimationsWithDestroyedFrame
* mAnimationsWithDestroyedFrame
= nullptr;
558 const SnapshotTable
& Snapshots() const { return mSnapshots
; }
559 void ClearSnapshots();
560 ServoElementSnapshot
& SnapshotFor(Element
&);
561 void TakeSnapshotForAttributeChange(Element
&, int32_t aNameSpaceID
,
564 void DoProcessPendingRestyles(ServoTraversalFlags aFlags
);
566 // Function to do the actual (recursive) work of
567 // ReparentComputedStyleForFirstLine, once we have asserted the invariants
568 // that only hold on the initial call.
569 void DoReparentComputedStyleForFirstLine(nsIFrame
*, ServoStyleSet
&);
571 // We use a separate data structure from nsStyleChangeList because we need a
572 // frame to create nsStyleChangeList entries, and the primary frame may not be
574 struct ReentrantChange
{
575 nsCOMPtr
<nsIContent
> mContent
;
578 typedef AutoTArray
<ReentrantChange
, 10> ReentrantChangeList
;
580 // Only non-null while processing change hints. See the comment in
581 // ProcessPendingRestyles.
582 ReentrantChangeList
* mReentrantChanges
= nullptr;
584 // We use this flag to track if the current restyle contains any non-animation
585 // update, which triggers a normal restyle, and so there might be any new
586 // transition created later. Therefore, if this flag is true, we need to
587 // increase mAnimationGeneration before creating new transitions, so their
588 // creation sequence will be correct.
589 bool mHaveNonAnimationRestyles
= false;
591 // Set to true when posting restyle events triggered by CSS rule changes.
592 // This flag is cleared once ProcessPendingRestyles has completed.
593 // When we process a traversal all descendants elements of the document
594 // triggered by CSS rule changes, we will need to update all elements with
595 // CSS animations. We propagate TraversalRestyleBehavior::ForCSSRuleChanges
596 // to traversal function if this flag is set.
597 bool mRestyleForCSSRuleChanges
= false;
599 // A hashtable with the elements that have changed state or attributes, in
600 // order to calculate restyle hints during the traversal.
601 SnapshotTable mSnapshots
;
604 } // namespace mozilla