Skip to content

Commit 54ee61e

Browse files
authored
Migrate vsync away from Mojo services (flutter-team-archive/engine#3169)
Instead, just use JNI and Objective-C directly.
1 parent 562000b commit 54ee61e

37 files changed

Lines changed: 549 additions & 235 deletions

DEPS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ deps = {
5454
# and not have to specific specific hashes.
5555

5656
'src/lib/ftl':
57-
Var('fuchsia_git') + '/ftl' + '@' + 'e8d8bf108418d5de96971ecd04b7cc0a58d8a568',
57+
Var('fuchsia_git') + '/ftl' + '@' + '9f51f24056554352b045fda338e9101c0e5af272',
5858

5959
'src/lib/tonic':
6060
Var('fuchsia_git') + '/tonic' + '@' + 'e1d221b924cb2a604363a8a9dd393d319becc0e8',

engine/src/flutter/shell/common/BUILD.gn

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ source_set("common") {
4040
"switches.h",
4141
"tracing_controller.cc",
4242
"tracing_controller.h",
43+
"vsync_waiter.cc",
44+
"vsync_waiter.h",
45+
"vsync_waiter_fallback.cc",
46+
"vsync_waiter_fallback.h",
4347
]
4448

4549
deps = [
@@ -54,7 +58,6 @@ source_set("common") {
5458
"//flutter/lib/ui",
5559
"//flutter/runtime",
5660
"//flutter/services/engine:interfaces",
57-
"//flutter/services/vsync",
5861
"//flutter/skia",
5962
"//flutter/sky/engine/wtf",
6063
"//flutter/synchronization",

engine/src/flutter/shell/common/animator.cc

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@
1212

1313
namespace shell {
1414

15-
Animator::Animator(ftl::WeakPtr<Rasterizer> rasterizer, Engine* engine)
15+
Animator::Animator(ftl::WeakPtr<Rasterizer> rasterizer,
16+
VsyncWaiter* waiter,
17+
Engine* engine)
1618
: rasterizer_(rasterizer),
19+
waiter_(waiter),
1720
engine_(engine),
1821
layer_tree_pipeline_(ftl::MakeRefCounted<LayerTreePipeline>(3)),
1922
pending_frame_semaphore_(1),
2023
paused_(false),
21-
weak_factory_(this) {
22-
new sky::services::vsync::VsyncProviderFallbackImpl(
23-
mojo::InterfaceRequest<::vsync::VSyncProvider>(
24-
mojo::GetProxy(&fallback_vsync_provider_)));
25-
}
24+
weak_factory_(this) {}
2625

2726
Animator::~Animator() = default;
2827

@@ -39,7 +38,7 @@ void Animator::Start() {
3938
RequestFrame();
4039
}
4140

42-
void Animator::BeginFrame(int64_t time_stamp) {
41+
void Animator::BeginFrame(ftl::TimePoint frame_time) {
4342
pending_frame_semaphore_.Signal();
4443

4544
if (!producer_continuation_) {
@@ -63,6 +62,8 @@ void Animator::BeginFrame(int64_t time_stamp) {
6362
// to service potential frame.
6463
DCHECK(producer_continuation_);
6564

65+
// TODO(abarth): We should use |frame_time| instead, but the frame time we get
66+
// on Android appears to be unstable.
6667
last_begin_frame_time_ = ftl::TimePoint::Now();
6768
engine_->BeginFrame(last_begin_frame_time_);
6869
}
@@ -92,7 +93,7 @@ void Animator::RequestFrame() {
9293

9394
if (!pending_frame_semaphore_.TryWait()) {
9495
// Multiple calls to Animator::RequestFrame will still result in a single
95-
// request to the VSyncProvider.
96+
// request to the VsyncWaiter.
9697
return;
9798
}
9899

@@ -107,30 +108,16 @@ void Animator::RequestFrame() {
107108
if (!self.get())
108109
return;
109110
TRACE_EVENT_INSTANT0("flutter", "RequestFrame", TRACE_EVENT_SCOPE_PROCESS);
110-
self->AwaitVSync(base::Bind(&Animator::BeginFrame, self));
111+
self->AwaitVSync();
111112
});
112113
}
113114

114-
void Animator::set_vsync_provider(vsync::VSyncProviderPtr vsync_provider) {
115-
vsync_provider_ = vsync_provider.Pass();
116-
117-
// We may be waiting on a VSync signal from the old VSync provider.
118-
pending_frame_semaphore_.Signal();
119-
120-
RequestFrame();
121-
}
122-
123-
void Animator::AwaitVSync(
124-
const vsync::VSyncProvider::AwaitVSyncCallback& callback) {
125-
// First, try the platform provided VSync provider.
126-
if (vsync_provider_) {
127-
vsync_provider_->AwaitVSync(callback);
128-
return;
129-
}
130-
131-
// Then, use the fallback provider if the platform cannot reliably supply
132-
// VSync signals to us.
133-
return fallback_vsync_provider_->AwaitVSync(callback);
115+
void Animator::AwaitVSync() {
116+
waiter_->AsyncWaitForVsync([self = weak_factory_.GetWeakPtr()](
117+
ftl::TimePoint frame_time) {
118+
if (self)
119+
self->BeginFrame(frame_time);
120+
});
134121
}
135122

136123
} // namespace shell

engine/src/flutter/shell/common/animator.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#ifndef SHELL_COMMON_ANIMATOR_H_
6-
#define SHELL_COMMON_ANIMATOR_H_
5+
#ifndef FLUTTER_SHELL_COMMON_ANIMATOR_H_
6+
#define FLUTTER_SHELL_COMMON_ANIMATOR_H_
77

8-
#include "base/memory/weak_ptr.h"
9-
#include "flutter/services/vsync/fallback/vsync_provider_fallback_impl.h"
108
#include "flutter/shell/common/engine.h"
119
#include "flutter/shell/common/rasterizer.h"
10+
#include "flutter/shell/common/vsync_waiter.h"
1211
#include "flutter/synchronization/pipeline.h"
1312
#include "flutter/synchronization/semaphore.h"
1413
#include "lib/ftl/memory/ref_ptr.h"
14+
#include "lib/ftl/memory/weak_ptr.h"
1515
#include "lib/ftl/time/time_point.h"
16-
#include "mojo/services/vsync/interfaces/vsync.mojom.h"
1716

1817
namespace shell {
1918

2019
class Animator {
2120
public:
22-
explicit Animator(ftl::WeakPtr<Rasterizer> rasterizer, Engine* engine);
21+
Animator(ftl::WeakPtr<Rasterizer> rasterizer,
22+
VsyncWaiter* waiter,
23+
Engine* engine);
2324

2425
~Animator();
2526

@@ -31,30 +32,28 @@ class Animator {
3132

3233
void Stop();
3334

34-
void set_vsync_provider(vsync::VSyncProviderPtr vsync_provider);
35-
3635
private:
3736
using LayerTreePipeline = flutter::Pipeline<flow::LayerTree>;
3837

39-
void BeginFrame(int64_t time_stamp);
38+
void BeginFrame(ftl::TimePoint frame_time);
4039

41-
void AwaitVSync(const vsync::VSyncProvider::AwaitVSyncCallback& callback);
40+
void AwaitVSync();
4241

4342
ftl::WeakPtr<Rasterizer> rasterizer_;
43+
VsyncWaiter* waiter_;
4444
Engine* engine_;
45-
vsync::VSyncProviderPtr vsync_provider_;
46-
vsync::VSyncProviderPtr fallback_vsync_provider_;
45+
4746
ftl::TimePoint last_begin_frame_time_;
4847
ftl::RefPtr<LayerTreePipeline> layer_tree_pipeline_;
4948
flutter::Semaphore pending_frame_semaphore_;
5049
LayerTreePipeline::ProducerContinuation producer_continuation_;
5150
bool paused_;
5251

53-
base::WeakPtrFactory<Animator> weak_factory_;
52+
ftl::WeakPtrFactory<Animator> weak_factory_;
5453

5554
FTL_DISALLOW_COPY_AND_ASSIGN(Animator);
5655
};
5756

5857
} // namespace shell
5958

60-
#endif // SHELL_COMMON_ANIMATOR_H_
59+
#endif // FLUTTER_SHELL_COMMON_ANIMATOR_H_

engine/src/flutter/shell/common/engine.cc

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ std::string FindPackagesPath(const std::string& main_dart) {
5252

5353
Engine::Engine(PlatformView* platform_view)
5454
: platform_view_(platform_view->GetWeakPtr()),
55-
animator_(new Animator(platform_view->rasterizer().GetWeakRasterizerPtr(),
56-
this)),
55+
animator_(std::make_unique<Animator>(
56+
platform_view->rasterizer().GetWeakRasterizerPtr(),
57+
platform_view->GetVsyncWaiter(),
58+
this)),
5759
binding_(this),
5860
activity_running_(false),
5961
have_surface_(false),
@@ -119,32 +121,6 @@ void Engine::OnOutputSurfaceDestroyed(const ftl::Closure& gpu_continuation) {
119121
blink::Threads::Gpu()->PostTask(gpu_continuation);
120122
}
121123

122-
void Engine::SetServices(sky::ServicesDataPtr services) {
123-
services_ = services.Pass();
124-
125-
if (services_->incoming_services) {
126-
incoming_services_ =
127-
mojo::ServiceProviderPtr::Create(services_->incoming_services.Pass());
128-
service_provider_impl_.set_fallback_service_provider(
129-
incoming_services_.get());
130-
}
131-
132-
vsync::VSyncProviderPtr vsync_provider;
133-
if (services_->shell) {
134-
// We bind and unbind our Shell here, since this is the only place we
135-
// use
136-
// it in this class.
137-
auto shell = mojo::ShellPtr::Create(services_->shell.Pass());
138-
mojo::ConnectToService(shell.get(), "mojo:vsync",
139-
mojo::GetProxy(&vsync_provider));
140-
services_->shell = shell.Pass();
141-
} else {
142-
mojo::ConnectToService(incoming_services_.get(),
143-
mojo::GetProxy(&vsync_provider));
144-
}
145-
animator_->set_vsync_provider(vsync_provider.Pass());
146-
}
147-
148124
void Engine::OnViewportMetricsChanged(sky::ViewportMetricsPtr metrics) {
149125
viewport_metrics_ = metrics.Pass();
150126
if (runtime_)
@@ -307,12 +283,6 @@ void Engine::DidCreateMainIsolate(Dart_Isolate isolate) {
307283

308284
void Engine::DidCreateSecondaryIsolate(Dart_Isolate isolate) {}
309285

310-
void Engine::BindToServiceProvider(
311-
mojo::InterfaceRequest<mojo::ServiceProvider> request) {
312-
service_provider_bindings_.AddBinding(&service_provider_impl_,
313-
request.Pass());
314-
}
315-
316286
void Engine::StopAnimator() {
317287
animator_->Stop();
318288
}

engine/src/flutter/shell/common/engine.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class Engine : public sky::SkyEngine, public blink::RuntimeDelegate {
6363

6464
private:
6565
// SkyEngine implementation:
66-
void SetServices(sky::ServicesDataPtr services) override;
6766
void OnViewportMetricsChanged(sky::ViewportMetricsPtr metrics) override;
6867
void OnLocaleChanged(const mojo::String& language_code,
6968
const mojo::String& country_code) override;
@@ -90,9 +89,6 @@ class Engine : public sky::SkyEngine, public blink::RuntimeDelegate {
9089
void DidCreateMainIsolate(Dart_Isolate isolate) override;
9190
void DidCreateSecondaryIsolate(Dart_Isolate isolate) override;
9291

93-
void BindToServiceProvider(
94-
mojo::InterfaceRequest<mojo::ServiceProvider> request);
95-
9692
void RunFromSnapshotStream(const std::string& script_uri,
9793
mojo::ScopedDataPipeConsumerHandle snapshot);
9894

@@ -107,11 +103,6 @@ class Engine : public sky::SkyEngine, public blink::RuntimeDelegate {
107103
ftl::WeakPtr<PlatformView> platform_view_;
108104
std::unique_ptr<Animator> animator_;
109105

110-
sky::ServicesDataPtr services_;
111-
mojo::ServiceProviderImpl service_provider_impl_;
112-
mojo::ServiceProviderPtr incoming_services_;
113-
mojo::BindingSet<mojo::ServiceProvider> service_provider_bindings_;
114-
115106
mojo::asset_bundle::AssetBundlePtr root_bundle_;
116107
std::unique_ptr<blink::RuntimeController> runtime_;
117108

engine/src/flutter/shell/common/platform_view.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "flutter/common/threads.h"
1010
#include "flutter/lib/ui/painting/resource_context.h"
1111
#include "flutter/shell/common/rasterizer.h"
12+
#include "flutter/shell/common/vsync_waiter_fallback.h"
1213
#include "lib/ftl/functional/make_copyable.h"
1314
#include "third_party/skia/include/gpu/gl/GrGLInterface.h"
1415

@@ -17,9 +18,7 @@ namespace shell {
1718
PlatformView::PlatformView(std::unique_ptr<Rasterizer> rasterizer)
1819
: rasterizer_(std::move(rasterizer)),
1920
size_(SkISize::Make(0, 0)),
20-
weak_factory_(this) {
21-
engine_.reset(new Engine(this));
22-
}
21+
weak_factory_(this) {}
2322

2423
PlatformView::~PlatformView() {
2524
blink::Threads::UI()->PostTask(
@@ -32,6 +31,10 @@ PlatformView::~PlatformView() {
3231
blink::Threads::UI()->PostTask([engine]() { delete engine; });
3332
}
3433

34+
void PlatformView::CreateEngine() {
35+
engine_.reset(new Engine(this));
36+
}
37+
3538
void PlatformView::DispatchPlatformMessage(
3639
ftl::RefPtr<blink::PlatformMessage> message) {
3740
blink::Threads::UI()->PostTask(
@@ -124,6 +127,12 @@ ftl::WeakPtr<PlatformView> PlatformView::GetWeakPtr() {
124127
return weak_factory_.GetWeakPtr();
125128
}
126129

130+
VsyncWaiter* PlatformView::GetVsyncWaiter() {
131+
if (!vsync_waiter_)
132+
vsync_waiter_ = std::make_unique<VsyncWaiterFallback>();
133+
return vsync_waiter_.get();
134+
}
135+
127136
void PlatformView::UpdateSemantics(std::vector<blink::SemanticsNode> update) {}
128137

129138
void PlatformView::HandlePlatformMessage(

engine/src/flutter/shell/common/platform_view.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "flutter/shell/common/engine.h"
1212
#include "flutter/shell/common/shell.h"
1313
#include "flutter/shell/common/surface.h"
14+
#include "flutter/shell/common/vsync_waiter.h"
1415
#include "lib/ftl/macros.h"
1516
#include "lib/ftl/memory/weak_ptr.h"
1617
#include "lib/ftl/synchronization/waitable_event.h"
@@ -51,6 +52,9 @@ class PlatformView {
5152

5253
ftl::WeakPtr<PlatformView> GetWeakPtr();
5354

55+
// The VsyncWaiter will live at least as long as the PlatformView.
56+
virtual VsyncWaiter* GetVsyncWaiter();
57+
5458
virtual bool ResourceContextMakeCurrent() = 0;
5559

5660
virtual void UpdateSemantics(std::vector<blink::SemanticsNode> update);
@@ -67,12 +71,15 @@ class PlatformView {
6771
protected:
6872
explicit PlatformView(std::unique_ptr<Rasterizer> rasterizer);
6973

74+
void CreateEngine();
75+
7076
void SetupResourceContextOnIOThreadPerform(
7177
ftl::AutoResetWaitableEvent* event);
7278

7379
SurfaceConfig surface_config_;
7480
std::unique_ptr<Rasterizer> rasterizer_;
7581
std::unique_ptr<Engine> engine_;
82+
std::unique_ptr<VsyncWaiter> vsync_waiter_;
7683
SkISize size_;
7784

7885
private:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2015 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/shell/common/vsync_waiter.h"
6+
7+
namespace shell {
8+
9+
VsyncWaiter::~VsyncWaiter() = default;
10+
11+
} // namespace shell
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_SHELL_COMMON_VSYNC_WAITER_H_
6+
#define FLUTTER_SHELL_COMMON_VSYNC_WAITER_H_
7+
8+
#include <functional>
9+
10+
#include "lib/ftl/time/time_point.h"
11+
12+
namespace shell {
13+
14+
class VsyncWaiter {
15+
public:
16+
using Callback = std::function<void(ftl::TimePoint frame_time)>;
17+
18+
virtual void AsyncWaitForVsync(Callback callback) = 0;
19+
20+
virtual ~VsyncWaiter();
21+
};
22+
23+
} // namespace shell
24+
25+
#endif // FLUTTER_SHELL_COMMON_VSYNC_WAITER_H_

0 commit comments

Comments
 (0)