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 | |
| 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 Alekseev | 5baf92b | 2020-08-28 21:37:18 | [diff] [blame^] | 56 | size_t GetDataBufferSize() const { return data_.BufferSize(); } |
| 57 | |
Alexander Alekseev | e328c80 | 2020-03-27 03:25:48 | [diff] [blame] | 58 | #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_ |