Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / svg / RenderSVGResourceGradient.cpp
1 /*
2  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
4  * Copyright (C) 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 #include "config.h"
24
25 #include "core/rendering/svg/RenderSVGResourceGradient.h"
26
27 #include "core/rendering/svg/RenderSVGShape.h"
28 #include "core/rendering/svg/SVGRenderSupport.h"
29 #include "platform/graphics/GraphicsContext.h"
30
31 namespace WebCore {
32
33 RenderSVGResourceGradient::RenderSVGResourceGradient(SVGGradientElement* node)
34     : RenderSVGResourceContainer(node)
35     , m_shouldCollectGradientAttributes(true)
36 {
37 }
38
39 void RenderSVGResourceGradient::removeAllClientsFromCache(bool markForInvalidation)
40 {
41     m_gradientMap.clear();
42     m_shouldCollectGradientAttributes = true;
43     markAllClientsForInvalidation(markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
44 }
45
46 void RenderSVGResourceGradient::removeClientFromCache(RenderObject* client, bool markForInvalidation)
47 {
48     ASSERT(client);
49     m_gradientMap.remove(client);
50     markClientForInvalidation(client, markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
51 }
52
53 bool RenderSVGResourceGradient::applyResource(RenderObject* object, RenderStyle* style, GraphicsContext*& context, unsigned short resourceMode)
54 {
55     ASSERT(object);
56     ASSERT(style);
57     ASSERT(context);
58     ASSERT(resourceMode != ApplyToDefaultMode);
59
60     clearInvalidationMask();
61
62     // Be sure to synchronize all SVG properties on the gradientElement _before_ processing any further.
63     // Otherwhise the call to collectGradientAttributes() in createTileImage(), may cause the SVG DOM property
64     // synchronization to kick in, which causes removeAllClientsFromCache() to be called, which in turn deletes our
65     // GradientData object! Leaving out the line below will cause svg/dynamic-updates/SVG*GradientElement-svgdom* to crash.
66     SVGGradientElement* gradientElement = toSVGGradientElement(element());
67     if (!gradientElement)
68         return false;
69
70     if (m_shouldCollectGradientAttributes) {
71         gradientElement->synchronizeAnimatedSVGAttribute(anyQName());
72         if (!collectGradientAttributes(gradientElement))
73             return false;
74
75         m_shouldCollectGradientAttributes = false;
76     }
77
78     // Spec: When the geometry of the applicable element has no width or height and objectBoundingBox is specified,
79     // then the given effect (e.g. a gradient or a filter) will be ignored.
80     FloatRect objectBoundingBox = object->objectBoundingBox();
81     if (gradientUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX && objectBoundingBox.isEmpty())
82         return false;
83
84     OwnPtr<GradientData>& gradientData = m_gradientMap.add(object, nullptr).storedValue->value;
85     if (!gradientData)
86         gradientData = adoptPtr(new GradientData);
87
88     // Create gradient object
89     if (!gradientData->gradient) {
90         buildGradient(gradientData.get());
91
92         // We want the text bounding box applied to the gradient space transform now, so the gradient shader can use it.
93         if (gradientUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX && !objectBoundingBox.isEmpty()) {
94             gradientData->userspaceTransform.translate(objectBoundingBox.x(), objectBoundingBox.y());
95             gradientData->userspaceTransform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
96         }
97
98         AffineTransform gradientTransform;
99         calculateGradientTransform(gradientTransform);
100
101         gradientData->userspaceTransform *= gradientTransform;
102     }
103
104     if (!gradientData->gradient)
105         return false;
106
107     const SVGRenderStyle* svgStyle = style->svgStyle();
108     ASSERT(svgStyle);
109
110     AffineTransform computedGradientSpaceTransform = computeResourceSpaceTransform(object, gradientData->userspaceTransform, svgStyle, resourceMode);
111     gradientData->gradient->setGradientSpaceTransform(computedGradientSpaceTransform);
112
113     // Draw gradient
114     context->save();
115
116     if (resourceMode & ApplyToTextMode)
117         context->setTextDrawingMode(resourceMode & ApplyToFillMode ? TextModeFill : TextModeStroke);
118
119     if (resourceMode & ApplyToFillMode) {
120         context->setAlphaAsFloat(svgStyle->fillOpacity());
121         context->setFillGradient(gradientData->gradient);
122         context->setFillRule(svgStyle->fillRule());
123     } else if (resourceMode & ApplyToStrokeMode) {
124         context->setAlphaAsFloat(svgStyle->strokeOpacity());
125         context->setStrokeGradient(gradientData->gradient);
126         SVGRenderSupport::applyStrokeStyleToContext(context, style, object);
127     }
128
129     return true;
130 }
131
132 void RenderSVGResourceGradient::postApplyResource(RenderObject*, GraphicsContext*& context, unsigned short resourceMode, const Path* path, const RenderSVGShape* shape)
133 {
134     ASSERT(context);
135     ASSERT(resourceMode != ApplyToDefaultMode);
136
137     if (resourceMode & ApplyToFillMode) {
138         if (path)
139             context->fillPath(*path);
140         else if (shape)
141             shape->fillShape(context);
142     }
143     if (resourceMode & ApplyToStrokeMode) {
144         if (path)
145             context->strokePath(*path);
146         else if (shape)
147             shape->strokeShape(context);
148     }
149
150     context->restore();
151 }
152
153 void RenderSVGResourceGradient::addStops(GradientData* gradientData, const Vector<Gradient::ColorStop>& stops) const
154 {
155     ASSERT(gradientData->gradient);
156
157     const Vector<Gradient::ColorStop>::const_iterator end = stops.end();
158     for (Vector<Gradient::ColorStop>::const_iterator it = stops.begin(); it != end; ++it)
159         gradientData->gradient->addColorStop(*it);
160 }
161
162 GradientSpreadMethod RenderSVGResourceGradient::platformSpreadMethodFromSVGType(SVGSpreadMethodType method) const
163 {
164     switch (method) {
165     case SVGSpreadMethodUnknown:
166     case SVGSpreadMethodPad:
167         return SpreadMethodPad;
168     case SVGSpreadMethodReflect:
169         return SpreadMethodReflect;
170     case SVGSpreadMethodRepeat:
171         return SpreadMethodRepeat;
172     }
173
174     ASSERT_NOT_REACHED();
175     return SpreadMethodPad;
176 }
177
178 }