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
38namespace WebCore {
39
40
41void 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
65FloatRect RenderSVGResourceFilterPrimitive::determineFilterPrimitiveSubregion(FilterEffect* effect)
66{
67    FloatRect uniteRect;
68    FloatRect subregionBoundingBox = effect->effectBoundaries();
69    FloatRect subregion = subregionBoundingBox;
70    SVGFilter* filter = static_cast<SVGFilter*>(effect->filter());
71    ASSERT(filter);
72
73    if (effect->filterEffectType() != FilterEffectTypeTile) {
74        // FETurbulence, FEImage and FEFlood don't have input effects, take the filter region as unite rect.
75        if (unsigned numberOfInputEffects = effect->inputEffects().size()) {
76            for (unsigned i = 0; i < numberOfInputEffects; ++i)
77                uniteRect.unite(determineFilterPrimitiveSubregion(effect->inputEffect(i)));
78        } else
79            uniteRect = filter->filterRegionInUserSpace();
80    } else {
81        determineFilterPrimitiveSubregion(effect->inputEffect(0));
82        uniteRect = filter->filterRegionInUserSpace();
83    }
84
85    if (filter->effectBoundingBoxMode()) {
86        subregion = uniteRect;
87        // Avoid the calling of a virtual method several times.
88        FloatRect targetBoundingBox = filter->targetBoundingBox();
89
90        if (effect->hasX())
91            subregion.setX(targetBoundingBox.x() + subregionBoundingBox.x() * targetBoundingBox.width());
92
93        if (effect->hasY())
94            subregion.setY(targetBoundingBox.y() + subregionBoundingBox.y() * targetBoundingBox.height());
95
96        if (effect->hasWidth())
97            subregion.setWidth(subregionBoundingBox.width() * targetBoundingBox.width());
98
99        if (effect->hasHeight())
100            subregion.setHeight(subregionBoundingBox.height() * targetBoundingBox.height());
101    } else {
102        if (!effect->hasX())
103            subregion.setX(uniteRect.x());
104
105        if (!effect->hasY())
106            subregion.setY(uniteRect.y());
107
108        if (!effect->hasWidth())
109            subregion.setWidth(uniteRect.width());
110
111        if (!effect->hasHeight())
112            subregion.setHeight(uniteRect.height());
113    }
114
115    effect->setFilterPrimitiveSubregion(subregion);
116
117    FloatRect absoluteSubregion = filter->mapLocalRectToAbsoluteRect(subregion);
118    FloatSize filterResolution = filter->filterResolution();
119    absoluteSubregion.scale(filterResolution.width(), filterResolution.height());
120
121    // FEImage needs the unclipped subregion in absolute coordinates to determine the correct
122    // destination rect in combination with preserveAspectRatio.
123    if (effect->filterEffectType() == FilterEffectTypeImage)
124        static_cast<FEImage*>(effect)->setAbsoluteSubregion(absoluteSubregion);
125
126    // Clip every filter effect to the filter region.
127    FloatRect absoluteScaledFilterRegion = filter->filterRegion();
128    absoluteScaledFilterRegion.scale(filterResolution.width(), filterResolution.height());
129    absoluteSubregion.intersect(absoluteScaledFilterRegion);
130
131    effect->setMaxEffectRect(absoluteSubregion);
132    return subregion;
133}
134
135} // namespace WebCore
136
137#endif // ENABLE(SVG) && ENABLE(FILTERS)
138