Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2016 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 #include <memory> | |
| 12 | |
| 13 #include "webrtc/base/checks.h" | |
| 14 #include "webrtc/modules/desktop_capture/screen_drawer.h" | |
| 15 #include "webrtc/modules/desktop_capture/x11/shared_x_display.h" | |
| 16 | |
| 17 namespace webrtc { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 int XErrorHandler(Display* display, XErrorEvent* event) { | |
| 22 RTC_CHECK(false) << "XError"; | |
| 23 } | |
| 24 | |
| 25 int XIOErrorHandler(Display* display) { | |
| 26 RTC_CHECK(false) << "XIOError"; | |
| 27 } | |
| 28 | |
| 29 // A ScreenDrawer implementation for X11. | |
| 30 class ScreenDrawerLinux : public ScreenDrawer { | |
| 31 public: | |
| 32 ScreenDrawerLinux(); | |
| 33 ~ScreenDrawerLinux() override; | |
| 34 | |
| 35 // ScreenDrawer interface. | |
| 36 DesktopRect DrawableRegion() override; | |
| 37 void DrawRectangle(DesktopRect rect, uint32_t rgba) override; | |
| 38 | |
| 39 private: | |
| 40 rtc::scoped_refptr<SharedXDisplay> display_; | |
| 41 Screen* screen_; | |
| 42 int screen_num_; | |
| 43 DesktopRect rect_; | |
| 44 Window window_; | |
| 45 GC context_; | |
| 46 Colormap colormap_; | |
| 47 }; | |
| 48 | |
| 49 ScreenDrawerLinux::ScreenDrawerLinux() { | |
| 50 XSetErrorHandler(&XErrorHandler); | |
| 51 XSetIOErrorHandler(&XIOErrorHandler); | |
|
Sergey Ulanov
2016/08/15 21:41:09
I don't think these are useful, since the default
Hzj_jie
2016/08/15 23:47:18
Done.
| |
| 52 display_ = SharedXDisplay::CreateDefault(); | |
| 53 RTC_CHECK(display_.get()); | |
| 54 screen_ = DefaultScreenOfDisplay(display_->display()); | |
| 55 RTC_CHECK(screen_); | |
| 56 screen_num_ = DefaultScreen(display_->display()); | |
| 57 rect_ = DesktopRect::MakeWH(screen_->width, screen_->height); | |
| 58 window_ = XCreateSimpleWindow(display_->display(), | |
| 59 RootWindow(display_->display(), screen_num_), 0, | |
| 60 0, rect_.width(), rect_.height(), 0, | |
| 61 BlackPixel(display_->display(), screen_num_), | |
| 62 BlackPixel(display_->display(), screen_num_)); | |
| 63 XSelectInput(display_->display(), window_, StructureNotifyMask); | |
| 64 XMapWindow(display_->display(), window_); | |
| 65 while (true) { | |
| 66 XEvent event; | |
| 67 XNextEvent(display_->display(), &event); | |
| 68 if (event.type == MapNotify) { | |
| 69 break; | |
| 70 } | |
| 71 } | |
| 72 XFlush(display_->display()); | |
| 73 context_ = DefaultGC(display_->display(), screen_num_); | |
| 74 colormap_ = DefaultColormap(display_->display(), screen_num_); | |
| 75 } | |
| 76 | |
| 77 ScreenDrawerLinux::~ScreenDrawerLinux() { | |
| 78 XUnmapWindow(display_->display(), window_); | |
| 79 XDestroyWindow(display_->display(), window_); | |
| 80 } | |
| 81 | |
| 82 DesktopRect ScreenDrawerLinux::DrawableRegion() { | |
| 83 return rect_; | |
| 84 } | |
| 85 | |
| 86 void ScreenDrawerLinux::DrawRectangle(DesktopRect rect, uint32_t rgba) { | |
| 87 int r = (rgba & 0xff00) >> 8; | |
| 88 int g = (rgba & 0xff0000) >> 16; | |
| 89 int b = (rgba & 0xff000000) >> 24; | |
| 90 // X11 does not support Alpha. | |
| 91 XColor color; | |
| 92 // X11 uses 16 bits for each primary color. | |
| 93 color.red = r * 256; | |
| 94 color.green = g * 256; | |
| 95 color.blue = b * 256; | |
| 96 color.flags = DoRed | DoGreen | DoBlue; | |
| 97 XAllocColor(display_->display(), colormap_, &color); | |
| 98 XSetForeground(display_->display(), context_, color.pixel); | |
| 99 XFillRectangle(display_->display(), window_, context_, rect.left(), | |
| 100 rect.top(), rect.width(), rect.height()); | |
| 101 XFlush(display_->display()); | |
| 102 } | |
| 103 | |
| 104 } // namespace | |
| 105 | |
| 106 // static | |
| 107 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { | |
| 108 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerLinux()); | |
| 109 } | |
| 110 | |
| 111 } // namespace webrtc | |
| OLD | NEW |