2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef FilterEffectRenderer_h
27 #define FilterEffectRenderer_h
29 #if ENABLE(CSS_FILTERS)
32 #include "FilterEffect.h"
33 #include "FilterOperations.h"
34 #include "FloatRect.h"
35 #include "GraphicsContext.h"
36 #include "ImageBuffer.h"
37 #include "SVGFilterBuilder.h"
38 #include "SourceGraphic.h"
40 #include <wtf/PassRefPtr.h>
41 #include <wtf/RefCounted.h>
42 #include <wtf/RefPtr.h>
46 typedef Vector<RefPtr<FilterEffect> > FilterEffectList;
48 class CustomFilterProgram;
50 class GraphicsContext;
53 class FilterEffectRendererHelper {
55 FilterEffectRendererHelper(bool haveFilterEffect)
56 : m_savedGraphicsContext(0)
58 , m_haveFilterEffect(haveFilterEffect)
62 bool haveFilterEffect() const { return m_haveFilterEffect; }
63 bool hasStartedFilterEffect() const { return m_savedGraphicsContext; }
65 bool prepareFilterEffect(RenderLayer*, const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect, const LayoutRect& layerRepaintRect);
66 GraphicsContext* beginFilterEffect(GraphicsContext* oldContext);
67 GraphicsContext* applyFilterEffect();
69 const LayoutRect& repaintRect() const { return m_repaintRect; }
71 GraphicsContext* m_savedGraphicsContext;
72 RenderLayer* m_renderLayer;
73 LayoutPoint m_paintOffset;
74 LayoutRect m_repaintRect;
75 bool m_haveFilterEffect;
78 class FilterEffectRenderer : public Filter
80 WTF_MAKE_FAST_ALLOCATED;
82 static PassRefPtr<FilterEffectRenderer> create()
84 return adoptRef(new FilterEffectRenderer());
87 virtual void setSourceImageRect(const FloatRect& sourceImageRect)
89 m_sourceDrawingRegion = sourceImageRect;
90 setMaxEffectRects(sourceImageRect);
91 setFilterRegion(sourceImageRect);
92 m_graphicsBufferAttached = false;
94 virtual FloatRect sourceImageRect() const { return m_sourceDrawingRegion; }
96 virtual void setFilterRegion(const FloatRect& filterRegion) { m_filterRegion = filterRegion; }
97 virtual FloatRect filterRegion() const { return m_filterRegion; }
99 GraphicsContext* inputContext();
100 ImageBuffer* output() const { return lastEffect()->asImageBuffer(); }
102 bool build(Document*, const FilterOperations&);
103 PassRefPtr<FilterEffect> buildReferenceFilter(Document*, PassRefPtr<FilterEffect> previousEffect, ReferenceFilterOperation*);
104 bool updateBackingStoreRect(const FloatRect& filterRect);
105 void allocateBackingStoreIfNeeded();
106 void clearIntermediateResults();
109 IntRect outputRect() const { return lastEffect()->hasResult() ? lastEffect()->requestedRegionOfInputImageData(IntRect(m_filterRegion)) : IntRect(); }
111 bool hasFilterThatMovesPixels() const { return m_hasFilterThatMovesPixels; }
112 LayoutRect computeSourceImageRectForDirtyRect(const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect);
114 #if ENABLE(CSS_SHADERS)
115 bool hasCustomShaderFilter() const { return m_hasCustomShaderFilter; }
118 void setMaxEffectRects(const FloatRect& effectRect)
120 for (size_t i = 0; i < m_effects.size(); ++i) {
121 RefPtr<FilterEffect> effect = m_effects.at(i);
122 effect->setMaxEffectRect(effectRect);
125 PassRefPtr<FilterEffect> lastEffect() const
127 if (m_effects.size() > 0)
128 return m_effects.last();
132 FilterEffectRenderer();
133 virtual ~FilterEffectRenderer();
135 FloatRect m_sourceDrawingRegion;
136 FloatRect m_filterRegion;
138 FilterEffectList m_effects;
139 RefPtr<SourceGraphic> m_sourceGraphic;
146 bool m_graphicsBufferAttached;
147 bool m_hasFilterThatMovesPixels;
148 #if ENABLE(CSS_SHADERS)
149 bool m_hasCustomShaderFilter;
153 } // namespace WebCore
155 #endif // ENABLE(CSS_FILTERS)
157 #endif // FilterEffectRenderer_h