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
42namespace blink {
43
44class GraphicsContext;
45class RenderLayer;
46class RenderObject;
47
48class FilterEffectRendererHelper {
49public:
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& paintInvalidationRect() const { return m_paintInvalidationRect; }
65private:
66    GraphicsContext* m_savedGraphicsContext;
67    RenderLayer* m_renderLayer;
68
69    LayoutRect m_paintInvalidationRect;
70    FloatRect m_filterBoxRect;
71    bool m_haveFilterEffect;
72};
73
74class FilterEffectRenderer FINAL : public Filter
75{
76    WTF_MAKE_FAST_ALLOCATED;
77public:
78    static PassRefPtr<FilterEffectRenderer> create()
79    {
80        return adoptRef(new FilterEffectRenderer());
81    }
82
83    void setSourceImageRect(const IntRect& sourceImageRect)
84    {
85        m_sourceDrawingRegion = sourceImageRect;
86        m_graphicsBufferAttached = false;
87    }
88    virtual IntRect sourceImageRect() const OVERRIDE { return m_sourceDrawingRegion; }
89
90    GraphicsContext* inputContext();
91    ImageBuffer* output() const { return lastEffect()->asImageBuffer(); }
92
93    bool build(RenderObject* renderer, const FilterOperations&);
94    bool updateBackingStoreRect(const FloatRect& filterRect);
95    void allocateBackingStoreIfNeeded();
96    void clearIntermediateResults();
97    void apply();
98
99    IntRect outputRect() const { return lastEffect()->hasResult() ? lastEffect()->absolutePaintRect() : IntRect(); }
100
101    bool hasFilterThatMovesPixels() const { return m_hasFilterThatMovesPixels; }
102    LayoutRect computeSourceImageRectForDirtyRect(const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect);
103
104    PassRefPtr<FilterEffect> lastEffect() const
105    {
106        return m_lastEffect;
107    }
108private:
109
110    FilterEffectRenderer();
111    virtual ~FilterEffectRenderer();
112
113    IntRect m_sourceDrawingRegion;
114
115    RefPtr<SourceGraphic> m_sourceGraphic;
116    RefPtr<FilterEffect> m_lastEffect;
117
118    bool m_graphicsBufferAttached;
119    bool m_hasFilterThatMovesPixels;
120};
121
122} // namespace blink
123
124
125#endif // FilterEffectRenderer_h
126