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 "SVGImageBufferTools.h"
32 #include "SVGRenderSupport.h"
33 #include <wtf/UnusedParam.h>
37 RenderSVGResourceGradient::RenderSVGResourceGradient(SVGGradientElement* node)
38 : RenderSVGResourceContainer(node)
45 RenderSVGResourceGradient::~RenderSVGResourceGradient()
47 if (m_gradient.isEmpty())
50 deleteAllValues(m_gradient);
54 void RenderSVGResourceGradient::removeAllClientsFromCache(bool markForInvalidation)
56 if (!m_gradient.isEmpty()) {
57 deleteAllValues(m_gradient);
61 markAllClientsForInvalidation(markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
64 void RenderSVGResourceGradient::removeClientFromCache(RenderObject* client, bool markForInvalidation)
68 if (m_gradient.contains(client))
69 delete m_gradient.take(client);
71 markClientForInvalidation(client, markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
75 static inline bool createMaskAndSwapContextForTextGradient(GraphicsContext*& context,
76 GraphicsContext*& savedContext,
77 OwnPtr<ImageBuffer>& imageBuffer,
78 const RenderObject* object)
80 const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
81 ASSERT(textRootBlock);
83 AffineTransform absoluteTransform;
84 SVGImageBufferTools::calculateTransformationToOutermostSVGCoordinateSystem(textRootBlock, absoluteTransform);
86 FloatRect absoluteTargetRect = absoluteTransform.mapRect(textRootBlock->repaintRectInLocalCoordinates());
87 FloatRect clampedAbsoluteTargetRect = SVGImageBufferTools::clampedAbsoluteTargetRectForRenderer(textRootBlock, absoluteTargetRect);
88 if (clampedAbsoluteTargetRect.isEmpty())
91 OwnPtr<ImageBuffer> maskImage;
92 if (!SVGImageBufferTools::createImageBuffer(absoluteTargetRect, clampedAbsoluteTargetRect, maskImage, DeviceRGB))
95 GraphicsContext* maskImageContext = maskImage->context();
96 ASSERT(maskImageContext);
98 maskImageContext->translate(-clampedAbsoluteTargetRect.x(), -clampedAbsoluteTargetRect.y());
99 maskImageContext->concatCTM(absoluteTransform);
102 savedContext = context;
103 context = maskImageContext;
104 imageBuffer = maskImage.release();
108 static inline AffineTransform clipToTextMask(GraphicsContext* context,
109 OwnPtr<ImageBuffer>& imageBuffer,
110 FloatRect& targetRect,
111 const RenderObject* object,
112 GradientData* gradientData)
114 const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
115 ASSERT(textRootBlock);
117 targetRect = textRootBlock->repaintRectInLocalCoordinates();
119 AffineTransform absoluteTransform;
120 SVGImageBufferTools::calculateTransformationToOutermostSVGCoordinateSystem(textRootBlock, absoluteTransform);
122 FloatRect absoluteTargetRect = absoluteTransform.mapRect(targetRect);
123 FloatRect clampedAbsoluteTargetRect = SVGImageBufferTools::clampedAbsoluteTargetRectForRenderer(textRootBlock, absoluteTargetRect);
125 SVGImageBufferTools::clipToImageBuffer(context, absoluteTransform, clampedAbsoluteTargetRect, imageBuffer);
127 AffineTransform matrix;
128 if (gradientData->boundingBoxMode) {
129 FloatRect maskBoundingBox = textRootBlock->objectBoundingBox();
130 matrix.translate(maskBoundingBox.x(), maskBoundingBox.y());
131 matrix.scaleNonUniform(maskBoundingBox.width(), maskBoundingBox.height());
133 matrix.multLeft(gradientData->transform);
138 bool RenderSVGResourceGradient::applyResource(RenderObject* object, RenderStyle* style, GraphicsContext*& context, unsigned short resourceMode)
143 ASSERT(resourceMode != ApplyToDefaultMode);
145 // Be sure to synchronize all SVG properties on the gradientElement _before_ processing any further.
146 // Otherwhise the call to collectGradientAttributes() in createTileImage(), may cause the SVG DOM property
147 // synchronization to kick in, which causes removeAllClientsFromCache() to be called, which in turn deletes our
148 // GradientData object! Leaving out the line below will cause svg/dynamic-updates/SVG*GradientElement-svgdom* to crash.
149 SVGGradientElement* gradientElement = static_cast<SVGGradientElement*>(node());
150 if (!gradientElement)
153 gradientElement->updateAnimatedSVGAttribute(anyQName());
155 if (!m_gradient.contains(object))
156 m_gradient.set(object, new GradientData);
158 GradientData* gradientData = m_gradient.get(object);
160 bool isPaintingText = resourceMode & ApplyToTextMode;
162 // Create gradient object
163 if (!gradientData->gradient) {
164 buildGradient(gradientData, gradientElement);
166 // CG platforms will handle the gradient space transform for text after applying the
167 // resource, so don't apply it here. For non-CG platforms, we want the text bounding
168 // box applied to the gradient space transform now, so the gradient shader can use it.
170 if (gradientData->boundingBoxMode && !isPaintingText) {
172 if (gradientData->boundingBoxMode) {
174 FloatRect objectBoundingBox = object->objectBoundingBox();
175 gradientData->userspaceTransform.translate(objectBoundingBox.x(), objectBoundingBox.y());
176 gradientData->userspaceTransform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
179 gradientData->userspaceTransform.multLeft(gradientData->transform);
180 gradientData->gradient->setGradientSpaceTransform(gradientData->userspaceTransform);
183 if (!gradientData->gradient)
189 if (isPaintingText) {
191 if (!createMaskAndSwapContextForTextGradient(context, m_savedContext, m_imageBuffer, object)) {
197 context->setTextDrawingMode(resourceMode & ApplyToFillMode ? cTextFill : cTextStroke);
200 const SVGRenderStyle* svgStyle = style->svgStyle();
203 if (resourceMode & ApplyToFillMode) {
204 context->setAlpha(svgStyle->fillOpacity());
205 context->setFillGradient(gradientData->gradient);
206 context->setFillRule(svgStyle->fillRule());
207 } else if (resourceMode & ApplyToStrokeMode) {
208 if (svgStyle->vectorEffect() == VE_NON_SCALING_STROKE)
209 gradientData->gradient->setGradientSpaceTransform(transformOnNonScalingStroke(object, gradientData->userspaceTransform));
210 context->setAlpha(svgStyle->strokeOpacity());
211 context->setStrokeGradient(gradientData->gradient);
212 SVGRenderSupport::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 FloatRect targetRect;
234 gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, targetRect, object, gradientData));
235 context->setFillGradient(gradientData->gradient);
237 context->fillRect(targetRect);
238 m_imageBuffer.clear();
241 UNUSED_PARAM(object);
244 if (resourceMode & ApplyToFillMode)
246 else if (resourceMode & ApplyToStrokeMode)
247 context->strokePath();
253 void RenderSVGResourceGradient::addStops(GradientData* gradientData, const Vector<Gradient::ColorStop>& stops) const
255 ASSERT(gradientData->gradient);
257 const Vector<Gradient::ColorStop>::const_iterator end = stops.end();
258 for (Vector<Gradient::ColorStop>::const_iterator it = stops.begin(); it != end; ++it)
259 gradientData->gradient->addColorStop(*it);