Flutter Engine
The Flutter Engine
pointer_injector_delegate.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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_PLATFORM_FUCHSIA_FLUTTER_POINTER_INJECTOR_DELEGATE_H_
6#define FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_POINTER_INJECTOR_DELEGATE_H_
7
8#include <fuchsia/ui/pointerinjector/cpp/fidl.h>
9#include <fuchsia/ui/views/cpp/fidl.h>
10
11#include <queue>
12#include <unordered_map>
13#include <vector>
14
15#include "flutter/fml/macros.h"
16#include "flutter/fml/memory/weak_ptr.h"
17#include "flutter/lib/ui/window/platform_message.h"
18#include "third_party/rapidjson/include/rapidjson/document.h"
19
20namespace flutter_runner {
21
22// This class is responsible for handling the platform messages related to
23// pointer events and managing the lifecycle of
24// |fuchsia.ui.pointerinjector.Device| client side endpoint for embedded views.
26 public:
27 static constexpr auto kPointerInjectorMethodPrefix =
28 "View.pointerinjector.inject";
29
30 PointerInjectorDelegate(fuchsia::ui::pointerinjector::RegistryHandle registry,
31 fuchsia::ui::views::ViewRef host_view_ref)
32 : registry_(std::make_shared<fuchsia::ui::pointerinjector::RegistryPtr>(
33 registry.Bind())),
34 host_view_ref_(std::make_shared<fuchsia::ui::views::ViewRef>(
35 std::move(host_view_ref))) {}
36
37 // Handles the following pointer event related platform message requests:
38 // View.Pointerinjector.inject
39 // - Attempts to dispatch a pointer event to the given viewRef. Completes
40 // with [0] when the pointer event is sent to the given viewRef.
42 rapidjson::Value request,
44
45 // Adds an endpoint for |view_id| in |valid_views_| for lifecycle management.
46 // Called in |PlatformView::OnChildViewViewRef()|.
47 void OnCreateView(
48 uint64_t view_id,
49 std::optional<fuchsia::ui::views::ViewRef> view_ref = std::nullopt);
50
51 // Closes the |fuchsia.ui.pointerinjector.Device| channel for |view_id| and
52 // cleans up resources.
53 void OnDestroyView(uint64_t view_id) { valid_views_.erase(view_id); }
54
55 private:
56 using ViewId = int64_t;
57
58 struct PointerInjectorRequest {
59 // The position of the pointer event in viewport's coordinate system.
60 float x = 0.f, y = 0.f;
61
62 // |fuchsia.ui.pointerinjector.PointerSample.pointer_id|.
63 uint32_t pointer_id = 0;
64
65 // |fuchsia.ui.pointerinjector.PointerSample.phase|.
66 fuchsia::ui::pointerinjector::EventPhase phase =
68
69 // |fuchsia.ui.pointerinjector.Event.trace_flow_id|.
70 uint64_t trace_flow_id = 0;
71
72 // Logical size of the view's coordinate system.
73 std::array<float, 2> logical_size = {0.f, 0.f};
74
75 // |fuchsia.ui.pointerinjector.Event.timestamp|.
76 zx_time_t timestamp = 0;
77 };
78
79 // This class is responsible for dispatching pointer events to a view by first
80 // registering the injector device using
81 // |fuchsia.ui.pointerinjector.Registry.Register| and then injecting the
82 // pointer event using |fuchsia.ui.pointerinjector.Device.Inject|.
83 class PointerInjectorEndpoint {
84 public:
85 PointerInjectorEndpoint(
86 std::shared_ptr<fuchsia::ui::pointerinjector::RegistryPtr> registry,
87 std::shared_ptr<fuchsia::ui::views::ViewRef> host_view_ref,
88 std::optional<fuchsia::ui::views::ViewRef> view_ref)
89 : registry_(std::move(registry)),
90 host_view_ref_(std::move(host_view_ref)),
91 view_ref_(std::move(view_ref)),
92 weak_factory_(this) {
93 // Try to re-register the |device_| if the |device_| gets closed due to
94 // some error.
95 device_.set_error_handler(
96 [weak = weak_factory_.GetWeakPtr()](auto status) {
97 FML_LOG(WARNING)
98 << "fuchsia.ui.pointerinjector.Device closed " << status;
99 if (!weak) {
100 return;
101 }
102
103 // Clear all the stale pointer events in |injector_events_| and
104 // reset the state of |weak| so that any future calls do not inject
105 // any stale pointer events.
106 weak->Reset();
107 });
108 }
109
110 // Registers |device_| if it has not been registered and calls
111 // |DispatchPendingEvents()| to dispatch |request| to the view.
112 void InjectEvent(PointerInjectorRequest request);
113
114 private:
115 // Registers with the pointer injector service.
116 //
117 // Sets |registered_| to true immediately after submitting the registration
118 // request. This means that the registration request may still be in-flight
119 // on the server side when the function returns. Events can safely be
120 // injected into the channel while registration is pending ("feed forward").
121 void RegisterInjector(const PointerInjectorRequest& request);
122
123 // Recursively calls |fuchsia.ui.pointerinjector.Device.Inject| to dispatch
124 // the pointer events in |injector_events_| to the view.
125 void DispatchPendingEvents();
126
128
129 // Resets |registered_|, |injection_in_flight_| and |injector_events_| so
130 // that |device_| can be re-registered and future calls to
131 // |fuchsia.ui.pointerinjector.Device.Inject| do not include any stale
132 // pointer events.
133 void Reset();
134
135 // Set to true if there is a |fuchsia.ui.pointerinjector.Device.Inject| call
136 // in progress. If true, the |fuchsia.ui.pointerinjector.Event| is buffered
137 // in |injector_events_|.
138 bool injection_in_flight_ = false;
139
140 // Set to true if |device_| has been registered using
141 // |fuchsia.ui.pointerinjector.Registry.Register|. False otherwise.
142 bool registered_ = false;
143
144 std::shared_ptr<fuchsia::ui::pointerinjector::RegistryPtr> registry_;
145
146 // ViewRef for the main flutter app launching the embedded child views.
147 std::shared_ptr<fuchsia::ui::views::ViewRef> host_view_ref_;
148
149 // ViewRef for a flatland view.
150 // Set in |OnCreateView|.
151 std::optional<fuchsia::ui::views::ViewRef> view_ref_;
152
153 fuchsia::ui::pointerinjector::DevicePtr device_;
154
155 // A queue containing all the pending |fuchsia.ui.pointerinjector.Event|s
156 // which have to be dispatched to the view.
157 // Note: The size of a vector inside |injector_events_| should not exceed
158 // |fuchsia.ui.pointerinjector.MAX_INJECT|.
159 std::queue<std::vector<fuchsia::ui::pointerinjector::Event>>
160 injector_events_;
161
163 weak_factory_; // Must be the last member.
164
165 FML_DISALLOW_COPY_AND_ASSIGN(PointerInjectorEndpoint);
166 };
167
169 std::string value);
170
171 // Generates a |fuchsia.ui.pointerinjector.Event| from |request| by extracting
172 // information like timestamp, trace flow id and pointer sample from
173 // |request|.
174 static fuchsia::ui::pointerinjector::Event ExtractPointerEvent(
175 PointerInjectorRequest request);
176
177 // A map of valid views keyed by its view id. A view can receive pointer
178 // events only if it is present in |valid_views_|.
179 std::unordered_map<ViewId, PointerInjectorEndpoint> valid_views_;
180
181 std::shared_ptr<fuchsia::ui::pointerinjector::RegistryPtr> registry_;
182
183 // ViewRef for the main flutter app launching the embedded child views.
184 std::shared_ptr<fuchsia::ui::views::ViewRef> host_view_ref_;
185
186 FML_DISALLOW_COPY_AND_ASSIGN(PointerInjectorDelegate);
187};
188
189} // namespace flutter_runner
190#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_POINTER_INJECTOR_DELEGATE_H_
GrAATriangulator::Event Event
PointerInjectorDelegate(fuchsia::ui::pointerinjector::RegistryHandle registry, fuchsia::ui::views::ViewRef host_view_ref)
bool HandlePlatformMessage(rapidjson::Value request, fml::RefPtr< flutter::PlatformMessageResponse > response)
void OnCreateView(uint64_t view_id, std::optional< fuchsia::ui::views::ViewRef > view_ref=std::nullopt)
FlKeyEvent * event
uint8_t value
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
double y
double x
void Reset(SkPath *path)
Definition: path_ops.cc:40
static bool Bind(PassBindingsCacheMTL &pass, ShaderStage stage, size_t bind_index, const BufferView &view)
Definition: ref_ptr.h:256
fuchsia::ui::views::ViewRef view_ref_