OSDN Git Service

am 939d1819: am 9d4701f9: Push a local reference frame to avoid table overflow.
[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);
55
56     deleteAllValues(m_gradient);
57     m_gradient.clear();
58 }
59
60 void RenderSVGResourceGradient::invalidateClient(RenderObject* object)
61 {
62     ASSERT(object);
63
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))
69         return;
70
71     delete m_gradient.take(object);
72     markForLayoutAndResourceInvalidation(object);
73 }
74
75 #if PLATFORM(CG)
76 static inline AffineTransform absoluteTransformForRenderer(const RenderObject* object)
77 {
78     AffineTransform absoluteTransform;
79
80     const RenderObject* currentObject = object;
81     while (currentObject) {
82         absoluteTransform = currentObject->localToParentTransform() * absoluteTransform;
83         currentObject = currentObject->parent();
84     }
85
86     return absoluteTransform;
87 }
88
89 static inline bool createMaskAndSwapContextForTextGradient(GraphicsContext*& context,
90                                                            GraphicsContext*& savedContext,
91                                                            OwnPtr<ImageBuffer>& imageBuffer,
92                                                            const RenderObject* object)
93 {
94     const RenderObject* textRootBlock = findTextRootObject(object);
95
96     AffineTransform transform = absoluteTransformForRenderer(textRootBlock);
97     FloatRect maskAbsoluteBoundingBox = transform.mapRect(textRootBlock->repaintRectInLocalCoordinates());
98
99     IntRect maskImageRect = enclosingIntRect(maskAbsoluteBoundingBox);
100     if (maskImageRect.isEmpty())
101         return false;
102
103     // Allocate an image buffer as big as the absolute unclipped size of the object
104     OwnPtr<ImageBuffer> maskImage = ImageBuffer::create(maskImageRect.size());
105     if (!maskImage)
106         return false;
107
108     GraphicsContext* maskImageContext = maskImage->context();
109
110     // Transform the mask image coordinate system to absolute screen coordinates
111     maskImageContext->translate(-maskAbsoluteBoundingBox.x(), -maskAbsoluteBoundingBox.y());
112     maskImageContext->concatCTM(transform);
113
114     imageBuffer.set(maskImage.release());
115     savedContext = context;
116     context = maskImageContext;
117
118     return true;
119 }
120
121 static inline AffineTransform clipToTextMask(GraphicsContext* context,
122                                              OwnPtr<ImageBuffer>& imageBuffer,
123                                              const RenderObject* object,
124                                              GradientData* gradientData)
125 {
126     const RenderObject* textRootBlock = findTextRootObject(object);
127     context->clipToImageBuffer(textRootBlock->repaintRectInLocalCoordinates(), imageBuffer.get());
128
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());
134     }
135     matrix.multiply(gradientData->transform);
136     return matrix;
137 }
138 #endif
139
140 bool RenderSVGResourceGradient::applyResource(RenderObject* object, RenderStyle* style, GraphicsContext*& context, unsigned short resourceMode)
141 {
142     ASSERT(object);
143     ASSERT(style);
144     ASSERT(context);
145     ASSERT(resourceMode != ApplyToDefaultMode);
146
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)
153         return false;
154
155     gradientElement->updateAnimatedSVGAttribute(anyQName());
156
157     if (!m_gradient.contains(object))
158         m_gradient.set(object, new GradientData);
159
160     GradientData* gradientData = m_gradient.get(object);
161
162     bool isPaintingText = resourceMode & ApplyToTextMode;
163
164     // Create gradient object
165     if (!gradientData->gradient) {
166         buildGradient(gradientData, gradientElement);
167
168         // CG platforms will handle the gradient space transform for text after applying the
169         // resource, so don't apply it here. For non-CG platforms, we want the text bounding
170         // box applied to the gradient space transform now, so the gradient shader can use it.
171 #if PLATFORM(CG)
172         if (gradientData->boundingBoxMode && !isPaintingText) {
173 #else
174         if (gradientData->boundingBoxMode) {
175 #endif
176             FloatRect objectBoundingBox = object->objectBoundingBox();
177             gradientData->userspaceTransform.translate(objectBoundingBox.x(), objectBoundingBox.y());
178             gradientData->userspaceTransform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
179         }
180
181         gradientData->userspaceTransform.multiply(gradientData->transform);
182         gradientData->gradient->setGradientSpaceTransform(gradientData->userspaceTransform);
183     }
184
185     if (!gradientData->gradient)
186         return false;
187
188     // Draw gradient
189     context->save();
190
191     if (isPaintingText) {
192 #if PLATFORM(CG)
193         if (!createMaskAndSwapContextForTextGradient(context, m_savedContext, m_imageBuffer, object)) {
194             context->restore();
195             return false;
196         }
197 #endif
198
199         context->setTextDrawingMode(resourceMode & ApplyToFillMode ? cTextFill : cTextStroke);
200     }
201
202     const SVGRenderStyle* svgStyle = style->svgStyle();
203     ASSERT(svgStyle);
204
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         if (svgStyle->vectorEffect() == VE_NON_SCALING_STROKE)
211             gradientData->gradient->setGradientSpaceTransform(transformOnNonScalingStroke(object, gradientData->userspaceTransform));
212         context->setAlpha(svgStyle->strokeOpacity());
213         context->setStrokeGradient(gradientData->gradient);
214         applyStrokeStyleToContext(context, style, object);
215     }
216
217     return true;
218 }
219
220 void RenderSVGResourceGradient::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode)
221 {
222     ASSERT(context);
223     ASSERT(resourceMode != ApplyToDefaultMode);
224
225     if (resourceMode & ApplyToTextMode) {
226 #if PLATFORM(CG)
227         // CG requires special handling for gradient on text
228         if (m_savedContext && m_gradient.contains(object)) {
229             GradientData* gradientData = m_gradient.get(object);
230
231             // Restore on-screen drawing context
232             context = m_savedContext;
233             m_savedContext = 0;
234
235             gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, object, gradientData));
236             context->setFillGradient(gradientData->gradient);
237
238             const RenderObject* textRootBlock = findTextRootObject(object);
239             context->fillRect(textRootBlock->repaintRectInLocalCoordinates());
240
241             m_imageBuffer.clear();
242         }
243 #else
244         UNUSED_PARAM(object);
245 #endif
246     } else {
247         if (resourceMode & ApplyToFillMode)
248             context->fillPath();
249         else if (resourceMode & ApplyToStrokeMode)
250             context->strokePath();
251     }
252
253     context->restore();
254 }
255
256 void RenderSVGResourceGradient::addStops(GradientData* gradientData, const Vector<Gradient::ColorStop>& stops) const
257 {
258     ASSERT(gradientData->gradient);
259
260     const Vector<Gradient::ColorStop>::const_iterator end = stops.end();
261     for (Vector<Gradient::ColorStop>::const_iterator it = stops.begin(); it != end; ++it)
262         gradientData->gradient->addColorStop(*it);
263 }
264
265 }
266
267 #endif