From 86f523d3c13224f0a3dadac919ec25e24d4f8b80 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 18 Sep 2015 15:57:43 +0800 Subject: [PATCH] Move BeginFrameSubscription to WebContents --- atom/browser/api/atom_api_web_contents.cc | 17 ++++++ atom/browser/api/atom_api_web_contents.h | 6 +++ atom/browser/api/atom_api_window.cc | 23 +------- atom/browser/api/atom_api_window.h | 4 -- atom/browser/api/frame_subscriber.cc | 66 +++++++++++++++++++++++ atom/browser/api/frame_subscriber.h | 45 ++++++++++++++++ atom/browser/native_window.cc | 56 ------------------- atom/browser/native_window.h | 37 ------------- atom/browser/native_window_observer.h | 2 - filenames.gypi | 2 + 10 files changed, 137 insertions(+), 121 deletions(-) create mode 100644 atom/browser/api/frame_subscriber.cc create mode 100644 atom/browser/api/frame_subscriber.h diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 43b20a3d..1a9ffeb6 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -850,7 +850,21 @@ void WebContents::SendInputEvent(v8::Isolate* isolate, isolate, "Invalid event type"))); } +void WebContents::BeginFrameSubscription( + const FrameSubscriber::FrameCaptureCallback& callback) { + const auto view = web_contents()->GetRenderWidgetHostView(); + if (view) { + scoped_ptr frame_subscriber(new FrameSubscriber( + isolate(), view->GetVisibleViewportSize(), callback)); + view->BeginFrameSubscription(frame_subscriber.Pass()); + } +} +void WebContents::EndFrameSubscription() { + const auto view = web_contents()->GetRenderWidgetHostView(); + if (view) + view->EndFrameSubscription(); +} void WebContents::SetSize(const SetSizeParams& params) { if (guest_delegate_) @@ -915,6 +929,9 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( .SetMethod("tabTraverse", &WebContents::TabTraverse) .SetMethod("_send", &WebContents::SendIPCMessage, true) .SetMethod("sendInputEvent", &WebContents::SendInputEvent) + .SetMethod("beginFrameSubscription", + &WebContents::BeginFrameSubscription) + .SetMethod("endFrameSubscription", &WebContents::EndFrameSubscription) .SetMethod("setSize", &WebContents::SetSize) .SetMethod("setAllowTransparency", &WebContents::SetAllowTransparency) .SetMethod("isGuest", &WebContents::IsGuest) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 6dc26605..c8ea6908 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -8,6 +8,7 @@ #include #include +#include "atom/browser/api/frame_subscriber.h" #include "atom/browser/api/trackable_object.h" #include "atom/browser/common_web_contents_delegate.h" #include "content/public/browser/web_contents_observer.h" @@ -121,6 +122,11 @@ class WebContents : public mate::TrackableObject, // Send WebInputEvent to the page. void SendInputEvent(v8::Isolate* isolate, v8::Local input_event); + // Subscribe to the frame updates. + void BeginFrameSubscription( + const FrameSubscriber::FrameCaptureCallback& callback); + void EndFrameSubscription(); + // Methods for creating . void SetSize(const SetSizeParams& params); void SetAllowTransparency(bool allow); diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 985b6a75..3a44115d 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -13,6 +13,7 @@ #include "atom/common/native_mate_converters/gurl_converter.h" #include "atom/common/native_mate_converters/image_converter.h" #include "atom/common/native_mate_converters/string16_converter.h" +#include "atom/common/node_includes.h" #include "atom/common/options_switches.h" #include "content/public/browser/render_process_host.h" #include "native_mate/constructor.h" @@ -24,8 +25,6 @@ #include "atom/browser/ui/win/taskbar_host.h" #endif -#include "atom/common/node_includes.h" - #if defined(OS_WIN) namespace mate { @@ -105,16 +104,6 @@ void Window::WillCloseWindow(bool* prevent_default) { *prevent_default = Emit("close"); } -void Window::OnFrameRendered(scoped_ptr rgb, const int size) { - v8::Locker locker(isolate()); - v8::HandleScope handle_scope(isolate()); - v8::MaybeLocal data = node::Buffer::New( - isolate(), - reinterpret_cast(rgb.release()), - static_cast(size)); - Emit("frame-rendered", data.ToLocalChecked(), size); -} - void Window::OnWindowClosed() { if (api_web_contents_) { api_web_contents_->DestroyWebContents(); @@ -536,14 +525,6 @@ bool Window::IsVisibleOnAllWorkspaces() { return window_->IsVisibleOnAllWorkspaces(); } -void Window::BeginFrameSubscription() { - window_->SetFrameSubscription(true); -} - -void Window::EndFrameSubscription() { - window_->SetFrameSubscription(false); -} - int32_t Window::ID() const { return weak_map_id(); } @@ -628,8 +609,6 @@ void Window::BuildPrototype(v8::Isolate* isolate, &Window::SetVisibleOnAllWorkspaces) .SetMethod("isVisibleOnAllWorkspaces", &Window::IsVisibleOnAllWorkspaces) - .SetMethod("beginFrameSubscription", &Window::BeginFrameSubscription) - .SetMethod("endFrameSubscription", &Window::EndFrameSubscription) #if defined(OS_MACOSX) .SetMethod("showDefinitionForSelection", &Window::ShowDefinitionForSelection) diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 83322c16..d60bf0d8 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -53,7 +53,6 @@ class Window : public mate::TrackableObject, void OnPageTitleUpdated(bool* prevent_default, const std::string& title) override; void WillCloseWindow(bool* prevent_default) override; - void OnFrameRendered(scoped_ptr rgb, const int size) override; void OnWindowClosed() override; void OnWindowBlur() override; void OnWindowFocus() override; @@ -142,9 +141,6 @@ class Window : public mate::TrackableObject, bool IsMenuBarVisible(); void SetAspectRatio(double aspect_ratio, mate::Arguments* args); - void BeginFrameSubscription(); - void EndFrameSubscription(); - #if defined(OS_MACOSX) void ShowDefinitionForSelection(); #endif diff --git a/atom/browser/api/frame_subscriber.cc b/atom/browser/api/frame_subscriber.cc new file mode 100644 index 00000000..526769f9 --- /dev/null +++ b/atom/browser/api/frame_subscriber.cc @@ -0,0 +1,66 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/browser/api/frame_subscriber.h" + +#include "atom/common/node_includes.h" +#include "base/bind.h" +#include "media/base/video_frame.h" +#include "media/base/yuv_convert.h" + +namespace atom { + +namespace api { + +FrameSubscriber::FrameSubscriber(v8::Isolate* isolate, + const gfx::Size& size, + const FrameCaptureCallback& callback) + : isolate_(isolate), size_(size), callback_(callback) { +} + +bool FrameSubscriber::ShouldCaptureFrame( + const gfx::Rect& damage_rect, + base::TimeTicks present_time, + scoped_refptr* storage, + DeliverFrameCallback* callback) { + *storage = media::VideoFrame::CreateFrame(media::VideoFrame::YV12, size_, + gfx::Rect(size_), size_, + base::TimeDelta()); + *callback = base::Bind(&FrameSubscriber::OnFrameDelivered, + base::Unretained(this), + *storage); + return true; +} + +void FrameSubscriber::OnFrameDelivered( + scoped_refptr frame, base::TimeTicks, bool result) { + if (!result) + return; + + gfx::Rect rect = frame->visible_rect(); + size_t rgb_arr_size = rect.width() * rect.height() * 4; + v8::MaybeLocal buffer = node::Buffer::New(isolate_, rgb_arr_size); + if (buffer.IsEmpty()) + return; + + // Convert a frame of YUV to 32 bit ARGB. + media::ConvertYUVToRGB32(frame->data(media::VideoFrame::kYPlane), + frame->data(media::VideoFrame::kUPlane), + frame->data(media::VideoFrame::kVPlane), + reinterpret_cast( + node::Buffer::Data(buffer.ToLocalChecked())), + rect.width(), rect.height(), + frame->stride(media::VideoFrame::kYPlane), + frame->stride(media::VideoFrame::kUVPlane), + rect.width() * 4, + media::YV12); + + v8::Locker locker(isolate_); + v8::HandleScope handle_scope(isolate_); + callback_.Run(buffer.ToLocalChecked()); +} + +} // namespace api + +} // namespace atom diff --git a/atom/browser/api/frame_subscriber.h b/atom/browser/api/frame_subscriber.h new file mode 100644 index 00000000..f7748aa5 --- /dev/null +++ b/atom/browser/api/frame_subscriber.h @@ -0,0 +1,45 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_BROWSER_API_FRAME_SUBSCRIBER_H_ +#define ATOM_BROWSER_API_FRAME_SUBSCRIBER_H_ + +#include "base/callback.h" +#include "content/public/browser/render_widget_host_view_frame_subscriber.h" +#include "ui/gfx/geometry/size.h" +#include "v8/include/v8.h" + +namespace atom { + +namespace api { + +class FrameSubscriber : public content::RenderWidgetHostViewFrameSubscriber { + public: + using FrameCaptureCallback = base::Callback)>; + + FrameSubscriber(v8::Isolate* isolate, + const gfx::Size& size, + const FrameCaptureCallback& callback); + + bool ShouldCaptureFrame(const gfx::Rect& damage_rect, + base::TimeTicks present_time, + scoped_refptr* storage, + DeliverFrameCallback* callback) override; + + private: + void OnFrameDelivered( + scoped_refptr frame, base::TimeTicks, bool); + + v8::Isolate* isolate_; + gfx::Size size_; + FrameCaptureCallback callback_; + + DISALLOW_COPY_AND_ASSIGN(FrameSubscriber); +}; + +} // namespace api + +} // namespace atom + +#endif // ATOM_BROWSER_API_FRAME_SUBSCRIBER_H_ diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index eee53e35..4d5f2733 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -8,7 +8,6 @@ #include #include -#include "media/base/yuv_convert.h" #include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/window_list.h" @@ -283,21 +282,6 @@ void NativeWindow::SetAspectRatio(double aspect_ratio, aspect_ratio_extraSize_ = extra_size; } -void NativeWindow::SetFrameSubscription(bool isOffscreen) { - const auto view = web_contents()->GetRenderWidgetHostView(); - if (view) { - if (isOffscreen) { - scoped_ptr subscriber( - new RenderSubscriber( - view->GetVisibleViewportSize(), - base::Bind(&NativeWindow::OnFrameReceived, base::Unretained(this)))); - view->BeginFrameSubscription(subscriber.Pass()); - } else { - view->EndFrameSubscription(); - } - } -} - void NativeWindow::RequestToClosePage() { bool prevent_default = false; FOR_EACH_OBSERVER(NativeWindowObserver, @@ -517,44 +501,4 @@ void NativeWindow::OnCapturePageDone(const CapturePageCallback& callback, callback.Run(bitmap); } -void NativeWindow::OnFrameReceived(bool result, - scoped_refptr frame) { - if (result) { - gfx::Rect rect = frame->visible_rect(); - - const int rgb_arr_size = rect.width() * rect.height() * 4; - scoped_ptr rgb_bytes(new uint8[rgb_arr_size]); - - // Convert a frame of YUV to 32 bit ARGB. - media::ConvertYUVToRGB32(frame->data(media::VideoFrame::kYPlane), - frame->data(media::VideoFrame::kUPlane), - frame->data(media::VideoFrame::kVPlane), - rgb_bytes.get(), - rect.width(), rect.height(), - frame->stride(media::VideoFrame::kYPlane), - frame->stride(media::VideoFrame::kUVPlane), - rect.width() * 4, - media::YV12); - - FOR_EACH_OBSERVER(NativeWindowObserver, - observers_, - OnFrameRendered(rgb_bytes.Pass(), rgb_arr_size)); - } -} - -bool RenderSubscriber::ShouldCaptureFrame( - const gfx::Rect& damage_rect, - base::TimeTicks present_time, - scoped_refptr* storage, - DeliverFrameCallback* callback) { - last_present_time_ = present_time; - *storage = media::VideoFrame::CreateFrame(media::VideoFrame::YV12, size_, - gfx::Rect(size_), size_, - base::TimeDelta()); - - *callback = - base::Bind(&RenderSubscriber::CallbackMethod, callback_, *storage); - return true; -} - } // namespace atom diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index a593fdc3..e9a2b943 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -9,8 +9,6 @@ #include #include -#include "media/base/video_frame.h" -#include "content/public/browser/render_widget_host_view_frame_subscriber.h" #include "atom/browser/native_window_observer.h" #include "atom/browser/ui/accelerator_util.h" #include "base/cancelable_callback.h" @@ -171,9 +169,6 @@ class NativeWindow : public content::WebContentsObserver, gfx::Size GetAspectRatioExtraSize(); void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size); - // Subscribe to the frame updates. - void SetFrameSubscription(bool isOffscreen); - base::WeakPtr GetWeakPtr() { return weak_factory_.GetWeakPtr(); } @@ -228,8 +223,6 @@ class NativeWindow : public content::WebContentsObserver, has_dialog_attached_ = has_dialog_attached; } - void OnFrameReceived(bool result, scoped_refptr frame); - protected: NativeWindow(brightray::InspectableWebContents* inspectable_web_contents, const mate::Dictionary& options); @@ -303,36 +296,6 @@ class NativeWindow : public content::WebContentsObserver, DISALLOW_COPY_AND_ASSIGN(NativeWindow); }; -// This class provides a way to listen to frame renders and to use the rendered -// frames for offscreen rendering -class RenderSubscriber : public content::RenderWidgetHostViewFrameSubscriber { - public: - RenderSubscriber( - gfx::Size size, - base::Callback)> callback) - : size_(size), callback_(callback) {} - - bool ShouldCaptureFrame(const gfx::Rect& damage_rect, - base::TimeTicks present_time, - scoped_refptr* storage, - DeliverFrameCallback* callback) override; - - base::TimeTicks last_present_time() const { return last_present_time_; } - - static void CallbackMethod( - base::Callback)> callback, - scoped_refptr frame, - base::TimeTicks present_time, - bool success) { - callback.Run(success, frame); - } - - private: - gfx::Size size_; - base::Callback)> callback_; - base::TimeTicks last_present_time_; -}; - // This class provides a hook to get a NativeWindow from a WebContents. class NativeWindowRelay : public content::WebContentsUserData { diff --git a/atom/browser/native_window_observer.h b/atom/browser/native_window_observer.h index db6587de..5b0a0c56 100644 --- a/atom/browser/native_window_observer.h +++ b/atom/browser/native_window_observer.h @@ -27,8 +27,6 @@ class NativeWindowObserver { const std::string& partition_id, WindowOpenDisposition disposition) {} - virtual void OnFrameRendered(scoped_ptr rgb, const int size) {} - // Called when user is starting an navigation in web page. virtual void WillNavigate(bool* prevent_default, const GURL& url) {} diff --git a/filenames.gypi b/filenames.gypi index f25ec4a8..99d6bc6d 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -103,6 +103,8 @@ 'atom/browser/api/event_emitter.h', 'atom/browser/api/trackable_object.cc', 'atom/browser/api/trackable_object.h', + 'atom/browser/api/frame_subscriber.cc', + 'atom/browser/api/frame_subscriber.h', 'atom/browser/auto_updater.cc', 'atom/browser/auto_updater.h', 'atom/browser/auto_updater_delegate.h', -- 2.43.0