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