Skip to content

Instantly share code, notes, and snippets.

@chinmaygarde
Created May 12, 2018 05:06
Show Gist options
  • Select an option

  • Save chinmaygarde/01e14c95c2f34205378aa1372de4bb71 to your computer and use it in GitHub Desktop.

Select an option

Save chinmaygarde/01e14c95c2f34205378aa1372de4bb71 to your computer and use it in GitHub Desktop.
diff --git a/content_handler/compositor_context.cc b/content_handler/compositor_context.cc
index 904ef4fad..04b882db4 100644
--- a/content_handler/compositor_context.cc
+++ b/content_handler/compositor_context.cc
@@ -60,13 +60,15 @@ CompositorContext::CompositorContext(
std::string debug_label,
zx::eventpair import_token,
OnMetricsUpdate session_metrics_did_change_callback,
- fxl::Closure session_error_callback)
+ fxl::Closure session_error_callback,
+ fxl::Closure session_on_present)
: debug_label_(std::move(debug_label)),
session_connection_(std::move(scenic),
debug_label_,
std::move(import_token),
std::move(session_metrics_did_change_callback),
- std::move(session_error_callback)) {}
+ std::move(session_error_callback),
+ std::move(session_on_present)) {}
CompositorContext::~CompositorContext() = default;
diff --git a/content_handler/compositor_context.h b/content_handler/compositor_context.h
index 199c7a013..b1d356496 100644
--- a/content_handler/compositor_context.h
+++ b/content_handler/compositor_context.h
@@ -20,7 +20,8 @@ class CompositorContext final : public flow::CompositorContext {
std::string debug_label,
zx::eventpair import_token,
OnMetricsUpdate session_metrics_did_change_callback,
- fxl::Closure session_error_callback);
+ fxl::Closure session_error_callback,
+ fxl::Closure session_on_present);
~CompositorContext() override;
diff --git a/content_handler/engine.cc b/content_handler/engine.cc
index 8310c3418..401baa981 100644
--- a/content_handler/engine.cc
+++ b/content_handler/engine.cc
@@ -103,7 +103,8 @@ Engine::Engine(Delegate& delegate,
thread_label_, // debug label
std::move(import_token), // import token
on_session_metrics_change_callback, // session metrics did change
- on_session_error_callback // session did encounter error
+ on_session_error_callback, // session did encounter error
+ on_session_present // session on present
);
// Setup the callback that will instantiate the platform view.
@@ -301,6 +302,20 @@ void Engine::OnSessionMetricsDidChange(double device_pixel_ratio) {
});
}
+void Engine::OnSessionPresent() {
+ if (!shell_) {
+ return;
+ }
+
+ shell_->GetTaskRunners().GetPlatformTaskRunner()->PostTask(
+ [platform_view = shell_->GetPlatformView(), device_pixel_ratio]() {
+ if (platform_view) {
+ reinterpret_cast<flutter::PlatformView*>(platform_view.get())
+ ->UpdateViewportMetrics(device_pixel_ratio);
+ }
+ });
+}
+
// |mozart::NativesDelegate|
void Engine::OfferServiceProvider(
fidl::InterfaceHandle<component::ServiceProvider> service_provider,
diff --git a/content_handler/engine.h b/content_handler/engine.h
index 679e88355..0a9018dc7 100644
--- a/content_handler/engine.h
+++ b/content_handler/engine.h
@@ -59,6 +59,8 @@ class Engine final : public mozart::NativesDelegate {
void OnSessionMetricsDidChange(double device_pixel_ratio);
+ void OnSessionPresent();
+
// |mozart::NativesDelegate|
void OfferServiceProvider(
fidl::InterfaceHandle<component::ServiceProvider> service_provider,
diff --git a/content_handler/platform_view.cc b/content_handler/platform_view.cc
index e62f9e44a..48fc589e6 100644
--- a/content_handler/platform_view.cc
+++ b/content_handler/platform_view.cc
@@ -395,6 +395,13 @@ bool PlatformView::OnHandleFocusEvent(const input::FocusEvent& focus) {
return false;
}
+// |shell::PlatformView|
+std::unique_ptr<VsyncWaiter> PlatformView::CreateVSyncWaiter() {
+ auto waiter = std::make_unique<flutter::VsyncWaiter>(task_runners_);
+ vsync_target_ = waiter.GetWeakVsyncTarget();
+ return std::move(waiter);
+}
+
// |shell::PlatformView|
std::unique_ptr<shell::Surface> PlatformView::CreateRenderingSurface() {
// This platform does not repeatly lose and gain a surface connection. So the
diff --git a/content_handler/platform_view.h b/content_handler/platform_view.h
index 50a654487..7254da284 100644
--- a/content_handler/platform_view.h
+++ b/content_handler/platform_view.h
@@ -18,6 +18,7 @@
#include "lib/fidl/cpp/binding.h"
#include "lib/fxl/macros.h"
#include "surface.h"
+#include "vsync_waiter.h"
namespace flutter {
@@ -64,6 +65,7 @@ class PlatformView final : public shell::PlatformView,
component::ServiceProviderPtr parent_environment_service_provider_;
modular::ClipboardPtr clipboard_;
AccessibilityBridge accessibility_bridge_;
+ fxl::weakPtr<flutter::VsyncWaiter> vsync_target_;
std::unique_ptr<Surface> surface_;
blink::LogicalMetrics metrics_;
std::set<int> down_pointers_;
@@ -99,6 +101,9 @@ class PlatformView final : public shell::PlatformView,
bool OnHandleFocusEvent(const input::FocusEvent& focus);
+ // |shell::PlatformView|
+ std::unique_ptr<VsyncWaiter> CreateVSyncWaiter() override;
+
// |shell::PlatformView|
std::unique_ptr<shell::Surface> CreateRenderingSurface() override;
diff --git a/content_handler/session_connection.cc b/content_handler/session_connection.cc
index ef43a2fd5..8cf23b33a 100644
--- a/content_handler/session_connection.cc
+++ b/content_handler/session_connection.cc
@@ -14,22 +14,23 @@ SessionConnection::SessionConnection(
std::string debug_label,
zx::eventpair import_token,
OnMetricsUpdate session_metrics_did_change_callback,
- fxl::Closure session_error_callback)
+ fxl::Closure session_error_callback,
+ fxl::Closure on_session_present)
: debug_label_(std::move(debug_label)),
scenic_(scenic_handle.Bind()),
session_(scenic_.get()),
root_node_(&session_),
surface_producer_(std::make_unique<VulkanSurfaceProducer>(&session_)),
scene_update_context_(&session_, surface_producer_.get()),
- metrics_changed_callback_(
- std::move(session_metrics_did_change_callback)) {
+ metrics_changed_callback_(std::move(session_metrics_did_change_callback)),
+ on_session_present_(on_session_present) {
session_.set_error_handler(std::move(session_error_callback));
session_.set_event_handler(std::bind(&SessionConnection::OnSessionEvents,
this, std::placeholders::_1));
root_node_.Bind(std::move(import_token));
root_node_.SetEventMask(gfx::kMetricsEventMask);
- session_.Present(0, [](auto) {});
+ session_.Present(0, [on_session_present_](auto) { on_session_present_(); });
}
SessionConnection::~SessionConnection() = default;
@@ -66,8 +67,9 @@ void SessionConnection::Present(flow::CompositorContext::ScopedFrame& frame) {
// Flush all session ops. Paint tasks have not yet executed but those are
// fenced. The compositor can start processing ops while we finalize paint
// tasks.
- session_.Present(0, // presentation_time. (placeholder).
- [](auto) {} // callback
+ session_.Present(
+ 0, // presentation_time. (placeholder).
+ [on_session_present_](auto) { on_session_present_(); } // callback
);
// Execute paint tasks and signal fences.
diff --git a/content_handler/session_connection.h b/content_handler/session_connection.h
index 432188207..f3f23136b 100644
--- a/content_handler/session_connection.h
+++ b/content_handler/session_connection.h
@@ -27,7 +27,8 @@ class SessionConnection final {
std::string debug_label,
zx::eventpair import_token,
OnMetricsUpdate session_metrics_did_change_callback,
- fxl::Closure session_error_callback);
+ fxl::Closure session_error_callback,
+ fxl::Closure on_session_present);
~SessionConnection();
@@ -53,6 +54,7 @@ class SessionConnection final {
std::unique_ptr<VulkanSurfaceProducer> surface_producer_;
flow::SceneUpdateContext scene_update_context_;
OnMetricsUpdate metrics_changed_callback_;
+ fxl::Closure on_session_present_;
void OnSessionEvents(fidl::VectorPtr<ui::Event> events);
diff --git a/content_handler/vsync_waiter.cc b/content_handler/vsync_waiter.cc
new file mode 100644
index 000000000..e6f55e654
--- /dev/null
+++ b/content_handler/vsync_waiter.cc
@@ -0,0 +1,43 @@
+// Copyright 2018 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "third_party/flutter/content_handler/vsync_waiter.h"
+
+namespace flutter {
+
+static constexpr size_t kMaxSessionPresentsInFlight = 1;
+
+VsyncWaiter::VsyncWaiter(blink::TaskRunners task_runners)
+ : shell::VsyncWaiter(std::move(task_runners)),
+ sem_(kMaxSessionPresentsInFlight) {}
+
+VsyncWaiter::~VsyncWaiter() = default;
+
+void VsyncWaiter::SessionDidPresent() {
+ sem_.Signal();
+ TryFire();
+}
+
+void VsyncWaiter::AwaitVSync() {
+ TryFire();
+}
+
+void VsyncWaiter::TryFire() {
+ if (sem_.TryWait()) {
+ FireNow();
+ }
+}
+
+void VsyncWaiter::FireNow() {
+ // We don't know the display refresh rate on this platform. Since the target
+ // time is advisory, assume 60Hz.
+ constexpr fxl::TimeDelta interval = fxl::TimeDelta::FromSecondsF(1.0 / 60.0);
+
+ auto now = fxl::TimePoint::Now();
+ auto next = now + interval;
+
+ FireCallback(now, next);
+}
+
+} // namespace flutter
diff --git a/content_handler/vsync_waiter.h b/content_handler/vsync_waiter.h
new file mode 100644
index 000000000..0359a618c
--- /dev/null
+++ b/content_handler/vsync_waiter.h
@@ -0,0 +1,37 @@
+// Copyright 2018 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#pragma once
+
+#include "flutter/shell/common/vsync_waiter.h"
+#include "lib/fxl/macros.h"
+#include "lib/fxl/memory/weak_ptr.h"
+#include "third_party/flutter/synchronization/semaphore.h"
+
+namespace flutter {
+
+class VsyncWaiter final : private shell::VsyncWaiter {
+ public:
+ VsyncWaiter(blink::TaskRunners task_runners);
+
+ ~VsyncWaiter() override;
+
+ void SessionDidPresent();
+
+ private:
+ friend class VsyncWaiter;
+
+ Semaphore sem_;
+
+ // |shell::VsyncWaiter|
+ void AwaitVSync() override;
+
+ void TryFire();
+
+ void FireNow();
+
+ FXL_DISALLOW_COPY_AND_ASSIGN(VsyncWaiter);
+}
+
+} // namespace flutter
diff --git a/shell/common/vsync_waiter_fallback.cc b/shell/common/vsync_waiter_fallback.cc
index bcf061bb7..31744d59c 100644
--- a/shell/common/vsync_waiter_fallback.cc
+++ b/shell/common/vsync_waiter_fallback.cc
@@ -29,6 +29,7 @@ VsyncWaiterFallback::~VsyncWaiterFallback() = default;
constexpr fxl::TimeDelta interval = fxl::TimeDelta::FromSecondsF(1.0 / 60.0);
+// |shell::VsyncWaiter|
void VsyncWaiterFallback::AwaitVSync() {
fxl::TimePoint now = fxl::TimePoint::Now();
fxl::TimePoint next = SnapToNextTick(now, phase_, interval);
diff --git a/shell/common/vsync_waiter_fallback.h b/shell/common/vsync_waiter_fallback.h
index d3cc8faaf..6fb47658d 100644
--- a/shell/common/vsync_waiter_fallback.h
+++ b/shell/common/vsync_waiter_fallback.h
@@ -22,6 +22,7 @@ class VsyncWaiterFallback final : public VsyncWaiter {
fxl::TimePoint phase_;
fxl::WeakPtrFactory<VsyncWaiterFallback> weak_factory_;
+ // |shell::VsyncWaiter|
void AwaitVSync() override;
FXL_DISALLOW_COPY_AND_ASSIGN(VsyncWaiterFallback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment