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