Flutter Engine
 
Loading...
Searching...
No Matches
stopwatch.h
Go to the documentation of this file.
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_STOPWATCH_H_
6#define FLUTTER_FLOW_STOPWATCH_H_
7
8#include <vector>
9
11#include "flutter/fml/macros.h"
14
15namespace flutter {
16
17class Stopwatch {
18 public:
19 // The number of samples that will be accumulated for performance monitoring
20 static const size_t kMaxSamples = 120;
21
22 /// The refresh rate interface for `Stopwatch`.
24 public:
25 /// Time limit for a smooth frame.
26 /// See: `DisplayManager::GetMainDisplayRefreshRate`.
27 virtual fml::Milliseconds GetFrameBudget() const = 0;
28 };
29
30 /// The constructor with a updater parameter, it will update frame_budget
31 /// everytime when `GetFrameBudget()` is called.
32 explicit Stopwatch(const RefreshRateUpdater& updater);
33
35
36 const fml::TimeDelta& GetLap(size_t index) const;
37
38 /// Return a reference to all the laps.
39 size_t GetLapsCount() const;
40
41 size_t GetCurrentSample() const;
42
43 const fml::TimeDelta& LastLap() const;
44
46
48
49 void Start();
50
51 void Stop();
52
53 void SetLapTime(const fml::TimeDelta& delta);
54
55 /// All places which want to get frame_budget should call this function.
57
58 private:
59 const RefreshRateUpdater& refresh_rate_updater_;
60 fml::TimePoint start_;
61 std::vector<fml::TimeDelta> laps_;
62 size_t current_sample_ = kMaxSamples - 1u;
63
65};
66
67/// Used for fixed refresh rate query cases.
69 fml::Milliseconds GetFrameBudget() const override;
70
71 public:
73 fml::Milliseconds fixed_frame_budget = fml::kDefaultFrameBudget);
74
75 private:
76 fml::Milliseconds fixed_frame_budget_;
77};
78
79/// Used for fixed refresh rate cases.
81 public:
83 fml::Milliseconds fixed_frame_budget = fml::kDefaultFrameBudget);
84
85 private:
86 FixedRefreshRateUpdater fixed_delegate_;
87};
88
89//------------------------------------------------------------------------------
90/// @brief Abstract class for visualizing (i.e. drawing) a stopwatch.
91///
92/// @note This was originally folded into the |Stopwatch| class, but
93/// was separated out to make it easier to change the underlying
94/// implementation (which relied directly on Skia, not showing on
95/// Impeller: https://github.com/flutter/flutter/issues/126009).
97 public:
98 explicit StopwatchVisualizer(const Stopwatch& stopwatch)
99 : stopwatch_(stopwatch) {
100 // Looking up the frame budget from the stopwatch delegate class may call
101 // into JNI or make platform calls which are slow. This value is safe to
102 // cache since the StopwatchVisualizer is recreated on each frame.
104 }
105
106 virtual ~StopwatchVisualizer() = default;
107
108 /// @brief Renders the stopwatch as a graph.
109 ///
110 /// @param canvas The canvas to draw on.
111 /// @param[in] rect The rectangle to draw in.
112 virtual void Visualize(DlCanvas* canvas, const DlRect& rect) const = 0;
113
115
116 protected:
117 /// @brief Converts a raster time to a unit interval.
118 double UnitFrameInterval(double time_ms) const;
119
120 /// @brief Converts a raster time to a unit height.
121 double UnitHeight(double time_ms, double max_height) const;
122
124
127};
128
129} // namespace flutter
130
131#endif // FLUTTER_FLOW_STOPWATCH_H_
Developer-facing API for rendering anything within the engine.
Definition dl_canvas.h:32
Used for fixed refresh rate cases.
Definition stopwatch.h:80
Used for fixed refresh rate query cases.
Definition stopwatch.h:68
The refresh rate interface for Stopwatch.
Definition stopwatch.h:23
virtual fml::Milliseconds GetFrameBudget() const =0
const fml::TimeDelta & GetLap(size_t index) const
Definition stopwatch.cc:42
const fml::TimeDelta & LastLap() const
Definition stopwatch.cc:38
fml::Milliseconds GetFrameBudget() const
All places which want to get frame_budget should call this function.
Definition stopwatch.cc:85
fml::TimeDelta MaxDelta() const
Definition stopwatch.cc:67
size_t GetLapsCount() const
Return a reference to all the laps.
Definition stopwatch.cc:46
size_t GetCurrentSample() const
Definition stopwatch.cc:50
void SetLapTime(const fml::TimeDelta &delta)
Definition stopwatch.cc:33
static const size_t kMaxSamples
Definition stopwatch.h:20
fml::TimeDelta AverageDelta() const
Definition stopwatch.cc:77
Abstract class for visualizing (i.e. drawing) a stopwatch.
Definition stopwatch.h:96
double UnitHeight(double time_ms, double max_height) const
Converts a raster time to a unit height.
Definition stopwatch.cc:58
const Stopwatch & stopwatch_
Definition stopwatch.h:125
fml::Milliseconds GetFrameBudget() const
Definition stopwatch.h:123
fml::Milliseconds frame_budget_
Definition stopwatch.h:126
double UnitFrameInterval(double time_ms) const
Converts a raster time to a unit interval.
Definition stopwatch.cc:54
virtual ~StopwatchVisualizer()=default
StopwatchVisualizer(const Stopwatch &stopwatch)
Definition stopwatch.h:98
virtual void Visualize(DlCanvas *canvas, const DlRect &rect) const =0
Renders the stopwatch as a graph.
FML_DISALLOW_COPY_AND_ASSIGN(StopwatchVisualizer)
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
std::chrono::duration< double, std::milli > Milliseconds
Definition time_delta.h:18
constexpr Milliseconds kDefaultFrameBudget
Definition time_delta.h:21