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 | 17 |
17 namespace webrtc { | 18 namespace webrtc { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 DesktopRect GetScreenRect() { | 22 DesktopRect GetScreenRect() { |
22 HDC hdc = GetDC(NULL); | 23 HDC hdc = GetDC(NULL); |
23 DesktopRect rect = DesktopRect::MakeWH(GetDeviceCaps(hdc, HORZRES), | 24 DesktopRect rect = DesktopRect::MakeWH(GetDeviceCaps(hdc, HORZRES), |
24 GetDeviceCaps(hdc, VERTRES)); | 25 GetDeviceCaps(hdc, VERTRES)); |
25 ReleaseDC(NULL, hdc); | 26 ReleaseDC(NULL, hdc); |
26 return rect; | 27 return rect; |
27 } | 28 } |
28 | 29 |
29 HWND CreateDrawerWindow(DesktopRect rect) { | 30 HWND CreateDrawerWindow(DesktopRect rect) { |
30 HWND hwnd = CreateWindowA( | 31 HWND hwnd = CreateWindowA( |
31 "STATIC", "DrawerWindow", WS_POPUPWINDOW | WS_VISIBLE, rect.left(), | 32 "STATIC", "DrawerWindow", WS_POPUPWINDOW | WS_VISIBLE, rect.left(), |
32 rect.top(), rect.width(), rect.height(), NULL, NULL, NULL, NULL); | 33 rect.top(), rect.width(), rect.height(), NULL, NULL, NULL, NULL); |
33 SetForegroundWindow(hwnd); | 34 SetForegroundWindow(hwnd); |
34 return hwnd; | 35 return hwnd; |
35 } | 36 } |
36 | 37 |
| 38 COLORREF ColorToRef(RgbaColor color) { |
| 39 // Windows device context does not support alpha. |
| 40 return RGB(color.red, color.green, color.blue); |
| 41 } |
| 42 |
37 // A ScreenDrawer implementation for Windows. | 43 // A ScreenDrawer implementation for Windows. |
38 class ScreenDrawerWin : public ScreenDrawer { | 44 class ScreenDrawerWin : public ScreenDrawer { |
39 public: | 45 public: |
40 ScreenDrawerWin(); | 46 ScreenDrawerWin(); |
41 ~ScreenDrawerWin() override; | 47 ~ScreenDrawerWin() override; |
42 | 48 |
43 // ScreenDrawer interface. | 49 // ScreenDrawer interface. |
44 DesktopRect DrawableRegion() override; | 50 DesktopRect DrawableRegion() override; |
45 void DrawRectangle(DesktopRect rect, uint32_t rgba) override; | 51 void DrawRectangle(DesktopRect rect, RgbaColor color) override; |
46 void Clear() override; | 52 void Clear() override; |
| 53 void WaitForPendingDraws() override; |
47 | 54 |
48 private: | 55 private: |
| 56 // Draw a line with |color|. |
| 57 void DrawLine(DesktopVector start, DesktopVector end, RgbaColor color); |
| 58 |
| 59 // Draw a dot with |color|. |
| 60 void DrawDot(DesktopVector vect, RgbaColor color); |
| 61 |
49 const DesktopRect rect_; | 62 const DesktopRect rect_; |
50 HWND window_; | 63 HWND window_; |
51 HDC hdc_; | 64 HDC hdc_; |
52 }; | 65 }; |
53 | 66 |
54 ScreenDrawerWin::ScreenDrawerWin() | 67 ScreenDrawerWin::ScreenDrawerWin() |
55 : ScreenDrawer(), | 68 : ScreenDrawer(), |
56 rect_(GetScreenRect()), | 69 rect_(GetScreenRect()), |
57 window_(CreateDrawerWindow(rect_)), | 70 window_(CreateDrawerWindow(rect_)), |
58 hdc_(GetWindowDC(window_)) { | 71 hdc_(GetWindowDC(window_)) { |
59 // We do not need to handle any messages for the |window_|, so disable Windows | 72 // We do not need to handle any messages for the |window_|, so disable Windows |
60 // process windows ghosting feature. | 73 // from processing windows ghosting feature. |
61 DisableProcessWindowsGhosting(); | 74 DisableProcessWindowsGhosting(); |
| 75 |
| 76 // Always use stock pen (DC_PEN) and brush (DC_BRUSH). |
| 77 SelectObject(hdc_, GetStockObject(DC_PEN)); |
| 78 SelectObject(hdc_, GetStockObject(DC_BRUSH)); |
62 } | 79 } |
63 | 80 |
64 ScreenDrawerWin::~ScreenDrawerWin() { | 81 ScreenDrawerWin::~ScreenDrawerWin() { |
65 ReleaseDC(NULL, hdc_); | 82 ReleaseDC(NULL, hdc_); |
66 DestroyWindow(window_); | 83 DestroyWindow(window_); |
67 // Unfortunately there is no EnableProcessWindowsGhosting() API. | 84 // Unfortunately there is no EnableProcessWindowsGhosting() API. |
68 } | 85 } |
69 | 86 |
70 DesktopRect ScreenDrawerWin::DrawableRegion() { | 87 DesktopRect ScreenDrawerWin::DrawableRegion() { |
71 return rect_; | 88 return rect_; |
72 } | 89 } |
73 | 90 |
74 void ScreenDrawerWin::DrawRectangle(DesktopRect rect, uint32_t rgba) { | 91 void ScreenDrawerWin::DrawRectangle(DesktopRect rect, RgbaColor color) { |
75 int r = (rgba & 0xff00) >> 8; | 92 if (rect.width() == 1 && rect.height() == 1) { |
76 int g = (rgba & 0xff0000) >> 16; | 93 // Rectangle function cannot draw a 1 pixel rectangle. |
77 int b = (rgba & 0xff000000) >> 24; | 94 DrawDot(rect.top_left(), color); |
78 // Windows device context does not support Alpha. | 95 return; |
79 SelectObject(hdc_, GetStockObject(DC_PEN)); | 96 } |
80 SelectObject(hdc_, GetStockObject(DC_BRUSH)); | 97 |
81 SetDCBrushColor(hdc_, RGB(r, g, b)); | 98 if (rect.width() == 1 || rect.height() == 1) { |
82 SetDCPenColor(hdc_, RGB(r, g, b)); | 99 // Rectangle function cannot draw a 1 pixel rectangle. |
| 100 DrawLine(rect.top_left(), DesktopVector(rect.right(), rect.bottom()), |
| 101 color); |
| 102 return; |
| 103 } |
| 104 |
| 105 SetDCBrushColor(hdc_, ColorToRef(color)); |
| 106 SetDCPenColor(hdc_, ColorToRef(color)); |
83 Rectangle(hdc_, rect.left(), rect.top(), rect.right(), rect.bottom()); | 107 Rectangle(hdc_, rect.left(), rect.top(), rect.right(), rect.bottom()); |
84 } | 108 } |
85 | 109 |
86 void ScreenDrawerWin::Clear() { | 110 void ScreenDrawerWin::Clear() { |
87 DrawRectangle(DrawableRegion(), 0); | 111 DrawRectangle(rect_, RgbaColor(0, 0, 0)); |
| 112 } |
| 113 |
| 114 // TODO(zijiehe): Find the right signal to indicate the finish of all pending |
| 115 // paintings. |
| 116 void ScreenDrawerWin::WaitForPendingDraws() { |
| 117 SleepMs(50); |
| 118 } |
| 119 |
| 120 void ScreenDrawerWin::DrawLine(DesktopVector start, |
| 121 DesktopVector end, |
| 122 RgbaColor color) { |
| 123 POINT points[2]; |
| 124 points[0].x = start.x(); |
| 125 points[0].y = start.y(); |
| 126 points[1].x = end.x(); |
| 127 points[1].y = end.y(); |
| 128 SetDCPenColor(hdc_, ColorToRef(color)); |
| 129 Polyline(hdc_, points, 2); |
| 130 } |
| 131 |
| 132 void ScreenDrawerWin::DrawDot(DesktopVector vect, RgbaColor color) { |
| 133 SetPixel(hdc_, vect.x(), vect.y(), ColorToRef(color)); |
88 } | 134 } |
89 | 135 |
90 } // namespace | 136 } // namespace |
91 | 137 |
92 // static | 138 // static |
93 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { | 139 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { |
94 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerWin()); | 140 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerWin()); |
95 } | 141 } |
96 | 142 |
97 } // namespace webrtc | 143 } // namespace webrtc |
OLD | NEW |