[Release] Webkit-EFL Ver. 2.0_beta_118996_0.6.24
[framework/web/webkit-efl.git] / Source / WebCore / rendering / svg / RenderSVGResourceFilterPrimitive.cpp
1 /*
2  * Copyright (C) 2010 University of Szeged
3  * Copyright (C) 2010 Zoltan Herczeg
4  * Copyright (C) 2011 Renata Hodovan (reni@webkit.org)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "config.h"
29
30 #if ENABLE(SVG) && ENABLE(FILTERS)
31 #include "RenderSVGResourceFilterPrimitive.h"
32
33 #include "RenderSVGResource.h"
34 #include "SVGFEImage.h"
35 #include "SVGFilter.h"
36 #include "SVGNames.h"
37
38 namespace WebCore {
39
40
41 void RenderSVGResourceFilterPrimitive::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
42 {
43     RenderSVGHiddenContainer::styleDidChange(diff, oldStyle);
44
45     RenderObject* filter = parent();
46     if (!filter)
47         return;
48     ASSERT(filter->isSVGResourceFilter());
49
50     if (diff == StyleDifferenceEqual || !oldStyle)
51         return;
52
53     const SVGRenderStyle* newStyle = this->style()->svgStyle();
54     if (node()->hasTagName(SVGNames::feFloodTag)) {
55         if (newStyle->floodColor() != oldStyle->svgStyle()->floodColor())
56             static_cast<RenderSVGResourceFilter*>(filter)->primitiveAttributeChanged(this, SVGNames::flood_colorAttr);
57         if (newStyle->floodOpacity() != oldStyle->svgStyle()->floodOpacity())
58             static_cast<RenderSVGResourceFilter*>(filter)->primitiveAttributeChanged(this, SVGNames::flood_opacityAttr);
59     } else if (node()->hasTagName(SVGNames::feDiffuseLightingTag) || node()->hasTagName(SVGNames::feSpecularLightingTag)) {
60         if (newStyle->lightingColor() != oldStyle->svgStyle()->lightingColor())
61             static_cast<RenderSVGResourceFilter*>(filter)->primitiveAttributeChanged(this, SVGNames::lighting_colorAttr);
62     }
63 }
64
65 FloatRect RenderSVGResourceFilterPrimitive::determineFilterPrimitiveSubregion(FilterEffect* effect)
66 {
67     SVGFilter* filter = static_cast<SVGFilter*>(effect->filter());
68     ASSERT(filter);
69
70     // FETile, FETurbulence, FEFlood don't have input effects, take the filter region as unite rect.
71     FloatRect subregion;
72     if (unsigned numberOfInputEffects = effect->inputEffects().size()) {
73         subregion = determineFilterPrimitiveSubregion(effect->inputEffect(0));
74         for (unsigned i = 1; i < numberOfInputEffects; ++i)
75             subregion.unite(determineFilterPrimitiveSubregion(effect->inputEffect(i)));
76     } else
77         subregion = filter->filterRegionInUserSpace();
78
79     // After calling determineFilterPrimitiveSubregion on the target effect, reset the subregion again for <feTile>.
80     if (effect->filterEffectType() == FilterEffectTypeTile)
81         subregion = filter->filterRegionInUserSpace();
82
83     FloatRect effectBoundaries = effect->effectBoundaries();
84     if (effect->hasX())
85         subregion.setX(effectBoundaries.x());
86     if (effect->hasY())
87         subregion.setY(effectBoundaries.y());
88     if (effect->hasWidth())
89         subregion.setWidth(effectBoundaries.width());
90     if (effect->hasHeight())
91         subregion.setHeight(effectBoundaries.height());
92
93     effect->setFilterPrimitiveSubregion(subregion);
94
95     FloatRect absoluteSubregion = filter->absoluteTransform().mapRect(subregion);
96     FloatSize filterResolution = filter->filterResolution();
97     absoluteSubregion.scale(filterResolution.width(), filterResolution.height());
98
99     // Clip every filter effect to the filter region.
100     FloatRect absoluteScaledFilterRegion = filter->filterRegion();
101     absoluteScaledFilterRegion.scale(filterResolution.width(), filterResolution.height());
102     absoluteSubregion.intersect(absoluteScaledFilterRegion);
103
104     effect->setMaxEffectRect(absoluteSubregion);
105     return subregion;
106 }
107
108 } // namespace WebCore
109
110 #endif // ENABLE(SVG) && ENABLE(FILTERS)