xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/linux/x11/x_server_pixel_buffer.h (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 // Don't include this file in any .h files because it pulls in some X headers.
12 
13 #ifndef MODULES_DESKTOP_CAPTURE_LINUX_X11_X_SERVER_PIXEL_BUFFER_H_
14 #define MODULES_DESKTOP_CAPTURE_LINUX_X11_X_SERVER_PIXEL_BUFFER_H_
15 
16 #include <X11/Xutil.h>
17 #include <X11/extensions/XShm.h>
18 
19 #include <memory>
20 #include <vector>
21 
22 #include "modules/desktop_capture/desktop_geometry.h"
23 
24 namespace webrtc {
25 
26 class DesktopFrame;
27 class XAtomCache;
28 
29 // A class to allow the X server's pixel buffer to be accessed as efficiently
30 // as possible.
31 class XServerPixelBuffer {
32  public:
33   XServerPixelBuffer();
34   ~XServerPixelBuffer();
35 
36   XServerPixelBuffer(const XServerPixelBuffer&) = delete;
37   XServerPixelBuffer& operator=(const XServerPixelBuffer&) = delete;
38 
39   void Release();
40 
41   // Allocate (or reallocate) the pixel buffer for `window`. Returns false in
42   // case of an error (e.g. window doesn't exist).
43   bool Init(XAtomCache* cache, Window window);
44 
is_initialized()45   bool is_initialized() { return window_ != 0; }
46 
47   // Returns the size of the window the buffer was initialized for.
window_size()48   DesktopSize window_size() { return window_rect_.size(); }
49 
50   // Returns the rectangle of the window the buffer was initialized for.
window_rect()51   const DesktopRect& window_rect() { return window_rect_; }
52 
53   // Returns true if the window can be found.
54   bool IsWindowValid() const;
55 
56   // If shared memory is being used without pixmaps, synchronize this pixel
57   // buffer with the root window contents (otherwise, this is a no-op).
58   // This is to avoid doing a full-screen capture for each individual
59   // rectangle in the capture list, when it only needs to be done once at the
60   // beginning.
61   void Synchronize();
62 
63   // Capture the specified rectangle and stores it in the `frame`. In the case
64   // where the full-screen data is captured by Synchronize(), this simply
65   // returns the pointer without doing any more work. The caller must ensure
66   // that `rect` is not larger than window_size().
67   bool CaptureRect(const DesktopRect& rect, DesktopFrame* frame);
68 
69  private:
70   void ReleaseSharedMemorySegment();
71 
72   void InitShm(const XWindowAttributes& attributes);
73   bool InitPixmaps(int depth);
74 
75   Display* display_ = nullptr;
76   Window window_ = 0;
77   DesktopRect window_rect_;
78   XImage* x_image_ = nullptr;
79   XShmSegmentInfo* shm_segment_info_ = nullptr;
80   XImage* x_shm_image_ = nullptr;
81   Pixmap shm_pixmap_ = 0;
82   GC shm_gc_ = nullptr;
83   bool xshm_get_image_succeeded_ = false;
84   std::vector<uint8_t> icc_profile_;
85 };
86 
87 }  // namespace webrtc
88 
89 #endif  // MODULES_DESKTOP_CAPTURE_LINUX_X11_X_SERVER_PIXEL_BUFFER_H_
90