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