File: wayland_frame_manager.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (295 lines) | stat: -rw-r--r-- 12,024 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_OZONE_PLATFORM_WAYLAND_HOST_WAYLAND_FRAME_MANAGER_H_
#define UI_OZONE_PLATFORM_WAYLAND_HOST_WAYLAND_FRAME_MANAGER_H_

#include <cstdint>
#include <memory>
#include <vector>

#include "base/containers/circular_deque.h"
#include "base/containers/flat_map.h"
#include "base/files/scoped_file.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "ui/gfx/frame_data.h"
#include "ui/gfx/gpu_fence_handle.h"
#include "ui/gfx/presentation_feedback.h"
#include "ui/ozone/platform/wayland/common/wayland_object.h"
#include "ui/ozone/platform/wayland/common/wayland_overlay_config.h"
#include "ui/ozone/platform/wayland/common/wayland_presentation_info.h"

namespace ui {

class WaylandBufferHandle;
class WaylandConnection;
class WaylandWindow;
class WaylandSurface;
class WaylandSubsurface;

// Representation of a graphical frame update (DrawingFrame), stores the
// configuration information required to present this frame across Wayland. It
// also has information about when/whether resources of the frame has been
// presented and released.
struct WaylandFrame {
 public:
  // A frame originated from gpu process, and hence, requires acknowledgements.
  WaylandFrame(uint32_t frame_id,
               const gfx::FrameData& data,
               WaylandSurface* root_surface,
               wl::WaylandOverlayConfig root_config,
               base::circular_deque<
                   std::pair<WaylandSubsurface*, wl::WaylandOverlayConfig>>
                   subsurfaces_to_overlays = {});

  // A frame that does not require acknowledgements.
  WaylandFrame(WaylandSurface* root_surface,
               wl::WaylandOverlayConfig root_config,
               base::circular_deque<
                   std::pair<WaylandSubsurface*, wl::WaylandOverlayConfig>>
                   subsurfaces_to_overlays = {});

  WaylandFrame() = delete;
  WaylandFrame(const WaylandFrame&) = delete;
  WaylandFrame& operator=(const WaylandFrame&) = delete;
  ~WaylandFrame();

 private:
  friend class WaylandFrameManager;
  friend class WaylandFrameManagerTest;

  uint32_t frame_id;
  raw_ptr<WaylandSurface, DanglingUntriaged> root_surface;
  wl::WaylandOverlayConfig root_config;
  base::circular_deque<std::pair<WaylandSubsurface*, wl::WaylandOverlayConfig>>
      subsurfaces_to_overlays;

  base::flat_map<WaylandSurface*, raw_ptr<WaylandBufferHandle, CtnExperimental>>
      submitted_buffers;

  // An indicator that there are buffers destrotyed before frame playback. This
  // frame should be skipped.
  bool buffer_lost = false;

  // A Wayland callback, which is triggered once wl_buffer has been committed
  // and it is the right time to notify the GPU that it can start a new drawing
  // operation.
  wl::Object<wl_callback> wl_frame_callback;

  // Merged release fence fd. This is taken as the union of all release fences
  // for this frame.
  base::ScopedFD merged_release_fence_fd;
  // Whether this frame has had OnSubmission sent for it.
  bool submission_acked;
  // Whether OnSubmission for this frame should be sent with
  // SWAP_NAK_RECREATE_BUFFERS. This is used if there was a failure in
  // submitting this frame which shouldn't result in a GPU process restart.
  bool swap_result_recreate_buffers = false;

  // The wayland object identifying this feedback.
  wl::Object<struct wp_presentation_feedback> pending_feedback;
  // The actual presentation feedback. May be missing if the callback from the
  // Wayland server has not arrived yet.
  std::optional<gfx::PresentationFeedback> feedback = std::nullopt;
  // Whether this frame has had OnPresentation sent for it.
  bool presentation_acked;

  // The sequence ID for this frame. This is used to know when the proper
  // buffers associated with a configure arrive.
  [[maybe_unused]] int64_t seq = -1;

  // Trace ID for tracking submission of the current frame.
  int64_t trace_id = -1;
};

// This is the frame update manager that configures graphical window/surface
// state and submits buffer swaps to a window. Meanwhile it keeps track of the
// pending/submitted swaps in order to send the acknowledgements back to gpu
// process.
class WaylandFrameManager {
 public:
  WaylandFrameManager(WaylandWindow* window, WaylandConnection* connection);
  WaylandFrameManager(const WaylandFrameManager&) = delete;
  WaylandFrameManager& operator=(const WaylandFrameManager&) = delete;
  ~WaylandFrameManager();

  // WaylandWindow::CommitOverlays() calls this to put a pending frame in,
  // without making any actual Wayland protocol requests to the compositor that
  // will change the visual properties of the window.
  // A pending frame will be played back in PlayBackFrame() if the requirements
  // for submitting it are met.
  void RecordFrame(std::unique_ptr<WaylandFrame> frame);

  // Verifies if the first pending_frame can be played back. Requirements are:
  //   1) Previous frame's wl_frame_callback must be ack'ed;
  //   2) Window has been configured;
  //   3) All required wl_buffers are ready.
  // Does the playback if all requirements are met.
  void MaybeProcessPendingFrame();

  // Clears the state of the |frame_manager_| when the GPU channel is destroyed.
  void ClearStates();

  // Similar to ClearStates(), but does not clear submitted frames.
  void Hide();

  void SetVideoCapture();
  void ReleaseVideoCapture();

  void OnWindowSuspensionChanged();
  void FrameCallbackTimeout();

  static base::TimeDelta GetPresentationFlushTimerDurationForTesting();

 private:
  friend class WaylandFrameManagerTest;

  void PlayBackFrame(std::unique_ptr<WaylandFrame> frame);
  void DiscardFrame(std::unique_ptr<WaylandFrame> frame);

  void OnVideoCaptureUpdate();

  // Checks if ACKs for swaps should be sent immediately instead of sending
  // frames to wayland.
  // This is done when window is SUSPENDED during video capture to ensure the
  // video capture still works as compositors may throttle occluded windows.
  void EvaluateShouldAckSwapWithoutCommit();

  // Checks if frames should be sent without setting frame callbacks. This is
  // done when window is not focused during video capture as a fallback to
  // ensure video capture works in case the compositor stops sending frame
  // callbacks and doesn't support SUSPENDED state yet or SUSPENDED state is
  // sent after a delay [1].
  // [1] https://gitlab.gnome.org/GNOME/mutter/-/issues/3663
  void EvaluateShouldSkipFrameCallbacks();

  // Configures |surface| but does not commit wl_surface states yet.
  // Returns whether or not changes require a commit to the wl_surface, or
  // std::nullopt if there was a failure in configuring the surface.
  std::optional<bool> ApplySurfaceConfigure(WaylandFrame* frame,
                                            WaylandSurface* surface,
                                            wl::WaylandOverlayConfig& config,
                                            bool needs_opaque_region);

  void MaybeProcessSubmittedFrames();
  void SetFakeFeedback(WaylandFrame* frame);
  void ProcessOldSubmittedFrame(WaylandFrame* frame);

  // Gets presentation feedback information ready to be sent for submitted
  // frames. Also updates `presentation_acked` of corresponding frames to true.
  std::vector<wl::WaylandPresentationInfo> GetReadyPresentations();
  bool HaveReadyPresentations() const;

  // Clears submitted frames that are fully released and have already sent
  // presentation feedback info.
  void ClearProcessedSubmittedFrames();

  void OnExplicitBufferRelease(WaylandSurface* surface,
                               wl_buffer* wl_buffer,
                               base::ScopedFD fence);
  void OnWlBufferRelease(WaylandSurface* surface, wl_buffer* wl_buffer);

  // wl_callback_listener callbacks:
  static void OnFrameDone(void* data, wl_callback* callback, uint32_t time);

  void HandleFrameCallback(wl_callback* callback);

  // wp_presentation_feedback_listener callbacks:
  static void OnSyncOutput(
      void* data,
      struct wp_presentation_feedback* presentation_feedback,
      wl_output* output);
  static void OnPresented(
      void* data,
      struct wp_presentation_feedback* presentation_feedback,
      uint32_t tv_sec_hi,
      uint32_t tv_sec_lo,
      uint32_t tv_nsec,
      uint32_t refresh,
      uint32_t seq_hi,
      uint32_t seq_lo,
      uint32_t flags);
  static void OnDiscarded(
      void* data,
      struct wp_presentation_feedback* presentation_feedback);

  void HandlePresentationFeedback(
      struct wp_presentation_feedback* presentation_feedback,
      const gfx::PresentationFeedback& feedback,
      bool discarded = false);

  // Verifies the number of submitted frames and discards pending presentation
  // feedbacks if the number is too big.
  void VerifyNumberOfSubmittedFrames();

  // Verifies wl_buffers for the given |frame| exist. If they do not yet exist,
  // a callback to |MaybeProcessPendingFrame| is set and false is returned.
  // If the frame contains a buffer id for an invalid WaylandBufferHandle, the
  // |frame::buffer_lost| is set and false is returned. That means that the
  // frame must not be used for the further submission.
  bool EnsureWlBuffersExist(WaylandFrame& frame);

  // Immediately clears submitted_buffers in the 1st in-flight submitted_frame.
  // This unblocks the pipeline.
  // TODO(crbug.com/40237160): Remove related workaround once CrOS side fix
  // stablizes.
  void FreezeTimeout();

  void UpdatePresentationFlushTimer();
  void OnPresentationFlushTimerFired();

  const raw_ptr<WaylandWindow> window_;

  // When RecordFrame() is called, a Frame is pushed to |pending_frames_|. See
  // RecordFrame().
  base::circular_deque<std::unique_ptr<WaylandFrame>> pending_frames_;

  // After PlayBackFrame() is called, a Frame is pushed to |submitted_frames_|.
  // See MaybeProcessPendingFrame().
  base::circular_deque<std::unique_ptr<WaylandFrame>> submitted_frames_;

  // Non-owned pointer to the main connection.
  const raw_ptr<WaylandConnection> connection_;

  // Set when invalid frame data is sent and the gpu process must be terminated.
  std::string fatal_error_message_;

  base::OneShotTimer presentation_flush_timer_;

  base::OneShotTimer frame_callback_timer_;

  int video_capture_count_ = 0;

  // Indicates that a graphics freeze was detected from the compositor no longer
  // sending frame callbacks, which is the case in mutter before before it sends
  // the suspended state. If this occurs during tab capture, the captured
  // content would look frozen until the suspended state is received.
  // This is set when that is detected and a fallback rendering can be used
  // during tab capture without relying on frame callbacks. See
  // |should_skip_frame_callbacks_| below.
  int frame_callback_freeze_detected_ = false;

  // Indicates if fallback rendering should be used by not relying on frame
  // callbacks to drive playback when |frame_callback_freeze_detected_| is true
  // and |video_capture_count_| is more than 0. The frame callbacks are still
  // set to be able to get notified once the compositor starts sending frame
  // callbacks again so that they can be used for playback again.
  bool should_skip_frame_callbacks_ = false;

  // Indicates if rendering should continue in the background without sending
  // surface commits to wayland. This is used to ensure video capture works when
  // we know the window is occluded and can simply bypass wayland in this case.
  // This optimized rendering path can only be used after we are notified of
  // suspended state which may be sent a few seconds after the window gets
  // occluded, as is the case in mutter.
  bool should_ack_swap_without_commit_ = false;

  base::WeakPtrFactory<WaylandFrameManager> weak_factory_;
};

}  // namespace ui

#endif  // UI_OZONE_PLATFORM_WAYLAND_HOST_WAYLAND_FRAME_MANAGER_H_