Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / layout / svg / LayoutSVGResourceContainer.cpp
blob8b46803e094506c34d4b7c6286ce423f1d943d8e
1 /*
2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include "config.h"
21 #include "core/layout/svg/LayoutSVGResourceContainer.h"
23 #include "core/layout/svg/LayoutSVGResourceClipper.h"
24 #include "core/layout/svg/LayoutSVGResourceFilter.h"
25 #include "core/layout/svg/LayoutSVGResourceMasker.h"
26 #include "core/layout/svg/SVGResources.h"
27 #include "core/layout/svg/SVGResourcesCache.h"
28 #include "core/paint/DeprecatedPaintLayer.h"
30 #include "wtf/TemporaryChange.h"
32 namespace blink {
34 static inline SVGDocumentExtensions& svgExtensionsFromElement(Element* element)
36 ASSERT(element);
37 return element->document().accessSVGExtensions();
40 LayoutSVGResourceContainer::LayoutSVGResourceContainer(SVGElement* node)
41 : LayoutSVGHiddenContainer(node)
42 , m_isInLayout(false)
43 , m_id(node->getIdAttribute())
44 , m_invalidationMask(0)
45 , m_registered(false)
46 , m_isInvalidating(false)
50 LayoutSVGResourceContainer::~LayoutSVGResourceContainer()
54 void LayoutSVGResourceContainer::layout()
56 // FIXME: Investigate a way to detect and break resource layout dependency cycles early.
57 // Then we can remove this method altogether, and fall back onto LayoutSVGHiddenContainer::layout().
58 ASSERT(needsLayout());
59 if (m_isInLayout)
60 return;
62 TemporaryChange<bool> inLayoutChange(m_isInLayout, true);
64 LayoutSVGHiddenContainer::layout();
66 clearInvalidationMask();
69 void LayoutSVGResourceContainer::willBeDestroyed()
71 // Detach all clients referring to this resource. If the resource itself is
72 // a client, it will be detached from any such resources by the call to
73 // LayoutSVGHiddenContainer::willBeDestroyed() below.
74 detachAllClients();
76 LayoutSVGHiddenContainer::willBeDestroyed();
77 if (m_registered)
78 svgExtensionsFromElement(element()).removeResource(m_id);
81 void LayoutSVGResourceContainer::styleDidChange(StyleDifference diff, const ComputedStyle* oldStyle)
83 LayoutSVGHiddenContainer::styleDidChange(diff, oldStyle);
85 if (!m_registered) {
86 m_registered = true;
87 registerResource();
91 void LayoutSVGResourceContainer::detachAllClients()
93 for (auto* client : m_clients) {
94 // Unlink the resource from the client's SVGResources. (The actual
95 // removal will be signaled after processing all the clients.)
96 SVGResources* resources = SVGResourcesCache::cachedResourcesForLayoutObject(client);
97 ASSERT(resources); // Or else the client wouldn't be in the list in the first place.
98 resources->resourceDestroyed(this);
100 // Add a pending resolution based on the id of the old resource.
101 Element* clientElement = toElement(client->node());
102 svgExtensionsFromElement(clientElement).addPendingResource(m_id, clientElement);
105 removeAllClientsFromCache();
108 void LayoutSVGResourceContainer::idChanged()
110 // Invalidate all our current clients.
111 removeAllClientsFromCache();
113 // Remove old id, that is guaranteed to be present in cache.
114 SVGDocumentExtensions& extensions = svgExtensionsFromElement(element());
115 extensions.removeResource(m_id);
116 m_id = element()->getIdAttribute();
118 registerResource();
121 void LayoutSVGResourceContainer::markAllClientsForInvalidation(InvalidationMode mode)
123 if ((m_clients.isEmpty() && m_clientLayers.isEmpty()) || m_isInvalidating)
124 return;
126 if (m_invalidationMask & mode)
127 return;
129 m_invalidationMask |= mode;
130 m_isInvalidating = true;
131 bool needsLayout = mode == LayoutAndBoundariesInvalidation;
132 bool markForInvalidation = mode != ParentOnlyInvalidation;
134 for (auto* client : m_clients) {
135 if (client->isSVGResourceContainer()) {
136 toLayoutSVGResourceContainer(client)->removeAllClientsFromCache(markForInvalidation);
137 continue;
140 if (markForInvalidation)
141 markClientForInvalidation(client, mode);
143 LayoutSVGResourceContainer::markForLayoutAndParentResourceInvalidation(client, needsLayout);
146 markAllClientLayersForInvalidation();
148 m_isInvalidating = false;
151 void LayoutSVGResourceContainer::markAllClientLayersForInvalidation()
153 for (auto* layer : m_clientLayers)
154 layer->filterNeedsPaintInvalidation();
157 void LayoutSVGResourceContainer::markClientForInvalidation(LayoutObject* client, InvalidationMode mode)
159 ASSERT(client);
160 ASSERT(!m_clients.isEmpty());
162 switch (mode) {
163 case LayoutAndBoundariesInvalidation:
164 case BoundariesInvalidation:
165 client->setNeedsBoundariesUpdate();
166 break;
167 case PaintInvalidation:
168 // Since LayoutSVGInlineTexts don't have SVGResources (they use their
169 // parent's), they will not be notified of changes to paint servers. So
170 // if the client is one that could have a LayoutSVGInlineText use a
171 // paint invalidation reason that will force paint invalidation of the
172 // entire <text>/<tspan>/... subtree.
173 client->setShouldDoFullPaintInvalidation(PaintInvalidationSVGResourceChange);
174 break;
175 case ParentOnlyInvalidation:
176 break;
180 void LayoutSVGResourceContainer::addClient(LayoutObject* client)
182 ASSERT(client);
183 m_clients.add(client);
184 clearInvalidationMask();
187 void LayoutSVGResourceContainer::removeClient(LayoutObject* client)
189 ASSERT(client);
190 removeClientFromCache(client, false);
191 m_clients.remove(client);
194 void LayoutSVGResourceContainer::addClientLayer(Node* node)
196 ASSERT(node);
197 if (!node->layoutObject() || !node->layoutObject()->hasLayer())
198 return;
199 m_clientLayers.add(toLayoutBoxModelObject(node->layoutObject())->layer());
200 clearInvalidationMask();
203 void LayoutSVGResourceContainer::addClientLayer(DeprecatedPaintLayer* client)
205 ASSERT(client);
206 m_clientLayers.add(client);
207 clearInvalidationMask();
210 void LayoutSVGResourceContainer::removeClientLayer(DeprecatedPaintLayer* client)
212 ASSERT(client);
213 m_clientLayers.remove(client);
216 void LayoutSVGResourceContainer::invalidateCacheAndMarkForLayout(SubtreeLayoutScope* layoutScope)
218 if (selfNeedsLayout())
219 return;
221 setNeedsLayoutAndFullPaintInvalidation(LayoutInvalidationReason::SvgResourceInvalidated, MarkContainerChain, layoutScope);
223 if (everHadLayout())
224 removeAllClientsFromCache();
227 void LayoutSVGResourceContainer::registerResource()
229 SVGDocumentExtensions& extensions = svgExtensionsFromElement(element());
230 if (!extensions.hasPendingResource(m_id)) {
231 extensions.addResource(m_id, this);
232 return;
235 OwnPtrWillBeRawPtr<SVGDocumentExtensions::SVGPendingElements> clients(extensions.removePendingResource(m_id));
237 // Cache us with the new id.
238 extensions.addResource(m_id, this);
240 // Update cached resources of pending clients.
241 const SVGDocumentExtensions::SVGPendingElements::const_iterator end = clients->end();
242 for (SVGDocumentExtensions::SVGPendingElements::const_iterator it = clients->begin(); it != end; ++it) {
243 ASSERT((*it)->hasPendingResources());
244 extensions.clearHasPendingResourcesIfPossible(*it);
245 LayoutObject* layoutObject = (*it)->layoutObject();
246 if (!layoutObject)
247 continue;
249 // If the client has a layer (is a non-SVGElement) we need to signal
250 // invalidation in the same way as is done in markAllClientLayersForInvalidation above.
251 if (layoutObject->hasLayer() && resourceType() == FilterResourceType) {
252 toLayoutBoxModelObject(layoutObject)->layer()->filterNeedsPaintInvalidation();
253 continue;
256 StyleDifference diff;
257 diff.setNeedsFullLayout();
258 SVGResourcesCache::clientStyleChanged(layoutObject, diff, layoutObject->styleRef());
259 layoutObject->setNeedsLayoutAndFullPaintInvalidation(LayoutInvalidationReason::SvgResourceInvalidated);
263 static inline void removeFromCacheAndInvalidateDependencies(LayoutObject* object, bool needsLayout)
265 ASSERT(object);
266 if (SVGResources* resources = SVGResourcesCache::cachedResourcesForLayoutObject(object)) {
267 if (LayoutSVGResourceFilter* filter = resources->filter())
268 filter->removeClientFromCache(object);
270 if (LayoutSVGResourceMasker* masker = resources->masker())
271 masker->removeClientFromCache(object);
273 if (LayoutSVGResourceClipper* clipper = resources->clipper())
274 clipper->removeClientFromCache(object);
277 if (!object->node() || !object->node()->isSVGElement())
278 return;
280 SVGElementSet* dependencies = toSVGElement(object->node())->setOfIncomingReferences();
281 if (!dependencies)
282 return;
284 // We allow cycles in SVGDocumentExtensions reference sets in order to avoid expensive
285 // reference graph adjustments on changes, so we need to break possible cycles here.
286 // This strong reference is safe, as it is guaranteed that this set will be emptied
287 // at the end of recursion.
288 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<SVGElementSet>, invalidatingDependencies, (adoptPtrWillBeNoop(new SVGElementSet)));
290 for (SVGElement* element : *dependencies) {
291 if (LayoutObject* layoutObject = element->layoutObject()) {
292 if (UNLIKELY(!invalidatingDependencies->add(element).isNewEntry)) {
293 // Reference cycle: we are in process of invalidating this dependant.
294 continue;
297 LayoutSVGResourceContainer::markForLayoutAndParentResourceInvalidation(layoutObject, needsLayout);
298 invalidatingDependencies->remove(element);
303 void LayoutSVGResourceContainer::markForLayoutAndParentResourceInvalidation(LayoutObject* object, bool needsLayout)
305 ASSERT(object);
306 ASSERT(object->node());
308 if (needsLayout && !object->documentBeingDestroyed())
309 object->setNeedsLayoutAndFullPaintInvalidation(LayoutInvalidationReason::SvgResourceInvalidated);
311 removeFromCacheAndInvalidateDependencies(object, needsLayout);
313 // Invalidate resources in ancestor chain, if needed.
314 LayoutObject* current = object->parent();
315 while (current) {
316 removeFromCacheAndInvalidateDependencies(current, needsLayout);
318 if (current->isSVGResourceContainer()) {
319 // This will process the rest of the ancestors.
320 toLayoutSVGResourceContainer(current)->removeAllClientsFromCache();
321 break;
324 current = current->parent();