1 /*
2  *  Copyright (c) 2020 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/win/window_capturer_win_gdi.h"
12 
13 #include <map>
14 #include <memory>
15 #include <utility>
16 #include <vector>
17 
18 #include "modules/desktop_capture/cropped_desktop_frame.h"
19 #include "modules/desktop_capture/desktop_capturer.h"
20 #include "modules/desktop_capture/desktop_frame_win.h"
21 #include "modules/desktop_capture/win/screen_capture_utils.h"
22 #include "modules/desktop_capture/win/selected_window_context.h"
23 #include "rtc_base/arraysize.h"
24 #include "rtc_base/checks.h"
25 #include "rtc_base/logging.h"
26 #include "rtc_base/string_utils.h"
27 #include "rtc_base/trace_event.h"
28 #include "rtc_base/win32.h"
29 
30 namespace webrtc {
31 
32 // Used to pass input/output data during the EnumWindows call to collect
33 // owned/pop-up windows that should be captured.
34 struct OwnedWindowCollectorContext : public SelectedWindowContext {
OwnedWindowCollectorContextwebrtc::OwnedWindowCollectorContext35   OwnedWindowCollectorContext(HWND selected_window,
36                               DesktopRect selected_window_rect,
37                               WindowCaptureHelperWin* window_capture_helper,
38                               std::vector<HWND>* owned_windows)
39       : SelectedWindowContext(selected_window,
40                               selected_window_rect,
41                               window_capture_helper),
42         owned_windows(owned_windows) {}
43 
44   std::vector<HWND>* owned_windows;
45 };
46 
47 // Called via EnumWindows for each root window; adds owned/pop-up windows that
48 // should be captured to a vector it's passed.
OwnedWindowCollector(HWND hwnd,LPARAM param)49 BOOL CALLBACK OwnedWindowCollector(HWND hwnd, LPARAM param) {
50   OwnedWindowCollectorContext* context =
51       reinterpret_cast<OwnedWindowCollectorContext*>(param);
52   if (hwnd == context->selected_window()) {
53     // Windows are enumerated in top-down z-order, so we can stop enumerating
54     // upon reaching the selected window.
55     return FALSE;
56   }
57 
58   // Skip windows that aren't visible pop-up windows.
59   if (!(GetWindowLong(hwnd, GWL_STYLE) & WS_POPUP) ||
60       !context->window_capture_helper()->IsWindowVisibleOnCurrentDesktop(
61           hwnd)) {
62     return TRUE;
63   }
64 
65   // Owned windows that intersect the selected window should be captured.
66   if (context->IsWindowOwnedBySelectedWindow(hwnd) &&
67       context->IsWindowOverlappingSelectedWindow(hwnd)) {
68     // Skip windows that draw shadows around menus. These "SysShadow" windows
69     // would otherwise be captured as solid black bars with no transparency
70     // gradient (since this capturer doesn't detect / respect variations in the
71     // window alpha channel). Any other semi-transparent owned windows will be
72     // captured fully-opaque. This seems preferable to excluding them (at least
73     // when they have content aside from a solid fill color / visual adornment;
74     // e.g. some tooltips have the transparent style set).
75     if (GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TRANSPARENT) {
76       const WCHAR kSysShadow[] = L"SysShadow";
77       const size_t kClassLength = arraysize(kSysShadow);
78       WCHAR class_name[kClassLength];
79       const int class_name_length =
80           GetClassNameW(hwnd, class_name, kClassLength);
81       if (class_name_length == kClassLength - 1 &&
82           wcscmp(class_name, kSysShadow) == 0) {
83         return TRUE;
84       }
85     }
86 
87     context->owned_windows->push_back(hwnd);
88   }
89 
90   return TRUE;
91 }
92 
WindowCapturerWinGdi()93 WindowCapturerWinGdi::WindowCapturerWinGdi() {}
~WindowCapturerWinGdi()94 WindowCapturerWinGdi::~WindowCapturerWinGdi() {}
95 
GetSourceList(SourceList * sources)96 bool WindowCapturerWinGdi::GetSourceList(SourceList* sources) {
97   if (!window_capture_helper_.EnumerateCapturableWindows(sources))
98     return false;
99 
100   std::map<HWND, DesktopSize> new_map;
101   for (const auto& item : *sources) {
102     HWND hwnd = reinterpret_cast<HWND>(item.id);
103     new_map[hwnd] = window_size_map_[hwnd];
104   }
105   window_size_map_.swap(new_map);
106 
107   return true;
108 }
109 
SelectSource(SourceId id)110 bool WindowCapturerWinGdi::SelectSource(SourceId id) {
111   HWND window = reinterpret_cast<HWND>(id);
112   if (!IsWindowValidAndVisible(window))
113     return false;
114 
115   window_ = window;
116   // When a window is not in the map, window_size_map_[window] will create an
117   // item with DesktopSize (0, 0).
118   previous_size_ = window_size_map_[window];
119   return true;
120 }
121 
FocusOnSelectedSource()122 bool WindowCapturerWinGdi::FocusOnSelectedSource() {
123   if (!window_)
124     return false;
125 
126   if (!IsWindowValidAndVisible(window_))
127     return false;
128 
129   return BringWindowToTop(window_) && SetForegroundWindow(window_);
130 }
131 
IsOccluded(const DesktopVector & pos)132 bool WindowCapturerWinGdi::IsOccluded(const DesktopVector& pos) {
133   DesktopVector sys_pos = pos.add(GetFullscreenRect().top_left());
134   HWND hwnd =
135       reinterpret_cast<HWND>(window_finder_.GetWindowUnderPoint(sys_pos));
136 
137   return hwnd != window_ &&
138          std::find(owned_windows_.begin(), owned_windows_.end(), hwnd) ==
139              owned_windows_.end();
140 }
141 
Start(Callback * callback)142 void WindowCapturerWinGdi::Start(Callback* callback) {
143   RTC_DCHECK(!callback_);
144   RTC_DCHECK(callback);
145 
146   callback_ = callback;
147 }
148 
CaptureFrame()149 void WindowCapturerWinGdi::CaptureFrame() {
150   RTC_DCHECK(callback_);
151 
152   CaptureResults results = CaptureFrame(/*capture_owned_windows*/ true);
153   callback_->OnCaptureResult(results.result, std::move(results.frame));
154 }
155 
CaptureFrame(bool capture_owned_windows)156 WindowCapturerWinGdi::CaptureResults WindowCapturerWinGdi::CaptureFrame(
157     bool capture_owned_windows) {
158   TRACE_EVENT0("webrtc", "WindowCapturerWinGdi::CaptureFrame");
159 
160   if (!window_) {
161     RTC_LOG(LS_ERROR) << "Window hasn't been selected: " << GetLastError();
162     return {Result::ERROR_PERMANENT, nullptr};
163   }
164 
165   // Stop capturing if the window has been closed.
166   if (!IsWindow(window_)) {
167     RTC_LOG(LS_ERROR) << "Target window has been closed.";
168     return {Result::ERROR_PERMANENT, nullptr};
169   }
170 
171   // Determine the window region excluding any resize border, and including
172   // any visible border if capturing an owned window / dialog. (Don't include
173   // any visible border for the selected window for consistency with
174   // CroppingWindowCapturerWin, which would expose a bit of the background
175   // through the partially-transparent border.)
176   const bool avoid_cropping_border = !capture_owned_windows;
177   DesktopRect cropped_rect;
178   DesktopRect original_rect;
179 
180   if (!GetCroppedWindowRect(window_, avoid_cropping_border, &cropped_rect,
181                             &original_rect)) {
182     RTC_LOG(LS_WARNING) << "Failed to get drawable window area: "
183                         << GetLastError();
184     return {Result::ERROR_TEMPORARY, nullptr};
185   }
186 
187   // Return a 1x1 black frame if the window is minimized or invisible on current
188   // desktop, to match behavior on mace. Window can be temporarily invisible
189   // during the transition of full screen mode on/off.
190   if (original_rect.is_empty() ||
191       !window_capture_helper_.IsWindowVisibleOnCurrentDesktop(window_)) {
192     std::unique_ptr<DesktopFrame> frame(
193         new BasicDesktopFrame(DesktopSize(1, 1)));
194 
195     previous_size_ = frame->size();
196     window_size_map_[window_] = previous_size_;
197     return {Result::SUCCESS, std::move(frame)};
198   }
199 
200   HDC window_dc = GetWindowDC(window_);
201   if (!window_dc) {
202     RTC_LOG(LS_WARNING) << "Failed to get window DC: " << GetLastError();
203     return {Result::ERROR_TEMPORARY, nullptr};
204   }
205 
206   DesktopRect unscaled_cropped_rect = cropped_rect;
207   double horizontal_scale = 1.0;
208   double vertical_scale = 1.0;
209 
210   DesktopSize window_dc_size;
211   if (GetDcSize(window_dc, &window_dc_size)) {
212     // The |window_dc_size| is used to detect the scaling of the original
213     // window. If the application does not support high-DPI settings, it will
214     // be scaled by Windows according to the scaling setting.
215     // https://www.google.com/search?q=windows+scaling+settings&ie=UTF-8
216     // So the size of the |window_dc|, i.e. the bitmap we can retrieve from
217     // PrintWindow() or BitBlt() function, will be smaller than
218     // |original_rect| and |cropped_rect|. Part of the captured desktop frame
219     // will be black. See
220     // bug https://bugs.chromium.org/p/webrtc/issues/detail?id=8112 for
221     // details.
222 
223     // If |window_dc_size| is smaller than |window_rect|, let's resize both
224     // |original_rect| and |cropped_rect| according to the scaling factor.
225     horizontal_scale =
226         static_cast<double>(window_dc_size.width()) / original_rect.width();
227     vertical_scale =
228         static_cast<double>(window_dc_size.height()) / original_rect.height();
229     original_rect.Scale(horizontal_scale, vertical_scale);
230     cropped_rect.Scale(horizontal_scale, vertical_scale);
231   }
232 
233   std::unique_ptr<DesktopFrameWin> frame(
234       DesktopFrameWin::Create(original_rect.size(), nullptr, window_dc));
235   if (!frame.get()) {
236     RTC_LOG(LS_WARNING) << "Failed to create frame.";
237     ReleaseDC(window_, window_dc);
238     return {Result::ERROR_TEMPORARY, nullptr};
239   }
240 
241   HDC mem_dc = CreateCompatibleDC(window_dc);
242   HGDIOBJ previous_object = SelectObject(mem_dc, frame->bitmap());
243   BOOL result = FALSE;
244 
245   // When desktop composition (Aero) is enabled each window is rendered to a
246   // private buffer allowing BitBlt() to get the window content even if the
247   // window is occluded. PrintWindow() is slower but lets rendering the window
248   // contents to an off-screen device context when Aero is not available.
249   // PrintWindow() is not supported by some applications.
250   //
251   // If Aero is enabled, we prefer BitBlt() because it's faster and avoids
252   // window flickering. Otherwise, we prefer PrintWindow() because BitBlt() may
253   // render occluding windows on top of the desired window.
254   //
255   // When composition is enabled the DC returned by GetWindowDC() doesn't always
256   // have window frame rendered correctly. Windows renders it only once and then
257   // caches the result between captures. We hack it around by calling
258   // PrintWindow() whenever window size changes, including the first time of
259   // capturing - it somehow affects what we get from BitBlt() on the subsequent
260   // captures.
261   //
262   // For Windows 8.1 and later, we want to always use PrintWindow when the
263   // cropping screen capturer falls back to the window capturer. I.e.
264   // on Windows 8.1 and later, PrintWindow is only used when the window is
265   // occluded. When the window is not occluded, it is much faster to capture
266   // the screen and to crop it to the window position and size.
267   if (rtc::IsWindows8OrLater()) {
268     // Special flag that makes PrintWindow to work on Windows 8.1 and later.
269     // Indeed certain apps (e.g. those using DirectComposition rendering) can't
270     // be captured using BitBlt or PrintWindow without this flag. Note that on
271     // Windows 8.0 this flag is not supported so the block below will fallback
272     // to the other call to PrintWindow. It seems to be very tricky to detect
273     // Windows 8.0 vs 8.1 so a try/fallback is more approriate here.
274     const UINT flags = PW_RENDERFULLCONTENT;
275     result = PrintWindow(window_, mem_dc, flags);
276   }
277 
278   if (!result && (!window_capture_helper_.IsAeroEnabled() ||
279                   !previous_size_.equals(frame->size()))) {
280     result = PrintWindow(window_, mem_dc, 0);
281   }
282 
283   // Aero is enabled or PrintWindow() failed, use BitBlt.
284   if (!result) {
285     result = BitBlt(mem_dc, 0, 0, frame->size().width(), frame->size().height(),
286                     window_dc, 0, 0, SRCCOPY);
287   }
288 
289   SelectObject(mem_dc, previous_object);
290   DeleteDC(mem_dc);
291   ReleaseDC(window_, window_dc);
292 
293   previous_size_ = frame->size();
294   window_size_map_[window_] = previous_size_;
295 
296   frame->mutable_updated_region()->SetRect(
297       DesktopRect::MakeSize(frame->size()));
298   frame->set_top_left(
299       original_rect.top_left().subtract(GetFullscreenRect().top_left()));
300 
301   if (!result) {
302     RTC_LOG(LS_ERROR) << "Both PrintWindow() and BitBlt() failed.";
303     return {Result::ERROR_TEMPORARY, nullptr};
304   }
305 
306   // Rect for the data is relative to the first pixel of the frame.
307   cropped_rect.Translate(-original_rect.left(), -original_rect.top());
308   std::unique_ptr<DesktopFrame> cropped_frame =
309       CreateCroppedDesktopFrame(std::move(frame), cropped_rect);
310   RTC_DCHECK(cropped_frame);
311 
312   if (capture_owned_windows) {
313     // If any owned/pop-up windows overlap the selected window, capture them
314     // and copy/composite their contents into the frame.
315     owned_windows_.clear();
316     OwnedWindowCollectorContext context(window_, unscaled_cropped_rect,
317                                         &window_capture_helper_,
318                                         &owned_windows_);
319 
320     if (context.IsSelectedWindowValid()) {
321       EnumWindows(OwnedWindowCollector, reinterpret_cast<LPARAM>(&context));
322 
323       if (!owned_windows_.empty()) {
324         if (!owned_window_capturer_) {
325           owned_window_capturer_ = std::make_unique<WindowCapturerWinGdi>();
326         }
327 
328         // Owned windows are stored in top-down z-order, so this iterates in
329         // reverse to capture / draw them in bottom-up z-order
330         for (auto it = owned_windows_.rbegin(); it != owned_windows_.rend();
331              it++) {
332           HWND hwnd = *it;
333           if (owned_window_capturer_->SelectSource(
334                   reinterpret_cast<SourceId>(hwnd))) {
335             CaptureResults results = owned_window_capturer_->CaptureFrame(
336                 /*capture_owned_windows*/ false);
337 
338             if (results.result != DesktopCapturer::Result::SUCCESS) {
339               // Simply log any error capturing an owned/pop-up window without
340               // bubbling it up to the caller (an expected error here is that
341               // the owned/pop-up window was closed; any unexpected errors won't
342               // fail the outer capture).
343               RTC_LOG(LS_INFO) << "Capturing owned window failed (previous "
344                                   "error/warning pertained to that)";
345             } else {
346               // Copy / composite the captured frame into the outer frame. This
347               // may no-op if they no longer intersect (if the owned window was
348               // moved outside the owner bounds since scheduled for capture.)
349               cropped_frame->CopyIntersectingPixelsFrom(
350                   *results.frame, horizontal_scale, vertical_scale);
351             }
352           }
353         }
354       }
355     }
356   }
357 
358   return {Result::SUCCESS, std::move(cropped_frame)};
359 }
360 
361 // static
CreateRawWindowCapturer(const DesktopCaptureOptions & options)362 std::unique_ptr<DesktopCapturer> WindowCapturerWinGdi::CreateRawWindowCapturer(
363     const DesktopCaptureOptions& options) {
364   return std::unique_ptr<DesktopCapturer>(new WindowCapturerWinGdi());
365 }
366 
367 }  // namespace webrtc
368