diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index d9b47d0555..9418162840 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -1418,6 +1418,15 @@ int WebContents::GetFrameRate() const { return osr_rwhv ? osr_rwhv->GetFrameRate() : 0; } +void WebContents::Invalidate() { + if (!IsOffScreen()) + return; + + auto* osr_rwhv = static_cast( + web_contents()->GetRenderWidgetHostView()); + if (osr_rwhv) + osr_rwhv->Invalidate(); +} v8::Local WebContents::GetWebPreferences(v8::Isolate* isolate) { WebContentsPreferences* web_preferences = @@ -1527,6 +1536,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, .SetMethod("isPainting", &WebContents::IsPainting) .SetMethod("setFrameRate", &WebContents::SetFrameRate) .SetMethod("getFrameRate", &WebContents::GetFrameRate) + .SetMethod("invalidate", &WebContents::Invalidate) .SetMethod("getType", &WebContents::GetType) .SetMethod("getWebPreferences", &WebContents::GetWebPreferences) .SetMethod("getOwnerBrowserWindow", &WebContents::GetOwnerBrowserWindow) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index aa78ec9ae8..a6b3fd3fd7 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -164,6 +164,7 @@ class WebContents : public mate::TrackableObject, bool IsPainting() const; void SetFrameRate(int frame_rate); int GetFrameRate() const; + void Invalidate(); // Callback triggered on permission response. void OnEnterFullscreenModeForTab(content::WebContents* source, diff --git a/atom/browser/osr/osr_render_widget_host_view.cc b/atom/browser/osr/osr_render_widget_host_view.cc index 606aba3ddd..918fced503 100644 --- a/atom/browser/osr/osr_render_widget_host_view.cc +++ b/atom/browser/osr/osr_render_widget_host_view.cc @@ -371,6 +371,8 @@ OffScreenRenderWidgetHostView::OffScreenRenderWidgetHostView( compositor_->SetRootLayer(root_layer_.get()); #endif + native_window_->AddObserver(this); + ResizeRootLayer(); } @@ -439,11 +441,7 @@ content::RenderWidgetHost* OffScreenRenderWidgetHostView::GetRenderWidgetHost() void OffScreenRenderWidgetHostView::SetSize(const gfx::Size& size) { size_ = size; - const gfx::Size& size_in_pixels = - gfx::ConvertSizeToPixel(scale_factor_, size); - - GetRootLayer()->SetBounds(gfx::Rect(size)); - GetCompositor()->SetScaleAndSize(scale_factor_, size_in_pixels); + ResizeRootLayer(); } void OffScreenRenderWidgetHostView::SetBounds(const gfx::Rect& new_bounds) { @@ -892,6 +890,16 @@ void OffScreenRenderWidgetHostView::SetupFrameRate(bool force) { } } +void OffScreenRenderWidgetHostView::Invalidate() { + const gfx::Rect& bounds_in_pixels = GetViewBounds(); + + if (software_output_device_) { + software_output_device_->OnPaint(bounds_in_pixels); + } else if (copy_frame_generator_.get()) { + copy_frame_generator_->GenerateCopyFrame(true, bounds_in_pixels); + } +} + void OffScreenRenderWidgetHostView::ResizeRootLayer() { SetupFrameRate(false); @@ -910,4 +918,10 @@ void OffScreenRenderWidgetHostView::ResizeRootLayer() { GetCompositor()->SetScaleAndSize(scale_factor_, size_in_pixels); } +void OffScreenRenderWidgetHostView::OnWindowResize() { + // In offscreen mode call RenderWidgetHostView's SetSize explicitly + auto size = native_window_->GetSize(); + SetSize(size); +} + } // 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 c6641ec06a..8ea3003a4e 100644 --- a/atom/browser/osr/osr_render_widget_host_view.h +++ b/atom/browser/osr/osr_render_widget_host_view.h @@ -13,6 +13,7 @@ #endif #include "atom/browser/native_window.h" +#include "atom/browser/native_window_observer.h" #include "atom/browser/osr/osr_output_device.h" #include "base/process/kill.h" #include "base/threading/thread.h" @@ -60,7 +61,8 @@ class MacHelper; class OffScreenRenderWidgetHostView : public content::RenderWidgetHostViewBase, public ui::CompositorDelegate, - public content::DelegatedFrameHostClient { + public content::DelegatedFrameHostClient, + public NativeWindowObserver { public: OffScreenRenderWidgetHostView(bool transparent, const OnPaintCallback& callback, @@ -172,6 +174,9 @@ class OffScreenRenderWidgetHostView bool IsAutoResizeEnabled() const; void OnSetNeedsBeginFrames(bool enabled); + // NativeWindowObserver: + void OnWindowResize() override; + void OnBeginFrameTimerTick(); void SendBeginFrame(base::TimeTicks frame_time, base::TimeDelta vsync_period); @@ -193,6 +198,8 @@ class OffScreenRenderWidgetHostView ui::Layer* GetRootLayer() const; content::DelegatedFrameHost* GetDelegatedFrameHost() const; + void Invalidate(); + content::RenderWidgetHostImpl* render_widget_host() const { return render_widget_host_; } NativeWindow* window() const { return native_window_; } diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 55cbfc63c3..289b0e4b09 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -1146,6 +1146,11 @@ Only values between 1 and 60 are accepted. If *offscreen rendering* is enabled returns the current frame rate. +#### `contents.invalidate()` + +If *offscreen rendering* is enabled invalidates the frame and generates a new +one through the `'paint'` event. + ### Instance Properties #### `contents.id`