Bug 1637593 [wpt PR 23564] - Fix superclass calls in MarionetteRefTestExecutor, a...
[gecko.git] / layout / style / ComputedStyle.cpp
blobeede16c600fa12fc1ba12ccce62a4d89ddde9a5a
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 /* the interface (to internal code) for retrieving computed style data */
9 #include "mozilla/ComputedStyle.h"
10 #include "mozilla/DebugOnly.h"
11 #include "mozilla/Maybe.h"
12 #include "mozilla/ToString.h"
14 #include "nsCSSAnonBoxes.h"
15 #include "nsCSSPseudoElements.h"
16 #include "nsFontMetrics.h"
17 #include "nsStyleConsts.h"
18 #include "nsStyleStruct.h"
19 #include "nsStyleStructInlines.h"
20 #include "nsString.h"
21 #include "nsPresContext.h"
22 #include "nsWindowSizes.h"
24 #include "nsCOMPtr.h"
26 #include "GeckoProfiler.h"
27 #include "mozilla/dom/Document.h"
28 #include "nsPrintfCString.h"
29 #include "RubyUtils.h"
30 #include "mozilla/ComputedStyleInlines.h"
31 #include "mozilla/Preferences.h"
33 #include "mozilla/ReflowInput.h"
34 #include "nsLayoutUtils.h"
35 #include "nsCoord.h"
37 // Ensure the binding function declarations in ComputedStyle.h matches
38 // those in ServoBindings.h.
39 #include "mozilla/ServoBindings.h"
41 namespace mozilla {
43 ComputedStyle::ComputedStyle(PseudoStyleType aPseudoType,
44 ServoComputedDataForgotten aComputedValues)
45 : mSource(aComputedValues), mPseudoType(aPseudoType) {}
47 // If a struct returned nsChangeHint_UpdateContainingBlock, that means that one
48 // property's influence on whether we're a containing block for abs-pos or
49 // fixed-pos elements has changed.
51 // However, we only need to return the hint if the overall computation of
52 // whether we establish a containing block has really changed.
53 static bool ContainingBlockMayHaveChanged(const ComputedStyle& aOldStyle,
54 const ComputedStyle& aNewStyle) {
55 auto* oldDisp = aOldStyle.StyleDisplay();
56 auto* newDisp = aNewStyle.StyleDisplay();
58 if (oldDisp->IsAbsPosContainingBlockForNonSVGTextFrames() !=
59 newDisp->IsAbsPosContainingBlockForNonSVGTextFrames()) {
60 return true;
63 bool fixedCB =
64 oldDisp->IsFixedPosContainingBlockForNonSVGTextFrames(aOldStyle);
65 if (fixedCB !=
66 newDisp->IsFixedPosContainingBlockForNonSVGTextFrames(aNewStyle)) {
67 return true;
69 // If we were both before and after a fixed-pos containing-block that means
70 // that everything else doesn't matter, since all the other conditions are a
71 // subset of this.
72 if (fixedCB) {
73 return false;
75 // Note that neither of these two following sets of frames
76 // (transform-supporting and layout-and-paint-supporting frames) is a subset
77 // of the other, because table frames support contain: layout/paint but not
78 // transforms (which are instead inherited to the table wrapper), and quite a
79 // few frame types support transforms but not contain: layout/paint (e.g.,
80 // table rows and row groups, many SVG frames).
81 if (oldDisp->IsFixedPosContainingBlockForTransformSupportingFrames() !=
82 newDisp->IsFixedPosContainingBlockForTransformSupportingFrames()) {
83 return true;
85 if (oldDisp
86 ->IsFixedPosContainingBlockForContainLayoutAndPaintSupportingFrames() !=
87 newDisp
88 ->IsFixedPosContainingBlockForContainLayoutAndPaintSupportingFrames()) {
89 return true;
91 return false;
94 nsChangeHint ComputedStyle::CalcStyleDifference(const ComputedStyle& aNewStyle,
95 uint32_t* aEqualStructs) const {
96 AUTO_PROFILER_LABEL("ComputedStyle::CalcStyleDifference", LAYOUT);
97 static_assert(StyleStructConstants::kStyleStructCount <= 32,
98 "aEqualStructs is not big enough");
100 *aEqualStructs = 0;
102 nsChangeHint hint = nsChangeHint(0);
103 // We must always ensure that we populate the structs on the new style
104 // context that are filled in on the old context, so that if we get
105 // two style changes in succession, the second of which causes a real
106 // style change, the PeekStyleData doesn't return null (implying that
107 // nobody ever looked at that struct's data). In other words, we
108 // can't skip later structs if we get a big change up front, because
109 // we could later get a small change in one of those structs that we
110 // don't want to miss.
112 DebugOnly<uint32_t> structsFound = 0;
114 DebugOnly<int> styleStructCount = 0;
116 // Servo's optimization to stop the cascade when there are no style changes
117 // that children need to be recascade for relies on comparing all of the
118 // structs, not just those that are returned from PeekStyleData, although
119 // if PeekStyleData does return null we could avoid to accumulate any change
120 // hints for those structs.
122 // FIXME(emilio): Reintroduce that optimization either for all kind of structs
123 // after bug 1368290 with a weak parent pointer from text, or just for reset
124 // structs.
125 #define STYLE_STRUCT_BIT(name_) \
126 StyleStructConstants::BitFor(StyleStructID::name_)
128 #define EXPAND(...) __VA_ARGS__
129 #define DO_STRUCT_DIFFERENCE_WITH_ARGS(struct_, extra_args_) \
130 PR_BEGIN_MACRO \
131 const nsStyle##struct_* this##struct_ = Style##struct_(); \
132 structsFound |= STYLE_STRUCT_BIT(struct_); \
134 const nsStyle##struct_* other##struct_ = aNewStyle.Style##struct_(); \
135 if (this##struct_ == other##struct_) { \
136 /* The very same struct, so we know that there will be no */ \
137 /* differences. */ \
138 *aEqualStructs |= STYLE_STRUCT_BIT(struct_); \
139 } else { \
140 nsChangeHint difference = \
141 this##struct_->CalcDifference(*other##struct_ EXPAND extra_args_); \
142 hint |= difference; \
143 if (!difference) { \
144 *aEqualStructs |= STYLE_STRUCT_BIT(struct_); \
147 styleStructCount++; \
148 PR_END_MACRO
149 #define DO_STRUCT_DIFFERENCE(struct_) \
150 DO_STRUCT_DIFFERENCE_WITH_ARGS(struct_, ())
152 // FIXME: The order of these DO_STRUCT_DIFFERENCE calls is no longer
153 // significant. With a small amount of effort, we could replace them with a
154 // #include "nsStyleStructList.h".
155 DO_STRUCT_DIFFERENCE_WITH_ARGS(Display, (, *StylePosition()));
156 DO_STRUCT_DIFFERENCE(XUL);
157 DO_STRUCT_DIFFERENCE(Column);
158 DO_STRUCT_DIFFERENCE(Content);
159 DO_STRUCT_DIFFERENCE(UI);
160 DO_STRUCT_DIFFERENCE(Visibility);
161 DO_STRUCT_DIFFERENCE(Outline);
162 DO_STRUCT_DIFFERENCE(TableBorder);
163 DO_STRUCT_DIFFERENCE(Table);
164 DO_STRUCT_DIFFERENCE(UIReset);
165 DO_STRUCT_DIFFERENCE(Text);
166 DO_STRUCT_DIFFERENCE_WITH_ARGS(List, (, *StyleDisplay()));
167 DO_STRUCT_DIFFERENCE(SVGReset);
168 DO_STRUCT_DIFFERENCE(SVG);
169 DO_STRUCT_DIFFERENCE_WITH_ARGS(Position, (, *StyleVisibility()));
170 DO_STRUCT_DIFFERENCE(Font);
171 DO_STRUCT_DIFFERENCE(Margin);
172 DO_STRUCT_DIFFERENCE(Padding);
173 DO_STRUCT_DIFFERENCE(Border);
174 DO_STRUCT_DIFFERENCE(TextReset);
175 DO_STRUCT_DIFFERENCE(Effects);
176 DO_STRUCT_DIFFERENCE(Background);
178 #undef DO_STRUCT_DIFFERENCE
179 #undef DO_STRUCT_DIFFERENCE_WITH_ARGS
180 #undef EXPAND
182 MOZ_ASSERT(styleStructCount == StyleStructConstants::kStyleStructCount,
183 "missing a call to DO_STRUCT_DIFFERENCE");
185 // Note that we do not check whether this->RelevantLinkVisited() !=
186 // aNewContext->RelevantLinkVisited(); we don't need to since
187 // nsCSSFrameConstructor::DoContentStateChanged always adds
188 // nsChangeHint_RepaintFrame for NS_EVENT_STATE_VISITED changes (and
189 // needs to, since HasStateDependentStyle probably doesn't work right
190 // for NS_EVENT_STATE_VISITED). Hopefully this doesn't actually
191 // expose whether links are visited to performance tests since all
192 // link coloring happens asynchronously at a time when it's hard for
193 // the page to measure.
194 // However, we do need to compute the larger of the changes that can
195 // happen depending on whether the link is visited or unvisited, since
196 // doing only the one that's currently appropriate would expose which
197 // links are in history to easy performance measurement. Therefore,
198 // here, we add nsChangeHint_RepaintFrame hints (the maximum for
199 // things that can depend on :visited) for the properties on which we
200 // call GetVisitedDependentColor.
201 const ComputedStyle* thisVis = GetStyleIfVisited();
202 const ComputedStyle* otherVis = aNewStyle.GetStyleIfVisited();
203 if (!thisVis != !otherVis) {
204 // One style has a style-if-visited and the other doesn't.
205 // Presume a difference.
206 #define STYLE_STRUCT(name_, fields_) *aEqualStructs &= ~STYLE_STRUCT_BIT(name_);
207 #include "nsCSSVisitedDependentPropList.h"
208 #undef STYLE_STRUCT
209 hint |= nsChangeHint_RepaintFrame;
210 } else if (thisVis) {
211 // Both styles have a style-if-visited.
212 bool change = false;
214 // NB: Calling Peek on |this|, not |thisVis|, since callers may look
215 // at a struct on |this| without looking at the same struct on
216 // |thisVis| (including this function if we skip one of these checks
217 // due to change being true already or due to the old style not having a
218 // style-if-visited), but not the other way around.
219 #define STYLE_FIELD(name_) thisVisStruct->name_ != otherVisStruct->name_
220 #define STYLE_STRUCT(name_, fields_) \
222 const nsStyle##name_* thisVisStruct = thisVis->Style##name_(); \
223 const nsStyle##name_* otherVisStruct = otherVis->Style##name_(); \
224 if (MOZ_FOR_EACH_SEPARATED(STYLE_FIELD, (||), (), fields_)) { \
225 *aEqualStructs &= ~STYLE_STRUCT_BIT(name_); \
226 change = true; \
229 #include "nsCSSVisitedDependentPropList.h"
230 #undef STYLE_STRUCT
231 #undef STYLE_FIELD
232 #undef STYLE_STRUCT_BIT
234 if (change) {
235 hint |= nsChangeHint_RepaintFrame;
239 if (hint & nsChangeHint_UpdateContainingBlock) {
240 if (!ContainingBlockMayHaveChanged(*this, aNewStyle)) {
241 // While some styles that cause the frame to be a containing block
242 // has changed, the overall result cannot have changed (no matter
243 // what the frame type is).
244 hint &= ~nsChangeHint_UpdateContainingBlock;
248 MOZ_ASSERT(NS_IsHintSubset(hint, nsChangeHint_AllHints),
249 "Added a new hint without bumping AllHints?");
250 return hint & ~nsChangeHint_NeutralChange;
253 #ifdef DEBUG
254 void ComputedStyle::List(FILE* out, int32_t aIndent) {
255 nsAutoCString str;
256 // Indent
257 int32_t ix;
258 for (ix = aIndent; --ix >= 0;) {
259 str.AppendLiteral(" ");
261 str.Append(nsPrintfCString("%p(%d) parent=%p ", (void*)this, 0, nullptr));
262 if (mPseudoType != PseudoStyleType::NotPseudo) {
263 str.Append(nsPrintfCString("%s ", ToString(mPseudoType).c_str()));
266 fprintf_stderr(out, "%s{ServoComputedData}\n", str.get());
268 #endif
270 template <typename Func>
271 static nscolor GetVisitedDependentColorInternal(const ComputedStyle& aStyle,
272 Func aColorFunc) {
273 nscolor colors[2];
274 colors[0] = aColorFunc(aStyle);
275 if (const ComputedStyle* visitedStyle = aStyle.GetStyleIfVisited()) {
276 colors[1] = aColorFunc(*visitedStyle);
277 return ComputedStyle::CombineVisitedColors(colors,
278 aStyle.RelevantLinkVisited());
280 return colors[0];
283 static nscolor ExtractColor(const ComputedStyle& aStyle,
284 const StyleRGBA& aColor) {
285 return aColor.ToColor();
288 static nscolor ExtractColor(const ComputedStyle& aStyle,
289 const StyleColor& aColor) {
290 return aColor.CalcColor(aStyle);
293 // Currently caret-color, the only property in the list which is a ColorOrAuto,
294 // always maps auto to currentcolor.
295 static nscolor ExtractColor(const ComputedStyle& aStyle,
296 const StyleColorOrAuto& aColor) {
297 if (aColor.IsAuto()) {
298 return ExtractColor(aStyle, StyleColor::CurrentColor());
300 return ExtractColor(aStyle, aColor.AsColor());
303 static nscolor ExtractColor(const ComputedStyle& aStyle,
304 const StyleSVGPaint& aPaintServer) {
305 return aPaintServer.kind.IsColor()
306 ? ExtractColor(aStyle, aPaintServer.kind.AsColor())
307 : NS_RGBA(0, 0, 0, 0);
310 #define STYLE_FIELD(struct_, field_) aField == &struct_::field_ ||
311 #define STYLE_STRUCT(name_, fields_) \
312 template <> \
313 nscolor ComputedStyle::GetVisitedDependentColor(decltype( \
314 nsStyle##name_::MOZ_ARG_1 fields_) nsStyle##name_::*aField) const { \
315 MOZ_ASSERT(MOZ_FOR_EACH(STYLE_FIELD, (nsStyle##name_, ), fields_) false, \
316 "Getting visited-dependent color for a field in nsStyle" #name_ \
317 " which is not listed in nsCSSVisitedDependentPropList.h"); \
318 return GetVisitedDependentColorInternal( \
319 *this, [aField](const ComputedStyle& aStyle) { \
320 return ExtractColor(aStyle, aStyle.Style##name_()->*aField); \
321 }); \
323 #include "nsCSSVisitedDependentPropList.h"
324 #undef STYLE_STRUCT
325 #undef STYLE_FIELD
327 struct ColorIndexSet {
328 uint8_t colorIndex, alphaIndex;
331 static const ColorIndexSet gVisitedIndices[2] = {{0, 0}, {1, 0}};
333 /* static */
334 nscolor ComputedStyle::CombineVisitedColors(nscolor* aColors,
335 bool aLinkIsVisited) {
336 if (NS_GET_A(aColors[1]) == 0) {
337 // If the style-if-visited is transparent, then just use the
338 // unvisited style rather than using the (meaningless) color
339 // components of the visited style along with a potentially
340 // non-transparent alpha value.
341 aLinkIsVisited = false;
344 // NOTE: We want this code to have as little timing dependence as
345 // possible on whether this->RelevantLinkVisited() is true.
346 const ColorIndexSet& set = gVisitedIndices[aLinkIsVisited ? 1 : 0];
348 nscolor colorColor = aColors[set.colorIndex];
349 nscolor alphaColor = aColors[set.alphaIndex];
350 return NS_RGBA(NS_GET_R(colorColor), NS_GET_G(colorColor),
351 NS_GET_B(colorColor), NS_GET_A(alphaColor));
354 #ifdef DEBUG
355 /* static */ const char* ComputedStyle::StructName(StyleStructID aSID) {
356 switch (aSID) {
357 # define STYLE_STRUCT(name_) \
358 case StyleStructID::name_: \
359 return #name_;
360 # include "nsStyleStructList.h"
361 # undef STYLE_STRUCT
362 default:
363 return "Unknown";
367 /* static */
368 Maybe<StyleStructID> ComputedStyle::LookupStruct(const nsACString& aName) {
369 # define STYLE_STRUCT(name_) \
370 if (aName.EqualsLiteral(#name_)) return Some(StyleStructID::name_);
371 # include "nsStyleStructList.h"
372 # undef STYLE_STRUCT
373 return Nothing();
375 #endif // DEBUG
377 ComputedStyle* ComputedStyle::GetCachedLazyPseudoStyle(
378 PseudoStyleType aPseudo) const {
379 MOZ_ASSERT(PseudoStyle::IsPseudoElement(aPseudo));
381 if (nsCSSPseudoElements::PseudoElementSupportsUserActionState(aPseudo)) {
382 return nullptr;
385 return mCachedInheritingStyles.Lookup(aPseudo);
388 MOZ_DEFINE_MALLOC_ENCLOSING_SIZE_OF(ServoComputedValuesMallocEnclosingSizeOf)
390 void ComputedStyle::AddSizeOfIncludingThis(nsWindowSizes& aSizes,
391 size_t* aCVsSize) const {
392 // Note: |this| sits within a servo_arc::Arc, i.e. it is preceded by a
393 // refcount. So we need to measure it with a function that can handle an
394 // interior pointer. We use ServoComputedValuesMallocEnclosingSizeOf to
395 // clearly identify in DMD's output the memory measured here.
396 *aCVsSize += ServoComputedValuesMallocEnclosingSizeOf(this);
397 mSource.AddSizeOfExcludingThis(aSizes);
398 mCachedInheritingStyles.AddSizeOfIncludingThis(aSizes, aCVsSize);
401 #ifdef DEBUG
402 bool ComputedStyle::EqualForCachedAnonymousContentStyle(
403 const ComputedStyle& aOther) const {
404 // One thing we can't add UA rules to prevent is different -x-lang
405 // values being inherited in. So we use this FFI function function rather
406 // than rely on CalcStyleDifference, which can't tell us which specific
407 // properties have changed.
408 return Servo_ComputedValues_EqualForCachedAnonymousContentStyle(this,
409 &aOther);
412 #endif
414 } // namespace mozilla