From c8ac17ca04897f8f5fbb5d179e8b7573f66b148a Mon Sep 17 00:00:00 2001 From: "jiayl@webrtc.org" Date: Thu, 20 Mar 2014 00:06:41 +0000 Subject: [PATCH] Adjust the captured window rect when the window is maximized. GetWindowRect includes the window frames for maximized window even they are off screen, causing content outside the window being captured falsely. The fix is to remove the left/right/bottom window frame from the captured rect. Mouse capturing is adjusted accordingly as well. BUG=3076 R=sergeyu@chromium.org Review URL: https://webrtc-codereview.appspot.com/10149004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@5732 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../desktop_capture/desktop_capture.gypi | 2 + .../mouse_cursor_monitor_win.cc | 8 ++-- .../win/window_capture_utils.cc | 45 +++++++++++++++++++ .../win/window_capture_utils.h | 25 +++++++++++ .../desktop_capture/window_capturer_win.cc | 17 ++++--- 5 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 webrtc/modules/desktop_capture/win/window_capture_utils.cc create mode 100644 webrtc/modules/desktop_capture/win/window_capture_utils.h diff --git a/webrtc/modules/desktop_capture/desktop_capture.gypi b/webrtc/modules/desktop_capture/desktop_capture.gypi index 9a0293512..5f967453f 100644 --- a/webrtc/modules/desktop_capture/desktop_capture.gypi +++ b/webrtc/modules/desktop_capture/desktop_capture.gypi @@ -69,6 +69,8 @@ "win/scoped_gdi_object.h", "win/scoped_thread_desktop.cc", "win/scoped_thread_desktop.h", + "win/window_capture_utils.cc", + "win/window_capture_utils.h", "window_capturer.cc", "window_capturer.h", "window_capturer_mac.mm", diff --git a/webrtc/modules/desktop_capture/mouse_cursor_monitor_win.cc b/webrtc/modules/desktop_capture/mouse_cursor_monitor_win.cc index fc041ef51..d68fe4c5a 100644 --- a/webrtc/modules/desktop_capture/mouse_cursor_monitor_win.cc +++ b/webrtc/modules/desktop_capture/mouse_cursor_monitor_win.cc @@ -13,6 +13,7 @@ #include "webrtc/modules/desktop_capture/desktop_frame.h" #include "webrtc/modules/desktop_capture/mouse_cursor.h" #include "webrtc/modules/desktop_capture/win/cursor.h" +#include "webrtc/modules/desktop_capture/win/window_capture_utils.h" #include "webrtc/system_wrappers/interface/logging.h" namespace webrtc { @@ -100,8 +101,9 @@ void MouseCursorMonitorWin::Capture() { bool inside = cursor_info.flags == CURSOR_SHOWING; if (window_) { - RECT rect; - if (!GetWindowRect(window_, &rect)) { + DesktopRect original_rect; + DesktopRect cropped_rect; + if (!GetCroppedWindowRect(window_, &cropped_rect, &original_rect)) { position.set(0, 0); inside = false; } else { @@ -110,7 +112,7 @@ void MouseCursorMonitorWin::Capture() { inside = windowUnderCursor ? (window_ == GetAncestor(windowUnderCursor, GA_ROOT)) : false; } - position = position.subtract(DesktopVector(rect.left, rect.top)); + position = position.subtract(cropped_rect.top_left()); } } else { assert(screen_ != kInvalidScreenId); diff --git a/webrtc/modules/desktop_capture/win/window_capture_utils.cc b/webrtc/modules/desktop_capture/win/window_capture_utils.cc new file mode 100644 index 000000000..c878a63cd --- /dev/null +++ b/webrtc/modules/desktop_capture/win/window_capture_utils.cc @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/modules/desktop_capture/win/window_capture_utils.h" + +namespace webrtc { + +bool +GetCroppedWindowRect(HWND window, + DesktopRect* cropped_rect, + DesktopRect* original_rect) { + RECT rect; + if (!GetWindowRect(window, &rect)) { + return false; + } + WINDOWPLACEMENT window_placement; + if (!GetWindowPlacement(window, &window_placement)) { + return false; + } + + *original_rect = DesktopRect::MakeLTRB( + rect.left, rect.top, rect.right, rect.bottom); + + if (window_placement.showCmd & SW_SHOWMAXIMIZED) { + DesktopSize border = DesktopSize(GetSystemMetrics(SM_CXSIZEFRAME), + GetSystemMetrics(SM_CYSIZEFRAME)); + *cropped_rect = DesktopRect::MakeLTRB( + rect.left + border.width(), + rect.top, + rect.right - border.width(), + rect.bottom - border.height()); + } else { + *cropped_rect = *original_rect; + } + return true; +} + +} // namespace webrtc diff --git a/webrtc/modules/desktop_capture/win/window_capture_utils.h b/webrtc/modules/desktop_capture/win/window_capture_utils.h new file mode 100644 index 000000000..2a3a470c5 --- /dev/null +++ b/webrtc/modules/desktop_capture/win/window_capture_utils.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include + +#include "webrtc/modules/desktop_capture/desktop_geometry.h" + +namespace webrtc { + +// Output the window rect, with the left/right/bottom frame border cropped if +// the window is maximized. |cropped_rect| is the cropped rect relative to the +// desktop. |original_rect| is the original rect returned from GetWindowRect. +// Returns true if all API calls succeeded. +bool GetCroppedWindowRect(HWND window, + DesktopRect* cropped_rect, + DesktopRect* original_rect); + +} // namespace webrtc diff --git a/webrtc/modules/desktop_capture/window_capturer_win.cc b/webrtc/modules/desktop_capture/window_capturer_win.cc index 97a15dd9a..b9764b3a4 100644 --- a/webrtc/modules/desktop_capture/window_capturer_win.cc +++ b/webrtc/modules/desktop_capture/window_capturer_win.cc @@ -11,9 +11,9 @@ #include "webrtc/modules/desktop_capture/window_capturer.h" #include -#include #include "webrtc/modules/desktop_capture/desktop_frame_win.h" +#include "webrtc/modules/desktop_capture/win/window_capture_utils.h" #include "webrtc/system_wrappers/interface/logging.h" #include "webrtc/system_wrappers/interface/scoped_ptr.h" @@ -188,9 +188,10 @@ void WindowCapturerWin::Capture(const DesktopRegion& region) { return; } - RECT rect; - if (!GetWindowRect(window_, &rect)) { - LOG(LS_WARNING) << "Failed to get window size: " << GetLastError(); + DesktopRect original_rect; + DesktopRect cropped_rect; + if (!GetCroppedWindowRect(window_, &cropped_rect, &original_rect)) { + LOG(LS_WARNING) << "Failed to get window info: " << GetLastError(); callback_->OnCaptureCompleted(NULL); return; } @@ -203,8 +204,7 @@ void WindowCapturerWin::Capture(const DesktopRegion& region) { } scoped_ptr frame(DesktopFrameWin::Create( - DesktopSize(rect.right - rect.left, rect.bottom - rect.top), - NULL, window_dc)); + cropped_rect.size(), NULL, window_dc)); if (!frame.get()) { ReleaseDC(window_, window_dc); callback_->OnCaptureCompleted(NULL); @@ -239,7 +239,10 @@ void WindowCapturerWin::Capture(const DesktopRegion& region) { // Aero is enabled or PrintWindow() failed, use BitBlt. if (!result) { result = BitBlt(mem_dc, 0, 0, frame->size().width(), frame->size().height(), - window_dc, 0, 0, SRCCOPY); + window_dc, + cropped_rect.left() - original_rect.left(), + cropped_rect.top() - original_rect.top(), + SRCCOPY); } SelectObject(mem_dc, previous_object);