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.
28 #include "SVGRenderingContext.h"
30 #include "BasicShapes.h"
32 #include "FrameView.h"
33 #include "RenderSVGResource.h"
34 #include "RenderSVGResourceClipper.h"
35 #include "RenderSVGResourceFilter.h"
36 #include "RenderSVGResourceMasker.h"
37 #include "SVGResources.h"
38 #include "SVGResourcesCache.h"
40 static int kMaxImageBufferSize = 4096;
44 static inline bool isRenderingMaskImage(RenderObject* object)
46 if (object->frame() && object->frame()->view())
47 return object->frame()->view()->paintBehavior() & PaintBehaviorRenderingSVGMask;
51 SVGRenderingContext::~SVGRenderingContext()
53 // Fast path if we don't need to restore anything.
54 if (!(m_renderingFlags & ActionsNeeded))
57 ASSERT(m_object && m_paintInfo);
60 if (m_renderingFlags & EndFilterLayer) {
62 m_filter->postApplyResource(static_cast<RenderSVGShape*>(m_object), m_paintInfo->context, ApplyToDefaultMode, 0, 0);
63 m_paintInfo->context = m_savedContext;
64 m_paintInfo->rect = m_savedPaintRect;
68 if (m_renderingFlags & EndOpacityLayer)
69 m_paintInfo->context->endTransparencyLayer();
71 if (m_renderingFlags & EndShadowLayer)
72 m_paintInfo->context->endTransparencyLayer();
74 if (m_renderingFlags & RestoreGraphicsContext)
75 m_paintInfo->context->restore();
78 void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintInfo& paintInfo, NeedsGraphicsContextSave needsGraphicsContextSave)
83 // This function must not be called twice!
84 ASSERT(!(m_renderingFlags & PrepareToRenderSVGContentWasCalled));
85 m_renderingFlags |= PrepareToRenderSVGContentWasCalled;
89 m_paintInfo = &paintInfo;
94 // We need to save / restore the context even if the initialization failed.
95 if (needsGraphicsContextSave == SaveGraphicsContext) {
96 m_paintInfo->context->save();
97 m_renderingFlags |= RestoreGraphicsContext;
100 RenderStyle* style = m_object->style();
103 const SVGRenderStyle* svgStyle = style->svgStyle();
106 // Setup transparency layers before setting up SVG resources!
107 bool isRenderingMask = isRenderingMaskImage(m_object);
108 float opacity = isRenderingMask ? 1 : style->opacity();
109 const ShadowData* shadow = svgStyle->shadow();
110 if (opacity < 1 || shadow) {
111 FloatRect repaintRect = m_object->repaintRectInLocalCoordinates();
114 m_paintInfo->context->clip(repaintRect);
115 m_paintInfo->context->beginTransparencyLayer(opacity);
116 m_renderingFlags |= EndOpacityLayer;
120 m_paintInfo->context->clip(repaintRect);
121 m_paintInfo->context->setShadow(IntSize(roundToInt(shadow->x()), roundToInt(shadow->y())), shadow->blur(), shadow->color(), style->colorSpace());
122 m_paintInfo->context->beginTransparencyLayer(1);
123 m_renderingFlags |= EndShadowLayer;
127 ClipPathOperation* clipPathOperation = style->clipPath();
128 if (clipPathOperation && clipPathOperation->getOperationType() == ClipPathOperation::SHAPE) {
129 ShapeClipPathOperation* clipPath = static_cast<ShapeClipPathOperation*>(clipPathOperation);
130 m_paintInfo->context->clipPath(clipPath->path(object->objectBoundingBox()), clipPath->windRule());
133 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(m_object);
136 if (svgStyle->hasFilter())
139 m_renderingFlags |= RenderingPrepared;
143 if (!isRenderingMask) {
144 if (RenderSVGResourceMasker* masker = resources->masker()) {
145 if (!masker->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
150 RenderSVGResourceClipper* clipper = resources->clipper();
151 if (!clipPathOperation && clipper) {
152 if (!clipper->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
157 if (!isRenderingMask) {
158 m_filter = resources->filter();
160 m_savedContext = m_paintInfo->context;
161 m_savedPaintRect = m_paintInfo->rect;
162 // Return with false here may mean that we don't need to draw the content
163 // (because it was either drawn before or empty) but we still need to apply the filter.
164 m_renderingFlags |= EndFilterLayer;
165 if (!m_filter->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
168 // Since we're caching the resulting bitmap and do not invalidate it on repaint rect
169 // changes, we need to paint the whole filter region. Otherwise, elements not visible
170 // at the time of the initial paint (due to scrolling, window size, etc.) will never
172 m_paintInfo->rect = IntRect(m_filter->drawingRegion(m_object));
177 m_renderingFlags |= RenderingPrepared;
180 static AffineTransform& currentContentTransformation()
182 DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ());
183 return s_currentContentTransformation;
186 float SVGRenderingContext::calculateScreenFontSizeScalingFactor(const RenderObject* renderer)
191 calculateTransformationToOutermostSVGCoordinateSystem(renderer, ctm);
192 return narrowPrecisionToFloat(sqrt((pow(ctm.xScale(), 2) + pow(ctm.yScale(), 2)) / 2));
195 void SVGRenderingContext::calculateTransformationToOutermostSVGCoordinateSystem(const RenderObject* renderer, AffineTransform& absoluteTransform)
197 const RenderObject* current = renderer;
200 absoluteTransform = currentContentTransformation();
202 absoluteTransform = current->localToParentTransform() * absoluteTransform;
203 if (current->isSVGRoot())
205 current = current->parent();
209 bool SVGRenderingContext::createImageBuffer(const FloatRect& targetRect, const AffineTransform& absoluteTransform, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace, RenderingMode renderingMode)
211 IntRect paintRect = calculateImageBufferRect(targetRect, absoluteTransform);
212 // Don't create empty ImageBuffers.
213 if (paintRect.isEmpty())
216 IntSize clampedSize = clampedAbsoluteSize(paintRect.size());
217 OwnPtr<ImageBuffer> image = ImageBuffer::create(clampedSize, 1, colorSpace, renderingMode);
221 GraphicsContext* imageContext = image->context();
222 ASSERT(imageContext);
224 imageContext->scale(FloatSize(static_cast<float>(clampedSize.width()) / paintRect.width(),
225 static_cast<float>(clampedSize.height()) / paintRect.height()));
226 imageContext->translate(-paintRect.x(), -paintRect.y());
227 imageContext->concatCTM(absoluteTransform);
229 imageBuffer = image.release();
233 bool SVGRenderingContext::createImageBufferForPattern(const FloatRect& absoluteTargetRect, const FloatRect& clampedAbsoluteTargetRect, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace, RenderingMode renderingMode)
235 IntSize imageSize(roundedIntSize(clampedAbsoluteTargetRect.size()));
236 IntSize unclampedImageSize(roundedIntSize(absoluteTargetRect.size()));
238 // Don't create empty ImageBuffers.
239 if (imageSize.isEmpty())
242 OwnPtr<ImageBuffer> image = ImageBuffer::create(imageSize, 1, colorSpace, renderingMode);
246 GraphicsContext* imageContext = image->context();
247 ASSERT(imageContext);
249 // Compensate rounding effects, as the absolute target rect is using floating-point numbers and the image buffer size is integer.
250 imageContext->scale(FloatSize(unclampedImageSize.width() / absoluteTargetRect.width(), unclampedImageSize.height() / absoluteTargetRect.height()));
252 imageBuffer = image.release();
256 void SVGRenderingContext::renderSubtreeToImageBuffer(ImageBuffer* image, RenderObject* item, const AffineTransform& subtreeContentTransformation)
260 ASSERT(image->context());
262 PaintInfo info(image->context(), PaintInfo::infiniteRect(), PaintPhaseForeground, PaintBehaviorNormal);
264 AffineTransform& contentTransformation = currentContentTransformation();
265 AffineTransform savedContentTransformation = contentTransformation;
266 contentTransformation = subtreeContentTransformation * contentTransformation;
268 ASSERT(!item->needsLayout());
269 item->paint(info, IntPoint());
271 contentTransformation = savedContentTransformation;
274 void SVGRenderingContext::clipToImageBuffer(GraphicsContext* context, const AffineTransform& absoluteTransform, const FloatRect& targetRect, OwnPtr<ImageBuffer>& imageBuffer, bool safeToClear)
279 FloatRect absoluteTargetRect = calculateImageBufferRect(targetRect, absoluteTransform);
281 // The mask image has been created in the absolute coordinate space, as the image should not be scaled.
282 // So the actual masking process has to be done in the absolute coordinate space as well.
283 context->concatCTM(absoluteTransform.inverse());
284 context->clipToImageBuffer(imageBuffer.get(), absoluteTargetRect);
285 context->concatCTM(absoluteTransform);
287 // When nesting resources, with objectBoundingBox as content unit types, there's no use in caching the
288 // resulting image buffer as the parent resource already caches the result.
289 if (safeToClear && !currentContentTransformation().isIdentity())
293 FloatRect SVGRenderingContext::clampedAbsoluteTargetRect(const FloatRect& absoluteTargetRect)
295 const FloatSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize);
296 return FloatRect(absoluteTargetRect.location(), absoluteTargetRect.size().shrunkTo(maxImageBufferSize));
299 IntSize SVGRenderingContext::clampedAbsoluteSize(const IntSize& absoluteSize)
301 const IntSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize);
302 return absoluteSize.shrunkTo(maxImageBufferSize);
305 void SVGRenderingContext::clear2DRotation(AffineTransform& transform)
307 AffineTransform::DecomposedType decomposition;
308 transform.decompose(decomposition);
309 decomposition.angle = 0;
310 transform.recompose(decomposition);