Flutter Engine
platform_view.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_PLATFORM_VIEW_H_
6 #define FLUTTER_SHELL_PLATFORM_FUCHSIA_PLATFORM_VIEW_H_
7 
8 #include <fuchsia/sys/cpp/fidl.h>
9 #include <fuchsia/ui/input/cpp/fidl.h>
10 #include <fuchsia/ui/input3/cpp/fidl.h>
11 #include <fuchsia/ui/pointer/cpp/fidl.h>
12 #include <fuchsia/ui/scenic/cpp/fidl.h>
13 #include <lib/fidl/cpp/binding.h>
14 #include <lib/fit/function.h>
15 #include <lib/sys/cpp/service_directory.h>
16 #include <lib/ui/scenic/cpp/id.h>
17 
18 #include <array>
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <set>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include "flow/embedded_views.h"
28 #include "flutter/fml/macros.h"
29 #include "flutter/fml/memory/weak_ptr.h"
30 #include "flutter/fml/time/time_delta.h"
31 #include "flutter/shell/common/platform_view.h"
32 #include "flutter/shell/platform/fuchsia/flutter/keyboard.h"
33 #include "flutter/shell/platform/fuchsia/flutter/vsync_waiter.h"
34 #include "focus_delegate.h"
35 #include "pointer_delegate.h"
36 
37 namespace flutter_runner {
38 
39 using OnEnableWireframe = fit::function<void(bool)>;
40 using ViewCallback = std::function<void()>;
41 using OnUpdateView = fit::function<void(int64_t, SkRect, bool, bool)>;
42 using OnCreateSurface = fit::function<std::unique_ptr<flutter::Surface>()>;
44  fit::function<void(flutter::SemanticsNodeUpdates, float)>;
45 using OnRequestAnnounce = fit::function<void(std::string)>;
46 // we use an std::function here because the fit::funtion causes problems with
47 // std:bind since HandleFuchsiaShaderWarmupChannelPlatformMessage takes one of
48 // these as its first argument.
49 using OnShaderWarmup = std::function<void(const std::vector<std::string>&,
50  std::function<void(uint32_t)>,
51  uint64_t,
52  uint64_t)>;
53 
54 // PlatformView is the per-engine component residing on the platform thread that
55 // is responsible for all platform specific integrations -- particularly
56 // integration with the platform's accessibility, input, and windowing features.
57 //
58 // PlatformView communicates with the Dart code via "platform messages" handled
59 // in HandlePlatformMessage. This communication is bidirectional. Platform
60 // messages are notably responsible for communication related to input and
61 // external views / windowing.
63  private fuchsia::ui::input3::KeyboardListener,
64  private fuchsia::ui::input::InputMethodEditorClient {
65  public:
68  flutter::TaskRunners task_runners,
69  fuchsia::ui::views::ViewRef view_ref,
70  std::shared_ptr<flutter::ExternalViewEmbedder> external_view_embedder,
71  fidl::InterfaceHandle<fuchsia::ui::input::ImeService> ime_service,
72  fidl::InterfaceHandle<fuchsia::ui::input3::Keyboard> keyboard,
73  fidl::InterfaceHandle<fuchsia::ui::pointer::TouchSource> touch_source,
74  fidl::InterfaceHandle<fuchsia::ui::views::Focuser> focuser,
75  fidl::InterfaceHandle<fuchsia::ui::views::ViewRefFocused>
76  view_ref_focused,
77  OnEnableWireframe wireframe_enabled_callback,
78  OnUpdateView on_update_view_callback,
79  OnCreateSurface on_create_surface_callback,
80  OnSemanticsNodeUpdate on_semantics_node_update_callback,
81  OnRequestAnnounce on_request_announce_callback,
82  OnShaderWarmup on_shader_warmup,
83  AwaitVsyncCallback await_vsync_callback,
85  await_vsync_for_secondary_callback_callback);
86 
87  ~PlatformView() override;
88 
89  // |flutter::PlatformView|
90  void SetSemanticsEnabled(bool enabled) override;
91 
92  // |flutter::PlatformView|
93  std::shared_ptr<flutter::ExternalViewEmbedder> CreateExternalViewEmbedder()
94  override;
95 
96  protected:
98 
99  // |fuchsia.ui.input3.KeyboardListener|
100  // Called by the embedder every time there is a key event to process.
101  void OnKeyEvent(fuchsia::ui::input3::KeyEvent key_event,
102  fuchsia::ui::input3::KeyboardListener::OnKeyEventCallback
103  callback) override;
104 
105  // |fuchsia::ui::input::InputMethodEditorClient|
106  void DidUpdateState(
107  fuchsia::ui::input::TextInputState state,
108  std::unique_ptr<fuchsia::ui::input::InputEvent> event) override;
109 
110  // |fuchsia::ui::input::InputMethodEditorClient|
111  void OnAction(fuchsia::ui::input::InputMethodAction action) override;
112 
113  bool OnHandlePointerEvent(const fuchsia::ui::input::PointerEvent& pointer);
114 
115  bool OnHandleFocusEvent(const fuchsia::ui::input::FocusEvent& focus);
116 
117  // Gets a new input method editor from the input connection. Run when both
118  // Scenic has focus and Flutter has requested input with setClient.
119  void ActivateIme();
120 
121  // Detaches the input method editor connection, ending the edit session and
122  // closing the onscreen keyboard. Call when input is no longer desired, either
123  // because Scenic says we lost focus or when Flutter no longer has a text
124  // field focused.
125  void DeactivateIme();
126 
127  // |flutter::PlatformView|
128  std::unique_ptr<flutter::VsyncWaiter> CreateVSyncWaiter() override;
129 
130  // |flutter::PlatformView|
131  std::unique_ptr<flutter::Surface> CreateRenderingSurface() override;
132 
133  // |flutter::PlatformView|
135  std::unique_ptr<flutter::PlatformMessage> message) override;
136 
137  // |flutter::PlatformView|
138  void UpdateSemantics(
141 
142  // Channel handler for kAccessibilityChannel. This is currently not
143  // being used, but it is necessary to handle accessibility messages
144  // that are sent by Flutter when semantics is enabled.
146  std::unique_ptr<flutter::PlatformMessage> message);
147 
148  // Channel handler for kFlutterPlatformChannel
150  std::unique_ptr<flutter::PlatformMessage> message);
151 
152  // Channel handler for kTextInputChannel
154  std::unique_ptr<flutter::PlatformMessage> message);
155 
156  // Channel handler for kPlatformViewsChannel.
158  std::unique_ptr<flutter::PlatformMessage> message);
159 
160  // Channel handler for kFuchsiaShaderWarmupChannel.
162  OnShaderWarmup on_shader_warmup,
163  std::unique_ptr<flutter::PlatformMessage> message);
164 
165  virtual void OnCreateView(ViewCallback on_view_created,
166  int64_t view_id_raw,
167  bool hit_testable,
168  bool focusable) = 0;
169  virtual void OnDisposeView(int64_t view_id_raw) = 0;
170 
171  // Utility function for coordinate massaging.
172  std::array<float, 2> ClampToViewSpace(const float x, const float y) const;
173 
174  // Logical size and origin, and logical->physical ratio. These are optional
175  // to provide an "unset" state during program startup, before Scenic has sent
176  // any metrics-related events to provide initial values for these.
177  //
178  // The engine internally uses a default size of (0.f 0.f) with a default 1.f
179  // ratio, so there is no need to emit events until Scenic has actually sent a
180  // valid size and ratio.
181  std::optional<std::array<float, 2>> view_logical_size_;
182  std::optional<std::array<float, 2>> view_logical_origin_;
183  std::optional<float> view_pixel_ratio_;
184 
185  std::shared_ptr<flutter::ExternalViewEmbedder> external_view_embedder_;
186 
187  std::shared_ptr<FocusDelegate> focus_delegate_;
188  std::shared_ptr<PointerDelegate> pointer_delegate_;
189 
190  fidl::Binding<fuchsia::ui::input::InputMethodEditorClient> ime_client_;
191  fuchsia::ui::input::InputMethodEditorPtr ime_;
192  fuchsia::ui::input::ImeServicePtr text_sync_service_;
194 
195  fidl::Binding<fuchsia::ui::input3::KeyboardListener>
197  fuchsia::ui::input3::KeyboardPtr keyboard_;
199 
200  // last_text_state_ is the last state of the text input as reported by the IME
201  // or initialized by Flutter. We set it to null if Flutter doesn't want any
202  // input, since then there is no text input state at all.
203  std::unique_ptr<fuchsia::ui::input::TextInputState> last_text_state_;
204 
205  std::set<int> down_pointers_;
206  std::map<std::string /* channel */,
207  std::function<bool /* response_handled */ (
208  std::unique_ptr<
209  flutter::PlatformMessage> /* message */)> /* handler */>
211  // These are the channels that aren't registered and have been notified as
212  // such. Notifying via logs multiple times results in log-spam. See:
213  // https://github.com/flutter/flutter/issues/55966
214  std::set<std::string /* channel */> unregistered_channels_;
215 
219  OnSemanticsNodeUpdate on_semantics_node_update_callback_;
225 
227 
229 };
230 
231 } // namespace flutter_runner
232 
233 #endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_PLATFORM_VIEW_H_
std::optional< float > view_pixel_ratio_
bool HandleFlutterTextInputChannelPlatformMessage(std::unique_ptr< flutter::PlatformMessage > message)
std::function< void(const std::vector< std::string > &, std::function< void(uint32_t)>, uint64_t, uint64_t)> OnShaderWarmup
Definition: platform_view.h:52
fit::function< void(std::string)> OnRequestAnnounce
Definition: platform_view.h:45
std::shared_ptr< FocusDelegate > focus_delegate_
fuchsia::ui::input::InputMethodEditorPtr ime_
bool HandleAccessibilityChannelPlatformMessage(std::unique_ptr< flutter::PlatformMessage > message)
~PlatformView() override
Destroys the platform view. The platform view is owned by the shell and will be destroyed by the same...
void HandlePlatformMessage(std::unique_ptr< flutter::PlatformMessage > message) override
std::shared_ptr< flutter::ExternalViewEmbedder > external_view_embedder_
fit::function< std::unique_ptr< flutter::Surface >()> OnCreateSurface
Definition: platform_view.h:42
std::unique_ptr< flutter::VsyncWaiter > CreateVSyncWaiter() override
Invoked by the shell to obtain a platform specific vsync waiter. It is optional for platforms to over...
OnCreateSurface on_create_surface_callback_
OnSemanticsNodeUpdate on_semantics_node_update_callback_
std::function< void(FireCallbackCallback)> AwaitVsyncForSecondaryCallbackCallback
Definition: vsync_waiter.h:25
void OnKeyEvent(fuchsia::ui::input3::KeyEvent key_event, fuchsia::ui::input3::KeyboardListener::OnKeyEventCallback callback) override
Dart_NativeFunction function
Definition: fuchsia.cc:51
std::function< void(FireCallbackCallback)> AwaitVsyncCallback
Definition: vsync_waiter.h:22
std::unordered_map< int32_t, SemanticsNode > SemanticsNodeUpdates
AwaitVsyncCallback await_vsync_callback_
void OnAction(fuchsia::ui::input::InputMethodAction action) override
std::optional< std::array< float, 2 > > view_logical_origin_
virtual void OnCreateView(ViewCallback on_view_created, int64_t view_id_raw, bool hit_testable, bool focusable)=0
bool HandleFlutterPlatformChannelPlatformMessage(std::unique_ptr< flutter::PlatformMessage > message)
Platform views are created by the shell on the platform task runner. Unless explicitly specified...
Definition: platform_view.h:46
FlutterKeyEvent key_event
FlKeyEvent FlKeyResponderAsyncCallback callback
AwaitVsyncForSecondaryCallbackCallback await_vsync_for_secondary_callback_callback_
fit::function< void(int64_t, SkRect, bool, bool)> OnUpdateView
Definition: platform_view.h:41
FlKeyEvent * event
std::unique_ptr< fuchsia::ui::input::TextInputState > last_text_state_
fidl::Binding< fuchsia::ui::input3::KeyboardListener > keyboard_listener_binding_
std::map< std::string, std::function< bool(std::unique_ptr< flutter::PlatformMessage >)> > platform_message_handlers_
fit::function< void(flutter::SemanticsNodeUpdates, float)> OnSemanticsNodeUpdate
Definition: platform_view.h:44
std::set< std::string > unregistered_channels_
std::set< int > down_pointers_
void DidUpdateState(fuchsia::ui::input::TextInputState state, std::unique_ptr< fuchsia::ui::input::InputEvent > event) override
SemanticsAction action
fidl::Binding< fuchsia::ui::input::InputMethodEditorClient > ime_client_
OnEnableWireframe wireframe_enabled_callback_
OnRequestAnnounce on_request_announce_callback_
bool OnHandlePointerEvent(const fuchsia::ui::input::PointerEvent &pointer)
bool OnHandleFocusEvent(const fuchsia::ui::input::FocusEvent &focus)
std::function< void()> ViewCallback
FML_DISALLOW_COPY_AND_ASSIGN(PlatformView)
static bool HandleFuchsiaShaderWarmupChannelPlatformMessage(OnShaderWarmup on_shader_warmup, std::unique_ptr< flutter::PlatformMessage > message)
std::array< float, 2 > ClampToViewSpace(const float x, const float y) const
fuchsia::ui::input::ImeServicePtr text_sync_service_
std::shared_ptr< flutter::ExternalViewEmbedder > CreateExternalViewEmbedder() override
void SetSemanticsEnabled(bool enabled) override
Used by embedder to notify the running isolate hosted by the engine on the UI thread that the accessi...
OnUpdateView on_update_view_callback_
std::shared_ptr< PointerDelegate > pointer_delegate_
std::optional< std::array< float, 2 > > view_logical_size_
fuchsia::ui::input3::KeyboardPtr keyboard_
std::unordered_map< int32_t, CustomAccessibilityAction > CustomAccessibilityActionUpdates
PlatformView(flutter::PlatformView::Delegate &delegate, flutter::TaskRunners task_runners, fuchsia::ui::views::ViewRef view_ref, std::shared_ptr< flutter::ExternalViewEmbedder > external_view_embedder, fidl::InterfaceHandle< fuchsia::ui::input::ImeService > ime_service, fidl::InterfaceHandle< fuchsia::ui::input3::Keyboard > keyboard, fidl::InterfaceHandle< fuchsia::ui::pointer::TouchSource > touch_source, fidl::InterfaceHandle< fuchsia::ui::views::Focuser > focuser, fidl::InterfaceHandle< fuchsia::ui::views::ViewRefFocused > view_ref_focused, OnEnableWireframe wireframe_enabled_callback, OnUpdateView on_update_view_callback, OnCreateSurface on_create_surface_callback, OnSemanticsNodeUpdate on_semantics_node_update_callback, OnRequestAnnounce on_request_announce_callback, OnShaderWarmup on_shader_warmup, AwaitVsyncCallback await_vsync_callback, AwaitVsyncForSecondaryCallbackCallback await_vsync_for_secondary_callback_callback)
Used to forward events from the platform view to interested subsystems. This forwarding is done by th...
Definition: platform_view.h:53
bool HandleFlutterPlatformViewsChannelPlatformMessage(std::unique_ptr< flutter::PlatformMessage > message)
fit::function< void(bool)> OnEnableWireframe
Definition: platform_view.h:39
std::unique_ptr< flutter::Surface > CreateRenderingSurface() override
fml::WeakPtrFactory< PlatformView > weak_factory_
AtkStateType state
virtual void OnDisposeView(int64_t view_id_raw)=0
void UpdateSemantics(flutter::SemanticsNodeUpdates update, flutter::CustomAccessibilityActionUpdates actions) override
Used by the framework to tell the embedder to apply the specified semantics node updates. The default implementation of this method does nothing.
OnShaderWarmup on_shader_warmup_