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 COMPUTER, 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 #ifndef ShadowBlur_h
30 #define ShadowBlur_h
31 
32 #include "Color.h"
33 #include "ColorSpace.h"
34 #include "FloatRect.h"
35 #include "RoundedIntRect.h"
36 #include <wtf/Noncopyable.h>
37 
38 namespace WebCore {
39 
40 class AffineTransform;
41 class GraphicsContext;
42 class ImageBuffer;
43 
44 class ShadowBlur {
45     WTF_MAKE_NONCOPYABLE(ShadowBlur);
46 public:
47     ShadowBlur(const FloatSize& radius, const FloatSize& offset, const Color&, ColorSpace);
48 
setShadowsIgnoreTransforms(bool ignoreTransforms)49     void setShadowsIgnoreTransforms(bool ignoreTransforms) { m_shadowsIgnoreTransforms = ignoreTransforms; }
shadowsIgnoreTransforms()50     bool shadowsIgnoreTransforms() const { return m_shadowsIgnoreTransforms; }
51 
52     void drawRectShadow(GraphicsContext*, const FloatRect&, const RoundedIntRect::Radii&);
53     void drawInsetShadow(GraphicsContext*, const FloatRect&, const FloatRect& holeRect, const RoundedIntRect::Radii& holeRadii);
54 
55     void blurLayerImage(unsigned char*, const IntSize&, int stride);
56 
57 private:
58     void drawShadowBuffer(GraphicsContext*);
59 
60     void adjustBlurRadius(GraphicsContext*);
61 
62     enum ShadowDirection {
63         OuterShadow,
64         InnerShadow
65     };
66 
67     IntRect calculateLayerBoundingRect(GraphicsContext*, const FloatRect& layerArea, const IntRect& clipRect);
68     IntSize templateSize(const IntSize& blurredEdgeSize, const RoundedIntRect::Radii&) const;
69 
70     void drawRectShadowWithoutTiling(GraphicsContext*, const FloatRect&, const RoundedIntRect::Radii&, const IntRect& layerRect);
71     void drawRectShadowWithTiling(GraphicsContext*, const FloatRect&, const RoundedIntRect::Radii&, const IntSize& shadowTemplateSize, const IntSize& blurredEdgeSize);
72 
73     void drawInsetShadowWithoutTiling(GraphicsContext*, const FloatRect&, const FloatRect& holeRect, const RoundedIntRect::Radii&, const IntRect& layerRect);
74     void drawInsetShadowWithTiling(GraphicsContext*, const FloatRect&, const FloatRect& holeRect, const RoundedIntRect::Radii&, const IntSize& shadowTemplateSize, const IntSize& blurredEdgeSize);
75 
76     void drawLayerPieces(GraphicsContext*, const FloatRect& shadowBounds, const RoundedIntRect::Radii&, const IntSize& roundedRadius, const IntSize& templateSize, ShadowDirection);
77 
78     void blurShadowBuffer(const IntSize& templateSize);
79     void blurAndColorShadowBuffer(const IntSize& templateSize);
80 
81     IntSize blurredEdgeSize() const;
82 
83     enum ShadowType {
84         NoShadow,
85         SolidShadow,
86         BlurShadow
87     };
88 
89     ShadowType m_type;
90 
91     Color m_color;
92     ColorSpace m_colorSpace;
93     FloatSize m_blurRadius;
94     FloatSize m_offset;
95 
96     ImageBuffer* m_layerImage; // Buffer to where the temporary shadow will be drawn to.
97 
98     FloatRect m_sourceRect; // Sub-rect of m_layerImage that contains the shadow pixels.
99     FloatPoint m_layerOrigin; // Top-left corner of the (possibly clipped) bounding rect to draw the shadow to.
100     FloatSize m_layerSize; // Size of m_layerImage pixels that need blurring.
101     FloatSize m_layerContextTranslation; // Translation to apply to m_layerContext for the shadow to be correctly clipped.
102 
103     bool m_shadowsIgnoreTransforms;
104 };
105 
106 } // namespace WebCore
107 
108 #endif // ShadowBlur_h
109