Skip to content

Commit

Permalink
Fixup compile issues
Browse files Browse the repository at this point in the history
  • Loading branch information
John Kleinschmidt committed Mar 25, 2020
1 parent 4e31cb2 commit d6befd7
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 34 deletions.
4 changes: 2 additions & 2 deletions shell/browser/api/electron_api_top_level_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ TopLevelWindow::TopLevelWindow(v8::Isolate* isolate,

#if BUILDFLAG(ENABLE_OSR)
// Offscreen windows are always created frameless.
gin_helper::Dictionary web_preferences;
mate::Dictionary web_preferences;
bool offscreen;
if (options.Get(options::kWebPreferences, &web_preferences) &&
web_preferences.Get(options::kOffscreen, &offscreen) && offscreen) {
const_cast<gin_helper::Dictionary&>(options).Set(options::kFrame, false);
const_cast<mate::Dictionary&>(options).Set(options::kFrame, false);
}
#endif

Expand Down
3 changes: 2 additions & 1 deletion shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2378,7 +2378,8 @@ bool WebContents::IsOffScreen() const {

#if BUILDFLAG(ENABLE_OSR)
void WebContents::OnPaint(const gfx::Rect& dirty_rect, const SkBitmap& bitmap) {
Emit("paint", dirty_rect, gfx::Image::CreateFrom1xBitmap(bitmap));
Emit("paint", gin::ConvertToV8(isolate(), dirty_rect),
gfx::Image::CreateFrom1xBitmap(bitmap));
}

void WebContents::StartPainting() {
Expand Down
150 changes: 119 additions & 31 deletions shell/browser/osr/osr_render_widget_host_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ ui::MouseEvent UiMouseEventFromWebMouseEvent(blink::WebMouseEvent event) {
}

ui::MouseEvent ui_event(type,
gfx::Point(std::floor(event.PositionInWidget().x()),
std::floor(event.PositionInWidget().y())),
gfx::Point(std::floor(event.PositionInWidget().x()),
std::floor(event.PositionInWidget().y())),
gfx::Point(std::floor(event.PositionInWidget().x),
std::floor(event.PositionInWidget().y)),
gfx::Point(std::floor(event.PositionInWidget().x),
std::floor(event.PositionInWidget().y)),
ui::EventTimeForNow(), button_flags, button_flags);
ui_event.SetClickCount(event.click_count);

Expand All @@ -122,6 +122,37 @@ ui::MouseWheelEvent UiMouseWheelEventFromWebMouseEvent(

} // namespace

class ElectronBeginFrameTimer : public viz::DelayBasedTimeSourceClient {
public:
ElectronBeginFrameTimer(int frame_rate_threshold_us,
const base::Closure& callback)
: callback_(callback) {
time_source_ = std::make_unique<viz::DelayBasedTimeSource>(
base::CreateSingleThreadTaskRunner({content::BrowserThread::UI}).get());
time_source_->SetTimebaseAndInterval(
base::TimeTicks(),
base::TimeDelta::FromMicroseconds(frame_rate_threshold_us));
time_source_->SetClient(this);
}

void SetActive(bool active) { time_source_->SetActive(active); }

bool IsActive() const { return time_source_->Active(); }

void SetFrameRateThresholdUs(int frame_rate_threshold_us) {
time_source_->SetTimebaseAndInterval(
base::TimeTicks::Now(),
base::TimeDelta::FromMicroseconds(frame_rate_threshold_us));
}

private:
void OnTimerTick() override { callback_.Run(); }

const base::Closure callback_;
std::unique_ptr<viz::DelayBasedTimeSource> time_source_;

DISALLOW_COPY_AND_ASSIGN(ElectronBeginFrameTimer);
};
class ElectronDelegatedFrameHostClient
: public content::DelegatedFrameHostClient {
public:
Expand Down Expand Up @@ -158,6 +189,7 @@ class ElectronDelegatedFrameHostClient

bool ShouldShowStaleContentOnEviction() override { return false; }

void OnBeginFrame(base::TimeTicks frame_time) override {}
void InvalidateLocalSurfaceIdOnEviction() override {}

private:
Expand Down Expand Up @@ -255,6 +287,35 @@ OffScreenRenderWidgetHostView::CreateBrowserAccessibilityManager(
return nullptr;
}

void OffScreenRenderWidgetHostView::OnBeginFrameTimerTick() {
const base::TimeTicks frame_time = base::TimeTicks::Now();
const base::TimeDelta vsync_period =
base::TimeDelta::FromMicroseconds(frame_rate_threshold_us_);
SendBeginFrame(frame_time, 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 =
base::TimeDelta::FromMicroseconds(
(1.0f * base::Time::kMicrosecondsPerSecond) / (3.0f * 60));

base::TimeTicks deadline = display_time - estimated_browser_composite_time;

const viz::BeginFrameArgs& begin_frame_args = viz::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, begin_frame_source_.source_id(),
begin_frame_number_, frame_time, deadline, vsync_period,
viz::BeginFrameArgs::NORMAL);
DCHECK(begin_frame_args.IsValid());
begin_frame_number_++;

compositor_->context_factory_private()->IssueExternalBeginFrame(
compositor_.get(), begin_frame_args, /* force */ true,
base::NullCallback());
}
void OffScreenRenderWidgetHostView::InitAsChild(gfx::NativeView) {
DCHECK(parent_host_view_);

Expand Down Expand Up @@ -393,6 +454,22 @@ void OffScreenRenderWidgetHostView::TakeFallbackContentFrom(
host()->GetContentRenderingTimeoutFrom(view_osr->host());
}

void OffScreenRenderWidgetHostView::DidCreateNewRendererCompositorFrameSink(
viz::mojom::CompositorFrameSinkClient* renderer_compositor_frame_sink) {
renderer_compositor_frame_sink_ = renderer_compositor_frame_sink;

if (GetDelegatedFrameHost()) {
GetDelegatedFrameHost()->DidCreateNewRendererCompositorFrameSink(
renderer_compositor_frame_sink_);
}
}

void OffScreenRenderWidgetHostView::SubmitCompositorFrame(
const viz::LocalSurfaceId& local_surface_id,
viz::CompositorFrame frame,
base::Optional<viz::HitTestRegionList> hit_test_region_list) {
NOTREACHED();
}
void OffScreenRenderWidgetHostView::ResetFallbackToFirstNavigationSurface() {
GetDelegatedFrameHost()->ResetFallbackToFirstNavigationSurface();
}
Expand Down Expand Up @@ -637,6 +714,13 @@ bool OffScreenRenderWidgetHostView::InstallTransparency() {
return false;
}

void OffScreenRenderWidgetHostView::SetNeedsBeginFrames(
bool needs_begin_frames) {
SetupFrameRate(true);
begin_frame_timer_->SetActive(needs_begin_frames);
}

void OffScreenRenderWidgetHostView::SetWantsAnimateOnlyBeginFrames() {}
#if defined(OS_MACOSX)
void OffScreenRenderWidgetHostView::SetActive(bool active) {}

Expand Down Expand Up @@ -761,12 +845,12 @@ void OffScreenRenderWidgetHostView::SendMouseEvent(
const blink::WebMouseEvent& event) {
for (auto* proxy_view : proxy_views_) {
gfx::Rect bounds = proxy_view->GetBounds();
if (bounds.Contains(event.PositionInWidget().x(),
event.PositionInWidget().y())) {
if (bounds.Contains(event.PositionInWidget().x,
event.PositionInWidget().y)) {
blink::WebMouseEvent proxy_event(event);
proxy_event.SetPositionInWidget(
proxy_event.PositionInWidget().x() - bounds.x(),
proxy_event.PositionInWidget().y() - bounds.y());
proxy_event.PositionInWidget().x - bounds.x(),
proxy_event.PositionInWidget().y - bounds.y());

ui::MouseEvent ui_event = UiMouseEventFromWebMouseEvent(proxy_event);
proxy_view->OnEvent(&ui_event);
Expand All @@ -777,12 +861,12 @@ void OffScreenRenderWidgetHostView::SendMouseEvent(
if (!IsPopupWidget()) {
if (popup_host_view_ &&
popup_host_view_->popup_position_.Contains(
event.PositionInWidget().x(), event.PositionInWidget().y())) {
event.PositionInWidget().x, event.PositionInWidget().y)) {
blink::WebMouseEvent popup_event(event);
popup_event.SetPositionInWidget(
popup_event.PositionInWidget().x() -
popup_event.PositionInWidget().x -
popup_host_view_->popup_position_.x(),
popup_event.PositionInWidget().y() -
popup_event.PositionInWidget().y -
popup_host_view_->popup_position_.y());

popup_host_view_->ProcessMouseEvent(popup_event, ui::LatencyInfo());
Expand All @@ -799,12 +883,12 @@ void OffScreenRenderWidgetHostView::SendMouseWheelEvent(
const blink::WebMouseWheelEvent& event) {
for (auto* proxy_view : proxy_views_) {
gfx::Rect bounds = proxy_view->GetBounds();
if (bounds.Contains(event.PositionInWidget().x(),
event.PositionInWidget().y())) {
if (bounds.Contains(event.PositionInWidget().x,
event.PositionInWidget().y)) {
blink::WebMouseWheelEvent proxy_event(event);
proxy_event.SetPositionInWidget(
proxy_event.PositionInWidget().x() - bounds.x(),
proxy_event.PositionInWidget().y() - bounds.y());
proxy_event.PositionInWidget().x - bounds.x(),
proxy_event.PositionInWidget().y - bounds.y());

ui::MouseWheelEvent ui_event =
UiMouseWheelEventFromWebMouseEvent(proxy_event);
Expand All @@ -826,17 +910,17 @@ void OffScreenRenderWidgetHostView::SendMouseWheelEvent(
if (!IsPopupWidget()) {
if (popup_host_view_) {
if (popup_host_view_->popup_position_.Contains(
mouse_wheel_event.PositionInWidget().x(),
mouse_wheel_event.PositionInWidget().y())) {
mouse_wheel_event.PositionInWidget().x,
mouse_wheel_event.PositionInWidget().y)) {
blink::WebMouseWheelEvent popup_mouse_wheel_event(mouse_wheel_event);
popup_mouse_wheel_event.SetPositionInWidget(
mouse_wheel_event.PositionInWidget().x() -
mouse_wheel_event.PositionInWidget().x -
popup_host_view_->popup_position_.x(),
mouse_wheel_event.PositionInWidget().y() -
mouse_wheel_event.PositionInWidget().y -
popup_host_view_->popup_position_.y());
popup_mouse_wheel_event.SetPositionInScreen(
popup_mouse_wheel_event.PositionInWidget().x(),
popup_mouse_wheel_event.PositionInWidget().y());
popup_mouse_wheel_event.PositionInWidget().x,
popup_mouse_wheel_event.PositionInWidget().y);

popup_host_view_->SendMouseWheelEvent(popup_mouse_wheel_event);
return;
Expand All @@ -857,15 +941,15 @@ void OffScreenRenderWidgetHostView::SendMouseWheelEvent(
}
const gfx::Rect& guest_bounds =
guest_host_view->render_widget_host_->GetView()->GetViewBounds();
if (guest_bounds.Contains(mouse_wheel_event.PositionInWidget().x(),
mouse_wheel_event.PositionInWidget().y())) {
if (guest_bounds.Contains(mouse_wheel_event.PositionInWidget().x,
mouse_wheel_event.PositionInWidget().y)) {
blink::WebMouseWheelEvent guest_mouse_wheel_event(mouse_wheel_event);
guest_mouse_wheel_event.SetPositionInWidget(
mouse_wheel_event.PositionInWidget().x() - guest_bounds.x(),
mouse_wheel_event.PositionInWidget().y() - guest_bounds.y());
mouse_wheel_event.PositionInWidget().x - guest_bounds.x(),
mouse_wheel_event.PositionInWidget().y - guest_bounds.y());
guest_mouse_wheel_event.SetPositionInScreen(
guest_mouse_wheel_event.PositionInWidget().x(),
guest_mouse_wheel_event.PositionInWidget().y());
guest_mouse_wheel_event.PositionInWidget().x,
guest_mouse_wheel_event.PositionInWidget().y);

guest_host_view->SendMouseWheelEvent(guest_mouse_wheel_event);
return;
Expand Down Expand Up @@ -952,10 +1036,14 @@ void OffScreenRenderWidgetHostView::SetupFrameRate(bool force) {

frame_rate_threshold_us_ = 1000000 / frame_rate_;

if (compositor_) {
compositor_->SetDisplayVSyncParameters(
base::TimeTicks::Now(),
base::TimeDelta::FromMicroseconds(frame_rate_threshold_us_));
if (begin_frame_timer_.get()) {
begin_frame_timer_->SetFrameRateThresholdUs(frame_rate_threshold_us_);
} else {
begin_frame_timer_ = std::make_unique<ElectronBeginFrameTimer>(
frame_rate_threshold_us_,
base::BindRepeating(
&OffScreenRenderWidgetHostView::OnBeginFrameTimerTick,
weak_ptr_factory_.GetWeakPtr()));
}
}

Expand Down
19 changes: 19 additions & 0 deletions shell/browser/osr/osr_render_widget_host_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "base/process/kill.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"
#include "components/viz/common/frame_sinks/begin_frame_source.h"
#include "components/viz/common/quads/compositor_frame.h"
#include "components/viz/common/surfaces/parent_local_surface_id_allocator.h"
#include "content/browser/renderer_host/delegated_frame_host.h" // nogncheck
Expand Down Expand Up @@ -96,6 +98,8 @@ class OffScreenRenderWidgetHostView : public content::RenderWidgetHostViewBase,
bool LockMouse(bool request_unadjusted_movement) override;
void UnlockMouse(void) override;
void TakeFallbackContentFrom(content::RenderWidgetHostView* view) override;
void SetNeedsBeginFrames(bool needs_begin_frames) override;
void SetWantsAnimateOnlyBeginFrames() override;
#if defined(OS_MACOSX)
void SetActive(bool active) override;
void ShowDefinitionForSelection() override;
Expand All @@ -104,6 +108,13 @@ class OffScreenRenderWidgetHostView : public content::RenderWidgetHostViewBase,
#endif // defined(OS_MACOSX)

// content::RenderWidgetHostViewBase:
void DidCreateNewRendererCompositorFrameSink(
viz::mojom::CompositorFrameSinkClient* renderer_compositor_frame_sink)
override;
void SubmitCompositorFrame(
const viz::LocalSurfaceId& local_surface_id,
viz::CompositorFrame frame,
base::Optional<viz::HitTestRegionList> hit_test_region_list) override;

void ResetFallbackToFirstNavigationSurface() override;
void InitAsPopup(content::RenderWidgetHostView* rwhv,
Expand Down Expand Up @@ -153,6 +164,8 @@ class OffScreenRenderWidgetHostView : public content::RenderWidgetHostViewBase,

bool InstallTransparency();

void OnBeginFrameTimerTick();
void SendBeginFrame(base::TimeTicks frame_time, base::TimeDelta vsync_period);
void CancelWidget();
void AddGuestHostView(OffScreenRenderWidgetHostView* guest_host);
void RemoveGuestHostView(OffScreenRenderWidgetHostView* guest_host);
Expand Down Expand Up @@ -262,13 +275,19 @@ class OffScreenRenderWidgetHostView : public content::RenderWidgetHostViewBase,

std::unique_ptr<content::CursorManager> cursor_manager_;

std::unique_ptr<ElectronBeginFrameTimer> begin_frame_timer_;
OffScreenHostDisplayClient* host_display_client_;
std::unique_ptr<OffScreenVideoConsumer> video_consumer_;

// Provides |source_id| for BeginFrameArgs that we create.
viz::StubBeginFrameSource begin_frame_source_;
uint64_t begin_frame_number_ = viz::BeginFrameArgs::kStartingFrameNumber;
std::unique_ptr<ElectronDelegatedFrameHostClient>
delegated_frame_host_client_;

content::MouseWheelPhaseHandler mouse_wheel_phase_handler_;
viz::mojom::CompositorFrameSinkClient* renderer_compositor_frame_sink_ =
nullptr;

// Latest capture sequence number which is incremented when the caller
// requests surfaces be synchronized via
Expand Down

0 comments on commit d6befd7

Please sign in to comment.