tizen beta release
[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     FloatRect uniteRect;
68     FloatRect subregion = effect->effectBoundaries();
69     SVGFilter* filter = static_cast<SVGFilter*>(effect->filter());
70     ASSERT(filter);
71
72     if (effect->filterEffectType() != FilterEffectTypeTile) {
73         // FETurbulence, FEImage and FEFlood don't have input effects, take the filter region as unite rect.
74         if (unsigned numberOfInputEffects = effect->inputEffects().size()) {
75             for (unsigned i = 0; i < numberOfInputEffects; ++i)
76                 uniteRect.unite(determineFilterPrimitiveSubregion(effect->inputEffect(i)));
77         } else
78             uniteRect = filter->filterRegionInUserSpace();
79     } else {
80         determineFilterPrimitiveSubregion(effect->inputEffect(0));
81         uniteRect = filter->filterRegionInUserSpace();
82     }
83
84     if (!effect->hasX())
85         subregion.setX(uniteRect.x());
86     if (!effect->hasY())
87         subregion.setY(uniteRect.y());
88     if (!effect->hasWidth())
89         subregion.setWidth(uniteRect.width());
90     if (!effect->hasHeight())
91         subregion.setHeight(uniteRect.height());
92     effect->setFilterPrimitiveSubregion(subregion);
93
94     FloatRect absoluteSubregion = filter->mapLocalRectToAbsoluteRect(subregion);
95     FloatSize filterResolution = filter->filterResolution();
96     absoluteSubregion.scale(filterResolution.width(), filterResolution.height());
97
98     // FEImage needs the unclipped subregion in absolute coordinates to determine the correct
99     // destination rect in combination with preserveAspectRatio.
100     if (effect->filterEffectType() == FilterEffectTypeImage)
101         static_cast<FEImage*>(effect)->setAbsoluteSubregion(absoluteSubregion);
102
103     // Clip every filter effect to the filter region.
104     FloatRect absoluteScaledFilterRegion = filter->filterRegion();
105     absoluteScaledFilterRegion.scale(filterResolution.width(), filterResolution.height());
106     absoluteSubregion.intersect(absoluteScaledFilterRegion);
107
108     effect->setMaxEffectRect(absoluteSubregion);
109     return subregion;
110 }
111
112 } // namespace WebCore
113
114 #endif // ENABLE(SVG) && ENABLE(FILTERS)