[go: nahoru, domu]

blob: 39538d084678d0273730a550c0973c635505500c [file] [log] [blame]
Alexander Alekseeve328c802020-03-27 03:25:481// 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 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
16namespace gfx {
17class Canvas;
18}
19
20namespace ash {
21namespace hud_display {
22
23class Graph {
24 public:
Alexander Alekseevfefc6c12020-08-19 10:48:4425 // Graph screen size (that is used in Layout()) should match (ring buffer
Alexander Alekseeve328c802020-03-27 03:25:4826 // size - 1) to prevent scaling, because RingBuffer always keeps one element
27 // unused.
Alexander Alekseevfefc6c12020-08-19 10:48:4428 using Data = base::RingBuffer<float, kDefaultGraphWidth + 1>;
Alexander Alekseeve328c802020-03-27 03:25:4829
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 Alekseev666011d62020-10-26 20:28:5042 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 Alekseeve328c802020-03-27 03:25:4853 ~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 Alekseev0fbea5c2020-09-29 05:05:4760 // |unscaled_value| is used to label graph values to the user.
61 void AddValue(float value, float unscaled_value);
Alexander Alekseeve328c802020-03-27 03:25:4862 void Layout(const gfx::Rect& graph_bounds, const Graph* base);
63 void Draw(gfx::Canvas* canvas) const;
Alexander Alekseev666011d62020-10-26 20:28:5064 void UpdateLastValue(float value, float unscaled_value);
Alexander Alekseeve328c802020-03-27 03:25:4865
66 const std::vector<SkPoint>& top_path() const { return top_path_; }
67
Alexander Alekseev666011d62020-10-26 20:28:5068 // Returns number of data points displayed on the graph.
69 size_t max_data_points() const { return max_data_points_; }
Alexander Alekseev5baf92b2020-08-28 21:37:1870
Alexander Alekseev7d66f342020-09-03 15:57:1771 SkColor color() const { return color_; }
72
Alexander Alekseev0fbea5c2020-09-29 05:05:4773 // 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 Oshima33133e552020-11-05 20:13:2285 // Reset the data.
86 void Reset();
87
Alexander Alekseeve328c802020-03-27 03:25:4888#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 Alekseev666011d62020-10-26 20:28:5096 const Style style_;
Alexander Alekseeve328c802020-03-27 03:25:4897 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 Alekseev666011d62020-10-26 20:28:50109 // Bottom path style should follow base graph style.
110 Style bottom_path_style_ = Style::LINES;
Alexander Alekseeve328c802020-03-27 03:25:48111
112 Data data_;
Alexander Alekseev0fbea5c2020-09-29 05:05:47113 Data unscaled_data_;
Alexander Alekseev666011d62020-10-26 20:28:50114 size_t max_data_points_ = 0;
Alexander Alekseeve328c802020-03-27 03:25:48115};
116
117} // namespace hud_display
118} // namespace ash
119
120#endif // ASH_HUD_DISPLAY_GRAPH_H_