1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc.
6 * All rights reserved.
7 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
8 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
9 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
10 * (http://www.torchmobile.com/)
11 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
12 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
13 * Copyright (C) 2013 Google Inc. All rights reserved.
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Library General Public
17 * License as published by the Free Software Foundation; either
18 * version 2 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Library General Public License for more details.
24 *
25 * You should have received a copy of the GNU Library General Public License
26 * along with this library; see the file COPYING.LIB. If not, write to
27 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 * Boston, MA 02110-1301, USA.
29 */
30
31 #include "third_party/blink/renderer/core/css/resolver/matched_properties_cache.h"
32
33 #include "third_party/blink/renderer/core/css/css_property_value_set.h"
34 #include "third_party/blink/renderer/core/css/properties/css_property_ref.h"
35 #include "third_party/blink/renderer/core/css/resolver/style_resolver_state.h"
36 #include "third_party/blink/renderer/core/style/computed_style.h"
37 #include "third_party/blink/renderer/platform/runtime_enabled_features.h"
38 #include "third_party/blink/renderer/platform/wtf/text/string_hasher.h"
39
40 namespace blink {
41
ComputeMatchedPropertiesHash(const MatchResult & result)42 static unsigned ComputeMatchedPropertiesHash(const MatchResult& result) {
43 const MatchedPropertiesVector& vector = result.GetMatchedProperties();
44 return StringHasher::HashMemory(vector.data(),
45 sizeof(MatchedProperties) * vector.size());
46 }
47
Set(const ComputedStyle & style,const ComputedStyle & parent_style,const MatchedPropertiesVector & properties,const HashSet<CSSPropertyName> & new_dependencies)48 void CachedMatchedProperties::Set(
49 const ComputedStyle& style,
50 const ComputedStyle& parent_style,
51 const MatchedPropertiesVector& properties,
52 const HashSet<CSSPropertyName>& new_dependencies) {
53 for (const auto& new_matched_properties : properties) {
54 matched_properties.push_back(new_matched_properties.properties);
55 matched_properties_types.push_back(new_matched_properties.types_);
56 }
57
58 // Note that we don't cache the original ComputedStyle instance. It may be
59 // further modified. The ComputedStyle in the cache is really just a holder
60 // for the substructures and never used as-is.
61 this->computed_style = ComputedStyle::Clone(style);
62 this->parent_computed_style = ComputedStyle::Clone(parent_style);
63
64 DCHECK(
65 RuntimeEnabledFeatures::CSSMatchedPropertiesCacheDependenciesEnabled() ||
66 new_dependencies.IsEmpty());
67 if (new_dependencies.size()) {
68 DCHECK(new_dependencies.size() <= StyleResolverState::kMaxDependencies);
69 // Plus one for g_null_atom.
70 dependencies =
71 std::make_unique<AtomicString[]>(new_dependencies.size() + 1);
72
73 size_t index = 0;
74 for (const CSSPropertyName& name : new_dependencies) {
75 DCHECK_LT(index, new_dependencies.size());
76 dependencies[index++] = name.ToAtomicString();
77 }
78 DCHECK_EQ(index, new_dependencies.size());
79 dependencies[index] = g_null_atom;
80 }
81 }
82
Clear()83 void CachedMatchedProperties::Clear() {
84 matched_properties.clear();
85 matched_properties_types.clear();
86 computed_style = nullptr;
87 parent_computed_style = nullptr;
88 dependencies.reset();
89 }
90
DependenciesEqual(const StyleResolverState & state)91 bool CachedMatchedProperties::DependenciesEqual(
92 const StyleResolverState& state) {
93 if (!state.ParentStyle())
94 return false;
95 if ((parent_computed_style->IsEnsuredInDisplayNone() ||
96 computed_style->IsEnsuredOutsideFlatTree()) &&
97 !state.ParentStyle()->IsEnsuredInDisplayNone() &&
98 !state.Style()->IsEnsuredOutsideFlatTree()) {
99 // If we cached a ComputedStyle in a display:none subtree, or outside the
100 // flat tree, we would not have triggered fetches for external resources
101 // and have StylePendingImages in the ComputedStyle. Instead of having to
102 // inspect the cached ComputedStyle for such resources, don't use a cached
103 // ComputedStyle when it was cached in display:none but is now rendered.
104 return false;
105 }
106
107 for (const AtomicString* name = dependencies.get(); name && !name->IsNull();
108 name++) {
109 CSSPropertyRef ref(*name, state.GetDocument());
110 DCHECK(ref.IsValid());
111 if (!ref.GetProperty().ComputedValuesEqual(*parent_computed_style,
112 *state.ParentStyle())) {
113 return false;
114 }
115 }
116
117 return true;
118 }
119
120 MatchedPropertiesCache::MatchedPropertiesCache() = default;
121
Key(const MatchResult & result)122 MatchedPropertiesCache::Key::Key(const MatchResult& result)
123 : Key(result,
124 result.IsCacheable() ? ComputeMatchedPropertiesHash(result)
125 : HashTraits<unsigned>::EmptyValue()) {}
126
Key(const MatchResult & result,unsigned hash)127 MatchedPropertiesCache::Key::Key(const MatchResult& result, unsigned hash)
128 : result_(result), hash_(hash) {}
129
Find(const Key & key,const StyleResolverState & style_resolver_state)130 const CachedMatchedProperties* MatchedPropertiesCache::Find(
131 const Key& key,
132 const StyleResolverState& style_resolver_state) {
133 DCHECK(key.IsValid());
134 Cache::iterator it = cache_.find(key.hash_);
135 if (it == cache_.end())
136 return nullptr;
137 CachedMatchedProperties* cache_item = it->value.Get();
138 if (!cache_item)
139 return nullptr;
140 if (*cache_item != key.result_.GetMatchedProperties())
141 return nullptr;
142 if (cache_item->computed_style->InsideLink() !=
143 style_resolver_state.Style()->InsideLink())
144 return nullptr;
145 if (!cache_item->DependenciesEqual(style_resolver_state))
146 return nullptr;
147 return cache_item;
148 }
149
operator ==(const MatchedPropertiesVector & properties)150 bool CachedMatchedProperties::operator==(
151 const MatchedPropertiesVector& properties) {
152 if (properties.size() != matched_properties.size())
153 return false;
154 for (wtf_size_t i = 0; i < properties.size(); ++i) {
155 if (properties[i].properties != matched_properties[i])
156 return false;
157 if (properties[i].types_.link_match_type !=
158 matched_properties_types[i].link_match_type)
159 return false;
160 if (properties[i].types_.tree_order !=
161 matched_properties_types[i].tree_order)
162 return false;
163 if (properties[i].types_.valid_property_filter !=
164 matched_properties_types[i].valid_property_filter)
165 return false;
166 }
167 return true;
168 }
169
operator !=(const MatchedPropertiesVector & properties)170 bool CachedMatchedProperties::operator!=(
171 const MatchedPropertiesVector& properties) {
172 return !(*this == properties);
173 }
174
Add(const Key & key,const ComputedStyle & style,const ComputedStyle & parent_style,const HashSet<CSSPropertyName> & dependencies)175 void MatchedPropertiesCache::Add(const Key& key,
176 const ComputedStyle& style,
177 const ComputedStyle& parent_style,
178 const HashSet<CSSPropertyName>& dependencies) {
179 DCHECK(key.IsValid());
180 Cache::AddResult add_result = cache_.insert(key.hash_, nullptr);
181 if (add_result.is_new_entry || !add_result.stored_value->value) {
182 add_result.stored_value->value =
183 MakeGarbageCollected<CachedMatchedProperties>();
184 }
185
186 CachedMatchedProperties* cache_item = add_result.stored_value->value.Get();
187 if (!add_result.is_new_entry)
188 cache_item->Clear();
189
190 cache_item->Set(style, parent_style, key.result_.GetMatchedProperties(),
191 dependencies);
192 }
193
Clear()194 void MatchedPropertiesCache::Clear() {
195 // MatchedPropertiesCache must be cleared promptly because some
196 // destructors in the properties (e.g., ~FontFallbackList) expect that
197 // the destructors are called promptly without relying on a GC timing.
198 for (auto& cache_entry : cache_) {
199 if (cache_entry.value)
200 cache_entry.value->Clear();
201 }
202 cache_.clear();
203 }
204
ClearViewportDependent()205 void MatchedPropertiesCache::ClearViewportDependent() {
206 Vector<unsigned, 16> to_remove;
207 for (const auto& cache_entry : cache_) {
208 CachedMatchedProperties* cache_item = cache_entry.value.Get();
209 if (cache_item && cache_item->computed_style->HasViewportUnits())
210 to_remove.push_back(cache_entry.key);
211 }
212 cache_.RemoveAll(to_remove);
213 }
214
IsStyleCacheable(const ComputedStyle & style)215 bool MatchedPropertiesCache::IsStyleCacheable(const ComputedStyle& style) {
216 // Content property with attr() values depend on the attribute value of the
217 // originating element, thus we cannot cache based on the matched properties
218 // because the value of content is retrieved from the attribute at apply time.
219 if (style.HasAttrContent())
220 return false;
221 if (style.Zoom() != ComputedStyleInitialValues::InitialZoom())
222 return false;
223 if (style.TextAutosizingMultiplier() != 1)
224 return false;
225 if (!RuntimeEnabledFeatures::CSSMatchedPropertiesCacheDependenciesEnabled()) {
226 if (style.GetWritingMode() !=
227 ComputedStyleInitialValues::InitialWritingMode() ||
228 style.Direction() != ComputedStyleInitialValues::InitialDirection()) {
229 return false;
230 }
231
232 // styles with non inherited properties that reference variables are not
233 // cacheable.
234 if (style.HasVariableReferenceFromNonInheritedProperty())
235 return false;
236 }
237 // -internal-light-dark() values in UA sheets have different computed values
238 // based on the used value of color-scheme.
239 if (style.HasNonInheritedLightDarkValue())
240 return false;
241 return true;
242 }
243
IsCacheable(const StyleResolverState & state)244 bool MatchedPropertiesCache::IsCacheable(const StyleResolverState& state) {
245 const ComputedStyle& style = *state.Style();
246 const ComputedStyle& parent_style = *state.ParentStyle();
247
248 if (!IsStyleCacheable(style))
249 return false;
250
251 if (!RuntimeEnabledFeatures::CSSMatchedPropertiesCacheDependenciesEnabled()) {
252 // The cache assumes static knowledge about which properties are inherited.
253 // Without a flat tree parent, StyleBuilder::ApplyProperty will not
254 // SetChildHasExplicitInheritance on the parent style.
255 if (!state.ParentNode() || parent_style.ChildHasExplicitInheritance())
256 return false;
257 return true;
258 }
259
260 return state.HasValidDependencies() && !state.HasIncomparableDependency();
261 }
262
Trace(Visitor * visitor) const263 void MatchedPropertiesCache::Trace(Visitor* visitor) const {
264 visitor->Trace(cache_);
265 visitor->RegisterWeakCallbackMethod<
266 MatchedPropertiesCache,
267 &MatchedPropertiesCache::RemoveCachedMatchedPropertiesWithDeadEntries>(
268 this);
269 }
270
RemoveCachedMatchedPropertiesWithDeadEntries(const LivenessBroker & info)271 void MatchedPropertiesCache::RemoveCachedMatchedPropertiesWithDeadEntries(
272 const LivenessBroker& info) {
273 Vector<unsigned> to_remove;
274 for (const auto& entry_pair : cache_) {
275 // A nullptr value indicates that the entry is currently being created; see
276 // |MatchedPropertiesCache::Add|. Keep such entries.
277 if (!entry_pair.value)
278 continue;
279 for (const auto& matched_properties :
280 entry_pair.value->matched_properties) {
281 if (!info.IsHeapObjectAlive(matched_properties)) {
282 to_remove.push_back(entry_pair.key);
283 break;
284 }
285 }
286 }
287 // Allocation is forbidden during executing weak callbacks, so the data
288 // structure will not be rehashed here. The next insertion/deletion from
289 // regular code will take care of shrinking accordingly.
290 cache_.RemoveAll(to_remove);
291 }
292
293 } // namespace blink
294