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.
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.
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.
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.
26 #include "core/rendering/svg/SVGRenderingContext.h"
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"
40 SVGRenderingContext::~SVGRenderingContext()
42 // Fast path if we don't need to restore anything.
43 if (!(m_renderingFlags & ActionsNeeded))
46 ASSERT(m_object && m_paintInfo);
48 if (m_renderingFlags & PostApplyResources) {
49 ASSERT(m_masker || m_clipper || m_filter);
50 ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object));
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;
60 ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->clipper() == m_clipper);
61 m_clipper->postApplyStatefulResource(m_object, m_paintInfo->context, m_clipperState);
65 ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->masker() == m_masker);
66 m_masker->finishEffect(m_object, m_paintInfo->context);
70 if (m_renderingFlags & EndOpacityLayer)
71 m_paintInfo->context->endLayer();
73 if (m_renderingFlags & RestoreGraphicsContext)
74 m_paintInfo->context->restore();
77 void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintInfo& paintInfo)
82 // This function must not be called twice!
83 ASSERT(!(m_renderingFlags & PrepareToRenderSVGContentWasCalled));
84 m_renderingFlags |= PrepareToRenderSVGContentWasCalled;
88 m_paintInfo = &paintInfo;
90 RenderStyle* style = m_object->style();
93 const SVGRenderStyle& svgStyle = style->svgStyle();
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();
101 if (!isRenderingMask && (opacity < 1 || hasBlendMode || style->hasIsolation())) {
102 FloatRect paintInvalidationRect = m_object->paintInvalidationRectInLocalCoordinates();
103 m_paintInfo->context->clip(paintInvalidationRect);
106 if (!(m_renderingFlags & RestoreGraphicsContext)) {
107 m_paintInfo->context->save();
108 m_renderingFlags |= RestoreGraphicsContext;
110 m_paintInfo->context->setCompositeOperation(CompositeSourceOver, style->blendMode());
113 m_paintInfo->context->beginTransparencyLayer(opacity);
116 m_paintInfo->context->setCompositeOperation(CompositeSourceOver, WebBlendModeNormal);
118 m_renderingFlags |= EndOpacityLayer;
121 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(m_object);
123 // Prefer a 'clipper' (non-prefixed 'clip-path') to a 'clip shape'
124 // ('-webkit-clip-path'), until these two properties end up being merged
126 if (RenderSVGResourceClipper* clipper = resources ? resources->clipper() : nullptr) {
127 if (!clipper->applyStatefulResource(m_object, m_paintInfo->context, m_clipperState))
130 m_renderingFlags |= PostApplyResources;
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());
139 if (isRenderingMask) {
140 m_renderingFlags |= RenderingPrepared;
145 if (RenderSVGResourceMasker* masker = resources->masker()) {
146 if (!masker->prepareEffect(m_object, m_paintInfo->context))
149 m_renderingFlags |= PostApplyResources;
152 m_filter = resources->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))
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
166 m_paintInfo->rect = IntRect(m_filter->drawingRegion(m_object));
169 // Broken filter disables rendering.
170 if (svgStyle.hasFilter())
174 m_renderingFlags |= RenderingPrepared;
177 static AffineTransform& currentContentTransformation()
179 DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ());
180 return s_currentContentTransformation;
183 SubtreeContentTransformScope::SubtreeContentTransformScope(const AffineTransform& subtreeContentTransformation)
185 AffineTransform& contentTransformation = currentContentTransformation();
186 m_savedContentTransformation = contentTransformation;
187 contentTransformation = subtreeContentTransformation * contentTransformation;
190 SubtreeContentTransformScope::~SubtreeContentTransformScope()
192 currentContentTransformation() = m_savedContentTransformation;
195 float SVGRenderingContext::calculateScreenFontSizeScalingFactor(const RenderObject* renderer)
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.
201 // We're about to possibly clear renderer, so save the deviceScaleFactor now.
202 float deviceScaleFactor = renderer->document().frameHost()->deviceScaleFactor();
204 // Walk up the render tree, accumulating SVG transforms.
205 AffineTransform ctm = currentContentTransformation();
207 ctm = renderer->localToParentTransform() * ctm;
208 if (renderer->isSVGRoot())
210 renderer = renderer->parent();
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)
226 if (TransformationMatrix* layerTransform = layer->transform())
227 ctm = layerTransform->toAffineTransform() * ctm;
229 layer = layer->parent();
232 ctm.scale(deviceScaleFactor);
234 return narrowPrecisionToFloat(sqrt((pow(ctm.xScale(), 2) + pow(ctm.yScale(), 2)) / 2));
237 void SVGRenderingContext::renderSubtree(GraphicsContext* context, RenderObject* item)
241 ASSERT(!item->needsLayout());
243 PaintInfo info(context, PaintInfo::infiniteRect(), PaintPhaseForeground, PaintBehaviorNormal);
244 item->paint(info, IntPoint());