1 | /* |
2 | * Copyright (C) 2011 Apple Inc. |
3 | * Copyright (C) 2010 Sencha, Inc. |
4 | * Copyright (C) 2010 Igalia S.L. |
5 | * All rights reserved. |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
20 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
21 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
24 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | #pragma once |
30 | |
31 | #include "Color.h" |
32 | #include "FloatRect.h" |
33 | #include "FloatRoundedRect.h" |
34 | #include <wtf/Function.h> |
35 | #include <wtf/Noncopyable.h> |
36 | |
37 | namespace WebCore { |
38 | |
39 | class AffineTransform; |
40 | class GraphicsContext; |
41 | struct GraphicsContextState; |
42 | class ImageBuffer; |
43 | |
44 | class ShadowBlur { |
45 | WTF_MAKE_NONCOPYABLE(ShadowBlur); |
46 | public: |
47 | enum ShadowType { |
48 | NoShadow, |
49 | SolidShadow, |
50 | BlurShadow |
51 | }; |
52 | |
53 | ShadowBlur(); |
54 | ShadowBlur(const FloatSize& radius, const FloatSize& offset, const Color&, bool shadowsIgnoreTransforms = false); |
55 | ShadowBlur(const GraphicsContextState&); |
56 | |
57 | void setShadowValues(const FloatSize&, const FloatSize& , const Color&, bool ignoreTransforms = false); |
58 | |
59 | void setShadowsIgnoreTransforms(bool ignoreTransforms) { m_shadowsIgnoreTransforms = ignoreTransforms; } |
60 | bool shadowsIgnoreTransforms() const { return m_shadowsIgnoreTransforms; } |
61 | |
62 | void drawRectShadow(GraphicsContext&, const FloatRoundedRect&); |
63 | void drawInsetShadow(GraphicsContext&, const FloatRect&, const FloatRoundedRect& holeRect); |
64 | |
65 | using DrawBufferCallback = WTF::Function<void(ImageBuffer&, const FloatPoint&, const FloatSize&)>; |
66 | using DrawImageCallback = WTF::Function<void(ImageBuffer&, const FloatRect&, const FloatRect&)>; |
67 | using FillRectCallback = WTF::Function<void(const FloatRect&, const Color&)>; |
68 | using FillRectWithHoleCallback = WTF::Function<void(const FloatRect&, const FloatRect&, const Color&)>; |
69 | using DrawShadowCallback = WTF::Function<void(GraphicsContext&)>; |
70 | |
71 | // DrawBufferCallback is for drawing shadow without tiling. |
72 | // DrawImageCallback and FillRectCallback is for drawing shadow with tiling. |
73 | void drawRectShadow(const AffineTransform&, const IntRect& clipBounds, const FloatRoundedRect& shadowedRect, const DrawBufferCallback&, const DrawImageCallback&, const FillRectCallback&); |
74 | void drawInsetShadow(const AffineTransform&, const IntRect& clipBounds, const FloatRect& fullRect, const FloatRoundedRect& holeRect, const DrawBufferCallback&, const DrawImageCallback&, const FillRectWithHoleCallback&); |
75 | void drawShadowLayer(const AffineTransform&, const IntRect& clipBounds, const FloatRect& layerArea, const DrawShadowCallback&, const DrawBufferCallback&); |
76 | |
77 | void blurLayerImage(unsigned char*, const IntSize&, int stride); |
78 | |
79 | void clear(); |
80 | |
81 | ShadowType type() const { return m_type; } |
82 | |
83 | private: |
84 | void updateShadowBlurValues(); |
85 | |
86 | void drawShadowBuffer(GraphicsContext&); |
87 | |
88 | void adjustBlurRadius(const AffineTransform&); |
89 | |
90 | enum ShadowDirection { |
91 | OuterShadow, |
92 | InnerShadow |
93 | }; |
94 | |
95 | IntSize calculateLayerBoundingRect(const AffineTransform&, const FloatRect& layerArea, const IntRect& clipRect); |
96 | IntSize templateSize(const IntSize& blurredEdgeSize, const FloatRoundedRect::Radii&) const; |
97 | |
98 | void blurShadowBuffer(const IntSize& templateSize); |
99 | void blurAndColorShadowBuffer(const IntSize& templateSize); |
100 | |
101 | void drawInsetShadowWithoutTiling(const AffineTransform&, const FloatRect& fullRect, const FloatRoundedRect& holeRect, const IntSize& layerSize, const DrawBufferCallback&); |
102 | void drawInsetShadowWithTiling(const AffineTransform&, const FloatRect& fullRect, const FloatRoundedRect& holeRect, const IntSize& shadowTemplateSize, const IntSize& blurredEdgeSize, const DrawImageCallback&, const FillRectWithHoleCallback&); |
103 | |
104 | void drawRectShadowWithoutTiling(const AffineTransform&, const FloatRoundedRect& shadowedRect, const IntSize& layerSize, const DrawBufferCallback&); |
105 | void drawRectShadowWithTiling(const AffineTransform&, const FloatRoundedRect& shadowedRect, const IntSize& shadowTemplateSize, const IntSize& blurredEdgeSize, const DrawImageCallback&, const FillRectCallback&); |
106 | |
107 | void drawLayerPiecesAndFillCenter(const FloatRect& shadowBounds, const FloatRoundedRect::Radii&, const IntSize& roundedRadius, const IntSize& templateSize, const DrawImageCallback&, const FillRectCallback&); |
108 | void drawLayerPieces(const FloatRect& shadowBounds, const FloatRoundedRect::Radii&, const IntSize& roundedRadius, const IntSize& templateSize, const DrawImageCallback&); |
109 | |
110 | IntSize blurredEdgeSize() const; |
111 | |
112 | ShadowType m_type { NoShadow }; |
113 | |
114 | Color m_color; |
115 | FloatSize m_blurRadius; |
116 | FloatSize m_offset; |
117 | |
118 | ImageBuffer* m_layerImage { nullptr }; // Buffer to where the temporary shadow will be drawn to. |
119 | |
120 | FloatSize m_shadowedResultSize; // Size of the result of shadowing which is same as shadowedRect + blurred edges. |
121 | FloatPoint m_layerOrigin; // Top-left corner of the (possibly clipped) bounding rect to draw the shadow to. |
122 | FloatSize m_layerSize; // Size of m_layerImage pixels that need blurring. |
123 | FloatSize m_layerContextTranslation; // Translation to apply to m_layerContext for the shadow to be correctly clipped. |
124 | |
125 | bool m_shadowsIgnoreTransforms { false }; |
126 | }; |
127 | |
128 | } // namespace WebCore |
129 | |