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 COMPUTER, 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 COMPUTER, 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 "third_party/blink/renderer/core/style/filter_operations.h"
27 
28 #include <numeric>
29 
30 namespace blink {
31 
32 FilterOperations::FilterOperations() = default;
33 
Trace(Visitor * visitor) const34 void FilterOperations::Trace(Visitor* visitor) const {
35   visitor->Trace(operations_);
36 }
37 
38 FilterOperations& FilterOperations::operator=(const FilterOperations& other) =
39     default;
40 
operator ==(const FilterOperations & o) const41 bool FilterOperations::operator==(const FilterOperations& o) const {
42   if (operations_.size() != o.operations_.size())
43     return false;
44 
45   unsigned s = operations_.size();
46   for (unsigned i = 0; i < s; i++) {
47     if (*operations_[i] != *o.operations_[i])
48       return false;
49   }
50 
51   return true;
52 }
53 
CanInterpolateWith(const FilterOperations & other) const54 bool FilterOperations::CanInterpolateWith(const FilterOperations& other) const {
55   auto can_interpolate = [](FilterOperation* operation) {
56     return FilterOperation::CanInterpolate(operation->GetType());
57   };
58   if (!std::all_of(Operations().begin(), Operations().end(), can_interpolate) ||
59       !std::all_of(other.Operations().begin(), other.Operations().end(),
60                    can_interpolate)) {
61     return false;
62   }
63 
64   wtf_size_t common_size =
65       std::min(Operations().size(), other.Operations().size());
66   for (wtf_size_t i = 0; i < common_size; ++i) {
67     if (!Operations()[i]->IsSameType(*other.Operations()[i]))
68       return false;
69   }
70   return true;
71 }
72 
MapRect(const FloatRect & rect) const73 FloatRect FilterOperations::MapRect(const FloatRect& rect) const {
74   auto accumulate_mapped_rect = [](const FloatRect& rect,
75                                    const Member<FilterOperation>& op) {
76     return op->MapRect(rect);
77   };
78   return std::accumulate(operations_.begin(), operations_.end(), rect,
79                          accumulate_mapped_rect);
80 }
81 
HasFilterThatAffectsOpacity() const82 bool FilterOperations::HasFilterThatAffectsOpacity() const {
83   return std::any_of(
84       operations_.begin(), operations_.end(),
85       [](const auto& operation) { return operation->AffectsOpacity(); });
86 }
87 
HasFilterThatMovesPixels() const88 bool FilterOperations::HasFilterThatMovesPixels() const {
89   return std::any_of(
90       operations_.begin(), operations_.end(),
91       [](const auto& operation) { return operation->MovesPixels(); });
92 }
93 
HasReferenceFilter() const94 bool FilterOperations::HasReferenceFilter() const {
95   return std::any_of(
96       operations_.begin(), operations_.end(), [](const auto& operation) {
97         return operation->GetType() == FilterOperation::REFERENCE;
98       });
99 }
100 
AddClient(SVGResourceClient & client) const101 void FilterOperations::AddClient(SVGResourceClient& client) const {
102   for (FilterOperation* operation : operations_) {
103     if (operation->GetType() == FilterOperation::REFERENCE)
104       To<ReferenceFilterOperation>(*operation).AddClient(client);
105   }
106 }
107 
RemoveClient(SVGResourceClient & client) const108 void FilterOperations::RemoveClient(SVGResourceClient& client) const {
109   for (FilterOperation* operation : operations_) {
110     if (operation->GetType() == FilterOperation::REFERENCE)
111       To<ReferenceFilterOperation>(*operation).RemoveClient(client);
112   }
113 }
114 
115 }  // namespace blink
116