1 /* 2 * Copyright (C) 2007, 2008 Rob Buis <buis (at) kde.org> 3 * Copyright (C) 2007 Nikolas Zimmermann <zimmermann (at) kde.org> 4 * Copyright (C) 2007 Eric Seidel <eric (at) webkit.org> 5 * Copyright (C) 2009 Google, Inc. All rights reserved. 6 * Copyright (C) 2009 Dirk Schulze <krit (at) 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 #include "core/rendering/svg/SVGRenderingContext.h" 28 29 #include "core/frame/Frame.h" 30 #include "core/frame/FrameView.h" 31 #include "core/page/Page.h" 32 #include "core/rendering/RenderLayer.h" 33 #include "core/rendering/svg/RenderSVGImage.h" 34 #include "core/rendering/svg/RenderSVGResource.h" 35 #include "core/rendering/svg/RenderSVGResourceFilter.h" 36 #include "core/rendering/svg/RenderSVGResourceMasker.h" 37 #include "core/rendering/svg/SVGResources.h" 38 #include "core/rendering/svg/SVGResourcesCache.h" 39 40 static int kMaxImageBufferSize = 4096; 41 42 namespace WebCore { 43 44 static inline bool isRenderingMaskImage(RenderObject* object) 45 { 46 if (object->frame() && object->frame()->view()) 47 return object->frame()->view()->paintBehavior() & PaintBehaviorRenderingSVGMask; 48 return false; 49 } 50 51 SVGRenderingContext::~SVGRenderingContext() 52 { 53 // Fast path if we don't need to restore anything. 54 if (!(m_renderingFlags & ActionsNeeded)) 55 return; 56 57 ASSERT(m_object && m_paintInfo); 58 59 if (m_renderingFlags & PostApplyResources) { 60 ASSERT(m_masker || m_clipper || m_filter); 61 ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)); 62 63 if (m_filter) { 64 ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->filter() == 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 70 if (m_clipper) { 71 ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->clipper() == m_clipper); 72 m_clipper->postApplyStatefulResource(m_object, m_paintInfo->context, m_clipperContext); 73 } 74 75 if (m_masker) { 76 ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->masker() == m_masker); 77 m_masker->postApplyResource(m_object, m_paintInfo->context, ApplyToDefaultMode, 0, 0); 78 } 79 } 80 81 if (m_renderingFlags & EndOpacityLayer) 82 m_paintInfo->context->endLayer(); 83 84 if (m_renderingFlags & RestoreGraphicsContext) 85 m_paintInfo->context->restore(); 86 } 87 88 void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintInfo& paintInfo, NeedsGraphicsContextSave needsGraphicsContextSave) 89 { 90 ASSERT(object); 91 92 #ifndef NDEBUG 93 // This function must not be called twice! 94 ASSERT(!(m_renderingFlags & PrepareToRenderSVGContentWasCalled)); 95 m_renderingFlags |= PrepareToRenderSVGContentWasCalled; 96 #endif 97 98 m_object = object; 99 m_paintInfo = &paintInfo; 100 m_filter = 0; 101 102 // We need to save / restore the context even if the initialization failed. 103 if (needsGraphicsContextSave == SaveGraphicsContext) { 104 m_paintInfo->context->save(); 105 m_renderingFlags |= RestoreGraphicsContext; 106 } 107 108 RenderStyle* style = m_object->style(); 109 ASSERT(style); 110 111 const SVGRenderStyle* svgStyle = style->svgStyle(); 112 ASSERT(svgStyle); 113 114 // Setup transparency layers before setting up SVG resources! 115 bool isRenderingMask = isRenderingMaskImage(m_object); 116 float opacity = isRenderingMask ? 1 : style->opacity(); 117 blink::WebBlendMode blendMode = isRenderingMask ? blink::WebBlendModeNormal : style->blendMode(); 118 if (opacity < 1 || blendMode != blink::WebBlendModeNormal) { 119 FloatRect repaintRect = m_object->repaintRectInLocalCoordinates(); 120 121 if (opacity < 1 || blendMode != blink::WebBlendModeNormal) { 122 m_paintInfo->context->clip(repaintRect); 123 if (blendMode != blink::WebBlendModeNormal) { 124 if (!(m_renderingFlags & RestoreGraphicsContext)) { 125 m_paintInfo->context->save(); 126 m_renderingFlags |= RestoreGraphicsContext; 127 } 128 m_paintInfo->context->setCompositeOperation(CompositeSourceOver, blendMode); 129 } 130 m_paintInfo->context->beginTransparencyLayer(opacity); 131 m_renderingFlags |= EndOpacityLayer; 132 } 133 } 134 135 ClipPathOperation* clipPathOperation = style->clipPath(); 136 if (clipPathOperation && clipPathOperation->type() == ClipPathOperation::SHAPE) { 137 ShapeClipPathOperation* clipPath = toShapeClipPathOperation(clipPathOperation); 138 m_paintInfo->context->clipPath(clipPath->path(object->objectBoundingBox()), clipPath->windRule()); 139 } 140 141 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(m_object); 142 if (!resources) { 143 if (svgStyle->hasFilter()) 144 return; 145 146 m_renderingFlags |= RenderingPrepared; 147 return; 148 } 149 150 if (!isRenderingMask) { 151 if (RenderSVGResourceMasker* masker = resources->masker()) { 152 if (!masker->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode)) 153 return; 154 m_masker = masker; 155 m_renderingFlags |= PostApplyResources; 156 } 157 } 158 159 RenderSVGResourceClipper* clipper = resources->clipper(); 160 if (!clipPathOperation && clipper) { 161 if (!clipper->applyStatefulResource(m_object, m_paintInfo->context, m_clipperContext)) 162 return; 163 m_clipper = clipper; 164 m_renderingFlags |= PostApplyResources; 165 } 166 167 if (!isRenderingMask) { 168 m_filter = resources->filter(); 169 if (m_filter) { 170 m_savedContext = m_paintInfo->context; 171 m_savedPaintRect = m_paintInfo->rect; 172 // Return with false here may mean that we don't need to draw the content 173 // (because it was either drawn before or empty) but we still need to apply the filter. 174 m_renderingFlags |= PostApplyResources; 175 if (!m_filter->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode)) 176 return; 177 178 // Since we're caching the resulting bitmap and do not invalidate it on repaint rect 179 // changes, we need to paint the whole filter region. Otherwise, elements not visible 180 // at the time of the initial paint (due to scrolling, window size, etc.) will never 181 // be drawn. 182 m_paintInfo->rect = IntRect(m_filter->drawingRegion(m_object)); 183 } 184 } 185 186 m_renderingFlags |= RenderingPrepared; 187 } 188 189 static AffineTransform& currentContentTransformation() 190 { 191 DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ()); 192 return s_currentContentTransformation; 193 } 194 195 float SVGRenderingContext::calculateScreenFontSizeScalingFactor(const RenderObject* renderer) 196 { 197 ASSERT(renderer); 198 199 AffineTransform ctm; 200 calculateTransformationToOutermostCoordinateSystem(renderer, ctm); 201 return narrowPrecisionToFloat(sqrt((pow(ctm.xScale(), 2) + pow(ctm.yScale(), 2)) / 2)); 202 } 203 204 void SVGRenderingContext::calculateTransformationToOutermostCoordinateSystem(const RenderObject* renderer, AffineTransform& absoluteTransform) 205 { 206 ASSERT(renderer); 207 absoluteTransform = currentContentTransformation(); 208 209 float deviceScaleFactor = 1; 210 if (Page* page = renderer->document().page()) 211 deviceScaleFactor = page->deviceScaleFactor(); 212 213 // Walk up the render tree, accumulating SVG transforms. 214 while (renderer) { 215 absoluteTransform = renderer->localToParentTransform() * absoluteTransform; 216 if (renderer->isSVGRoot()) 217 break; 218 renderer = renderer->parent(); 219 } 220 221 // Continue walking up the layer tree, accumulating CSS transforms. 222 RenderLayer* layer = renderer ? renderer->enclosingLayer() : 0; 223 while (layer) { 224 if (TransformationMatrix* layerTransform = layer->transform()) 225 absoluteTransform = layerTransform->toAffineTransform() * absoluteTransform; 226 227 // We can stop at compositing layers, to match the backing resolution. 228 // FIXME: should we be computing the transform to the nearest composited layer, 229 // or the nearest composited layer that does not paint into its ancestor? 230 // I think this is the nearest composited ancestor since we will inherit its 231 // transforms in the composited layer tree. 232 if (layer->hasCompositedLayerMapping()) 233 break; 234 235 layer = layer->parent(); 236 } 237 238 if (deviceScaleFactor != 1) 239 absoluteTransform.scale(deviceScaleFactor); 240 } 241 242 void SVGRenderingContext::renderSubtree(GraphicsContext* context, RenderObject* item, const AffineTransform& subtreeContentTransformation) 243 { 244 ASSERT(item); 245 ASSERT(context); 246 247 PaintInfo info(context, PaintInfo::infiniteRect(), PaintPhaseForeground, PaintBehaviorNormal); 248 249 AffineTransform& contentTransformation = currentContentTransformation(); 250 AffineTransform savedContentTransformation = contentTransformation; 251 contentTransformation = subtreeContentTransformation * contentTransformation; 252 253 ASSERT(!item->needsLayout()); 254 item->paint(info, IntPoint()); 255 256 contentTransformation = savedContentTransformation; 257 } 258 259 FloatRect SVGRenderingContext::clampedAbsoluteTargetRect(const FloatRect& absoluteTargetRect) 260 { 261 const FloatSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize); 262 return FloatRect(absoluteTargetRect.location(), absoluteTargetRect.size().shrunkTo(maxImageBufferSize)); 263 } 264 265 void SVGRenderingContext::clear2DRotation(AffineTransform& transform) 266 { 267 AffineTransform::DecomposedType decomposition; 268 transform.decompose(decomposition); 269 decomposition.angle = 0; 270 transform.recompose(decomposition); 271 } 272 273 bool SVGRenderingContext::bufferForeground(OwnPtr<ImageBuffer>& imageBuffer) 274 { 275 ASSERT(m_paintInfo); 276 ASSERT(m_object->isSVGImage()); 277 FloatRect boundingBox = m_object->objectBoundingBox(); 278 279 // Invalidate an existing buffer if the scale is not correct. 280 if (imageBuffer) { 281 AffineTransform transform = m_paintInfo->context->getCTM(GraphicsContext::DefinitelyIncludeDeviceScale); 282 IntSize expandedBoundingBox = expandedIntSize(boundingBox.size()); 283 IntSize bufferSize(static_cast<int>(ceil(expandedBoundingBox.width() * transform.xScale())), static_cast<int>(ceil(expandedBoundingBox.height() * transform.yScale()))); 284 if (bufferSize != imageBuffer->size()) 285 imageBuffer.clear(); 286 } 287 288 // Create a new buffer and paint the foreground into it. 289 if (!imageBuffer) { 290 if ((imageBuffer = m_paintInfo->context->createCompatibleBuffer(expandedIntSize(boundingBox.size())))) { 291 GraphicsContext* bufferedRenderingContext = imageBuffer->context(); 292 bufferedRenderingContext->translate(-boundingBox.x(), -boundingBox.y()); 293 PaintInfo bufferedInfo(*m_paintInfo); 294 bufferedInfo.context = bufferedRenderingContext; 295 toRenderSVGImage(m_object)->paintForeground(bufferedInfo); 296 } else 297 return false; 298 } 299 300 m_paintInfo->context->drawImageBuffer(imageBuffer.get(), boundingBox); 301 return true; 302 } 303 304 } 305