Flutter Engine
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 SHELL_PLATFORM_IOS_PLATFORM_VIEW_IOS_H_
6 #define SHELL_PLATFORM_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/fml/memory/weak_ptr.h"
13 #include "flutter/fml/platform/darwin/scoped_nsobject.h"
14 #include "flutter/shell/common/platform_view.h"
15 #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h"
16 #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
17 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h"
18 #import "flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h"
19 #import "flutter/shell/platform/darwin/ios/framework/Source/platform_message_router.h"
20 #import "flutter/shell/platform/darwin/ios/ios_context.h"
21 #import "flutter/shell/platform/darwin/ios/ios_surface.h"
22 #import "flutter/shell/platform/darwin/ios/rendering_api_selection.h"
23 
25 
26 namespace flutter {
27 
28 /**
29  * A bridge connecting the platform agnostic shell and the iOS embedding.
30  *
31  * The shell provides and requests for UI related data and this PlatformView subclass fulfills
32  * it with iOS specific capabilities. As an example, the iOS embedding (the `FlutterEngine` and the
33  * `FlutterViewController`) sends pointer data to the shell and receives the shell's request for a
34  * Skia GrDirectContext and supplies it.
35  *
36  * Despite the name "view", this class is unrelated to UIViews on iOS and doesn't have the same
37  * lifecycle. It's a long lived bridge owned by the `FlutterEngine` and can be attached and
38  * detached sequentially to multiple `FlutterViewController`s and `FlutterView`s.
39  */
40 class PlatformViewIOS final : public PlatformView {
41  public:
42  explicit PlatformViewIOS(PlatformView::Delegate& delegate,
43  IOSRenderingAPI rendering_api,
44  flutter::TaskRunners task_runners);
45 
46  ~PlatformViewIOS() override;
47 
48  /**
49  * The `PlatformMessageRouter` is the iOS bridge connecting the shell's
50  * platform agnostic `PlatformMessage` to iOS's channel message handler.
51  */
53 
54  /**
55  * Returns the `FlutterViewController` currently attached to the `FlutterEngine` owning
56  * this PlatformViewIOS.
57  */
59 
60  /**
61  * Updates the `FlutterViewController` currently attached to the `FlutterEngine` owning
62  * this PlatformViewIOS. This should be updated when the `FlutterEngine`
63  * is given a new `FlutterViewController`.
64  */
66 
67  /**
68  * Called one time per `FlutterViewController` when the `FlutterViewController`'s
69  * UIView is first loaded.
70  *
71  * Can be used to perform late initialization after `FlutterViewController`'s
72  * init.
73  */
74  void attachView();
75 
76  /**
77  * Called through when an external texture such as video or camera is
78  * given to the `FlutterEngine` or `FlutterViewController`.
79  */
80  void RegisterExternalTexture(int64_t id, NSObject<FlutterTexture>* texture);
81 
82  // |PlatformView|
84 
85  // |PlatformView|
86  void SetSemanticsEnabled(bool enabled) override;
87 
88  private:
89  /// Smart pointer for use with objective-c observers.
90  /// This guarentees we remove the observer.
91  class ScopedObserver {
92  public:
93  ScopedObserver();
94  ~ScopedObserver();
95  void reset(id<NSObject> observer);
96  ScopedObserver(const ScopedObserver&) = delete;
97  ScopedObserver& operator=(const ScopedObserver&) = delete;
98 
99  private:
100  id<NSObject> observer_;
101  };
102 
103  /// Smart pointer that guarentees we communicate clearing Accessibility
104  /// information to Dart.
105  class AccessibilityBridgePtr {
106  public:
107  AccessibilityBridgePtr(const std::function<void(bool)>& set_semantics_enabled);
108  AccessibilityBridgePtr(const std::function<void(bool)>& set_semantics_enabled,
109  AccessibilityBridge* bridge);
110  ~AccessibilityBridgePtr();
111  explicit operator bool() const noexcept { return static_cast<bool>(accessibility_bridge_); }
112  AccessibilityBridge* operator->() const noexcept { return accessibility_bridge_.get(); }
113  void reset(AccessibilityBridge* bridge = nullptr);
114 
115  private:
116  FML_DISALLOW_COPY_AND_ASSIGN(AccessibilityBridgePtr);
117  std::unique_ptr<AccessibilityBridge> accessibility_bridge_;
118  std::function<void(bool)> set_semantics_enabled_;
119  };
120 
121  fml::WeakPtr<FlutterViewController> owner_controller_;
122  // Since the `ios_surface_` is created on the platform thread but
123  // used on the raster thread we need to protect it with a mutex.
124  std::mutex ios_surface_mutex_;
125  std::unique_ptr<IOSSurface> ios_surface_;
126  std::shared_ptr<IOSContext> ios_context_;
127  PlatformMessageRouter platform_message_router_;
128  AccessibilityBridgePtr accessibility_bridge_;
130  fml::closure firstFrameCallback_;
131  ScopedObserver dealloc_view_controller_observer_;
132  std::vector<std::string> platform_resolved_locale_;
133 
134  // |PlatformView|
135  void HandlePlatformMessage(fml::RefPtr<flutter::PlatformMessage> message) override;
136 
137  // |PlatformView|
138  std::unique_ptr<Surface> CreateRenderingSurface() override;
139 
140  // |PlatformView|
141  sk_sp<GrDirectContext> CreateResourceContext() const override;
142 
143  // |PlatformView|
144  void SetAccessibilityFeatures(int32_t flags) override;
145 
146  // |PlatformView|
147  void UpdateSemantics(flutter::SemanticsNodeUpdates update,
149 
150  // |PlatformView|
151  std::unique_ptr<VsyncWaiter> CreateVSyncWaiter() override;
152 
153  // |PlatformView|
154  void OnPreEngineRestart() const override;
155 
156  // |PlatformView|
157  std::unique_ptr<std::vector<std::string>> ComputePlatformResolvedLocales(
158  const std::vector<std::string>& supported_locale_data) override;
159 
161 };
162 
163 } // namespace flutter
164 
165 #endif // SHELL_PLATFORM_IOS_PLATFORM_VIEW_IOS_H_
void RegisterExternalTexture(int64_t id, NSObject< FlutterTexture > *texture)
Dart_NativeFunction function
Definition: fuchsia.cc:51
std::unordered_map< int32_t, SemanticsNode > SemanticsNodeUpdates
std::function< std::unique_ptr< PointerDataDispatcher >(PointerDataDispatcher::Delegate &)> PointerDataDispatcherMaker
Signature for constructing PointerDataDispatcher.
Platform views are created by the shell on the platform task runner. Unless explicitly specified...
Definition: platform_view.h:44
PointerDataDispatcherMaker GetDispatcherMaker() override
Returns a platform-specific PointerDataDispatcherMaker so the Engine can construct the PointerDataPac...
PlatformMessageRouter & GetPlatformMessageRouter()
std::function< void()> closure
Definition: closure.h:14
void SetSemanticsEnabled(bool enabled) override
Used by embedder to notify the running isolate hosted by the engine on the UI thread that the accessi...
std::unordered_map< int32_t, CustomAccessibilityAction > CustomAccessibilityActionUpdates
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
fml::WeakPtr< FlutterViewController > GetOwnerViewController() const
Used to forward events from the platform view to interested subsystems. This forwarding is done by th...
Definition: platform_view.h:51
void SetOwnerViewController(fml::WeakPtr< FlutterViewController > owner_controller)
PlatformViewIOS(PlatformView::Delegate &delegate, IOSRenderingAPI rendering_api, flutter::TaskRunners task_runners)
DEF_SWITCHES_START snapshot asset Path to the directory containing the four files specified by VmSnapshotInstructions and IsolateSnapshotInstructions vm snapshot The VM instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present isolate snapshot The isolate instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present icu symbol Prefix for the symbols representing ICU data linked into the Flutter library dart flags
Definition: switches.h:66