Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / svg / SVGRenderingContext.cpp
1 /*
2  * Copyright (C) 2007, 2008 Rob Buis <buis@kde.org>
3  * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
4  * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
5  * Copyright (C) 2009 Google, Inc.  All rights reserved.
6  * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
7  * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this library; see the file COPYING.LIB.  If not, write to
21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24
25 #include "config.h"
26 #include "core/rendering/svg/SVGRenderingContext.h"
27
28 #include "core/frame/FrameHost.h"
29 #include "core/rendering/PaintInfo.h"
30 #include "core/rendering/RenderLayer.h"
31 #include "core/rendering/svg/RenderSVGResourceFilter.h"
32 #include "core/rendering/svg/RenderSVGResourceMasker.h"
33 #include "core/rendering/svg/SVGRenderSupport.h"
34 #include "core/rendering/svg/SVGResources.h"
35 #include "core/rendering/svg/SVGResourcesCache.h"
36 #include "platform/FloatConversion.h"
37
38 namespace blink {
39
40 SVGRenderingContext::~SVGRenderingContext()
41 {
42     // Fast path if we don't need to restore anything.
43     if (!(m_renderingFlags & ActionsNeeded))
44         return;
45
46     ASSERT(m_object && m_paintInfo);
47
48     if (m_renderingFlags & PostApplyResources) {
49         ASSERT(m_masker || m_clipper || m_filter);
50         ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object));
51
52         if (m_filter) {
53             ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->filter() == m_filter);
54             m_filter->finishEffect(m_object, m_paintInfo->context);
55             m_paintInfo->context = m_savedContext;
56             m_paintInfo->rect = m_savedPaintRect;
57         }
58
59         if (m_clipper) {
60             ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->clipper() == m_clipper);
61             m_clipper->postApplyStatefulResource(m_object, m_paintInfo->context, m_clipperState);
62         }
63
64         if (m_masker) {
65             ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->masker() == m_masker);
66             m_masker->finishEffect(m_object, m_paintInfo->context);
67         }
68     }
69
70     if (m_renderingFlags & EndOpacityLayer)
71         m_paintInfo->context->endLayer();
72
73     if (m_renderingFlags & RestoreGraphicsContext)
74         m_paintInfo->context->restore();
75 }
76
77 void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintInfo& paintInfo)
78 {
79     ASSERT(object);
80
81 #if ENABLE(ASSERT)
82     // This function must not be called twice!
83     ASSERT(!(m_renderingFlags & PrepareToRenderSVGContentWasCalled));
84     m_renderingFlags |= PrepareToRenderSVGContentWasCalled;
85 #endif
86
87     m_object = object;
88     m_paintInfo = &paintInfo;
89
90     RenderStyle* style = m_object->style();
91     ASSERT(style);
92
93     const SVGRenderStyle& svgStyle = style->svgStyle();
94
95     // Setup transparency layers before setting up SVG resources!
96     bool isRenderingMask = SVGRenderSupport::isRenderingClipPathAsMaskImage(*m_object);
97     // RenderLayer takes care of root opacity.
98     float opacity = object->isSVGRoot() ? 1 : style->opacity();
99     bool hasBlendMode = style->hasBlendMode();
100
101     if (!isRenderingMask && (opacity < 1 || hasBlendMode || style->hasIsolation())) {
102         FloatRect paintInvalidationRect = m_object->paintInvalidationRectInLocalCoordinates();
103         m_paintInfo->context->clip(paintInvalidationRect);
104
105         if (hasBlendMode) {
106             if (!(m_renderingFlags & RestoreGraphicsContext)) {
107                 m_paintInfo->context->save();
108                 m_renderingFlags |= RestoreGraphicsContext;
109             }
110             m_paintInfo->context->setCompositeOperation(CompositeSourceOver, style->blendMode());
111         }
112
113         m_paintInfo->context->beginTransparencyLayer(opacity);
114
115         if (hasBlendMode)
116             m_paintInfo->context->setCompositeOperation(CompositeSourceOver, WebBlendModeNormal);
117
118         m_renderingFlags |= EndOpacityLayer;
119     }
120
121     SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(m_object);
122
123     // Prefer a 'clipper' (non-prefixed 'clip-path') to a 'clip shape'
124     // ('-webkit-clip-path'), until these two properties end up being merged
125     // properly.
126     if (RenderSVGResourceClipper* clipper = resources ? resources->clipper() : nullptr) {
127         if (!clipper->applyStatefulResource(m_object, m_paintInfo->context, m_clipperState))
128             return;
129         m_clipper = clipper;
130         m_renderingFlags |= PostApplyResources;
131     } else {
132         ClipPathOperation* clipPathOperation = style->clipPath();
133         if (clipPathOperation && clipPathOperation->type() == ClipPathOperation::SHAPE) {
134             ShapeClipPathOperation* clipPath = toShapeClipPathOperation(clipPathOperation);
135             m_paintInfo->context->clipPath(clipPath->path(object->objectBoundingBox()), clipPath->windRule());
136         }
137     }
138
139     if (isRenderingMask) {
140         m_renderingFlags |= RenderingPrepared;
141         return;
142     }
143
144     if (resources) {
145         if (RenderSVGResourceMasker* masker = resources->masker()) {
146             if (!masker->prepareEffect(m_object, m_paintInfo->context))
147                 return;
148             m_masker = masker;
149             m_renderingFlags |= PostApplyResources;
150         }
151
152         m_filter = resources->filter();
153         if (m_filter) {
154             m_savedContext = m_paintInfo->context;
155             m_savedPaintRect = m_paintInfo->rect;
156             // Return with false here may mean that we don't need to draw the content
157             // (because it was either drawn before or empty) but we still need to apply the filter.
158             m_renderingFlags |= PostApplyResources;
159             if (!m_filter->prepareEffect(m_object, m_paintInfo->context))
160                 return;
161
162             // Since we're caching the resulting bitmap and do not invalidate it on paint invalidation rect
163             // changes, we need to paint the whole filter region. Otherwise, elements not visible
164             // at the time of the initial paint (due to scrolling, window size, etc.) will never
165             // be drawn.
166             m_paintInfo->rect = IntRect(m_filter->drawingRegion(m_object));
167         }
168     } else {
169         // Broken filter disables rendering.
170         if (svgStyle.hasFilter())
171             return;
172     }
173
174     m_renderingFlags |= RenderingPrepared;
175 }
176
177 static AffineTransform& currentContentTransformation()
178 {
179     DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ());
180     return s_currentContentTransformation;
181 }
182
183 SubtreeContentTransformScope::SubtreeContentTransformScope(const AffineTransform& subtreeContentTransformation)
184 {
185     AffineTransform& contentTransformation = currentContentTransformation();
186     m_savedContentTransformation = contentTransformation;
187     contentTransformation = subtreeContentTransformation * contentTransformation;
188 }
189
190 SubtreeContentTransformScope::~SubtreeContentTransformScope()
191 {
192     currentContentTransformation() = m_savedContentTransformation;
193 }
194
195 float SVGRenderingContext::calculateScreenFontSizeScalingFactor(const RenderObject* renderer)
196 {
197     // FIXME: trying to compute a device space transform at record time is wrong. All clients
198     // should be updated to avoid relying on this information, and the method should be removed.
199
200     ASSERT(renderer);
201     // We're about to possibly clear renderer, so save the deviceScaleFactor now.
202     float deviceScaleFactor = renderer->document().frameHost()->deviceScaleFactor();
203
204     // Walk up the render tree, accumulating SVG transforms.
205     AffineTransform ctm = currentContentTransformation();
206     while (renderer) {
207         ctm = renderer->localToParentTransform() * ctm;
208         if (renderer->isSVGRoot())
209             break;
210         renderer = renderer->parent();
211     }
212
213     // Continue walking up the layer tree, accumulating CSS transforms.
214     // FIXME: this queries layer compositing state - which is not
215     // supported during layout. Hence, the result may not include all CSS transforms.
216     RenderLayer* layer = renderer ? renderer->enclosingLayer() : 0;
217     while (layer && layer->isAllowedToQueryCompositingState()) {
218         // We can stop at compositing layers, to match the backing resolution.
219         // FIXME: should we be computing the transform to the nearest composited layer,
220         // or the nearest composited layer that does not paint into its ancestor?
221         // I think this is the nearest composited ancestor since we will inherit its
222         // transforms in the composited layer tree.
223         if (layer->compositingState() != NotComposited)
224             break;
225
226         if (TransformationMatrix* layerTransform = layer->transform())
227             ctm = layerTransform->toAffineTransform() * ctm;
228
229         layer = layer->parent();
230     }
231
232     ctm.scale(deviceScaleFactor);
233
234     return narrowPrecisionToFloat(sqrt((pow(ctm.xScale(), 2) + pow(ctm.yScale(), 2)) / 2));
235 }
236
237 void SVGRenderingContext::renderSubtree(GraphicsContext* context, RenderObject* item)
238 {
239     ASSERT(context);
240     ASSERT(item);
241     ASSERT(!item->needsLayout());
242
243     PaintInfo info(context, PaintInfo::infiniteRect(), PaintPhaseForeground, PaintBehaviorNormal);
244     item->paint(info, IntPoint());
245 }
246
247 } // namespace blink