jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 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 "webrtc/modules/desktop_capture/cropping_window_capturer.h" |
| 12 | |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 13 | #include "webrtc/modules/desktop_capture/win/screen_capture_utils.h" |
| 14 | #include "webrtc/modules/desktop_capture/win/window_capture_utils.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 15 | #include "webrtc/rtc_base/logging.h" |
| 16 | #include "webrtc/rtc_base/win32.h" |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 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 { |
braveyao | b2b803c | 2017-08-08 13:30:01 -0700 | [diff] [blame] | 25 | TopWindowVerifierContext(HWND selected_window, |
| 26 | HWND excluded_window, |
| 27 | DesktopRect selected_window_rect) |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 28 | : selected_window(selected_window), |
| 29 | excluded_window(excluded_window), |
braveyao | b2b803c | 2017-08-08 13:30:01 -0700 | [diff] [blame] | 30 | selected_window_rect(selected_window_rect), |
| 31 | is_top_window(false) { |
| 32 | RTC_DCHECK_NE(selected_window, excluded_window); |
| 33 | } |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 34 | |
braveyao | b2b803c | 2017-08-08 13:30:01 -0700 | [diff] [blame] | 35 | const HWND selected_window; |
| 36 | const HWND excluded_window; |
| 37 | const DesktopRect selected_window_rect; |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 38 | bool is_top_window; |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 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. |
| 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 | |
Zijie He | f9d7eca | 2017-08-22 14:18:37 -0700 | [diff] [blame] | 62 | // Ignore descendant windows since we want to capture them. |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 63 | // This check does not work for tooltips and context menus. Drop down menus |
| 64 | // and popup windows are fine. |
Zijie He | f9d7eca | 2017-08-22 14:18:37 -0700 | [diff] [blame] | 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) { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 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. |
Zijie He | f9d7eca | 2017-08-22 14:18:37 -0700 | [diff] [blame] | 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. |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 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) { |
braveyao | b2b803c | 2017-08-08 13:30:01 -0700 | [diff] [blame] | 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) { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 89 | return TRUE; |
| 90 | } |
| 91 | } |
| 92 | |
Zijie He | 2a47e5d | 2017-08-25 17:42:38 -0700 | [diff] [blame] | 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)) { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 113 | // Bail out if failed to get the window area. |
| 114 | context->is_top_window = false; |
| 115 | return FALSE; |
| 116 | } |
| 117 | |
Zijie He | 2a47e5d | 2017-08-25 17:42:38 -0700 | [diff] [blame] | 118 | content_rect.IntersectWith(context->selected_window_rect); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 119 | |
| 120 | // If intersection is not empty, the selected window is not on top. |
Zijie He | 2a47e5d | 2017-08-25 17:42:38 -0700 | [diff] [blame] | 121 | if (!content_rect.is_empty()) { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 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: |
| 131 | CroppingWindowCapturerWin( |
| 132 | const DesktopCaptureOptions& options) |
| 133 | : CroppingWindowCapturer(options) {} |
| 134 | |
| 135 | private: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 136 | bool ShouldUseScreenCapturer() override; |
| 137 | DesktopRect GetWindowRectInVirtualScreen() override; |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 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_; |
Jiayang Liu | d848d5e | 2015-07-16 08:49:38 -0700 | [diff] [blame] | 142 | |
| 143 | AeroChecker aero_checker_; |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | bool CroppingWindowCapturerWin::ShouldUseScreenCapturer() { |
Zijie He | f9d7eca | 2017-08-22 14:18:37 -0700 | [diff] [blame] | 147 | if (!rtc::IsWindows8OrLater() && aero_checker_.IsAeroEnabled()) { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 148 | return false; |
Zijie He | f9d7eca | 2017-08-22 14:18:37 -0700 | [diff] [blame] | 149 | } |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 150 | |
Zijie He | dab31ce | 2017-08-23 11:21:09 -0700 | [diff] [blame] | 151 | const HWND selected = reinterpret_cast<HWND>(selected_window()); |
braveyao | b2b803c | 2017-08-08 13:30:01 -0700 | [diff] [blame] | 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. |
Zijie He | dab31ce | 2017-08-23 11:21:09 -0700 | [diff] [blame] | 158 | const LONG window_ex_style = GetWindowLong(selected, GWL_EXSTYLE); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 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. |
Zijie He | f9d7eca | 2017-08-22 14:18:37 -0700 | [diff] [blame] | 173 | if ((flags & LWA_COLORKEY) || ((flags & LWA_ALPHA) && (alpha < 255))) { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 174 | return false; |
Zijie He | f9d7eca | 2017-08-22 14:18:37 -0700 | [diff] [blame] | 175 | } |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Zijie He | f9d7eca | 2017-08-22 14:18:37 -0700 | [diff] [blame] | 178 | if (!GetWindowRect(selected, &window_region_rect_)) { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 179 | return false; |
| 180 | } |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 181 | |
Zijie He | dab31ce | 2017-08-23 11:21:09 -0700 | [diff] [blame] | 182 | DesktopRect content_rect; |
| 183 | if (!GetWindowContentRect(selected, &content_rect)) { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | DesktopRect region_rect; |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 188 | // Get the window region and check if it is rectangular. |
Zijie He | dab31ce | 2017-08-23 11:21:09 -0700 | [diff] [blame] | 189 | const int region_type = |
| 190 | GetWindowRegionTypeWithBoundary(selected, ®ion_rect); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 191 | |
| 192 | // Do not use the screen capturer if the region is empty or not rectangular. |
Zijie He | f9d7eca | 2017-08-22 14:18:37 -0700 | [diff] [blame] | 193 | if (region_type == COMPLEXREGION || region_type == NULLREGION) { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 194 | return false; |
Zijie He | f9d7eca | 2017-08-22 14:18:37 -0700 | [diff] [blame] | 195 | } |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 196 | |
| 197 | if (region_type == SIMPLEREGION) { |
Zijie He | dab31ce | 2017-08-23 11:21:09 -0700 | [diff] [blame] | 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); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 213 | } |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 214 | |
Zijie He | dab31ce | 2017-08-23 11:21:09 -0700 | [diff] [blame] | 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 | } |
Zijie He | f9d7eca | 2017-08-22 14:18:37 -0700 | [diff] [blame] | 221 | |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 222 | // Check if the window is occluded by any other window, excluding the child |
| 223 | // windows, context menus, and |excluded_window_|. |
Zijie He | 2a47e5d | 2017-08-25 17:42:38 -0700 | [diff] [blame] | 224 | // |content_rect| is preferred, see the comments in TopWindowVerifier() |
| 225 | // function. |
braveyao | b2b803c | 2017-08-08 13:30:01 -0700 | [diff] [blame] | 226 | TopWindowVerifierContext context( |
Zijie He | 2a47e5d | 2017-08-25 17:42:38 -0700 | [diff] [blame] | 227 | selected, reinterpret_cast<HWND>(excluded_window()), content_rect); |
Zijie He | f9d7eca | 2017-08-22 14:18:37 -0700 | [diff] [blame] | 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); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 239 | return context.is_top_window; |
| 240 | } |
| 241 | |
| 242 | DesktopRect CroppingWindowCapturerWin::GetWindowRectInVirtualScreen() { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 243 | DesktopRect window_rect; |
| 244 | HWND hwnd = reinterpret_cast<HWND>(selected_window()); |
Zijie He | f9d7eca | 2017-08-22 14:18:37 -0700 | [diff] [blame] | 245 | if (!GetCroppedWindowRect(hwnd, &window_rect, /* original_rect */ nullptr)) { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 246 | 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. |
Zijie He | 74544f9 | 2017-07-24 16:52:17 -0700 | [diff] [blame] | 252 | DesktopRect screen_rect(GetFullscreenRect()); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 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 |
zijiehe | c9a6e4a | 2016-11-11 15:13:32 -0800 | [diff] [blame] | 261 | std::unique_ptr<DesktopCapturer> CroppingWindowCapturer::CreateCapturer( |
| 262 | const DesktopCaptureOptions& options) { |
| 263 | return std::unique_ptr<DesktopCapturer>( |
| 264 | new CroppingWindowCapturerWin(options)); |
| 265 | } |
| 266 | |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 267 | } // namespace webrtc |