Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve OSR API (1-8-x) #11729

Merged
merged 7 commits into from Feb 12, 2018
43 changes: 23 additions & 20 deletions atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -377,8 +377,8 @@ WebContents::WebContents(v8::Isolate* isolate, const mate::Dictionary& options)
options.Get("transparent", &transparent);

content::WebContents::CreateParams params(session->browser_context());
auto* view = new OffScreenWebContentsView(
transparent, base::Bind(&WebContents::OnPaint, base::Unretained(this)));
auto* view = new OffScreenWebContentsView(transparent,
base::Bind(&WebContents::OnPaint, base::Unretained(this)));
params.view = view;
params.delegate_view = view;

Expand Down Expand Up @@ -1644,10 +1644,10 @@ void WebContents::StartPainting() {
return;

#if defined(ENABLE_OSR)
auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
web_contents()->GetRenderWidgetHostView());
if (osr_rwhv)
osr_rwhv->SetPainting(true);
const auto* wc_impl = static_cast<content::WebContentsImpl*>(web_contents());
auto* osr_wcv = static_cast<OffScreenWebContentsView*>(wc_impl->GetView());
if (osr_wcv)
osr_wcv->SetPainting(true);
#endif
}

Expand All @@ -1656,10 +1656,10 @@ void WebContents::StopPainting() {
return;

#if defined(ENABLE_OSR)
auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
web_contents()->GetRenderWidgetHostView());
if (osr_rwhv)
osr_rwhv->SetPainting(false);
const auto* wc_impl = static_cast<content::WebContentsImpl*>(web_contents());
auto* osr_wcv = static_cast<OffScreenWebContentsView*>(wc_impl->GetView());
if (osr_wcv)
osr_wcv->SetPainting(false);
#endif
}

Expand All @@ -1668,9 +1668,10 @@ bool WebContents::IsPainting() const {
return false;

#if defined(ENABLE_OSR)
const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
web_contents()->GetRenderWidgetHostView());
return osr_rwhv && osr_rwhv->IsPainting();
const auto* wc_impl = static_cast<content::WebContentsImpl*>(web_contents());
auto* osr_wcv = static_cast<OffScreenWebContentsView*>(wc_impl->GetView());

return osr_wcv && osr_wcv->IsPainting();
#else
return false;
#endif
Expand All @@ -1681,10 +1682,11 @@ void WebContents::SetFrameRate(int frame_rate) {
return;

#if defined(ENABLE_OSR)
auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
web_contents()->GetRenderWidgetHostView());
if (osr_rwhv)
osr_rwhv->SetFrameRate(frame_rate);
const auto* wc_impl = static_cast<content::WebContentsImpl*>(web_contents());
auto* osr_wcv = static_cast<OffScreenWebContentsView*>(wc_impl->GetView());

if (osr_wcv)
osr_wcv->SetFrameRate(frame_rate);
#endif
}

Expand All @@ -1693,9 +1695,10 @@ int WebContents::GetFrameRate() const {
return 0;

#if defined(ENABLE_OSR)
const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
web_contents()->GetRenderWidgetHostView());
return osr_rwhv ? osr_rwhv->GetFrameRate() : 0;
const auto* wc_impl = static_cast<content::WebContentsImpl*>(web_contents());
auto* osr_wcv = static_cast<OffScreenWebContentsView*>(wc_impl->GetView());

return osr_wcv ? osr_wcv->GetFrameRate() : 0;
#else
return 0;
#endif
Expand Down
29 changes: 18 additions & 11 deletions atom/browser/osr/osr_render_widget_host_view.cc
Expand Up @@ -124,7 +124,7 @@ class AtomCopyFrameGenerator {
}

void GenerateCopyFrame(const gfx::Rect& damage_rect) {
if (!view_->render_widget_host())
if (!view_->render_widget_host() || !view_->IsPainting())
return;

std::unique_ptr<cc::CopyOutputRequest> request =
Expand Down Expand Up @@ -255,6 +255,8 @@ class AtomBeginFrameTimer : public cc::DelayBasedTimeSourceClient {

OffScreenRenderWidgetHostView::OffScreenRenderWidgetHostView(
bool transparent,
bool painting,
int frame_rate,
const OnPaintCallback& callback,
content::RenderWidgetHost* host,
OffScreenRenderWidgetHostView* parent_host_view,
Expand All @@ -268,17 +270,18 @@ OffScreenRenderWidgetHostView::OffScreenRenderWidgetHostView(
transparent_(transparent),
callback_(callback),
parent_callback_(nullptr),
frame_rate_(60),
frame_rate_(frame_rate),
frame_rate_threshold_us_(0),
last_time_(base::Time::Now()),
scale_factor_(kDefaultScaleFactor),
size_(native_window->GetSize()),
painting_(true),
painting_(painting),
is_showing_(!render_widget_host_->is_hidden()),
is_destroyed_(false),
popup_position_(gfx::Rect()),
hold_resize_(false),
pending_resize_(false),
paint_callback_running_(false),
renderer_compositor_frame_sink_(nullptr),
background_color_(SkColor()),
weak_ptr_factory_(this) {
Expand All @@ -303,7 +306,7 @@ OffScreenRenderWidgetHostView::OffScreenRenderWidgetHostView(
new ui::Compositor(context_factory_private->AllocateFrameSinkId(),
content::GetContextFactory(), context_factory_private,
base::ThreadTaskRunnerHandle::Get()));
compositor_->SetAcceleratedWidget(native_window_->GetAcceleratedWidget());
compositor_->SetAcceleratedWidget(gfx::kNullAcceleratedWidget);
compositor_->SetRootLayer(root_layer_.get());
#endif
GetCompositor()->SetDelegate(this);
Expand Down Expand Up @@ -738,6 +741,8 @@ content::RenderWidgetHostViewBase*

return new OffScreenRenderWidgetHostView(
transparent_,
true,
embedder_host_view->GetFrameRate(),
callback_,
render_widget_host,
embedder_host_view,
Expand Down Expand Up @@ -930,7 +935,7 @@ bool OffScreenRenderWidgetHostView::IsAutoResizeEnabled() const {

void OffScreenRenderWidgetHostView::SetNeedsBeginFrames(
bool needs_begin_frames) {
SetupFrameRate(false);
SetupFrameRate(true);

begin_frame_timer_->SetActive(needs_begin_frames);

Expand Down Expand Up @@ -1004,7 +1009,9 @@ void OffScreenRenderWidgetHostView::OnPaint(
}

damage.Intersect(GetViewBounds());
paint_callback_running_ = true;
callback_.Run(damage, bitmap);
paint_callback_running_ = false;

for (size_t i = 0; i < damages.size(); i++) {
CopyBitmapTo(bitmap, originals[i], damages[i]);
Expand Down Expand Up @@ -1151,7 +1158,7 @@ void OffScreenRenderWidgetHostView::SetPainting(bool painting) {
painting_ = painting;

if (software_output_device_) {
software_output_device_->SetActive(painting_, true);
software_output_device_->SetActive(painting_, !paint_callback_running_);
}
}

Expand All @@ -1168,16 +1175,16 @@ void OffScreenRenderWidgetHostView::SetFrameRate(int frame_rate) {
} else {
if (frame_rate <= 0)
frame_rate = 1;
if (frame_rate > 60)
frame_rate = 60;
if (frame_rate > 240)
frame_rate = 240;

frame_rate_ = frame_rate;
}

SetupFrameRate(true);

for (auto guest_host_view : guest_host_views_)
guest_host_view->SetFrameRate(frame_rate);

SetupFrameRate(true);
}

int OffScreenRenderWidgetHostView::GetFrameRate() const {
Expand Down Expand Up @@ -1205,7 +1212,7 @@ void OffScreenRenderWidgetHostView::SetupFrameRate(bool force) {

frame_rate_threshold_us_ = 1000000 / frame_rate_;

GetCompositor()->vsync_manager()->SetAuthoritativeVSyncInterval(
GetCompositor()->SetAuthoritativeVSyncInterval(
base::TimeDelta::FromMicroseconds(frame_rate_threshold_us_));

if (copy_frame_generator_.get()) {
Expand Down
4 changes: 4 additions & 0 deletions atom/browser/osr/osr_render_widget_host_view.h
Expand Up @@ -74,6 +74,8 @@ class OffScreenRenderWidgetHostView
public OffscreenViewProxyObserver {
public:
OffScreenRenderWidgetHostView(bool transparent,
bool painting,
int frame_rate,
const OnPaintCallback& callback,
content::RenderWidgetHost* render_widget_host,
OffScreenRenderWidgetHostView* parent_host_view,
Expand Down Expand Up @@ -314,6 +316,8 @@ class OffScreenRenderWidgetHostView
bool hold_resize_;
bool pending_resize_;

bool paint_callback_running_;

std::unique_ptr<ui::Layer> root_layer_;
std::unique_ptr<ui::Compositor> compositor_;
std::unique_ptr<content::DelegatedFrameHost> delegated_frame_host_;
Expand Down
42 changes: 42 additions & 0 deletions atom/browser/osr/osr_web_contents_view.cc
Expand Up @@ -15,6 +15,8 @@ namespace atom {
OffScreenWebContentsView::OffScreenWebContentsView(
bool transparent, const OnPaintCallback& callback)
: transparent_(transparent),
painting_(true),
frame_rate_(60),
callback_(callback),
web_contents_(nullptr) {
#if defined(OS_MACOSX)
Expand Down Expand Up @@ -103,6 +105,8 @@ content::RenderWidgetHostViewBase*
auto relay = NativeWindowRelay::FromWebContents(web_contents_);
return new OffScreenRenderWidgetHostView(
transparent_,
painting_,
GetFrameRate(),
callback_,
render_widget_host,
nullptr,
Expand All @@ -125,6 +129,8 @@ content::RenderWidgetHostViewBase*

return new OffScreenRenderWidgetHostView(
transparent_,
true,
view->GetFrameRate(),
callback_,
render_widget_host,
view,
Expand Down Expand Up @@ -202,6 +208,42 @@ void OffScreenWebContentsView::UpdateDragCursor(
blink::WebDragOperation operation) {
}

void OffScreenWebContentsView::SetPainting(bool painting) {
auto* view = GetView();
if (view != nullptr) {
view->SetPainting(painting);
} else {
painting_ = painting;
}
}

bool OffScreenWebContentsView::IsPainting() const {
auto* view = GetView();
if (view != nullptr) {
return view->IsPainting();
} else {
return painting_;
}
}

void OffScreenWebContentsView::SetFrameRate(int frame_rate) {
auto* view = GetView();
if (view != nullptr) {
view->SetFrameRate(frame_rate);
} else {
frame_rate_ = frame_rate;
}
}

int OffScreenWebContentsView::GetFrameRate() const {
auto* view = GetView();
if (view != nullptr) {
return view->GetFrameRate();
} else {
return frame_rate_;
}
}

OffScreenRenderWidgetHostView* OffScreenWebContentsView::GetView() const {
if (web_contents_) {
return static_cast<OffScreenRenderWidgetHostView*>(
Expand Down
7 changes: 7 additions & 0 deletions atom/browser/osr/osr_web_contents_view.h
Expand Up @@ -69,6 +69,11 @@ class OffScreenWebContentsView : public content::WebContentsView,
content::RenderWidgetHostImpl* source_rwh) override;
void UpdateDragCursor(blink::WebDragOperation operation) override;

void SetPainting(bool painting);
bool IsPainting() const;
void SetFrameRate(int frame_rate);
int GetFrameRate() const;

private:
#if defined(OS_MACOSX)
void PlatformCreate();
Expand All @@ -78,6 +83,8 @@ class OffScreenWebContentsView : public content::WebContentsView,
OffScreenRenderWidgetHostView* GetView() const;

const bool transparent_;
bool painting_;
int frame_rate_;
OnPaintCallback callback_;

// Weak refs.
Expand Down