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