Flutter iOS Embedder
platform_view_ios.mm
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 
6 #include <memory>
7 
8 #include <utility>
9 
10 #include "flutter/common/task_runners.h"
11 #include "flutter/fml/logging.h"
12 #include "flutter/fml/synchronization/waitable_event.h"
13 #include "flutter/fml/trace_event.h"
14 #include "flutter/shell/common/shell_io_manager.h"
15 #import "flutter/shell/platform/darwin/common/InternalFlutterSwiftCommon/InternalFlutterSwiftCommon.h"
16 #import "flutter/shell/platform/darwin/ios/InternalFlutterSwift/InternalFlutterSwift.h"
19 
21 
22 namespace flutter {
23 
24 PlatformViewIOS::PlatformViewIOS(PlatformView::Delegate& delegate,
25  const std::shared_ptr<IOSContext>& context,
26  __weak FlutterPlatformViewsController* platform_views_controller,
27  const flutter::TaskRunners& task_runners)
28  : PlatformView(delegate, task_runners),
29  ios_context_(context),
30  platform_views_controller_(platform_views_controller),
31  platform_message_handler_(
32  new PlatformMessageHandlerIos(task_runners.GetPlatformTaskRunner())) {}
33 
35  PlatformView::Delegate& delegate,
36  __weak FlutterPlatformViewsController* platform_views_controller,
37  const flutter::TaskRunners& task_runners,
38  const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch)
40  delegate,
41  IOSContext::Create(is_gpu_disabled_sync_switch, delegate.OnPlatformViewGetSettings()),
42  platform_views_controller,
43  task_runners) {}
44 
46 
47 // |PlatformView|
48 void PlatformViewIOS::HandlePlatformMessage(std::unique_ptr<flutter::PlatformMessage> message) {
49  platform_message_handler_->HandlePlatformMessage(std::move(message));
50 }
51 
53  return owner_controller_;
54 }
55 
57  FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread());
58  std::lock_guard<std::mutex> guard(ios_surface_mutex_);
59  FlutterView* previousView = static_cast<FlutterView*>(owner_controller_.viewIfLoaded);
60  if (ios_surface_ || !owner_controller) {
61  NotifyDestroyed();
62  ios_surface_.reset();
63  }
64  owner_controller_ = owner_controller;
65  ApplyLocaleToOwnerController();
66 
67  // Clear the owner and rebind the accessibility bridge to nil when the view controller is deleted.
68  dealloc_view_controller_observer_.reset([[NSNotificationCenter defaultCenter]
69  addObserverForName:FlutterViewControllerWillDealloc
70  object:owner_controller_
71  queue:[NSOperationQueue mainQueue]
72  usingBlock:^(NSNotification* note) {
73  // Implicit copy of 'this' is fine.
74  if (accessibility_bridge_) {
75  FlutterViewController* deallocatingController =
76  static_cast<FlutterViewController*>(note.object);
77  FlutterView* deallocatingView =
78  static_cast<FlutterView*>(deallocatingController.viewIfLoaded);
79  accessibility_bridge_->SetViewController(nil, deallocatingView);
80  }
81  owner_controller_ = nil;
82  }]);
83 
84  if (owner_controller_ && owner_controller_.isViewLoaded) {
85  this->attachView(previousView);
86  } else {
87  EnsureAccessibilityBridge();
88  if (accessibility_bridge_) {
89  accessibility_bridge_->SetViewController(owner_controller_, previousView);
90  }
91  }
92  // Do not call `NotifyCreated()` here - let FlutterViewController take care
93  // of that when its Viewport is sized. If `NotifyCreated()` is called here,
94  // it can occasionally get invoked before the viewport is sized resulting in
95  // a framebuffer that will not be able to completely attach.
96 }
97 
99  FML_DCHECK(owner_controller_);
100  FML_DCHECK(owner_controller_.isViewLoaded) << "FlutterViewController's view should be loaded "
101  "before attaching to PlatformViewIOS.";
102  FlutterView* flutter_view = static_cast<FlutterView*>(owner_controller_.view);
103  CALayer* ca_layer = flutter_view.layer;
104  ios_surface_ = IOSSurface::Create(ios_context_, ca_layer);
105  FML_DCHECK(ios_surface_ != nullptr);
106 
107  EnsureAccessibilityBridge();
108  if (accessibility_bridge_) {
109  accessibility_bridge_->SetViewController(owner_controller_, previousView);
110  if (accessibility_bridge_->HasSemantics()) {
111  PostSemanticsUpdateNotification();
112  }
113  }
114 }
115 
117  NSObject<FlutterTexture>* texture) {
118  RegisterTexture(ios_context_->CreateExternalTexture(texture_id, texture));
119 }
120 
121 // |PlatformView|
122 std::unique_ptr<Surface> PlatformViewIOS::CreateRenderingSurface() {
123  FML_DCHECK(task_runners_.GetRasterTaskRunner()->RunsTasksOnCurrentThread());
124  std::lock_guard<std::mutex> guard(ios_surface_mutex_);
125  if (!ios_surface_) {
126  FML_DLOG(INFO) << "Could not CreateRenderingSurface, this PlatformViewIOS "
127  "has no ViewController.";
128  return nullptr;
129  }
130  return ios_surface_->CreateGPUSurface();
131 }
132 
133 // |PlatformView|
134 std::shared_ptr<ExternalViewEmbedder> PlatformViewIOS::CreateExternalViewEmbedder() {
135  return std::make_shared<IOSExternalViewEmbedder>(platform_views_controller_, ios_context_);
136 }
137 
138 // |PlatformView|
139 std::shared_ptr<impeller::Context> PlatformViewIOS::GetImpellerContext() const {
140  return ios_context_->GetImpellerContext();
141 }
142 
143 // |PlatformView|
144 void PlatformViewIOS::UpdateSemantics(int64_t view_id,
145  flutter::SemanticsNodeUpdates update,
146  flutter::CustomAccessibilityActionUpdates actions) {
147  EnsureAccessibilityBridge();
148  if (!accessibility_bridge_) {
149  return;
150  }
151  accessibility_bridge_.get()->UpdateSemantics(std::move(update), actions);
152  if (owner_controller_ && owner_controller_.isViewLoaded) {
153  // The bridge can cache semantics before the owner FlutterView is loaded. For example, a
154  // pre-warmed engine can receive semantics before FlutterViewController.loadView, when
155  // viewIfLoaded is still nil. Only notify observers after the semantics are applied to a loaded
156  // UIKit view, so accessibilityElements are ready to inspect. If cached semantics are applied
157  // later by attachView, attachView posts this same notification.
158  PostSemanticsUpdateNotification();
159  }
160 }
161 
162 // |PlatformView|
163 void PlatformViewIOS::SetApplicationLocale(std::string locale) {
164  application_locale_ = std::move(locale);
165  ApplyLocaleToOwnerController();
166 }
167 
168 // |PlatformView|
170  semantics_tree_enabled_ = enabled;
171  if (enabled) {
172  EnsureAccessibilityBridge();
173  } else {
174  accessibility_bridge_.reset();
175  }
176 }
177 
178 // |PlatformView|
179 std::unique_ptr<VsyncWaiter> PlatformViewIOS::CreateVSyncWaiter() {
180  return std::make_unique<VsyncWaiterIOS>(task_runners_, FlutterDisplayLinkManager.shared);
181 }
182 
183 // |PlatformView|
185  if (accessibility_bridge_) {
186  accessibility_bridge_.get()->clearState();
187  }
188  if (!owner_controller_) {
189  return;
190  }
191  [owner_controller_.platformViewsController reset];
192  [owner_controller_.restorationPlugin reset];
193  [owner_controller_.textInputPlugin reset];
194 }
195 
196 // |PlatformView|
197 std::unique_ptr<std::vector<std::string>> PlatformViewIOS::ComputePlatformResolvedLocales(
198  const std::vector<std::string>& supported_locale_data) {
199  size_t localeDataLength = 3;
200  NSMutableArray<NSString*>* supported_locale_identifiers =
201  [NSMutableArray arrayWithCapacity:supported_locale_data.size() / localeDataLength];
202  for (size_t i = 0; i < supported_locale_data.size(); i += localeDataLength) {
203  NSDictionary<NSString*, NSString*>* dict = @{
204  NSLocaleLanguageCode : [NSString stringWithUTF8String:supported_locale_data[i].c_str()]
205  ?: @"",
206  NSLocaleCountryCode : [NSString stringWithUTF8String:supported_locale_data[i + 1].c_str()]
207  ?: @"",
208  NSLocaleScriptCode : [NSString stringWithUTF8String:supported_locale_data[i + 2].c_str()]
209  ?: @""
210  };
211  [supported_locale_identifiers addObject:[NSLocale localeIdentifierFromComponents:dict]];
212  }
213  NSArray<NSString*>* result =
214  [NSBundle preferredLocalizationsFromArray:supported_locale_identifiers];
215 
216  // Output format should be either empty or 3 strings for language, country, and script.
217  std::unique_ptr<std::vector<std::string>> out = std::make_unique<std::vector<std::string>>();
218 
219  if (result != nullptr && [result count] > 0) {
220  NSLocale* locale = [NSLocale localeWithLocaleIdentifier:[result firstObject]];
221  NSString* languageCode = [locale languageCode];
222  out->emplace_back(languageCode == nullptr ? "" : languageCode.UTF8String);
223  NSString* countryCode = [locale countryCode];
224  out->emplace_back(countryCode == nullptr ? "" : countryCode.UTF8String);
225  NSString* scriptCode = [locale scriptCode];
226  out->emplace_back(scriptCode == nullptr ? "" : scriptCode.UTF8String);
227  }
228  return out;
229 }
230 
231 void PlatformViewIOS::PostSemanticsUpdateNotification() {
232  [[NSNotificationCenter defaultCenter] postNotificationName:FlutterSemanticsUpdateNotification
233  object:owner_controller_];
234 }
235 
236 void PlatformViewIOS::ApplyLocaleToOwnerController() {
237  if (owner_controller_) {
238  owner_controller_.applicationLocale =
239  application_locale_.empty() ? nil : @(application_locale_.data());
240  }
241 }
242 
243 void PlatformViewIOS::EnsureAccessibilityBridge() {
244  if (semantics_tree_enabled_ && !accessibility_bridge_ && owner_controller_) {
245  accessibility_bridge_ =
246  std::make_unique<AccessibilityBridge>(owner_controller_, this, platform_views_controller_);
247  }
248 }
249 
250 PlatformViewIOS::ScopedObserver::ScopedObserver() {}
251 
252 PlatformViewIOS::ScopedObserver::~ScopedObserver() {
253  if (observer_) {
254  [[NSNotificationCenter defaultCenter] removeObserver:observer_];
255  }
256 }
257 
258 void PlatformViewIOS::ScopedObserver::reset(id<NSObject> observer) {
259  if (observer != observer_) {
260  if (observer_) {
261  [[NSNotificationCenter defaultCenter] removeObserver:observer_];
262  }
263  observer_ = observer;
264  }
265 }
266 
267 } // namespace flutter
FLUTTER_DARWIN_EXPORT NSNotificationName const FlutterSemanticsUpdateNotification
NSNotificationName const FlutterViewControllerWillDealloc
Manages the lifetime of the on-screen and off-screen rendering contexts on iOS. On-screen contexts ar...
Definition: ios_context.h:38
static std::unique_ptr< IOSSurface > Create(const std::shared_ptr< IOSContext > &context, CALayer *layer)
Definition: ios_surface.mm:18
std::unique_ptr< Surface > CreateRenderingSurface() override
std::shared_ptr< impeller::Context > GetImpellerContext() const override
void UpdateSemantics(int64_t view_id, flutter::SemanticsNodeUpdates update, flutter::CustomAccessibilityActionUpdates actions) override
std::unique_ptr< VsyncWaiter > CreateVSyncWaiter() override
std::shared_ptr< ExternalViewEmbedder > CreateExternalViewEmbedder() override
void HandlePlatformMessage(std::unique_ptr< flutter::PlatformMessage > message) override
PlatformViewIOS(PlatformView::Delegate &delegate, const std::shared_ptr< IOSContext > &context, __weak FlutterPlatformViewsController *platform_views_controller, const flutter::TaskRunners &task_runners)
void attachView(FlutterView *previousView=nil)
FlutterViewController * GetOwnerViewController() const __attribute__((cf_audited_transfer))
void SetSemanticsTreeEnabled(bool enabled) override
void OnPreEngineRestart() const override
void SetOwnerViewController(__weak FlutterViewController *owner_controller)
void RegisterExternalTexture(int64_t id, NSObject< FlutterTexture > *texture)
void SetApplicationLocale(std::string locale) override
std::unique_ptr< std::vector< std::string > > ComputePlatformResolvedLocales(const std::vector< std::string > &supported_locale_data) override
int64_t texture_id