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 // Create gradient object
163 if (!gradientData->gradient)
164 buildGradient(gradientData, gradientElement);
166 if (!gradientData->gradient)
172 bool isPaintingText = resourceMode & ApplyToTextMode;
173 if (isPaintingText) {
175 if (!createMaskAndSwapContextForTextGradient(context, m_savedContext, m_imageBuffer, object)) {
181 context->setTextDrawingMode(resourceMode & ApplyToFillMode ? cTextFill : cTextStroke);
184 AffineTransform transform;
186 // CG platforms will handle the gradient space transform for text after applying the
187 // resource, so don't apply it here. For non-CG platforms, we want the text bounding
188 // box applied to the gradient space transform now, so the gradient shader can use it.
190 if (gradientData->boundingBoxMode && !isPaintingText) {
192 if (gradientData->boundingBoxMode) {
194 FloatRect objectBoundingBox = object->objectBoundingBox();
195 transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
196 transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
199 transform.multiply(gradientData->transform);
200 gradientData->gradient->setGradientSpaceTransform(transform);
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 context->setAlpha(svgStyle->strokeOpacity());
211 context->setStrokeGradient(gradientData->gradient);
212 applyStrokeStyleToContext(context, style, object);
218 void RenderSVGResourceGradient::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode)
221 ASSERT(resourceMode != ApplyToDefaultMode);
223 if (resourceMode & ApplyToTextMode) {
225 // CG requires special handling for gradient on text
226 if (m_savedContext && m_gradient.contains(object)) {
227 GradientData* gradientData = m_gradient.get(object);
229 // Restore on-screen drawing context
230 context = m_savedContext;
233 gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, object, gradientData));
234 context->setFillGradient(gradientData->gradient);
236 const RenderObject* textRootBlock = findTextRootObject(object);
237 context->fillRect(textRootBlock->repaintRectInLocalCoordinates());
239 m_imageBuffer.clear();
242 UNUSED_PARAM(object);
245 if (resourceMode & ApplyToFillMode)
247 else if (resourceMode & ApplyToStrokeMode)
248 context->strokePath();
254 void RenderSVGResourceGradient::addStops(GradientData* gradientData, const Vector<Gradient::ColorStop>& stops) const
256 ASSERT(gradientData->gradient);
258 const Vector<Gradient::ColorStop>::const_iterator end = stops.end();
259 for (Vector<Gradient::ColorStop>::const_iterator it = stops.begin(); it != end; ++it)
260 gradientData->gradient->addColorStop(*it);