graph.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 ASH_HUD_DISPLAY_GRAPH_H_
  5. #define ASH_HUD_DISPLAY_GRAPH_H_
  6. #include <vector>
  7. #include "ash/hud_display/hud_constants.h"
  8. #include "base/containers/ring_buffer.h"
  9. #include "third_party/skia/include/core/SkColor.h"
  10. #include "ui/gfx/geometry/point.h"
  11. #include "ui/gfx/geometry/rect.h"
  12. struct SkPoint;
  13. namespace gfx {
  14. class Canvas;
  15. }
  16. namespace ash {
  17. namespace hud_display {
  18. class Graph {
  19. public:
  20. // Graph screen size (that is used in Layout()) should match (ring buffer
  21. // size - 1) to prevent scaling, because RingBuffer always keeps one element
  22. // unused.
  23. using Data = base::RingBuffer<float, kHUDGraphWidth + 1>;
  24. enum class Baseline {
  25. BASELINE_BOTTOM, // Positive values will be drawn from the bottom border
  26. // up.
  27. BASELINE_TOP, // Positive values will be drawn from the top border down.
  28. };
  29. // Whether to draw the graph as a filled polygon.
  30. enum class Fill {
  31. NONE,
  32. SOLID,
  33. };
  34. enum class Style {
  35. LINES,
  36. SKYLINE,
  37. };
  38. // |max_data_points| must be less than the ring buffer size.
  39. Graph(size_t max_data_points,
  40. Baseline baseline,
  41. Fill fill,
  42. Style style,
  43. SkColor color);
  44. ~Graph();
  45. Graph(const Graph&) = delete;
  46. Graph& operator=(const Graph&) = delete;
  47. // |value| must be normalized to [0,1]. When graphs are drawn stacked,
  48. // the full stack must be normalized.
  49. // |unscaled_value| is used to label graph values to the user.
  50. void AddValue(float value, float unscaled_value);
  51. void Layout(const gfx::Rect& graph_bounds, const Graph* base);
  52. void Draw(gfx::Canvas* canvas) const;
  53. void UpdateLastValue(float value, float unscaled_value);
  54. const std::vector<SkPoint>& top_path() const { return top_path_; }
  55. // Returns number of data points displayed on the graph.
  56. size_t max_data_points() const { return max_data_points_; }
  57. SkColor color() const { return color_; }
  58. // Returns value from |unscaled_data_|.
  59. // |index| is always interpreted as "negative", i.e. "0" - current data, "1"
  60. // - previous graph data, 2 - two steps "ago". I.e. it's number of graph
  61. // points from the right graph edge.
  62. float GetUnscaledValueAt(size_t index) const;
  63. // Returns true if |data_| is populated at the given index.
  64. // |index| is always interpreted as "negative", i.e. "0" - current data, "1"
  65. // - previous graph data, 2 - two steps ago. I.e. it's number of graph
  66. // points from the right graph edge.
  67. bool IsFilledIndex(size_t index) const;
  68. // Reset the data.
  69. void Reset();
  70. #if !defined(NDEBUG)
  71. // Returns string representation os this object for debug.
  72. std::string DebugDump(const std::string& name) const;
  73. #endif
  74. private:
  75. const Baseline baseline_;
  76. const Fill fill_;
  77. const Style style_;
  78. const SkColor color_;
  79. // Result of last Layout() call.
  80. gfx::Rect graph_bounds_;
  81. // Paths are measured from the top left corner.
  82. // Partial graph is assumed to be right-justified.
  83. // For BASELINE_BOTTOM |top_path_| has y values that are less than
  84. // |bottom_path_|. (And opposite for the BASELINE_TOP.)
  85. // Paths are calculated by Layout() from the |data_|.
  86. std::vector<SkPoint> top_path_;
  87. std::vector<SkPoint> bottom_path_;
  88. // Bottom path style should follow base graph style.
  89. Style bottom_path_style_ = Style::LINES;
  90. Data data_;
  91. Data unscaled_data_;
  92. size_t max_data_points_ = 0;
  93. };
  94. } // namespace hud_display
  95. } // namespace ash
  96. #endif // ASH_HUD_DISPLAY_GRAPH_H_