OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 | 12 |
13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
14 #include "webrtc/modules/desktop_capture/screen_drawer.h" | 14 #include "webrtc/modules/desktop_capture/screen_drawer.h" |
15 #include "webrtc/modules/desktop_capture/x11/shared_x_display.h" | 15 #include "webrtc/modules/desktop_capture/x11/shared_x_display.h" |
| 16 #include "webrtc/system_wrappers/include/sleep.h" |
16 | 17 |
17 namespace webrtc { | 18 namespace webrtc { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 // A ScreenDrawer implementation for X11. | 22 // A ScreenDrawer implementation for X11. |
22 class ScreenDrawerLinux : public ScreenDrawer { | 23 class ScreenDrawerLinux : public ScreenDrawer { |
23 public: | 24 public: |
24 ScreenDrawerLinux(); | 25 ScreenDrawerLinux(); |
25 ~ScreenDrawerLinux() override; | 26 ~ScreenDrawerLinux() override; |
26 | 27 |
27 // ScreenDrawer interface. | 28 // ScreenDrawer interface. |
28 DesktopRect DrawableRegion() override; | 29 DesktopRect DrawableRegion() override; |
29 void DrawRectangle(DesktopRect rect, uint32_t rgba) override; | 30 void DrawRectangle(DesktopRect rect, RgbaColor color) override; |
30 void Clear() override; | 31 void Clear() override; |
| 32 void WaitForPendingDraws() override; |
31 | 33 |
32 private: | 34 private: |
33 rtc::scoped_refptr<SharedXDisplay> display_; | 35 rtc::scoped_refptr<SharedXDisplay> display_; |
34 Screen* screen_; | |
35 int screen_num_; | 36 int screen_num_; |
36 DesktopRect rect_; | 37 DesktopRect rect_; |
37 Window window_; | 38 Window window_; |
38 GC context_; | 39 GC context_; |
39 Colormap colormap_; | 40 Colormap colormap_; |
40 }; | 41 }; |
41 | 42 |
42 ScreenDrawerLinux::ScreenDrawerLinux() { | 43 ScreenDrawerLinux::ScreenDrawerLinux() { |
43 display_ = SharedXDisplay::CreateDefault(); | 44 display_ = SharedXDisplay::CreateDefault(); |
44 RTC_CHECK(display_.get()); | 45 RTC_CHECK(display_.get()); |
45 screen_ = DefaultScreenOfDisplay(display_->display()); | |
46 RTC_CHECK(screen_); | |
47 screen_num_ = DefaultScreen(display_->display()); | 46 screen_num_ = DefaultScreen(display_->display()); |
48 rect_ = DesktopRect::MakeWH(screen_->width, screen_->height); | 47 XWindowAttributes root_attributes; |
49 window_ = XCreateSimpleWindow(display_->display(), | 48 if (!XGetWindowAttributes(display_->display(), |
50 RootWindow(display_->display(), screen_num_), 0, | 49 RootWindow(display_->display(), screen_num_), |
51 0, rect_.width(), rect_.height(), 0, | 50 &root_attributes)) { |
52 BlackPixel(display_->display(), screen_num_), | 51 RTC_DCHECK(false) << "Failed to get root window size."; |
53 BlackPixel(display_->display(), screen_num_)); | 52 } |
| 53 window_ = XCreateSimpleWindow( |
| 54 display_->display(), RootWindow(display_->display(), screen_num_), 0, 0, |
| 55 root_attributes.width, root_attributes.height, 0, |
| 56 BlackPixel(display_->display(), screen_num_), |
| 57 BlackPixel(display_->display(), screen_num_)); |
54 XSelectInput(display_->display(), window_, StructureNotifyMask); | 58 XSelectInput(display_->display(), window_, StructureNotifyMask); |
55 XMapWindow(display_->display(), window_); | 59 XMapWindow(display_->display(), window_); |
56 while (true) { | 60 while (true) { |
57 XEvent event; | 61 XEvent event; |
58 XNextEvent(display_->display(), &event); | 62 XNextEvent(display_->display(), &event); |
59 if (event.type == MapNotify) { | 63 if (event.type == MapNotify) { |
60 break; | 64 break; |
61 } | 65 } |
62 } | 66 } |
63 XFlush(display_->display()); | 67 XFlush(display_->display()); |
| 68 Window child; |
| 69 int x, y; |
| 70 if (!XTranslateCoordinates(display_->display(), window_, |
| 71 RootWindow(display_->display(), screen_num_), 0, 0, |
| 72 &x, &y, &child)) { |
| 73 RTC_DCHECK(false) << "Failed to get window position."; |
| 74 } |
| 75 // Some window manager does not allow a window to cover two or more monitors. |
| 76 // So if the window is on the first monitor of a two-monitor system, the |
| 77 // second half won't be able to show up without changing configurations of WM, |
| 78 // and its DrawableRegion() is not accurate. |
| 79 rect_ = DesktopRect::MakeLTRB(x, y, root_attributes.width, |
| 80 root_attributes.height); |
64 context_ = DefaultGC(display_->display(), screen_num_); | 81 context_ = DefaultGC(display_->display(), screen_num_); |
65 colormap_ = DefaultColormap(display_->display(), screen_num_); | 82 colormap_ = DefaultColormap(display_->display(), screen_num_); |
| 83 // Wait for window animations. |
| 84 SleepMs(200); |
66 } | 85 } |
67 | 86 |
68 ScreenDrawerLinux::~ScreenDrawerLinux() { | 87 ScreenDrawerLinux::~ScreenDrawerLinux() { |
69 XUnmapWindow(display_->display(), window_); | 88 XUnmapWindow(display_->display(), window_); |
70 XDestroyWindow(display_->display(), window_); | 89 XDestroyWindow(display_->display(), window_); |
71 } | 90 } |
72 | 91 |
73 DesktopRect ScreenDrawerLinux::DrawableRegion() { | 92 DesktopRect ScreenDrawerLinux::DrawableRegion() { |
74 return rect_; | 93 return rect_; |
75 } | 94 } |
76 | 95 |
77 void ScreenDrawerLinux::DrawRectangle(DesktopRect rect, uint32_t rgba) { | 96 void ScreenDrawerLinux::DrawRectangle(DesktopRect rect, RgbaColor color) { |
78 int r = (rgba & 0xff00) >> 8; | 97 rect.Translate(-rect_.left(), -rect_.top()); |
79 int g = (rgba & 0xff0000) >> 16; | 98 XColor xcolor; |
80 int b = (rgba & 0xff000000) >> 24; | |
81 // X11 does not support Alpha. | 99 // X11 does not support Alpha. |
82 XColor color; | 100 // X11 uses 16 bits for each primary color, so we need to slightly normalize |
83 // X11 uses 16 bits for each primary color. | 101 // a 8 bits channel to 16 bits channel, by setting the low 8 bits as its high |
84 color.red = r * 256; | 102 // 8 bits to avoid a mismatch of color returned by capturer. |
85 color.green = g * 256; | 103 xcolor.red = (color.red << 8) + color.red; |
86 color.blue = b * 256; | 104 xcolor.green = (color.green << 8) + color.green; |
87 color.flags = DoRed | DoGreen | DoBlue; | 105 xcolor.blue = (color.blue << 8) + color.blue; |
88 XAllocColor(display_->display(), colormap_, &color); | 106 xcolor.flags = DoRed | DoGreen | DoBlue; |
89 XSetForeground(display_->display(), context_, color.pixel); | 107 XAllocColor(display_->display(), colormap_, &xcolor); |
| 108 XSetForeground(display_->display(), context_, xcolor.pixel); |
90 XFillRectangle(display_->display(), window_, context_, rect.left(), | 109 XFillRectangle(display_->display(), window_, context_, rect.left(), |
91 rect.top(), rect.width(), rect.height()); | 110 rect.top(), rect.width(), rect.height()); |
92 XFlush(display_->display()); | 111 XFlush(display_->display()); |
93 } | 112 } |
94 | 113 |
95 void ScreenDrawerLinux::Clear() { | 114 void ScreenDrawerLinux::Clear() { |
96 DrawRectangle(DrawableRegion(), 0); | 115 DrawRectangle(rect_, RgbaColor(0, 0, 0)); |
| 116 } |
| 117 |
| 118 // TODO(zijiehe): Find the right signal from X11 to indicate the finish of all |
| 119 // pending paintings. |
| 120 void ScreenDrawerLinux::WaitForPendingDraws() { |
| 121 SleepMs(50); |
97 } | 122 } |
98 | 123 |
99 } // namespace | 124 } // namespace |
100 | 125 |
101 // static | 126 // static |
102 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { | 127 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { |
103 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerLinux()); | 128 if (SharedXDisplay::CreateDefault().get()) { |
| 129 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerLinux()); |
| 130 } |
| 131 return nullptr; |
104 } | 132 } |
105 | 133 |
106 } // namespace webrtc | 134 } // namespace webrtc |
OLD | NEW |