initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / ShadowBlur.h
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 "RoundedRect.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     enum ShadowType {
48         NoShadow,
49         SolidShadow,
50         BlurShadow
51     };
52
53     ShadowBlur(const FloatSize& radius, const FloatSize& offset, const Color&, ColorSpace);
54     ShadowBlur();
55
56     void setShadowValues(const FloatSize&, const FloatSize& , const Color&, ColorSpace, bool ignoreTransforms = false);
57
58     void setShadowsIgnoreTransforms(bool ignoreTransforms) { m_shadowsIgnoreTransforms = ignoreTransforms; }
59     bool shadowsIgnoreTransforms() const { return m_shadowsIgnoreTransforms; }
60
61     GraphicsContext* beginShadowLayer(GraphicsContext*, const FloatRect& layerArea);
62     void endShadowLayer(GraphicsContext*);
63
64     void drawRectShadow(GraphicsContext*, const FloatRect&, const RoundedRect::Radii&);
65     void drawInsetShadow(GraphicsContext*, const FloatRect&, const FloatRect& holeRect, const RoundedRect::Radii& holeRadii);
66
67     void blurLayerImage(unsigned char*, const IntSize&, int stride);
68
69     void clear();
70
71     ShadowType type() const { return m_type; }
72
73 #if PLATFORM(QT) || USE(CAIRO)
74     bool mustUseShadowBlur(GraphicsContext*) const;
75 #endif
76
77 private:
78     void updateShadowBlurValues();
79
80     void drawShadowBuffer(GraphicsContext*);
81
82     void adjustBlurRadius(GraphicsContext*);
83     
84     enum ShadowDirection {
85         OuterShadow,
86         InnerShadow
87     };
88     
89     IntRect calculateLayerBoundingRect(GraphicsContext*, const FloatRect& layerArea, const IntRect& clipRect);
90     IntSize templateSize(const IntSize& blurredEdgeSize, const RoundedRect::Radii&) const;
91
92     void drawRectShadowWithoutTiling(GraphicsContext*, const FloatRect&, const RoundedRect::Radii&, const IntRect& layerRect);
93     void drawRectShadowWithTiling(GraphicsContext*, const FloatRect&, const RoundedRect::Radii&, const IntSize& shadowTemplateSize, const IntSize& blurredEdgeSize);
94
95     void drawInsetShadowWithoutTiling(GraphicsContext*, const FloatRect&, const FloatRect& holeRect, const RoundedRect::Radii&, const IntRect& layerRect);
96     void drawInsetShadowWithTiling(GraphicsContext*, const FloatRect&, const FloatRect& holeRect, const RoundedRect::Radii&, const IntSize& shadowTemplateSize, const IntSize& blurredEdgeSize);
97     
98     void drawLayerPieces(GraphicsContext*, const FloatRect& shadowBounds, const RoundedRect::Radii&, const IntSize& roundedRadius, const IntSize& templateSize, ShadowDirection);
99     
100     void blurShadowBuffer(const IntSize& templateSize);
101     void blurAndColorShadowBuffer(const IntSize& templateSize);
102     
103     IntSize blurredEdgeSize() const;
104     
105     
106     ShadowType m_type;
107
108     Color m_color;
109     ColorSpace m_colorSpace;
110     FloatSize m_blurRadius;
111     FloatSize m_offset;
112
113     ImageBuffer* m_layerImage; // Buffer to where the temporary shadow will be drawn to.
114
115     FloatRect m_sourceRect; // Sub-rect of m_layerImage that contains the shadow pixels.
116     FloatPoint m_layerOrigin; // Top-left corner of the (possibly clipped) bounding rect to draw the shadow to.
117     FloatSize m_layerSize; // Size of m_layerImage pixels that need blurring.
118     FloatSize m_layerContextTranslation; // Translation to apply to m_layerContext for the shadow to be correctly clipped.
119
120     bool m_shadowsIgnoreTransforms;
121 };
122
123 } // namespace WebCore
124
125 #endif // ShadowBlur_h