2010-06-18 09:23:04 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2010-06-18 09:23:04 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A class which manages pending restyles. This handles keeping track
|
|
|
|
* of what nodes restyles need to happen on and so forth.
|
|
|
|
*/
|
|
|
|
|
2013-07-20 12:14:24 -07:00
|
|
|
#ifndef mozilla_RestyleTracker_h
|
|
|
|
#define mozilla_RestyleTracker_h
|
2010-06-18 12:35:52 -07:00
|
|
|
|
2010-06-18 09:23:04 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "nsDataHashtable.h"
|
2010-06-18 09:23:05 -07:00
|
|
|
#include "nsIFrame.h"
|
2013-01-15 15:12:40 -08:00
|
|
|
#include "mozilla/SplayTree.h"
|
2010-06-18 09:23:04 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2013-07-20 12:14:25 -07:00
|
|
|
class RestyleManager;
|
|
|
|
|
2012-12-09 18:32:55 -08:00
|
|
|
/**
|
|
|
|
* 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:
|
|
|
|
|
2013-02-19 16:13:01 -08:00
|
|
|
~OverflowChangedTracker()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mEntryList.empty(), "Need to flush before destroying!");
|
|
|
|
}
|
|
|
|
|
2012-12-09 18:32:55 -08:00
|
|
|
/**
|
|
|
|
* 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) {
|
2013-03-20 19:33:00 -07:00
|
|
|
uint32_t depth = aFrame->GetDepthInFrameTree();
|
|
|
|
if (mEntryList.empty() ||
|
|
|
|
!mEntryList.contains(Entry(aFrame, depth, true))) {
|
|
|
|
mEntryList.insert(new Entry(aFrame, depth, true));
|
2013-01-15 15:12:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-25 17:34:58 -08:00
|
|
|
* Remove a frame.
|
2013-01-15 15:12:40 -08:00
|
|
|
*/
|
2013-01-25 17:34:58 -08:00
|
|
|
void RemoveFrame(nsIFrame* aFrame) {
|
2013-03-20 19:33:00 -07:00
|
|
|
if (mEntryList.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t depth = aFrame->GetDepthInFrameTree();
|
|
|
|
if (mEntryList.contains(Entry(aFrame, depth, false))) {
|
|
|
|
delete mEntryList.remove(Entry(aFrame, depth, false));
|
2013-01-15 15:12:40 -08:00
|
|
|
}
|
2012-12-09 18:32:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
2013-01-15 15:12:40 -08:00
|
|
|
while (!mEntryList.empty()) {
|
|
|
|
Entry *entry = mEntryList.removeMin();
|
2012-12-09 18:32:55 -08:00
|
|
|
|
2013-01-15 15:12:40 -08:00
|
|
|
nsIFrame *frame = entry->mFrame;
|
2012-12-09 18:32:55 -08:00
|
|
|
|
|
|
|
bool updateParent = false;
|
2013-01-15 15:12:40 -08:00
|
|
|
if (entry->mInitial) {
|
2012-12-09 18:32:55 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2013-01-15 15:12:40 -08:00
|
|
|
|
2012-12-09 18:32:55 -08:00
|
|
|
// If the overflow changed, then we want to also update the parent's
|
|
|
|
// overflow. We always update the parent for initial frames.
|
|
|
|
if (!updateParent) {
|
2013-01-15 15:12:40 -08:00
|
|
|
updateParent = frame->UpdateOverflow() || entry->mInitial;
|
2012-12-09 18:32:55 -08:00
|
|
|
}
|
|
|
|
if (updateParent) {
|
|
|
|
nsIFrame *parent = frame->GetParent();
|
|
|
|
if (parent) {
|
2013-01-15 15:12:40 -08:00
|
|
|
if (!mEntryList.contains(Entry(parent, entry->mDepth - 1, false))) {
|
|
|
|
mEntryList.insert(new Entry(parent, entry->mDepth - 1, false));
|
|
|
|
}
|
2012-12-09 18:32:55 -08:00
|
|
|
}
|
|
|
|
}
|
2013-01-15 15:12:40 -08:00
|
|
|
delete entry;
|
2012-12-09 18:32:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-01-15 15:12:40 -08:00
|
|
|
struct Entry : SplayTreeNode<Entry>
|
2012-12-09 18:32:55 -08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-10 12:44:13 -07:00
|
|
|
* Sort by *reverse* depth in the tree, and break ties with
|
|
|
|
* the frame pointer.
|
2012-12-09 18:32:55 -08:00
|
|
|
*/
|
|
|
|
bool operator<(const Entry& aOther) const
|
|
|
|
{
|
2013-03-10 12:44:13 -07:00
|
|
|
if (mDepth == aOther.mDepth) {
|
|
|
|
return mFrame < aOther.mFrame;
|
|
|
|
}
|
|
|
|
return mDepth > aOther.mDepth; /* reverse, want "min" to be deepest */
|
2012-12-09 18:32:55 -08:00
|
|
|
}
|
|
|
|
|
2013-01-15 15:12:40 -08:00
|
|
|
static int compare(const Entry& aOne, const Entry& aTwo)
|
|
|
|
{
|
2013-01-25 17:34:58 -08:00
|
|
|
if (aOne == aTwo) {
|
2013-01-15 15:12:40 -08:00
|
|
|
return 0;
|
2013-01-25 17:34:58 -08:00
|
|
|
} else if (aOne < aTwo) {
|
|
|
|
return -1;
|
2013-01-15 15:12:40 -08:00
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-09 18:32:55 -08:00
|
|
|
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 */
|
2013-01-15 15:12:40 -08:00
|
|
|
SplayTree<Entry, Entry> mEntryList;
|
2012-12-09 18:32:55 -08:00
|
|
|
};
|
|
|
|
|
2010-06-18 09:23:04 -07:00
|
|
|
class RestyleTracker {
|
|
|
|
public:
|
|
|
|
typedef mozilla::dom::Element Element;
|
|
|
|
|
2013-04-28 03:02:35 -07:00
|
|
|
RestyleTracker(uint32_t aRestyleBits) :
|
|
|
|
mRestyleBits(aRestyleBits),
|
2011-10-17 07:59:28 -07:00
|
|
|
mHaveLaterSiblingRestyles(false)
|
2010-06-18 09:23:04 -07:00
|
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2013-07-20 12:14:25 -07:00
|
|
|
void Init(RestyleManager* aRestyleManager) {
|
|
|
|
mRestyleManager = aRestyleManager;
|
2012-05-18 10:30:49 -07:00
|
|
|
mPendingRestyles.Init();
|
2010-06-18 09:23:04 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t Count() const {
|
2010-06-18 09:23:04 -07:00
|
|
|
return mPendingRestyles.Count();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-18 09:23:05 -07:00
|
|
|
* Add a restyle for the given element to the tracker. Returns true
|
|
|
|
* if the element already had eRestyle_LaterSiblings set on it.
|
2010-06-18 09:23:04 -07:00
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool AddPendingRestyle(Element* aElement, nsRestyleHint aRestyleHint,
|
2010-06-18 09:23:05 -07:00
|
|
|
nsChangeHint aMinChangeHint);
|
2010-06-18 09:23:04 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the restyles we've been tracking.
|
|
|
|
*/
|
2011-12-14 20:42:14 -08:00
|
|
|
void ProcessRestyles() {
|
|
|
|
// Fast-path the common case (esp. for the animation restyle
|
|
|
|
// tracker) of not having anything to do.
|
|
|
|
if (mPendingRestyles.Count()) {
|
|
|
|
DoProcessRestyles();
|
|
|
|
}
|
|
|
|
}
|
2010-06-18 09:23:04 -07:00
|
|
|
|
2010-06-18 09:23:05 -07:00
|
|
|
// Return our ELEMENT_HAS_PENDING_(ANIMATION_)RESTYLE bit
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t RestyleBit() const {
|
2010-06-18 09:23:05 -07:00
|
|
|
return mRestyleBits & ELEMENT_PENDING_RESTYLE_FLAGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return our ELEMENT_IS_POTENTIAL_(ANIMATION_)RESTYLE_ROOT bit
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t RootBit() const {
|
2010-06-18 09:23:05 -07:00
|
|
|
return mRestyleBits & ~ELEMENT_PENDING_RESTYLE_FLAGS;
|
|
|
|
}
|
|
|
|
|
2010-06-18 09:23:04 -07:00
|
|
|
struct RestyleData {
|
|
|
|
nsRestyleHint mRestyleHint; // What we want to restyle
|
|
|
|
nsChangeHint mChangeHint; // The minimal change hint for "self"
|
|
|
|
};
|
|
|
|
|
2010-06-18 09:23:05 -07:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool GetRestyleData(Element* aElement, RestyleData* aData);
|
2010-06-18 09:23:05 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The document we're associated with.
|
|
|
|
*/
|
|
|
|
inline nsIDocument* Document() const;
|
|
|
|
|
2010-06-18 09:23:04 -07:00
|
|
|
struct RestyleEnumerateData : public RestyleData {
|
2010-07-30 20:44:45 -07:00
|
|
|
nsRefPtr<Element> mElement;
|
2010-06-18 09:23:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2010-06-18 09:23:05 -07:00
|
|
|
/**
|
2010-06-18 09:23:05 -07:00
|
|
|
* Handle a single mPendingRestyles entry. aRestyleHint must not
|
|
|
|
* include eRestyle_LaterSiblings; that needs to be dealt with
|
|
|
|
* before calling this function.
|
2010-06-18 09:23:05 -07:00
|
|
|
*/
|
2010-06-18 09:23:05 -07:00
|
|
|
inline void ProcessOneRestyle(Element* aElement,
|
|
|
|
nsRestyleHint aRestyleHint,
|
2013-02-19 16:13:01 -08:00
|
|
|
nsChangeHint aChangeHint);
|
2010-06-18 09:23:05 -07:00
|
|
|
|
2011-12-14 20:42:14 -08:00
|
|
|
/**
|
|
|
|
* The guts of our restyle processing.
|
|
|
|
*/
|
|
|
|
void DoProcessRestyles();
|
|
|
|
|
2010-06-18 09:23:04 -07:00
|
|
|
typedef nsDataHashtable<nsISupportsHashKey, RestyleData> PendingRestyleTable;
|
2010-06-18 09:23:05 -07:00
|
|
|
typedef nsAutoTArray< nsRefPtr<Element>, 32> RestyleRootArray;
|
2010-06-18 09:23:04 -07:00
|
|
|
// 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.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mRestyleBits;
|
2013-07-20 12:14:25 -07:00
|
|
|
RestyleManager* mRestyleManager; // Owns us
|
2010-06-18 09:23:05 -07:00
|
|
|
// 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.
|
2010-06-18 09:23:04 -07:00
|
|
|
PendingRestyleTable mPendingRestyles;
|
2010-06-18 09:23:05 -07:00
|
|
|
// 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.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mHaveLaterSiblingRestyles;
|
2010-06-18 09:23:04 -07:00
|
|
|
};
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
inline bool RestyleTracker::AddPendingRestyle(Element* aElement,
|
2010-06-18 09:23:05 -07:00
|
|
|
nsRestyleHint aRestyleHint,
|
|
|
|
nsChangeHint aMinChangeHint)
|
2010-06-18 09:23:04 -07:00
|
|
|
{
|
|
|
|
RestyleData existingData;
|
|
|
|
existingData.mRestyleHint = nsRestyleHint(0);
|
|
|
|
existingData.mChangeHint = NS_STYLE_HINT_NONE;
|
|
|
|
|
2010-06-18 09:23:05 -07:00
|
|
|
// 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());
|
|
|
|
}
|
2010-06-18 09:23:04 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hadRestyleLaterSiblings =
|
2010-06-18 09:23:05 -07:00
|
|
|
(existingData.mRestyleHint & eRestyle_LaterSiblings) != 0;
|
2010-06-18 09:23:04 -07:00
|
|
|
existingData.mRestyleHint =
|
2010-06-18 09:23:05 -07:00
|
|
|
nsRestyleHint(existingData.mRestyleHint | aRestyleHint);
|
2010-06-18 09:23:04 -07:00
|
|
|
NS_UpdateHint(existingData.mChangeHint, aMinChangeHint);
|
|
|
|
|
|
|
|
mPendingRestyles.Put(aElement, existingData);
|
2010-06-18 09:23:05 -07:00
|
|
|
|
|
|
|
// 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).
|
2010-06-18 09:23:05 -07:00
|
|
|
if ((aRestyleHint & (eRestyle_Self | eRestyle_Subtree)) ||
|
2010-06-18 09:23:05 -07:00
|
|
|
(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;
|
2010-06-18 09:23:04 -07:00
|
|
|
}
|
|
|
|
|
2010-06-18 12:18:02 -07:00
|
|
|
} // namespace mozilla
|
2010-06-18 09:23:04 -07:00
|
|
|
|
2013-07-20 12:14:24 -07:00
|
|
|
#endif /* mozilla_RestyleTracker_h */
|