OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "cc/base/math_util.h" | 7 #include "cc/base/math_util.h" |
8 #include "cc/output/filter_operation.h" | 8 #include "cc/output/filter_operation.h" |
9 #include "third_party/skia/include/core/SkMath.h" | 9 #include "third_party/skia/include/core/SkMath.h" |
10 | 10 |
11 namespace cc { | 11 namespace cc { |
12 | 12 |
13 bool FilterOperation::operator==(const FilterOperation& other) const { | 13 bool FilterOperation::operator==(const FilterOperation& other) const { |
14 if (type_ != other.type_) | 14 if (type_ != other.type_) |
15 return false; | 15 return false; |
16 if (type_ == COLOR_MATRIX) | 16 if (type_ == COLOR_MATRIX) |
17 return !memcmp(matrix_, other.matrix_, sizeof(matrix_)); | 17 return !memcmp(matrix_, other.matrix_, sizeof(matrix_)); |
18 if (type_ == DROP_SHADOW) { | 18 if (type_ == DROP_SHADOW) { |
19 return amount_ == other.amount_ && | 19 return amount_ == other.amount_ && |
20 drop_shadow_offset_ == other.drop_shadow_offset_ && | 20 drop_shadow_offset_ == other.drop_shadow_offset_ && |
21 drop_shadow_color_ == other.drop_shadow_color_; | 21 drop_shadow_color_ == other.drop_shadow_color_; |
22 } | 22 } |
23 if (type_ == REFERENCE) | |
24 return image_filter_.get() == other.image_filter_.get(); | |
23 return amount_ == other.amount_; | 25 return amount_ == other.amount_; |
24 } | 26 } |
25 | 27 |
26 FilterOperation::FilterOperation(FilterType type, float amount) | 28 FilterOperation::FilterOperation(FilterType type, float amount) |
27 : type_(type), | 29 : type_(type), |
28 amount_(amount), | 30 amount_(amount), |
29 drop_shadow_offset_(0, 0), | 31 drop_shadow_offset_(0, 0), |
30 drop_shadow_color_(0), | 32 drop_shadow_color_(0), |
31 zoom_inset_(0) { | 33 zoom_inset_(0) { |
32 DCHECK_NE(type_, DROP_SHADOW); | 34 DCHECK_NE(type_, DROP_SHADOW); |
33 DCHECK_NE(type_, COLOR_MATRIX); | 35 DCHECK_NE(type_, COLOR_MATRIX); |
36 DCHECK_NE(type_, REFERENCE); | |
34 memset(matrix_, 0, sizeof(matrix_)); | 37 memset(matrix_, 0, sizeof(matrix_)); |
35 } | 38 } |
36 | 39 |
37 FilterOperation::FilterOperation(FilterType type, | 40 FilterOperation::FilterOperation(FilterType type, |
38 gfx::Point offset, | 41 gfx::Point offset, |
39 float stdDeviation, | 42 float stdDeviation, |
40 SkColor color) | 43 SkColor color) |
41 : type_(type), | 44 : type_(type), |
42 amount_(stdDeviation), | 45 amount_(stdDeviation), |
43 drop_shadow_offset_(offset), | 46 drop_shadow_offset_(offset), |
(...skipping 16 matching lines...) Expand all Loading... | |
60 FilterOperation::FilterOperation(FilterType type, float amount, int inset) | 63 FilterOperation::FilterOperation(FilterType type, float amount, int inset) |
61 : type_(type), | 64 : type_(type), |
62 amount_(amount), | 65 amount_(amount), |
63 drop_shadow_offset_(0, 0), | 66 drop_shadow_offset_(0, 0), |
64 drop_shadow_color_(0), | 67 drop_shadow_color_(0), |
65 zoom_inset_(inset) { | 68 zoom_inset_(inset) { |
66 DCHECK_EQ(type_, ZOOM); | 69 DCHECK_EQ(type_, ZOOM); |
67 memset(matrix_, 0, sizeof(matrix_)); | 70 memset(matrix_, 0, sizeof(matrix_)); |
68 } | 71 } |
69 | 72 |
73 FilterOperation::FilterOperation( | |
74 FilterType type, | |
75 const skia::RefPtr<SkImageFilter>& image_filter) | |
76 : type_(type), | |
77 amount_(0), | |
78 drop_shadow_offset_(0, 0), | |
79 drop_shadow_color_(0), | |
80 image_filter_(image_filter), | |
81 zoom_inset_(0) { | |
82 DCHECK_EQ(type_, REFERENCE); | |
83 DCHECK(image_filter.get()); | |
84 memset(matrix_, 0, sizeof(matrix_)); | |
85 } | |
86 | |
87 FilterOperation::FilterOperation(const FilterOperation& other) | |
88 : type_(other.type_), | |
89 amount_(other.amount_), | |
90 drop_shadow_offset_(other.drop_shadow_offset_), | |
91 drop_shadow_color_(other.drop_shadow_color_), | |
92 image_filter_(other.image_filter_), | |
93 zoom_inset_(other.zoom_inset_) { | |
94 memcpy(matrix_, other.matrix_, sizeof(matrix_)); | |
95 } | |
96 | |
97 FilterOperation::~FilterOperation() { | |
98 } | |
99 | |
70 // TODO(ajuma): Define a version of ui::Tween::ValueBetween for floats, and use | 100 // TODO(ajuma): Define a version of ui::Tween::ValueBetween for floats, and use |
71 // that instead. | 101 // that instead. |
72 static float BlendFloats(float from, float to, double progress) { | 102 static float BlendFloats(float from, float to, double progress) { |
73 return from * (1.0 - progress) + to * progress; | 103 return from * (1.0 - progress) + to * progress; |
74 } | 104 } |
75 | 105 |
76 static int BlendInts(int from, int to, double progress) { | 106 static int BlendInts(int from, int to, double progress) { |
77 return static_cast<int>( | 107 return static_cast<int>( |
78 MathUtil::Round(from * (1.0 - progress) + to * progress)); | 108 MathUtil::Round(from * (1.0 - progress) + to * progress)); |
79 } | 109 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 case FilterOperation::COLOR_MATRIX: { | 169 case FilterOperation::COLOR_MATRIX: { |
140 SkScalar matrix[20]; | 170 SkScalar matrix[20]; |
141 memset(matrix, 0, 20 * sizeof(SkScalar)); | 171 memset(matrix, 0, 20 * sizeof(SkScalar)); |
142 matrix[0] = matrix[6] = matrix[12] = matrix[18] = 1.f; | 172 matrix[0] = matrix[6] = matrix[12] = matrix[18] = 1.f; |
143 return FilterOperation::CreateColorMatrixFilter(matrix); | 173 return FilterOperation::CreateColorMatrixFilter(matrix); |
144 } | 174 } |
145 case FilterOperation::ZOOM: | 175 case FilterOperation::ZOOM: |
146 return FilterOperation::CreateZoomFilter(1.f, 0); | 176 return FilterOperation::CreateZoomFilter(1.f, 0); |
147 case FilterOperation::SATURATING_BRIGHTNESS: | 177 case FilterOperation::SATURATING_BRIGHTNESS: |
148 return FilterOperation::CreateSaturatingBrightnessFilter(0.f); | 178 return FilterOperation::CreateSaturatingBrightnessFilter(0.f); |
179 case FilterOperation::REFERENCE: | |
149 default: | 180 default: |
150 NOTREACHED(); | 181 NOTREACHED(); |
151 return FilterOperation::CreateEmptyFilter(); | 182 return FilterOperation::CreateEmptyFilter(); |
152 } | 183 } |
153 } | 184 } |
154 | 185 |
155 static float ClampAmountForFilterType(float amount, | 186 static float ClampAmountForFilterType(float amount, |
156 FilterOperation::FilterType type) { | 187 FilterOperation::FilterType type) { |
157 switch (type) { | 188 switch (type) { |
158 case FilterOperation::GRAYSCALE: | 189 case FilterOperation::GRAYSCALE: |
159 case FilterOperation::SEPIA: | 190 case FilterOperation::SEPIA: |
160 case FilterOperation::INVERT: | 191 case FilterOperation::INVERT: |
161 case FilterOperation::OPACITY: | 192 case FilterOperation::OPACITY: |
162 return MathUtil::ClampToRange(amount, 0.f, 1.f); | 193 return MathUtil::ClampToRange(amount, 0.f, 1.f); |
163 case FilterOperation::SATURATE: | 194 case FilterOperation::SATURATE: |
164 case FilterOperation::BRIGHTNESS: | 195 case FilterOperation::BRIGHTNESS: |
165 case FilterOperation::CONTRAST: | 196 case FilterOperation::CONTRAST: |
166 case FilterOperation::BLUR: | 197 case FilterOperation::BLUR: |
167 case FilterOperation::DROP_SHADOW: | 198 case FilterOperation::DROP_SHADOW: |
168 return std::max(amount, 0.f); | 199 return std::max(amount, 0.f); |
169 case FilterOperation::ZOOM: | 200 case FilterOperation::ZOOM: |
170 return std::max(amount, 1.f); | 201 return std::max(amount, 1.f); |
171 case FilterOperation::HUE_ROTATE: | 202 case FilterOperation::HUE_ROTATE: |
172 case FilterOperation::SATURATING_BRIGHTNESS: | 203 case FilterOperation::SATURATING_BRIGHTNESS: |
173 return amount; | 204 return amount; |
174 case FilterOperation::COLOR_MATRIX: | 205 case FilterOperation::COLOR_MATRIX: |
206 case FilterOperation::REFERENCE: | |
175 default: | 207 default: |
176 NOTREACHED(); | 208 NOTREACHED(); |
177 return amount; | 209 return amount; |
178 } | 210 } |
179 } | 211 } |
180 | 212 |
181 // static | 213 // static |
182 FilterOperation FilterOperation::Blend(const FilterOperation* from, | 214 FilterOperation FilterOperation::Blend(const FilterOperation* from, |
183 const FilterOperation* to, | 215 const FilterOperation* to, |
184 double progress) { | 216 double progress) { |
185 FilterOperation blended_filter = FilterOperation::CreateEmptyFilter(); | 217 FilterOperation blended_filter = FilterOperation::CreateEmptyFilter(); |
186 | 218 |
187 if (!from && !to) | 219 if (!from && !to) |
188 return blended_filter; | 220 return blended_filter; |
189 | 221 |
222 if ((from && from->type() == FilterOperation::REFERENCE) || | |
223 (to && to->type() == FilterOperation::REFERENCE)) { | |
224 if (progress > 0.5) | |
Stephen White
2013/07/31 19:18:51
Heh.. I guess it's better than nothing! :)
| |
225 return to ? *to : blended_filter; | |
226 else | |
227 return from ? *from : blended_filter; | |
228 } | |
229 | |
190 const FilterOperation& from_op = from ? *from : CreateNoOpFilter(to->type()); | 230 const FilterOperation& from_op = from ? *from : CreateNoOpFilter(to->type()); |
191 const FilterOperation& to_op = to ? *to : CreateNoOpFilter(from->type()); | 231 const FilterOperation& to_op = to ? *to : CreateNoOpFilter(from->type()); |
192 | 232 |
193 if (from_op.type() != to_op.type()) | 233 if (from_op.type() != to_op.type()) |
194 return blended_filter; | 234 return blended_filter; |
195 | 235 |
196 DCHECK(to_op.type() != FilterOperation::COLOR_MATRIX); | 236 DCHECK(to_op.type() != FilterOperation::COLOR_MATRIX); |
197 blended_filter.set_type(to_op.type()); | 237 blended_filter.set_type(to_op.type()); |
198 | 238 |
199 blended_filter.set_amount(ClampAmountForFilterType( | 239 blended_filter.set_amount(ClampAmountForFilterType( |
(...skipping 11 matching lines...) Expand all Loading... | |
211 from_op.drop_shadow_color(), to_op.drop_shadow_color(), progress)); | 251 from_op.drop_shadow_color(), to_op.drop_shadow_color(), progress)); |
212 } else if (to_op.type() == FilterOperation::ZOOM) { | 252 } else if (to_op.type() == FilterOperation::ZOOM) { |
213 blended_filter.set_zoom_inset(std::max( | 253 blended_filter.set_zoom_inset(std::max( |
214 BlendInts(from_op.zoom_inset(), to_op.zoom_inset(), progress), 0)); | 254 BlendInts(from_op.zoom_inset(), to_op.zoom_inset(), progress), 0)); |
215 } | 255 } |
216 | 256 |
217 return blended_filter; | 257 return blended_filter; |
218 } | 258 } |
219 | 259 |
220 } // namespace cc | 260 } // namespace cc |
OLD | NEW |