File: frame_sorter.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (138 lines) | stat: -rw-r--r-- 4,840 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_METRICS_FRAME_SORTER_H_
#define CC_METRICS_FRAME_SORTER_H_

#include <stddef.h>

#include <map>
#include <optional>
#include <queue>
#include <utility>
#include <vector>

#include "base/containers/circular_deque.h"
#include "base/containers/flat_set.h"
#include "base/containers/ring_buffer.h"
#include "base/functional/callback.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "cc/cc_export.h"
#include "cc/metrics/frame_info.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"

namespace cc {

// FrameSorterObserver class notifies registered
// observers when frames are flushed by the FrameSorter.
class FrameSorterObserver : public base::CheckedObserver {
 public:
  virtual void AddSortedFrame(const viz::BeginFrameArgs&, const FrameInfo&) = 0;
};

// This class is used to process the frames in order of initiation.
// So regardless of which order frames are terminated, the frames
// sorter will be called on the frames in the order of initiation
// (e.g. frame_time of that frame).
class CC_EXPORT FrameSorter {
 public:
  class FrameState {
   public:
    void OnBegin();
    void OnAck(bool frame_is_dropped);
    void OnReset();
    bool IsComplete() const;  // Checks if all acks are received.
    bool is_dropped() const { return is_dropped_; }
    bool should_ignore() const { return should_ignore_; }

   private:
    uint16_t on_begin_counter = 0;  // Counts the number of AddNewFrame calls.
    uint16_t ack_counter = 0;       // Counts the number of acks received.
    bool is_dropped_ = false;       // Flags if any of the acks were dropped.
    bool should_ignore_ = false;    // Flags if there was a reset prior to acks.
  };

  FrameSorter();
  void AddObserver(FrameSorterObserver* frame_sorter_observer);
  void RemoveObserver(FrameSorterObserver* frame_sorter_observer);
  ~FrameSorter();

  FrameSorter(const FrameSorter&) = delete;
  FrameSorter& operator=(const FrameSorter&) = delete;

  // The frames must be added in the correct order.
  void AddNewFrame(const viz::BeginFrameArgs& args);

  // Called on all frames, including those before FirstContentfulPaint.
  void AddFrameInfoToBuffer(const FrameInfo& frame_info);

  // The results can be added in any order. However, the frame must have been
  // added by an earlier call to |AddNewFrame()|.
  virtual void AddFrameResult(const viz::BeginFrameArgs& args,
                              const FrameInfo& frame_info);

  // Check if a frame has been previously reported as dropped.
  bool IsAlreadyReportedDropped(const viz::BeginFrameId& id) const;

  // Ring buffer which keeps a state shorthand of recently finished frames.
  typedef base::RingBuffer<FrameInfo::FrameFinalState, 180> RingBufferType;
  RingBufferType::Iterator Begin() const { return ring_buffer_.Begin(); }
  // `End()` points to the last `FrameState`, not past it.
  RingBufferType::Iterator End() const { return ring_buffer_.End(); }

  // For requesting recent frame state information.
  size_t frame_history_size() const { return ring_buffer_.BufferSize(); }
  size_t total_frames() const { return total_frames_; }
  size_t total_dropped() const { return total_dropped_; }
  size_t total_partial() const { return total_partial_; }

  uint32_t GetAverageThroughput() const;

  void Reset(bool reset_fcp);

  void OnFirstContentfulPaintReceived();

  bool first_contentful_paint_received() {
    return first_contentful_paint_received_;
  }

  // Enable dropped frame report for ui::Compositor..
  void EnableReportForUI();

 private:
  void FlushFrames();
  base::TimeDelta ComputeCurrentWindowSize() const;
  void PopSlidingWindow(const viz::BeginFrameArgs& args);
  std::queue<std::pair<const viz::BeginFrameArgs, FrameInfo>> sliding_window_;

  const uint64_t kPendingFramesMaxSize = 300u;

  // The callback to run for each flushed frame.
  base::ObserverList<FrameSorterObserver> observers_;

  // Frames which are started.
  base::circular_deque<viz::BeginFrameArgs> pending_frames_;

  // State of each frame in terms of ack expectation.
  std::map<viz::BeginFrameId, FrameState> frame_states_;
  std::map<viz::BeginFrameId, FrameInfo> frame_infos_;

  // Ring buffer that stores the state of recently completed frames
  // and the associated counters.
  RingBufferType ring_buffer_;
  size_t total_frames_ = 0;
  size_t total_partial_ = 0;
  size_t total_dropped_ = 0;

  std::optional<uint64_t> current_source_id_;
  bool first_contentful_paint_received_ = false;
  bool report_for_ui_ = false;
  std::optional<double> sliding_window_current_percent_dropped_;
  uint32_t dropped_frame_count_in_window_ = 0;
};

}  // namespace cc

#endif  // CC_METRICS_FRAME_SORTER_H_