1 /*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/desktop_capture/cropping_window_capturer.h"
12
13 #include "modules/desktop_capture/win/screen_capture_utils.h"
14 #include "modules/desktop_capture/win/window_capture_utils.h"
15 #include "rtc_base/logging.h"
16 #include "rtc_base/win32.h"
17
18 namespace webrtc {
19
20 namespace {
21
22 // Used to pass input/output data during the EnumWindow call for verifying if
23 // the selected window is on top.
24 struct TopWindowVerifierContext {
TopWindowVerifierContextwebrtc::__anonf8f461f30111::TopWindowVerifierContext25 TopWindowVerifierContext(HWND selected_window,
26 HWND excluded_window,
27 DesktopRect selected_window_rect)
28 : selected_window(selected_window),
29 excluded_window(excluded_window),
30 selected_window_rect(selected_window_rect),
31 is_top_window(false) {
32 RTC_DCHECK_NE(selected_window, excluded_window);
33 }
34
35 const HWND selected_window;
36 const HWND excluded_window;
37 const DesktopRect selected_window_rect;
38 bool is_top_window;
39 };
40
41 // The function is called during EnumWindow for every window enumerated and is
42 // responsible for verifying if the selected window is on top.
TopWindowVerifier(HWND hwnd,LPARAM param)43 BOOL CALLBACK TopWindowVerifier(HWND hwnd, LPARAM param) {
44 TopWindowVerifierContext* context =
45 reinterpret_cast<TopWindowVerifierContext*>(param);
46
47 if (hwnd == context->selected_window) {
48 context->is_top_window = true;
49 return FALSE;
50 }
51
52 // Ignore the excluded window.
53 if (hwnd == context->excluded_window) {
54 return TRUE;
55 }
56
57 // Ignore hidden or minimized window.
58 if (IsIconic(hwnd) || !IsWindowVisible(hwnd)) {
59 return TRUE;
60 }
61
62 // Ignore descendant windows since we want to capture them.
63 // This check does not work for tooltips and context menus. Drop down menus
64 // and popup windows are fine.
65 //
66 // GA_ROOT returns the root window instead of the owner. I.e. for a dialog
67 // window, GA_ROOT returns the dialog window itself. GA_ROOTOWNER returns the
68 // application main window which opens the dialog window. Since we are sharing
69 // the application main window, GA_ROOT should be used here.
70 if (GetAncestor(hwnd, GA_ROOT) == context->selected_window) {
71 return TRUE;
72 }
73
74 // If |hwnd| has no title and belongs to the same process, assume it's a
75 // tooltip or context menu from the selected window and ignore it.
76 // TODO(zijiehe): This check cannot cover the case where tooltip or context
77 // menu of the child-window is covering the main window. See
78 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8062 for details.
79 const size_t kTitleLength = 32;
80 WCHAR window_title[kTitleLength];
81 GetWindowText(hwnd, window_title, kTitleLength);
82 if (wcsnlen_s(window_title, kTitleLength) == 0) {
83 DWORD enumerated_window_process_id;
84 DWORD selected_window_process_id;
85 GetWindowThreadProcessId(hwnd, &enumerated_window_process_id);
86 GetWindowThreadProcessId(context->selected_window,
87 &selected_window_process_id);
88 if (selected_window_process_id == enumerated_window_process_id) {
89 return TRUE;
90 }
91 }
92
93 // Checks whether current window |hwnd| intersects with
94 // |context|->selected_window.
95 // |content_rect| is preferred because,
96 // 1. WindowCapturerWin is using GDI capturer, which cannot capture DX output.
97 // So ScreenCapturer should be used as much as possible to avoid
98 // uncapturable cases. Note: lots of new applications are using DX output
99 // (hardware acceleration) to improve the performance which cannot be
100 // captured by WindowCapturerWin. See bug http://crbug.com/741770.
101 // 2. WindowCapturerWin is still useful because we do not want to expose the
102 // content on other windows if the target window is covered by them.
103 // 3. Shadow and borders should not be considered as "content" on other
104 // windows because they do not expose any useful information.
105 //
106 // So we can bear the false-negative cases (target window is covered by the
107 // borders or shadow of other windows, but we have not detected it) in favor
108 // of using ScreenCapturer, rather than let the false-positive cases (target
109 // windows is only covered by borders or shadow of other windows, but we treat
110 // it as overlapping) impact the user experience.
111 DesktopRect content_rect;
112 if (!GetWindowContentRect(hwnd, &content_rect)) {
113 // Bail out if failed to get the window area.
114 context->is_top_window = false;
115 return FALSE;
116 }
117
118 content_rect.IntersectWith(context->selected_window_rect);
119
120 // If intersection is not empty, the selected window is not on top.
121 if (!content_rect.is_empty()) {
122 context->is_top_window = false;
123 return FALSE;
124 }
125 // Otherwise, keep enumerating.
126 return TRUE;
127 }
128
129 class CroppingWindowCapturerWin : public CroppingWindowCapturer {
130 public:
CroppingWindowCapturerWin(const DesktopCaptureOptions & options)131 CroppingWindowCapturerWin(
132 const DesktopCaptureOptions& options)
133 : CroppingWindowCapturer(options) {}
134
135 private:
136 bool ShouldUseScreenCapturer() override;
137 DesktopRect GetWindowRectInVirtualScreen() override;
138
139 // The region from GetWindowRgn in the desktop coordinate if the region is
140 // rectangular, or the rect from GetWindowRect if the region is not set.
141 DesktopRect window_region_rect_;
142
143 AeroChecker aero_checker_;
144 };
145
ShouldUseScreenCapturer()146 bool CroppingWindowCapturerWin::ShouldUseScreenCapturer() {
147 if (!rtc::IsWindows8OrLater() && aero_checker_.IsAeroEnabled()) {
148 return false;
149 }
150
151 const HWND selected = reinterpret_cast<HWND>(selected_window());
152 // Check if the window is hidden or minimized.
153 if (IsIconic(selected) || !IsWindowVisible(selected)) {
154 return false;
155 }
156
157 // Check if the window is a translucent layered window.
158 const LONG window_ex_style = GetWindowLong(selected, GWL_EXSTYLE);
159 if (window_ex_style & WS_EX_LAYERED) {
160 COLORREF color_ref_key = 0;
161 BYTE alpha = 0;
162 DWORD flags = 0;
163
164 // GetLayeredWindowAttributes fails if the window was setup with
165 // UpdateLayeredWindow. We have no way to know the opacity of the window in
166 // that case. This happens for Stiky Note (crbug/412726).
167 if (!GetLayeredWindowAttributes(selected, &color_ref_key, &alpha, &flags))
168 return false;
169
170 // UpdateLayeredWindow is the only way to set per-pixel alpha and will cause
171 // the previous GetLayeredWindowAttributes to fail. So we only need to check
172 // the window wide color key or alpha.
173 if ((flags & LWA_COLORKEY) || ((flags & LWA_ALPHA) && (alpha < 255))) {
174 return false;
175 }
176 }
177
178 if (!GetWindowRect(selected, &window_region_rect_)) {
179 return false;
180 }
181
182 DesktopRect content_rect;
183 if (!GetWindowContentRect(selected, &content_rect)) {
184 return false;
185 }
186
187 DesktopRect region_rect;
188 // Get the window region and check if it is rectangular.
189 const int region_type =
190 GetWindowRegionTypeWithBoundary(selected, ®ion_rect);
191
192 // Do not use the screen capturer if the region is empty or not rectangular.
193 if (region_type == COMPLEXREGION || region_type == NULLREGION) {
194 return false;
195 }
196
197 if (region_type == SIMPLEREGION) {
198 // The |region_rect| returned from GetRgnBox() is always in window
199 // coordinate.
200 region_rect.Translate(
201 window_region_rect_.left(), window_region_rect_.top());
202 // MSDN: The window region determines the area *within* the window where the
203 // system permits drawing.
204 // https://msdn.microsoft.com/en-us/library/windows/desktop/dd144950(v=vs.85).aspx.
205 //
206 // |region_rect| should always be inside of |window_region_rect_|. So after
207 // the intersection, |window_region_rect_| == |region_rect|. If so, what's
208 // the point of the intersecting operations? Why cannot we directly retrieve
209 // |window_region_rect_| from GetWindowRegionTypeWithBoundary() function?
210 // TODO(zijiehe): Figure out the purpose of these intersections.
211 window_region_rect_.IntersectWith(region_rect);
212 content_rect.IntersectWith(region_rect);
213 }
214
215 // Check if the client area is out of the screen area. When the window is
216 // maximized, only its client area is visible in the screen, the border will
217 // be hidden. So we are using |content_rect| here.
218 if (!GetFullscreenRect().ContainsRect(content_rect)) {
219 return false;
220 }
221
222 // Check if the window is occluded by any other window, excluding the child
223 // windows, context menus, and |excluded_window_|.
224 // |content_rect| is preferred, see the comments in TopWindowVerifier()
225 // function.
226 TopWindowVerifierContext context(
227 selected, reinterpret_cast<HWND>(excluded_window()), content_rect);
228 const LPARAM enum_param = reinterpret_cast<LPARAM>(&context);
229 EnumWindows(&TopWindowVerifier, enum_param);
230 if (!context.is_top_window) {
231 return false;
232 }
233
234 // If |selected| is not covered by other windows, check whether it is
235 // covered by its own child windows. Note: EnumChildWindows() enumerates child
236 // windows in all generations, but does not include any controls like buttons
237 // or textboxes.
238 EnumChildWindows(selected, &TopWindowVerifier, enum_param);
239 return context.is_top_window;
240 }
241
GetWindowRectInVirtualScreen()242 DesktopRect CroppingWindowCapturerWin::GetWindowRectInVirtualScreen() {
243 DesktopRect window_rect;
244 HWND hwnd = reinterpret_cast<HWND>(selected_window());
245 if (!GetCroppedWindowRect(hwnd, &window_rect, /* original_rect */ nullptr)) {
246 RTC_LOG(LS_WARNING) << "Failed to get window info: " << GetLastError();
247 return window_rect;
248 }
249 window_rect.IntersectWith(window_region_rect_);
250
251 // Convert |window_rect| to be relative to the top-left of the virtual screen.
252 DesktopRect screen_rect(GetFullscreenRect());
253 window_rect.IntersectWith(screen_rect);
254 window_rect.Translate(-screen_rect.left(), -screen_rect.top());
255 return window_rect;
256 }
257
258 } // namespace
259
260 // static
CreateCapturer(const DesktopCaptureOptions & options)261 std::unique_ptr<DesktopCapturer> CroppingWindowCapturer::CreateCapturer(
262 const DesktopCaptureOptions& options) {
263 return std::unique_ptr<DesktopCapturer>(
264 new CroppingWindowCapturerWin(options));
265 }
266
267 } // namespace webrtc
268