ink_drop_painted_layer_delegates.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2015 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. #include "ui/views/animation/ink_drop_painted_layer_delegates.h"
  5. #include <vector>
  6. #include "cc/paint/paint_canvas.h"
  7. #include "cc/paint/paint_flags.h"
  8. #include "third_party/skia/include/core/SkDrawLooper.h"
  9. #include "third_party/skia/include/core/SkRRect.h"
  10. #include "ui/compositor/paint_recorder.h"
  11. #include "ui/gfx/canvas.h"
  12. #include "ui/gfx/color_palette.h"
  13. #include "ui/gfx/geometry/insets.h"
  14. #include "ui/gfx/geometry/point.h"
  15. #include "ui/gfx/geometry/point_conversions.h"
  16. #include "ui/gfx/geometry/rect.h"
  17. #include "ui/gfx/geometry/rect_conversions.h"
  18. #include "ui/gfx/geometry/rect_f.h"
  19. #include "ui/gfx/geometry/size_conversions.h"
  20. #include "ui/gfx/geometry/skia_conversions.h"
  21. #include "ui/gfx/skia_paint_util.h"
  22. namespace views {
  23. ////////////////////////////////////////////////////////////////////////////////
  24. //
  25. // BasePaintedLayerDelegate
  26. //
  27. BasePaintedLayerDelegate::BasePaintedLayerDelegate(SkColor color)
  28. : color_(color) {}
  29. BasePaintedLayerDelegate::~BasePaintedLayerDelegate() = default;
  30. gfx::Vector2dF BasePaintedLayerDelegate::GetCenteringOffset() const {
  31. return gfx::RectF(GetPaintedBounds()).CenterPoint().OffsetFromOrigin();
  32. }
  33. void BasePaintedLayerDelegate::OnDeviceScaleFactorChanged(
  34. float old_device_scale_factor,
  35. float new_device_scale_factor) {}
  36. ////////////////////////////////////////////////////////////////////////////////
  37. //
  38. // CircleLayerDelegate
  39. //
  40. CircleLayerDelegate::CircleLayerDelegate(SkColor color, int radius)
  41. : BasePaintedLayerDelegate(color), radius_(radius) {}
  42. CircleLayerDelegate::~CircleLayerDelegate() = default;
  43. gfx::RectF CircleLayerDelegate::GetPaintedBounds() const {
  44. const int diameter = radius_ * 2;
  45. return gfx::RectF(0, 0, diameter, diameter);
  46. }
  47. void CircleLayerDelegate::OnPaintLayer(const ui::PaintContext& context) {
  48. cc::PaintFlags flags;
  49. flags.setColor(color());
  50. flags.setAntiAlias(true);
  51. flags.setStyle(cc::PaintFlags::kFill_Style);
  52. ui::PaintRecorder recorder(context,
  53. gfx::ToEnclosingRect(GetPaintedBounds()).size());
  54. gfx::Canvas* canvas = recorder.canvas();
  55. canvas->DrawCircle(GetPaintedBounds().CenterPoint(), radius_, flags);
  56. }
  57. ////////////////////////////////////////////////////////////////////////////////
  58. //
  59. // RectangleLayerDelegate
  60. //
  61. RectangleLayerDelegate::RectangleLayerDelegate(SkColor color, gfx::SizeF size)
  62. : BasePaintedLayerDelegate(color), size_(size) {}
  63. RectangleLayerDelegate::~RectangleLayerDelegate() = default;
  64. gfx::RectF RectangleLayerDelegate::GetPaintedBounds() const {
  65. return gfx::RectF(size_);
  66. }
  67. void RectangleLayerDelegate::OnPaintLayer(const ui::PaintContext& context) {
  68. cc::PaintFlags flags;
  69. flags.setColor(color());
  70. flags.setAntiAlias(true);
  71. flags.setStyle(cc::PaintFlags::kFill_Style);
  72. ui::PaintRecorder recorder(context, gfx::ToCeiledSize(size_));
  73. gfx::Canvas* canvas = recorder.canvas();
  74. canvas->DrawRect(GetPaintedBounds(), flags);
  75. }
  76. ////////////////////////////////////////////////////////////////////////////////
  77. //
  78. // RoundedRectangleLayerDelegate
  79. //
  80. RoundedRectangleLayerDelegate::RoundedRectangleLayerDelegate(
  81. SkColor color,
  82. const gfx::SizeF& size,
  83. int corner_radius)
  84. : BasePaintedLayerDelegate(color),
  85. size_(size),
  86. corner_radius_(corner_radius) {}
  87. RoundedRectangleLayerDelegate::~RoundedRectangleLayerDelegate() = default;
  88. gfx::RectF RoundedRectangleLayerDelegate::GetPaintedBounds() const {
  89. return gfx::RectF(size_);
  90. }
  91. void RoundedRectangleLayerDelegate::OnPaintLayer(
  92. const ui::PaintContext& context) {
  93. cc::PaintFlags flags;
  94. flags.setColor(color());
  95. flags.setAntiAlias(true);
  96. flags.setStyle(cc::PaintFlags::kFill_Style);
  97. ui::PaintRecorder recorder(context, gfx::ToCeiledSize(size_));
  98. const float dsf = recorder.canvas()->UndoDeviceScaleFactor();
  99. gfx::RectF rect = GetPaintedBounds();
  100. rect.Scale(dsf);
  101. recorder.canvas()->DrawRoundRect(gfx::ToEnclosingRect(rect),
  102. dsf * corner_radius_, flags);
  103. }
  104. ////////////////////////////////////////////////////////////////////////////////
  105. //
  106. // BorderShadowLayerDelegate
  107. //
  108. BorderShadowLayerDelegate::BorderShadowLayerDelegate(
  109. const std::vector<gfx::ShadowValue>& shadows,
  110. const gfx::Rect& shadowed_area_bounds,
  111. SkColor fill_color,
  112. int corner_radius)
  113. : BasePaintedLayerDelegate(gfx::kPlaceholderColor),
  114. shadows_(shadows),
  115. bounds_(shadowed_area_bounds),
  116. fill_color_(fill_color),
  117. corner_radius_(corner_radius) {}
  118. BorderShadowLayerDelegate::~BorderShadowLayerDelegate() = default;
  119. gfx::RectF BorderShadowLayerDelegate::GetPaintedBounds() const {
  120. gfx::Rect total_rect(bounds_);
  121. total_rect.Inset(gfx::ShadowValue::GetMargin(shadows_));
  122. return gfx::RectF(total_rect);
  123. }
  124. gfx::Vector2dF BorderShadowLayerDelegate::GetCenteringOffset() const {
  125. return gfx::RectF(bounds_).CenterPoint().OffsetFromOrigin();
  126. }
  127. void BorderShadowLayerDelegate::OnPaintLayer(const ui::PaintContext& context) {
  128. cc::PaintFlags flags;
  129. flags.setStyle(cc::PaintFlags::kFill_Style);
  130. flags.setAntiAlias(true);
  131. flags.setColor(fill_color_);
  132. gfx::RectF rrect_bounds =
  133. gfx::RectF(bounds_) - GetPaintedBounds().OffsetFromOrigin();
  134. SkRRect r_rect = SkRRect::MakeRectXY(gfx::RectFToSkRect(rrect_bounds),
  135. corner_radius_, corner_radius_);
  136. // First the fill color.
  137. ui::PaintRecorder recorder(context,
  138. gfx::ToCeiledSize(GetPaintedBounds().size()));
  139. recorder.canvas()->sk_canvas()->drawRRect(r_rect, flags);
  140. // Now the shadow.
  141. flags.setLooper(gfx::CreateShadowDrawLooper(shadows_));
  142. recorder.canvas()->sk_canvas()->clipRRect(r_rect, SkClipOp::kDifference,
  143. true);
  144. recorder.canvas()->sk_canvas()->drawRRect(r_rect, flags);
  145. }
  146. } // namespace views