| 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" | |
| 17 | 16 |
| 18 namespace webrtc { | 17 namespace webrtc { |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 // A ScreenDrawer implementation for X11. | 21 // A ScreenDrawer implementation for X11. |
| 23 class ScreenDrawerLinux : public ScreenDrawer { | 22 class ScreenDrawerLinux : public ScreenDrawer { |
| 24 public: | 23 public: |
| 25 ScreenDrawerLinux(); | 24 ScreenDrawerLinux(); |
| 26 ~ScreenDrawerLinux() override; | 25 ~ScreenDrawerLinux() override; |
| 27 | 26 |
| 28 // ScreenDrawer interface. | 27 // ScreenDrawer interface. |
| 29 DesktopRect DrawableRegion() override; | 28 DesktopRect DrawableRegion() override; |
| 30 void DrawRectangle(DesktopRect rect, RgbaColor color) override; | 29 void DrawRectangle(DesktopRect rect, uint32_t rgba) override; |
| 31 void Clear() override; | 30 void Clear() override; |
| 32 void WaitForPendingDraws() override; | |
| 33 | 31 |
| 34 private: | 32 private: |
| 35 rtc::scoped_refptr<SharedXDisplay> display_; | 33 rtc::scoped_refptr<SharedXDisplay> display_; |
| 34 Screen* screen_; |
| 36 int screen_num_; | 35 int screen_num_; |
| 37 DesktopRect rect_; | 36 DesktopRect rect_; |
| 38 Window window_; | 37 Window window_; |
| 39 GC context_; | 38 GC context_; |
| 40 Colormap colormap_; | 39 Colormap colormap_; |
| 41 }; | 40 }; |
| 42 | 41 |
| 43 ScreenDrawerLinux::ScreenDrawerLinux() { | 42 ScreenDrawerLinux::ScreenDrawerLinux() { |
| 44 display_ = SharedXDisplay::CreateDefault(); | 43 display_ = SharedXDisplay::CreateDefault(); |
| 45 RTC_CHECK(display_.get()); | 44 RTC_CHECK(display_.get()); |
| 45 screen_ = DefaultScreenOfDisplay(display_->display()); |
| 46 RTC_CHECK(screen_); |
| 46 screen_num_ = DefaultScreen(display_->display()); | 47 screen_num_ = DefaultScreen(display_->display()); |
| 47 XWindowAttributes root_attributes; | 48 rect_ = DesktopRect::MakeWH(screen_->width, screen_->height); |
| 48 if (!XGetWindowAttributes(display_->display(), | 49 window_ = XCreateSimpleWindow(display_->display(), |
| 49 RootWindow(display_->display(), screen_num_), | 50 RootWindow(display_->display(), screen_num_), 0, |
| 50 &root_attributes)) { | 51 0, rect_.width(), rect_.height(), 0, |
| 51 RTC_DCHECK(false) << "Failed to get root window size."; | 52 BlackPixel(display_->display(), screen_num_), |
| 52 } | 53 BlackPixel(display_->display(), screen_num_)); |
| 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_)); | |
| 58 XSelectInput(display_->display(), window_, StructureNotifyMask); | 54 XSelectInput(display_->display(), window_, StructureNotifyMask); |
| 59 XMapWindow(display_->display(), window_); | 55 XMapWindow(display_->display(), window_); |
| 60 while (true) { | 56 while (true) { |
| 61 XEvent event; | 57 XEvent event; |
| 62 XNextEvent(display_->display(), &event); | 58 XNextEvent(display_->display(), &event); |
| 63 if (event.type == MapNotify) { | 59 if (event.type == MapNotify) { |
| 64 break; | 60 break; |
| 65 } | 61 } |
| 66 } | 62 } |
| 67 XFlush(display_->display()); | 63 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); | |
| 81 context_ = DefaultGC(display_->display(), screen_num_); | 64 context_ = DefaultGC(display_->display(), screen_num_); |
| 82 colormap_ = DefaultColormap(display_->display(), screen_num_); | 65 colormap_ = DefaultColormap(display_->display(), screen_num_); |
| 83 // Wait for window animations. | |
| 84 SleepMs(200); | |
| 85 } | 66 } |
| 86 | 67 |
| 87 ScreenDrawerLinux::~ScreenDrawerLinux() { | 68 ScreenDrawerLinux::~ScreenDrawerLinux() { |
| 88 XUnmapWindow(display_->display(), window_); | 69 XUnmapWindow(display_->display(), window_); |
| 89 XDestroyWindow(display_->display(), window_); | 70 XDestroyWindow(display_->display(), window_); |
| 90 } | 71 } |
| 91 | 72 |
| 92 DesktopRect ScreenDrawerLinux::DrawableRegion() { | 73 DesktopRect ScreenDrawerLinux::DrawableRegion() { |
| 93 return rect_; | 74 return rect_; |
| 94 } | 75 } |
| 95 | 76 |
| 96 void ScreenDrawerLinux::DrawRectangle(DesktopRect rect, RgbaColor color) { | 77 void ScreenDrawerLinux::DrawRectangle(DesktopRect rect, uint32_t rgba) { |
| 97 rect.Translate(-rect_.left(), -rect_.top()); | 78 int r = (rgba & 0xff00) >> 8; |
| 98 XColor xcolor; | 79 int g = (rgba & 0xff0000) >> 16; |
| 80 int b = (rgba & 0xff000000) >> 24; |
| 99 // X11 does not support Alpha. | 81 // X11 does not support Alpha. |
| 100 // X11 uses 16 bits for each primary color, so we need to slightly normalize | 82 XColor color; |
| 101 // a 8 bits channel to 16 bits channel, by setting the low 8 bits as its high | 83 // X11 uses 16 bits for each primary color. |
| 102 // 8 bits to avoid a mismatch of color returned by capturer. | 84 color.red = r * 256; |
| 103 xcolor.red = (color.red << 8) + color.red; | 85 color.green = g * 256; |
| 104 xcolor.green = (color.green << 8) + color.green; | 86 color.blue = b * 256; |
| 105 xcolor.blue = (color.blue << 8) + color.blue; | 87 color.flags = DoRed | DoGreen | DoBlue; |
| 106 xcolor.flags = DoRed | DoGreen | DoBlue; | 88 XAllocColor(display_->display(), colormap_, &color); |
| 107 XAllocColor(display_->display(), colormap_, &xcolor); | 89 XSetForeground(display_->display(), context_, color.pixel); |
| 108 XSetForeground(display_->display(), context_, xcolor.pixel); | |
| 109 XFillRectangle(display_->display(), window_, context_, rect.left(), | 90 XFillRectangle(display_->display(), window_, context_, rect.left(), |
| 110 rect.top(), rect.width(), rect.height()); | 91 rect.top(), rect.width(), rect.height()); |
| 111 XFlush(display_->display()); | 92 XFlush(display_->display()); |
| 112 } | 93 } |
| 113 | 94 |
| 114 void ScreenDrawerLinux::Clear() { | 95 void ScreenDrawerLinux::Clear() { |
| 115 DrawRectangle(rect_, RgbaColor(0, 0, 0)); | 96 DrawRectangle(DrawableRegion(), 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); | |
| 122 } | 97 } |
| 123 | 98 |
| 124 } // namespace | 99 } // namespace |
| 125 | 100 |
| 126 // static | 101 // static |
| 127 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { | 102 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { |
| 128 if (SharedXDisplay::CreateDefault().get()) { | 103 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerLinux()); |
| 129 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerLinux()); | |
| 130 } | |
| 131 return nullptr; | |
| 132 } | 104 } |
| 133 | 105 |
| 134 } // namespace webrtc | 106 } // namespace webrtc |
| OLD | NEW |