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"
31 #include "FrameView.h"
32 #include "RenderSVGResource.h"
33 #include "RenderSVGResourceClipper.h"
34 #include "RenderSVGResourceFilter.h"
35 #include "RenderSVGResourceMasker.h"
36 #include "SVGResources.h"
37 #include "SVGResourcesCache.h"
39 static int kMaxImageBufferSize = 4096;
43 static inline bool isRenderingMaskImage(RenderObject* object)
45 if (object->frame() && object->frame()->view())
46 return object->frame()->view()->paintBehavior() & PaintBehaviorRenderingSVGMask;
50 SVGRenderingContext::~SVGRenderingContext()
52 // Fast path if we don't need to restore anything.
53 if (!(m_renderingFlags & ActionsNeeded))
56 ASSERT(m_object && m_paintInfo);
59 if (m_renderingFlags & EndFilterLayer) {
61 m_filter->postApplyResource(static_cast<RenderSVGShape*>(m_object), m_paintInfo->context, ApplyToDefaultMode, 0, 0);
62 m_paintInfo->context = m_savedContext;
66 if (m_renderingFlags & EndOpacityLayer)
67 m_paintInfo->context->endTransparencyLayer();
69 if (m_renderingFlags & EndShadowLayer)
70 m_paintInfo->context->endTransparencyLayer();
72 if (m_renderingFlags & RestoreGraphicsContext)
73 m_paintInfo->context->restore();
76 void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintInfo& paintInfo, NeedsGraphicsContextSave needsGraphicsContextSave)
81 // This function must not be called twice!
82 ASSERT(!(m_renderingFlags & PrepareToRenderSVGContentWasCalled));
83 m_renderingFlags |= PrepareToRenderSVGContentWasCalled;
87 m_paintInfo = &paintInfo;
92 // We need to save / restore the context even if the initialization failed.
93 if (needsGraphicsContextSave == SaveGraphicsContext) {
94 m_paintInfo->context->save();
95 m_renderingFlags |= RestoreGraphicsContext;
98 RenderStyle* style = m_object->style();
101 const SVGRenderStyle* svgStyle = style->svgStyle();
104 // Setup transparency layers before setting up SVG resources!
105 bool isRenderingMask = isRenderingMaskImage(m_object);
106 float opacity = isRenderingMask ? 1 : style->opacity();
107 const ShadowData* shadow = svgStyle->shadow();
108 if (opacity < 1 || shadow) {
109 FloatRect repaintRect = m_object->repaintRectInLocalCoordinates();
112 m_paintInfo->context->clip(repaintRect);
113 m_paintInfo->context->beginTransparencyLayer(opacity);
114 m_renderingFlags |= EndOpacityLayer;
118 m_paintInfo->context->clip(repaintRect);
119 m_paintInfo->context->setShadow(IntSize(roundToInt(shadow->x()), roundToInt(shadow->y())), shadow->blur(), shadow->color(), style->colorSpace());
120 m_paintInfo->context->beginTransparencyLayer(1);
121 m_renderingFlags |= EndShadowLayer;
125 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(m_object);
128 if (svgStyle->hasFilter())
131 m_renderingFlags |= RenderingPrepared;
135 if (!isRenderingMask) {
136 if (RenderSVGResourceMasker* masker = resources->masker()) {
137 if (!masker->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
142 if (RenderSVGResourceClipper* clipper = resources->clipper()) {
143 if (!clipper->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
148 if (!isRenderingMask) {
149 m_filter = resources->filter();
151 m_savedContext = m_paintInfo->context;
152 // Return with false here may mean that we don't need to draw the content
153 // (because it was either drawn before or empty) but we still need to apply the filter.
154 m_renderingFlags |= EndFilterLayer;
155 if (!m_filter->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
161 m_renderingFlags |= RenderingPrepared;
164 static AffineTransform& currentContentTransformation()
166 DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ());
167 return s_currentContentTransformation;
170 void SVGRenderingContext::calculateTransformationToOutermostSVGCoordinateSystem(const RenderObject* renderer, AffineTransform& absoluteTransform)
172 const RenderObject* current = renderer;
175 absoluteTransform = currentContentTransformation();
177 absoluteTransform = current->localToParentTransform() * absoluteTransform;
178 if (current->isSVGRoot())
180 current = current->parent();
184 bool SVGRenderingContext::createImageBuffer(const FloatRect& targetRect, const AffineTransform& absoluteTransform, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace, RenderingMode renderingMode)
186 IntRect paintRect = calculateImageBufferRect(targetRect, absoluteTransform);
187 // Don't create empty ImageBuffers.
188 if (paintRect.isEmpty())
191 IntSize clampedSize = clampedAbsoluteSize(paintRect.size());
192 OwnPtr<ImageBuffer> image = ImageBuffer::create(clampedSize, 1, colorSpace, renderingMode);
196 GraphicsContext* imageContext = image->context();
197 ASSERT(imageContext);
199 // This is done in absolute coordinates.
200 imageContext->translate(-paintRect.x(), -paintRect.y());
202 imageContext->concatCTM(absoluteTransform);
204 // This happens in local coordinates.
205 imageContext->scale(FloatSize(static_cast<float>(clampedSize.width()) / paintRect.width(),
206 static_cast<float>(clampedSize.height()) / paintRect.height()));
208 imageBuffer = image.release();
212 bool SVGRenderingContext::createImageBufferForPattern(const FloatRect& absoluteTargetRect, const FloatRect& clampedAbsoluteTargetRect, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace, RenderingMode renderingMode)
214 IntSize imageSize(roundedIntSize(clampedAbsoluteTargetRect.size()));
215 IntSize unclampedImageSize(roundedIntSize(absoluteTargetRect.size()));
217 // Don't create empty ImageBuffers.
218 if (imageSize.isEmpty())
221 OwnPtr<ImageBuffer> image = ImageBuffer::create(imageSize, 1, colorSpace, renderingMode);
225 GraphicsContext* imageContext = image->context();
226 ASSERT(imageContext);
228 // Compensate rounding effects, as the absolute target rect is using floating-point numbers and the image buffer size is integer.
229 imageContext->scale(FloatSize(unclampedImageSize.width() / absoluteTargetRect.width(), unclampedImageSize.height() / absoluteTargetRect.height()));
231 imageBuffer = image.release();
235 void SVGRenderingContext::renderSubtreeToImageBuffer(ImageBuffer* image, RenderObject* item, const AffineTransform& subtreeContentTransformation)
239 ASSERT(image->context());
241 PaintInfo info(image->context(), PaintInfo::infiniteRect(), PaintPhaseForeground, 0, 0, 0, 0);
243 AffineTransform& contentTransformation = currentContentTransformation();
244 AffineTransform savedContentTransformation = contentTransformation;
245 contentTransformation = subtreeContentTransformation * contentTransformation;
247 ASSERT(!item->needsLayout());
248 item->paint(info, IntPoint());
250 contentTransformation = savedContentTransformation;
253 void SVGRenderingContext::clipToImageBuffer(GraphicsContext* context, const AffineTransform& absoluteTransform, const FloatRect& targetRect, OwnPtr<ImageBuffer>& imageBuffer, bool safeToClear)
258 FloatRect absoluteTargetRect = calculateImageBufferRect(targetRect, absoluteTransform);
260 // The mask image has been created in the absolute coordinate space, as the image should not be scaled.
261 // So the actual masking process has to be done in the absolute coordinate space as well.
262 context->concatCTM(absoluteTransform.inverse());
263 context->clipToImageBuffer(imageBuffer.get(), absoluteTargetRect);
264 context->concatCTM(absoluteTransform);
266 // When nesting resources, with objectBoundingBox as content unit types, there's no use in caching the
267 // resulting image buffer as the parent resource already caches the result.
268 if (safeToClear && !currentContentTransformation().isIdentity())
272 FloatRect SVGRenderingContext::clampedAbsoluteTargetRect(const FloatRect& absoluteTargetRect)
274 const FloatSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize);
275 return FloatRect(absoluteTargetRect.location(), absoluteTargetRect.size().shrunkTo(maxImageBufferSize));
278 IntSize SVGRenderingContext::clampedAbsoluteSize(const IntSize& absoluteSize)
280 const IntSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize);
281 return absoluteSize.shrunkTo(maxImageBufferSize);
284 void SVGRenderingContext::clear2DRotation(AffineTransform& transform)
286 AffineTransform::DecomposedType decomposition;
287 transform.decompose(decomposition);
288 decomposition.angle = 0;
289 transform.recompose(decomposition);