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 27#if ENABLE(SVG) 28#include "SVGRenderingContext.h" 29 30#include "BasicShapes.h" 31#include "Frame.h" 32#include "FrameView.h" 33#include "Page.h" 34#include "RenderLayer.h" 35#include "RenderSVGImage.h" 36#include "RenderSVGResource.h" 37#include "RenderSVGResourceClipper.h" 38#include "RenderSVGResourceFilter.h" 39#include "RenderSVGResourceMasker.h" 40#include "SVGResources.h" 41#include "SVGResourcesCache.h" 42 43static int kMaxImageBufferSize = 4096; 44 45namespace WebCore { 46 47static inline bool isRenderingMaskImage(RenderObject* object) 48{ 49 if (object->frame() && object->frame()->view()) 50 return object->frame()->view()->paintBehavior() & PaintBehaviorRenderingSVGMask; 51 return false; 52} 53 54SVGRenderingContext::~SVGRenderingContext() 55{ 56 // Fast path if we don't need to restore anything. 57 if (!(m_renderingFlags & ActionsNeeded)) 58 return; 59 60 ASSERT(m_object && m_paintInfo); 61 62#if ENABLE(FILTERS) 63 if (m_renderingFlags & EndFilterLayer) { 64 ASSERT(m_filter); 65 m_filter->postApplyResource(m_object, m_paintInfo->context, ApplyToDefaultMode, 0, 0); 66 m_paintInfo->context = m_savedContext; 67 m_paintInfo->rect = m_savedPaintRect; 68 } 69#endif 70 71 if (m_renderingFlags & EndOpacityLayer) 72 m_paintInfo->context->endTransparencyLayer(); 73 74 if (m_renderingFlags & EndShadowLayer) 75 m_paintInfo->context->endTransparencyLayer(); 76 77 if (m_renderingFlags & RestoreGraphicsContext) 78 m_paintInfo->context->restore(); 79} 80 81void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintInfo& paintInfo, NeedsGraphicsContextSave needsGraphicsContextSave) 82{ 83 ASSERT(object); 84 85#ifndef NDEBUG 86 // This function must not be called twice! 87 ASSERT(!(m_renderingFlags & PrepareToRenderSVGContentWasCalled)); 88 m_renderingFlags |= PrepareToRenderSVGContentWasCalled; 89#endif 90 91 m_object = object; 92 m_paintInfo = &paintInfo; 93#if ENABLE(FILTERS) 94 m_filter = 0; 95#endif 96 97 // We need to save / restore the context even if the initialization failed. 98 if (needsGraphicsContextSave == SaveGraphicsContext) { 99 m_paintInfo->context->save(); 100 m_renderingFlags |= RestoreGraphicsContext; 101 } 102 103 RenderStyle* style = m_object->style(); 104 ASSERT(style); 105 106 const SVGRenderStyle* svgStyle = style->svgStyle(); 107 ASSERT(svgStyle); 108 109 // Setup transparency layers before setting up SVG resources! 110 bool isRenderingMask = isRenderingMaskImage(m_object); 111 float opacity = isRenderingMask ? 1 : style->opacity(); 112 const ShadowData* shadow = svgStyle->shadow(); 113 if (opacity < 1 || shadow) { 114 FloatRect repaintRect = m_object->repaintRectInLocalCoordinates(); 115 116 if (opacity < 1) { 117 m_paintInfo->context->clip(repaintRect); 118 m_paintInfo->context->beginTransparencyLayer(opacity); 119 m_renderingFlags |= EndOpacityLayer; 120 } 121 122 if (shadow) { 123 m_paintInfo->context->clip(repaintRect); 124 m_paintInfo->context->setShadow(IntSize(roundToInt(shadow->x()), roundToInt(shadow->y())), shadow->radius(), shadow->color(), style->colorSpace()); 125 m_paintInfo->context->beginTransparencyLayer(1); 126 m_renderingFlags |= EndShadowLayer; 127 } 128 } 129 130 ClipPathOperation* clipPathOperation = style->clipPath(); 131 if (clipPathOperation && clipPathOperation->getOperationType() == ClipPathOperation::SHAPE) { 132 ShapeClipPathOperation* clipPath = static_cast<ShapeClipPathOperation*>(clipPathOperation); 133 m_paintInfo->context->clipPath(clipPath->path(object->objectBoundingBox()), clipPath->windRule()); 134 } 135 136 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(m_object); 137 if (!resources) { 138#if ENABLE(FILTERS) 139 if (svgStyle->hasFilter()) 140 return; 141#endif 142 m_renderingFlags |= RenderingPrepared; 143 return; 144 } 145 146 if (!isRenderingMask) { 147 if (RenderSVGResourceMasker* masker = resources->masker()) { 148 if (!masker->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode)) 149 return; 150 } 151 } 152 153 RenderSVGResourceClipper* clipper = resources->clipper(); 154 if (!clipPathOperation && clipper) { 155 if (!clipper->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode)) 156 return; 157 } 158 159#if ENABLE(FILTERS) 160 if (!isRenderingMask) { 161 m_filter = resources->filter(); 162 if (m_filter) { 163 m_savedContext = m_paintInfo->context; 164 m_savedPaintRect = m_paintInfo->rect; 165 // Return with false here may mean that we don't need to draw the content 166 // (because it was either drawn before or empty) but we still need to apply the filter. 167 m_renderingFlags |= EndFilterLayer; 168 if (!m_filter->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode)) 169 return; 170 171 // Since we're caching the resulting bitmap and do not invalidate it on repaint rect 172 // changes, we need to paint the whole filter region. Otherwise, elements not visible 173 // at the time of the initial paint (due to scrolling, window size, etc.) will never 174 // be drawn. 175 m_paintInfo->rect = IntRect(m_filter->drawingRegion(m_object)); 176 } 177 } 178#endif 179 180 m_renderingFlags |= RenderingPrepared; 181} 182 183static AffineTransform& currentContentTransformation() 184{ 185 DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ()); 186 return s_currentContentTransformation; 187} 188 189float SVGRenderingContext::calculateScreenFontSizeScalingFactor(const RenderObject* renderer) 190{ 191 ASSERT(renderer); 192 193 AffineTransform ctm; 194 calculateTransformationToOutermostCoordinateSystem(renderer, ctm); 195 return narrowPrecisionToFloat(sqrt((pow(ctm.xScale(), 2) + pow(ctm.yScale(), 2)) / 2)); 196} 197 198void SVGRenderingContext::calculateTransformationToOutermostCoordinateSystem(const RenderObject* renderer, AffineTransform& absoluteTransform) 199{ 200 ASSERT(renderer); 201 absoluteTransform = currentContentTransformation(); 202 203 float deviceScaleFactor = 1; 204 if (Page* page = renderer->document()->page()) 205 deviceScaleFactor = page->deviceScaleFactor(); 206 207 // Walk up the render tree, accumulating SVG transforms. 208 while (renderer) { 209 absoluteTransform = renderer->localToParentTransform() * absoluteTransform; 210 if (renderer->isSVGRoot()) 211 break; 212 renderer = renderer->parent(); 213 } 214 215 // Continue walking up the layer tree, accumulating CSS transforms. 216 RenderLayer* layer = renderer ? renderer->enclosingLayer() : 0; 217 while (layer) { 218 if (TransformationMatrix* layerTransform = layer->transform()) 219 absoluteTransform = layerTransform->toAffineTransform() * absoluteTransform; 220 221 // We can stop at compositing layers, to match the backing resolution. 222 if (layer->isComposited()) 223 break; 224 225 layer = layer->parent(); 226 } 227 228 absoluteTransform.scale(deviceScaleFactor); 229} 230 231bool SVGRenderingContext::createImageBuffer(const FloatRect& targetRect, const AffineTransform& absoluteTransform, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace, RenderingMode renderingMode) 232{ 233 IntRect paintRect = calculateImageBufferRect(targetRect, absoluteTransform); 234 // Don't create empty ImageBuffers. 235 if (paintRect.isEmpty()) 236 return false; 237 238 IntSize clampedSize = clampedAbsoluteSize(paintRect.size()); 239 OwnPtr<ImageBuffer> image = ImageBuffer::create(clampedSize, 1, colorSpace, renderingMode); 240 if (!image) 241 return false; 242 243 GraphicsContext* imageContext = image->context(); 244 ASSERT(imageContext); 245 246 imageContext->scale(FloatSize(static_cast<float>(clampedSize.width()) / paintRect.width(), 247 static_cast<float>(clampedSize.height()) / paintRect.height())); 248 imageContext->translate(-paintRect.x(), -paintRect.y()); 249 imageContext->concatCTM(absoluteTransform); 250 251 imageBuffer = image.release(); 252 return true; 253} 254 255bool SVGRenderingContext::createImageBufferForPattern(const FloatRect& absoluteTargetRect, const FloatRect& clampedAbsoluteTargetRect, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace, RenderingMode renderingMode) 256{ 257 IntSize imageSize(roundedIntSize(clampedAbsoluteTargetRect.size())); 258 IntSize unclampedImageSize(roundedIntSize(absoluteTargetRect.size())); 259 260 // Don't create empty ImageBuffers. 261 if (imageSize.isEmpty()) 262 return false; 263 264 OwnPtr<ImageBuffer> image = ImageBuffer::create(imageSize, 1, colorSpace, renderingMode); 265 if (!image) 266 return false; 267 268 GraphicsContext* imageContext = image->context(); 269 ASSERT(imageContext); 270 271 // Compensate rounding effects, as the absolute target rect is using floating-point numbers and the image buffer size is integer. 272 imageContext->scale(FloatSize(unclampedImageSize.width() / absoluteTargetRect.width(), unclampedImageSize.height() / absoluteTargetRect.height())); 273 274 imageBuffer = image.release(); 275 return true; 276} 277 278void SVGRenderingContext::renderSubtreeToImageBuffer(ImageBuffer* image, RenderObject* item, const AffineTransform& subtreeContentTransformation) 279{ 280 ASSERT(item); 281 ASSERT(image); 282 ASSERT(image->context()); 283 284 PaintInfo info(image->context(), PaintInfo::infiniteRect(), PaintPhaseForeground, PaintBehaviorNormal); 285 286 AffineTransform& contentTransformation = currentContentTransformation(); 287 AffineTransform savedContentTransformation = contentTransformation; 288 contentTransformation = subtreeContentTransformation * contentTransformation; 289 290 ASSERT(!item->needsLayout()); 291 item->paint(info, IntPoint()); 292 293 contentTransformation = savedContentTransformation; 294} 295 296void SVGRenderingContext::clipToImageBuffer(GraphicsContext* context, const AffineTransform& absoluteTransform, const FloatRect& targetRect, OwnPtr<ImageBuffer>& imageBuffer, bool safeToClear) 297{ 298 ASSERT(context); 299 ASSERT(imageBuffer); 300 301 FloatRect absoluteTargetRect = calculateImageBufferRect(targetRect, absoluteTransform); 302 303 // The mask image has been created in the absolute coordinate space, as the image should not be scaled. 304 // So the actual masking process has to be done in the absolute coordinate space as well. 305 context->concatCTM(absoluteTransform.inverse()); 306 context->clipToImageBuffer(imageBuffer.get(), absoluteTargetRect); 307 context->concatCTM(absoluteTransform); 308 309 // When nesting resources, with objectBoundingBox as content unit types, there's no use in caching the 310 // resulting image buffer as the parent resource already caches the result. 311 if (safeToClear && !currentContentTransformation().isIdentity()) 312 imageBuffer.clear(); 313} 314 315FloatRect SVGRenderingContext::clampedAbsoluteTargetRect(const FloatRect& absoluteTargetRect) 316{ 317 const FloatSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize); 318 return FloatRect(absoluteTargetRect.location(), absoluteTargetRect.size().shrunkTo(maxImageBufferSize)); 319} 320 321IntSize SVGRenderingContext::clampedAbsoluteSize(const IntSize& absoluteSize) 322{ 323 const IntSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize); 324 return absoluteSize.shrunkTo(maxImageBufferSize); 325} 326 327void SVGRenderingContext::clear2DRotation(AffineTransform& transform) 328{ 329 AffineTransform::DecomposedType decomposition; 330 transform.decompose(decomposition); 331 decomposition.angle = 0; 332 transform.recompose(decomposition); 333} 334 335bool SVGRenderingContext::bufferForeground(OwnPtr<ImageBuffer>& imageBuffer) 336{ 337 ASSERT(m_paintInfo); 338 ASSERT(m_object->isSVGImage()); 339 FloatRect boundingBox = m_object->objectBoundingBox(); 340 341 // Invalidate an existing buffer if the scale is not correct. 342 if (imageBuffer) { 343 AffineTransform transform = m_paintInfo->context->getCTM(GraphicsContext::DefinitelyIncludeDeviceScale); 344 IntSize expandedBoundingBox = expandedIntSize(boundingBox.size()); 345 IntSize bufferSize(static_cast<int>(ceil(expandedBoundingBox.width() * transform.xScale())), static_cast<int>(ceil(expandedBoundingBox.height() * transform.yScale()))); 346 if (bufferSize != imageBuffer->internalSize()) 347 imageBuffer.clear(); 348 } 349 350 // Create a new buffer and paint the foreground into it. 351 if (!imageBuffer) { 352 if ((imageBuffer = m_paintInfo->context->createCompatibleBuffer(expandedIntSize(boundingBox.size()), true))) { 353 GraphicsContext* bufferedRenderingContext = imageBuffer->context(); 354 bufferedRenderingContext->translate(-boundingBox.x(), -boundingBox.y()); 355 PaintInfo bufferedInfo(*m_paintInfo); 356 bufferedInfo.context = bufferedRenderingContext; 357 toRenderSVGImage(m_object)->paintForeground(bufferedInfo); 358 } else 359 return false; 360 } 361 362 m_paintInfo->context->drawImageBuffer(imageBuffer.get(), ColorSpaceDeviceRGB, boundingBox); 363 return true; 364} 365 366} 367 368#endif 369