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