Chromium Code Reviews| 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 <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 // A cross application lock to ensure only one ScreenDrawerWin can be used at a | |
| 26 // certain time. | |
| 27 struct ScreenDrawerLock { | |
| 28 // Blocks current thread until a global lock is acquired. | |
| 29 ScreenDrawerLock(); | |
| 30 // Releases the global lock acquired. | |
| 31 ~ScreenDrawerLock(); | |
| 32 | |
| 33 private: | |
| 34 HANDLE mutex_; | |
| 35 }; | |
| 36 | |
| 37 ScreenDrawerLock::ScreenDrawerLock() { | |
|
Sergey Ulanov
2016/10/14 17:38:25
it looks like this code is duplicated between the
Hzj_jie
2016/10/19 00:46:47
Yes, part of the logic is shareable. I will make a
| |
| 38 while (true) { | |
| 39 mutex_ = CreateMutex(NULL, FALSE, kMutexName); | |
| 40 if (GetLastError() != ERROR_ALREADY_EXISTS && mutex_ != NULL) { | |
| 41 break; | |
| 42 } else { | |
| 43 if (mutex_) { | |
| 44 CloseHandle(mutex_); | |
| 45 } | |
| 46 SleepMs(1000); | |
| 47 } | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 ScreenDrawerLock::~ScreenDrawerLock() { | |
| 52 CloseHandle(mutex_); | |
| 53 } | |
| 54 | |
| 55 std::unique_ptr<ScreenDrawerLock> g_screen_drawer_lock; | |
| 56 | |
| 22 DesktopRect GetScreenRect() { | 57 DesktopRect GetScreenRect() { |
| 23 HDC hdc = GetDC(NULL); | 58 HDC hdc = GetDC(NULL); |
| 24 DesktopRect rect = DesktopRect::MakeWH(GetDeviceCaps(hdc, HORZRES), | 59 DesktopRect rect = DesktopRect::MakeWH(GetDeviceCaps(hdc, HORZRES), |
| 25 GetDeviceCaps(hdc, VERTRES)); | 60 GetDeviceCaps(hdc, VERTRES)); |
| 26 ReleaseDC(NULL, hdc); | 61 ReleaseDC(NULL, hdc); |
| 27 return rect; | 62 return rect; |
| 28 } | 63 } |
| 29 | 64 |
| 30 HWND CreateDrawerWindow(DesktopRect rect) { | 65 HWND CreateDrawerWindow(DesktopRect rect) { |
| 31 HWND hwnd = CreateWindowA( | 66 HWND hwnd = CreateWindowA( |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 44 class ScreenDrawerWin : public ScreenDrawer { | 79 class ScreenDrawerWin : public ScreenDrawer { |
| 45 public: | 80 public: |
| 46 ScreenDrawerWin(); | 81 ScreenDrawerWin(); |
| 47 ~ScreenDrawerWin() override; | 82 ~ScreenDrawerWin() override; |
| 48 | 83 |
| 49 // ScreenDrawer interface. | 84 // ScreenDrawer interface. |
| 50 DesktopRect DrawableRegion() override; | 85 DesktopRect DrawableRegion() override; |
| 51 void DrawRectangle(DesktopRect rect, RgbaColor color) override; | 86 void DrawRectangle(DesktopRect rect, RgbaColor color) override; |
| 52 void Clear() override; | 87 void Clear() override; |
| 53 void WaitForPendingDraws() override; | 88 void WaitForPendingDraws() override; |
| 89 bool MayDrawIncompleteShapes() override; | |
| 54 | 90 |
| 55 private: | 91 private: |
| 92 // Bring the window to the front, this can help to avoid the impact from other | |
| 93 // windows or shadow effects. | |
| 94 void BringToFront(); | |
| 95 | |
| 56 // Draw a line with |color|. | 96 // Draw a line with |color|. |
| 57 void DrawLine(DesktopVector start, DesktopVector end, RgbaColor color); | 97 void DrawLine(DesktopVector start, DesktopVector end, RgbaColor color); |
| 58 | 98 |
| 59 // Draw a dot with |color|. | 99 // Draw a dot with |color|. |
| 60 void DrawDot(DesktopVector vect, RgbaColor color); | 100 void DrawDot(DesktopVector vect, RgbaColor color); |
| 61 | 101 |
| 62 const DesktopRect rect_; | 102 const DesktopRect rect_; |
| 63 HWND window_; | 103 HWND window_; |
| 64 HDC hdc_; | 104 HDC hdc_; |
| 65 }; | 105 }; |
| 66 | 106 |
| 67 ScreenDrawerWin::ScreenDrawerWin() | 107 ScreenDrawerWin::ScreenDrawerWin() |
| 68 : ScreenDrawer(), | 108 : ScreenDrawer(), |
| 69 rect_(GetScreenRect()), | 109 rect_(GetScreenRect()), |
| 70 window_(CreateDrawerWindow(rect_)), | 110 window_(CreateDrawerWindow(rect_)), |
| 71 hdc_(GetWindowDC(window_)) { | 111 hdc_(GetWindowDC(window_)) { |
| 72 // We do not need to handle any messages for the |window_|, so disable Windows | 112 // We do not need to handle any messages for the |window_|, so disable Windows |
| 73 // from processing windows ghosting feature. | 113 // from processing windows ghosting feature. |
| 74 DisableProcessWindowsGhosting(); | 114 DisableProcessWindowsGhosting(); |
| 75 | 115 |
| 76 // Always use stock pen (DC_PEN) and brush (DC_BRUSH). | 116 // Always use stock pen (DC_PEN) and brush (DC_BRUSH). |
| 77 SelectObject(hdc_, GetStockObject(DC_PEN)); | 117 SelectObject(hdc_, GetStockObject(DC_PEN)); |
| 78 SelectObject(hdc_, GetStockObject(DC_BRUSH)); | 118 SelectObject(hdc_, GetStockObject(DC_BRUSH)); |
| 119 BringToFront(); | |
| 79 } | 120 } |
| 80 | 121 |
| 81 ScreenDrawerWin::~ScreenDrawerWin() { | 122 ScreenDrawerWin::~ScreenDrawerWin() { |
| 82 ReleaseDC(NULL, hdc_); | 123 ReleaseDC(NULL, hdc_); |
| 83 DestroyWindow(window_); | 124 DestroyWindow(window_); |
| 84 // Unfortunately there is no EnableProcessWindowsGhosting() API. | 125 // Unfortunately there is no EnableProcessWindowsGhosting() API. |
| 126 g_screen_drawer_lock.reset(); | |
| 85 } | 127 } |
| 86 | 128 |
| 87 DesktopRect ScreenDrawerWin::DrawableRegion() { | 129 DesktopRect ScreenDrawerWin::DrawableRegion() { |
| 88 return rect_; | 130 return rect_; |
| 89 } | 131 } |
| 90 | 132 |
| 91 void ScreenDrawerWin::DrawRectangle(DesktopRect rect, RgbaColor color) { | 133 void ScreenDrawerWin::DrawRectangle(DesktopRect rect, RgbaColor color) { |
| 92 if (rect.width() == 1 && rect.height() == 1) { | 134 if (rect.width() == 1 && rect.height() == 1) { |
| 93 // Rectangle function cannot draw a 1 pixel rectangle. | 135 // Rectangle function cannot draw a 1 pixel rectangle. |
| 94 DrawDot(rect.top_left(), color); | 136 DrawDot(rect.top_left(), color); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 107 Rectangle(hdc_, rect.left(), rect.top(), rect.right(), rect.bottom()); | 149 Rectangle(hdc_, rect.left(), rect.top(), rect.right(), rect.bottom()); |
| 108 } | 150 } |
| 109 | 151 |
| 110 void ScreenDrawerWin::Clear() { | 152 void ScreenDrawerWin::Clear() { |
| 111 DrawRectangle(rect_, RgbaColor(0, 0, 0)); | 153 DrawRectangle(rect_, RgbaColor(0, 0, 0)); |
| 112 } | 154 } |
| 113 | 155 |
| 114 // TODO(zijiehe): Find the right signal to indicate the finish of all pending | 156 // TODO(zijiehe): Find the right signal to indicate the finish of all pending |
| 115 // paintings. | 157 // paintings. |
| 116 void ScreenDrawerWin::WaitForPendingDraws() { | 158 void ScreenDrawerWin::WaitForPendingDraws() { |
| 159 BringToFront(); | |
| 117 SleepMs(50); | 160 SleepMs(50); |
| 118 } | 161 } |
| 119 | 162 |
| 163 bool ScreenDrawerWin::MayDrawIncompleteShapes() { | |
| 164 return true; | |
| 165 } | |
| 166 | |
| 120 void ScreenDrawerWin::DrawLine(DesktopVector start, | 167 void ScreenDrawerWin::DrawLine(DesktopVector start, |
| 121 DesktopVector end, | 168 DesktopVector end, |
| 122 RgbaColor color) { | 169 RgbaColor color) { |
| 123 POINT points[2]; | 170 POINT points[2]; |
| 124 points[0].x = start.x(); | 171 points[0].x = start.x(); |
| 125 points[0].y = start.y(); | 172 points[0].y = start.y(); |
| 126 points[1].x = end.x(); | 173 points[1].x = end.x(); |
| 127 points[1].y = end.y(); | 174 points[1].y = end.y(); |
| 128 SetDCPenColor(hdc_, ColorToRef(color)); | 175 SetDCPenColor(hdc_, ColorToRef(color)); |
| 129 Polyline(hdc_, points, 2); | 176 Polyline(hdc_, points, 2); |
| 130 } | 177 } |
| 131 | 178 |
| 132 void ScreenDrawerWin::DrawDot(DesktopVector vect, RgbaColor color) { | 179 void ScreenDrawerWin::DrawDot(DesktopVector vect, RgbaColor color) { |
| 133 SetPixel(hdc_, vect.x(), vect.y(), ColorToRef(color)); | 180 SetPixel(hdc_, vect.x(), vect.y(), ColorToRef(color)); |
| 134 } | 181 } |
| 135 | 182 |
| 183 void ScreenDrawerWin::BringToFront() { | |
| 184 BringWindowToTop(window_); | |
| 185 } | |
| 186 | |
| 136 } // namespace | 187 } // namespace |
| 137 | 188 |
| 138 // static | 189 // static |
| 139 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { | 190 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { |
| 191 g_screen_drawer_lock.reset(new ScreenDrawerLock()); | |
| 140 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerWin()); | 192 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerWin()); |
| 141 } | 193 } |
| 142 | 194 |
| 143 } // namespace webrtc | 195 } // namespace webrtc |
| OLD | NEW |