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