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(SVGImageBufferTools::absoluteTransformFromContext(context));
84 FloatRect absoluteTargetRect = absoluteTransform.mapRect(textRootBlock->repaintRectInLocalCoordinates());
86 OwnPtr<ImageBuffer> maskImage;
87 if (!SVGImageBufferTools::createImageBuffer(absoluteTransform, absoluteTargetRect, maskImage, DeviceRGB))
91 savedContext = context;
92 context = maskImage->context();
93 imageBuffer = maskImage.release();
97 static inline AffineTransform clipToTextMask(GraphicsContext* context,
98 OwnPtr<ImageBuffer>& imageBuffer,
99 FloatRect& repaintRect,
100 const RenderObject* object,
101 GradientData* gradientData)
103 const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
104 ASSERT(textRootBlock);
106 repaintRect = textRootBlock->repaintRectInLocalCoordinates();
108 AffineTransform absoluteTransform(SVGImageBufferTools::absoluteTransformFromContext(context));
109 FloatRect absoluteTargetRect = absoluteTransform.mapRect(repaintRect);
111 SVGImageBufferTools::clipToImageBuffer(context, absoluteTransform, absoluteTargetRect, imageBuffer.get());
113 AffineTransform matrix;
114 if (gradientData->boundingBoxMode) {
115 FloatRect maskBoundingBox = textRootBlock->objectBoundingBox();
116 matrix.translate(maskBoundingBox.x(), maskBoundingBox.y());
117 matrix.scaleNonUniform(maskBoundingBox.width(), maskBoundingBox.height());
119 matrix.multLeft(gradientData->transform);
124 bool RenderSVGResourceGradient::applyResource(RenderObject* object, RenderStyle* style, GraphicsContext*& context, unsigned short resourceMode)
129 ASSERT(resourceMode != ApplyToDefaultMode);
131 // Be sure to synchronize all SVG properties on the gradientElement _before_ processing any further.
132 // Otherwhise the call to collectGradientAttributes() in createTileImage(), may cause the SVG DOM property
133 // synchronization to kick in, which causes removeAllClientsFromCache() to be called, which in turn deletes our
134 // GradientData object! Leaving out the line below will cause svg/dynamic-updates/SVG*GradientElement-svgdom* to crash.
135 SVGGradientElement* gradientElement = static_cast<SVGGradientElement*>(node());
136 if (!gradientElement)
139 gradientElement->updateAnimatedSVGAttribute(anyQName());
141 if (!m_gradient.contains(object))
142 m_gradient.set(object, new GradientData);
144 GradientData* gradientData = m_gradient.get(object);
146 bool isPaintingText = resourceMode & ApplyToTextMode;
148 // Create gradient object
149 if (!gradientData->gradient) {
150 buildGradient(gradientData, gradientElement);
152 // CG platforms will handle the gradient space transform for text after applying the
153 // resource, so don't apply it here. For non-CG platforms, we want the text bounding
154 // box applied to the gradient space transform now, so the gradient shader can use it.
156 if (gradientData->boundingBoxMode && !isPaintingText) {
158 if (gradientData->boundingBoxMode) {
160 FloatRect objectBoundingBox = object->objectBoundingBox();
161 gradientData->userspaceTransform.translate(objectBoundingBox.x(), objectBoundingBox.y());
162 gradientData->userspaceTransform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
165 gradientData->userspaceTransform.multLeft(gradientData->transform);
166 gradientData->gradient->setGradientSpaceTransform(gradientData->userspaceTransform);
169 if (!gradientData->gradient)
175 if (isPaintingText) {
177 if (!createMaskAndSwapContextForTextGradient(context, m_savedContext, m_imageBuffer, object)) {
183 context->setTextDrawingMode(resourceMode & ApplyToFillMode ? cTextFill : cTextStroke);
186 const SVGRenderStyle* svgStyle = style->svgStyle();
189 if (resourceMode & ApplyToFillMode) {
190 context->setAlpha(svgStyle->fillOpacity());
191 context->setFillGradient(gradientData->gradient);
192 context->setFillRule(svgStyle->fillRule());
193 } else if (resourceMode & ApplyToStrokeMode) {
194 if (svgStyle->vectorEffect() == VE_NON_SCALING_STROKE)
195 gradientData->gradient->setGradientSpaceTransform(transformOnNonScalingStroke(object, gradientData->userspaceTransform));
196 context->setAlpha(svgStyle->strokeOpacity());
197 context->setStrokeGradient(gradientData->gradient);
198 SVGRenderSupport::applyStrokeStyleToContext(context, style, object);
204 void RenderSVGResourceGradient::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode)
207 ASSERT(resourceMode != ApplyToDefaultMode);
209 if (resourceMode & ApplyToTextMode) {
211 // CG requires special handling for gradient on text
212 if (m_savedContext && m_gradient.contains(object)) {
213 GradientData* gradientData = m_gradient.get(object);
215 // Restore on-screen drawing context
216 context = m_savedContext;
219 FloatRect repaintRect;
220 gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, repaintRect, object, gradientData));
221 context->setFillGradient(gradientData->gradient);
223 context->fillRect(repaintRect);
224 m_imageBuffer.clear();
227 UNUSED_PARAM(object);
230 if (resourceMode & ApplyToFillMode)
232 else if (resourceMode & ApplyToStrokeMode)
233 context->strokePath();
239 void RenderSVGResourceGradient::addStops(GradientData* gradientData, const Vector<Gradient::ColorStop>& stops) const
241 ASSERT(gradientData->gradient);
243 const Vector<Gradient::ColorStop>::const_iterator end = stops.end();
244 for (Vector<Gradient::ColorStop>::const_iterator it = stops.begin(); it != end; ++it)
245 gradientData->gradient->addColorStop(*it);