Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1cbbd4c

Browse files
committed
Use delegate
1 parent ea0f8ad commit 1cbbd4c

7 files changed

Lines changed: 52 additions & 45 deletions

File tree

shell/common/engine.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ Engine::Engine(Delegate& delegate,
4747
: delegate_(delegate),
4848
settings_(std::move(settings)),
4949
animator_(std::move(animator)),
50-
dispatcher_maker_(std::move(dispatcher_maker)),
5150
activity_running_(true),
5251
have_surface_(false),
5352
image_decoder_(task_runners,
@@ -73,8 +72,7 @@ Engine::Engine(Delegate& delegate,
7372
settings_.isolate_shutdown_callback // isolate shutdown callback
7473
);
7574

76-
pointer_data_dispatcher_ =
77-
dispatcher_maker_(*animator_, *runtime_controller_, task_runners_);
75+
pointer_data_dispatcher_ = dispatcher_maker(*this);
7876
}
7977

8078
Engine::~Engine() = default;
@@ -117,8 +115,6 @@ bool Engine::Restart(RunConfiguration configuration) {
117115
}
118116
delegate_.OnPreEngineRestart();
119117
runtime_controller_ = runtime_controller_->Clone();
120-
pointer_data_dispatcher_ =
121-
dispatcher_maker_(*animator_, *runtime_controller_, task_runners_);
122118
UpdateAssetManager(nullptr);
123119
return Run(std::move(configuration)) == Engine::RunStatus::Success;
124120
}
@@ -472,6 +468,12 @@ FontCollection& Engine::GetFontCollection() {
472468
return font_collection_;
473469
}
474470

471+
void Engine::DoDispatchPacket(std::unique_ptr<PointerDataPacket> packet,
472+
uint64_t trace_flow_id) {
473+
animator_->EnqueueTraceFlowId(trace_flow_id);
474+
runtime_controller_->DispatchPointerDataPacket(*packet);
475+
}
476+
475477
void Engine::HandleAssetPlatformMessage(fml::RefPtr<PlatformMessage> message) {
476478
fml::RefPtr<PlatformMessageResponse> response = message->response();
477479
if (!response) {

shell/common/engine.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace flutter {
6767
/// name and it does happen to be one of the older classes in the
6868
/// repository.
6969
///
70-
class Engine final : public RuntimeDelegate {
70+
class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {
7171
public:
7272
//----------------------------------------------------------------------------
7373
/// @brief Indicates the result of the call to `Engine::Run`.
@@ -709,13 +709,23 @@ class Engine final : public RuntimeDelegate {
709709
// |RuntimeDelegate|
710710
FontCollection& GetFontCollection() override;
711711

712+
// |PointerDataDispatcher::Delegate|
713+
void DoDispatchPacket(std::unique_ptr<PointerDataPacket> packet,
714+
uint64_t trace_flow_id) override;
715+
716+
TaskRunners& task_runners() override { return task_runners_; }
717+
712718
private:
713719
Engine::Delegate& delegate_;
714720
const Settings settings_;
715721
std::unique_ptr<Animator> animator_;
716722
std::unique_ptr<RuntimeController> runtime_controller_;
723+
724+
// The pointer_data_dispatcher_ depends on animator_ and runtime_controller_.
725+
// So it should be defined after them to ensure that pointer_data_dispatcher_
726+
// is destructed first.
717727
std::unique_ptr<PointerDataDispatcher> pointer_data_dispatcher_;
718-
PointerDataDispatcherMaker dispatcher_maker_; // stored for restart
728+
719729
std::string initial_route_;
720730
ViewportMetrics viewport_metrics_;
721731
std::shared_ptr<AssetManager> asset_manager_;

shell/common/platform_view.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ sk_sp<GrContext> PlatformView::CreateResourceContext() const {
8989
void PlatformView::ReleaseResourceContext() const {}
9090

9191
PointerDataDispatcherMaker PlatformView::GetDispatcherMaker() {
92-
return [](Animator& animator, RuntimeController& controller,
93-
TaskRunners task_runners) {
94-
return std::make_unique<DefaultPointerDataDispatcher>(animator, controller);
92+
return [](DefaultPointerDataDispatcher::Delegate& delegate) {
93+
return std::make_unique<DefaultPointerDataDispatcher>(delegate);
9594
};
9695
}
9796

shell/common/pointer_data_dispatcher.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ SmoothPointerDataDispatcher::~SmoothPointerDataDispatcher() = default;
1313
void DefaultPointerDataDispatcher::DispatchPacket(
1414
std::unique_ptr<PointerDataPacket> packet,
1515
uint64_t trace_flow_id) {
16-
animator_.EnqueueTraceFlowId(trace_flow_id);
17-
runtime_controller_.DispatchPointerDataPacket(*packet);
16+
delegate_.DoDispatchPacket(std::move(packet), trace_flow_id);
1817
}
1918

2019
// Intentional no-op.
@@ -49,7 +48,7 @@ void SmoothPointerDataDispatcher::OnFrameLayerTreeReceived() {
4948
// we'll post a new UI thread task to fire the packet after `VSYNC` task
5049
// is done. When a non-VSYNC UI thread task (like the following one) is
5150
// run, the Flutter framework is always in `SchedulerPhase.idle` phase).
52-
task_runners_.GetUITaskRunner()->PostTask(
51+
delegate_.task_runners().GetUITaskRunner()->PostTask(
5352
// Use and validate a `fml::WeakPtr` because this dispatcher might
5453
// have been destructed with engine when the task is run.
5554
[dispatcher = weak_factory_.GetWeakPtr()]() {

shell/common/pointer_data_dispatcher.h

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@ namespace flutter {
1212

1313
class PointerDataDispatcher;
1414

15-
//--------------------------------------------------------------------------
16-
/// @brief Signature for constructing PointerDataDispatcher.
17-
///
18-
/// @param[in] animator the animator of `Flutter::Engine`
19-
/// @param[in] controller the runtime controller of `Flutter::Engine`
20-
/// @param[in] task_runners the task runners of `Flutter::Engine`
21-
///
22-
using PointerDataDispatcherMaker =
23-
std::function<std::unique_ptr<PointerDataDispatcher>(
24-
Animator& animator,
25-
RuntimeController& runtime_controller,
26-
TaskRunners task_runners)>;
27-
2815
//------------------------------------------------------------------------------
2916
/// The `Engine` pointer data dispatcher that forwards the packet received from
3017
/// `PlatformView::DispatchPointerDataPacket` on the platform thread, to
@@ -52,6 +39,18 @@ using PointerDataDispatcherMaker =
5239
/// construction of the `PointerDataDispatcher`.
5340
class PointerDataDispatcher {
5441
public:
42+
/// The interface for Engine to implement.
43+
class Delegate {
44+
public:
45+
/// Actually dispatch the packet using Engine's `animator_` and
46+
/// `runtime_controller_`.
47+
virtual void DoDispatchPacket(std::unique_ptr<PointerDataPacket> packet,
48+
uint64_t trace_flow_id) = 0;
49+
50+
/// Get the task runners to schedule tasks on specific threads.
51+
virtual TaskRunners& task_runners() = 0;
52+
};
53+
5554
//----------------------------------------------------------------------------
5655
/// @brief Signal that `PlatformView` has a packet to be dispatched.
5756
///
@@ -77,9 +76,7 @@ class PointerDataDispatcher {
7776
///
7877
class DefaultPointerDataDispatcher : public PointerDataDispatcher {
7978
public:
80-
DefaultPointerDataDispatcher(Animator& animator,
81-
RuntimeController& runtime_controller)
82-
: runtime_controller_(runtime_controller), animator_(animator) {}
79+
DefaultPointerDataDispatcher(Delegate& delegate) : delegate_(delegate) {}
8380

8481
// |PointerDataDispatcer|
8582
void DispatchPacket(std::unique_ptr<PointerDataPacket> packet,
@@ -91,8 +88,7 @@ class DefaultPointerDataDispatcher : public PointerDataDispatcher {
9188
virtual ~DefaultPointerDataDispatcher();
9289

9390
protected:
94-
RuntimeController& runtime_controller_;
95-
Animator& animator_;
91+
Delegate& delegate_;
9692

9793
FML_DISALLOW_COPY_AND_ASSIGN(DefaultPointerDataDispatcher);
9894
};
@@ -136,12 +132,8 @@ class DefaultPointerDataDispatcher : public PointerDataDispatcher {
136132
/// See also input_events_unittests.cc where we test all our claims above.
137133
class SmoothPointerDataDispatcher : public DefaultPointerDataDispatcher {
138134
public:
139-
SmoothPointerDataDispatcher(Animator& animator,
140-
RuntimeController& runtime_controller,
141-
TaskRunners task_runners)
142-
: DefaultPointerDataDispatcher(animator, runtime_controller),
143-
task_runners_(task_runners),
144-
weak_factory_(this) {}
135+
SmoothPointerDataDispatcher(Delegate& delegate)
136+
: DefaultPointerDataDispatcher(delegate), weak_factory_(this) {}
145137

146138
// |PointerDataDispatcer|
147139
void DispatchPacket(std::unique_ptr<PointerDataPacket> packet,
@@ -153,8 +145,6 @@ class SmoothPointerDataDispatcher : public DefaultPointerDataDispatcher {
153145
virtual ~SmoothPointerDataDispatcher();
154146

155147
private:
156-
TaskRunners task_runners_;
157-
158148
// If non-null, this will be a pending pointer data packet for the next frame
159149
// to consume. This is used to smooth out the irregular drag events delivery.
160150
// See also `DispatchPointerDataPacket` and input_events_unittests.cc.
@@ -170,6 +160,15 @@ class SmoothPointerDataDispatcher : public DefaultPointerDataDispatcher {
170160
FML_DISALLOW_COPY_AND_ASSIGN(SmoothPointerDataDispatcher);
171161
};
172162

163+
//--------------------------------------------------------------------------
164+
/// @brief Signature for constructing PointerDataDispatcher.
165+
///
166+
/// @param[in] delegate the `Flutter::Engine`
167+
///
168+
using PointerDataDispatcherMaker =
169+
std::function<std::unique_ptr<PointerDataDispatcher>(
170+
PointerDataDispatcher::Delegate&)>;
171+
173172
} // namespace flutter
174173

175174
#endif // POINTER_DATA_DISPATCHER_H_

shell/common/shell_test.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,8 @@ std::unique_ptr<Surface> ShellTestPlatformView::CreateRenderingSurface() {
245245

246246
// |PlatformView|
247247
PointerDataDispatcherMaker ShellTestPlatformView::GetDispatcherMaker() {
248-
return [](Animator& animator, RuntimeController& controller,
249-
TaskRunners task_runners) {
250-
return std::make_unique<SmoothPointerDataDispatcher>(animator, controller,
251-
task_runners);
248+
return [](DefaultPointerDataDispatcher::Delegate& delegate) {
249+
return std::make_unique<SmoothPointerDataDispatcher>(delegate);
252250
};
253251
}
254252

shell/platform/darwin/ios/platform_view_ios.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ new AccessibilityBridge(static_cast<FlutterView*>(owner_controller_.get().view),
6767
}
6868

6969
PointerDataDispatcherMaker PlatformViewIOS::GetDispatcherMaker() {
70-
return [](Animator& animator, RuntimeController& controller, TaskRunners task_runners) {
71-
return std::make_unique<SmoothPointerDataDispatcher>(animator, controller, task_runners);
70+
return [](DefaultPointerDataDispatcher::Delegate& delegate) {
71+
return std::make_unique<SmoothPointerDataDispatcher>(delegate);
7272
};
7373
}
7474

0 commit comments

Comments
 (0)