OSDN Git Service

Merge "Crash fix, don't delete WebUrlLoaderClient before WebRequest is finished."
[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 "SVGRenderSupport.h"
32 #include <wtf/UnusedParam.h>
33
34 namespace WebCore {
35
36 RenderSVGResourceGradient::RenderSVGResourceGradient(SVGGradientElement* node)
37     : RenderSVGResourceContainer(node)
38 #if PLATFORM(CG)
39     , m_savedContext(0)
40 #endif
41 {
42 }
43
44 RenderSVGResourceGradient::~RenderSVGResourceGradient()
45 {
46     deleteAllValues(m_gradient);
47     m_gradient.clear();
48 }
49
50 void RenderSVGResourceGradient::invalidateClients()
51 {
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, false);
55
56     deleteAllValues(m_gradient);
57     m_gradient.clear();
58 }
59
60 void RenderSVGResourceGradient::invalidateClient(RenderObject* object)
61 {
62     ASSERT(object);
63     if (!m_gradient.contains(object))
64         return;
65
66     delete m_gradient.take(object);
67     markForLayoutAndResourceInvalidation(object, false);
68 }
69
70 #if PLATFORM(CG)
71 static inline AffineTransform absoluteTransformFromContext(GraphicsContext* context)
72 {
73     // Extract current transformation matrix used in the original context. Note that this coordinate
74     // system is flipped compared to SVGs internal coordinate system, done in WebKit level. Fix
75     // this transformation by flipping the y component.
76     return context->getCTM() * AffineTransform().flipY();
77 }
78
79 static inline bool createMaskAndSwapContextForTextGradient(GraphicsContext*& context,
80                                                            GraphicsContext*& savedContext,
81                                                            OwnPtr<ImageBuffer>& imageBuffer,
82                                                            const RenderObject* object)
83 {
84     const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
85
86     AffineTransform transform(absoluteTransformFromContext(context));
87     FloatRect maskAbsoluteBoundingBox = transform.mapRect(textRootBlock->repaintRectInLocalCoordinates());
88
89     IntRect maskImageRect = enclosingIntRect(maskAbsoluteBoundingBox);
90     if (maskImageRect.isEmpty())
91         return false;
92
93     // Allocate an image buffer as big as the absolute unclipped size of the object
94     OwnPtr<ImageBuffer> maskImage = ImageBuffer::create(maskImageRect.size());
95     if (!maskImage)
96         return false;
97
98     GraphicsContext* maskImageContext = maskImage->context();
99
100     // Transform the mask image coordinate system to absolute screen coordinates
101     maskImageContext->translate(-maskAbsoluteBoundingBox.x(), -maskAbsoluteBoundingBox.y());
102     maskImageContext->concatCTM(transform);
103
104     imageBuffer = maskImage.release();
105     savedContext = context;
106     context = maskImageContext;
107
108     return true;
109 }
110
111 static inline AffineTransform clipToTextMask(GraphicsContext* context,
112                                              OwnPtr<ImageBuffer>& imageBuffer,
113                                              const RenderObject* object,
114                                              GradientData* gradientData)
115 {
116     const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
117
118     // The mask image has been created in the device coordinate space, as the image should not be scaled.
119     // So the actual masking process has to be done in the device coordinate space as well.
120     AffineTransform transform(absoluteTransformFromContext(context));
121     context->concatCTM(transform.inverse());
122     context->clipToImageBuffer(transform.mapRect(textRootBlock->repaintRectInLocalCoordinates()), imageBuffer.get());
123     context->concatCTM(transform);
124
125     AffineTransform matrix;
126     if (gradientData->boundingBoxMode) {
127         FloatRect maskBoundingBox = textRootBlock->objectBoundingBox();
128         matrix.translate(maskBoundingBox.x(), maskBoundingBox.y());
129         matrix.scaleNonUniform(maskBoundingBox.width(), maskBoundingBox.height());
130     }
131     matrix.multLeft(gradientData->transform);
132     return matrix;
133 }
134 #endif
135
136 bool RenderSVGResourceGradient::applyResource(RenderObject* object, RenderStyle* style, GraphicsContext*& context, unsigned short resourceMode)
137 {
138     ASSERT(object);
139     ASSERT(style);
140     ASSERT(context);
141     ASSERT(resourceMode != ApplyToDefaultMode);
142
143     // Be sure to synchronize all SVG properties on the gradientElement _before_ processing any further.
144     // Otherwhise the call to collectGradientAttributes() in createTileImage(), may cause the SVG DOM property
145     // synchronization to kick in, which causes invalidateClients() to be called, which in turn deletes our
146     // GradientData object! Leaving out the line below will cause svg/dynamic-updates/SVG*GradientElement-svgdom* to crash.
147     SVGGradientElement* gradientElement = static_cast<SVGGradientElement*>(node());
148     if (!gradientElement)
149         return false;
150
151     gradientElement->updateAnimatedSVGAttribute(anyQName());
152
153     if (!m_gradient.contains(object))
154         m_gradient.set(object, new GradientData);
155
156     GradientData* gradientData = m_gradient.get(object);
157
158     bool isPaintingText = resourceMode & ApplyToTextMode;
159
160     // Create gradient object
161     if (!gradientData->gradient) {
162         buildGradient(gradientData, gradientElement);
163
164         // CG platforms will handle the gradient space transform for text after applying the
165         // resource, so don't apply it here. For non-CG platforms, we want the text bounding
166         // box applied to the gradient space transform now, so the gradient shader can use it.
167 #if PLATFORM(CG)
168         if (gradientData->boundingBoxMode && !isPaintingText) {
169 #else
170         if (gradientData->boundingBoxMode) {
171 #endif
172             FloatRect objectBoundingBox = object->objectBoundingBox();
173             gradientData->userspaceTransform.translate(objectBoundingBox.x(), objectBoundingBox.y());
174             gradientData->userspaceTransform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
175         }
176
177         gradientData->userspaceTransform.multLeft(gradientData->transform);
178         gradientData->gradient->setGradientSpaceTransform(gradientData->userspaceTransform);
179     }
180
181     if (!gradientData->gradient)
182         return false;
183
184     // Draw gradient
185     context->save();
186
187     if (isPaintingText) {
188 #if PLATFORM(CG)
189         if (!createMaskAndSwapContextForTextGradient(context, m_savedContext, m_imageBuffer, object)) {
190             context->restore();
191             return false;
192         }
193 #endif
194
195         context->setTextDrawingMode(resourceMode & ApplyToFillMode ? cTextFill : cTextStroke);
196     }
197
198     const SVGRenderStyle* svgStyle = style->svgStyle();
199     ASSERT(svgStyle);
200
201     if (resourceMode & ApplyToFillMode) {
202         context->setAlpha(svgStyle->fillOpacity());
203         context->setFillGradient(gradientData->gradient);
204         context->setFillRule(svgStyle->fillRule());
205     } else if (resourceMode & ApplyToStrokeMode) {
206         if (svgStyle->vectorEffect() == VE_NON_SCALING_STROKE)
207             gradientData->gradient->setGradientSpaceTransform(transformOnNonScalingStroke(object, gradientData->userspaceTransform));
208         context->setAlpha(svgStyle->strokeOpacity());
209         context->setStrokeGradient(gradientData->gradient);
210         SVGRenderSupport::applyStrokeStyleToContext(context, style, object);
211     }
212
213     return true;
214 }
215
216 void RenderSVGResourceGradient::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode)
217 {
218     ASSERT(context);
219     ASSERT(resourceMode != ApplyToDefaultMode);
220
221     if (resourceMode & ApplyToTextMode) {
222 #if PLATFORM(CG)
223         // CG requires special handling for gradient on text
224         if (m_savedContext && m_gradient.contains(object)) {
225             GradientData* gradientData = m_gradient.get(object);
226
227             // Restore on-screen drawing context
228             context = m_savedContext;
229             m_savedContext = 0;
230
231             gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, object, gradientData));
232             context->setFillGradient(gradientData->gradient);
233
234             const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
235             context->fillRect(textRootBlock->repaintRectInLocalCoordinates());
236
237             m_imageBuffer.clear();
238         }
239 #else
240         UNUSED_PARAM(object);
241 #endif
242     } else {
243         if (resourceMode & ApplyToFillMode)
244             context->fillPath();
245         else if (resourceMode & ApplyToStrokeMode)
246             context->strokePath();
247     }
248
249     context->restore();
250 }
251
252 void RenderSVGResourceGradient::addStops(GradientData* gradientData, const Vector<Gradient::ColorStop>& stops) const
253 {
254     ASSERT(gradientData->gradient);
255
256     const Vector<Gradient::ColorStop>::const_iterator end = stops.end();
257     for (Vector<Gradient::ColorStop>::const_iterator it = stops.begin(); it != end; ++it)
258         gradientData->gradient->addColorStop(*it);
259 }
260
261 }
262
263 #endif