paint_info.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2017 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_VIEWS_PAINT_INFO_H_
  5. #define UI_VIEWS_PAINT_INFO_H_
  6. #include "base/gtest_prod_util.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "ui/compositor/paint_context.h"
  9. #include "ui/gfx/geometry/rect.h"
  10. #include "ui/views/views_export.h"
  11. namespace views {
  12. // This class manages the context required during View::Paint(). It is
  13. // responsible for setting the paint recording size and the paint recording
  14. // scale factors for an individual View.
  15. // Each PaintInfo instance has paint recording offset relative to a root
  16. // PaintInfo.
  17. // All coordinates are in paint recording space. If pixel canvas is enabled this
  18. // essentially becomes pixel coordinate space.
  19. class VIEWS_EXPORT PaintInfo {
  20. public:
  21. enum class ScaleType {
  22. // Scale the recordings by the default device scale factor while maintaining
  23. // the aspect ratio. Use this when a view contains an image or icon that
  24. // should not get distorted due to scaling.
  25. kUniformScaling = 0,
  26. // Scale the recordings based on the device scale factor but snap to the
  27. // parent's bottom or right edge whenever possible. This may lead to minor
  28. // distortion and is not recommended to be used with views that contain
  29. // images.
  30. kScaleWithEdgeSnapping
  31. };
  32. // Instantiates a root PaintInfo. This should only be initialized at the Paint
  33. // root, ie., a layer or the root of a widget.
  34. static PaintInfo CreateRootPaintInfo(const ui::PaintContext& root_context,
  35. const gfx::Size& size);
  36. // Instantiate a child PaintInfo instance. All bounds for this object are
  37. // relative to its root PaintInfo.
  38. static PaintInfo CreateChildPaintInfo(const PaintInfo& parent_paint_info,
  39. const gfx::Rect& bounds,
  40. const gfx::Size& parent_size,
  41. ScaleType scale_type,
  42. bool is_layer,
  43. bool needs_paint = false);
  44. PaintInfo(const PaintInfo& other);
  45. ~PaintInfo();
  46. // Returns true if all paint commands are recorded at pixel size.
  47. bool IsPixelCanvas() const;
  48. // Returns true if the View should be painted based on whether per-view
  49. // invalidation is enabled or not.
  50. bool ShouldPaint() const;
  51. const ui::PaintContext& context() const {
  52. return root_context_ ? *root_context_ : context_;
  53. }
  54. gfx::Vector2d offset_from_root() const {
  55. return paint_recording_bounds_.OffsetFromOrigin();
  56. }
  57. const gfx::Vector2d& offset_from_parent() const {
  58. return offset_from_parent_;
  59. }
  60. float paint_recording_scale_x() const { return paint_recording_scale_x_; }
  61. float paint_recording_scale_y() const { return paint_recording_scale_y_; }
  62. const gfx::Size& paint_recording_size() const {
  63. return paint_recording_bounds_.size();
  64. }
  65. const gfx::Rect& paint_recording_bounds() const {
  66. return paint_recording_bounds_;
  67. }
  68. private:
  69. friend class PaintInfoTest;
  70. FRIEND_TEST_ALL_PREFIXES(PaintInfoTest, LayerPaintInfo);
  71. PaintInfo(const ui::PaintContext& root_context, const gfx::Size& size);
  72. PaintInfo(const PaintInfo& parent_paint_info,
  73. const gfx::Rect& bounds,
  74. const gfx::Size& parent_size,
  75. ScaleType scale_type,
  76. bool is_layer,
  77. bool needs_paint = false);
  78. // Scales the |child_bounds| to its recording bounds based on the
  79. // |context.device_scale_factor()|. The recording bounds are snapped to the
  80. // parent's right and/or bottom edge if required.
  81. // If pixel canvas is disabled, this function returns |child_bounds| as is.
  82. gfx::Rect GetSnappedRecordingBounds(const gfx::Size& parent_size,
  83. const gfx::Rect& child_bounds) const;
  84. // The scale at which the paint commands are recorded at. Due to the decimal
  85. // rounding and snapping to edges during the scale operation, the effective
  86. // paint recording scale may end up being slightly different between the x and
  87. // y axis.
  88. float paint_recording_scale_x_;
  89. float paint_recording_scale_y_;
  90. // Paint Recording bounds of the view. The offset is relative to the root
  91. // PaintInfo.
  92. const gfx::Rect paint_recording_bounds_;
  93. // Offset relative to the parent view's paint recording bounds. Returns 0
  94. // offset if this is the root.
  95. gfx::Vector2d offset_from_parent_;
  96. // Compositor PaintContext associated with the view this object belongs to.
  97. ui::PaintContext context_;
  98. raw_ptr<const ui::PaintContext> root_context_;
  99. // True if the individual View has been marked invalid for paint (i.e.
  100. // SchedulePaint() was invoked on the View).
  101. bool needs_paint_ = false;
  102. };
  103. } // namespace views
  104. #endif // UI_VIEWS_PAINT_INFO_H_