From ea8ea1543f9704a08ae05d26cd4b1de556bad15f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 3 Aug 2016 13:04:36 +0900 Subject: [PATCH] Refactor osr_output_device --- atom/browser/api/atom_api_web_contents.cc | 24 ++++++++++--------- atom/browser/api/atom_api_web_contents.h | 5 ++-- atom/browser/osr/osr_output_device.cc | 22 ++++++++--------- atom/browser/osr/osr_output_device.h | 9 ++++--- .../osr/osr_render_widget_host_view.cc | 15 +++++------- .../browser/osr/osr_render_widget_host_view.h | 5 ++-- atom/browser/web_contents_preferences.cc | 2 +- spec/api-browser-window-spec.js | 2 +- 8 files changed, 40 insertions(+), 44 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 10bcbfa150d2..74fb41afd21a 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -314,8 +314,9 @@ WebContents::WebContents(v8::Isolate* isolate, options.Get("transparent", &transparent); content::WebContents::CreateParams params(session->browser_context()); - params.view = new OffScreenWebContentsView(transparent); - params.delegate_view = params.view; + auto* view = new OffScreenWebContentsView(transparent); + params.view = view; + params.delegate_view = view; web_contents = content::WebContents::Create(params); view->SetWebContents(web_contents); @@ -609,9 +610,9 @@ void WebContents::DocumentLoadedInFrame( content::RenderFrameHost* render_frame_host) { if (!render_frame_host->GetParent()) { if (IsOffScreen()) { - const auto* rwhv = web_contents()->GetRenderWidgetHostView(); + auto* rwhv = web_contents()->GetRenderWidgetHostView(); static_cast(rwhv)->SetPaintCallback( - base::Bind(&WebContents::OnPaint, base::Unretained(this), isolate)); + base::Bind(&WebContents::OnPaint, base::Unretained(this))); } Emit("dom-ready"); @@ -1347,19 +1348,20 @@ bool WebContents::IsOffScreen() const { return type_ == OFF_SCREEN; } -void WebContents::OnPaint(v8::Isolate* isolate, const gfx::Rect& dirty_rect, - const gfx::Size& bitmap_size, void* bitmap_pixels) { +void WebContents::OnPaint(const gfx::Rect& dirty_rect, + const gfx::Size& bitmap_size, + void* bitmap_pixels) { v8::MaybeLocal buffer = node::Buffer::New( - isolate, reinterpret_cast(bitmap_pixels), sizeof(bitmap_pixels)); + isolate(), reinterpret_cast(bitmap_pixels), sizeof(bitmap_pixels)); if (!buffer.IsEmpty()) - Emit("paint", damage_rect, buffer.ToLocalChecked(), bitmap_size); + Emit("paint", dirty_rect, buffer.ToLocalChecked(), bitmap_size); } void WebContents::StartPainting() { if (!IsOffScreen()) return; - const auto* osr_rwhv = static_cast( + auto* osr_rwhv = static_cast( web_contents()->GetRenderWidgetHostView()); if (osr_rwhv) { osr_rwhv->SetPainting(true); @@ -1371,7 +1373,7 @@ void WebContents::StopPainting() { if (!IsOffScreen()) return; - const auto* osr_rwhv = static_cast( + auto* osr_rwhv = static_cast( web_contents()->GetRenderWidgetHostView()); if (osr_rwhv) { osr_rwhv->SetPainting(false); @@ -1392,7 +1394,7 @@ void WebContents::SetFrameRate(int frame_rate) { if (!IsOffScreen()) return; - const auto* osr_rwhv = static_cast( + auto* osr_rwhv = static_cast( web_contents()->GetRenderWidgetHostView()); if (osr_rwhv) osr_rwhv->SetFrameRate(frame_rate); diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 4b4290e6a18a..3a6cbaa3f9e1 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -158,8 +158,9 @@ class WebContents : public mate::TrackableObject, // Methods for offscreen rendering bool IsOffScreen() const; - void OnPaint(v8::Isolate* isolate, const gfx::Rect& dirty_rect, - const gfx::Size& bitmap_size, void* bitmap_pixels); + void OnPaint(const gfx::Rect& dirty_rect, + const gfx::Size& bitmap_size, + void* bitmap_pixels); void StartPainting(); void StopPainting(); bool IsPainting() const; diff --git a/atom/browser/osr/osr_output_device.cc b/atom/browser/osr/osr_output_device.cc index 897748ad8b23..9e84ff23f6ef 100644 --- a/atom/browser/osr/osr_output_device.cc +++ b/atom/browser/osr/osr_output_device.cc @@ -10,10 +10,10 @@ namespace atom { OffScreenOutputDevice::OffScreenOutputDevice(bool transparent, - const OnPaintCallback& callback): - transparent_(transparent), - callback_(callback), - active_(false) { + const OnPaintCallback& callback) + : transparent_(transparent), + callback_(callback), + active_(false) { DCHECK(!callback_.is_null()); } @@ -27,21 +27,21 @@ void OffScreenOutputDevice::Resize( if (viewport_pixel_size_ == pixel_size) return; viewport_pixel_size_ = pixel_size; - canvas_.reset(NULL); + canvas_.reset(); bitmap_.reset(new SkBitmap); bitmap_->allocN32Pixels(viewport_pixel_size_.width(), viewport_pixel_size_.height(), !transparent_); if (bitmap_->drawsNothing()) { NOTREACHED(); - bitmap_.reset(NULL); + bitmap_.reset(); return; } if (transparent_) bitmap_->eraseARGB(0, 0, 0, 0); - canvas_.reset(new SkCanvas(*bitmap_.get())); + canvas_.reset(new SkCanvas(*bitmap_)); } SkCanvas* OffScreenOutputDevice::BeginPaint(const gfx::Rect& damage_rect) { @@ -71,8 +71,7 @@ void OffScreenOutputDevice::SetActive(bool active) { active_ = active; if (active_) - OnPaint(gfx::Rect(0, 0, viewport_pixel_size_.width(), - viewport_pixel_size_.height())); + OnPaint(gfx::Rect(viewport_pixel_size_)); } void OffScreenOutputDevice::OnPaint(const gfx::Rect& damage_rect) { @@ -86,9 +85,8 @@ void OffScreenOutputDevice::OnPaint(const gfx::Rect& damage_rect) { if (rect.IsEmpty()) return; - SkAutoLockPixels bitmap_pixels_lock(*bitmap_.get()); - - callback_.Run(rect, bitmap_->width(), bitmap_->height(), + SkAutoLockPixels bitmap_pixels_lock(*bitmap_); + callback_.Run(rect, gfx::Size(bitmap_->width(), bitmap_->height()), bitmap_->getPixels()); } diff --git a/atom/browser/osr/osr_output_device.h b/atom/browser/osr/osr_output_device.h index 34402e17c051..1cec55037fe3 100644 --- a/atom/browser/osr/osr_output_device.h +++ b/atom/browser/osr/osr_output_device.h @@ -12,26 +12,25 @@ namespace atom { -typedef base::Callback OnPaintCallback; +typedef base::Callback OnPaintCallback; class OffScreenOutputDevice : public cc::SoftwareOutputDevice { public: OffScreenOutputDevice(bool transparent, const OnPaintCallback& callback); ~OffScreenOutputDevice(); + // cc::SoftwareOutputDevice: void Resize(const gfx::Size& pixel_size, float scale_factor) override; - SkCanvas* BeginPaint(const gfx::Rect& damage_rect) override; - void EndPaint() override; void SetActive(bool active); - void OnPaint(const gfx::Rect& damage_rect); private: const bool transparent_; - const OnPaintCallback callback_; + OnPaintCallback callback_; bool active_; diff --git a/atom/browser/osr/osr_render_widget_host_view.cc b/atom/browser/osr/osr_render_widget_host_view.cc index c009b9158ca7..6756fecc1490 100644 --- a/atom/browser/osr/osr_render_widget_host_view.cc +++ b/atom/browser/osr/osr_render_widget_host_view.cc @@ -259,9 +259,9 @@ class AtomCopyFrameGenerator { const gfx::Rect& damage_rect, const SkBitmap& bitmap, std::unique_ptr bitmap_pixels_lock) { - uint8_t* pixels = reinterpret_cast(bitmap.getPixels()); - - view_->OnPaint(damage_rect, bitmap.width(), bitmap.height(), pixels); + view_->OnPaint(damage_rect, + gfx::Size(bitmap.width(), bitmap.height()), + bitmap.getPixels()); if (frame_retry_count_ > 0) frame_retry_count_ = 0; @@ -845,15 +845,12 @@ bool OffScreenRenderWidgetHostView::IsAutoResizeEnabled() const { void OffScreenRenderWidgetHostView::OnPaint( const gfx::Rect& damage_rect, - int bitmap_width, - int bitmap_height, + const gfx::Size& bitmap_size, void* bitmap_pixels) { TRACE_EVENT0("electron", "OffScreenRenderWidgetHostView::OnPaint"); - if (!callback_.is_null()) { - callback_.Run(damage_rect, gfx::Size(bitmap_width, bitmap_height), - bitmap_pixels); - } + if (!callback_.is_null()) + callback_.Run(damage_rect, bitmap_size, bitmap_pixels); } void OffScreenRenderWidgetHostView::SetPainting(bool painting) { diff --git a/atom/browser/osr/osr_render_widget_host_view.h b/atom/browser/osr/osr_render_widget_host_view.h index 6c9fe7755e4d..8460af2ae4e1 100644 --- a/atom/browser/osr/osr_render_widget_host_view.h +++ b/atom/browser/osr/osr_render_widget_host_view.h @@ -198,8 +198,7 @@ class OffScreenRenderWidgetHostView: void SetPaintCallback(const OnPaintCallback& callback); void OnPaint(const gfx::Rect& damage_rect, - int bitmap_width, - int bitmap_height, + const gfx::Size& bitmap_size, void* bitmap_pixels); void SetPainting(bool painting); @@ -219,7 +218,7 @@ private: OffScreenOutputDevice* software_output_device_; - const OnPaintCallback callback_; + OnPaintCallback callback_; int frame_rate_; int frame_rate_threshold_ms_; diff --git a/atom/browser/web_contents_preferences.cc b/atom/browser/web_contents_preferences.cc index 49a42a3ee128..7faf8c8899ca 100644 --- a/atom/browser/web_contents_preferences.cc +++ b/atom/browser/web_contents_preferences.cc @@ -190,7 +190,7 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches( // Use frame scheduling for offscreen renderers. // TODO(zcbenz): Remove this after Chrome 54, on which it becomes default. bool offscreen; - if (web_preferences.Get("offscreen", &offscreen) && offscreen) + if (web_preferences.GetBoolean("offscreen", &offscreen) && offscreen) command_line->AppendSwitch(cc::switches::kEnableBeginFrameScheduling); } diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 645ced7e9ded..b5cfea354dff 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1244,8 +1244,8 @@ describe('browser-window module', function () { assert.equal(w.webContents.getFrameRate(), 60) done() }) + w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html') }) - w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html') }) describe('window.webContents.setFrameRate(frameRate)', function () {