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