2 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "core/rendering/svg/RenderSVGResourceMasker.h"
23 #include "core/dom/ElementTraversal.h"
24 #include "core/rendering/svg/SVGRenderingContext.h"
25 #include "core/svg/SVGElement.h"
26 #include "platform/graphics/DisplayList.h"
27 #include "platform/graphics/GraphicsContextStateSaver.h"
28 #include "platform/transforms/AffineTransform.h"
32 RenderSVGResourceMasker::RenderSVGResourceMasker(SVGMaskElement* node)
33 : RenderSVGResourceContainer(node)
37 RenderSVGResourceMasker::~RenderSVGResourceMasker()
41 void RenderSVGResourceMasker::removeAllClientsFromCache(bool markForInvalidation)
43 m_maskContentDisplayList.clear();
44 m_maskContentBoundaries = FloatRect();
45 markAllClientsForInvalidation(markForInvalidation ? LayoutAndBoundariesInvalidation : ParentOnlyInvalidation);
48 void RenderSVGResourceMasker::removeClientFromCache(RenderObject* client, bool markForInvalidation)
51 markClientForInvalidation(client, markForInvalidation ? BoundariesInvalidation : ParentOnlyInvalidation);
54 bool RenderSVGResourceMasker::prepareEffect(RenderObject* object, GraphicsContext*& context)
59 ASSERT_WITH_SECURITY_IMPLICATION(!needsLayout());
61 clearInvalidationMask();
63 FloatRect paintInvalidationRect = object->paintInvalidationRectInLocalCoordinates();
64 if (paintInvalidationRect.isEmpty() || !element()->hasChildren())
67 // Content layer start.
68 context->beginTransparencyLayer(1, &paintInvalidationRect);
73 void RenderSVGResourceMasker::finishEffect(RenderObject* object, GraphicsContext*& context)
78 ASSERT_WITH_SECURITY_IMPLICATION(!needsLayout());
80 FloatRect paintInvalidationRect = object->paintInvalidationRectInLocalCoordinates();
82 const SVGRenderStyle& svgStyle = style()->svgStyle();
83 ColorFilter maskLayerFilter = svgStyle.maskType() == MT_LUMINANCE
84 ? ColorFilterLuminanceToAlpha : ColorFilterNone;
85 ColorFilter maskContentFilter = svgStyle.colorInterpolation() == CI_LINEARRGB
86 ? ColorFilterSRGBToLinearRGB : ColorFilterNone;
89 context->beginLayer(1, CompositeDestinationIn, &paintInvalidationRect, maskLayerFilter);
91 // Draw the mask with color conversion (when needed).
92 GraphicsContextStateSaver maskContentSaver(*context);
93 context->setColorFilter(maskContentFilter);
95 drawMaskForRenderer(context, object->objectBoundingBox());
98 // Transfer mask layer -> content layer (DstIn)
100 // Transfer content layer -> backdrop (SrcOver)
104 void RenderSVGResourceMasker::drawMaskForRenderer(GraphicsContext* context, const FloatRect& targetBoundingBox)
108 AffineTransform contentTransformation;
109 SVGUnitTypes::SVGUnitType contentUnits = toSVGMaskElement(element())->maskContentUnits()->currentValue()->enumValue();
110 if (contentUnits == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
111 contentTransformation.translate(targetBoundingBox.x(), targetBoundingBox.y());
112 contentTransformation.scaleNonUniform(targetBoundingBox.width(), targetBoundingBox.height());
113 context->concatCTM(contentTransformation);
116 if (!m_maskContentDisplayList) {
117 SubtreeContentTransformScope contentTransformScope(contentTransformation);
118 createDisplayList(context);
120 ASSERT(m_maskContentDisplayList);
121 context->drawDisplayList(m_maskContentDisplayList.get());
124 void RenderSVGResourceMasker::createDisplayList(GraphicsContext* context)
128 // Using strokeBoundingBox (instead of paintInvalidationRectInLocalCoordinates) to avoid the intersection
129 // with local clips/mask, which may yield incorrect results when mixing objectBoundingBox and
130 // userSpaceOnUse units (http://crbug.com/294900).
131 FloatRect bounds = strokeBoundingBox();
132 context->beginRecording(bounds);
133 for (SVGElement* childElement = Traversal<SVGElement>::firstChild(*element()); childElement; childElement = Traversal<SVGElement>::nextSibling(*childElement)) {
134 RenderObject* renderer = childElement->renderer();
137 RenderStyle* style = renderer->style();
138 if (!style || style->display() == NONE || style->visibility() != VISIBLE)
141 SVGRenderingContext::renderSubtree(context, renderer);
143 m_maskContentDisplayList = context->endRecording();
146 void RenderSVGResourceMasker::calculateMaskContentPaintInvalidationRect()
148 for (SVGElement* childElement = Traversal<SVGElement>::firstChild(*element()); childElement; childElement = Traversal<SVGElement>::nextSibling(*childElement)) {
149 RenderObject* renderer = childElement->renderer();
152 RenderStyle* style = renderer->style();
153 if (!style || style->display() == NONE || style->visibility() != VISIBLE)
155 m_maskContentBoundaries.unite(renderer->localToParentTransform().mapRect(renderer->paintInvalidationRectInLocalCoordinates()));
159 FloatRect RenderSVGResourceMasker::resourceBoundingBox(const RenderObject* object)
161 SVGMaskElement* maskElement = toSVGMaskElement(element());
164 FloatRect objectBoundingBox = object->objectBoundingBox();
165 FloatRect maskBoundaries = SVGLengthContext::resolveRectangle<SVGMaskElement>(maskElement, maskElement->maskUnits()->currentValue()->enumValue(), objectBoundingBox);
167 // Resource was not layouted yet. Give back clipping rect of the mask.
168 if (selfNeedsLayout())
169 return maskBoundaries;
171 if (m_maskContentBoundaries.isEmpty())
172 calculateMaskContentPaintInvalidationRect();
174 FloatRect maskRect = m_maskContentBoundaries;
175 if (maskElement->maskContentUnits()->currentValue()->value() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
176 AffineTransform transform;
177 transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
178 transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
179 maskRect = transform.mapRect(maskRect);
182 maskRect.intersect(maskBoundaries);