2 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3 * 2008 Eric Seidel <eric@webkit.org>
4 * 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.
27 #include "RenderSVGResourceGradient.h"
29 #include "GradientAttributes.h"
30 #include "GraphicsContext.h"
31 #include "SVGRenderSupport.h"
32 #include <wtf/UnusedParam.h>
36 RenderSVGResourceGradient::RenderSVGResourceGradient(SVGGradientElement* node)
37 : RenderSVGResourceContainer(node)
44 RenderSVGResourceGradient::~RenderSVGResourceGradient()
46 deleteAllValues(m_gradient);
50 void RenderSVGResourceGradient::invalidateClients()
52 const HashMap<RenderObject*, GradientData*>::const_iterator end = m_gradient.end();
53 for (HashMap<RenderObject*, GradientData*>::const_iterator it = m_gradient.begin(); it != end; ++it)
54 markForLayoutAndResourceInvalidation(it->first);
56 deleteAllValues(m_gradient);
60 void RenderSVGResourceGradient::invalidateClient(RenderObject* object)
64 // FIXME: The HashMap should always contain the object on calling invalidateClient. A race condition
65 // during the parsing can causes a call of invalidateClient right before the call of applyResource.
66 // We return earlier for the moment. This bug should be fixed in:
67 // https://bugs.webkit.org/show_bug.cgi?id=35181
68 if (!m_gradient.contains(object))
71 delete m_gradient.take(object);
72 markForLayoutAndResourceInvalidation(object);
76 static inline AffineTransform absoluteTransformForRenderer(const RenderObject* object)
78 AffineTransform absoluteTransform;
80 const RenderObject* currentObject = object;
81 while (currentObject) {
82 absoluteTransform = currentObject->localToParentTransform() * absoluteTransform;
83 currentObject = currentObject->parent();
86 return absoluteTransform;
89 static inline bool createMaskAndSwapContextForTextGradient(GraphicsContext*& context,
90 GraphicsContext*& savedContext,
91 OwnPtr<ImageBuffer>& imageBuffer,
92 const RenderObject* object)
94 const RenderObject* textRootBlock = findTextRootObject(object);
96 AffineTransform transform = absoluteTransformForRenderer(textRootBlock);
97 FloatRect maskAbsoluteBoundingBox = transform.mapRect(textRootBlock->repaintRectInLocalCoordinates());
99 IntRect maskImageRect = enclosingIntRect(maskAbsoluteBoundingBox);
100 if (maskImageRect.isEmpty())
103 // Allocate an image buffer as big as the absolute unclipped size of the object
104 OwnPtr<ImageBuffer> maskImage = ImageBuffer::create(maskImageRect.size());
108 GraphicsContext* maskImageContext = maskImage->context();
110 // Transform the mask image coordinate system to absolute screen coordinates
111 maskImageContext->translate(-maskAbsoluteBoundingBox.x(), -maskAbsoluteBoundingBox.y());
112 maskImageContext->concatCTM(transform);
114 imageBuffer.set(maskImage.release());
115 savedContext = context;
116 context = maskImageContext;
121 static inline AffineTransform clipToTextMask(GraphicsContext* context,
122 OwnPtr<ImageBuffer>& imageBuffer,
123 const RenderObject* object,
124 GradientData* gradientData)
126 const RenderObject* textRootBlock = findTextRootObject(object);
127 context->clipToImageBuffer(textRootBlock->repaintRectInLocalCoordinates(), imageBuffer.get());
129 AffineTransform matrix;
130 if (gradientData->boundingBoxMode) {
131 FloatRect maskBoundingBox = textRootBlock->objectBoundingBox();
132 matrix.translate(maskBoundingBox.x(), maskBoundingBox.y());
133 matrix.scaleNonUniform(maskBoundingBox.width(), maskBoundingBox.height());
135 matrix.multiply(gradientData->transform);
140 bool RenderSVGResourceGradient::applyResource(RenderObject* object, RenderStyle* style, GraphicsContext*& context, unsigned short resourceMode)
145 ASSERT(resourceMode != ApplyToDefaultMode);
147 // Be sure to synchronize all SVG properties on the gradientElement _before_ processing any further.
148 // Otherwhise the call to collectGradientAttributes() in createTileImage(), may cause the SVG DOM property
149 // synchronization to kick in, which causes invalidateClients() to be called, which in turn deletes our
150 // GradientData object! Leaving out the line below will cause svg/dynamic-updates/SVG*GradientElement-svgdom* to crash.
151 SVGGradientElement* gradientElement = static_cast<SVGGradientElement*>(node());
152 if (!gradientElement)
155 gradientElement->updateAnimatedSVGAttribute(anyQName());
157 if (!m_gradient.contains(object))
158 m_gradient.set(object, new GradientData);
160 GradientData* gradientData = m_gradient.get(object);
162 bool isPaintingText = resourceMode & ApplyToTextMode;
164 // Create gradient object
165 if (!gradientData->gradient) {
166 buildGradient(gradientData, gradientElement);
168 // CG platforms will handle the gradient space transform for text after applying the
169 // resource, so don't apply it here. For non-CG platforms, we want the text bounding
170 // box applied to the gradient space transform now, so the gradient shader can use it.
172 if (gradientData->boundingBoxMode && !isPaintingText) {
174 if (gradientData->boundingBoxMode) {
176 FloatRect objectBoundingBox = object->objectBoundingBox();
177 gradientData->userspaceTransform.translate(objectBoundingBox.x(), objectBoundingBox.y());
178 gradientData->userspaceTransform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
181 gradientData->userspaceTransform.multiply(gradientData->transform);
182 gradientData->gradient->setGradientSpaceTransform(gradientData->userspaceTransform);
185 if (!gradientData->gradient)
191 if (isPaintingText) {
193 if (!createMaskAndSwapContextForTextGradient(context, m_savedContext, m_imageBuffer, object)) {
199 context->setTextDrawingMode(resourceMode & ApplyToFillMode ? cTextFill : cTextStroke);
202 const SVGRenderStyle* svgStyle = style->svgStyle();
205 if (resourceMode & ApplyToFillMode) {
206 context->setAlpha(svgStyle->fillOpacity());
207 context->setFillGradient(gradientData->gradient);
208 context->setFillRule(svgStyle->fillRule());
209 } else if (resourceMode & ApplyToStrokeMode) {
210 if (svgStyle->vectorEffect() == VE_NON_SCALING_STROKE)
211 gradientData->gradient->setGradientSpaceTransform(transformOnNonScalingStroke(object, gradientData->userspaceTransform));
212 context->setAlpha(svgStyle->strokeOpacity());
213 context->setStrokeGradient(gradientData->gradient);
214 applyStrokeStyleToContext(context, style, object);
220 void RenderSVGResourceGradient::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode)
223 ASSERT(resourceMode != ApplyToDefaultMode);
225 if (resourceMode & ApplyToTextMode) {
227 // CG requires special handling for gradient on text
228 if (m_savedContext && m_gradient.contains(object)) {
229 GradientData* gradientData = m_gradient.get(object);
231 // Restore on-screen drawing context
232 context = m_savedContext;
235 gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, object, gradientData));
236 context->setFillGradient(gradientData->gradient);
238 const RenderObject* textRootBlock = findTextRootObject(object);
239 context->fillRect(textRootBlock->repaintRectInLocalCoordinates());
241 m_imageBuffer.clear();
244 UNUSED_PARAM(object);
247 if (resourceMode & ApplyToFillMode)
249 else if (resourceMode & ApplyToStrokeMode)
250 context->strokePath();
256 void RenderSVGResourceGradient::addStops(GradientData* gradientData, const Vector<Gradient::ColorStop>& stops) const
258 ASSERT(gradientData->gradient);
260 const Vector<Gradient::ColorStop>::const_iterator end = stops.end();
261 for (Vector<Gradient::ColorStop>::const_iterator it = stops.begin(); it != end; ++it)
262 gradientData->gradient->addColorStop(*it);