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 #ifndef CC_OUTPUT_FILTER_OPERATION_H_ | |
6 #define CC_OUTPUT_FILTER_OPERATION_H_ | |
7 | |
8 #include "base/logging.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "skia/ext/refptr.h" | |
11 #include "third_party/skia/include/core/SkColor.h" | |
12 #include "third_party/skia/include/core/SkImageFilter.h" | |
13 #include "third_party/skia/include/core/SkRegion.h" | |
14 #include "third_party/skia/include/core/SkScalar.h" | |
15 #include "ui/gfx/geometry/point.h" | |
16 | |
17 namespace base { | |
18 namespace trace_event { | |
19 class TracedValue; | |
20 } | |
21 class Value; | |
22 } | |
23 | |
24 namespace cc { | |
25 | |
26 class FilterOperation { | |
27 public: | |
28 enum FilterType { | |
29 GRAYSCALE, | |
30 SEPIA, | |
31 SATURATE, | |
32 HUE_ROTATE, | |
33 INVERT, | |
34 BRIGHTNESS, | |
35 CONTRAST, | |
36 OPACITY, | |
37 BLUR, | |
38 DROP_SHADOW, | |
39 COLOR_MATRIX, | |
40 ZOOM, | |
41 REFERENCE, | |
42 SATURATING_BRIGHTNESS, // Not used in CSS/SVG. | |
43 ALPHA_THRESHOLD, // Not used in CSS/SVG. | |
44 FILTER_TYPE_LAST = ALPHA_THRESHOLD | |
45 }; | |
46 | |
47 FilterOperation(const FilterOperation& other); | |
48 | |
49 ~FilterOperation(); | |
50 | |
51 FilterType type() const { return type_; } | |
52 | |
53 float amount() const { | |
54 DCHECK_NE(type_, COLOR_MATRIX); | |
55 DCHECK_NE(type_, REFERENCE); | |
56 return amount_; | |
57 } | |
58 | |
59 float outer_threshold() const { | |
60 DCHECK_EQ(type_, ALPHA_THRESHOLD); | |
61 return outer_threshold_; | |
62 } | |
63 | |
64 gfx::Point drop_shadow_offset() const { | |
65 DCHECK_EQ(type_, DROP_SHADOW); | |
66 return drop_shadow_offset_; | |
67 } | |
68 | |
69 SkColor drop_shadow_color() const { | |
70 DCHECK_EQ(type_, DROP_SHADOW); | |
71 return drop_shadow_color_; | |
72 } | |
73 | |
74 skia::RefPtr<SkImageFilter> image_filter() const { | |
75 DCHECK_EQ(type_, REFERENCE); | |
76 return image_filter_; | |
77 } | |
78 | |
79 const SkScalar* matrix() const { | |
80 DCHECK_EQ(type_, COLOR_MATRIX); | |
81 return matrix_; | |
82 } | |
83 | |
84 int zoom_inset() const { | |
85 DCHECK_EQ(type_, ZOOM); | |
86 return zoom_inset_; | |
87 } | |
88 | |
89 const SkRegion& region() const { | |
90 DCHECK_EQ(type_, ALPHA_THRESHOLD); | |
91 return region_; | |
92 } | |
93 | |
94 static FilterOperation CreateGrayscaleFilter(float amount) { | |
95 return FilterOperation(GRAYSCALE, amount); | |
96 } | |
97 | |
98 static FilterOperation CreateSepiaFilter(float amount) { | |
99 return FilterOperation(SEPIA, amount); | |
100 } | |
101 | |
102 static FilterOperation CreateSaturateFilter(float amount) { | |
103 return FilterOperation(SATURATE, amount); | |
104 } | |
105 | |
106 static FilterOperation CreateHueRotateFilter(float amount) { | |
107 return FilterOperation(HUE_ROTATE, amount); | |
108 } | |
109 | |
110 static FilterOperation CreateInvertFilter(float amount) { | |
111 return FilterOperation(INVERT, amount); | |
112 } | |
113 | |
114 static FilterOperation CreateBrightnessFilter(float amount) { | |
115 return FilterOperation(BRIGHTNESS, amount); | |
116 } | |
117 | |
118 static FilterOperation CreateContrastFilter(float amount) { | |
119 return FilterOperation(CONTRAST, amount); | |
120 } | |
121 | |
122 static FilterOperation CreateOpacityFilter(float amount) { | |
123 return FilterOperation(OPACITY, amount); | |
124 } | |
125 | |
126 static FilterOperation CreateBlurFilter(float amount) { | |
127 return FilterOperation(BLUR, amount); | |
128 } | |
129 | |
130 static FilterOperation CreateDropShadowFilter(const gfx::Point& offset, | |
131 float std_deviation, | |
132 SkColor color) { | |
133 return FilterOperation(DROP_SHADOW, offset, std_deviation, color); | |
134 } | |
135 | |
136 static FilterOperation CreateColorMatrixFilter(SkScalar matrix[20]) { | |
137 return FilterOperation(COLOR_MATRIX, matrix); | |
138 } | |
139 | |
140 static FilterOperation CreateZoomFilter(float amount, int inset) { | |
141 return FilterOperation(ZOOM, amount, inset); | |
142 } | |
143 | |
144 static FilterOperation CreateReferenceFilter( | |
145 const skia::RefPtr<SkImageFilter>& image_filter) { | |
146 return FilterOperation(REFERENCE, image_filter); | |
147 } | |
148 | |
149 static FilterOperation CreateSaturatingBrightnessFilter(float amount) { | |
150 return FilterOperation(SATURATING_BRIGHTNESS, amount); | |
151 } | |
152 | |
153 static FilterOperation CreateAlphaThresholdFilter(const SkRegion& region, | |
154 float inner_threshold, | |
155 float outer_threshold) { | |
156 return FilterOperation(ALPHA_THRESHOLD, region, | |
157 inner_threshold, outer_threshold); | |
158 } | |
159 | |
160 bool operator==(const FilterOperation& other) const; | |
161 | |
162 bool operator!=(const FilterOperation& other) const { | |
163 return !(*this == other); | |
164 } | |
165 | |
166 // Methods for restoring a FilterOperation. | |
167 static FilterOperation CreateEmptyFilter() { | |
168 return FilterOperation(GRAYSCALE, 0.f); | |
169 } | |
170 | |
171 void set_type(FilterType type) { type_ = type; } | |
172 | |
173 void set_amount(float amount) { | |
174 DCHECK_NE(type_, COLOR_MATRIX); | |
175 DCHECK_NE(type_, REFERENCE); | |
176 amount_ = amount; | |
177 } | |
178 | |
179 void set_outer_threshold(float outer_threshold) { | |
180 DCHECK_EQ(type_, ALPHA_THRESHOLD); | |
181 outer_threshold_ = outer_threshold; | |
182 } | |
183 | |
184 void set_drop_shadow_offset(const gfx::Point& offset) { | |
185 DCHECK_EQ(type_, DROP_SHADOW); | |
186 drop_shadow_offset_ = offset; | |
187 } | |
188 | |
189 void set_drop_shadow_color(SkColor color) { | |
190 DCHECK_EQ(type_, DROP_SHADOW); | |
191 drop_shadow_color_ = color; | |
192 } | |
193 | |
194 void set_image_filter(const skia::RefPtr<SkImageFilter>& image_filter) { | |
195 DCHECK_EQ(type_, REFERENCE); | |
196 image_filter_ = image_filter; | |
197 } | |
198 | |
199 void set_matrix(const SkScalar matrix[20]) { | |
200 DCHECK_EQ(type_, COLOR_MATRIX); | |
201 for (unsigned i = 0; i < 20; ++i) | |
202 matrix_[i] = matrix[i]; | |
203 } | |
204 | |
205 void set_zoom_inset(int inset) { | |
206 DCHECK_EQ(type_, ZOOM); | |
207 zoom_inset_ = inset; | |
208 } | |
209 | |
210 void set_region(const SkRegion& region) { | |
211 DCHECK_EQ(type_, ALPHA_THRESHOLD); | |
212 region_ = region; | |
213 } | |
214 | |
215 // Given two filters of the same type, returns a filter operation created by | |
216 // linearly interpolating a |progress| fraction from |from| to |to|. If either | |
217 // |from| or |to| (but not both) is null, it is treated as a no-op filter of | |
218 // the same type as the other given filter. If both |from| and |to| are null, | |
219 // or if |from| and |to| are non-null but of different types, returns a | |
220 // no-op filter. | |
221 static FilterOperation Blend(const FilterOperation* from, | |
222 const FilterOperation* to, | |
223 double progress); | |
224 | |
225 void AsValueInto(base::trace_event::TracedValue* value) const; | |
226 | |
227 private: | |
228 FilterOperation(FilterType type, float amount); | |
229 | |
230 FilterOperation(FilterType type, | |
231 const gfx::Point& offset, | |
232 float stdDeviation, | |
233 SkColor color); | |
234 | |
235 FilterOperation(FilterType, SkScalar matrix[20]); | |
236 | |
237 FilterOperation(FilterType type, float amount, int inset); | |
238 | |
239 FilterOperation(FilterType type, | |
240 const skia::RefPtr<SkImageFilter>& image_filter); | |
241 | |
242 FilterOperation(FilterType type, | |
243 const SkRegion& region, | |
244 float inner_threshold, | |
245 float outer_threshold); | |
246 | |
247 FilterType type_; | |
248 float amount_; | |
249 float outer_threshold_; | |
250 gfx::Point drop_shadow_offset_; | |
251 SkColor drop_shadow_color_; | |
252 skia::RefPtr<SkImageFilter> image_filter_; | |
253 SkScalar matrix_[20]; | |
254 int zoom_inset_; | |
255 SkRegion region_; | |
256 }; | |
257 | |
258 } // namespace cc | |
259 | |
260 #endif // CC_OUTPUT_FILTER_OPERATION_H_ | |
OLD | NEW |