Avi Drissman | 3a215d1e | 2022-09-07 19:43:09 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef ASH_HUD_DISPLAY_GRAPH_H_ |
| 6 | #define ASH_HUD_DISPLAY_GRAPH_H_ |
| 7 | |
Hans Wennborg | 9a98527 | 2020-04-20 16:48:16 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
Alexander Alekseev | fefc6c1 | 2020-08-19 10:48:44 | [diff] [blame] | 10 | #include "ash/hud_display/hud_constants.h" |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 11 | #include "base/containers/ring_buffer.h" |
| 12 | #include "third_party/skia/include/core/SkColor.h" |
| 13 | #include "ui/gfx/geometry/point.h" |
| 14 | #include "ui/gfx/geometry/rect.h" |
| 15 | |
Kevin Lubick | 1753a3d | 2022-03-14 18:50:45 | [diff] [blame] | 16 | struct SkPoint; |
| 17 | |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 18 | namespace gfx { |
| 19 | class Canvas; |
| 20 | } |
| 21 | |
| 22 | namespace ash { |
| 23 | namespace hud_display { |
| 24 | |
| 25 | class Graph { |
| 26 | public: |
Peter Kasting | 2f8ba320 | 2024-01-29 17:39:10 | [diff] [blame] | 27 | // Graph screen size (that is used during layout) should match (ring buffer |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 28 | // size - 1) to prevent scaling, because RingBuffer always keeps one element |
| 29 | // unused. |
Avery Musbach | f67288f9 | 2021-07-22 22:02:29 | [diff] [blame] | 30 | using Data = base::RingBuffer<float, kHUDGraphWidth + 1>; |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 31 | |
| 32 | enum class Baseline { |
Jitesh Pareek | 22c9434 | 2023-09-01 14:55:05 | [diff] [blame] | 33 | kBaselineBottom, // Positive values will be drawn from the bottom border |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 34 | // up. |
Jitesh Pareek | 22c9434 | 2023-09-01 14:55:05 | [diff] [blame] | 35 | kBaselineTop, // Positive values will be drawn from the top border down. |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | // Whether to draw the graph as a filled polygon. |
| 39 | enum class Fill { |
Jitesh Pareek | 22c9434 | 2023-09-01 14:55:05 | [diff] [blame] | 40 | kNone, |
| 41 | kSolid, |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 42 | }; |
| 43 | |
Alexander Alekseev | 666011d6 | 2020-10-26 20:28:50 | [diff] [blame] | 44 | enum class Style { |
Jitesh Pareek | 22c9434 | 2023-09-01 14:55:05 | [diff] [blame] | 45 | kLines, |
| 46 | kSkyline, |
Alexander Alekseev | 666011d6 | 2020-10-26 20:28:50 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | // |max_data_points| must be less than the ring buffer size. |
| 50 | Graph(size_t max_data_points, |
| 51 | Baseline baseline, |
| 52 | Fill fill, |
| 53 | Style style, |
| 54 | SkColor color); |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 55 | ~Graph(); |
| 56 | |
| 57 | Graph(const Graph&) = delete; |
| 58 | Graph& operator=(const Graph&) = delete; |
| 59 | |
| 60 | // |value| must be normalized to [0,1]. When graphs are drawn stacked, |
| 61 | // the full stack must be normalized. |
Alexander Alekseev | 0fbea5c | 2020-09-29 05:05:47 | [diff] [blame] | 62 | // |unscaled_value| is used to label graph values to the user. |
| 63 | void AddValue(float value, float unscaled_value); |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 64 | void Layout(const gfx::Rect& graph_bounds, const Graph* base); |
| 65 | void Draw(gfx::Canvas* canvas) const; |
Alexander Alekseev | 666011d6 | 2020-10-26 20:28:50 | [diff] [blame] | 66 | void UpdateLastValue(float value, float unscaled_value); |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 67 | |
| 68 | const std::vector<SkPoint>& top_path() const { return top_path_; } |
| 69 | |
Alexander Alekseev | 666011d6 | 2020-10-26 20:28:50 | [diff] [blame] | 70 | // Returns number of data points displayed on the graph. |
| 71 | size_t max_data_points() const { return max_data_points_; } |
Alexander Alekseev | 5baf92b | 2020-08-28 21:37:18 | [diff] [blame] | 72 | |
Alexander Alekseev | 7d66f34 | 2020-09-03 15:57:17 | [diff] [blame] | 73 | SkColor color() const { return color_; } |
| 74 | |
Alexander Alekseev | 0fbea5c | 2020-09-29 05:05:47 | [diff] [blame] | 75 | // Returns value from |unscaled_data_|. |
| 76 | // |index| is always interpreted as "negative", i.e. "0" - current data, "1" |
| 77 | // - previous graph data, 2 - two steps "ago". I.e. it's number of graph |
| 78 | // points from the right graph edge. |
| 79 | float GetUnscaledValueAt(size_t index) const; |
| 80 | |
| 81 | // Returns true if |data_| is populated at the given index. |
| 82 | // |index| is always interpreted as "negative", i.e. "0" - current data, "1" |
| 83 | // - previous graph data, 2 - two steps ago. I.e. it's number of graph |
| 84 | // points from the right graph edge. |
| 85 | bool IsFilledIndex(size_t index) const; |
| 86 | |
Mitsuru Oshima | 33133e55 | 2020-11-05 20:13:22 | [diff] [blame] | 87 | // Reset the data. |
| 88 | void Reset(); |
| 89 | |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 90 | #if !defined(NDEBUG) |
| 91 | // Returns string representation os this object for debug. |
| 92 | std::string DebugDump(const std::string& name) const; |
| 93 | #endif |
| 94 | |
| 95 | private: |
| 96 | const Baseline baseline_; |
| 97 | const Fill fill_; |
Alexander Alekseev | 666011d6 | 2020-10-26 20:28:50 | [diff] [blame] | 98 | const Style style_; |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 99 | const SkColor color_; |
| 100 | |
| 101 | // Result of last Layout() call. |
| 102 | gfx::Rect graph_bounds_; |
| 103 | |
| 104 | // Paths are measured from the top left corner. |
| 105 | // Partial graph is assumed to be right-justified. |
Jitesh Pareek | 22c9434 | 2023-09-01 14:55:05 | [diff] [blame] | 106 | // For kBaselineBottom |top_path_| has y values that are less than |
| 107 | // |bottom_path_|. (And opposite for the kBaselineTop.) |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 108 | // Paths are calculated by Layout() from the |data_|. |
| 109 | std::vector<SkPoint> top_path_; |
| 110 | std::vector<SkPoint> bottom_path_; |
Alexander Alekseev | 666011d6 | 2020-10-26 20:28:50 | [diff] [blame] | 111 | // Bottom path style should follow base graph style. |
Jitesh Pareek | 22c9434 | 2023-09-01 14:55:05 | [diff] [blame] | 112 | Style bottom_path_style_ = Style::kLines; |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 113 | |
| 114 | Data data_; |
Alexander Alekseev | 0fbea5c | 2020-09-29 05:05:47 | [diff] [blame] | 115 | Data unscaled_data_; |
Alexander Alekseev | 666011d6 | 2020-10-26 20:28:50 | [diff] [blame] | 116 | size_t max_data_points_ = 0; |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | } // namespace hud_display |
| 120 | } // namespace ash |
| 121 | |
| 122 | #endif // ASH_HUD_DISPLAY_GRAPH_H_ |