1 | // Copyright 2013 The Flutter 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 FLUTTER_FLOW_INSTRUMENTATION_H_ |
6 | #define FLUTTER_FLOW_INSTRUMENTATION_H_ |
7 | |
8 | #include <vector> |
9 | |
10 | #include "flutter/display_list/dl_canvas.h" |
11 | #include "flutter/fml/macros.h" |
12 | #include "flutter/fml/time/time_delta.h" |
13 | #include "flutter/fml/time/time_point.h" |
14 | |
15 | #include "third_party/skia/include/core/SkSurface.h" |
16 | |
17 | namespace flutter { |
18 | |
19 | class Stopwatch { |
20 | public: |
21 | /// The refresh rate interface for `Stopwatch`. |
22 | class RefreshRateUpdater { |
23 | public: |
24 | /// Time limit for a smooth frame. |
25 | /// See: `DisplayManager::GetMainDisplayRefreshRate`. |
26 | virtual fml::Milliseconds GetFrameBudget() const = 0; |
27 | }; |
28 | |
29 | /// The constructor with a updater parameter, it will update frame_budget |
30 | /// everytime when `GetFrameBudget()` is called. |
31 | explicit Stopwatch(const RefreshRateUpdater& updater); |
32 | |
33 | ~Stopwatch(); |
34 | |
35 | const fml::TimeDelta& LastLap() const; |
36 | |
37 | fml::TimeDelta MaxDelta() const; |
38 | |
39 | fml::TimeDelta AverageDelta() const; |
40 | |
41 | void InitVisualizeSurface(SkISize size) const; |
42 | |
43 | void Visualize(DlCanvas* canvas, const SkRect& rect) const; |
44 | |
45 | void Start(); |
46 | |
47 | void Stop(); |
48 | |
49 | void SetLapTime(const fml::TimeDelta& delta); |
50 | |
51 | /// All places which want to get frame_budget should call this function. |
52 | fml::Milliseconds GetFrameBudget() const; |
53 | |
54 | private: |
55 | inline double UnitFrameInterval(double time_ms) const; |
56 | inline double UnitHeight(double time_ms, double max_height) const; |
57 | |
58 | const RefreshRateUpdater& refresh_rate_updater_; |
59 | fml::TimePoint start_; |
60 | std::vector<fml::TimeDelta> laps_; |
61 | size_t current_sample_; |
62 | |
63 | // Mutable data cache for performance optimization of the graphs. Prevents |
64 | // expensive redrawing of old data. |
65 | mutable bool cache_dirty_; |
66 | mutable sk_sp<SkSurface> visualize_cache_surface_; |
67 | mutable size_t prev_drawn_sample_index_; |
68 | |
69 | FML_DISALLOW_COPY_AND_ASSIGN(Stopwatch); |
70 | }; |
71 | |
72 | /// Used for fixed refresh rate query cases. |
73 | class FixedRefreshRateUpdater : public Stopwatch::RefreshRateUpdater { |
74 | fml::Milliseconds GetFrameBudget() const override; |
75 | |
76 | public: |
77 | explicit FixedRefreshRateUpdater( |
78 | fml::Milliseconds fixed_frame_budget = fml::kDefaultFrameBudget); |
79 | |
80 | private: |
81 | fml::Milliseconds fixed_frame_budget_; |
82 | }; |
83 | |
84 | /// Used for fixed refresh rate cases. |
85 | class FixedRefreshRateStopwatch : public Stopwatch { |
86 | public: |
87 | explicit FixedRefreshRateStopwatch( |
88 | fml::Milliseconds fixed_frame_budget = fml::kDefaultFrameBudget); |
89 | |
90 | private: |
91 | FixedRefreshRateUpdater fixed_delegate_; |
92 | }; |
93 | |
94 | } // namespace flutter |
95 | |
96 | #endif // FLUTTER_FLOW_INSTRUMENTATION_H_ |
97 | |