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(SVG) && ENABLE(FILTERS)
23#include "SVGFilterBuilder.h"
24
25#include "FilterEffect.h"
26#include "PlatformString.h"
27#include "SourceAlpha.h"
28#include "SourceGraphic.h"
29
30#include <wtf/PassRefPtr.h>
31
32namespace WebCore {
33
34SVGFilterBuilder::SVGFilterBuilder(Filter* filter)
35    : m_lastEffect(0)
36{
37    m_builtinEffects.add(SourceGraphic::effectName(), SourceGraphic::create(filter));
38    m_builtinEffects.add(SourceAlpha::effectName(), SourceAlpha::create(filter));
39    addBuiltinEffects();
40}
41
42void SVGFilterBuilder::add(const AtomicString& id, RefPtr<FilterEffect> effect)
43{
44    if (id.isEmpty()) {
45        m_lastEffect = effect;
46        return;
47    }
48
49    if (m_builtinEffects.contains(id))
50        return;
51
52    m_lastEffect = effect;
53    m_namedEffects.set(id, m_lastEffect);
54}
55
56FilterEffect* SVGFilterBuilder::getEffectById(const AtomicString& id) const
57{
58    if (id.isEmpty()) {
59        if (m_lastEffect)
60            return m_lastEffect.get();
61
62        return m_builtinEffects.get(SourceGraphic::effectName()).get();
63    }
64
65    if (m_builtinEffects.contains(id))
66        return m_builtinEffects.get(id).get();
67
68    return m_namedEffects.get(id).get();
69}
70
71void SVGFilterBuilder::appendEffectToEffectReferences(RefPtr<FilterEffect> effectReference, RenderObject* object)
72{
73    // The effect must be a newly created filter effect.
74    ASSERT(!m_effectReferences.contains(effectReference));
75    ASSERT(object && !m_effectRenderer.contains(object));
76    m_effectReferences.add(effectReference, FilterEffectSet());
77
78    FilterEffect* effect = effectReference.get();
79    unsigned numberOfInputEffects = effect->inputEffects().size();
80
81    // It is not possible to add the same value to a set twice.
82    for (unsigned i = 0; i < numberOfInputEffects; ++i)
83        effectReferences(effect->inputEffect(i)).add(effect);
84    m_effectRenderer.add(object, effectReference.get());
85}
86
87void SVGFilterBuilder::clearEffects()
88{
89    m_lastEffect = 0;
90    m_namedEffects.clear();
91    m_effectReferences.clear();
92    m_effectRenderer.clear();
93    addBuiltinEffects();
94}
95
96void SVGFilterBuilder::clearResultsRecursive(FilterEffect* effect)
97{
98    if (!effect->hasResult())
99        return;
100
101    effect->clearResult();
102
103    HashSet<FilterEffect*>& effectReferences = this->effectReferences(effect);
104    HashSet<FilterEffect*>::iterator end = effectReferences.end();
105    for (HashSet<FilterEffect*>::iterator it = effectReferences.begin(); it != end; ++it)
106         clearResultsRecursive(*it);
107}
108
109} // namespace WebCore
110
111#endif // ENABLE(SVG) && ENABLE(FILTERS)
112