[go: nahoru, domu]

blob: 75216c07635eefc17aa8dbbca5613403b9ee92c1 [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
42 Graph(Baseline baseline, Fill fill, SkColor color);
43 ~Graph();
44
45 Graph(const Graph&) = delete;
46 Graph& operator=(const Graph&) = delete;
47
48 // |value| must be normalized to [0,1]. When graphs are drawn stacked,
49 // the full stack must be normalized.
50 void AddValue(float value);
51 void Layout(const gfx::Rect& graph_bounds, const Graph* base);
52 void Draw(gfx::Canvas* canvas) const;
53
54 const std::vector<SkPoint>& top_path() const { return top_path_; }
55
Alexander Alekseev5baf92b2020-08-28 21:37:1856 size_t GetDataBufferSize() const { return data_.BufferSize(); }
57
Alexander Alekseeve328c802020-03-27 03:25:4858#if !defined(NDEBUG)
59 // Returns string representation os this object for debug.
60 std::string DebugDump(const std::string& name) const;
61#endif
62
63 private:
64 const Baseline baseline_;
65 const Fill fill_;
66 const SkColor color_;
67
68 // Result of last Layout() call.
69 gfx::Rect graph_bounds_;
70
71 // Paths are measured from the top left corner.
72 // Partial graph is assumed to be right-justified.
73 // For BASELINE_BOTTOM |top_path_| has y values that are less than
74 // |bottom_path_|. (And opposite for the BASELINE_TOP.)
75 // Paths are calculated by Layout() from the |data_|.
76 std::vector<SkPoint> top_path_;
77 std::vector<SkPoint> bottom_path_;
78
79 Data data_;
80};
81
82} // namespace hud_display
83} // namespace ash
84
85#endif // ASH_HUD_DISPLAY_GRAPH_H_