mask_filter_info.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2020 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 UI_GFX_GEOMETRY_MASK_FILTER_INFO_H_
  5. #define UI_GFX_GEOMETRY_MASK_FILTER_INFO_H_
  6. #include "third_party/abseil-cpp/absl/types/optional.h"
  7. #include "ui/gfx/geometry/geometry_skia_export.h"
  8. #include "ui/gfx/geometry/linear_gradient.h"
  9. #include "ui/gfx/geometry/rect_f.h"
  10. #include "ui/gfx/geometry/rrect_f.h"
  11. namespace gfx {
  12. class Transform;
  13. // This class defines a mask filter to be applied to the given rect.
  14. class GEOMETRY_SKIA_EXPORT MaskFilterInfo {
  15. public:
  16. MaskFilterInfo() = default;
  17. explicit MaskFilterInfo(const RRectF& rrect)
  18. : rounded_corner_bounds_(rrect) {}
  19. MaskFilterInfo(const RRectF& rrect, const gfx::LinearGradient& gradient_mask)
  20. : rounded_corner_bounds_(rrect), gradient_mask_(gradient_mask) {}
  21. MaskFilterInfo(const RectF& bounds,
  22. const RoundedCornersF& radii,
  23. const gfx::LinearGradient& gradient_mask)
  24. : rounded_corner_bounds_(bounds, radii), gradient_mask_(gradient_mask) {}
  25. MaskFilterInfo(const MaskFilterInfo& copy) = default;
  26. ~MaskFilterInfo() = default;
  27. // The bounds the filter will be applied to.
  28. RectF bounds() const { return rounded_corner_bounds_.rect(); }
  29. // Defines the rounded corner bounds to clip.
  30. const RRectF& rounded_corner_bounds() const { return rounded_corner_bounds_; }
  31. // True if this contains a rounded corner mask.
  32. bool HasRoundedCorners() const {
  33. return rounded_corner_bounds_.GetType() != RRectF::Type::kEmpty &&
  34. rounded_corner_bounds_.GetType() != RRectF::Type::kRect;
  35. }
  36. const absl::optional<gfx::LinearGradient>& gradient_mask() const {
  37. return gradient_mask_;
  38. }
  39. // True if this contains an effective gradient mask (requires filter bounds).
  40. bool HasGradientMask() const {
  41. if (rounded_corner_bounds_.IsEmpty())
  42. return false;
  43. return gradient_mask_ && !gradient_mask_->IsEmpty();
  44. }
  45. // True if this contains no effective mask information.
  46. bool IsEmpty() const { return rounded_corner_bounds_.IsEmpty(); }
  47. // Transform the mask information. Returns false if the transform
  48. // cannot be applied.
  49. bool Transform(const gfx::Transform& transform);
  50. std::string ToString() const;
  51. private:
  52. // The rounded corner bounds. This also defines the bounds that the mask
  53. // filter will be applied to.
  54. RRectF rounded_corner_bounds_;
  55. // Shader based linear gradient mask to be applied to a layer.
  56. absl::optional<gfx::LinearGradient> gradient_mask_;
  57. };
  58. inline bool operator==(const MaskFilterInfo& lhs, const MaskFilterInfo& rhs) {
  59. return (lhs.rounded_corner_bounds() == rhs.rounded_corner_bounds()) &&
  60. (lhs.gradient_mask() == rhs.gradient_mask());
  61. }
  62. inline bool operator!=(const MaskFilterInfo& lhs, const MaskFilterInfo& rhs) {
  63. return !(lhs == rhs);
  64. }
  65. } // namespace gfx
  66. #endif // UI_GFX_GEOMETRY_MASK_FILTER_INFO_H_