2 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2007 Rob Buis <buis@kde.org>
4 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
5 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
25 #include "core/rendering/svg/RenderSVGResource.h"
27 #include "core/frame/FrameView.h"
28 #include "core/frame/LocalFrame.h"
29 #include "core/rendering/svg/RenderSVGResourceClipper.h"
30 #include "core/rendering/svg/RenderSVGResourceFilter.h"
31 #include "core/rendering/svg/RenderSVGResourceMasker.h"
32 #include "core/rendering/svg/RenderSVGResourceSolidColor.h"
33 #include "core/rendering/svg/SVGResources.h"
34 #include "core/rendering/svg/SVGResourcesCache.h"
38 static inline bool inheritColorFromParentStyle(RenderObject* object, bool applyToFill, Color& color)
40 if (!object->parent() || !object->parent()->style())
42 const SVGRenderStyle* parentSVGStyle = object->parent()->style()->svgStyle();
43 SVGPaint::SVGPaintType paintType = applyToFill ? parentSVGStyle->fillPaintType() : parentSVGStyle->strokePaintType();
44 if (paintType != SVGPaint::SVG_PAINTTYPE_RGBCOLOR && paintType != SVGPaint::SVG_PAINTTYPE_RGBCOLOR_ICCCOLOR)
46 color = applyToFill ? parentSVGStyle->fillPaintColor() : parentSVGStyle->strokePaintColor();
50 static inline RenderSVGResource* requestPaintingResource(RenderSVGResourceMode mode, RenderObject* object, const RenderStyle* style, bool& hasFallback)
57 // If we have no style at all, ignore it.
58 const SVGRenderStyle* svgStyle = style->svgStyle();
62 bool isRenderingMask = false;
63 if (object->frame() && object->frame()->view())
64 isRenderingMask = object->frame()->view()->paintBehavior() & PaintBehaviorRenderingSVGMask;
66 // If we have no fill/stroke, return 0.
67 if (mode == ApplyToFillMode) {
68 // When rendering the mask for a RenderSVGResourceClipper, always use the initial fill paint server, and ignore stroke.
69 if (isRenderingMask) {
70 RenderSVGResourceSolidColor* colorResource = RenderSVGResource::sharedSolidPaintingResource();
71 colorResource->setColor(SVGRenderStyle::initialFillPaintColor());
75 if (!svgStyle->hasFill())
78 if (!svgStyle->hasStroke() || isRenderingMask)
82 bool applyToFill = mode == ApplyToFillMode;
83 SVGPaint::SVGPaintType paintType = applyToFill ? svgStyle->fillPaintType() : svgStyle->strokePaintType();
84 ASSERT(paintType != SVGPaint::SVG_PAINTTYPE_NONE);
87 bool hasColor = false;
89 case SVGPaint::SVG_PAINTTYPE_CURRENTCOLOR:
90 case SVGPaint::SVG_PAINTTYPE_RGBCOLOR:
91 case SVGPaint::SVG_PAINTTYPE_RGBCOLOR_ICCCOLOR:
92 case SVGPaint::SVG_PAINTTYPE_URI_CURRENTCOLOR:
93 case SVGPaint::SVG_PAINTTYPE_URI_RGBCOLOR:
94 case SVGPaint::SVG_PAINTTYPE_URI_RGBCOLOR_ICCCOLOR:
95 color = applyToFill ? svgStyle->fillPaintColor() : svgStyle->strokePaintColor();
101 if (style->insideLink() == InsideVisitedLink) {
102 // FIXME: This code doesn't support the uri component of the visited link paint, https://bugs.webkit.org/show_bug.cgi?id=70006
103 SVGPaint::SVGPaintType visitedPaintType = applyToFill ? svgStyle->visitedLinkFillPaintType() : svgStyle->visitedLinkStrokePaintType();
105 // For SVG_PAINTTYPE_CURRENTCOLOR, 'color' already contains the 'visitedColor'.
106 if (visitedPaintType < SVGPaint::SVG_PAINTTYPE_URI_NONE && visitedPaintType != SVGPaint::SVG_PAINTTYPE_CURRENTCOLOR) {
107 const Color& visitedColor = applyToFill ? svgStyle->visitedLinkFillPaintColor() : svgStyle->visitedLinkStrokePaintColor();
108 color = Color(visitedColor.red(), visitedColor.green(), visitedColor.blue(), color.alpha());
113 // If the primary resource is just a color, return immediately.
114 RenderSVGResourceSolidColor* colorResource = RenderSVGResource::sharedSolidPaintingResource();
115 if (paintType < SVGPaint::SVG_PAINTTYPE_URI_NONE) {
116 if (!hasColor && !inheritColorFromParentStyle(object, applyToFill, color))
119 colorResource->setColor(color);
120 return colorResource;
123 // If no resources are associated with the given renderer, return the color resource.
124 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(object);
126 if (paintType == SVGPaint::SVG_PAINTTYPE_URI_NONE || (!hasColor && !inheritColorFromParentStyle(object, applyToFill, color)))
129 colorResource->setColor(color);
130 return colorResource;
133 // If the requested resource is not available, return the color resource.
134 RenderSVGResource* uriResource = mode == ApplyToFillMode ? resources->fill() : resources->stroke();
136 if (!hasColor && !inheritColorFromParentStyle(object, applyToFill, color))
139 colorResource->setColor(color);
140 return colorResource;
143 // The paint server resource exists, though it may be invalid (pattern with width/height=0). Pass the fallback color to our caller
144 // via sharedSolidPaintingResource so it can use the solid color painting resource, if applyResource() on the URI resource failed.
146 colorResource->setColor(color);
152 RenderSVGResource* RenderSVGResource::fillPaintingResource(RenderObject* object, const RenderStyle* style, bool& hasFallback)
154 return requestPaintingResource(ApplyToFillMode, object, style, hasFallback);
157 RenderSVGResource* RenderSVGResource::strokePaintingResource(RenderObject* object, const RenderStyle* style, bool& hasFallback)
159 return requestPaintingResource(ApplyToStrokeMode, object, style, hasFallback);
162 RenderSVGResourceSolidColor* RenderSVGResource::sharedSolidPaintingResource()
164 static RenderSVGResourceSolidColor* s_sharedSolidPaintingResource = 0;
165 if (!s_sharedSolidPaintingResource)
166 s_sharedSolidPaintingResource = new RenderSVGResourceSolidColor;
167 return s_sharedSolidPaintingResource;
170 static inline void removeFromCacheAndInvalidateDependencies(RenderObject* object, bool needsLayout)
173 if (SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(object)) {
174 if (RenderSVGResourceFilter* filter = resources->filter())
175 filter->removeClientFromCache(object);
177 if (RenderSVGResourceMasker* masker = resources->masker())
178 masker->removeClientFromCache(object);
180 if (RenderSVGResourceClipper* clipper = resources->clipper())
181 clipper->removeClientFromCache(object);
184 if (!object->node() || !object->node()->isSVGElement())
186 HashSet<SVGElement*>* dependencies = object->document().accessSVGExtensions().setOfElementsReferencingTarget(toSVGElement(object->node()));
190 // We allow cycles in SVGDocumentExtensions reference sets in order to avoid expensive
191 // reference graph adjustments on changes, so we need to break possible cycles here.
192 DEFINE_STATIC_LOCAL(HashSet<SVGElement*>, invalidatingDependencies, ());
194 HashSet<SVGElement*>::iterator end = dependencies->end();
195 for (HashSet<SVGElement*>::iterator it = dependencies->begin(); it != end; ++it) {
196 if (RenderObject* renderer = (*it)->renderer()) {
197 if (UNLIKELY(!invalidatingDependencies.add(*it).isNewEntry)) {
198 // Reference cycle: we are in process of invalidating this dependant.
202 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer, needsLayout);
203 invalidatingDependencies.remove(*it);
208 void RenderSVGResource::markForLayoutAndParentResourceInvalidation(RenderObject* object, bool needsLayout)
211 ASSERT(object->node());
213 if (needsLayout && !object->documentBeingDestroyed())
214 object->setNeedsLayout();
216 removeFromCacheAndInvalidateDependencies(object, needsLayout);
218 // Invalidate resources in ancestor chain, if needed.
219 RenderObject* current = object->parent();
221 removeFromCacheAndInvalidateDependencies(current, needsLayout);
223 if (current->isSVGResourceContainer()) {
224 // This will process the rest of the ancestors.
225 toRenderSVGResourceContainer(current)->removeAllClientsFromCache();
229 current = current->parent();