From d3dc66e3080459c4ab140acbb0acb052140a3b23 Mon Sep 17 00:00:00 2001 From: gellert Date: Fri, 9 Sep 2016 23:17:17 +0200 Subject: [PATCH] adds scale factor check and simplifies frame subscription --- atom/browser/api/atom_api_web_contents.cc | 2 +- atom/browser/api/frame_subscriber.cc | 37 ++++++++++------------- atom/browser/api/frame_subscriber.h | 8 ++--- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index f67926978d16..5a632d24d3c9 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -1242,7 +1242,7 @@ void WebContents::BeginFrameSubscription(mate::Arguments* args) { const auto view = web_contents()->GetRenderWidgetHostView(); if (view) { std::unique_ptr frame_subscriber(new FrameSubscriber( - isolate(), view, callback, only_dirty)); + view, callback, only_dirty)); view->BeginFrameSubscription(std::move(frame_subscriber)); } } diff --git a/atom/browser/api/frame_subscriber.cc b/atom/browser/api/frame_subscriber.cc index 1e80acd9233e..8bd97bb4e0fc 100644 --- a/atom/browser/api/frame_subscriber.cc +++ b/atom/browser/api/frame_subscriber.cc @@ -8,17 +8,17 @@ #include "atom/common/node_includes.h" #include "base/bind.h" #include "content/public/browser/render_widget_host.h" +#include "ui/display/screen.h" +#include "ui/display/display.h" namespace atom { namespace api { -FrameSubscriber::FrameSubscriber(v8::Isolate* isolate, - content::RenderWidgetHostView* view, +FrameSubscriber::FrameSubscriber(content::RenderWidgetHostView* view, const FrameCaptureCallback& callback, bool only_dirty) - : isolate_(isolate), - view_(view), + : view_(view), callback_(callback), only_dirty_(only_dirty), weak_factory_(this) { @@ -40,6 +40,17 @@ bool FrameSubscriber::ShouldCaptureFrame( if (only_dirty_) rect = dirty_rect; + gfx::Size view_size = rect.size(); + gfx::Size bitmap_size = view_size; + const gfx::NativeView native_view = view_->GetNativeView(); + const float scale = + display::Screen::GetScreen()->GetDisplayNearestWindow(native_view) + .device_scale_factor(); + if (scale > 1.0f) + bitmap_size = gfx::ScaleToCeiledSize(view_size, scale); + + rect = gfx::Rect(rect.origin(), bitmap_size); + host->CopyFromBackingStore( rect, rect.size(), @@ -57,23 +68,7 @@ void FrameSubscriber::OnFrameDelivered(const FrameCaptureCallback& callback, if (response != content::ReadbackResponse::READBACK_SUCCESS) return; - v8::Locker locker(isolate_); - v8::HandleScope handle_scope(isolate_); - - size_t rgb_arr_size = bitmap.width() * bitmap.height() * - bitmap.bytesPerPixel(); - v8::MaybeLocal buffer = node::Buffer::New(isolate_, rgb_arr_size); - if (buffer.IsEmpty()) - return; - - bitmap.copyPixelsTo( - reinterpret_cast(node::Buffer::Data(buffer.ToLocalChecked())), - rgb_arr_size); - - v8::Local damage = - mate::Converter::ToV8(isolate_, damage_rect); - - callback_.Run(buffer.ToLocalChecked(), damage); + callback.Run(gfx::Image::CreateFrom1xBitmap(bitmap), damage_rect); } } // namespace api diff --git a/atom/browser/api/frame_subscriber.h b/atom/browser/api/frame_subscriber.h index f1c7f0af45e2..96464beaabfb 100644 --- a/atom/browser/api/frame_subscriber.h +++ b/atom/browser/api/frame_subscriber.h @@ -11,8 +11,8 @@ #include "content/public/browser/render_widget_host_view.h" #include "content/public/browser/render_widget_host_view_frame_subscriber.h" #include "third_party/skia/include/core/SkBitmap.h" +#include "ui/gfx/image/image.h" #include "ui/gfx/geometry/size.h" -#include "v8/include/v8.h" namespace atom { @@ -21,10 +21,9 @@ namespace api { class FrameSubscriber : public content::RenderWidgetHostViewFrameSubscriber { public: using FrameCaptureCallback = - base::Callback, v8::Local)>; + base::Callback; - FrameSubscriber(v8::Isolate* isolate, - content::RenderWidgetHostView* view, + FrameSubscriber(content::RenderWidgetHostView* view, const FrameCaptureCallback& callback, bool only_dirty); @@ -39,7 +38,6 @@ class FrameSubscriber : public content::RenderWidgetHostViewFrameSubscriber { const SkBitmap& bitmap, content::ReadbackResponse response); - v8::Isolate* isolate_; content::RenderWidgetHostView* view_; FrameCaptureCallback callback_; bool only_dirty_;