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, Color 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, Color 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; | |
83 // X11 uses 16 bits for each primary color. | 100 // X11 uses 16 bits for each primary color. |
84 color.red = r * 256; | 101 xcolor.red = color.red() * 256 + color.red(); |
Sergey Ulanov
2016/09/01 19:26:38
Isn't it the same as color.red() * 257?
Same for g
Jamie
2016/09/01 20:38:53
It is the same, but I think the explicit addition
Hzj_jie
2016/09/01 23:25:09
My initial thought is, "color.red() * 257" looks l
| |
85 color.green = g * 256; | 102 xcolor.green = color.green() * 256 + color.green(); |
86 color.blue = b * 256; | 103 xcolor.blue = color.blue() * 256 + color.blue(); |
87 color.flags = DoRed | DoGreen | DoBlue; | 104 xcolor.flags = DoRed | DoGreen | DoBlue; |
88 XAllocColor(display_->display(), colormap_, &color); | 105 XAllocColor(display_->display(), colormap_, &xcolor); |
89 XSetForeground(display_->display(), context_, color.pixel); | 106 XSetForeground(display_->display(), context_, xcolor.pixel); |
90 XFillRectangle(display_->display(), window_, context_, rect.left(), | 107 XFillRectangle(display_->display(), window_, context_, rect.left(), |
91 rect.top(), rect.width(), rect.height()); | 108 rect.top(), rect.width(), rect.height()); |
92 XFlush(display_->display()); | 109 XFlush(display_->display()); |
93 } | 110 } |
94 | 111 |
95 void ScreenDrawerLinux::Clear() { | 112 void ScreenDrawerLinux::Clear() { |
96 DrawRectangle(DrawableRegion(), 0); | 113 DrawRectangle(rect_, Color(0, 0, 0)); |
114 } | |
115 | |
116 // TODO(zijiehe): Find the right signal from X11 to indicate the finish of all | |
117 // pending paintings. | |
118 void ScreenDrawerLinux::WaitForPendingDraws() { | |
119 SleepMs(50); | |
97 } | 120 } |
98 | 121 |
99 } // namespace | 122 } // namespace |
100 | 123 |
101 // static | 124 // static |
102 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { | 125 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { |
103 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerLinux()); | 126 if (SharedXDisplay::CreateDefault().get()) { |
127 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerLinux()); | |
128 } else { | |
Sergey Ulanov
2016/09/01 19:26:38
no else after return please.
Hzj_jie
2016/09/01 23:25:09
Done.
| |
129 return nullptr; | |
130 } | |
104 } | 131 } |
105 | 132 |
106 } // namespace webrtc | 133 } // namespace webrtc |
OLD | NEW |