OSDN Git Service

am 5a1f66aa: am ec92ec7c: merge from open-source master
[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     // Create gradient object
163     if (!gradientData->gradient)
164         buildGradient(gradientData, gradientElement);
165
166     if (!gradientData->gradient)
167         return false;
168
169     // Draw gradient
170     context->save();
171
172     bool isPaintingText = resourceMode & ApplyToTextMode;
173     if (isPaintingText) {
174 #if PLATFORM(CG)
175         if (!createMaskAndSwapContextForTextGradient(context, m_savedContext, m_imageBuffer, object)) {
176             context->restore();
177             return false;
178         }
179 #endif
180
181         context->setTextDrawingMode(resourceMode & ApplyToFillMode ? cTextFill : cTextStroke);
182     }
183
184     AffineTransform transform;
185
186     // CG platforms will handle the gradient space transform for text after applying the
187     // resource, so don't apply it here. For non-CG platforms, we want the text bounding
188     // box applied to the gradient space transform now, so the gradient shader can use it.
189 #if PLATFORM(CG)
190     if (gradientData->boundingBoxMode && !isPaintingText) {
191 #else
192     if (gradientData->boundingBoxMode) {
193 #endif
194         FloatRect objectBoundingBox = object->objectBoundingBox();
195         transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
196         transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
197     }
198
199     transform.multiply(gradientData->transform);
200     gradientData->gradient->setGradientSpaceTransform(transform);
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         context->setAlpha(svgStyle->strokeOpacity());
211         context->setStrokeGradient(gradientData->gradient);
212         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             gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, object, gradientData));
234             context->setFillGradient(gradientData->gradient);
235
236             const RenderObject* textRootBlock = findTextRootObject(object);
237             context->fillRect(textRootBlock->repaintRectInLocalCoordinates());
238
239             m_imageBuffer.clear();
240         }
241 #else
242         UNUSED_PARAM(object);
243 #endif
244     } else {
245         if (resourceMode & ApplyToFillMode)
246             context->fillPath();
247         else if (resourceMode & ApplyToStrokeMode)
248             context->strokePath();
249     }
250
251     context->restore();
252 }
253
254 void RenderSVGResourceGradient::addStops(GradientData* gradientData, const Vector<Gradient::ColorStop>& stops) const
255 {
256     ASSERT(gradientData->gradient);
257
258     const Vector<Gradient::ColorStop>::const_iterator end = stops.end();
259     for (Vector<Gradient::ColorStop>::const_iterator it = stops.begin(); it != end; ++it)
260         gradientData->gradient->addColorStop(*it);
261 }
262
263 }
264
265 #endif