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