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.
5 #ifndef CC_OUTPUT_FILTER_OPERATION_H_
6 #define CC_OUTPUT_FILTER_OPERATION_H_
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/geometry/point.h"
27 class CC_EXPORT FilterOperation
{
43 SATURATING_BRIGHTNESS
, // Not used in CSS/SVG.
44 ALPHA_THRESHOLD
, // Not used in CSS/SVG.
45 FILTER_TYPE_LAST
= ALPHA_THRESHOLD
48 FilterOperation(const FilterOperation
& other
);
52 FilterType
type() const { return type_
; }
54 float amount() const {
55 DCHECK_NE(type_
, COLOR_MATRIX
);
56 DCHECK_NE(type_
, REFERENCE
);
60 float outer_threshold() const {
61 DCHECK_EQ(type_
, ALPHA_THRESHOLD
);
62 return outer_threshold_
;
65 gfx::Point
drop_shadow_offset() const {
66 DCHECK_EQ(type_
, DROP_SHADOW
);
67 return drop_shadow_offset_
;
70 SkColor
drop_shadow_color() const {
71 DCHECK_EQ(type_
, DROP_SHADOW
);
72 return drop_shadow_color_
;
75 skia::RefPtr
<SkImageFilter
> image_filter() const {
76 DCHECK_EQ(type_
, REFERENCE
);
80 const SkScalar
* matrix() const {
81 DCHECK_EQ(type_
, COLOR_MATRIX
);
85 int zoom_inset() const {
86 DCHECK_EQ(type_
, ZOOM
);
90 const SkRegion
& region() const {
91 DCHECK_EQ(type_
, ALPHA_THRESHOLD
);
95 static FilterOperation
CreateGrayscaleFilter(float amount
) {
96 return FilterOperation(GRAYSCALE
, amount
);
99 static FilterOperation
CreateSepiaFilter(float amount
) {
100 return FilterOperation(SEPIA
, amount
);
103 static FilterOperation
CreateSaturateFilter(float amount
) {
104 return FilterOperation(SATURATE
, amount
);
107 static FilterOperation
CreateHueRotateFilter(float amount
) {
108 return FilterOperation(HUE_ROTATE
, amount
);
111 static FilterOperation
CreateInvertFilter(float amount
) {
112 return FilterOperation(INVERT
, amount
);
115 static FilterOperation
CreateBrightnessFilter(float amount
) {
116 return FilterOperation(BRIGHTNESS
, amount
);
119 static FilterOperation
CreateContrastFilter(float amount
) {
120 return FilterOperation(CONTRAST
, amount
);
123 static FilterOperation
CreateOpacityFilter(float amount
) {
124 return FilterOperation(OPACITY
, amount
);
127 static FilterOperation
CreateBlurFilter(float amount
) {
128 return FilterOperation(BLUR
, amount
);
131 static FilterOperation
CreateDropShadowFilter(const gfx::Point
& offset
,
134 return FilterOperation(DROP_SHADOW
, offset
, std_deviation
, color
);
137 static FilterOperation
CreateColorMatrixFilter(SkScalar matrix
[20]) {
138 return FilterOperation(COLOR_MATRIX
, matrix
);
141 static FilterOperation
CreateZoomFilter(float amount
, int inset
) {
142 return FilterOperation(ZOOM
, amount
, inset
);
145 static FilterOperation
CreateReferenceFilter(
146 const skia::RefPtr
<SkImageFilter
>& image_filter
) {
147 return FilterOperation(REFERENCE
, image_filter
);
150 static FilterOperation
CreateSaturatingBrightnessFilter(float amount
) {
151 return FilterOperation(SATURATING_BRIGHTNESS
, amount
);
154 static FilterOperation
CreateAlphaThresholdFilter(const SkRegion
& region
,
155 float inner_threshold
,
156 float outer_threshold
) {
157 return FilterOperation(ALPHA_THRESHOLD
, region
,
158 inner_threshold
, outer_threshold
);
161 bool operator==(const FilterOperation
& other
) const;
163 bool operator!=(const FilterOperation
& other
) const {
164 return !(*this == other
);
167 // Methods for restoring a FilterOperation.
168 static FilterOperation
CreateEmptyFilter() {
169 return FilterOperation(GRAYSCALE
, 0.f
);
172 void set_type(FilterType type
) { type_
= type
; }
174 void set_amount(float amount
) {
175 DCHECK_NE(type_
, COLOR_MATRIX
);
176 DCHECK_NE(type_
, REFERENCE
);
180 void set_outer_threshold(float outer_threshold
) {
181 DCHECK_EQ(type_
, ALPHA_THRESHOLD
);
182 outer_threshold_
= outer_threshold
;
185 void set_drop_shadow_offset(const gfx::Point
& offset
) {
186 DCHECK_EQ(type_
, DROP_SHADOW
);
187 drop_shadow_offset_
= offset
;
190 void set_drop_shadow_color(SkColor color
) {
191 DCHECK_EQ(type_
, DROP_SHADOW
);
192 drop_shadow_color_
= color
;
195 void set_image_filter(const skia::RefPtr
<SkImageFilter
>& image_filter
) {
196 DCHECK_EQ(type_
, REFERENCE
);
197 image_filter_
= image_filter
;
200 void set_matrix(const SkScalar matrix
[20]) {
201 DCHECK_EQ(type_
, COLOR_MATRIX
);
202 for (unsigned i
= 0; i
< 20; ++i
)
203 matrix_
[i
] = matrix
[i
];
206 void set_zoom_inset(int inset
) {
207 DCHECK_EQ(type_
, ZOOM
);
211 void set_region(const SkRegion
& region
) {
212 DCHECK_EQ(type_
, ALPHA_THRESHOLD
);
216 // Given two filters of the same type, returns a filter operation created by
217 // linearly interpolating a |progress| fraction from |from| to |to|. If either
218 // |from| or |to| (but not both) is null, it is treated as a no-op filter of
219 // the same type as the other given filter. If both |from| and |to| are null,
220 // or if |from| and |to| are non-null but of different types, returns a
222 static FilterOperation
Blend(const FilterOperation
* from
,
223 const FilterOperation
* to
,
226 void AsValueInto(base::debug::TracedValue
* value
) const;
229 FilterOperation(FilterType type
, float amount
);
231 FilterOperation(FilterType type
,
232 const gfx::Point
& offset
,
236 FilterOperation(FilterType
, SkScalar matrix
[20]);
238 FilterOperation(FilterType type
, float amount
, int inset
);
240 FilterOperation(FilterType type
,
241 const skia::RefPtr
<SkImageFilter
>& image_filter
);
243 FilterOperation(FilterType type
,
244 const SkRegion
& region
,
245 float inner_threshold
,
246 float outer_threshold
);
250 float outer_threshold_
;
251 gfx::Point drop_shadow_offset_
;
252 SkColor drop_shadow_color_
;
253 skia::RefPtr
<SkImageFilter
> image_filter_
;
254 SkScalar matrix_
[20];
261 #endif // CC_OUTPUT_FILTER_OPERATION_H_