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.
27 #include "core/rendering/svg/SVGRenderingContext.h"
29 #include "core/frame/FrameHost.h"
30 #include "core/frame/FrameView.h"
31 #include "core/frame/LocalFrame.h"
32 #include "core/frame/Settings.h"
33 #include "core/rendering/RenderLayer.h"
34 #include "core/rendering/svg/RenderSVGImage.h"
35 #include "core/rendering/svg/RenderSVGResource.h"
36 #include "core/rendering/svg/RenderSVGResourceFilter.h"
37 #include "core/rendering/svg/RenderSVGResourceMasker.h"
38 #include "core/rendering/svg/SVGResources.h"
39 #include "core/rendering/svg/SVGResourcesCache.h"
41 static int kMaxImageBufferSize = 4096;
45 static inline bool isRenderingMaskImage(RenderObject* object)
47 if (object->frame() && object->frame()->view())
48 return object->frame()->view()->paintBehavior() & PaintBehaviorRenderingSVGMask;
52 SVGRenderingContext::~SVGRenderingContext()
54 // Fast path if we don't need to restore anything.
55 if (!(m_renderingFlags & ActionsNeeded))
58 ASSERT(m_object && m_paintInfo);
60 if (m_renderingFlags & PostApplyResources) {
61 ASSERT(m_masker || m_clipper || m_filter);
62 ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object));
65 ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->filter() == m_filter);
66 m_filter->postApplyResource(m_object, m_paintInfo->context, ApplyToDefaultMode, 0, 0);
67 m_paintInfo->context = m_savedContext;
68 m_paintInfo->rect = m_savedPaintRect;
72 ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->clipper() == m_clipper);
73 m_clipper->postApplyStatefulResource(m_object, m_paintInfo->context, m_clipperContext);
77 ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->masker() == m_masker);
78 m_masker->postApplyResource(m_object, m_paintInfo->context, ApplyToDefaultMode, 0, 0);
82 if (m_renderingFlags & EndOpacityLayer)
83 m_paintInfo->context->endLayer();
85 if (m_renderingFlags & RestoreGraphicsContext)
86 m_paintInfo->context->restore();
89 void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintInfo& paintInfo, NeedsGraphicsContextSave needsGraphicsContextSave)
94 // This function must not be called twice!
95 ASSERT(!(m_renderingFlags & PrepareToRenderSVGContentWasCalled));
96 m_renderingFlags |= PrepareToRenderSVGContentWasCalled;
100 m_paintInfo = &paintInfo;
103 // We need to save / restore the context even if the initialization failed.
104 if (needsGraphicsContextSave == SaveGraphicsContext) {
105 m_paintInfo->context->save();
106 m_renderingFlags |= RestoreGraphicsContext;
109 RenderStyle* style = m_object->style();
112 const SVGRenderStyle* svgStyle = style->svgStyle();
115 // Setup transparency layers before setting up SVG resources!
116 bool isRenderingMask = isRenderingMaskImage(m_object);
117 float opacity = isRenderingMask ? 1 : style->opacity();
118 bool hasBlendMode = style->hasBlendMode() && !isRenderingMask;
120 if (opacity < 1 || hasBlendMode || style->hasIsolation()) {
121 FloatRect repaintRect = m_object->repaintRectInLocalCoordinates();
122 m_paintInfo->context->clip(repaintRect);
125 if (!(m_renderingFlags & RestoreGraphicsContext)) {
126 m_paintInfo->context->save();
127 m_renderingFlags |= RestoreGraphicsContext;
129 m_paintInfo->context->setCompositeOperation(CompositeSourceOver, style->blendMode());
132 m_paintInfo->context->beginTransparencyLayer(opacity);
135 m_paintInfo->context->setCompositeOperation(CompositeSourceOver, blink::WebBlendModeNormal);
137 m_renderingFlags |= EndOpacityLayer;
140 ClipPathOperation* clipPathOperation = style->clipPath();
141 if (clipPathOperation && clipPathOperation->type() == ClipPathOperation::SHAPE) {
142 ShapeClipPathOperation* clipPath = toShapeClipPathOperation(clipPathOperation);
143 m_paintInfo->context->clipPath(clipPath->path(object->objectBoundingBox()), clipPath->windRule());
146 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(m_object);
148 if (svgStyle->hasFilter())
151 m_renderingFlags |= RenderingPrepared;
155 if (!isRenderingMask) {
156 if (RenderSVGResourceMasker* masker = resources->masker()) {
157 if (!masker->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
160 m_renderingFlags |= PostApplyResources;
164 RenderSVGResourceClipper* clipper = resources->clipper();
165 if (!clipPathOperation && clipper) {
166 if (!clipper->applyStatefulResource(m_object, m_paintInfo->context, m_clipperContext))
169 m_renderingFlags |= PostApplyResources;
172 if (!isRenderingMask) {
173 m_filter = resources->filter();
175 m_savedContext = m_paintInfo->context;
176 m_savedPaintRect = m_paintInfo->rect;
177 // Return with false here may mean that we don't need to draw the content
178 // (because it was either drawn before or empty) but we still need to apply the filter.
179 m_renderingFlags |= PostApplyResources;
180 if (!m_filter->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
183 // Since we're caching the resulting bitmap and do not invalidate it on repaint rect
184 // changes, we need to paint the whole filter region. Otherwise, elements not visible
185 // at the time of the initial paint (due to scrolling, window size, etc.) will never
187 m_paintInfo->rect = IntRect(m_filter->drawingRegion(m_object));
191 m_renderingFlags |= RenderingPrepared;
194 static AffineTransform& currentContentTransformation()
196 DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ());
197 return s_currentContentTransformation;
200 float SVGRenderingContext::calculateScreenFontSizeScalingFactor(const RenderObject* renderer)
205 // FIXME: calculateDeviceSpaceTransformation() queries layer compositing state - which is not
206 // supported during layout. Hence, the result may not include all CSS transforms.
207 calculateDeviceSpaceTransformation(renderer, ctm);
208 return narrowPrecisionToFloat(sqrt((pow(ctm.xScale(), 2) + pow(ctm.yScale(), 2)) / 2));
211 void SVGRenderingContext::calculateDeviceSpaceTransformation(const RenderObject* renderer, AffineTransform& absoluteTransform)
213 // FIXME: trying to compute a device space transform at record time is wrong. All clients
214 // should be updated to avoid relying on this information, and the method should be removed.
217 // We're about to possibly clear renderer, so save the deviceScaleFactor now.
218 float deviceScaleFactor = renderer->document().frameHost()->deviceScaleFactor();
220 // Walk up the render tree, accumulating SVG transforms.
221 absoluteTransform = currentContentTransformation();
223 absoluteTransform = renderer->localToParentTransform() * absoluteTransform;
224 if (renderer->isSVGRoot())
226 renderer = renderer->parent();
229 // Continue walking up the layer tree, accumulating CSS transforms.
230 RenderLayer* layer = renderer ? renderer->enclosingLayer() : 0;
231 while (layer && layer->isAllowedToQueryCompositingState()) {
232 // We can stop at compositing layers, to match the backing resolution.
233 // FIXME: should we be computing the transform to the nearest composited layer,
234 // or the nearest composited layer that does not paint into its ancestor?
235 // I think this is the nearest composited ancestor since we will inherit its
236 // transforms in the composited layer tree.
237 if (layer->compositingState() != NotComposited)
240 if (TransformationMatrix* layerTransform = layer->transform())
241 absoluteTransform = layerTransform->toAffineTransform() * absoluteTransform;
243 layer = layer->parent();
246 absoluteTransform.scale(deviceScaleFactor);
249 void SVGRenderingContext::renderSubtree(GraphicsContext* context, RenderObject* item, const AffineTransform& subtreeContentTransformation)
254 PaintInfo info(context, PaintInfo::infiniteRect(), PaintPhaseForeground, PaintBehaviorNormal);
256 AffineTransform& contentTransformation = currentContentTransformation();
257 AffineTransform savedContentTransformation = contentTransformation;
258 contentTransformation = subtreeContentTransformation * contentTransformation;
260 ASSERT(!item->needsLayout());
261 item->paint(info, IntPoint());
263 contentTransformation = savedContentTransformation;
266 FloatRect SVGRenderingContext::clampedAbsoluteTargetRect(const FloatRect& absoluteTargetRect)
268 const FloatSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize);
269 return FloatRect(absoluteTargetRect.location(), absoluteTargetRect.size().shrunkTo(maxImageBufferSize));
272 void SVGRenderingContext::clear2DRotation(AffineTransform& transform)
274 AffineTransform::DecomposedType decomposition;
275 transform.decompose(decomposition);
276 decomposition.angle = 0;
277 transform.recompose(decomposition);
280 bool SVGRenderingContext::bufferForeground(OwnPtr<ImageBuffer>& imageBuffer)
283 ASSERT(m_object->isSVGImage());
284 FloatRect boundingBox = m_object->objectBoundingBox();
286 // Invalidate an existing buffer if the scale is not correct.
288 AffineTransform transform = m_paintInfo->context->getCTM(GraphicsContext::DefinitelyIncludeDeviceScale);
289 IntSize expandedBoundingBox = expandedIntSize(boundingBox.size());
290 IntSize bufferSize(static_cast<int>(ceil(expandedBoundingBox.width() * transform.xScale())), static_cast<int>(ceil(expandedBoundingBox.height() * transform.yScale())));
291 if (bufferSize != imageBuffer->size())
295 // Create a new buffer and paint the foreground into it.
297 if ((imageBuffer = m_paintInfo->context->createCompatibleBuffer(expandedIntSize(boundingBox.size())))) {
298 GraphicsContext* bufferedRenderingContext = imageBuffer->context();
299 bufferedRenderingContext->translate(-boundingBox.x(), -boundingBox.y());
300 PaintInfo bufferedInfo(*m_paintInfo);
301 bufferedInfo.context = bufferedRenderingContext;
302 toRenderSVGImage(m_object)->paintForeground(bufferedInfo);
307 m_paintInfo->context->drawImageBuffer(imageBuffer.get(), boundingBox);