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