| Flutter iOS Embedder |

platform_view_ios.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_DARWIN_IOS_PLATFORM_VIEW_IOS_H_
6 #define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_PLATFORM_VIEW_IOS_H_
7
8 #include <memory>
9
10 #include "flutter/fml/closure.h"
11 #include "flutter/fml/macros.h"
12 #include "flutter/shell/common/platform_view.h"
13 #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h"
14 #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
15 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h"
16 #import "flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h"
17 #import "flutter/shell/platform/darwin/ios/ios_context.h"
18 #import "flutter/shell/platform/darwin/ios/ios_external_view_embedder.h"
19 #import "flutter/shell/platform/darwin/ios/ios_surface.h"
20 #import "flutter/shell/platform/darwin/ios/platform_message_handler_ios.h"
21 #import "flutter/shell/platform/darwin/ios/rendering_api_selection.h"
22
23 @class FlutterViewController;
24
25 namespace flutter {
26
27 /**
28 * A bridge connecting the platform agnostic shell and the iOS embedding.
29 *
30 * The shell provides and requests for UI related data and this PlatformView subclass fulfills
31 * it with iOS specific capabilities. As an example, the iOS embedding (the `FlutterEngine` and the
32 * `FlutterViewController`) sends pointer data to the shell and receives the shell's request for a
33 * Skia GrDirectContext and supplies it.
34 *
35 * Despite the name "view", this class is unrelated to UIViews on iOS and doesn't have the same
36 * lifecycle. It's a long lived bridge owned by the `FlutterEngine` and can be attached and
37 * detached sequentially to multiple `FlutterViewController`s and `FlutterView`s.
38 */
39 class PlatformViewIOS final : public PlatformView {
40 public:
41 PlatformViewIOS(PlatformView::Delegate& delegate,
42 const std::shared_ptr<IOSContext>& context,
43 __weak FlutterPlatformViewsController* platform_views_controller,
44 const flutter::TaskRunners& task_runners);
45
46 explicit PlatformViewIOS(
47 PlatformView::Delegate& delegate,
48 IOSRenderingAPI rendering_api,
49 __weak FlutterPlatformViewsController* platform_views_controller,
50 const flutter::TaskRunners& task_runners,
51 const std::shared_ptr<fml::ConcurrentTaskRunner>& worker_task_runner,
52 const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch);
53
54 ~PlatformViewIOS() override;
55
56 /**
57 * Returns the `FlutterViewController` currently attached to the `FlutterEngine` owning
58 * this PlatformViewIOS.
59 */
60 FlutterViewController* GetOwnerViewController() const __attribute__((cf_audited_transfer));
61
62 /**
63 * Updates the `FlutterViewController` currently attached to the `FlutterEngine` owning
64 * this PlatformViewIOS. This should be updated when the `FlutterEngine`
65 * is given a new `FlutterViewController`.
66 */
67 void SetOwnerViewController(__weak FlutterViewController* owner_controller);
68
69 /**
70 * Called one time per `FlutterViewController` when the `FlutterViewController`'s
71 * UIView is first loaded.
72 *
73 * Can be used to perform late initialization after `FlutterViewController`'s
74 * init.
75 */
76 void attachView();
77
78 /**
79 * Called through when an external texture such as video or camera is
80 * given to the `FlutterEngine` or `FlutterViewController`.
81 */
82 void RegisterExternalTexture(int64_t id, NSObject<FlutterTexture>* texture);
83
84 // |PlatformView|
85 PointerDataDispatcherMaker GetDispatcherMaker() override;
86
87 // |PlatformView|
88 void SetSemanticsEnabled(bool enabled) override;
89
90 // |PlatformView|
91 void HandlePlatformMessage(std::unique_ptr<flutter::PlatformMessage> message) override;
92
93 // |PlatformView|
94 std::unique_ptr<Surface> CreateRenderingSurface() override;
95
96 // |PlatformView|
97 std::shared_ptr<ExternalViewEmbedder> CreateExternalViewEmbedder() override;
98
99 // |PlatformView|
100 sk_sp<GrDirectContext> CreateResourceContext() const override;
101
102 // |PlatformView|
103 std::shared_ptr<impeller::Context> GetImpellerContext() const override;
104
105 // |PlatformView|
106 void SetAccessibilityFeatures(int32_t flags) override;
107
108 // |PlatformView|
109 void UpdateSemantics(flutter::SemanticsNodeUpdates update,
110 flutter::CustomAccessibilityActionUpdates actions) override;
111
112 // |PlatformView|
113 std::unique_ptr<VsyncWaiter> CreateVSyncWaiter() override;
114
115 // |PlatformView|
116 void OnPreEngineRestart() const override;
117
118 // |PlatformView|
119 std::unique_ptr<std::vector<std::string>> ComputePlatformResolvedLocales(
120 const std::vector<std::string>& supported_locale_data) override;
121
122 /** Accessor for the `IOSContext` associated with the platform view. */
123 const std::shared_ptr<IOSContext>& GetIosContext() { return ios_context_; }
124
125 std::shared_ptr<PlatformMessageHandlerIos> GetPlatformMessageHandlerIos() const {
126 return platform_message_handler_;
127 }
128
129 std::shared_ptr<PlatformMessageHandler> GetPlatformMessageHandler() const override {
130 return platform_message_handler_;
131 }
132
133 private:
134 /// Smart pointer for use with objective-c observers.
135 /// This guarantees we remove the observer.
136 class ScopedObserver {
137 public:
138 ScopedObserver();
139 ~ScopedObserver();
140 void reset(id<NSObject> observer);
141 ScopedObserver(const ScopedObserver&) = delete;
142 ScopedObserver& operator=(const ScopedObserver&) = delete;
143
144 private:
145 id<NSObject> observer_ = nil;
146 };
147
148 /// Wrapper that guarantees we communicate clearing Accessibility
149 /// information to Dart.
150 class AccessibilityBridgeManager {
151 public:
152 explicit AccessibilityBridgeManager(const std::function<void(bool)>& set_semantics_enabled);
153 AccessibilityBridgeManager(const std::function<void(bool)>& set_semantics_enabled,
154 AccessibilityBridge* bridge);
155 explicit operator bool() const noexcept { return static_cast<bool>(accessibility_bridge_); }
156 AccessibilityBridge* get() const noexcept { return accessibility_bridge_.get(); }
157 void Set(std::unique_ptr<AccessibilityBridge> bridge);
158 void Clear();
159
160 private:
161 FML_DISALLOW_COPY_AND_ASSIGN(AccessibilityBridgeManager);
162 std::unique_ptr<AccessibilityBridge> accessibility_bridge_;
163 std::function<void(bool)> set_semantics_enabled_;
164 };
165
166 __weak FlutterViewController* owner_controller_;
167 // Since the `ios_surface_` is created on the platform thread but
168 // used on the raster thread we need to protect it with a mutex.
169 std::mutex ios_surface_mutex_;
170 std::unique_ptr<IOSSurface> ios_surface_;
171 std::shared_ptr<IOSContext> ios_context_;
172 __weak FlutterPlatformViewsController* platform_views_controller_;
173 AccessibilityBridgeManager accessibility_bridge_;
174 ScopedObserver dealloc_view_controller_observer_;
175 std::vector<std::string> platform_resolved_locale_;
176 std::shared_ptr<PlatformMessageHandlerIos> platform_message_handler_;
177
178 FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewIOS);
179 };
180
181 } // namespace flutter
182
183 #endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_PLATFORM_VIEW_IOS_H_
flutter::PlatformViewIOS::ComputePlatformResolvedLocales
std::unique_ptr< std::vector< std::string > > ComputePlatformResolvedLocales(const std::vector< std::string > &supported_locale_data) override
Definition: platform_view_ios.mm:215
flutter::PlatformViewIOS::CreateResourceContext
sk_sp< GrDirectContext > CreateResourceContext() const override
Definition: platform_view_ios.mm:157
flutter::PlatformViewIOS::CreateExternalViewEmbedder
std::shared_ptr< ExternalViewEmbedder > CreateExternalViewEmbedder() override
Definition: platform_view_ios.mm:152
flutter::PlatformViewIOS::OnPreEngineRestart
void OnPreEngineRestart() const override
Definition: platform_view_ios.mm:204
FlutterViewController
Definition: FlutterViewController.h:57
flutter::PlatformViewIOS::SetOwnerViewController
void SetOwnerViewController(__weak FlutterViewController *owner_controller)
Definition: platform_view_ios.mm:82
flutter::PlatformViewIOS::CreateRenderingSurface
std::unique_ptr< Surface > CreateRenderingSurface() override
Definition: platform_view_ios.mm:140
ios_external_view_embedder.h
FlutterTexture.h
flutter::PlatformViewIOS::HandlePlatformMessage
void HandlePlatformMessage(std::unique_ptr< flutter::PlatformMessage > message) override
Definition: platform_view_ios.mm:74
flutter::PlatformViewIOS::UpdateSemantics
void UpdateSemantics(flutter::SemanticsNodeUpdates update, flutter::CustomAccessibilityActionUpdates actions) override
Definition: platform_view_ios.mm:189
flutter::PlatformViewIOS
Definition: platform_view_ios.h:39
flutter::PlatformViewIOS::GetIosContext
const std::shared_ptr< IOSContext > & GetIosContext()
Definition: platform_view_ios.h:123
flutter::PlatformViewIOS::GetOwnerViewController
FlutterViewController * GetOwnerViewController() const __attribute__((cf_audited_transfer))
Definition: platform_view_ios.mm:78
ios_surface.h
flutter::PlatformViewIOS::~PlatformViewIOS
~PlatformViewIOS() override
flutter::PlatformViewIOS::GetPlatformMessageHandler
std::shared_ptr< PlatformMessageHandler > GetPlatformMessageHandler() const override
Definition: platform_view_ios.h:129
flutter
Definition: accessibility_bridge.h:27
accessibility_bridge.h
flutter::PlatformViewIOS::attachView
void attachView()
Definition: platform_view_ios.mm:113
flutter::IOSRenderingAPI
IOSRenderingAPI
Definition: rendering_api_selection.h:14
flutter::PlatformViewIOS::GetImpellerContext
std::shared_ptr< impeller::Context > GetImpellerContext() const override
Definition: platform_view_ios.mm:162
rendering_api_selection.h
flutter::PlatformViewIOS::SetAccessibilityFeatures
void SetAccessibilityFeatures(int32_t flags) override
Definition: platform_view_ios.mm:184
platform_message_handler_ios.h
FlutterPlatformViewsController
Definition: FlutterPlatformViewsController.h:31
flutter::PlatformViewIOS::GetPlatformMessageHandlerIos
std::shared_ptr< PlatformMessageHandlerIos > GetPlatformMessageHandlerIos() const
Definition: platform_view_ios.h:125
flutter::PlatformViewIOS::SetSemanticsEnabled
void SetSemanticsEnabled(bool enabled) override
Definition: platform_view_ios.mm:167
flutter::PlatformViewIOS::CreateVSyncWaiter
std::unique_ptr< VsyncWaiter > CreateVSyncWaiter() override
Definition: platform_view_ios.mm:200
ios_context.h
flutter::PlatformViewIOS::RegisterExternalTexture
void RegisterExternalTexture(int64_t id, NSObject< FlutterTexture > *texture)
Definition: platform_view_ios.mm:134
flutter::PlatformViewIOS::GetDispatcherMaker
PointerDataDispatcherMaker GetDispatcherMaker() override
Definition: platform_view_ios.mm:128
FlutterView.h
flutter::PlatformViewIOS::PlatformViewIOS
PlatformViewIOS(PlatformView::Delegate &delegate, const std::shared_ptr< IOSContext > &context, __weak FlutterPlatformViewsController *platform_views_controller, const flutter::TaskRunners &task_runners)
Definition: platform_view_ios.mm:44
FlutterViewController.h