[go: nahoru, domu]

blob: 7917f8818b208b8f9c5180edad6fd0c4ba065c20 [file] [log] [blame]
Avi Drissman3a215d1e2022-09-07 19:43:091// Copyright 2020 The Chromium Authors
Alexander Alekseeve328c802020-03-27 03:25:482// 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 Wennborg9a985272020-04-20 16:48:168#include <vector>
9
Alexander Alekseevfefc6c12020-08-19 10:48:4410#include "ash/hud_display/hud_constants.h"
Alexander Alekseeve328c802020-03-27 03:25:4811#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 Lubick1753a3d2022-03-14 18:50:4516struct SkPoint;
17
Alexander Alekseeve328c802020-03-27 03:25:4818namespace gfx {
19class Canvas;
20}
21
22namespace ash {
23namespace hud_display {
24
25class Graph {
26 public:
Peter Kasting2f8ba3202024-01-29 17:39:1027 // Graph screen size (that is used during layout) should match (ring buffer
Alexander Alekseeve328c802020-03-27 03:25:4828 // size - 1) to prevent scaling, because RingBuffer always keeps one element
29 // unused.
Avery Musbachf67288f92021-07-22 22:02:2930 using Data = base::RingBuffer<float, kHUDGraphWidth + 1>;
Alexander Alekseeve328c802020-03-27 03:25:4831
32 enum class Baseline {
Jitesh Pareek22c94342023-09-01 14:55:0533 kBaselineBottom, // Positive values will be drawn from the bottom border
Alexander Alekseeve328c802020-03-27 03:25:4834 // up.
Jitesh Pareek22c94342023-09-01 14:55:0535 kBaselineTop, // Positive values will be drawn from the top border down.
Alexander Alekseeve328c802020-03-27 03:25:4836 };
37
38 // Whether to draw the graph as a filled polygon.
39 enum class Fill {
Jitesh Pareek22c94342023-09-01 14:55:0540 kNone,
41 kSolid,
Alexander Alekseeve328c802020-03-27 03:25:4842 };
43
Alexander Alekseev666011d62020-10-26 20:28:5044 enum class Style {
Jitesh Pareek22c94342023-09-01 14:55:0545 kLines,
46 kSkyline,
Alexander Alekseev666011d62020-10-26 20:28:5047 };
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 Alekseeve328c802020-03-27 03:25:4855 ~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 Alekseev0fbea5c2020-09-29 05:05:4762 // |unscaled_value| is used to label graph values to the user.
63 void AddValue(float value, float unscaled_value);
Alexander Alekseeve328c802020-03-27 03:25:4864 void Layout(const gfx::Rect& graph_bounds, const Graph* base);
65 void Draw(gfx::Canvas* canvas) const;
Alexander Alekseev666011d62020-10-26 20:28:5066 void UpdateLastValue(float value, float unscaled_value);
Alexander Alekseeve328c802020-03-27 03:25:4867
68 const std::vector<SkPoint>& top_path() const { return top_path_; }
69
Alexander Alekseev666011d62020-10-26 20:28:5070 // Returns number of data points displayed on the graph.
71 size_t max_data_points() const { return max_data_points_; }
Alexander Alekseev5baf92b2020-08-28 21:37:1872
Alexander Alekseev7d66f342020-09-03 15:57:1773 SkColor color() const { return color_; }
74
Alexander Alekseev0fbea5c2020-09-29 05:05:4775 // 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 Oshima33133e552020-11-05 20:13:2287 // Reset the data.
88 void Reset();
89
Alexander Alekseeve328c802020-03-27 03:25:4890#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 Alekseev666011d62020-10-26 20:28:5098 const Style style_;
Alexander Alekseeve328c802020-03-27 03:25:4899 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 Pareek22c94342023-09-01 14:55:05106 // For kBaselineBottom |top_path_| has y values that are less than
107 // |bottom_path_|. (And opposite for the kBaselineTop.)
Alexander Alekseeve328c802020-03-27 03:25:48108 // Paths are calculated by Layout() from the |data_|.
109 std::vector<SkPoint> top_path_;
110 std::vector<SkPoint> bottom_path_;
Alexander Alekseev666011d62020-10-26 20:28:50111 // Bottom path style should follow base graph style.
Jitesh Pareek22c94342023-09-01 14:55:05112 Style bottom_path_style_ = Style::kLines;
Alexander Alekseeve328c802020-03-27 03:25:48113
114 Data data_;
Alexander Alekseev0fbea5c2020-09-29 05:05:47115 Data unscaled_data_;
Alexander Alekseev666011d62020-10-26 20:28:50116 size_t max_data_points_ = 0;
Alexander Alekseeve328c802020-03-27 03:25:48117};
118
119} // namespace hud_display
120} // namespace ash
121
122#endif // ASH_HUD_DISPLAY_GRAPH_H_