1/*
2 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
3 *
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.
8 *
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.
13 *
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.
18 */
19
20#pragma once
21
22#include "RenderSVGResourceContainer.h"
23#include "SVGClipPathElement.h"
24#include "SVGUnitTypes.h"
25
26#include <wtf/HashMap.h>
27
28namespace WebCore {
29
30class GraphicsContext;
31class ImageBuffer;
32
33typedef std::unique_ptr<ImageBuffer> ClipperMaskImage;
34
35class RenderSVGResourceClipper final : public RenderSVGResourceContainer {
36 WTF_MAKE_ISO_ALLOCATED(RenderSVGResourceClipper);
37public:
38 RenderSVGResourceClipper(SVGClipPathElement&, RenderStyle&&);
39 virtual ~RenderSVGResourceClipper();
40
41 SVGClipPathElement& clipPathElement() const { return downcast<SVGClipPathElement>(nodeForNonAnonymous()); }
42
43 void removeAllClientsFromCache(bool markForInvalidation = true) override;
44 void removeClientFromCache(RenderElement&, bool markForInvalidation = true) override;
45
46 bool applyResource(RenderElement&, const RenderStyle&, GraphicsContext*&, OptionSet<RenderSVGResourceMode>) override;
47 // clipPath can be clipped too, but don't have a boundingBox or repaintRect. So we can't call
48 // applyResource directly and use the rects from the object, since they are empty for RenderSVGResources
49 // FIXME: We made applyClippingToContext public because we cannot call applyResource on HTML elements (it asserts on RenderObject::objectBoundingBox)
50 bool applyClippingToContext(RenderElement&, const FloatRect&, const FloatRect&, GraphicsContext&);
51 FloatRect resourceBoundingBox(const RenderObject&) override;
52
53 RenderSVGResourceType resourceType() const override { return ClipperResourceType; }
54
55 bool hitTestClipContent(const FloatRect&, const FloatPoint&);
56
57 SVGUnitTypes::SVGUnitType clipPathUnits() const { return clipPathElement().clipPathUnits(); }
58
59protected:
60 bool selfNeedsClientInvalidation() const override { return (everHadLayout() || m_clipper.size()) && selfNeedsLayout(); }
61
62private:
63 void element() const = delete;
64
65 const char* renderName() const override { return "RenderSVGResourceClipper"; }
66 bool isSVGResourceClipper() const override { return true; }
67
68 bool pathOnlyClipping(GraphicsContext&, const AffineTransform&, const FloatRect&);
69 bool drawContentIntoMaskImage(const ClipperMaskImage&, const FloatRect& objectBoundingBox);
70 void calculateClipContentRepaintRect();
71 ClipperMaskImage& addRendererToClipper(const RenderObject&);
72
73 FloatRect m_clipBoundaries;
74 HashMap<const RenderObject*, ClipperMaskImage> m_clipper;
75};
76
77}
78
79SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::RenderSVGResourceClipper)
80static bool isType(const WebCore::RenderObject& renderer) { return renderer.isSVGResourceClipper(); }
81static bool isType(const WebCore::RenderSVGResource& resource) { return resource.resourceType() == WebCore::ClipperResourceType; }
82SPECIALIZE_TYPE_TRAITS_END()
83