Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / FilterEffectRenderer.h
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef FilterEffectRenderer_h
27 #define FilterEffectRenderer_h
28
29 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
30 #include "platform/geometry/FloatRect.h"
31 #include "platform/geometry/IntRectExtent.h"
32 #include "platform/geometry/LayoutRect.h"
33 #include "platform/graphics/ImageBuffer.h"
34 #include "platform/graphics/filters/Filter.h"
35 #include "platform/graphics/filters/FilterEffect.h"
36 #include "platform/graphics/filters/FilterOperations.h"
37 #include "platform/graphics/filters/SourceGraphic.h"
38 #include "wtf/PassRefPtr.h"
39 #include "wtf/RefCounted.h"
40 #include "wtf/RefPtr.h"
41
42 namespace blink {
43
44 class GraphicsContext;
45 class RenderLayer;
46 class RenderObject;
47
48 class FilterEffectRendererHelper {
49 public:
50     FilterEffectRendererHelper(bool haveFilterEffect)
51         : m_savedGraphicsContext(0)
52         , m_renderLayer(0)
53         , m_haveFilterEffect(haveFilterEffect)
54     {
55     }
56
57     bool haveFilterEffect() const { return m_haveFilterEffect; }
58     bool hasStartedFilterEffect() const { return m_savedGraphicsContext; }
59
60     bool prepareFilterEffect(RenderLayer*, const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect);
61     GraphicsContext* beginFilterEffect(GraphicsContext* oldContext);
62     GraphicsContext* applyFilterEffect();
63
64     const LayoutRect& repaintRect() const { return m_repaintRect; }
65 private:
66     GraphicsContext* m_savedGraphicsContext;
67     RenderLayer* m_renderLayer;
68
69     LayoutRect m_repaintRect;
70     bool m_haveFilterEffect;
71 };
72
73 class FilterEffectRenderer FINAL : public Filter
74 {
75     WTF_MAKE_FAST_ALLOCATED;
76 public:
77     static PassRefPtr<FilterEffectRenderer> create()
78     {
79         return adoptRef(new FilterEffectRenderer());
80     }
81
82     void setSourceImageRect(const IntRect& sourceImageRect)
83     {
84         m_sourceDrawingRegion = sourceImageRect;
85         m_graphicsBufferAttached = false;
86     }
87     virtual IntRect sourceImageRect() const OVERRIDE { return m_sourceDrawingRegion; }
88
89     GraphicsContext* inputContext();
90     ImageBuffer* output() const { return lastEffect()->asImageBuffer(); }
91
92     bool build(RenderObject* renderer, const FilterOperations&);
93     bool updateBackingStoreRect(const FloatRect& filterRect);
94     void allocateBackingStoreIfNeeded();
95     void clearIntermediateResults();
96     void apply();
97
98     IntRect outputRect() const { return lastEffect()->hasResult() ? lastEffect()->absolutePaintRect() : IntRect(); }
99
100     bool hasFilterThatMovesPixels() const { return m_hasFilterThatMovesPixels; }
101     LayoutRect computeSourceImageRectForDirtyRect(const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect);
102
103     PassRefPtr<FilterEffect> lastEffect() const
104     {
105         return m_lastEffect;
106     }
107 private:
108
109     FilterEffectRenderer();
110     virtual ~FilterEffectRenderer();
111
112     IntRect m_sourceDrawingRegion;
113
114     RefPtr<SourceGraphic> m_sourceGraphic;
115     RefPtr<FilterEffect> m_lastEffect;
116
117     bool m_graphicsBufferAttached;
118     bool m_hasFilterThatMovesPixels;
119 };
120
121 } // namespace blink
122
123
124 #endif // FilterEffectRenderer_h