From 4d85c1a557a9e2df2a3276a126edc6e893499dfa Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 3 Aug 2016 13:46:34 +0900 Subject: [PATCH] Refactor osr_render_widget_host_view --- .../osr/osr_render_widget_host_view.cc | 202 ++++++++---------- .../browser/osr/osr_render_widget_host_view.h | 77 +++---- .../osr/osr_render_widget_host_view_mac.mm | 3 +- atom/browser/osr/osr_web_contents_view.cc | 10 +- atom/browser/osr/osr_web_contents_view.h | 1 + 5 files changed, 132 insertions(+), 161 deletions(-) diff --git a/atom/browser/osr/osr_render_widget_host_view.cc b/atom/browser/osr/osr_render_widget_host_view.cc index 6756fecc149..f1d7c6b4033 100644 --- a/atom/browser/osr/osr_render_widget_host_view.cc +++ b/atom/browser/osr/osr_render_widget_host_view.cc @@ -6,27 +6,25 @@ #include -#include "third_party/WebKit/public/platform/WebScreenInfo.h" +#include "base/callback_helpers.h" +#include "base/location.h" +#include "base/memory/ptr_util.h" +#include "base/single_thread_task_runner.h" +#include "base/time/time.h" +#include "cc/output/copy_output_request.h" +#include "cc/scheduler/delay_based_time_source.h" #include "components/display_compositor/gl_helper.h" #include "content/browser/renderer_host/render_widget_host_impl.h" #include "content/browser/renderer_host/render_widget_host_delegate.h" -#include "content/public/browser/render_widget_host_view_frame_subscriber.h" -#include "ui/events/latency_info.h" #include "content/common/view_messages.h" -#include "ui/gfx/geometry/dip_util.h" -#include "base/memory/ptr_util.h" +#include "content/public/browser/browser_thread.h" #include "content/public/browser/context_factory.h" -#include "base/single_thread_task_runner.h" +#include "content/public/browser/render_widget_host_view_frame_subscriber.h" #include "ui/compositor/layer.h" #include "ui/compositor/layer_type.h" -#include "base/callback_helpers.h" -#include "base/location.h" -#include "base/time/time.h" +#include "ui/events/latency_info.h" +#include "ui/gfx/geometry/dip_util.h" #include "ui/gfx/native_widget_types.h" -#include "ui/views/widget/widget.h" -#include "cc/output/copy_output_request.h" -#include "cc/scheduler/delay_based_time_source.h" -#include "content/public/browser/browser_thread.h" const float kDefaultScaleFactor = 1.0; @@ -336,38 +334,33 @@ class AtomBeginFrameTimer : public cc::DelayBasedTimeSourceClient { }; OffScreenRenderWidgetHostView::OffScreenRenderWidgetHostView( - const bool transparent, content::RenderWidgetHost* host, - NativeWindow* native_window): - render_widget_host_(content::RenderWidgetHostImpl::From(host)), - native_window_(native_window), - software_output_device_(NULL), - frame_rate_(60), - frame_rate_threshold_ms_(0), - transparent_(transparent), - scale_factor_(kDefaultScaleFactor), - is_showing_(!render_widget_host_->is_hidden()), - size_(native_window->GetSize()), - painting_(true), - delegated_frame_host_(new content::DelegatedFrameHost(this)), - compositor_widget_(gfx::kNullAcceleratedWidget), - weak_ptr_factory_(this) { + bool transparent, + content::RenderWidgetHost* host, + NativeWindow* native_window) + : render_widget_host_(content::RenderWidgetHostImpl::From(host)), + native_window_(native_window), + software_output_device_(nullptr), + frame_rate_(60), + frame_rate_threshold_ms_(0), + last_time_(base::Time::Now()), + transparent_(transparent), + scale_factor_(kDefaultScaleFactor), + is_showing_(!render_widget_host_->is_hidden()), + size_(native_window->GetSize()), + painting_(true), + root_layer_(new ui::Layer(ui::LAYER_SOLID_COLOR)), + delegated_frame_host_(new content::DelegatedFrameHost(this)), + weak_ptr_factory_(this) { DCHECK(render_widget_host_); render_widget_host_->SetView(this); - last_time_ = base::Time::Now(); - - root_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR)); - #if defined(OS_MACOSX) CreatePlatformWidget(); -#endif - -#if !defined(OS_MACOSX) - compositor_widget_ = native_window_->GetAcceleratedWidget(); +#else compositor_.reset( new ui::Compositor(content::GetContextFactory(), base::ThreadTaskRunnerHandle::Get())); - compositor_->SetAcceleratedWidget(compositor_widget_); + compositor_->SetAcceleratedWidget(native_window_->GetAcceleratedWidget()); #endif compositor_->SetDelegate(this); compositor_->SetRootLayer(root_layer_.get()); @@ -383,32 +376,6 @@ OffScreenRenderWidgetHostView::~OffScreenRenderWidgetHostView() { #if defined(OS_MACOSX) DestroyPlatformWidget(); #endif - - if (copy_frame_generator_.get()) - copy_frame_generator_.reset(NULL); - - delegated_frame_host_.reset(NULL); - compositor_.reset(NULL); - root_layer_.reset(NULL); -} - - -void OffScreenRenderWidgetHostView::ResizeRootLayer() { - SetupFrameRate(false); - - const float orgScaleFactor = scale_factor_; - const bool scaleFactorDidChange = (orgScaleFactor != scale_factor_); - - gfx::Size size = GetViewBounds().size(); - - if (!scaleFactorDidChange && size == root_layer_->bounds().size()) - return; - - const gfx::Size& size_in_pixels = - gfx::ConvertSizeToPixel(scale_factor_, size); - - root_layer_->SetBounds(gfx::Rect(size)); - compositor_->SetScaleAndSize(scale_factor_, size_in_pixels); } void OffScreenRenderWidgetHostView::OnBeginFrameTimerTick() { @@ -418,8 +385,8 @@ void OffScreenRenderWidgetHostView::OnBeginFrameTimerTick() { SendBeginFrame(frame_time, vsync_period); } -void OffScreenRenderWidgetHostView::SendBeginFrame(base::TimeTicks frame_time, - base::TimeDelta vsync_period) { +void OffScreenRenderWidgetHostView::SendBeginFrame( + base::TimeTicks frame_time, base::TimeDelta vsync_period) { base::TimeTicks display_time = frame_time + vsync_period; base::TimeDelta estimated_browser_composite_time = @@ -448,11 +415,6 @@ bool OffScreenRenderWidgetHostView::OnMessageReceived( return handled; } -void OffScreenRenderWidgetHostView::SetPaintCallback( - const OnPaintCallback& callback) { - callback_ = callback; -} - void OffScreenRenderWidgetHostView::InitAsChild(gfx::NativeView) { } @@ -483,7 +445,7 @@ gfx::NativeView OffScreenRenderWidgetHostView::GetNativeView() const { } gfx::NativeViewAccessible - OffScreenRenderWidgetHostView::GetNativeViewAccessible() { +OffScreenRenderWidgetHostView::GetNativeViewAccessible() { return gfx::NativeViewAccessible(); } @@ -608,7 +570,6 @@ void OffScreenRenderWidgetHostView::ClearCompositorFrame() { void OffScreenRenderWidgetHostView::InitAsPopup( content::RenderWidgetHostView* parent_host_view, const gfx::Rect& pos) { - printf("popup, parent: %p\n", parent_host_view); } void OffScreenRenderWidgetHostView::InitAsFullscreen( @@ -779,13 +740,8 @@ void OffScreenRenderWidgetHostView::DelegatedFrameHostUpdateVSyncParameters( render_widget_host_->UpdateVSyncParameters(timebase, interval); } -bool OffScreenRenderWidgetHostView::InstallTransparency() { - if (transparent_) { - SetBackgroundColor(SkColor()); - compositor_->SetHostHasTransparentBackground(true); - return true; - } - return false; +void OffScreenRenderWidgetHostView::SetBeginFrameSource( + cc::BeginFrameSource* source) { } std::unique_ptr @@ -801,6 +757,19 @@ std::unique_ptr return base::WrapUnique(software_output_device_); } +bool OffScreenRenderWidgetHostView::InstallTransparency() { + if (transparent_) { + SetBackgroundColor(SkColor()); + compositor_->SetHostHasTransparentBackground(true); + return true; + } + return false; +} + +bool OffScreenRenderWidgetHostView::IsAutoResizeEnabled() const { + return false; +} + void OffScreenRenderWidgetHostView::OnSetNeedsBeginFrames(bool enabled) { SetupFrameRate(false); @@ -811,36 +780,9 @@ void OffScreenRenderWidgetHostView::OnSetNeedsBeginFrames(bool enabled) { } } -void OffScreenRenderWidgetHostView::SetupFrameRate(bool force) { - if (!force && frame_rate_threshold_ms_ != 0) - return; - - frame_rate_threshold_ms_ = 1000 / frame_rate_; - - compositor_->vsync_manager()->SetAuthoritativeVSyncInterval( - base::TimeDelta::FromMilliseconds(frame_rate_threshold_ms_)); - - if (copy_frame_generator_.get()) { - copy_frame_generator_->set_frame_rate_threshold_ms( - frame_rate_threshold_ms_); - } - - if (begin_frame_timer_.get()) { - begin_frame_timer_->SetFrameRateThresholdMs(frame_rate_threshold_ms_); - } else { - begin_frame_timer_.reset(new AtomBeginFrameTimer( - frame_rate_threshold_ms_, - base::Bind(&OffScreenRenderWidgetHostView::OnBeginFrameTimerTick, - weak_ptr_factory_.GetWeakPtr()))); - } -} - -void OffScreenRenderWidgetHostView::SetBeginFrameSource( - cc::BeginFrameSource* source) { -} - -bool OffScreenRenderWidgetHostView::IsAutoResizeEnabled() const { - return false; +void OffScreenRenderWidgetHostView::SetPaintCallback( + const OnPaintCallback& callback) { + callback_ = callback; } void OffScreenRenderWidgetHostView::OnPaint( @@ -880,4 +822,46 @@ int OffScreenRenderWidgetHostView::GetFrameRate() const { return frame_rate_; } +void OffScreenRenderWidgetHostView::SetupFrameRate(bool force) { + if (!force && frame_rate_threshold_ms_ != 0) + return; + + frame_rate_threshold_ms_ = 1000 / frame_rate_; + + compositor_->vsync_manager()->SetAuthoritativeVSyncInterval( + base::TimeDelta::FromMilliseconds(frame_rate_threshold_ms_)); + + if (copy_frame_generator_.get()) { + copy_frame_generator_->set_frame_rate_threshold_ms( + frame_rate_threshold_ms_); + } + + if (begin_frame_timer_.get()) { + begin_frame_timer_->SetFrameRateThresholdMs(frame_rate_threshold_ms_); + } else { + begin_frame_timer_.reset(new AtomBeginFrameTimer( + frame_rate_threshold_ms_, + base::Bind(&OffScreenRenderWidgetHostView::OnBeginFrameTimerTick, + weak_ptr_factory_.GetWeakPtr()))); + } +} + +void OffScreenRenderWidgetHostView::ResizeRootLayer() { + SetupFrameRate(false); + + const float orgScaleFactor = scale_factor_; + const bool scaleFactorDidChange = (orgScaleFactor != scale_factor_); + + gfx::Size size = GetViewBounds().size(); + + if (!scaleFactorDidChange && size == root_layer_->bounds().size()) + return; + + const gfx::Size& size_in_pixels = + gfx::ConvertSizeToPixel(scale_factor_, size); + + root_layer_->SetBounds(gfx::Rect(size)); + compositor_->SetScaleAndSize(scale_factor_, size_in_pixels); +} + } // namespace atom diff --git a/atom/browser/osr/osr_render_widget_host_view.h b/atom/browser/osr/osr_render_widget_host_view.h index 8460af2ae4e..9c1fc64024a 100644 --- a/atom/browser/osr/osr_render_widget_host_view.h +++ b/atom/browser/osr/osr_render_widget_host_view.h @@ -43,11 +43,9 @@ #ifdef __OBJC__ @class CALayer; @class NSWindow; -@class NSTextInputContext; #else class CALayer; class NSWindow; -class NSTextInputContext; #endif #endif @@ -56,19 +54,20 @@ namespace atom { class AtomCopyFrameGenerator; class AtomBeginFrameTimer; -class OffScreenRenderWidgetHostView: - public content::RenderWidgetHostViewBase, +class OffScreenRenderWidgetHostView + : public content::RenderWidgetHostViewBase, #if defined(OS_MACOSX) - public ui::AcceleratedWidgetMacNSView, + public ui::AcceleratedWidgetMacNSView, #endif - public ui::CompositorDelegate, - public content::DelegatedFrameHostClient { + public ui::CompositorDelegate, + public content::DelegatedFrameHostClient { public: - OffScreenRenderWidgetHostView(const bool transparent, - content::RenderWidgetHost*, NativeWindow*); - ~OffScreenRenderWidgetHostView(); + OffScreenRenderWidgetHostView(bool transparent, + content::RenderWidgetHost* render_widget_host, + NativeWindow* native_window); + ~OffScreenRenderWidgetHostView() override; - // content::RenderWidgetHostView + // content::RenderWidgetHostView: bool OnMessageReceived(const IPC::Message&) override; void InitAsChild(gfx::NativeView) override; content::RenderWidgetHost* GetRenderWidgetHost(void) const override; @@ -91,7 +90,6 @@ class OffScreenRenderWidgetHostView: bool LockMouse(void) override; void UnlockMouse(void) override; bool GetScreenColorProfile(std::vector*) override; - #if defined(OS_MACOSX) ui::AcceleratedWidgetMac* GetAcceleratedWidgetMac() const override; void SetActive(bool active) override; @@ -102,7 +100,7 @@ class OffScreenRenderWidgetHostView: void StopSpeaking() override; #endif // defined(OS_MACOSX) - // content::RenderWidgetHostViewBase + // content::RenderWidgetHostViewBase: void OnSwapCompositorFrame(uint32_t, std::unique_ptr) override; void ClearCompositorFrame(void) override; @@ -116,13 +114,11 @@ class OffScreenRenderWidgetHostView: void RenderProcessGone(base::TerminationStatus, int) override; void Destroy(void) override; void SetTooltipText(const base::string16 &) override; - #if defined(OS_MACOSX) void SelectionChanged(const base::string16& text, size_t offset, const gfx::Range& range) override; #endif - void SelectionBoundsChanged(const ViewHostMsg_SelectionBounds_Params &) override; void CopyFromCompositingSurface(const gfx::Rect &, @@ -149,7 +145,7 @@ class OffScreenRenderWidgetHostView: gfx::Size GetPhysicalBackingSize() const override; gfx::Size GetRequestedRendererSize() const override; - // content::DelegatedFrameHostClient + // content::DelegatedFrameHostClient: int DelegatedFrameHostGetGpuMemoryBufferClientId(void) const; ui::Layer *DelegatedFrameHostGetLayer(void) const override; bool DelegatedFrameHostIsVisible(void) const override; @@ -168,35 +164,32 @@ class OffScreenRenderWidgetHostView: const base::TimeTicks &, const base::TimeDelta &) override; void SetBeginFrameSource(cc::BeginFrameSource* source) override; - bool InstallTransparency(); - - bool IsAutoResizeEnabled() const; - + // ui::CompositorDelegate: std::unique_ptr CreateSoftwareOutputDevice( ui::Compositor* compositor) override; + + bool InstallTransparency(); + bool IsAutoResizeEnabled() const; void OnSetNeedsBeginFrames(bool enabled); #if defined(OS_MACOSX) - // AcceleratedWidgetMacNSView implementation. + // ui::AcceleratedWidgetMacNSView: NSView* AcceleratedWidgetGetNSView() const override; void AcceleratedWidgetGetVSyncParameters( base::TimeTicks* timebase, base::TimeDelta* interval) const override; void AcceleratedWidgetSwapCompleted() override; #endif // defined(OS_MACOSX) - ui::Compositor* compositor() const { return compositor_.get(); } - content::RenderWidgetHostImpl* render_widget_host() const - { return render_widget_host_; } - void OnBeginFrameTimerTick(); void SendBeginFrame(base::TimeTicks frame_time, base::TimeDelta vsync_period); +#if defined(OS_MACOSX) void CreatePlatformWidget(); void DestroyPlatformWidget(); +#endif void SetPaintCallback(const OnPaintCallback& callback); - void OnPaint(const gfx::Rect& damage_rect, const gfx::Size& bitmap_size, void* bitmap_pixels); @@ -206,16 +199,18 @@ class OffScreenRenderWidgetHostView: void SetFrameRate(int frame_rate); int GetFrameRate() const; -private: + + ui::Compositor* compositor() const { return compositor_.get(); } + content::RenderWidgetHostImpl* render_widget_host() const + { return render_widget_host_; } + + private: void SetupFrameRate(bool force); void ResizeRootLayer(); + // Weak ptrs. content::RenderWidgetHostImpl* render_widget_host_; NativeWindow* native_window_; - - std::unique_ptr copy_frame_generator_; - std::unique_ptr begin_frame_timer_; - OffScreenOutputDevice* software_output_device_; OnPaintCallback callback_; @@ -232,32 +227,24 @@ private: gfx::Size size_; bool painting_; - std::unique_ptr delegated_frame_host_; - std::unique_ptr compositor_; - gfx::AcceleratedWidget compositor_widget_; std::unique_ptr root_layer_; + std::unique_ptr compositor_; + std::unique_ptr delegated_frame_host_; + + std::unique_ptr copy_frame_generator_; + std::unique_ptr begin_frame_timer_; #if defined(OS_MACOSX) NSWindow* window_; CALayer* background_layer_; std::unique_ptr browser_compositor_; - NSTextInputContext* text_input_context_osr_mac_; // Selected text on the renderer. std::string selected_text_; - - // The current composition character range and its bounds. - gfx::Range composition_range_; - std::vector composition_bounds_; - - // The current caret bounds. - gfx::Rect caret_rect_; - - // The current first selection bounds. - gfx::Rect first_selection_rect_; #endif base::WeakPtrFactory weak_ptr_factory_; + DISALLOW_COPY_AND_ASSIGN(OffScreenRenderWidgetHostView); }; diff --git a/atom/browser/osr/osr_render_widget_host_view_mac.mm b/atom/browser/osr/osr_render_widget_host_view_mac.mm index 9aba2ee3d02..511012f5db2 100644 --- a/atom/browser/osr/osr_render_widget_host_view_mac.mm +++ b/atom/browser/osr/osr_render_widget_host_view_mac.mm @@ -10,8 +10,7 @@ #include "ui/accelerated_widget_mac/accelerated_widget_mac.h" ui::AcceleratedWidgetMac* - atom::OffScreenRenderWidgetHostView::GetAcceleratedWidgetMac() - const { +atom::OffScreenRenderWidgetHostView::GetAcceleratedWidgetMac() const { if (browser_compositor_) return browser_compositor_->accelerated_widget_mac(); return nullptr; diff --git a/atom/browser/osr/osr_web_contents_view.cc b/atom/browser/osr/osr_web_contents_view.cc index 6e0d10d2053..6d315ae9c16 100644 --- a/atom/browser/osr/osr_web_contents_view.cc +++ b/atom/browser/osr/osr_web_contents_view.cc @@ -6,9 +6,9 @@ namespace atom { -OffScreenWebContentsView::OffScreenWebContentsView(bool transparent): - transparent_(transparent), - web_contents_(nullptr) { +OffScreenWebContentsView::OffScreenWebContentsView(bool transparent) + : transparent_(transparent), + web_contents_(nullptr) { } OffScreenWebContentsView::~OffScreenWebContentsView() { @@ -67,7 +67,7 @@ content::RenderWidgetHostViewBase* content::RenderWidgetHost* render_widget_host, bool is_guest_view_hack) { auto relay = NativeWindowRelay::FromWebContents(web_contents_); view_ = new OffScreenRenderWidgetHostView(transparent_, render_widget_host, - relay->window.get()); + relay->window.get()); return view_; } @@ -76,7 +76,7 @@ content::RenderWidgetHostViewBase* content::RenderWidgetHost* render_widget_host) { auto relay = NativeWindowRelay::FromWebContents(web_contents_); view_ = new OffScreenRenderWidgetHostView(transparent_, render_widget_host, - relay->window.get()); + relay->window.get()); return view_; } diff --git a/atom/browser/osr/osr_web_contents_view.h b/atom/browser/osr/osr_web_contents_view.h index d3de42c3046..919a14cd593 100644 --- a/atom/browser/osr/osr_web_contents_view.h +++ b/atom/browser/osr/osr_web_contents_view.h @@ -63,6 +63,7 @@ class OffScreenWebContentsView : public content::WebContentsView, private: const bool transparent_; + // Weak refs. OffScreenRenderWidgetHostView* view_; content::WebContents* web_contents_; };