OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "cc/output/filter_operations.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include <cmath> | |
10 #include <numeric> | |
11 | |
12 #include "base/trace_event/trace_event_argument.h" | |
13 #include "base/values.h" | |
14 #include "cc/output/filter_operation.h" | |
15 #include "ui/gfx/geometry/rect.h" | |
16 | |
17 namespace cc { | |
18 | |
19 FilterOperations::FilterOperations() {} | |
20 | |
21 FilterOperations::FilterOperations(const FilterOperations& other) | |
22 : operations_(other.operations_) {} | |
23 | |
24 FilterOperations::FilterOperations(std::vector<FilterOperation>&& operations) | |
25 : operations_(std::move(operations)) {} | |
26 | |
27 FilterOperations::~FilterOperations() {} | |
28 | |
29 FilterOperations& FilterOperations::operator=(const FilterOperations& other) { | |
30 operations_ = other.operations_; | |
31 return *this; | |
32 } | |
33 | |
34 FilterOperations& FilterOperations::operator=(FilterOperations&& other) { | |
35 operations_ = std::move(other.operations_); | |
36 return *this; | |
37 } | |
38 | |
39 bool FilterOperations::operator==(const FilterOperations& other) const { | |
40 if (other.size() != size()) | |
41 return false; | |
42 for (size_t i = 0; i < size(); ++i) { | |
43 if (other.at(i) != at(i)) | |
44 return false; | |
45 } | |
46 return true; | |
47 } | |
48 | |
49 void FilterOperations::Append(const FilterOperation& filter) { | |
50 operations_.push_back(filter); | |
51 } | |
52 | |
53 void FilterOperations::Clear() { | |
54 operations_.clear(); | |
55 } | |
56 | |
57 bool FilterOperations::IsEmpty() const { | |
58 return operations_.empty(); | |
59 } | |
60 | |
61 static int SpreadForStdDeviation(float std_deviation) { | |
62 // Corresponds to MapStdDeviation in filter_operation.cc. | |
63 return std_deviation * 3; | |
64 } | |
65 | |
66 gfx::Rect FilterOperations::MapRect(const gfx::Rect& rect, | |
67 const SkMatrix& matrix) const { | |
68 auto accumulate_rect = [matrix](const gfx::Rect& rect, | |
69 const FilterOperation& op) { | |
70 return op.MapRect(rect, matrix); | |
71 }; | |
72 return std::accumulate(operations_.begin(), operations_.end(), rect, | |
73 accumulate_rect); | |
74 } | |
75 | |
76 gfx::Rect FilterOperations::MapRectReverse(const gfx::Rect& rect, | |
77 const SkMatrix& matrix) const { | |
78 auto accumulate_rect = [&matrix](const gfx::Rect& rect, | |
79 const FilterOperation& op) { | |
80 return op.MapRectReverse(rect, matrix); | |
81 }; | |
82 return std::accumulate(operations_.rbegin(), operations_.rend(), rect, | |
83 accumulate_rect); | |
84 } | |
85 | |
86 void FilterOperations::GetOutsets(int* top, | |
87 int* right, | |
88 int* bottom, | |
89 int* left) const { | |
90 *top = *right = *bottom = *left = 0; | |
91 for (size_t i = 0; i < operations_.size(); ++i) { | |
92 const FilterOperation& op = operations_[i]; | |
93 // TODO(hendrikw): We should refactor some of this. See crbug.com/523534. | |
94 if (op.type() == FilterOperation::REFERENCE) { | |
95 if (!op.image_filter()) | |
96 continue; | |
97 SkIRect src = SkIRect::MakeWH(0, 0); | |
98 SkIRect dst = op.image_filter()->filterBounds(src, SkMatrix::I()); | |
99 *top += std::max(0, -dst.top()); | |
100 *right += std::max(0, dst.right()); | |
101 *bottom += std::max(0, dst.bottom()); | |
102 *left += std::max(0, -dst.left()); | |
103 } else { | |
104 if (op.type() == FilterOperation::BLUR || | |
105 op.type() == FilterOperation::DROP_SHADOW) { | |
106 int spread = SpreadForStdDeviation(op.amount()); | |
107 if (op.type() == FilterOperation::BLUR) { | |
108 *top += spread; | |
109 *right += spread; | |
110 *bottom += spread; | |
111 *left += spread; | |
112 } else { | |
113 *top += std::max(0, spread - op.drop_shadow_offset().y()); | |
114 *right += std::max(0, spread + op.drop_shadow_offset().x()); | |
115 *bottom += std::max(0, spread + op.drop_shadow_offset().y()); | |
116 *left += std::max(0, spread - op.drop_shadow_offset().x()); | |
117 } | |
118 } | |
119 } | |
120 } | |
121 } | |
122 | |
123 bool FilterOperations::HasFilterThatMovesPixels() const { | |
124 for (size_t i = 0; i < operations_.size(); ++i) { | |
125 const FilterOperation& op = operations_[i]; | |
126 switch (op.type()) { | |
127 case FilterOperation::BLUR: | |
128 case FilterOperation::DROP_SHADOW: | |
129 case FilterOperation::ZOOM: | |
130 return true; | |
131 case FilterOperation::REFERENCE: | |
132 // TODO(hendrikw): SkImageFilter needs a function that tells us if the | |
133 // filter can move pixels. See crbug.com/523538. | |
134 return true; | |
135 case FilterOperation::OPACITY: | |
136 case FilterOperation::COLOR_MATRIX: | |
137 case FilterOperation::GRAYSCALE: | |
138 case FilterOperation::SEPIA: | |
139 case FilterOperation::SATURATE: | |
140 case FilterOperation::HUE_ROTATE: | |
141 case FilterOperation::INVERT: | |
142 case FilterOperation::BRIGHTNESS: | |
143 case FilterOperation::CONTRAST: | |
144 case FilterOperation::SATURATING_BRIGHTNESS: | |
145 case FilterOperation::ALPHA_THRESHOLD: | |
146 break; | |
147 } | |
148 } | |
149 return false; | |
150 } | |
151 | |
152 bool FilterOperations::HasFilterThatAffectsOpacity() const { | |
153 for (size_t i = 0; i < operations_.size(); ++i) { | |
154 const FilterOperation& op = operations_[i]; | |
155 // TODO(ajuma): Make this smarter for reference filters. Once SkImageFilter | |
156 // can report affectsOpacity(), call that. | |
157 switch (op.type()) { | |
158 case FilterOperation::OPACITY: | |
159 case FilterOperation::BLUR: | |
160 case FilterOperation::DROP_SHADOW: | |
161 case FilterOperation::ZOOM: | |
162 case FilterOperation::REFERENCE: | |
163 case FilterOperation::ALPHA_THRESHOLD: | |
164 return true; | |
165 case FilterOperation::COLOR_MATRIX: { | |
166 const SkScalar* matrix = op.matrix(); | |
167 if (matrix[15] || | |
168 matrix[16] || | |
169 matrix[17] || | |
170 matrix[18] != 1 || | |
171 matrix[19]) | |
172 return true; | |
173 break; | |
174 } | |
175 case FilterOperation::GRAYSCALE: | |
176 case FilterOperation::SEPIA: | |
177 case FilterOperation::SATURATE: | |
178 case FilterOperation::HUE_ROTATE: | |
179 case FilterOperation::INVERT: | |
180 case FilterOperation::BRIGHTNESS: | |
181 case FilterOperation::CONTRAST: | |
182 case FilterOperation::SATURATING_BRIGHTNESS: | |
183 break; | |
184 } | |
185 } | |
186 return false; | |
187 } | |
188 | |
189 bool FilterOperations::HasReferenceFilter() const { | |
190 for (size_t i = 0; i < operations_.size(); ++i) { | |
191 if (operations_[i].type() == FilterOperation::REFERENCE) | |
192 return true; | |
193 } | |
194 return false; | |
195 } | |
196 | |
197 FilterOperations FilterOperations::Blend(const FilterOperations& from, | |
198 double progress) const { | |
199 if (HasReferenceFilter() || from.HasReferenceFilter()) | |
200 return *this; | |
201 | |
202 bool from_is_longer = from.size() > size(); | |
203 | |
204 size_t shorter_size, longer_size; | |
205 if (size() == from.size()) { | |
206 shorter_size = longer_size = size(); | |
207 } else if (from_is_longer) { | |
208 longer_size = from.size(); | |
209 shorter_size = size(); | |
210 } else { | |
211 longer_size = size(); | |
212 shorter_size = from.size(); | |
213 } | |
214 | |
215 for (size_t i = 0; i < shorter_size; i++) { | |
216 if (from.at(i).type() != at(i).type()) | |
217 return *this; | |
218 } | |
219 | |
220 FilterOperations blended_filters; | |
221 for (size_t i = 0; i < shorter_size; i++) { | |
222 blended_filters.Append( | |
223 FilterOperation::Blend(&from.at(i), &at(i), progress)); | |
224 } | |
225 | |
226 if (from_is_longer) { | |
227 for (size_t i = shorter_size; i < longer_size; i++) { | |
228 blended_filters.Append( | |
229 FilterOperation::Blend(&from.at(i), NULL, progress)); | |
230 } | |
231 } else { | |
232 for (size_t i = shorter_size; i < longer_size; i++) | |
233 blended_filters.Append(FilterOperation::Blend(NULL, &at(i), progress)); | |
234 } | |
235 | |
236 return blended_filters; | |
237 } | |
238 | |
239 void FilterOperations::AsValueInto( | |
240 base::trace_event::TracedValue* value) const { | |
241 for (size_t i = 0; i < operations_.size(); ++i) { | |
242 value->BeginDictionary(); | |
243 operations_[i].AsValueInto(value); | |
244 value->EndDictionary(); | |
245 } | |
246 } | |
247 | |
248 std::string FilterOperations::ToString() const { | |
249 base::trace_event::TracedValue value; | |
250 value.BeginArray("FilterOperations"); | |
251 AsValueInto(&value); | |
252 value.EndArray(); | |
253 return value.ToString(); | |
254 } | |
255 | |
256 } // namespace cc | |
OLD | NEW |