xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/linux/x11/screen_capturer_x11.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_DESKTOP_CAPTURE_LINUX_X11_SCREEN_CAPTURER_X11_H_
12 #define MODULES_DESKTOP_CAPTURE_LINUX_X11_SCREEN_CAPTURER_X11_H_
13 
14 #include <X11/X.h>
15 #include <X11/Xlib.h>
16 #include <X11/extensions/Xdamage.h>
17 #include <X11/extensions/Xfixes.h>
18 #include <X11/extensions/Xrandr.h>
19 
20 #include <memory>
21 
22 #include "modules/desktop_capture/desktop_capture_options.h"
23 #include "modules/desktop_capture/desktop_capturer.h"
24 #include "modules/desktop_capture/desktop_frame.h"
25 #include "modules/desktop_capture/desktop_region.h"
26 #include "modules/desktop_capture/linux/x11/shared_x_display.h"
27 #include "modules/desktop_capture/linux/x11/x_atom_cache.h"
28 #include "modules/desktop_capture/linux/x11/x_server_pixel_buffer.h"
29 #include "modules/desktop_capture/screen_capture_frame_queue.h"
30 #include "modules/desktop_capture/screen_capturer_helper.h"
31 #include "modules/desktop_capture/shared_desktop_frame.h"
32 
33 namespace webrtc {
34 
35 // A class to perform video frame capturing for Linux on X11.
36 //
37 // If XDamage is used, this class sets DesktopFrame::updated_region() according
38 // to the areas reported by XDamage. Otherwise this class does not detect
39 // DesktopFrame::updated_region(), the field is always set to the entire frame
40 // rectangle. ScreenCapturerDifferWrapper should be used if that functionality
41 // is necessary.
42 class ScreenCapturerX11 : public DesktopCapturer,
43                           public SharedXDisplay::XEventHandler {
44  public:
45   ScreenCapturerX11();
46   ~ScreenCapturerX11() override;
47 
48   ScreenCapturerX11(const ScreenCapturerX11&) = delete;
49   ScreenCapturerX11& operator=(const ScreenCapturerX11&) = delete;
50 
51   static std::unique_ptr<DesktopCapturer> CreateRawScreenCapturer(
52       const DesktopCaptureOptions& options);
53 
54   // TODO(ajwong): Do we really want this to be synchronous?
55   bool Init(const DesktopCaptureOptions& options);
56 
57   // DesktopCapturer interface.
58   void Start(Callback* delegate) override;
59   void CaptureFrame() override;
60   bool GetSourceList(SourceList* sources) override;
61   bool SelectSource(SourceId id) override;
62 
63  private:
display()64   Display* display() { return options_.x_display()->display(); }
65 
66   // SharedXDisplay::XEventHandler interface.
67   bool HandleXEvent(const XEvent& event) override;
68 
69   void InitXDamage();
70   void InitXrandr();
71   void UpdateMonitors();
72 
73   // Capture screen pixels to the current buffer in the queue. In the DAMAGE
74   // case, the ScreenCapturerHelper already holds the list of invalid rectangles
75   // from HandleXEvent(). In the non-DAMAGE case, this captures the
76   // whole screen, then calculates some invalid rectangles that include any
77   // differences between this and the previous capture.
78   std::unique_ptr<DesktopFrame> CaptureScreen();
79 
80   // Called when the screen configuration is changed.
81   void ScreenConfigurationChanged();
82 
83   // Synchronize the current buffer with `last_buffer_`, by copying pixels from
84   // the area of `last_invalid_rects`.
85   // Note this only works on the assumption that kNumBuffers == 2, as
86   // `last_invalid_rects` holds the differences from the previous buffer and
87   // the one prior to that (which will then be the current buffer).
88   void SynchronizeFrame();
89 
90   void DeinitXlib();
91 
92   DesktopCaptureOptions options_;
93 
94   Callback* callback_ = nullptr;
95 
96   // X11 graphics context.
97   GC gc_ = nullptr;
98   Window root_window_ = BadValue;
99 
100   // XRandR 1.5 monitors.
101   bool use_randr_ = false;
102   int randr_event_base_ = 0;
103   XRRMonitorInfo* monitors_ = nullptr;
104   int num_monitors_ = 0;
105   DesktopRect selected_monitor_rect_;
106   // selected_monitor_name_ will be changed to kFullDesktopScreenId
107   // by a call to SelectSource() at the end of Init() because
108   // selected_monitor_rect_ should be updated as well.
109   // Setting it to kFullDesktopScreenId here might be misleading.
110   Atom selected_monitor_name_ = 0;
111   typedef XRRMonitorInfo* (*get_monitors_func)(Display*, Window, Bool, int*);
112   typedef void (*free_monitors_func)(XRRMonitorInfo*);
113   get_monitors_func get_monitors_ = nullptr;
114   free_monitors_func free_monitors_ = nullptr;
115 
116   // XFixes.
117   bool has_xfixes_ = false;
118   int xfixes_event_base_ = -1;
119   int xfixes_error_base_ = -1;
120 
121   // XDamage information.
122   bool use_damage_ = false;
123   Damage damage_handle_ = 0;
124   int damage_event_base_ = -1;
125   int damage_error_base_ = -1;
126   XserverRegion damage_region_ = 0;
127 
128   // Access to the X Server's pixel buffer.
129   XServerPixelBuffer x_server_pixel_buffer_;
130 
131   // A thread-safe list of invalid rectangles, and the size of the most
132   // recently captured screen.
133   ScreenCapturerHelper helper_;
134 
135   // Queue of the frames buffers.
136   ScreenCaptureFrameQueue<SharedDesktopFrame> queue_;
137 
138   // Invalid region from the previous capture. This is used to synchronize the
139   // current with the last buffer used.
140   DesktopRegion last_invalid_region_;
141 
142   std::unique_ptr<XAtomCache> atom_cache_;
143 };
144 
145 }  // namespace webrtc
146 
147 #endif  // MODULES_DESKTOP_CAPTURE_LINUX_X11_SCREEN_CAPTURER_X11_H_
148