filter_operation.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #ifndef CC_PAINT_FILTER_OPERATION_H_
  5. #define CC_PAINT_FILTER_OPERATION_H_
  6. #include <memory>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/check_op.h"
  10. #include "base/containers/span.h"
  11. #include "cc/paint/paint_export.h"
  12. #include "cc/paint/paint_filter.h"
  13. #include "third_party/skia/include/core/SkColor.h"
  14. #include "third_party/skia/include/core/SkScalar.h"
  15. #include "third_party/skia/include/core/SkTileMode.h"
  16. #include "ui/gfx/geometry/point.h"
  17. #include "ui/gfx/geometry/rect.h"
  18. namespace base {
  19. namespace trace_event {
  20. class TracedValue;
  21. }
  22. } // namespace base
  23. namespace cc {
  24. class CC_PAINT_EXPORT FilterOperation {
  25. public:
  26. using Matrix = SkScalar[20];
  27. using ShapeRects = std::vector<gfx::Rect>;
  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. STRETCH, // Not used in CSS/SVG.
  45. FILTER_TYPE_LAST = STRETCH
  46. };
  47. FilterOperation();
  48. FilterOperation(const FilterOperation& other);
  49. ~FilterOperation();
  50. FilterType type() const { return type_; }
  51. float amount() const {
  52. DCHECK_NE(type_, COLOR_MATRIX);
  53. DCHECK_NE(type_, REFERENCE);
  54. return amount_;
  55. }
  56. float outer_threshold() const {
  57. DCHECK(type_ == ALPHA_THRESHOLD || type_ == STRETCH);
  58. return outer_threshold_;
  59. }
  60. gfx::Point drop_shadow_offset() const {
  61. DCHECK_EQ(type_, DROP_SHADOW);
  62. return drop_shadow_offset_;
  63. }
  64. SkColor4f drop_shadow_color() const {
  65. DCHECK_EQ(type_, DROP_SHADOW);
  66. return drop_shadow_color_;
  67. }
  68. const sk_sp<PaintFilter>& image_filter() const {
  69. DCHECK_EQ(type_, REFERENCE);
  70. return image_filter_;
  71. }
  72. const Matrix& matrix() const {
  73. DCHECK_EQ(type_, COLOR_MATRIX);
  74. return matrix_;
  75. }
  76. int zoom_inset() const {
  77. DCHECK_EQ(type_, ZOOM);
  78. return zoom_inset_;
  79. }
  80. const ShapeRects& shape() const {
  81. DCHECK_EQ(type_, ALPHA_THRESHOLD);
  82. return shape_;
  83. }
  84. SkTileMode blur_tile_mode() const {
  85. DCHECK_EQ(type_, BLUR);
  86. return blur_tile_mode_;
  87. }
  88. static FilterOperation CreateGrayscaleFilter(float amount) {
  89. return FilterOperation(GRAYSCALE, amount);
  90. }
  91. static FilterOperation CreateSepiaFilter(float amount) {
  92. return FilterOperation(SEPIA, amount);
  93. }
  94. static FilterOperation CreateSaturateFilter(float amount) {
  95. return FilterOperation(SATURATE, amount);
  96. }
  97. static FilterOperation CreateHueRotateFilter(float amount) {
  98. return FilterOperation(HUE_ROTATE, amount);
  99. }
  100. static FilterOperation CreateInvertFilter(float amount) {
  101. return FilterOperation(INVERT, amount);
  102. }
  103. static FilterOperation CreateBrightnessFilter(float amount) {
  104. return FilterOperation(BRIGHTNESS, amount);
  105. }
  106. static FilterOperation CreateContrastFilter(float amount) {
  107. return FilterOperation(CONTRAST, amount);
  108. }
  109. static FilterOperation CreateOpacityFilter(float amount) {
  110. return FilterOperation(OPACITY, amount);
  111. }
  112. static FilterOperation CreateBlurFilter(
  113. float amount,
  114. SkTileMode tile_mode = SkTileMode::kDecal) {
  115. return FilterOperation(BLUR, amount, tile_mode);
  116. }
  117. static FilterOperation CreateDropShadowFilter(const gfx::Point& offset,
  118. float std_deviation,
  119. SkColor4f color) {
  120. return FilterOperation(DROP_SHADOW, offset, std_deviation, color);
  121. }
  122. static FilterOperation CreateColorMatrixFilter(const Matrix& matrix) {
  123. return FilterOperation(COLOR_MATRIX, matrix);
  124. }
  125. static FilterOperation CreateZoomFilter(float amount, int inset) {
  126. return FilterOperation(ZOOM, amount, inset);
  127. }
  128. static FilterOperation CreateReferenceFilter(
  129. sk_sp<PaintFilter> image_filter) {
  130. return FilterOperation(REFERENCE, std::move(image_filter));
  131. }
  132. static FilterOperation CreateSaturatingBrightnessFilter(float amount) {
  133. return FilterOperation(SATURATING_BRIGHTNESS, amount);
  134. }
  135. static FilterOperation CreateAlphaThresholdFilter(const ShapeRects& shape,
  136. float inner_threshold,
  137. float outer_threshold) {
  138. return FilterOperation(ALPHA_THRESHOLD, shape, inner_threshold,
  139. outer_threshold);
  140. }
  141. static FilterOperation CreateStretchFilter(float amount_x, float amount_y) {
  142. return FilterOperation(STRETCH, amount_x, amount_y);
  143. }
  144. bool operator==(const FilterOperation& other) const;
  145. bool operator!=(const FilterOperation& other) const {
  146. return !(*this == other);
  147. }
  148. // Methods for restoring a FilterOperation.
  149. static FilterOperation CreateEmptyFilter() {
  150. return FilterOperation(GRAYSCALE, 0.f);
  151. }
  152. void set_type(FilterType type) { type_ = type; }
  153. void set_amount(float amount) {
  154. DCHECK_NE(type_, COLOR_MATRIX);
  155. DCHECK_NE(type_, REFERENCE);
  156. amount_ = amount;
  157. }
  158. void set_outer_threshold(float outer_threshold) {
  159. DCHECK(type_ == ALPHA_THRESHOLD || type_ == STRETCH);
  160. outer_threshold_ = outer_threshold;
  161. }
  162. void set_drop_shadow_offset(const gfx::Point& offset) {
  163. DCHECK_EQ(type_, DROP_SHADOW);
  164. drop_shadow_offset_ = offset;
  165. }
  166. void set_drop_shadow_color(SkColor4f color) {
  167. DCHECK_EQ(type_, DROP_SHADOW);
  168. drop_shadow_color_ = color;
  169. }
  170. void set_image_filter(sk_sp<PaintFilter> image_filter) {
  171. DCHECK_EQ(type_, REFERENCE);
  172. image_filter_ = std::move(image_filter);
  173. }
  174. void set_matrix(base::span<const SkScalar, 20> matrix) {
  175. DCHECK_EQ(type_, COLOR_MATRIX);
  176. for (unsigned i = 0; i < 20; ++i)
  177. matrix_[i] = matrix[i];
  178. }
  179. void set_zoom_inset(int inset) {
  180. DCHECK_EQ(type_, ZOOM);
  181. zoom_inset_ = inset;
  182. }
  183. void set_shape(const ShapeRects& shape) {
  184. DCHECK_EQ(type_, ALPHA_THRESHOLD);
  185. shape_ = shape;
  186. }
  187. void set_blur_tile_mode(SkTileMode tile_mode) {
  188. DCHECK_EQ(type_, BLUR);
  189. blur_tile_mode_ = tile_mode;
  190. }
  191. // Given two filters of the same type, returns a filter operation created by
  192. // linearly interpolating a |progress| fraction from |from| to |to|. If either
  193. // |from| or |to| (but not both) is null, it is treated as a no-op filter of
  194. // the same type as the other given filter. If both |from| and |to| are null,
  195. // or if |from| and |to| are non-null but of different types, returns a
  196. // no-op filter.
  197. static FilterOperation Blend(const FilterOperation* from,
  198. const FilterOperation* to,
  199. double progress);
  200. void AsValueInto(base::trace_event::TracedValue* value) const;
  201. // Maps "forward" to determine which pixels in a destination rect are affected
  202. // by pixels in the source rect.
  203. gfx::Rect MapRect(const gfx::Rect& rect, const SkMatrix& matrix) const;
  204. // Maps "backward" to determine which pixels in the source affect the pixels
  205. // in the destination rect.
  206. gfx::Rect MapRectReverse(const gfx::Rect& rect, const SkMatrix& matrix) const;
  207. private:
  208. FilterOperation(FilterType type, float amount);
  209. FilterOperation(FilterType type, float amount, SkTileMode tile_mode);
  210. FilterOperation(FilterType type,
  211. const gfx::Point& offset,
  212. float stdDeviation,
  213. SkColor4f color);
  214. FilterOperation(FilterType, const Matrix& matrix);
  215. FilterOperation(FilterType type, float amount, int inset);
  216. FilterOperation(FilterType type, float amount, float outer_threshold);
  217. FilterOperation(FilterType type, sk_sp<PaintFilter> image_filter);
  218. FilterOperation(FilterType type,
  219. const ShapeRects& shape,
  220. float inner_threshold,
  221. float outer_threshold);
  222. FilterType type_;
  223. float amount_;
  224. float outer_threshold_;
  225. gfx::Point drop_shadow_offset_;
  226. SkColor4f drop_shadow_color_;
  227. sk_sp<PaintFilter> image_filter_;
  228. Matrix matrix_;
  229. int zoom_inset_;
  230. // Use a collection of |gfx::Rect| to make serialization simpler.
  231. ShapeRects shape_;
  232. SkTileMode blur_tile_mode_;
  233. };
  234. } // namespace cc
  235. #endif // CC_PAINT_FILTER_OPERATION_H_