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