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/layout/svg/LayoutSVGResourceMasker.h"
23 #include "core/dom/ElementTraversal.h"
24 #include "core/layout/svg/SVGLayoutSupport.h"
25 #include "core/paint/SVGPaintContext.h"
26 #include "core/svg/SVGElement.h"
27 #include "platform/graphics/paint/SkPictureBuilder.h"
28 #include "platform/transforms/AffineTransform.h"
29 #include "third_party/skia/include/core/SkPicture.h"
33 LayoutSVGResourceMasker::LayoutSVGResourceMasker(SVGMaskElement
* node
)
34 : LayoutSVGResourceContainer(node
)
38 LayoutSVGResourceMasker::~LayoutSVGResourceMasker()
42 void LayoutSVGResourceMasker::removeAllClientsFromCache(bool markForInvalidation
)
44 m_maskContentPicture
.clear();
45 m_maskContentBoundaries
= FloatRect();
46 markAllClientsForInvalidation(markForInvalidation
? LayoutAndBoundariesInvalidation
: ParentOnlyInvalidation
);
49 void LayoutSVGResourceMasker::removeClientFromCache(LayoutObject
* client
, bool markForInvalidation
)
52 markClientForInvalidation(client
, markForInvalidation
? BoundariesInvalidation
: ParentOnlyInvalidation
);
55 PassRefPtr
<const SkPicture
> LayoutSVGResourceMasker::createContentPicture(AffineTransform
& contentTransformation
, const FloatRect
& targetBoundingBox
,
56 GraphicsContext
* context
)
58 SVGUnitTypes::SVGUnitType contentUnits
= toSVGMaskElement(element())->maskContentUnits()->currentValue()->enumValue();
59 if (contentUnits
== SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX
) {
60 contentTransformation
.translate(targetBoundingBox
.x(), targetBoundingBox
.y());
61 contentTransformation
.scaleNonUniform(targetBoundingBox
.width(), targetBoundingBox
.height());
64 if (m_maskContentPicture
)
65 return m_maskContentPicture
;
67 SubtreeContentTransformScope
contentTransformScope(contentTransformation
);
69 // Using strokeBoundingBox (instead of paintInvalidationRectInLocalCoordinates) to avoid the intersection
70 // with local clips/mask, which may yield incorrect results when mixing objectBoundingBox and
71 // userSpaceOnUse units (http://crbug.com/294900).
72 FloatRect bounds
= strokeBoundingBox();
74 SkPictureBuilder
pictureBuilder(bounds
, nullptr, context
);
76 ColorFilter maskContentFilter
= style()->svgStyle().colorInterpolation() == CI_LINEARRGB
77 ? ColorFilterSRGBToLinearRGB
: ColorFilterNone
;
78 pictureBuilder
.context().setColorFilter(maskContentFilter
);
80 for (SVGElement
* childElement
= Traversal
<SVGElement
>::firstChild(*element()); childElement
; childElement
= Traversal
<SVGElement
>::nextSibling(*childElement
)) {
81 LayoutObject
* layoutObject
= childElement
->layoutObject();
84 const ComputedStyle
* style
= layoutObject
->style();
85 if (!style
|| style
->display() == NONE
|| style
->visibility() != VISIBLE
)
88 SVGPaintContext::paintSubtree(&pictureBuilder
.context(), layoutObject
);
91 m_maskContentPicture
= pictureBuilder
.endRecording();
92 return m_maskContentPicture
;
95 void LayoutSVGResourceMasker::calculateMaskContentPaintInvalidationRect()
97 for (SVGElement
* childElement
= Traversal
<SVGElement
>::firstChild(*element()); childElement
; childElement
= Traversal
<SVGElement
>::nextSibling(*childElement
)) {
98 LayoutObject
* layoutObject
= childElement
->layoutObject();
101 const ComputedStyle
* style
= layoutObject
->style();
102 if (!style
|| style
->display() == NONE
|| style
->visibility() != VISIBLE
)
104 m_maskContentBoundaries
.unite(layoutObject
->localToParentTransform().mapRect(layoutObject
->paintInvalidationRectInLocalCoordinates()));
108 FloatRect
LayoutSVGResourceMasker::resourceBoundingBox(const LayoutObject
* object
)
110 SVGMaskElement
* maskElement
= toSVGMaskElement(element());
113 FloatRect objectBoundingBox
= object
->objectBoundingBox();
114 FloatRect maskBoundaries
= SVGLengthContext::resolveRectangle
<SVGMaskElement
>(maskElement
, maskElement
->maskUnits()->currentValue()->enumValue(), objectBoundingBox
);
116 // Resource was not layouted yet. Give back clipping rect of the mask.
117 if (selfNeedsLayout())
118 return maskBoundaries
;
120 if (m_maskContentBoundaries
.isEmpty())
121 calculateMaskContentPaintInvalidationRect();
123 FloatRect maskRect
= m_maskContentBoundaries
;
124 if (maskElement
->maskContentUnits()->currentValue()->value() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX
) {
125 AffineTransform transform
;
126 transform
.translate(objectBoundingBox
.x(), objectBoundingBox
.y());
127 transform
.scaleNonUniform(objectBoundingBox
.width(), objectBoundingBox
.height());
128 maskRect
= transform
.mapRect(maskRect
);
131 maskRect
.intersect(maskBoundaries
);