Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(472)

Side by Side Diff: webrtc/modules/desktop_capture/screen_drawer_linux.cc

Issue 2268093002: [WebRTC] A real ScreenCapturer test (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Resolve review comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // In two monitor system, if this window is on the first monitor, part of the
76 // DrawableRegion() may be covered by the root window on the second monitor.
77 rect_ = DesktopRect::MakeLTRB(x, y, root_attributes.width,
78 root_attributes.height);
64 context_ = DefaultGC(display_->display(), screen_num_); 79 context_ = DefaultGC(display_->display(), screen_num_);
65 colormap_ = DefaultColormap(display_->display(), screen_num_); 80 colormap_ = DefaultColormap(display_->display(), screen_num_);
81 // Wait for window animations.
82 SleepMs(200);
66 } 83 }
67 84
68 ScreenDrawerLinux::~ScreenDrawerLinux() { 85 ScreenDrawerLinux::~ScreenDrawerLinux() {
69 XUnmapWindow(display_->display(), window_); 86 XUnmapWindow(display_->display(), window_);
70 XDestroyWindow(display_->display(), window_); 87 XDestroyWindow(display_->display(), window_);
71 } 88 }
72 89
73 DesktopRect ScreenDrawerLinux::DrawableRegion() { 90 DesktopRect ScreenDrawerLinux::DrawableRegion() {
74 return rect_; 91 return rect_;
75 } 92 }
76 93
77 void ScreenDrawerLinux::DrawRectangle(DesktopRect rect, uint32_t rgba) { 94 void ScreenDrawerLinux::DrawRectangle(DesktopRect rect, Color color) {
78 int r = (rgba & 0xff00) >> 8; 95 rect.Translate(-rect_.left(), -rect_.top());
79 int g = (rgba & 0xff0000) >> 16; 96 XColor xcolor;
80 int b = (rgba & 0xff000000) >> 24;
81 // X11 does not support Alpha. 97 // X11 does not support Alpha.
82 XColor color;
83 // X11 uses 16 bits for each primary color. 98 // X11 uses 16 bits for each primary color.
84 color.red = r * 256; 99 xcolor.red = color.red() * 256 + color.red();
85 color.green = g * 256; 100 xcolor.green = color.green() * 256 + color.green();
86 color.blue = b * 256; 101 xcolor.blue = color.blue() * 256 + color.blue();
87 color.flags = DoRed | DoGreen | DoBlue; 102 xcolor.flags = DoRed | DoGreen | DoBlue;
88 XAllocColor(display_->display(), colormap_, &color); 103 XAllocColor(display_->display(), colormap_, &xcolor);
89 XSetForeground(display_->display(), context_, color.pixel); 104 XSetForeground(display_->display(), context_, xcolor.pixel);
90 XFillRectangle(display_->display(), window_, context_, rect.left(), 105 XFillRectangle(display_->display(), window_, context_, rect.left(),
91 rect.top(), rect.width(), rect.height()); 106 rect.top(), rect.width(), rect.height());
92 XFlush(display_->display()); 107 XFlush(display_->display());
93 } 108 }
94 109
95 void ScreenDrawerLinux::Clear() { 110 void ScreenDrawerLinux::Clear() {
96 DrawRectangle(DrawableRegion(), 0); 111 DrawRectangle(rect_, Color(0, 0, 0));
112 }
113
114 // TODO(zijiehe): Find the right signal from X11 to indicate the finish of all
115 // pending paintings.
116 void ScreenDrawerLinux::WaitForPendingDraws() {
117 SleepMs(50);
97 } 118 }
98 119
99 } // namespace 120 } // namespace
100 121
101 // static 122 // static
102 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { 123 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() {
103 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerLinux()); 124 if (SharedXDisplay::CreateDefault().get()) {
125 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerLinux());
126 } else {
127 return nullptr;
128 }
104 } 129 }
105 130
106 } // namespace webrtc 131 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698