1/*
2 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21
22#if ENABLE(FILTERS)
23#include "SVGFilterBuilder.h"
24
25#include "FilterEffect.h"
26#include "SourceAlpha.h"
27#include "SourceGraphic.h"
28#include <wtf/PassRefPtr.h>
29#include <wtf/text/WTFString.h>
30
31namespace WebCore {
32
33SVGFilterBuilder::SVGFilterBuilder(PassRefPtr<FilterEffect> sourceGraphic, PassRefPtr<FilterEffect> sourceAlpha)
34{
35    m_builtinEffects.add(SourceGraphic::effectName(), sourceGraphic);
36    m_builtinEffects.add(SourceAlpha::effectName(), sourceAlpha);
37    addBuiltinEffects();
38}
39
40void SVGFilterBuilder::add(const AtomicString& id, PassRefPtr<FilterEffect> effect)
41{
42    if (id.isEmpty()) {
43        m_lastEffect = effect;
44        return;
45    }
46
47    if (m_builtinEffects.contains(id))
48        return;
49
50    m_lastEffect = effect;
51    m_namedEffects.set(id, m_lastEffect);
52}
53
54FilterEffect* SVGFilterBuilder::getEffectById(const AtomicString& id) const
55{
56    if (id.isEmpty()) {
57        if (m_lastEffect)
58            return m_lastEffect.get();
59
60        return m_builtinEffects.get(SourceGraphic::effectName());
61    }
62
63    if (m_builtinEffects.contains(id))
64        return m_builtinEffects.get(id);
65
66    return m_namedEffects.get(id);
67}
68
69void SVGFilterBuilder::appendEffectToEffectReferences(PassRefPtr<FilterEffect> prpEffect, RenderObject* object)
70{
71    RefPtr<FilterEffect> effect = prpEffect;
72
73    // The effect must be a newly created filter effect.
74    ASSERT(!m_effectReferences.contains(effect));
75    ASSERT(object && !m_effectRenderer.contains(object));
76    m_effectReferences.add(effect, FilterEffectSet());
77
78    unsigned numberOfInputEffects = effect->inputEffects().size();
79
80    // It is not possible to add the same value to a set twice.
81    for (unsigned i = 0; i < numberOfInputEffects; ++i)
82        effectReferences(effect->inputEffect(i)).add(effect.get());
83    m_effectRenderer.add(object, effect.get());
84}
85
86void SVGFilterBuilder::clearEffects()
87{
88    m_lastEffect = 0;
89    m_namedEffects.clear();
90    m_effectReferences.clear();
91    m_effectRenderer.clear();
92    addBuiltinEffects();
93}
94
95void SVGFilterBuilder::clearResultsRecursive(FilterEffect* effect)
96{
97    if (!effect->hasResult())
98        return;
99
100    effect->clearResult();
101
102    HashSet<FilterEffect*>& effectReferences = this->effectReferences(effect);
103    HashSet<FilterEffect*>::iterator end = effectReferences.end();
104    for (HashSet<FilterEffect*>::iterator it = effectReferences.begin(); it != end; ++it)
105         clearResultsRecursive(*it);
106}
107
108} // namespace WebCore
109
110#endif // ENABLE(FILTERS)
111