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