Flutter iOS Embedder
FlutterDartProject.mm File Reference
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h"
#import <Metal/Metal.h>
#import <UIKit/UIKit.h>
#include <syslog.h>
#include "flutter/common/constants.h"
#include "flutter/fml/build_config.h"
#include "flutter/shell/common/switches.h"
#include "flutter/shell/platform/darwin/common/command_line.h"

Go to the source code of this file.

Macros

#define FML_USED_ON_EMBEDDER
 

Functions

static BOOL DoesHardwareSupportWideGamut ()
 
flutter::Settings FLTDefaultSettingsForBundle (NSBundle *bundle, NSProcessInfo *processInfoOrNil)
 

Variables

FLUTTER_ASSERT_ARC const uint8_t kPlatformStrongDill []
 
const intptr_t kPlatformStrongDillSize
 
static const char * kApplicationKernelSnapshotFileName = "kernel_blob.bin"
 

Macro Definition Documentation

◆ FML_USED_ON_EMBEDDER

#define FML_USED_ON_EMBEDDER

Definition at line 5 of file FlutterDartProject.mm.

Function Documentation

◆ DoesHardwareSupportWideGamut()

static BOOL DoesHardwareSupportWideGamut ( )
static

Definition at line 31 of file FlutterDartProject.mm.

31  {
32  static BOOL result = NO;
33  static dispatch_once_t once_token = 0;
34  dispatch_once(&once_token, ^{
35  id<MTLDevice> device = MTLCreateSystemDefaultDevice();
36  if (@available(iOS 13.0, *)) {
37  // MTLGPUFamilyApple2 = A9/A10
38  result = [device supportsFamily:MTLGPUFamilyApple2];
39  } else {
40  // A9/A10 on iOS 10+
41  result = [device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v2];
42  }
43  });
44  return result;
45 }

Referenced by FlutterDartProject::defaultBundleIdentifier.

◆ FLTDefaultSettingsForBundle()

flutter::Settings FLTDefaultSettingsForBundle ( NSBundle *  bundle,
NSProcessInfo *  processInfoOrNil 
)

Definition at line 47 of file FlutterDartProject.mm.

47  {
48  auto command_line = flutter::CommandLineFromNSProcessInfo(processInfoOrNil);
49 
50  // Precedence:
51  // 1. Settings from the specified NSBundle.
52  // 2. Settings passed explicitly via command-line arguments.
53  // 3. Settings from the NSBundle with the default bundle ID.
54  // 4. Settings from the main NSBundle and default values.
55 
56  NSBundle* mainBundle = FLTGetApplicationBundle();
57  NSBundle* engineBundle = [NSBundle bundleForClass:[FlutterDartProject class]];
58 
59  bool hasExplicitBundle = bundle != nil;
60  if (bundle == nil) {
61  bundle = FLTFrameworkBundleWithIdentifier([FlutterDartProject defaultBundleIdentifier]);
62  }
63 
64  auto settings = flutter::SettingsFromCommandLine(command_line);
65 
66  settings.task_observer_add = [](intptr_t key, const fml::closure& callback) {
67  fml::MessageLoop::GetCurrent().AddTaskObserver(key, callback);
68  };
69 
70  settings.task_observer_remove = [](intptr_t key) {
71  fml::MessageLoop::GetCurrent().RemoveTaskObserver(key);
72  };
73 
74  settings.log_message_callback = [](const std::string& tag, const std::string& message) {
75  // TODO(cbracken): replace this with os_log-based approach.
76  // https://github.com/flutter/flutter/issues/44030
77  std::stringstream stream;
78  if (!tag.empty()) {
79  stream << tag << ": ";
80  }
81  stream << message;
82  std::string log = stream.str();
83  syslog(LOG_ALERT, "%.*s", (int)log.size(), log.c_str());
84  };
85 
86  settings.enable_platform_isolates = true;
87 
88  // The command line arguments may not always be complete. If they aren't, attempt to fill in
89  // defaults.
90 
91  // Flutter ships the ICU data file in the bundle of the engine. Look for it there.
92  if (settings.icu_data_path.empty()) {
93  NSString* icuDataPath = [engineBundle pathForResource:@"icudtl" ofType:@"dat"];
94  if (icuDataPath.length > 0) {
95  settings.icu_data_path = icuDataPath.UTF8String;
96  }
97  }
98 
99  if (flutter::DartVM::IsRunningPrecompiledCode()) {
100  if (hasExplicitBundle) {
101  NSString* executablePath = bundle.executablePath;
102  if ([[NSFileManager defaultManager] fileExistsAtPath:executablePath]) {
103  settings.application_library_path.push_back(executablePath.UTF8String);
104  }
105  }
106 
107  // No application bundle specified. Try a known location from the main bundle's Info.plist.
108  if (settings.application_library_path.empty()) {
109  NSString* libraryName = [mainBundle objectForInfoDictionaryKey:@"FLTLibraryPath"];
110  NSString* libraryPath = [mainBundle pathForResource:libraryName ofType:@""];
111  if (libraryPath.length > 0) {
112  NSString* executablePath = [NSBundle bundleWithPath:libraryPath].executablePath;
113  if (executablePath.length > 0) {
114  settings.application_library_path.push_back(executablePath.UTF8String);
115  }
116  }
117  }
118 
119  // In case the application bundle is still not specified, look for the App.framework in the
120  // Frameworks directory.
121  if (settings.application_library_path.empty()) {
122  NSString* applicationFrameworkPath = [mainBundle pathForResource:@"Frameworks/App.framework"
123  ofType:@""];
124  if (applicationFrameworkPath.length > 0) {
125  NSString* executablePath =
126  [NSBundle bundleWithPath:applicationFrameworkPath].executablePath;
127  if (executablePath.length > 0) {
128  settings.application_library_path.push_back(executablePath.UTF8String);
129  }
130  }
131  }
132  }
133 
134  // Checks to see if the flutter assets directory is already present.
135  if (settings.assets_path.empty()) {
136  NSString* assetsPath = FLTAssetsPathFromBundle(bundle);
137 
138  if (assetsPath.length == 0) {
139  NSLog(@"Failed to find assets path for \"%@\"", bundle);
140  } else {
141  settings.assets_path = assetsPath.UTF8String;
142 
143  // Check if there is an application kernel snapshot in the assets directory we could
144  // potentially use. Looking for the snapshot makes sense only if we have a VM that can use
145  // it.
146  if (!flutter::DartVM::IsRunningPrecompiledCode()) {
147  NSURL* applicationKernelSnapshotURL =
148  [NSURL URLWithString:@(kApplicationKernelSnapshotFileName)
149  relativeToURL:[NSURL fileURLWithPath:assetsPath]];
150  NSError* error;
151  if ([applicationKernelSnapshotURL checkResourceIsReachableAndReturnError:&error]) {
152  settings.application_kernel_asset = applicationKernelSnapshotURL.path.UTF8String;
153  } else {
154  NSLog(@"Failed to find snapshot at %@: %@", applicationKernelSnapshotURL.path, error);
155  }
156  }
157  }
158  }
159 
160  // Domain network configuration
161  // Disabled in https://github.com/flutter/flutter/issues/72723.
162  // Re-enable in https://github.com/flutter/flutter/issues/54448.
163  settings.may_insecurely_connect_to_all_domains = true;
164  settings.domain_network_policy = "";
165 
166  // Whether to enable wide gamut colors.
167 #if TARGET_OS_SIMULATOR
168  // As of Xcode 14.1, the wide gamut surface pixel formats are not supported by
169  // the simulator.
170  settings.enable_wide_gamut = false;
171  // Removes unused function warning.
173 #else
174  NSNumber* nsEnableWideGamut = [mainBundle objectForInfoDictionaryKey:@"FLTEnableWideGamut"];
175  BOOL enableWideGamut =
176  (nsEnableWideGamut ? nsEnableWideGamut.boolValue : YES) && DoesHardwareSupportWideGamut();
177  settings.enable_wide_gamut = enableWideGamut;
178 #endif
179 
180  NSNumber* nsAntialiasLines = [mainBundle objectForInfoDictionaryKey:@"FLTAntialiasLines"];
181  settings.impeller_antialiased_lines = (nsAntialiasLines ? nsAntialiasLines.boolValue : NO);
182 
183  settings.warn_on_impeller_opt_out = true;
184 
185  NSNumber* enableTraceSystrace = [mainBundle objectForInfoDictionaryKey:@"FLTTraceSystrace"];
186  // Change the default only if the option is present.
187  if (enableTraceSystrace != nil) {
188  settings.trace_systrace = enableTraceSystrace.boolValue;
189  }
190 
191  NSNumber* enableDartAsserts = [mainBundle objectForInfoDictionaryKey:@"FLTEnableDartAsserts"];
192  if (enableDartAsserts != nil) {
193  settings.dart_flags.push_back("--enable-asserts");
194  }
195 
196  NSNumber* enableDartProfiling = [mainBundle objectForInfoDictionaryKey:@"FLTEnableDartProfiling"];
197  // Change the default only if the option is present.
198  if (enableDartProfiling != nil) {
199  settings.enable_dart_profiling = enableDartProfiling.boolValue;
200  }
201 
202  // Leak Dart VM settings, set whether leave or clean up the VM after the last shell shuts down.
203  NSNumber* leakDartVM = [mainBundle objectForInfoDictionaryKey:@"FLTLeakDartVM"];
204  // It will change the default leak_vm value in settings only if the key exists.
205  if (leakDartVM != nil) {
206  settings.leak_vm = leakDartVM.boolValue;
207  }
208 
209  NSNumber* enableMergedPlatformUIThread =
210  [mainBundle objectForInfoDictionaryKey:@"FLTEnableMergedPlatformUIThread"];
211  if (enableMergedPlatformUIThread != nil) {
212  settings.merged_platform_ui_thread = enableMergedPlatformUIThread.boolValue;
213  }
214 
215 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
216  // There are no ownership concerns here as all mappings are owned by the
217  // embedder and not the engine.
218  auto make_mapping_callback = [](const uint8_t* mapping, size_t size) {
219  return [mapping, size]() { return std::make_unique<fml::NonOwnedMapping>(mapping, size); };
220  };
221 
222  settings.dart_library_sources_kernel =
223  make_mapping_callback(kPlatformStrongDill, kPlatformStrongDillSize);
224 #endif // FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
225 
226  // If we even support setting this e.g. from the command line or the plist,
227  // we should let the user override it.
228  // Otherwise, we want to set this to a value that will avoid having the OS
229  // kill us. On most iOS devices, that happens somewhere near half
230  // the available memory.
231  // The VM expects this value to be in megabytes.
232  if (settings.old_gen_heap_size <= 0) {
233  settings.old_gen_heap_size = std::round([NSProcessInfo processInfo].physicalMemory * .48 /
234  flutter::kMegaByteSizeInBytes);
235  }
236 
237  // This is the formula Android uses.
238  // https://android.googlesource.com/platform/frameworks/base/+/39ae5bac216757bc201490f4c7b8c0f63006c6cd/libs/hwui/renderthread/CacheManager.cpp#45
239  CGFloat scale = [UIScreen mainScreen].scale;
240  CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width * scale;
241  CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height * scale;
242  settings.resource_cache_max_bytes_threshold = screenWidth * screenHeight * 12 * 4;
243 
244  // Whether to enable ios embedder api.
245  NSNumber* enable_embedder_api =
246  [mainBundle objectForInfoDictionaryKey:@"FLTEnableIOSEmbedderAPI"];
247  // Change the default only if the option is present.
248  if (enable_embedder_api) {
249  settings.enable_embedder_api = enable_embedder_api.boolValue;
250  }
251 
252  return settings;
253 }
static const char * kApplicationKernelSnapshotFileName
const intptr_t kPlatformStrongDillSize
FLUTTER_ASSERT_ARC const uint8_t kPlatformStrongDill[]
static BOOL DoesHardwareSupportWideGamut()
NSBundle * FLTGetApplicationBundle()
NSBundle * FLTFrameworkBundleWithIdentifier(NSString *flutterFrameworkBundleID)
NSString * FLTAssetsPathFromBundle(NSBundle *bundle)
fml::CommandLine CommandLineFromNSProcessInfo(NSProcessInfo *processInfoOrNil=nil)
Definition: command_line.mm:13

Variable Documentation

◆ kApplicationKernelSnapshotFileName

const char* kApplicationKernelSnapshotFileName = "kernel_blob.bin"
static

Definition at line 29 of file FlutterDartProject.mm.

Referenced by FlutterDartProject::defaultBundleIdentifier.

◆ kPlatformStrongDill

FLUTTER_ASSERT_ARC const uint8_t kPlatformStrongDill[]
extern

◆ kPlatformStrongDillSize

const intptr_t kPlatformStrongDillSize
extern