| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 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 <windows.h> | 11 #include <windows.h> |
| 12 | 12 |
| 13 #include <memory> | 13 #include <memory> |
| 14 | 14 |
| 15 #include "webrtc/modules/desktop_capture/screen_drawer.h" | 15 #include "webrtc/modules/desktop_capture/screen_drawer.h" |
| 16 #include "webrtc/system_wrappers/include/sleep.h" | 16 #include "webrtc/system_wrappers/include/sleep.h" |
| 17 | 17 |
| 18 namespace webrtc { | 18 namespace webrtc { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 static constexpr TCHAR kMutexName[] = |
| 23 TEXT("Local\\ScreenDrawerWin-da834f82-8044-11e6-ac81-73dcdd1c1869"); |
| 24 |
| 25 class ScreenDrawerLockWin : public ScreenDrawerLock { |
| 26 public: |
| 27 ScreenDrawerLockWin(); |
| 28 ~ScreenDrawerLockWin(); |
| 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 |
| 22 DesktopRect GetScreenRect() { | 52 DesktopRect GetScreenRect() { |
| 23 HDC hdc = GetDC(NULL); | 53 HDC hdc = GetDC(NULL); |
| 24 DesktopRect rect = DesktopRect::MakeWH(GetDeviceCaps(hdc, HORZRES), | 54 DesktopRect rect = DesktopRect::MakeWH(GetDeviceCaps(hdc, HORZRES), |
| 25 GetDeviceCaps(hdc, VERTRES)); | 55 GetDeviceCaps(hdc, VERTRES)); |
| 26 ReleaseDC(NULL, hdc); | 56 ReleaseDC(NULL, hdc); |
| 27 return rect; | 57 return rect; |
| 28 } | 58 } |
| 29 | 59 |
| 30 HWND CreateDrawerWindow(DesktopRect rect) { | 60 HWND CreateDrawerWindow(DesktopRect rect) { |
| 31 HWND hwnd = CreateWindowA( | 61 HWND hwnd = CreateWindowA( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 44 class ScreenDrawerWin : public ScreenDrawer { | 74 class ScreenDrawerWin : public ScreenDrawer { |
| 45 public: | 75 public: |
| 46 ScreenDrawerWin(); | 76 ScreenDrawerWin(); |
| 47 ~ScreenDrawerWin() override; | 77 ~ScreenDrawerWin() override; |
| 48 | 78 |
| 49 // ScreenDrawer interface. | 79 // ScreenDrawer interface. |
| 50 DesktopRect DrawableRegion() override; | 80 DesktopRect DrawableRegion() override; |
| 51 void DrawRectangle(DesktopRect rect, RgbaColor color) override; | 81 void DrawRectangle(DesktopRect rect, RgbaColor color) override; |
| 52 void Clear() override; | 82 void Clear() override; |
| 53 void WaitForPendingDraws() override; | 83 void WaitForPendingDraws() override; |
| 84 bool MayDrawIncompleteShapes() override; |
| 54 | 85 |
| 55 private: | 86 private: |
| 87 // Bring the window to the front, this can help to avoid the impact from other |
| 88 // windows or shadow effects. |
| 89 void BringToFront(); |
| 90 |
| 56 // Draw a line with |color|. | 91 // Draw a line with |color|. |
| 57 void DrawLine(DesktopVector start, DesktopVector end, RgbaColor color); | 92 void DrawLine(DesktopVector start, DesktopVector end, RgbaColor color); |
| 58 | 93 |
| 59 // Draw a dot with |color|. | 94 // Draw a dot with |color|. |
| 60 void DrawDot(DesktopVector vect, RgbaColor color); | 95 void DrawDot(DesktopVector vect, RgbaColor color); |
| 61 | 96 |
| 62 const DesktopRect rect_; | 97 const DesktopRect rect_; |
| 63 HWND window_; | 98 HWND window_; |
| 64 HDC hdc_; | 99 HDC hdc_; |
| 65 }; | 100 }; |
| 66 | 101 |
| 67 ScreenDrawerWin::ScreenDrawerWin() | 102 ScreenDrawerWin::ScreenDrawerWin() |
| 68 : ScreenDrawer(), | 103 : ScreenDrawer(), |
| 69 rect_(GetScreenRect()), | 104 rect_(GetScreenRect()), |
| 70 window_(CreateDrawerWindow(rect_)), | 105 window_(CreateDrawerWindow(rect_)), |
| 71 hdc_(GetWindowDC(window_)) { | 106 hdc_(GetWindowDC(window_)) { |
| 72 // We do not need to handle any messages for the |window_|, so disable Windows | 107 // We do not need to handle any messages for the |window_|, so disable Windows |
| 73 // from processing windows ghosting feature. | 108 // from processing windows ghosting feature. |
| 74 DisableProcessWindowsGhosting(); | 109 DisableProcessWindowsGhosting(); |
| 75 | 110 |
| 76 // Always use stock pen (DC_PEN) and brush (DC_BRUSH). | 111 // Always use stock pen (DC_PEN) and brush (DC_BRUSH). |
| 77 SelectObject(hdc_, GetStockObject(DC_PEN)); | 112 SelectObject(hdc_, GetStockObject(DC_PEN)); |
| 78 SelectObject(hdc_, GetStockObject(DC_BRUSH)); | 113 SelectObject(hdc_, GetStockObject(DC_BRUSH)); |
| 114 BringToFront(); |
| 79 } | 115 } |
| 80 | 116 |
| 81 ScreenDrawerWin::~ScreenDrawerWin() { | 117 ScreenDrawerWin::~ScreenDrawerWin() { |
| 82 ReleaseDC(NULL, hdc_); | 118 ReleaseDC(NULL, hdc_); |
| 83 DestroyWindow(window_); | 119 DestroyWindow(window_); |
| 84 // Unfortunately there is no EnableProcessWindowsGhosting() API. | 120 // Unfortunately there is no EnableProcessWindowsGhosting() API. |
| 85 } | 121 } |
| 86 | 122 |
| 87 DesktopRect ScreenDrawerWin::DrawableRegion() { | 123 DesktopRect ScreenDrawerWin::DrawableRegion() { |
| 88 return rect_; | 124 return rect_; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 107 Rectangle(hdc_, rect.left(), rect.top(), rect.right(), rect.bottom()); | 143 Rectangle(hdc_, rect.left(), rect.top(), rect.right(), rect.bottom()); |
| 108 } | 144 } |
| 109 | 145 |
| 110 void ScreenDrawerWin::Clear() { | 146 void ScreenDrawerWin::Clear() { |
| 111 DrawRectangle(rect_, RgbaColor(0, 0, 0)); | 147 DrawRectangle(rect_, RgbaColor(0, 0, 0)); |
| 112 } | 148 } |
| 113 | 149 |
| 114 // TODO(zijiehe): Find the right signal to indicate the finish of all pending | 150 // TODO(zijiehe): Find the right signal to indicate the finish of all pending |
| 115 // paintings. | 151 // paintings. |
| 116 void ScreenDrawerWin::WaitForPendingDraws() { | 152 void ScreenDrawerWin::WaitForPendingDraws() { |
| 153 BringToFront(); |
| 117 SleepMs(50); | 154 SleepMs(50); |
| 118 } | 155 } |
| 119 | 156 |
| 157 bool ScreenDrawerWin::MayDrawIncompleteShapes() { |
| 158 return true; |
| 159 } |
| 160 |
| 120 void ScreenDrawerWin::DrawLine(DesktopVector start, | 161 void ScreenDrawerWin::DrawLine(DesktopVector start, |
| 121 DesktopVector end, | 162 DesktopVector end, |
| 122 RgbaColor color) { | 163 RgbaColor color) { |
| 123 POINT points[2]; | 164 POINT points[2]; |
| 124 points[0].x = start.x(); | 165 points[0].x = start.x(); |
| 125 points[0].y = start.y(); | 166 points[0].y = start.y(); |
| 126 points[1].x = end.x(); | 167 points[1].x = end.x(); |
| 127 points[1].y = end.y(); | 168 points[1].y = end.y(); |
| 128 SetDCPenColor(hdc_, ColorToRef(color)); | 169 SetDCPenColor(hdc_, ColorToRef(color)); |
| 129 Polyline(hdc_, points, 2); | 170 Polyline(hdc_, points, 2); |
| 130 } | 171 } |
| 131 | 172 |
| 132 void ScreenDrawerWin::DrawDot(DesktopVector vect, RgbaColor color) { | 173 void ScreenDrawerWin::DrawDot(DesktopVector vect, RgbaColor color) { |
| 133 SetPixel(hdc_, vect.x(), vect.y(), ColorToRef(color)); | 174 SetPixel(hdc_, vect.x(), vect.y(), ColorToRef(color)); |
| 134 } | 175 } |
| 135 | 176 |
| 177 void ScreenDrawerWin::BringToFront() { |
| 178 BringWindowToTop(window_); |
| 179 } |
| 180 |
| 136 } // namespace | 181 } // namespace |
| 137 | 182 |
| 138 // static | 183 // static |
| 184 std::unique_ptr<ScreenDrawerLock> ScreenDrawerLock::Create() { |
| 185 return std::unique_ptr<ScreenDrawerLock>(new ScreenDrawerLockWin()); |
| 186 } |
| 187 |
| 188 // static |
| 139 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { | 189 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { |
| 140 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerWin()); | 190 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerWin()); |
| 141 } | 191 } |
| 142 | 192 |
| 143 } // namespace webrtc | 193 } // namespace webrtc |
| OLD | NEW |