layout/base/RestyleTracker.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /**
michael@0 7 * A class which manages pending restyles. This handles keeping track
michael@0 8 * of what nodes restyles need to happen on and so forth.
michael@0 9 */
michael@0 10
michael@0 11 #include "RestyleTracker.h"
michael@0 12 #include "nsStyleChangeList.h"
michael@0 13 #include "RestyleManager.h"
michael@0 14 #include "GeckoProfiler.h"
michael@0 15
michael@0 16 namespace mozilla {
michael@0 17
michael@0 18 inline nsIDocument*
michael@0 19 RestyleTracker::Document() const {
michael@0 20 return mRestyleManager->PresContext()->Document();
michael@0 21 }
michael@0 22
michael@0 23 #define RESTYLE_ARRAY_STACKSIZE 128
michael@0 24
michael@0 25 struct LaterSiblingCollector {
michael@0 26 RestyleTracker* tracker;
michael@0 27 nsTArray< nsRefPtr<dom::Element> >* elements;
michael@0 28 };
michael@0 29
michael@0 30 static PLDHashOperator
michael@0 31 CollectLaterSiblings(nsISupports* aElement,
michael@0 32 RestyleTracker::RestyleData& aData,
michael@0 33 void* aSiblingCollector)
michael@0 34 {
michael@0 35 dom::Element* element =
michael@0 36 static_cast<dom::Element*>(aElement);
michael@0 37 LaterSiblingCollector* collector =
michael@0 38 static_cast<LaterSiblingCollector*>(aSiblingCollector);
michael@0 39 // Only collect the entries that actually need restyling by us (and
michael@0 40 // haven't, for example, already been restyled).
michael@0 41 // It's important to not mess with the flags on entries not in our
michael@0 42 // document.
michael@0 43 if (element->GetCurrentDoc() == collector->tracker->Document() &&
michael@0 44 element->HasFlag(collector->tracker->RestyleBit()) &&
michael@0 45 (aData.mRestyleHint & eRestyle_LaterSiblings)) {
michael@0 46 collector->elements->AppendElement(element);
michael@0 47 }
michael@0 48
michael@0 49 return PL_DHASH_NEXT;
michael@0 50 }
michael@0 51
michael@0 52 struct RestyleCollector {
michael@0 53 RestyleTracker* tracker;
michael@0 54 RestyleTracker::RestyleEnumerateData** restyleArrayPtr;
michael@0 55 };
michael@0 56
michael@0 57 static PLDHashOperator
michael@0 58 CollectRestyles(nsISupports* aElement,
michael@0 59 RestyleTracker::RestyleData& aData,
michael@0 60 void* aRestyleCollector)
michael@0 61 {
michael@0 62 dom::Element* element =
michael@0 63 static_cast<dom::Element*>(aElement);
michael@0 64 RestyleCollector* collector =
michael@0 65 static_cast<RestyleCollector*>(aRestyleCollector);
michael@0 66 // Only collect the entries that actually need restyling by us (and
michael@0 67 // haven't, for example, already been restyled).
michael@0 68 // It's important to not mess with the flags on entries not in our
michael@0 69 // document.
michael@0 70 if (element->GetCurrentDoc() != collector->tracker->Document() ||
michael@0 71 !element->HasFlag(collector->tracker->RestyleBit())) {
michael@0 72 return PL_DHASH_NEXT;
michael@0 73 }
michael@0 74
michael@0 75 NS_ASSERTION(!element->HasFlag(collector->tracker->RootBit()) ||
michael@0 76 // Maybe we're just not reachable via the frame tree?
michael@0 77 (element->GetFlattenedTreeParent() &&
michael@0 78 (!element->GetFlattenedTreeParent()->GetPrimaryFrame()||
michael@0 79 element->GetFlattenedTreeParent()->GetPrimaryFrame()->IsLeaf())) ||
michael@0 80 // Or not reachable due to an async reinsert we have
michael@0 81 // pending? If so, we'll have a reframe hint around.
michael@0 82 // That incidentally makes it safe that we still have
michael@0 83 // the bit, since any descendants that didn't get added
michael@0 84 // to the roots list because we had the bits will be
michael@0 85 // completely restyled in a moment.
michael@0 86 (aData.mChangeHint & nsChangeHint_ReconstructFrame),
michael@0 87 "Why did this not get handled while processing mRestyleRoots?");
michael@0 88
michael@0 89 // Unset the restyle bits now, so if they get readded later as we
michael@0 90 // process we won't clobber that adding of the bit.
michael@0 91 element->UnsetFlags(collector->tracker->RestyleBit() |
michael@0 92 collector->tracker->RootBit());
michael@0 93
michael@0 94 RestyleTracker::RestyleEnumerateData** restyleArrayPtr =
michael@0 95 collector->restyleArrayPtr;
michael@0 96 RestyleTracker::RestyleEnumerateData* currentRestyle =
michael@0 97 *restyleArrayPtr;
michael@0 98 currentRestyle->mElement = element;
michael@0 99 currentRestyle->mRestyleHint = aData.mRestyleHint;
michael@0 100 currentRestyle->mChangeHint = aData.mChangeHint;
michael@0 101
michael@0 102 // Increment to the next slot in the array
michael@0 103 *restyleArrayPtr = currentRestyle + 1;
michael@0 104
michael@0 105 return PL_DHASH_NEXT;
michael@0 106 }
michael@0 107
michael@0 108 inline void
michael@0 109 RestyleTracker::ProcessOneRestyle(Element* aElement,
michael@0 110 nsRestyleHint aRestyleHint,
michael@0 111 nsChangeHint aChangeHint)
michael@0 112 {
michael@0 113 NS_PRECONDITION((aRestyleHint & eRestyle_LaterSiblings) == 0,
michael@0 114 "Someone should have handled this before calling us");
michael@0 115 NS_PRECONDITION(Document(), "Must have a document");
michael@0 116 NS_PRECONDITION(aElement->GetCurrentDoc() == Document(),
michael@0 117 "Element has unexpected document");
michael@0 118
michael@0 119 nsIFrame* primaryFrame = aElement->GetPrimaryFrame();
michael@0 120 if (aRestyleHint & (eRestyle_Self | eRestyle_Subtree)) {
michael@0 121 mRestyleManager->RestyleElement(aElement, primaryFrame, aChangeHint,
michael@0 122 *this,
michael@0 123 (aRestyleHint & eRestyle_Subtree) != 0);
michael@0 124 } else if (aChangeHint &&
michael@0 125 (primaryFrame ||
michael@0 126 (aChangeHint & nsChangeHint_ReconstructFrame))) {
michael@0 127 // Don't need to recompute style; just apply the hint
michael@0 128 nsStyleChangeList changeList;
michael@0 129 changeList.AppendChange(primaryFrame, aElement, aChangeHint);
michael@0 130 mRestyleManager->ProcessRestyledFrames(changeList);
michael@0 131 }
michael@0 132 }
michael@0 133
michael@0 134 void
michael@0 135 RestyleTracker::DoProcessRestyles()
michael@0 136 {
michael@0 137 PROFILER_LABEL("CSS", "ProcessRestyles");
michael@0 138
michael@0 139 mRestyleManager->BeginProcessingRestyles();
michael@0 140
michael@0 141 // loop so that we process any restyle events generated by processing
michael@0 142 while (mPendingRestyles.Count()) {
michael@0 143 if (mHaveLaterSiblingRestyles) {
michael@0 144 // Convert them to individual restyles on all the later siblings
michael@0 145 nsAutoTArray<nsRefPtr<Element>, RESTYLE_ARRAY_STACKSIZE> laterSiblingArr;
michael@0 146 LaterSiblingCollector siblingCollector = { this, &laterSiblingArr };
michael@0 147 mPendingRestyles.Enumerate(CollectLaterSiblings, &siblingCollector);
michael@0 148 for (uint32_t i = 0; i < laterSiblingArr.Length(); ++i) {
michael@0 149 Element* element = laterSiblingArr[i];
michael@0 150 for (nsIContent* sibling = element->GetNextSibling();
michael@0 151 sibling;
michael@0 152 sibling = sibling->GetNextSibling()) {
michael@0 153 if (sibling->IsElement() &&
michael@0 154 AddPendingRestyle(sibling->AsElement(), eRestyle_Subtree,
michael@0 155 NS_STYLE_HINT_NONE)) {
michael@0 156 // Nothing else to do here; we'll handle the following
michael@0 157 // siblings when we get to |sibling| in laterSiblingArr.
michael@0 158 break;
michael@0 159 }
michael@0 160 }
michael@0 161 }
michael@0 162
michael@0 163 // Now remove all those eRestyle_LaterSiblings bits
michael@0 164 for (uint32_t i = 0; i < laterSiblingArr.Length(); ++i) {
michael@0 165 Element* element = laterSiblingArr[i];
michael@0 166 NS_ASSERTION(element->HasFlag(RestyleBit()), "How did that happen?");
michael@0 167 RestyleData data;
michael@0 168 #ifdef DEBUG
michael@0 169 bool found =
michael@0 170 #endif
michael@0 171 mPendingRestyles.Get(element, &data);
michael@0 172 NS_ASSERTION(found, "Where did our entry go?");
michael@0 173 data.mRestyleHint =
michael@0 174 nsRestyleHint(data.mRestyleHint & ~eRestyle_LaterSiblings);
michael@0 175
michael@0 176 mPendingRestyles.Put(element, data);
michael@0 177 }
michael@0 178
michael@0 179 mHaveLaterSiblingRestyles = false;
michael@0 180 }
michael@0 181
michael@0 182 uint32_t rootCount;
michael@0 183 while ((rootCount = mRestyleRoots.Length())) {
michael@0 184 // Make sure to pop the element off our restyle root array, so
michael@0 185 // that we can freely append to the array as we process this
michael@0 186 // element.
michael@0 187 nsRefPtr<Element> element;
michael@0 188 element.swap(mRestyleRoots[rootCount - 1]);
michael@0 189 mRestyleRoots.RemoveElementAt(rootCount - 1);
michael@0 190
michael@0 191 // Do the document check before calling GetRestyleData, since we
michael@0 192 // don't want to do the sibling-processing GetRestyleData does if
michael@0 193 // the node is no longer relevant.
michael@0 194 if (element->GetCurrentDoc() != Document()) {
michael@0 195 // Content node has been removed from our document; nothing else
michael@0 196 // to do here
michael@0 197 continue;
michael@0 198 }
michael@0 199
michael@0 200 RestyleData data;
michael@0 201 if (!GetRestyleData(element, &data)) {
michael@0 202 continue;
michael@0 203 }
michael@0 204
michael@0 205 ProcessOneRestyle(element, data.mRestyleHint, data.mChangeHint);
michael@0 206 }
michael@0 207
michael@0 208 if (mHaveLaterSiblingRestyles) {
michael@0 209 // Keep processing restyles for now
michael@0 210 continue;
michael@0 211 }
michael@0 212
michael@0 213 // Now we only have entries with change hints left. To be safe in
michael@0 214 // case of reentry from the handing of the change hint, use a
michael@0 215 // scratch array instead of calling out to ProcessOneRestyle while
michael@0 216 // enumerating the hashtable. Use the stack if we can, otherwise
michael@0 217 // fall back on heap-allocation.
michael@0 218 nsAutoTArray<RestyleEnumerateData, RESTYLE_ARRAY_STACKSIZE> restyleArr;
michael@0 219 RestyleEnumerateData* restylesToProcess =
michael@0 220 restyleArr.AppendElements(mPendingRestyles.Count());
michael@0 221 if (restylesToProcess) {
michael@0 222 RestyleEnumerateData* lastRestyle = restylesToProcess;
michael@0 223 RestyleCollector collector = { this, &lastRestyle };
michael@0 224 mPendingRestyles.Enumerate(CollectRestyles, &collector);
michael@0 225
michael@0 226 // Clear the hashtable now that we don't need it anymore
michael@0 227 mPendingRestyles.Clear();
michael@0 228
michael@0 229 for (RestyleEnumerateData* currentRestyle = restylesToProcess;
michael@0 230 currentRestyle != lastRestyle;
michael@0 231 ++currentRestyle) {
michael@0 232 ProcessOneRestyle(currentRestyle->mElement,
michael@0 233 currentRestyle->mRestyleHint,
michael@0 234 currentRestyle->mChangeHint);
michael@0 235 }
michael@0 236 }
michael@0 237 }
michael@0 238
michael@0 239 mRestyleManager->EndProcessingRestyles();
michael@0 240 }
michael@0 241
michael@0 242 bool
michael@0 243 RestyleTracker::GetRestyleData(Element* aElement, RestyleData* aData)
michael@0 244 {
michael@0 245 NS_PRECONDITION(aElement->GetCurrentDoc() == Document(),
michael@0 246 "Unexpected document; this will lead to incorrect behavior!");
michael@0 247
michael@0 248 if (!aElement->HasFlag(RestyleBit())) {
michael@0 249 NS_ASSERTION(!aElement->HasFlag(RootBit()), "Bogus root bit?");
michael@0 250 return false;
michael@0 251 }
michael@0 252
michael@0 253 #ifdef DEBUG
michael@0 254 bool gotData =
michael@0 255 #endif
michael@0 256 mPendingRestyles.Get(aElement, aData);
michael@0 257 NS_ASSERTION(gotData, "Must have data if restyle bit is set");
michael@0 258
michael@0 259 if (aData->mRestyleHint & eRestyle_LaterSiblings) {
michael@0 260 // Someone readded the eRestyle_LaterSiblings hint for this
michael@0 261 // element. Leave it around for now, but remove the other restyle
michael@0 262 // hints and the change hint for it. Also unset its root bit,
michael@0 263 // since it's no longer a root with the new restyle data.
michael@0 264 RestyleData newData;
michael@0 265 newData.mChangeHint = nsChangeHint(0);
michael@0 266 newData.mRestyleHint = eRestyle_LaterSiblings;
michael@0 267 mPendingRestyles.Put(aElement, newData);
michael@0 268 aElement->UnsetFlags(RootBit());
michael@0 269 aData->mRestyleHint =
michael@0 270 nsRestyleHint(aData->mRestyleHint & ~eRestyle_LaterSiblings);
michael@0 271 } else {
michael@0 272 mPendingRestyles.Remove(aElement);
michael@0 273 aElement->UnsetFlags(mRestyleBits);
michael@0 274 }
michael@0 275
michael@0 276 return true;
michael@0 277 }
michael@0 278
michael@0 279 } // namespace mozilla
michael@0 280

mercurial