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.
22 #include "core/rendering/svg/RenderSVGResourceContainer.h"
24 #include "core/rendering/RenderLayer.h"
25 #include "core/rendering/RenderView.h"
26 #include "core/rendering/svg/SVGRenderingContext.h"
27 #include "core/rendering/svg/SVGResourcesCache.h"
28 #include "core/svg/SVGGraphicsElement.h"
30 #include "wtf/TemporaryChange.h"
34 static inline SVGDocumentExtensions& svgExtensionsFromElement(SVGElement* element)
37 return element->document().accessSVGExtensions();
40 RenderSVGResourceContainer::RenderSVGResourceContainer(SVGElement* node)
41 : RenderSVGHiddenContainer(node)
43 , m_id(node->getIdAttribute())
44 , m_invalidationMask(0)
46 , m_isInvalidating(false)
50 RenderSVGResourceContainer::~RenderSVGResourceContainer()
54 void RenderSVGResourceContainer::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 RenderSVGHiddenContainer::layout().
58 ASSERT(needsLayout());
62 TemporaryChange<bool> inLayoutChange(m_isInLayout, true);
64 RenderSVGHiddenContainer::layout();
66 clearInvalidationMask();
69 void RenderSVGResourceContainer::willBeDestroyed()
71 SVGResourcesCache::resourceDestroyed(this);
72 RenderSVGHiddenContainer::willBeDestroyed();
74 svgExtensionsFromElement(element()).removeResource(m_id);
77 void RenderSVGResourceContainer::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
79 RenderSVGHiddenContainer::styleDidChange(diff, oldStyle);
87 void RenderSVGResourceContainer::idChanged()
89 // Invalidate all our current clients.
90 removeAllClientsFromCache();
92 // Remove old id, that is guaranteed to be present in cache.
93 SVGDocumentExtensions& extensions = svgExtensionsFromElement(element());
94 extensions.removeResource(m_id);
95 m_id = element()->getIdAttribute();
100 void RenderSVGResourceContainer::markAllClientsForInvalidation(InvalidationMode mode)
102 if ((m_clients.isEmpty() && m_clientLayers.isEmpty()) || m_isInvalidating)
105 if (m_invalidationMask & mode)
108 m_invalidationMask |= mode;
109 m_isInvalidating = true;
110 bool needsLayout = mode == LayoutAndBoundariesInvalidation;
111 bool markForInvalidation = mode != ParentOnlyInvalidation;
113 HashSet<RenderObject*>::iterator end = m_clients.end();
114 for (HashSet<RenderObject*>::iterator it = m_clients.begin(); it != end; ++it) {
115 RenderObject* client = *it;
116 if (client->isSVGResourceContainer()) {
117 toRenderSVGResourceContainer(client)->removeAllClientsFromCache(markForInvalidation);
121 if (markForInvalidation)
122 markClientForInvalidation(client, mode);
124 RenderSVGResource::markForLayoutAndParentResourceInvalidation(client, needsLayout);
127 markAllClientLayersForInvalidation();
129 m_isInvalidating = false;
132 void RenderSVGResourceContainer::markAllClientLayersForInvalidation()
134 HashSet<RenderLayer*>::iterator layerEnd = m_clientLayers.end();
135 for (HashSet<RenderLayer*>::iterator it = m_clientLayers.begin(); it != layerEnd; ++it)
136 (*it)->filterNeedsPaintInvalidation();
139 void RenderSVGResourceContainer::markClientForInvalidation(RenderObject* client, InvalidationMode mode)
142 ASSERT(!m_clients.isEmpty());
145 case LayoutAndBoundariesInvalidation:
146 case BoundariesInvalidation:
147 client->setNeedsBoundariesUpdate();
149 case PaintInvalidation:
150 client->setShouldDoFullPaintInvalidation(true);
152 case ParentOnlyInvalidation:
157 void RenderSVGResourceContainer::addClient(RenderObject* client)
160 m_clients.add(client);
161 clearInvalidationMask();
164 void RenderSVGResourceContainer::removeClient(RenderObject* client)
167 removeClientFromCache(client, false);
168 m_clients.remove(client);
171 void RenderSVGResourceContainer::addClientRenderLayer(Node* node)
174 if (!node->renderer() || !node->renderer()->hasLayer())
176 m_clientLayers.add(toRenderLayerModelObject(node->renderer())->layer());
177 clearInvalidationMask();
180 void RenderSVGResourceContainer::addClientRenderLayer(RenderLayer* client)
183 m_clientLayers.add(client);
184 clearInvalidationMask();
187 void RenderSVGResourceContainer::removeClientRenderLayer(RenderLayer* client)
190 m_clientLayers.remove(client);
193 void RenderSVGResourceContainer::invalidateCacheAndMarkForLayout(SubtreeLayoutScope* layoutScope)
195 if (selfNeedsLayout())
198 setNeedsLayoutAndFullPaintInvalidation(MarkContainingBlockChain, layoutScope);
201 removeAllClientsFromCache();
204 void RenderSVGResourceContainer::registerResource()
206 SVGDocumentExtensions& extensions = svgExtensionsFromElement(element());
207 if (!extensions.hasPendingResource(m_id)) {
208 extensions.addResource(m_id, this);
212 OwnPtrWillBeRawPtr<SVGDocumentExtensions::SVGPendingElements> clients(extensions.removePendingResource(m_id));
214 // Cache us with the new id.
215 extensions.addResource(m_id, this);
217 // Update cached resources of pending clients.
218 const SVGDocumentExtensions::SVGPendingElements::const_iterator end = clients->end();
219 for (SVGDocumentExtensions::SVGPendingElements::const_iterator it = clients->begin(); it != end; ++it) {
220 ASSERT((*it)->hasPendingResources());
221 extensions.clearHasPendingResourcesIfPossible(*it);
222 RenderObject* renderer = (*it)->renderer();
226 StyleDifference diff;
227 diff.setNeedsFullLayout();
228 SVGResourcesCache::clientStyleChanged(renderer, diff, renderer->style());
229 renderer->setNeedsLayoutAndFullPaintInvalidation();
233 static bool shouldTransformOnTextPainting(RenderObject* object, AffineTransform& resourceTransform)
237 // This method should only be called for RenderObjects that deal with text rendering. Cmp. RenderObject.h's is*() methods.
238 ASSERT(object->isSVGText() || object->isSVGTextPath() || object->isSVGInline());
240 // In text drawing, the scaling part of the graphics context CTM is removed, compare SVGInlineTextBox::paintTextWithShadows.
241 // So, we use that scaling factor here, too, and then push it down to pattern or gradient space
242 // in order to keep the pattern or gradient correctly scaled.
243 float scalingFactor = SVGRenderingContext::calculateScreenFontSizeScalingFactor(object);
244 if (scalingFactor == 1)
246 resourceTransform.scale(scalingFactor);
250 AffineTransform RenderSVGResourceContainer::computeResourceSpaceTransform(RenderObject* object, const AffineTransform& baseTransform, const SVGRenderStyle& svgStyle, unsigned short resourceMode)
252 AffineTransform computedSpaceTransform = baseTransform;
253 if (resourceMode & ApplyToTextMode) {
254 // Depending on the font scaling factor, we may need to apply an
255 // additional transform (scale-factor) the paintserver, since text
256 // painting removes the scale factor from the context. (See
257 // SVGInlineTextBox::paintTextWithShadows.)
258 AffineTransform additionalTextTransformation;
259 if (shouldTransformOnTextPainting(object, additionalTextTransformation))
260 computedSpaceTransform = additionalTextTransformation * computedSpaceTransform;
262 if (resourceMode & ApplyToStrokeMode) {
263 // Non-scaling stroke needs to reset the transform back to the host transform.
264 if (svgStyle.vectorEffect() == VE_NON_SCALING_STROKE)
265 computedSpaceTransform = transformOnNonScalingStroke(object, computedSpaceTransform);
267 return computedSpaceTransform;
270 // FIXME: This does not belong here.
271 AffineTransform RenderSVGResourceContainer::transformOnNonScalingStroke(RenderObject* object, const AffineTransform& resourceTransform)
273 if (!object->isSVGShape())
274 return resourceTransform;
276 SVGGraphicsElement* element = toSVGGraphicsElement(object->node());
277 AffineTransform transform = element->getScreenCTM(SVGGraphicsElement::DisallowStyleUpdate);
278 transform *= resourceTransform;