forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestyleTracker.h
341 lines (299 loc) · 11.6 KB
/
RestyleTracker.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* A class which manages pending restyles. This handles keeping track
* of what nodes restyles need to happen on and so forth.
*/
#ifndef mozilla_css_RestyleTracker_h
#define mozilla_css_RestyleTracker_h
#include "mozilla/dom/Element.h"
#include "nsDataHashtable.h"
#include "nsIFrame.h"
#include "nsTPriorityQueue.h"
class nsCSSFrameConstructor;
namespace mozilla {
namespace css {
/**
* Helper class that collects a list of frames that need
* UpdateOverflow() called on them, and coalesces them
* to avoid walking up the same ancestor tree multiple times.
*/
class OverflowChangedTracker
{
public:
/**
* Add a frame that has had a style change, and needs its
* overflow updated.
*
* If there are pre-transform overflow areas stored for this
* frame, then we will call FinishAndStoreOverflow with those
* areas instead of UpdateOverflow().
*
* If the overflow area changes, then UpdateOverflow will also
* be called on the parent.
*/
void AddFrame(nsIFrame* aFrame) {
mEntryList.Push(Entry(aFrame, true));
}
/**
* Update the overflow of all added frames, and clear the entry list.
*
* Start from those deepest in the frame tree and works upwards. This stops
* us from processing the same frame twice.
*/
void Flush() {
while (!mEntryList.IsEmpty()) {
Entry entry = mEntryList.Pop();
// Pop off any duplicate entries and copy back mInitial
// if any have it set.
while (!mEntryList.IsEmpty() &&
mEntryList.Top().mFrame == entry.mFrame) {
Entry next = mEntryList.Pop();
if (next.mInitial) {
entry.mInitial = true;
}
}
nsIFrame *frame = entry.mFrame;
bool updateParent = false;
if (entry.mInitial) {
nsOverflowAreas* pre = static_cast<nsOverflowAreas*>
(frame->Properties().Get(frame->PreTransformOverflowAreasProperty()));
if (pre) {
// FinishAndStoreOverflow will change the overflow areas passed in,
// so make a copy.
nsOverflowAreas overflowAreas = *pre;
frame->FinishAndStoreOverflow(overflowAreas, frame->GetSize());
// We can't tell if the overflow changed, so update the parent regardless
updateParent = true;
}
}
// If the overflow changed, then we want to also update the parent's
// overflow. We always update the parent for initial frames.
if (!updateParent) {
updateParent = frame->UpdateOverflow() || entry.mInitial;
}
if (updateParent) {
nsIFrame *parent = frame->GetParent();
if (parent) {
mEntryList.Push(Entry(parent, entry.mDepth - 1, false));
}
}
}
}
private:
struct Entry
{
Entry(nsIFrame* aFrame, bool aInitial)
: mFrame(aFrame)
, mDepth(aFrame->GetDepthInFrameTree())
, mInitial(aInitial)
{}
Entry(nsIFrame* aFrame, uint32_t aDepth, bool aInitial)
: mFrame(aFrame)
, mDepth(aDepth)
, mInitial(aInitial)
{}
bool operator==(const Entry& aOther) const
{
return mFrame == aOther.mFrame;
}
/**
* Sort by the depth in the frame tree, and then
* the frame pointer.
*/
bool operator<(const Entry& aOther) const
{
if (mDepth != aOther.mDepth) {
// nsTPriorityQueue implements a min-heap and we
// want the highest depth first, so reverse this check.
return mDepth > aOther.mDepth;
}
return mFrame < aOther.mFrame;
}
nsIFrame* mFrame;
/* Depth in the frame tree */
uint32_t mDepth;
/**
* True if the frame had the actual style change, and we
* want to check for pre-transform overflow areas.
*/
bool mInitial;
};
/* A list of frames to process, sorted by their depth in the frame tree */
nsTPriorityQueue<Entry> mEntryList;
};
class RestyleTracker {
public:
typedef mozilla::dom::Element Element;
RestyleTracker(uint32_t aRestyleBits,
nsCSSFrameConstructor* aFrameConstructor) :
mRestyleBits(aRestyleBits), mFrameConstructor(aFrameConstructor),
mHaveLaterSiblingRestyles(false)
{
NS_PRECONDITION((mRestyleBits & ~ELEMENT_ALL_RESTYLE_FLAGS) == 0,
"Why do we have these bits set?");
NS_PRECONDITION((mRestyleBits & ELEMENT_PENDING_RESTYLE_FLAGS) != 0,
"Must have a restyle flag");
NS_PRECONDITION((mRestyleBits & ELEMENT_PENDING_RESTYLE_FLAGS) !=
ELEMENT_PENDING_RESTYLE_FLAGS,
"Shouldn't have both restyle flags set");
NS_PRECONDITION((mRestyleBits & ~ELEMENT_PENDING_RESTYLE_FLAGS) != 0,
"Must have root flag");
NS_PRECONDITION((mRestyleBits & ~ELEMENT_PENDING_RESTYLE_FLAGS) !=
(ELEMENT_ALL_RESTYLE_FLAGS & ~ELEMENT_PENDING_RESTYLE_FLAGS),
"Shouldn't have both root flags");
}
void Init() {
mPendingRestyles.Init();
}
uint32_t Count() const {
return mPendingRestyles.Count();
}
/**
* Add a restyle for the given element to the tracker. Returns true
* if the element already had eRestyle_LaterSiblings set on it.
*/
bool AddPendingRestyle(Element* aElement, nsRestyleHint aRestyleHint,
nsChangeHint aMinChangeHint);
/**
* Process the restyles we've been tracking.
*/
void ProcessRestyles() {
// Fast-path the common case (esp. for the animation restyle
// tracker) of not having anything to do.
if (mPendingRestyles.Count()) {
DoProcessRestyles();
}
}
// Return our ELEMENT_HAS_PENDING_(ANIMATION_)RESTYLE bit
uint32_t RestyleBit() const {
return mRestyleBits & ELEMENT_PENDING_RESTYLE_FLAGS;
}
// Return our ELEMENT_IS_POTENTIAL_(ANIMATION_)RESTYLE_ROOT bit
uint32_t RootBit() const {
return mRestyleBits & ~ELEMENT_PENDING_RESTYLE_FLAGS;
}
struct RestyleData {
nsRestyleHint mRestyleHint; // What we want to restyle
nsChangeHint mChangeHint; // The minimal change hint for "self"
};
/**
* If the given Element has a restyle pending for it, return the
* relevant restyle data. This function will clear everything other
* than a possible eRestyle_LaterSiblings hint for aElement out of
* our hashtable. The returned aData will never have an
* eRestyle_LaterSiblings hint in it.
*
* The return value indicates whether any restyle data was found for
* the element. If false is returned, then the state of *aData is
* undefined.
*/
bool GetRestyleData(Element* aElement, RestyleData* aData);
/**
* The document we're associated with.
*/
inline nsIDocument* Document() const;
struct RestyleEnumerateData : public RestyleData {
nsRefPtr<Element> mElement;
};
private:
/**
* Handle a single mPendingRestyles entry. aRestyleHint must not
* include eRestyle_LaterSiblings; that needs to be dealt with
* before calling this function.
*/
inline void ProcessOneRestyle(Element* aElement,
nsRestyleHint aRestyleHint,
nsChangeHint aChangeHint,
OverflowChangedTracker& aTracker);
/**
* The guts of our restyle processing.
*/
void DoProcessRestyles();
typedef nsDataHashtable<nsISupportsHashKey, RestyleData> PendingRestyleTable;
typedef nsAutoTArray< nsRefPtr<Element>, 32> RestyleRootArray;
// Our restyle bits. These will be a subset of ELEMENT_ALL_RESTYLE_FLAGS, and
// will include one flag from ELEMENT_PENDING_RESTYLE_FLAGS and one flag
// that's not in ELEMENT_PENDING_RESTYLE_FLAGS.
uint32_t mRestyleBits;
nsCSSFrameConstructor* mFrameConstructor; // Owns us
// A hashtable that maps elements to RestyleData structs. The
// values only make sense if the element's current document is our
// document and it has our RestyleBit() flag set. In particular,
// said bit might not be set if the element had a restyle posted and
// then was moved around in the DOM.
PendingRestyleTable mPendingRestyles;
// An array that keeps track of our possible restyle roots. This
// maintains the invariant that if A and B are both restyle roots
// and A is an ancestor of B then A will come after B in the array.
// We maintain this invariant by checking whether an element has an
// ancestor with the restyle root bit set before appending it to the
// array.
RestyleRootArray mRestyleRoots;
// True if we have some entries with the eRestyle_LaterSiblings
// flag. We need this to avoid enumerating the hashtable looking
// for such entries when we can't possibly have any.
bool mHaveLaterSiblingRestyles;
};
inline bool RestyleTracker::AddPendingRestyle(Element* aElement,
nsRestyleHint aRestyleHint,
nsChangeHint aMinChangeHint)
{
RestyleData existingData;
existingData.mRestyleHint = nsRestyleHint(0);
existingData.mChangeHint = NS_STYLE_HINT_NONE;
// Check the RestyleBit() flag before doing the hashtable Get, since
// it's possible that the data in the hashtable isn't actually
// relevant anymore (if the flag is not set).
if (aElement->HasFlag(RestyleBit())) {
mPendingRestyles.Get(aElement, &existingData);
} else {
aElement->SetFlags(RestyleBit());
}
bool hadRestyleLaterSiblings =
(existingData.mRestyleHint & eRestyle_LaterSiblings) != 0;
existingData.mRestyleHint =
nsRestyleHint(existingData.mRestyleHint | aRestyleHint);
NS_UpdateHint(existingData.mChangeHint, aMinChangeHint);
mPendingRestyles.Put(aElement, existingData);
// We can only treat this element as a restyle root if we would
// actually restyle its descendants (so either call
// ReResolveStyleContext on it or just reframe it).
if ((aRestyleHint & (eRestyle_Self | eRestyle_Subtree)) ||
(aMinChangeHint & nsChangeHint_ReconstructFrame)) {
for (const Element* cur = aElement; !cur->HasFlag(RootBit()); ) {
nsIContent* parent = cur->GetFlattenedTreeParent();
// Stop if we have no parent or the parent is not an element or
// we're part of the viewport scrollbars (because those are not
// frametree descendants of the primary frame of the root
// element).
// XXXbz maybe the primary frame of the root should be the root scrollframe?
if (!parent || !parent->IsElement() ||
// If we've hit the root via a native anonymous kid and that
// this native anonymous kid is not obviously a descendant
// of the root's primary frame, assume we're under the root
// scrollbars. Since those don't get reresolved when
// reresolving the root, we need to make sure to add the
// element to mRestyleRoots.
(cur->IsInNativeAnonymousSubtree() && !parent->GetParent() &&
cur->GetPrimaryFrame() &&
cur->GetPrimaryFrame()->GetParent() != parent->GetPrimaryFrame())) {
mRestyleRoots.AppendElement(aElement);
break;
}
cur = parent->AsElement();
}
// At this point some ancestor of aElement (possibly aElement
// itself) is in mRestyleRoots. Set the root bit on aElement, to
// speed up searching for an existing root on its descendants.
aElement->SetFlags(RootBit());
}
mHaveLaterSiblingRestyles =
mHaveLaterSiblingRestyles || (aRestyleHint & eRestyle_LaterSiblings) != 0;
return hadRestyleLaterSiblings;
}
} // namespace css
} // namespace mozilla
#endif /* mozilla_css_RestyleTracker_h */