Refactor osr_output_device

This commit is contained in:
Cheng Zhao 2016-08-03 13:04:36 +09:00
parent 9d8e510a55
commit ea8ea1543f
8 changed files with 40 additions and 44 deletions

View file

@ -314,8 +314,9 @@ WebContents::WebContents(v8::Isolate* isolate,
options.Get("transparent", &transparent); options.Get("transparent", &transparent);
content::WebContents::CreateParams params(session->browser_context()); content::WebContents::CreateParams params(session->browser_context());
params.view = new OffScreenWebContentsView(transparent); auto* view = new OffScreenWebContentsView(transparent);
params.delegate_view = params.view; params.view = view;
params.delegate_view = view;
web_contents = content::WebContents::Create(params); web_contents = content::WebContents::Create(params);
view->SetWebContents(web_contents); view->SetWebContents(web_contents);
@ -609,9 +610,9 @@ void WebContents::DocumentLoadedInFrame(
content::RenderFrameHost* render_frame_host) { content::RenderFrameHost* render_frame_host) {
if (!render_frame_host->GetParent()) { if (!render_frame_host->GetParent()) {
if (IsOffScreen()) { if (IsOffScreen()) {
const auto* rwhv = web_contents()->GetRenderWidgetHostView(); auto* rwhv = web_contents()->GetRenderWidgetHostView();
static_cast<OffScreenRenderWidgetHostView*>(rwhv)->SetPaintCallback( static_cast<OffScreenRenderWidgetHostView*>(rwhv)->SetPaintCallback(
base::Bind(&WebContents::OnPaint, base::Unretained(this), isolate)); base::Bind(&WebContents::OnPaint, base::Unretained(this)));
} }
Emit("dom-ready"); Emit("dom-ready");
@ -1347,19 +1348,20 @@ bool WebContents::IsOffScreen() const {
return type_ == OFF_SCREEN; return type_ == OFF_SCREEN;
} }
void WebContents::OnPaint(v8::Isolate* isolate, const gfx::Rect& dirty_rect, void WebContents::OnPaint(const gfx::Rect& dirty_rect,
const gfx::Size& bitmap_size, void* bitmap_pixels) { const gfx::Size& bitmap_size,
void* bitmap_pixels) {
v8::MaybeLocal<v8::Object> buffer = node::Buffer::New( v8::MaybeLocal<v8::Object> buffer = node::Buffer::New(
isolate, reinterpret_cast<char*>(bitmap_pixels), sizeof(bitmap_pixels)); isolate(), reinterpret_cast<char*>(bitmap_pixels), sizeof(bitmap_pixels));
if (!buffer.IsEmpty()) if (!buffer.IsEmpty())
Emit("paint", damage_rect, buffer.ToLocalChecked(), bitmap_size); Emit("paint", dirty_rect, buffer.ToLocalChecked(), bitmap_size);
} }
void WebContents::StartPainting() { void WebContents::StartPainting() {
if (!IsOffScreen()) if (!IsOffScreen())
return; return;
const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>( auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
web_contents()->GetRenderWidgetHostView()); web_contents()->GetRenderWidgetHostView());
if (osr_rwhv) { if (osr_rwhv) {
osr_rwhv->SetPainting(true); osr_rwhv->SetPainting(true);
@ -1371,7 +1373,7 @@ void WebContents::StopPainting() {
if (!IsOffScreen()) if (!IsOffScreen())
return; return;
const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>( auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
web_contents()->GetRenderWidgetHostView()); web_contents()->GetRenderWidgetHostView());
if (osr_rwhv) { if (osr_rwhv) {
osr_rwhv->SetPainting(false); osr_rwhv->SetPainting(false);
@ -1392,7 +1394,7 @@ void WebContents::SetFrameRate(int frame_rate) {
if (!IsOffScreen()) if (!IsOffScreen())
return; return;
const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>( auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
web_contents()->GetRenderWidgetHostView()); web_contents()->GetRenderWidgetHostView());
if (osr_rwhv) if (osr_rwhv)
osr_rwhv->SetFrameRate(frame_rate); osr_rwhv->SetFrameRate(frame_rate);

View file

@ -158,8 +158,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
// Methods for offscreen rendering // Methods for offscreen rendering
bool IsOffScreen() const; bool IsOffScreen() const;
void OnPaint(v8::Isolate* isolate, const gfx::Rect& dirty_rect, void OnPaint(const gfx::Rect& dirty_rect,
const gfx::Size& bitmap_size, void* bitmap_pixels); const gfx::Size& bitmap_size,
void* bitmap_pixels);
void StartPainting(); void StartPainting();
void StopPainting(); void StopPainting();
bool IsPainting() const; bool IsPainting() const;

View file

@ -10,8 +10,8 @@
namespace atom { namespace atom {
OffScreenOutputDevice::OffScreenOutputDevice(bool transparent, OffScreenOutputDevice::OffScreenOutputDevice(bool transparent,
const OnPaintCallback& callback): const OnPaintCallback& callback)
transparent_(transparent), : transparent_(transparent),
callback_(callback), callback_(callback),
active_(false) { active_(false) {
DCHECK(!callback_.is_null()); DCHECK(!callback_.is_null());
@ -27,21 +27,21 @@ void OffScreenOutputDevice::Resize(
if (viewport_pixel_size_ == pixel_size) return; if (viewport_pixel_size_ == pixel_size) return;
viewport_pixel_size_ = pixel_size; viewport_pixel_size_ = pixel_size;
canvas_.reset(NULL); canvas_.reset();
bitmap_.reset(new SkBitmap); bitmap_.reset(new SkBitmap);
bitmap_->allocN32Pixels(viewport_pixel_size_.width(), bitmap_->allocN32Pixels(viewport_pixel_size_.width(),
viewport_pixel_size_.height(), viewport_pixel_size_.height(),
!transparent_); !transparent_);
if (bitmap_->drawsNothing()) { if (bitmap_->drawsNothing()) {
NOTREACHED(); NOTREACHED();
bitmap_.reset(NULL); bitmap_.reset();
return; return;
} }
if (transparent_) if (transparent_)
bitmap_->eraseARGB(0, 0, 0, 0); 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) { SkCanvas* OffScreenOutputDevice::BeginPaint(const gfx::Rect& damage_rect) {
@ -71,8 +71,7 @@ void OffScreenOutputDevice::SetActive(bool active) {
active_ = active; active_ = active;
if (active_) if (active_)
OnPaint(gfx::Rect(0, 0, viewport_pixel_size_.width(), OnPaint(gfx::Rect(viewport_pixel_size_));
viewport_pixel_size_.height()));
} }
void OffScreenOutputDevice::OnPaint(const gfx::Rect& damage_rect) { void OffScreenOutputDevice::OnPaint(const gfx::Rect& damage_rect) {
@ -86,9 +85,8 @@ void OffScreenOutputDevice::OnPaint(const gfx::Rect& damage_rect) {
if (rect.IsEmpty()) if (rect.IsEmpty())
return; return;
SkAutoLockPixels bitmap_pixels_lock(*bitmap_.get()); SkAutoLockPixels bitmap_pixels_lock(*bitmap_);
callback_.Run(rect, gfx::Size(bitmap_->width(), bitmap_->height()),
callback_.Run(rect, bitmap_->width(), bitmap_->height(),
bitmap_->getPixels()); bitmap_->getPixels());
} }

View file

@ -12,26 +12,25 @@
namespace atom { namespace atom {
typedef base::Callback<void(const gfx::Rect&, int, int, void*)> OnPaintCallback; typedef base::Callback<void(const gfx::Rect&,
const gfx::Size&, void*)> OnPaintCallback;
class OffScreenOutputDevice : public cc::SoftwareOutputDevice { class OffScreenOutputDevice : public cc::SoftwareOutputDevice {
public: public:
OffScreenOutputDevice(bool transparent, const OnPaintCallback& callback); OffScreenOutputDevice(bool transparent, const OnPaintCallback& callback);
~OffScreenOutputDevice(); ~OffScreenOutputDevice();
// cc::SoftwareOutputDevice:
void Resize(const gfx::Size& pixel_size, float scale_factor) override; void Resize(const gfx::Size& pixel_size, float scale_factor) override;
SkCanvas* BeginPaint(const gfx::Rect& damage_rect) override; SkCanvas* BeginPaint(const gfx::Rect& damage_rect) override;
void EndPaint() override; void EndPaint() override;
void SetActive(bool active); void SetActive(bool active);
void OnPaint(const gfx::Rect& damage_rect); void OnPaint(const gfx::Rect& damage_rect);
private: private:
const bool transparent_; const bool transparent_;
const OnPaintCallback callback_; OnPaintCallback callback_;
bool active_; bool active_;

View file

@ -259,9 +259,9 @@ class AtomCopyFrameGenerator {
const gfx::Rect& damage_rect, const gfx::Rect& damage_rect,
const SkBitmap& bitmap, const SkBitmap& bitmap,
std::unique_ptr<SkAutoLockPixels> bitmap_pixels_lock) { std::unique_ptr<SkAutoLockPixels> bitmap_pixels_lock) {
uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels()); view_->OnPaint(damage_rect,
gfx::Size(bitmap.width(), bitmap.height()),
view_->OnPaint(damage_rect, bitmap.width(), bitmap.height(), pixels); bitmap.getPixels());
if (frame_retry_count_ > 0) if (frame_retry_count_ > 0)
frame_retry_count_ = 0; frame_retry_count_ = 0;
@ -845,15 +845,12 @@ bool OffScreenRenderWidgetHostView::IsAutoResizeEnabled() const {
void OffScreenRenderWidgetHostView::OnPaint( void OffScreenRenderWidgetHostView::OnPaint(
const gfx::Rect& damage_rect, const gfx::Rect& damage_rect,
int bitmap_width, const gfx::Size& bitmap_size,
int bitmap_height,
void* bitmap_pixels) { void* bitmap_pixels) {
TRACE_EVENT0("electron", "OffScreenRenderWidgetHostView::OnPaint"); TRACE_EVENT0("electron", "OffScreenRenderWidgetHostView::OnPaint");
if (!callback_.is_null()) { if (!callback_.is_null())
callback_.Run(damage_rect, gfx::Size(bitmap_width, bitmap_height), callback_.Run(damage_rect, bitmap_size, bitmap_pixels);
bitmap_pixels);
}
} }
void OffScreenRenderWidgetHostView::SetPainting(bool painting) { void OffScreenRenderWidgetHostView::SetPainting(bool painting) {

View file

@ -198,8 +198,7 @@ class OffScreenRenderWidgetHostView:
void SetPaintCallback(const OnPaintCallback& callback); void SetPaintCallback(const OnPaintCallback& callback);
void OnPaint(const gfx::Rect& damage_rect, void OnPaint(const gfx::Rect& damage_rect,
int bitmap_width, const gfx::Size& bitmap_size,
int bitmap_height,
void* bitmap_pixels); void* bitmap_pixels);
void SetPainting(bool painting); void SetPainting(bool painting);
@ -219,7 +218,7 @@ private:
OffScreenOutputDevice* software_output_device_; OffScreenOutputDevice* software_output_device_;
const OnPaintCallback callback_; OnPaintCallback callback_;
int frame_rate_; int frame_rate_;
int frame_rate_threshold_ms_; int frame_rate_threshold_ms_;

View file

@ -190,7 +190,7 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
// Use frame scheduling for offscreen renderers. // Use frame scheduling for offscreen renderers.
// TODO(zcbenz): Remove this after Chrome 54, on which it becomes default. // TODO(zcbenz): Remove this after Chrome 54, on which it becomes default.
bool offscreen; bool offscreen;
if (web_preferences.Get("offscreen", &offscreen) && offscreen) if (web_preferences.GetBoolean("offscreen", &offscreen) && offscreen)
command_line->AppendSwitch(cc::switches::kEnableBeginFrameScheduling); command_line->AppendSwitch(cc::switches::kEnableBeginFrameScheduling);
} }

View file

@ -1244,9 +1244,9 @@ describe('browser-window module', function () {
assert.equal(w.webContents.getFrameRate(), 60) assert.equal(w.webContents.getFrameRate(), 60)
done() done()
}) })
})
w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html') w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
}) })
})
describe('window.webContents.setFrameRate(frameRate)', function () { describe('window.webContents.setFrameRate(frameRate)', function () {
it('sets custom frame rate', function (done) { it('sets custom frame rate', function (done) {