zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 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 <windows.h> |
| 12 | |
| 13 | #include <memory> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "modules/desktop_capture/screen_drawer.h" |
| 16 | #include "system_wrappers/include/sleep.h" |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | namespace { |
| 21 | |
zijiehe | 6a4607e | 2016-10-18 18:22:18 -0700 | [diff] [blame] | 22 | static constexpr TCHAR kMutexName[] = |
| 23 | TEXT("Local\\ScreenDrawerWin-da834f82-8044-11e6-ac81-73dcdd1c1869"); |
| 24 | |
| 25 | class ScreenDrawerLockWin : public ScreenDrawerLock { |
| 26 | public: |
| 27 | ScreenDrawerLockWin(); |
zijiehe | 6be0a65 | 2016-10-27 16:50:35 -0700 | [diff] [blame] | 28 | ~ScreenDrawerLockWin() override; |
zijiehe | 6a4607e | 2016-10-18 18:22:18 -0700 | [diff] [blame] | 29 | |
| 30 | private: |
| 31 | HANDLE mutex_; |
| 32 | }; |
| 33 | |
| 34 | ScreenDrawerLockWin::ScreenDrawerLockWin() { |
| 35 | while (true) { |
| 36 | mutex_ = CreateMutex(NULL, FALSE, kMutexName); |
| 37 | if (GetLastError() != ERROR_ALREADY_EXISTS && mutex_ != NULL) { |
| 38 | break; |
| 39 | } else { |
| 40 | if (mutex_) { |
| 41 | CloseHandle(mutex_); |
| 42 | } |
| 43 | SleepMs(1000); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | ScreenDrawerLockWin::~ScreenDrawerLockWin() { |
| 49 | CloseHandle(mutex_); |
| 50 | } |
| 51 | |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 52 | DesktopRect GetScreenRect() { |
| 53 | HDC hdc = GetDC(NULL); |
| 54 | DesktopRect rect = DesktopRect::MakeWH(GetDeviceCaps(hdc, HORZRES), |
| 55 | GetDeviceCaps(hdc, VERTRES)); |
| 56 | ReleaseDC(NULL, hdc); |
| 57 | return rect; |
| 58 | } |
| 59 | |
| 60 | HWND CreateDrawerWindow(DesktopRect rect) { |
| 61 | HWND hwnd = CreateWindowA( |
| 62 | "STATIC", "DrawerWindow", WS_POPUPWINDOW | WS_VISIBLE, rect.left(), |
| 63 | rect.top(), rect.width(), rect.height(), NULL, NULL, NULL, NULL); |
| 64 | SetForegroundWindow(hwnd); |
| 65 | return hwnd; |
| 66 | } |
| 67 | |
zijiehe | 0f49dac | 2016-09-07 11:52:25 -0700 | [diff] [blame] | 68 | COLORREF ColorToRef(RgbaColor color) { |
| 69 | // Windows device context does not support alpha. |
| 70 | return RGB(color.red, color.green, color.blue); |
| 71 | } |
| 72 | |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 73 | // A ScreenDrawer implementation for Windows. |
| 74 | class ScreenDrawerWin : public ScreenDrawer { |
| 75 | public: |
| 76 | ScreenDrawerWin(); |
| 77 | ~ScreenDrawerWin() override; |
| 78 | |
| 79 | // ScreenDrawer interface. |
| 80 | DesktopRect DrawableRegion() override; |
zijiehe | 0f49dac | 2016-09-07 11:52:25 -0700 | [diff] [blame] | 81 | void DrawRectangle(DesktopRect rect, RgbaColor color) override; |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 82 | void Clear() override; |
zijiehe | 0f49dac | 2016-09-07 11:52:25 -0700 | [diff] [blame] | 83 | void WaitForPendingDraws() override; |
zijiehe | 6a4607e | 2016-10-18 18:22:18 -0700 | [diff] [blame] | 84 | bool MayDrawIncompleteShapes() override; |
Zijie He | 77b7a1d | 2017-09-01 15:51:14 -0700 | [diff] [blame] | 85 | WindowId window_id() const override; |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 86 | |
| 87 | private: |
zijiehe | 6a4607e | 2016-10-18 18:22:18 -0700 | [diff] [blame] | 88 | // Bring the window to the front, this can help to avoid the impact from other |
| 89 | // windows or shadow effects. |
| 90 | void BringToFront(); |
| 91 | |
zijiehe | 0f49dac | 2016-09-07 11:52:25 -0700 | [diff] [blame] | 92 | // Draw a line with |color|. |
| 93 | void DrawLine(DesktopVector start, DesktopVector end, RgbaColor color); |
| 94 | |
| 95 | // Draw a dot with |color|. |
| 96 | void DrawDot(DesktopVector vect, RgbaColor color); |
| 97 | |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 98 | const DesktopRect rect_; |
| 99 | HWND window_; |
| 100 | HDC hdc_; |
| 101 | }; |
| 102 | |
| 103 | ScreenDrawerWin::ScreenDrawerWin() |
| 104 | : ScreenDrawer(), |
| 105 | rect_(GetScreenRect()), |
| 106 | window_(CreateDrawerWindow(rect_)), |
| 107 | hdc_(GetWindowDC(window_)) { |
| 108 | // We do not need to handle any messages for the |window_|, so disable Windows |
zijiehe | 0f49dac | 2016-09-07 11:52:25 -0700 | [diff] [blame] | 109 | // from processing windows ghosting feature. |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 110 | DisableProcessWindowsGhosting(); |
zijiehe | 0f49dac | 2016-09-07 11:52:25 -0700 | [diff] [blame] | 111 | |
| 112 | // Always use stock pen (DC_PEN) and brush (DC_BRUSH). |
| 113 | SelectObject(hdc_, GetStockObject(DC_PEN)); |
| 114 | SelectObject(hdc_, GetStockObject(DC_BRUSH)); |
zijiehe | 6a4607e | 2016-10-18 18:22:18 -0700 | [diff] [blame] | 115 | BringToFront(); |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | ScreenDrawerWin::~ScreenDrawerWin() { |
| 119 | ReleaseDC(NULL, hdc_); |
| 120 | DestroyWindow(window_); |
| 121 | // Unfortunately there is no EnableProcessWindowsGhosting() API. |
| 122 | } |
| 123 | |
| 124 | DesktopRect ScreenDrawerWin::DrawableRegion() { |
| 125 | return rect_; |
| 126 | } |
| 127 | |
zijiehe | 0f49dac | 2016-09-07 11:52:25 -0700 | [diff] [blame] | 128 | void ScreenDrawerWin::DrawRectangle(DesktopRect rect, RgbaColor color) { |
| 129 | if (rect.width() == 1 && rect.height() == 1) { |
| 130 | // Rectangle function cannot draw a 1 pixel rectangle. |
| 131 | DrawDot(rect.top_left(), color); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | if (rect.width() == 1 || rect.height() == 1) { |
| 136 | // Rectangle function cannot draw a 1 pixel rectangle. |
| 137 | DrawLine(rect.top_left(), DesktopVector(rect.right(), rect.bottom()), |
| 138 | color); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | SetDCBrushColor(hdc_, ColorToRef(color)); |
| 143 | SetDCPenColor(hdc_, ColorToRef(color)); |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 144 | Rectangle(hdc_, rect.left(), rect.top(), rect.right(), rect.bottom()); |
| 145 | } |
| 146 | |
| 147 | void ScreenDrawerWin::Clear() { |
zijiehe | 0f49dac | 2016-09-07 11:52:25 -0700 | [diff] [blame] | 148 | DrawRectangle(rect_, RgbaColor(0, 0, 0)); |
| 149 | } |
| 150 | |
| 151 | // TODO(zijiehe): Find the right signal to indicate the finish of all pending |
| 152 | // paintings. |
| 153 | void ScreenDrawerWin::WaitForPendingDraws() { |
zijiehe | 2184155 | 2016-11-18 20:31:02 -0800 | [diff] [blame] | 154 | BringToFront(); |
zijiehe | 0f49dac | 2016-09-07 11:52:25 -0700 | [diff] [blame] | 155 | SleepMs(50); |
| 156 | } |
| 157 | |
zijiehe | 6a4607e | 2016-10-18 18:22:18 -0700 | [diff] [blame] | 158 | bool ScreenDrawerWin::MayDrawIncompleteShapes() { |
| 159 | return true; |
| 160 | } |
| 161 | |
Zijie He | 77b7a1d | 2017-09-01 15:51:14 -0700 | [diff] [blame] | 162 | WindowId ScreenDrawerWin::window_id() const { |
| 163 | return reinterpret_cast<WindowId>(window_); |
| 164 | } |
| 165 | |
zijiehe | 0f49dac | 2016-09-07 11:52:25 -0700 | [diff] [blame] | 166 | void ScreenDrawerWin::DrawLine(DesktopVector start, |
| 167 | DesktopVector end, |
| 168 | RgbaColor color) { |
| 169 | POINT points[2]; |
| 170 | points[0].x = start.x(); |
| 171 | points[0].y = start.y(); |
| 172 | points[1].x = end.x(); |
| 173 | points[1].y = end.y(); |
| 174 | SetDCPenColor(hdc_, ColorToRef(color)); |
| 175 | Polyline(hdc_, points, 2); |
| 176 | } |
| 177 | |
| 178 | void ScreenDrawerWin::DrawDot(DesktopVector vect, RgbaColor color) { |
| 179 | SetPixel(hdc_, vect.x(), vect.y(), ColorToRef(color)); |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 180 | } |
| 181 | |
zijiehe | 6a4607e | 2016-10-18 18:22:18 -0700 | [diff] [blame] | 182 | void ScreenDrawerWin::BringToFront() { |
zijiehe | 84fbf9e | 2016-10-20 17:00:37 -0700 | [diff] [blame] | 183 | if (SetWindowPos(window_, HWND_TOPMOST, 0, 0, 0, 0, |
| 184 | SWP_NOMOVE | SWP_NOSIZE) != FALSE) { |
zijiehe | 6a4607e | 2016-10-18 18:22:18 -0700 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
| 188 | long ex_style = GetWindowLong(window_, GWL_EXSTYLE); |
| 189 | ex_style |= WS_EX_TOPMOST; |
| 190 | if (SetWindowLong(window_, GWL_EXSTYLE, ex_style) != 0) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | BringWindowToTop(window_); |
| 195 | } |
| 196 | |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 197 | } // namespace |
| 198 | |
| 199 | // static |
zijiehe | 6a4607e | 2016-10-18 18:22:18 -0700 | [diff] [blame] | 200 | std::unique_ptr<ScreenDrawerLock> ScreenDrawerLock::Create() { |
| 201 | return std::unique_ptr<ScreenDrawerLock>(new ScreenDrawerLockWin()); |
| 202 | } |
| 203 | |
| 204 | // static |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 205 | std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { |
| 206 | return std::unique_ptr<ScreenDrawer>(new ScreenDrawerWin()); |
| 207 | } |
| 208 | |
| 209 | } // namespace webrtc |