[M108 Migration][VD] Avoid pending frame counter becoming negative
[platform/framework/web/chromium-efl.git] / cc / metrics / events_metrics_manager.h
1 // Copyright 2020 The Chromium Authors
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 CC_METRICS_EVENTS_METRICS_MANAGER_H_
6 #define CC_METRICS_EVENTS_METRICS_MANAGER_H_
7
8 #include <memory>
9 #include <vector>
10
11 #include "base/callback.h"
12 #include "cc/cc_export.h"
13 #include "cc/metrics/event_metrics.h"
14
15 namespace cc {
16
17 // Manages a list of active EventMetrics objects. Each thread (main or impl) has
18 // its own instance of this class to help it determine which events have led to
19 // a frame update.
20 class CC_EXPORT EventsMetricsManager {
21  public:
22   // This interface is used to denote the scope of an event handling. The scope
23   // is started as soon as an instance is constructed and ended when the
24   // instance is destructed. EventsMetricsManager uses this to determine whether
25   // a frame update has happened due to handling of a specific event or not.
26   // Since it is possible to have nested event loops, scoped monitors can be
27   // nested.
28   class ScopedMonitor {
29    public:
30     // Type of the callback function that is called after a scoped monitor goes
31     // out of scope. `handled` specifies whether the corresponding event was
32     // handled in which case the function should return its corresponding
33     // `EventMetrics` object for reporting purposes. Since calling this callback
34     // denotes the end of event processing, clients can use this to set relevant
35     // timestamps to the metrics object, before potentially returning it.
36     using DoneCallback =
37         base::OnceCallback<std::unique_ptr<EventMetrics>(bool handled)>;
38
39     ScopedMonitor();
40     virtual ~ScopedMonitor();
41
42     virtual void SetSaveMetrics() = 0;
43
44     ScopedMonitor(const ScopedMonitor&) = delete;
45     ScopedMonitor& operator=(const ScopedMonitor&) = delete;
46   };
47
48   EventsMetricsManager();
49   ~EventsMetricsManager();
50
51   EventsMetricsManager(const EventsMetricsManager&) = delete;
52   EventsMetricsManager& operator=(const EventsMetricsManager&) = delete;
53
54   // Called by clients when they start handling an event. Destruction of the
55   // scoped monitor indicates the end of event handling which ends in calling
56   // `done_callback`. `done_callback` is allowed to be a null callback, which
57   // means the client is not interested in reporting metrics for the event being
58   // handled in this specific scope.
59   std::unique_ptr<ScopedMonitor> GetScopedMonitor(
60       ScopedMonitor::DoneCallback done_callback);
61
62   // Called by clients when a frame needs to be produced. If any scoped monitor
63   // is active at this time, its corresponding event metrics would be marked to
64   // be saved when it goes out of scope.
65   void SaveActiveEventMetrics();
66
67   // Empties the list of saved EventMetrics objects, returning them to the
68   // caller.
69   EventMetrics::List TakeSavedEventsMetrics();
70
71   size_t saved_events_metrics_count_for_testing() const {
72     return saved_events_.size();
73   }
74
75  private:
76   class ScopedMonitorImpl;
77
78   // Called when the most nested scoped monitor is destroyed. If the monitored
79   // metrics need to be saved it will be passed in as `metrics`.
80   void OnScopedMonitorEnded(std::unique_ptr<EventMetrics> metrics);
81
82   // Stack of active, potentially nested, scoped monitors.
83   std::vector<ScopedMonitorImpl*> active_scoped_monitors_;
84
85   // List of event metrics saved for reporting.
86   EventMetrics::List saved_events_;
87 };
88
89 }  // namespace cc
90
91 #endif  // CC_METRICS_EVENTS_METRICS_MANAGER_H_