Commit 1947a90d authored by Dana Fried's avatar Dana Fried Committed by Commit Bot

Account for scrollbars when capturing thumbnail video frames.

We already account for this when doing direct capture on tab switch;
this makes the same adjustment when capturing video from a background
tab.

Change-Id: I19791a5761ac4539b5eee8be37a189af75e922b7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1835273
Commit-Queue: Dana Fried <dfried@chromium.org>
Reviewed-by: default avatarCollin Baker <collinbaker@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702148}
parent 0f9c0b10
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "media/capture/mojom/video_capture_types.mojom.h" #include "media/capture/mojom/video_capture_types.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/size_f.h"
#include "ui/gfx/scrollbar_size.h" #include "ui/gfx/scrollbar_size.h"
#include "ui/gfx/skia_util.h" #include "ui/gfx/skia_util.h"
...@@ -132,11 +133,13 @@ void ThumbnailTabHelper::StartVideoCapture() { ...@@ -132,11 +133,13 @@ void ThumbnailTabHelper::StartVideoCapture() {
return; return;
} }
// TODO(dfried): this doesn't take scroll bars into account, so they show up const float scrollbar_size = gfx::scrollbar_size() * scale_factor;
// in the thumbnail. Fix it.
gfx::Size max_size_px; gfx::Size max_size_px;
if (source_size_px.width() < target_size_px.width() || if (source_size_px.width() - scrollbar_size < target_size_px.width() ||
source_size_px.height() < target_size_px.height()) { source_size_px.height() - scrollbar_size < target_size_px.height()) {
// We need all of the pixels we can get, so we won't trim off scrollbars
// later.
last_frame_scrollbar_percent_ = 0.0f;
// The source is smaller than the target - typically shorter, since normal // The source is smaller than the target - typically shorter, since normal
// browser windows have a minimum width. Allowing the capture to use up to // browser windows have a minimum width. Allowing the capture to use up to
// double the size of the target bitmap provides decent results in most // double the size of the target bitmap provides decent results in most
...@@ -145,13 +148,19 @@ void ThumbnailTabHelper::StartVideoCapture() { ...@@ -145,13 +148,19 @@ void ThumbnailTabHelper::StartVideoCapture() {
max_size_px = max_size_px =
gfx::Size(target_size_px.width() * 2, target_size_px.height() * 2); gfx::Size(target_size_px.width() * 2, target_size_px.height() * 2);
} else { } else {
const gfx::SizeF effective_source_size{
source_size_px.width() - scrollbar_size,
source_size_px.height() - scrollbar_size};
// Remember how much of the frame is likely to be scroll bars so we can trim
// them off later.
last_frame_scrollbar_percent_ = scrollbar_size / source_size_px.width();
// This scaling logic makes the maximum size equal to the size of the source // This scaling logic makes the maximum size equal to the size of the source
// scaled down so it is no smaller than the target bitmap in either // scaled down so it is no smaller than the target bitmap in either
// dimension. It means we always have an appropriate sized frame to clip // dimension. It means we always have an appropriate sized frame to clip
// from (the final clip region will be determined after capture). // from (the final clip region will be determined after capture).
const float min_ratio = const float min_ratio =
std::min(float{source_size_px.width()} / target_size_px.width(), std::min(effective_source_size.width() / target_size_px.width(),
float{source_size_px.height()} / target_size_px.height()); effective_source_size.height() / target_size_px.height());
max_size_px = gfx::ScaleToCeiledSize(source_size_px, 1.0f / min_ratio); max_size_px = gfx::ScaleToCeiledSize(source_size_px, 1.0f / min_ratio);
} }
...@@ -243,17 +252,25 @@ void ThumbnailTabHelper::OnFrameCaptured( ...@@ -243,17 +252,25 @@ void ThumbnailTabHelper::OnFrameCaptured(
viz::mojom::FrameSinkVideoConsumerFrameCallbacksPtr releaser; viz::mojom::FrameSinkVideoConsumerFrameCallbacksPtr releaser;
}; };
content::RenderWidgetHostView* const source_view = GetView();
const float scale_factor =
source_view ? source_view->GetDeviceScaleFactor() : 1.0f;
// Subtract back out the scroll bars if we decided there was enough canvas to
// account for them and still have a decent preview image.
const int scrollbar_width =
std::round(content_rect.right() * last_frame_scrollbar_percent_);
gfx::Rect effective_content_rect = content_rect;
effective_content_rect.Inset(0, 0, scrollbar_width, scrollbar_width);
// We want to grab a subset of the captured content rect. Since we've ensured // We want to grab a subset of the captured content rect. Since we've ensured
// that in the vast majority of cases the captured frame will be an // that in the vast majority of cases the captured frame will be an
// appropriate size to clip a thumbnail from, our standard clipping logic // appropriate size to clip a thumbnail from, our standard clipping logic
// should be adequate here. // should be adequate here.
content::RenderWidgetHostView* const source_view = GetView();
const float scale_factor =
source_view ? source_view->GetDeviceScaleFactor() : 1.0f;
auto copy_info = thumbnails::GetCanvasCopyInfo( auto copy_info = thumbnails::GetCanvasCopyInfo(
content_rect.size(), scale_factor, GetThumbnailSize()); effective_content_rect.size(), scale_factor, GetThumbnailSize());
gfx::Rect copy_rect = copy_info.copy_rect; gfx::Rect copy_rect = copy_info.copy_rect;
copy_rect.Offset(content_rect.x(), content_rect.y()); copy_rect.Offset(effective_content_rect.x(), effective_content_rect.y());
const gfx::Size bitmap_size(content_rect.right(), content_rect.bottom()); const gfx::Size bitmap_size(content_rect.right(), content_rect.bottom());
SkBitmap frame; SkBitmap frame;
......
...@@ -70,6 +70,10 @@ class ThumbnailTabHelper ...@@ -70,6 +70,10 @@ class ThumbnailTabHelper
// was last hidden. // was last hidden.
bool captured_loaded_thumbnail_since_tab_hidden_ = false; bool captured_loaded_thumbnail_since_tab_hidden_ = false;
// The estimated percentage of the most recent frame being captured that
// consists of possible right and bottom scroll bars.
float last_frame_scrollbar_percent_ = 0.0f;
// Captures frames from the WebContents while it's hidden. The capturer count // Captures frames from the WebContents while it's hidden. The capturer count
// of the WebContents is incremented/decremented when a capturer is set/unset. // of the WebContents is incremented/decremented when a capturer is set/unset.
std::unique_ptr<viz::ClientFrameSinkVideoCapturer> video_capturer_; std::unique_ptr<viz::ClientFrameSinkVideoCapturer> video_capturer_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment