OSDN Git Service

am 4b59a578: am 0a05678e: Cherry-pick security fix in WebKit change 65329
[android-x86/external-webkit.git] / WebCore / rendering / RenderSVGResourceGradient.cpp
1 /*
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.
6  *
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.
11  *
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.
16  *
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.
21  *
22  */
23
24 #include "config.h"
25
26 #if ENABLE(SVG)
27 #include "RenderSVGResourceGradient.h"
28
29 #include "GradientAttributes.h"
30 #include "GraphicsContext.h"
31 #include "SVGImageBufferTools.h"
32 #include "SVGRenderSupport.h"
33 #include <wtf/UnusedParam.h>
34
35 namespace WebCore {
36
37 RenderSVGResourceGradient::RenderSVGResourceGradient(SVGGradientElement* node)
38     : RenderSVGResourceContainer(node)
39 #if PLATFORM(CG)
40     , m_savedContext(0)
41 #endif
42 {
43 }
44
45 RenderSVGResourceGradient::~RenderSVGResourceGradient()
46 {
47     if (m_gradient.isEmpty())
48         return;
49
50     deleteAllValues(m_gradient);
51     m_gradient.clear();
52 }
53
54 void RenderSVGResourceGradient::removeAllClientsFromCache(bool markForInvalidation)
55 {
56     if (!m_gradient.isEmpty()) {
57         deleteAllValues(m_gradient);
58         m_gradient.clear();
59     }
60
61     markAllClientsForInvalidation(markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
62 }
63
64 void RenderSVGResourceGradient::removeClientFromCache(RenderObject* client, bool markForInvalidation)
65 {
66     ASSERT(client);
67
68     if (m_gradient.contains(client))
69         delete m_gradient.take(client);
70
71     markClientForInvalidation(client, markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
72 }
73
74 #if PLATFORM(CG)
75 static inline bool createMaskAndSwapContextForTextGradient(GraphicsContext*& context,
76                                                            GraphicsContext*& savedContext,
77                                                            OwnPtr<ImageBuffer>& imageBuffer,
78                                                            const RenderObject* object)
79 {
80     const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
81     ASSERT(textRootBlock);
82
83     AffineTransform absoluteTransform;
84     SVGImageBufferTools::calculateTransformationToOutermostSVGCoordinateSystem(textRootBlock, absoluteTransform);
85
86     FloatRect absoluteTargetRect = absoluteTransform.mapRect(textRootBlock->repaintRectInLocalCoordinates());
87     FloatRect clampedAbsoluteTargetRect = SVGImageBufferTools::clampedAbsoluteTargetRectForRenderer(textRootBlock, absoluteTargetRect);
88     if (clampedAbsoluteTargetRect.isEmpty())
89         return false;
90
91     OwnPtr<ImageBuffer> maskImage;
92     if (!SVGImageBufferTools::createImageBuffer(absoluteTargetRect, clampedAbsoluteTargetRect, maskImage, DeviceRGB))
93         return false;
94
95     GraphicsContext* maskImageContext = maskImage->context();
96     ASSERT(maskImageContext);
97
98     maskImageContext->translate(-clampedAbsoluteTargetRect.x(), -clampedAbsoluteTargetRect.y());
99     maskImageContext->concatCTM(absoluteTransform);
100
101     ASSERT(maskImage);
102     savedContext = context;
103     context = maskImageContext;
104     imageBuffer = maskImage.release();
105     return true;
106 }
107
108 static inline AffineTransform clipToTextMask(GraphicsContext* context,
109                                              OwnPtr<ImageBuffer>& imageBuffer,
110                                              FloatRect& targetRect,
111                                              const RenderObject* object,
112                                              GradientData* gradientData)
113 {
114     const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
115     ASSERT(textRootBlock);
116
117     targetRect = textRootBlock->repaintRectInLocalCoordinates();
118
119     AffineTransform absoluteTransform;
120     SVGImageBufferTools::calculateTransformationToOutermostSVGCoordinateSystem(textRootBlock, absoluteTransform);
121
122     FloatRect absoluteTargetRect = absoluteTransform.mapRect(targetRect);
123     FloatRect clampedAbsoluteTargetRect = SVGImageBufferTools::clampedAbsoluteTargetRectForRenderer(textRootBlock, absoluteTargetRect);
124
125     SVGImageBufferTools::clipToImageBuffer(context, absoluteTransform, clampedAbsoluteTargetRect, imageBuffer);
126
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());
132     }
133     matrix.multLeft(gradientData->transform);
134     return matrix;
135 }
136 #endif
137
138 bool RenderSVGResourceGradient::applyResource(RenderObject* object, RenderStyle* style, GraphicsContext*& context, unsigned short resourceMode)
139 {
140     ASSERT(object);
141     ASSERT(style);
142     ASSERT(context);
143     ASSERT(resourceMode != ApplyToDefaultMode);
144
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)
151         return false;
152
153     gradientElement->updateAnimatedSVGAttribute(anyQName());
154
155     if (!m_gradient.contains(object))
156         m_gradient.set(object, new GradientData);
157
158     GradientData* gradientData = m_gradient.get(object);
159
160     bool isPaintingText = resourceMode & ApplyToTextMode;
161
162     // Create gradient object
163     if (!gradientData->gradient) {
164         buildGradient(gradientData, gradientElement);
165
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.
169 #if PLATFORM(CG)
170         if (gradientData->boundingBoxMode && !isPaintingText) {
171 #else
172         if (gradientData->boundingBoxMode) {
173 #endif
174             FloatRect objectBoundingBox = object->objectBoundingBox();
175             gradientData->userspaceTransform.translate(objectBoundingBox.x(), objectBoundingBox.y());
176             gradientData->userspaceTransform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
177         }
178
179         gradientData->userspaceTransform.multLeft(gradientData->transform);
180         gradientData->gradient->setGradientSpaceTransform(gradientData->userspaceTransform);
181     }
182
183     if (!gradientData->gradient)
184         return false;
185
186     // Draw gradient
187     context->save();
188
189     if (isPaintingText) {
190 #if PLATFORM(CG)
191         if (!createMaskAndSwapContextForTextGradient(context, m_savedContext, m_imageBuffer, object)) {
192             context->restore();
193             return false;
194         }
195 #endif
196
197         context->setTextDrawingMode(resourceMode & ApplyToFillMode ? cTextFill : cTextStroke);
198     }
199
200     const SVGRenderStyle* svgStyle = style->svgStyle();
201     ASSERT(svgStyle);
202
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);
213     }
214
215     return true;
216 }
217
218 void RenderSVGResourceGradient::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode)
219 {
220     ASSERT(context);
221     ASSERT(resourceMode != ApplyToDefaultMode);
222
223     if (resourceMode & ApplyToTextMode) {
224 #if PLATFORM(CG)
225         // CG requires special handling for gradient on text
226         if (m_savedContext && m_gradient.contains(object)) {
227             GradientData* gradientData = m_gradient.get(object);
228
229             // Restore on-screen drawing context
230             context = m_savedContext;
231             m_savedContext = 0;
232
233             FloatRect targetRect;
234             gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, targetRect, object, gradientData));
235             context->setFillGradient(gradientData->gradient);
236
237             context->fillRect(targetRect);
238             m_imageBuffer.clear();
239         }
240 #else
241         UNUSED_PARAM(object);
242 #endif
243     } else {
244         if (resourceMode & ApplyToFillMode)
245             context->fillPath();
246         else if (resourceMode & ApplyToStrokeMode)
247             context->strokePath();
248     }
249
250     context->restore();
251 }
252
253 void RenderSVGResourceGradient::addStops(GradientData* gradientData, const Vector<Gradient::ColorStop>& stops) const
254 {
255     ASSERT(gradientData->gradient);
256
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);
260 }
261
262 }
263
264 #endif