xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/linux/x11/screen_capturer_x11.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2013 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 #include "modules/desktop_capture/linux/x11/screen_capturer_x11.h"
12 
13 #include <X11/Xlib.h>
14 #include <X11/extensions/Xdamage.h>
15 #include <X11/extensions/Xfixes.h>
16 #include <X11/extensions/damagewire.h>
17 #include <dlfcn.h>
18 #include <stdint.h>
19 #include <string.h>
20 
21 #include <memory>
22 #include <utility>
23 
24 #include "modules/desktop_capture/desktop_capture_options.h"
25 #include "modules/desktop_capture/desktop_capturer.h"
26 #include "modules/desktop_capture/desktop_frame.h"
27 #include "modules/desktop_capture/desktop_geometry.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 #include "rtc_base/checks.h"
33 #include "rtc_base/logging.h"
34 #include "rtc_base/sanitizer.h"
35 #include "rtc_base/time_utils.h"
36 #include "rtc_base/trace_event.h"
37 
38 namespace webrtc {
39 
ScreenCapturerX11()40 ScreenCapturerX11::ScreenCapturerX11() {
41   helper_.SetLogGridSize(4);
42 }
43 
~ScreenCapturerX11()44 ScreenCapturerX11::~ScreenCapturerX11() {
45   options_.x_display()->RemoveEventHandler(ConfigureNotify, this);
46   if (use_damage_) {
47     options_.x_display()->RemoveEventHandler(damage_event_base_ + XDamageNotify,
48                                              this);
49   }
50   if (use_randr_) {
51     options_.x_display()->RemoveEventHandler(
52         randr_event_base_ + RRScreenChangeNotify, this);
53   }
54   DeinitXlib();
55 }
56 
Init(const DesktopCaptureOptions & options)57 bool ScreenCapturerX11::Init(const DesktopCaptureOptions& options) {
58   TRACE_EVENT0("webrtc", "ScreenCapturerX11::Init");
59   options_ = options;
60 
61   atom_cache_ = std::make_unique<XAtomCache>(display());
62 
63   root_window_ = RootWindow(display(), DefaultScreen(display()));
64   if (root_window_ == BadValue) {
65     RTC_LOG(LS_ERROR) << "Unable to get the root window";
66     DeinitXlib();
67     return false;
68   }
69 
70   gc_ = XCreateGC(display(), root_window_, 0, NULL);
71   if (gc_ == NULL) {
72     RTC_LOG(LS_ERROR) << "Unable to get graphics context";
73     DeinitXlib();
74     return false;
75   }
76 
77   options_.x_display()->AddEventHandler(ConfigureNotify, this);
78 
79   // Check for XFixes extension. This is required for cursor shape
80   // notifications, and for our use of XDamage.
81   if (XFixesQueryExtension(display(), &xfixes_event_base_,
82                            &xfixes_error_base_)) {
83     has_xfixes_ = true;
84   } else {
85     RTC_LOG(LS_INFO) << "X server does not support XFixes.";
86   }
87 
88   // Register for changes to the dimensions of the root window.
89   XSelectInput(display(), root_window_, StructureNotifyMask);
90 
91   if (!x_server_pixel_buffer_.Init(atom_cache_.get(),
92                                    DefaultRootWindow(display()))) {
93     RTC_LOG(LS_ERROR) << "Failed to initialize pixel buffer.";
94     return false;
95   }
96 
97   if (options_.use_update_notifications()) {
98     InitXDamage();
99   }
100 
101   InitXrandr();
102 
103   // Default source set here so that selected_monitor_rect_ is sized correctly.
104   SelectSource(kFullDesktopScreenId);
105 
106   return true;
107 }
108 
InitXDamage()109 void ScreenCapturerX11::InitXDamage() {
110   // Our use of XDamage requires XFixes.
111   if (!has_xfixes_) {
112     return;
113   }
114 
115   // Check for XDamage extension.
116   if (!XDamageQueryExtension(display(), &damage_event_base_,
117                              &damage_error_base_)) {
118     RTC_LOG(LS_INFO) << "X server does not support XDamage.";
119     return;
120   }
121 
122   // TODO(lambroslambrou): Disable DAMAGE in situations where it is known
123   // to fail, such as when Desktop Effects are enabled, with graphics
124   // drivers (nVidia, ATI) that fail to report DAMAGE notifications
125   // properly.
126 
127   // Request notifications every time the screen becomes damaged.
128   damage_handle_ =
129       XDamageCreate(display(), root_window_, XDamageReportNonEmpty);
130   if (!damage_handle_) {
131     RTC_LOG(LS_ERROR) << "Unable to initialize XDamage.";
132     return;
133   }
134 
135   // Create an XFixes server-side region to collate damage into.
136   damage_region_ = XFixesCreateRegion(display(), 0, 0);
137   if (!damage_region_) {
138     XDamageDestroy(display(), damage_handle_);
139     RTC_LOG(LS_ERROR) << "Unable to create XFixes region.";
140     return;
141   }
142 
143   options_.x_display()->AddEventHandler(damage_event_base_ + XDamageNotify,
144                                         this);
145 
146   use_damage_ = true;
147   RTC_LOG(LS_INFO) << "Using XDamage extension.";
148 }
149 
150 RTC_NO_SANITIZE("cfi-icall")
InitXrandr()151 void ScreenCapturerX11::InitXrandr() {
152   int major_version = 0;
153   int minor_version = 0;
154   int error_base_ignored = 0;
155   if (XRRQueryExtension(display(), &randr_event_base_, &error_base_ignored) &&
156       XRRQueryVersion(display(), &major_version, &minor_version)) {
157     if (major_version > 1 || (major_version == 1 && minor_version >= 5)) {
158       // Dynamically link XRRGetMonitors and XRRFreeMonitors as a workaround
159       // to avoid a dependency issue with Debian 8.
160       get_monitors_ = reinterpret_cast<get_monitors_func>(
161           dlsym(RTLD_DEFAULT, "XRRGetMonitors"));
162       free_monitors_ = reinterpret_cast<free_monitors_func>(
163           dlsym(RTLD_DEFAULT, "XRRFreeMonitors"));
164       if (get_monitors_ && free_monitors_) {
165         use_randr_ = true;
166         RTC_LOG(LS_INFO) << "Using XRandR extension v" << major_version << '.'
167                          << minor_version << '.';
168         monitors_ =
169             get_monitors_(display(), root_window_, true, &num_monitors_);
170 
171         // Register for screen change notifications
172         XRRSelectInput(display(), root_window_, RRScreenChangeNotifyMask);
173         options_.x_display()->AddEventHandler(
174             randr_event_base_ + RRScreenChangeNotify, this);
175       } else {
176         RTC_LOG(LS_ERROR) << "Unable to link XRandR monitor functions.";
177       }
178     } else {
179       RTC_LOG(LS_ERROR) << "XRandR entension is older than v1.5.";
180     }
181   } else {
182     RTC_LOG(LS_ERROR) << "X server does not support XRandR.";
183   }
184 }
185 
186 RTC_NO_SANITIZE("cfi-icall")
UpdateMonitors()187 void ScreenCapturerX11::UpdateMonitors() {
188   // The queue should be reset whenever |selected_monitor_rect_| changes, so
189   // that the DCHECKs in CaptureScreen() are satisfied.
190   queue_.Reset();
191 
192   if (monitors_) {
193     free_monitors_(monitors_);
194     monitors_ = nullptr;
195   }
196 
197   monitors_ = get_monitors_(display(), root_window_, true, &num_monitors_);
198 
199   if (selected_monitor_name_) {
200     if (selected_monitor_name_ == static_cast<Atom>(kFullDesktopScreenId)) {
201       selected_monitor_rect_ =
202           DesktopRect::MakeSize(x_server_pixel_buffer_.window_size());
203       return;
204     }
205 
206     for (int i = 0; i < num_monitors_; ++i) {
207       XRRMonitorInfo& m = monitors_[i];
208       if (selected_monitor_name_ == m.name) {
209         RTC_LOG(LS_INFO) << "XRandR monitor " << m.name << " rect updated.";
210         selected_monitor_rect_ =
211             DesktopRect::MakeXYWH(m.x, m.y, m.width, m.height);
212         const auto& pixel_buffer_rect = x_server_pixel_buffer_.window_rect();
213         if (!pixel_buffer_rect.ContainsRect(selected_monitor_rect_)) {
214           // This is never expected to happen, but crop the rectangle anyway
215           // just in case the server returns inconsistent information.
216           // CaptureScreen() expects `selected_monitor_rect_` to lie within
217           // the pixel-buffer's rectangle.
218           RTC_LOG(LS_WARNING)
219               << "Cropping selected monitor rect to fit the pixel-buffer.";
220           selected_monitor_rect_.IntersectWith(pixel_buffer_rect);
221         }
222         return;
223       }
224     }
225 
226     // The selected monitor is not connected anymore
227     RTC_LOG(LS_INFO) << "XRandR selected monitor " << selected_monitor_name_
228                      << " lost.";
229     selected_monitor_rect_ = DesktopRect::MakeWH(0, 0);
230   }
231 }
232 
Start(Callback * callback)233 void ScreenCapturerX11::Start(Callback* callback) {
234   RTC_DCHECK(!callback_);
235   RTC_DCHECK(callback);
236 
237   callback_ = callback;
238 }
239 
CaptureFrame()240 void ScreenCapturerX11::CaptureFrame() {
241   TRACE_EVENT0("webrtc", "ScreenCapturerX11::CaptureFrame");
242   int64_t capture_start_time_nanos = rtc::TimeNanos();
243 
244   queue_.MoveToNextFrame();
245   if (queue_.current_frame() && queue_.current_frame()->IsShared()) {
246     RTC_DLOG(LS_WARNING) << "Overwriting frame that is still shared.";
247   }
248 
249   // Process XEvents for XDamage and cursor shape tracking.
250   options_.x_display()->ProcessPendingXEvents();
251 
252   // ProcessPendingXEvents() may call ScreenConfigurationChanged() which
253   // reinitializes `x_server_pixel_buffer_`. Check if the pixel buffer is still
254   // in a good shape.
255   if (!x_server_pixel_buffer_.is_initialized()) {
256     // We failed to initialize pixel buffer.
257     RTC_LOG(LS_ERROR) << "Pixel buffer is not initialized.";
258     callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr);
259     return;
260   }
261 
262   // Allocate the current frame buffer only if it is not already allocated.
263   // Note that we can't reallocate other buffers at this point, since the caller
264   // may still be reading from them.
265   if (!queue_.current_frame()) {
266     std::unique_ptr<DesktopFrame> frame(
267         new BasicDesktopFrame(selected_monitor_rect_.size()));
268 
269     // We set the top-left of the frame so the mouse cursor will be composited
270     // properly, and our frame buffer will not be overrun while blitting.
271     frame->set_top_left(selected_monitor_rect_.top_left());
272     queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(std::move(frame)));
273   }
274 
275   std::unique_ptr<DesktopFrame> result = CaptureScreen();
276   if (!result) {
277     RTC_LOG(LS_WARNING) << "Temporarily failed to capture screen.";
278     callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr);
279     return;
280   }
281 
282   last_invalid_region_ = result->updated_region();
283   result->set_capture_time_ms((rtc::TimeNanos() - capture_start_time_nanos) /
284                               rtc::kNumNanosecsPerMillisec);
285   result->set_capturer_id(DesktopCapturerId::kX11CapturerLinux);
286   callback_->OnCaptureResult(Result::SUCCESS, std::move(result));
287 }
288 
GetSourceList(SourceList * sources)289 bool ScreenCapturerX11::GetSourceList(SourceList* sources) {
290   RTC_DCHECK(sources->size() == 0);
291   if (!use_randr_) {
292     sources->push_back({});
293     return true;
294   }
295 
296   // Ensure that `monitors_` is updated with changes that may have happened
297   // between calls to GetSourceList().
298   options_.x_display()->ProcessPendingXEvents();
299 
300   for (int i = 0; i < num_monitors_; ++i) {
301     XRRMonitorInfo& m = monitors_[i];
302     char* monitor_title = XGetAtomName(display(), m.name);
303 
304     // Note name is an X11 Atom used to id the monitor.
305     sources->push_back({static_cast<SourceId>(m.name), monitor_title});
306     XFree(monitor_title);
307   }
308 
309   return true;
310 }
311 
SelectSource(SourceId id)312 bool ScreenCapturerX11::SelectSource(SourceId id) {
313   // Prevent the reuse of any frame buffers allocated for a previously selected
314   // source. This is required to stop crashes, or old data from appearing in
315   // a captured frame, when the new source is sized differently then the source
316   // that was selected at the time a reused frame buffer was created.
317   queue_.Reset();
318 
319   if (!use_randr_ || id == kFullDesktopScreenId) {
320     selected_monitor_name_ = kFullDesktopScreenId;
321     selected_monitor_rect_ =
322         DesktopRect::MakeSize(x_server_pixel_buffer_.window_size());
323     return true;
324   }
325 
326   for (int i = 0; i < num_monitors_; ++i) {
327     if (id == static_cast<SourceId>(monitors_[i].name)) {
328       RTC_LOG(LS_INFO) << "XRandR selected source: " << id;
329       XRRMonitorInfo& m = monitors_[i];
330       selected_monitor_name_ = m.name;
331       selected_monitor_rect_ =
332           DesktopRect::MakeXYWH(m.x, m.y, m.width, m.height);
333       const auto& pixel_buffer_rect = x_server_pixel_buffer_.window_rect();
334       if (!pixel_buffer_rect.ContainsRect(selected_monitor_rect_)) {
335         RTC_LOG(LS_WARNING)
336             << "Cropping selected monitor rect to fit the pixel-buffer.";
337         selected_monitor_rect_.IntersectWith(pixel_buffer_rect);
338       }
339       return true;
340     }
341   }
342   return false;
343 }
344 
HandleXEvent(const XEvent & event)345 bool ScreenCapturerX11::HandleXEvent(const XEvent& event) {
346   if (use_damage_ && (event.type == damage_event_base_ + XDamageNotify)) {
347     const XDamageNotifyEvent* damage_event =
348         reinterpret_cast<const XDamageNotifyEvent*>(&event);
349     if (damage_event->damage != damage_handle_)
350       return false;
351     RTC_DCHECK(damage_event->level == XDamageReportNonEmpty);
352     return true;
353   } else if (use_randr_ &&
354              event.type == randr_event_base_ + RRScreenChangeNotify) {
355     XRRUpdateConfiguration(const_cast<XEvent*>(&event));
356     UpdateMonitors();
357     RTC_LOG(LS_INFO) << "XRandR screen change event received.";
358     return false;
359   } else if (event.type == ConfigureNotify) {
360     ScreenConfigurationChanged();
361     return false;
362   }
363   return false;
364 }
365 
CaptureScreen()366 std::unique_ptr<DesktopFrame> ScreenCapturerX11::CaptureScreen() {
367   std::unique_ptr<SharedDesktopFrame> frame = queue_.current_frame()->Share();
368   RTC_DCHECK(selected_monitor_rect_.size().equals(frame->size()));
369   RTC_DCHECK(selected_monitor_rect_.top_left().equals(frame->top_left()));
370 
371   // Pass the screen size to the helper, so it can clip the invalid region if it
372   // expands that region to a grid. Note that the helper operates in the
373   // DesktopFrame coordinate system where the top-left pixel is (0, 0), even for
374   // a monitor with non-zero offset relative to `x_server_pixel_buffer_`.
375   helper_.set_size_most_recent(frame->size());
376 
377   // In the DAMAGE case, ensure the frame is up-to-date with the previous frame
378   // if any.  If there isn't a previous frame, that means a screen-resolution
379   // change occurred, and `invalid_rects` will be updated to include the whole
380   // screen.
381   if (use_damage_ && queue_.previous_frame())
382     SynchronizeFrame();
383 
384   DesktopRegion* updated_region = frame->mutable_updated_region();
385 
386   x_server_pixel_buffer_.Synchronize();
387   if (use_damage_ && queue_.previous_frame()) {
388     // Atomically fetch and clear the damage region.
389     XDamageSubtract(display(), damage_handle_, None, damage_region_);
390     int rects_num = 0;
391     XRectangle bounds;
392     XRectangle* rects = XFixesFetchRegionAndBounds(display(), damage_region_,
393                                                    &rects_num, &bounds);
394     for (int i = 0; i < rects_num; ++i) {
395       auto damage_rect = DesktopRect::MakeXYWH(rects[i].x, rects[i].y,
396                                                rects[i].width, rects[i].height);
397 
398       // Damage-regions are relative to `x_server_pixel_buffer`, so convert the
399       // region to DesktopFrame coordinates where the top-left is always (0, 0),
400       // before adding to the frame's updated_region. `helper_` also operates in
401       // DesktopFrame coordinates, and it will take care of cropping away any
402       // damage-regions that lie outside the selected monitor.
403       damage_rect.Translate(-frame->top_left());
404       updated_region->AddRect(damage_rect);
405     }
406     XFree(rects);
407     helper_.InvalidateRegion(*updated_region);
408 
409     // Capture the damaged portions of the desktop.
410     helper_.TakeInvalidRegion(updated_region);
411 
412     for (DesktopRegion::Iterator it(*updated_region); !it.IsAtEnd();
413          it.Advance()) {
414       auto rect = it.rect();
415       rect.Translate(frame->top_left());
416       if (!x_server_pixel_buffer_.CaptureRect(rect, frame.get()))
417         return nullptr;
418     }
419   } else {
420     // Doing full-screen polling, or this is the first capture after a
421     // screen-resolution change.  In either case, need a full-screen capture.
422     if (!x_server_pixel_buffer_.CaptureRect(selected_monitor_rect_,
423                                             frame.get())) {
424       return nullptr;
425     }
426     updated_region->SetRect(DesktopRect::MakeSize(frame->size()));
427   }
428 
429   return std::move(frame);
430 }
431 
ScreenConfigurationChanged()432 void ScreenCapturerX11::ScreenConfigurationChanged() {
433   TRACE_EVENT0("webrtc", "ScreenCapturerX11::ScreenConfigurationChanged");
434   // Make sure the frame buffers will be reallocated.
435   queue_.Reset();
436 
437   helper_.ClearInvalidRegion();
438   if (!x_server_pixel_buffer_.Init(atom_cache_.get(),
439                                    DefaultRootWindow(display()))) {
440     RTC_LOG(LS_ERROR) << "Failed to initialize pixel buffer after screen "
441                          "configuration change.";
442   }
443 
444   if (use_randr_) {
445     // Adding/removing RANDR monitors can generate a ConfigureNotify event
446     // without generating any RRScreenChangeNotify event. So it is important to
447     // update the monitors here even if the screen resolution hasn't changed.
448     UpdateMonitors();
449   } else {
450     selected_monitor_rect_ =
451         DesktopRect::MakeSize(x_server_pixel_buffer_.window_size());
452   }
453 }
454 
SynchronizeFrame()455 void ScreenCapturerX11::SynchronizeFrame() {
456   // Synchronize the current buffer with the previous one since we do not
457   // capture the entire desktop. Note that encoder may be reading from the
458   // previous buffer at this time so thread access complaints are false
459   // positives.
460 
461   // TODO(hclam): We can reduce the amount of copying here by subtracting
462   // `capturer_helper_`s region from `last_invalid_region_`.
463   // http://crbug.com/92354
464   RTC_DCHECK(queue_.previous_frame());
465 
466   DesktopFrame* current = queue_.current_frame();
467   DesktopFrame* last = queue_.previous_frame();
468   RTC_DCHECK(current != last);
469   for (DesktopRegion::Iterator it(last_invalid_region_); !it.IsAtEnd();
470        it.Advance()) {
471     const DesktopRect& r = it.rect();
472     current->CopyPixelsFrom(*last, r.top_left(), r);
473   }
474 }
475 
476 RTC_NO_SANITIZE("cfi-icall")
DeinitXlib()477 void ScreenCapturerX11::DeinitXlib() {
478   if (monitors_) {
479     free_monitors_(monitors_);
480     monitors_ = nullptr;
481   }
482 
483   if (gc_) {
484     XFreeGC(display(), gc_);
485     gc_ = nullptr;
486   }
487 
488   x_server_pixel_buffer_.Release();
489 
490   if (display()) {
491     if (damage_handle_) {
492       XDamageDestroy(display(), damage_handle_);
493       damage_handle_ = 0;
494     }
495 
496     if (damage_region_) {
497       XFixesDestroyRegion(display(), damage_region_);
498       damage_region_ = 0;
499     }
500   }
501 }
502 
503 // static
CreateRawScreenCapturer(const DesktopCaptureOptions & options)504 std::unique_ptr<DesktopCapturer> ScreenCapturerX11::CreateRawScreenCapturer(
505     const DesktopCaptureOptions& options) {
506   if (!options.x_display())
507     return nullptr;
508 
509   std::unique_ptr<ScreenCapturerX11> capturer(new ScreenCapturerX11());
510   if (!capturer.get()->Init(options)) {
511     return nullptr;
512   }
513 
514   return std::move(capturer);
515 }
516 
517 }  // namespace webrtc
518