Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / svg / ReferenceFilterBuilder.cpp
1 /*
2  * Copyright (C) 2013 Adobe Systems Inc. All rights reserved.
3  * Copyright (C) 2013 Google Inc.  All rights reserved.
4  * Copyright (C) 2011 Apple Inc. All rights reserved.
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 APPLE INC. ``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 APPLE COMPUTER, INC. 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 #include "core/rendering/svg/ReferenceFilterBuilder.h"
31
32 #include "core/css/CSSPrimitiveValue.h"
33 #include "core/css/CSSPrimitiveValueMappings.h"
34 #include "core/css/StylePropertySet.h"
35 #include "core/dom/Element.h"
36 #include "core/dom/ElementTraversal.h"
37 #include "core/fetch/DocumentResource.h"
38 #include "core/rendering/svg/RenderSVGResourceFilter.h"
39 #include "core/svg/SVGDocumentExtensions.h"
40 #include "core/svg/SVGFilterPrimitiveStandardAttributes.h"
41 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
42 #include "platform/graphics/filters/SourceAlpha.h"
43
44 namespace blink {
45
46 HashMap<const FilterOperation*, OwnPtr<DocumentResourceReference> >* ReferenceFilterBuilder::documentResourceReferences = 0;
47
48 DocumentResourceReference* ReferenceFilterBuilder::documentResourceReference(const FilterOperation* filterOperation)
49 {
50     if (!documentResourceReferences)
51         return 0;
52
53     return documentResourceReferences->get(filterOperation);
54 }
55
56 void ReferenceFilterBuilder::setDocumentResourceReference(const FilterOperation* filterOperation, PassOwnPtr<DocumentResourceReference> documentResourceReference)
57 {
58     if (!documentResourceReferences)
59         documentResourceReferences = new HashMap<const FilterOperation*, OwnPtr<DocumentResourceReference> >;
60     documentResourceReferences->add(filterOperation, documentResourceReference);
61 }
62
63 void ReferenceFilterBuilder::clearDocumentResourceReference(const FilterOperation* filterOperation)
64 {
65     if (!documentResourceReferences)
66         return;
67
68     documentResourceReferences->remove(filterOperation);
69 }
70
71 // Returns whether or not the SVGElement object contains a valid color-interpolation-filters attribute
72 static bool getSVGElementColorSpace(SVGElement* svgElement, ColorSpace& cs)
73 {
74     if (!svgElement)
75         return false;
76
77     const RenderObject* renderer = svgElement->renderer();
78     const RenderStyle* style = renderer ? renderer->style() : 0;
79     const SVGRenderStyle* svgStyle = style ? &style->svgStyle() : 0;
80     EColorInterpolation eColorInterpolation = CI_AUTO;
81     if (svgStyle) {
82         // If a layout has been performed, then we can use the fast path to get this attribute
83         eColorInterpolation = svgStyle->colorInterpolationFilters();
84     } else if (!svgElement->presentationAttributeStyle()) {
85         return false;
86     } else {
87         // Otherwise, use the slow path by using string comparison (used by external svg files)
88         RefPtrWillBeRawPtr<CSSValue> cssValue = svgElement->presentationAttributeStyle()->getPropertyCSSValue(CSSPropertyColorInterpolationFilters);
89         if (cssValue.get() && cssValue->isPrimitiveValue()) {
90             const CSSPrimitiveValue& primitiveValue = *((CSSPrimitiveValue*)cssValue.get());
91             eColorInterpolation = (EColorInterpolation)primitiveValue;
92         } else {
93             return false;
94         }
95     }
96
97     switch (eColorInterpolation) {
98     case CI_AUTO:
99     case CI_SRGB:
100         cs = ColorSpaceDeviceRGB;
101         break;
102     case CI_LINEARRGB:
103         cs = ColorSpaceLinearRGB;
104         break;
105     default:
106         return false;
107     }
108
109     return true;
110 }
111
112 PassRefPtr<FilterEffect> ReferenceFilterBuilder::build(Filter* parentFilter, RenderObject* renderer, FilterEffect* previousEffect, const ReferenceFilterOperation* filterOperation)
113 {
114     if (!renderer)
115         return nullptr;
116
117     TreeScope* treeScope = &renderer->node()->treeScope();
118
119     if (DocumentResourceReference* documentResourceRef = documentResourceReference(filterOperation)) {
120         DocumentResource* cachedSVGDocument = documentResourceRef->document();
121
122         // If we have an SVG document, this is an external reference. Otherwise
123         // we look up the referenced node in the current document.
124         if (cachedSVGDocument)
125             treeScope = cachedSVGDocument->document();
126     }
127
128     if (!treeScope)
129         return nullptr;
130
131     Element* filter = treeScope->getElementById(filterOperation->fragment());
132
133     if (!filter) {
134         // Although we did not find the referenced filter, it might exist later
135         // in the document.
136         treeScope->document().accessSVGExtensions().addPendingResource(filterOperation->fragment(), toElement(renderer->node()));
137         return nullptr;
138     }
139
140     if (!isSVGFilterElement(*filter))
141         return nullptr;
142
143     SVGFilterElement& filterElement = toSVGFilterElement(*filter);
144
145     // FIXME: Figure out what to do with SourceAlpha. Right now, we're
146     // using the alpha of the original input layer, which is obviously
147     // wrong. We should probably be extracting the alpha from the
148     // previousEffect, but this requires some more processing.
149     // This may need a spec clarification.
150     RefPtr<SVGFilterBuilder> builder = SVGFilterBuilder::create(previousEffect, SourceAlpha::create(parentFilter));
151
152     ColorSpace filterColorSpace = ColorSpaceDeviceRGB;
153     bool useFilterColorSpace = getSVGElementColorSpace(&filterElement, filterColorSpace);
154
155     for (SVGElement* element = Traversal<SVGElement>::firstChild(filterElement); element; element = Traversal<SVGElement>::nextSibling(*element)) {
156         if (!element->isFilterEffect())
157             continue;
158
159         SVGFilterPrimitiveStandardAttributes* effectElement = static_cast<SVGFilterPrimitiveStandardAttributes*>(element);
160
161         RefPtr<FilterEffect> effect = effectElement->build(builder.get(), parentFilter);
162         if (!effect)
163             continue;
164
165         effectElement->setStandardAttributes(effect.get());
166         effect->setEffectBoundaries(SVGLengthContext::resolveRectangle<SVGFilterPrimitiveStandardAttributes>(effectElement, filterElement.primitiveUnits()->currentValue()->enumValue(), parentFilter->sourceImageRect()));
167         ColorSpace colorSpace = filterColorSpace;
168         if (useFilterColorSpace || getSVGElementColorSpace(effectElement, colorSpace))
169             effect->setOperatingColorSpace(colorSpace);
170         builder->add(AtomicString(effectElement->result()->currentValue()->value()), effect);
171     }
172     return builder->lastEffect();
173 }
174
175 } // namespace blink