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(Color 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, Color color) override; |
46 void Clear() override; | 52 void Clear() override; |
53 void WaitForPendingPaintings() override; | |
47 | 54 |
48 private: | 55 private: |
56 // Draw a line with |color|. | |
57 void DrawLine(DesktopVector start, DesktopVector end, Color color); | |
58 | |
59 // Draw a dot with |color|. | |
60 void DrawDot(DesktopVector vect, Color 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, Color 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. |
Jamie
2016/08/26 22:29:10
These special cases don't look right. The document
Hzj_jie
2016/08/29 21:57:28
The ScreenDrawer::DrawRectangle has exactly the sa
Jamie
2016/08/31 17:39:39
Thanks for the clarification.
| |
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 DesktopRect rect = |
112 DesktopRect::MakeWH(DrawableRegion().width(), DrawableRegion().height()); | |
113 DrawRectangle(rect, Color::FromBGR(0)); | |
114 } | |
115 | |
116 // TODO(zijiehe): Find the right signal to indicate the finish of all pending | |
117 // paintings. | |
118 void ScreenDrawerWin::WaitForPendingPaintings() { | |
119 // DirectX capturer reads data from GPU, so there is a certain delay before | |
120 // Windows sends the data to GPU. | |
121 SleepMs(100); | |
122 } | |
123 | |
124 void ScreenDrawerWin::DrawLine(DesktopVector start, | |
125 DesktopVector end, | |
126 Color color) { | |
127 POINT points[2]; | |
128 points[0].x = start.x(); | |
129 points[0].y = start.y(); | |
130 points[1].x = end.x(); | |
131 points[1].y = end.y(); | |
132 SetDCPenColor(hdc_, ColorToRef(color)); | |
133 Polyline(hdc_, points, 2); | |
134 } | |
135 | |
136 void ScreenDrawerWin::DrawDot(DesktopVector vect, Color color) { | |
137 SetPixel(hdc_, vect.x(), vect.y(), ColorToRef(color)); | |
88 } | 138 } |
89 | 139 |
90 } // namespace | 140 } // namespace |
91 | 141 |
92 // static | 142 // static |
93 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { | 143 std::unique_ptr<ScreenDrawer> ScreenDrawer::Create() { |
94 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerWin()); | 144 return std::unique_ptr<ScreenDrawer>(new ScreenDrawerWin()); |
95 } | 145 } |
96 | 146 |
97 } // namespace webrtc | 147 } // namespace webrtc |
OLD | NEW |