Stop using PassRefPtr in platform/graphics
[WebKit-https.git] / Source / WebCore / platform / graphics / filters / FilterOperation.cpp
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "FilterOperation.h"
28
29 #include "AnimationUtilities.h"
30 #include "CachedResourceLoader.h"
31 #include "CachedSVGDocumentReference.h"
32 #include "FilterEffect.h"
33 #include "SVGURIReference.h"
34 #include "TextStream.h"
35
36 namespace WebCore {
37     
38 bool DefaultFilterOperation::operator==(const FilterOperation& operation) const
39 {
40     if (!isSameType(operation))
41         return false;
42     
43     return representedType() == downcast<DefaultFilterOperation>(operation).representedType();
44 }
45
46 ReferenceFilterOperation::ReferenceFilterOperation(const String& url, const String& fragment)
47     : FilterOperation(REFERENCE)
48     , m_url(url)
49     , m_fragment(fragment)
50 {
51 }
52
53 ReferenceFilterOperation::~ReferenceFilterOperation()
54 {
55 }
56     
57 bool ReferenceFilterOperation::operator==(const FilterOperation& operation) const
58 {
59     if (!isSameType(operation))
60         return false;
61     
62     return m_url == downcast<ReferenceFilterOperation>(operation).m_url;
63 }
64
65 void ReferenceFilterOperation::loadExternalDocumentIfNeeded(CachedResourceLoader& cachedResourceLoader, const ResourceLoaderOptions& options)
66 {
67     if (m_cachedSVGDocumentReference)
68         return;
69     if (!SVGURIReference::isExternalURIReference(m_url, *cachedResourceLoader.document()))
70         return;
71     m_cachedSVGDocumentReference = std::make_unique<CachedSVGDocumentReference>(m_url);
72     m_cachedSVGDocumentReference->load(cachedResourceLoader, options);
73 }
74
75 void ReferenceFilterOperation::setFilterEffect(RefPtr<FilterEffect>&& filterEffect)
76 {
77     m_filterEffect = WTFMove(filterEffect);
78 }
79
80 RefPtr<FilterOperation> BasicColorMatrixFilterOperation::blend(const FilterOperation* from, double progress, bool blendToPassthrough)
81 {
82     if (from && !from->isSameType(*this))
83         return this;
84     
85     if (blendToPassthrough)
86         return BasicColorMatrixFilterOperation::create(WebCore::blend(m_amount, passthroughAmount(), progress), m_type);
87         
88     const BasicColorMatrixFilterOperation* fromOperation = downcast<BasicColorMatrixFilterOperation>(from);
89     double fromAmount = fromOperation ? fromOperation->amount() : passthroughAmount();
90     return BasicColorMatrixFilterOperation::create(WebCore::blend(fromAmount, m_amount, progress), m_type);
91 }
92
93 inline bool BasicColorMatrixFilterOperation::operator==(const FilterOperation& operation) const
94 {
95     if (!isSameType(operation))
96         return false;
97     const BasicColorMatrixFilterOperation& other = downcast<BasicColorMatrixFilterOperation>(operation);
98     return m_amount == other.m_amount;
99 }
100
101 double BasicColorMatrixFilterOperation::passthroughAmount() const
102 {
103     switch (m_type) {
104     case GRAYSCALE:
105     case SEPIA:
106     case HUE_ROTATE:
107         return 0;
108     case SATURATE:
109         return 1;
110     default:
111         ASSERT_NOT_REACHED();
112         return 0;
113     }
114 }
115
116 RefPtr<FilterOperation> BasicComponentTransferFilterOperation::blend(const FilterOperation* from, double progress, bool blendToPassthrough)
117 {
118     if (from && !from->isSameType(*this))
119         return this;
120     
121     if (blendToPassthrough)
122         return BasicComponentTransferFilterOperation::create(WebCore::blend(m_amount, passthroughAmount(), progress), m_type);
123         
124     const BasicComponentTransferFilterOperation* fromOperation = downcast<BasicComponentTransferFilterOperation>(from);
125     double fromAmount = fromOperation ? fromOperation->amount() : passthroughAmount();
126     return BasicComponentTransferFilterOperation::create(WebCore::blend(fromAmount, m_amount, progress), m_type);
127 }
128
129 inline bool BasicComponentTransferFilterOperation::operator==(const FilterOperation& operation) const
130 {
131     if (!isSameType(operation))
132         return false;
133     const BasicComponentTransferFilterOperation& other = downcast<BasicComponentTransferFilterOperation>(operation);
134     return m_amount == other.m_amount;
135 }
136
137 double BasicComponentTransferFilterOperation::passthroughAmount() const
138 {
139     switch (m_type) {
140     case OPACITY:
141         return 1;
142     case INVERT:
143         return 0;
144     case CONTRAST:
145         return 1;
146     case BRIGHTNESS:
147         return 1;
148     default:
149         ASSERT_NOT_REACHED();
150         return 0;
151     }
152 }
153     
154 bool BlurFilterOperation::operator==(const FilterOperation& operation) const
155 {
156     if (!isSameType(operation))
157         return false;
158     
159     return m_stdDeviation == downcast<BlurFilterOperation>(operation).stdDeviation();
160 }
161     
162 RefPtr<FilterOperation> BlurFilterOperation::blend(const FilterOperation* from, double progress, bool blendToPassthrough)
163 {
164     if (from && !from->isSameType(*this))
165         return this;
166
167     LengthType lengthType = m_stdDeviation.type();
168
169     if (blendToPassthrough)
170         return BlurFilterOperation::create(WebCore::blend(m_stdDeviation, Length(lengthType), progress));
171
172     const BlurFilterOperation* fromOperation = downcast<BlurFilterOperation>(from);
173     Length fromLength = fromOperation ? fromOperation->m_stdDeviation : Length(lengthType);
174     return BlurFilterOperation::create(WebCore::blend(fromLength, m_stdDeviation, progress));
175 }
176     
177 bool DropShadowFilterOperation::operator==(const FilterOperation& operation) const
178 {
179     if (!isSameType(operation))
180         return false;
181     const DropShadowFilterOperation& other = downcast<DropShadowFilterOperation>(operation);
182     return m_location == other.m_location && m_stdDeviation == other.m_stdDeviation && m_color == other.m_color;
183 }
184     
185 RefPtr<FilterOperation> DropShadowFilterOperation::blend(const FilterOperation* from, double progress, bool blendToPassthrough)
186 {
187     if (from && !from->isSameType(*this))
188         return this;
189
190     if (blendToPassthrough)
191         return DropShadowFilterOperation::create(
192             WebCore::blend(m_location, IntPoint(), progress),
193             WebCore::blend(m_stdDeviation, 0, progress),
194             WebCore::blend(m_color, Color(Color::transparent), progress));
195
196     const DropShadowFilterOperation* fromOperation = downcast<DropShadowFilterOperation>(from);
197     IntPoint fromLocation = fromOperation ? fromOperation->location() : IntPoint();
198     int fromStdDeviation = fromOperation ? fromOperation->stdDeviation() : 0;
199     Color fromColor = fromOperation ? fromOperation->color() : Color(Color::transparent);
200     
201     return DropShadowFilterOperation::create(
202         WebCore::blend(fromLocation, m_location, progress),
203         WebCore::blend(fromStdDeviation, m_stdDeviation, progress),
204         WebCore::blend(fromColor, m_color, progress));
205 }
206
207 TextStream& operator<<(TextStream& ts, const FilterOperation& filter)
208 {
209     switch (filter.type()) {
210     case FilterOperation::REFERENCE:
211         ts << "reference";
212         break;
213     case FilterOperation::GRAYSCALE: {
214         const auto& colorMatrixFilter = downcast<BasicColorMatrixFilterOperation>(filter);
215         ts << "grayscale(" << colorMatrixFilter.amount() << ")";
216         break;
217     }
218     case FilterOperation::SEPIA: {
219         const auto& colorMatrixFilter = downcast<BasicColorMatrixFilterOperation>(filter);
220         ts << "sepia(" << colorMatrixFilter.amount() << ")";
221         break;
222     }
223     case FilterOperation::SATURATE: {
224         const auto& colorMatrixFilter = downcast<BasicColorMatrixFilterOperation>(filter);
225         ts << "saturate(" << colorMatrixFilter.amount() << ")";
226         break;
227     }
228     case FilterOperation::HUE_ROTATE: {
229         const auto& colorMatrixFilter = downcast<BasicColorMatrixFilterOperation>(filter);
230         ts << "hue-rotate(" << colorMatrixFilter.amount() << ")";
231         break;
232     }
233     case FilterOperation::INVERT: {
234         const auto& componentTransferFilter = downcast<BasicComponentTransferFilterOperation>(filter);
235         ts << "invert(" << componentTransferFilter.amount() << ")";
236         break;
237     }
238     case FilterOperation::OPACITY: {
239         const auto& componentTransferFilter = downcast<BasicComponentTransferFilterOperation>(filter);
240         ts << "opacity(" << componentTransferFilter.amount() << ")";
241         break;
242     }
243     case FilterOperation::BRIGHTNESS: {
244         const auto& componentTransferFilter = downcast<BasicComponentTransferFilterOperation>(filter);
245         ts << "brightness(" << componentTransferFilter.amount() << ")";
246         break;
247     }
248     case FilterOperation::CONTRAST: {
249         const auto& componentTransferFilter = downcast<BasicComponentTransferFilterOperation>(filter);
250         ts << "contrast(" << componentTransferFilter.amount() << ")";
251         break;
252     }
253     case FilterOperation::BLUR: {
254         const auto& blurFilter = downcast<BlurFilterOperation>(filter);
255         ts << "blur(" << blurFilter.stdDeviation().value() << ")"; // FIXME: should call floatValueForLength() but that's outisde of platform/.
256         break;
257     }
258     case FilterOperation::DROP_SHADOW: {
259         const auto& dropShadowFilter = downcast<DropShadowFilterOperation>(filter);
260         ts << "drop-shadow(" << dropShadowFilter.x() << " " << dropShadowFilter.y() << " " << dropShadowFilter.location() << " ";
261         ts << dropShadowFilter.color() << ")";
262         break;
263     }
264     case FilterOperation::PASSTHROUGH:
265         ts << "passthrough";
266         break;
267     case FilterOperation::DEFAULT: {
268         const auto& defaultFilter = downcast<DefaultFilterOperation>(filter);
269         ts << "default type=" << (int)defaultFilter.representedType();
270         break;
271     }
272     case FilterOperation::NONE:
273         ts << "none";
274         break;
275     }
276     return ts;
277 }
278
279 } // namespace WebCore