Flutter Windows Embedder
flutter::FlutterWindowsEngine Class Reference

#include <flutter_windows_engine.h>

Public Member Functions

 FlutterWindowsEngine (const FlutterProjectBundle &project, std::shared_ptr< WindowsProcTable > windows_proc_table=nullptr)
 
virtual ~FlutterWindowsEngine ()
 
bool Run ()
 
bool Run (std::string_view entrypoint)
 
virtual bool running () const
 
virtual bool Stop ()
 
std::unique_ptr< FlutterWindowsViewCreateView (std::unique_ptr< WindowBindingHandler > window)
 
FlutterWindowsViewview (FlutterViewId view_id) const
 
FlutterDesktopPluginRegistrarRef GetRegistrar ()
 
void AddPluginRegistrarDestructionCallback (FlutterDesktopOnPluginRegistrarDestroyed callback, FlutterDesktopPluginRegistrarRef registrar)
 
void SetSwitches (const std::vector< std::string > &switches)
 
FlutterDesktopMessengerRef messenger ()
 
IncomingMessageDispatchermessage_dispatcher ()
 
TaskRunnertask_runner ()
 
BinaryMessengermessenger_wrapper ()
 
FlutterWindowsTextureRegistrartexture_registrar ()
 
egl::Manageregl_manager () const
 
WindowProcDelegateManagerwindow_proc_delegate_manager ()
 
void SendWindowMetricsEvent (const FlutterWindowMetricsEvent &event)
 
void SendPointerEvent (const FlutterPointerEvent &event)
 
void SendKeyEvent (const FlutterKeyEvent &event, FlutterKeyEventCallback callback, void *user_data)
 
KeyboardHandlerBasekeyboard_key_handler ()
 
TextInputPlugintext_input_plugin ()
 
bool SendPlatformMessage (const char *channel, const uint8_t *message, const size_t message_size, const FlutterDesktopBinaryReply reply, void *user_data)
 
void SendPlatformMessageResponse (const FlutterDesktopMessageResponseHandle *handle, const uint8_t *data, size_t data_length)
 
void HandlePlatformMessage (const FlutterPlatformMessage *)
 
void ReloadSystemFonts ()
 
void ScheduleFrame ()
 
void SetNextFrameCallback (fml::closure callback)
 
bool RegisterExternalTexture (int64_t texture_id)
 
bool UnregisterExternalTexture (int64_t texture_id)
 
bool MarkExternalTextureFrameAvailable (int64_t texture_id)
 
virtual bool PostRasterThreadTask (fml::closure callback) const
 
void OnVsync (intptr_t baton)
 
bool DispatchSemanticsAction (uint64_t id, FlutterSemanticsAction action, fml::MallocMapping data)
 
void UpdateSemanticsEnabled (bool enabled)
 
bool semantics_enabled () const
 
void UpdateAccessibilityFeatures ()
 
void UpdateHighContrastMode ()
 
bool high_contrast_enabled () const
 
void SetRootIsolateCreateCallback (const fml::closure &callback)
 
std::string GetExecutableName () const
 
void OnQuit (std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)
 
void RequestApplicationQuit (HWND hwnd, WPARAM wparam, LPARAM lparam, AppExitType exit_type)
 
void OnDwmCompositionChanged ()
 
void OnWindowStateEvent (HWND hwnd, WindowStateEvent event)
 
std::optional< LRESULT > ProcessExternalWindowMessage (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
 
WindowsLifecycleManagerlifecycle_manager ()
 
std::shared_ptr< WindowsProcTablewindows_proc_table ()
 

Protected Member Functions

virtual std::unique_ptr< KeyboardHandlerBaseCreateKeyboardKeyHandler (BinaryMessenger *messenger, KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state, KeyboardKeyEmbedderHandler::MapVirtualKeyToScanCode map_vk_to_scan)
 
virtual std::unique_ptr< TextInputPluginCreateTextInputPlugin (BinaryMessenger *messenger)
 
void OnPreEngineRestart ()
 
virtual void OnChannelUpdate (std::string name, bool listening)
 

Friends

class EngineModifier
 

Detailed Description

Definition at line 89 of file flutter_windows_engine.h.

Constructor & Destructor Documentation

◆ FlutterWindowsEngine()

flutter::FlutterWindowsEngine::FlutterWindowsEngine ( const FlutterProjectBundle project,
std::shared_ptr< WindowsProcTable windows_proc_table = nullptr 
)

Definition at line 146 of file flutter_windows_engine.cc.

149  : project_(std::make_unique<FlutterProjectBundle>(project)),
150  windows_proc_table_(std::move(windows_proc_table)),
151  aot_data_(nullptr, nullptr),
152  lifecycle_manager_(std::make_unique<WindowsLifecycleManager>(this)) {
153  if (windows_proc_table_ == nullptr) {
154  windows_proc_table_ = std::make_shared<WindowsProcTable>();
155  }
156 
157  gl_ = egl::ProcTable::Create();
158 
159  embedder_api_.struct_size = sizeof(FlutterEngineProcTable);
160  FlutterEngineGetProcAddresses(&embedder_api_);
161 
162  task_runner_ =
163  std::make_unique<TaskRunner>(
164  embedder_api_.GetCurrentTime, [this](const auto* task) {
165  if (!engine_) {
166  FML_LOG(ERROR)
167  << "Cannot post an engine task when engine is not running.";
168  return;
169  }
170  if (embedder_api_.RunTask(engine_, task) != kSuccess) {
171  FML_LOG(ERROR) << "Failed to post an engine task.";
172  }
173  });
174 
175  // Set up the legacy structs backing the API handles.
176  messenger_ =
177  fml::RefPtr<FlutterDesktopMessenger>(new FlutterDesktopMessenger());
178  messenger_->SetEngine(this);
179  plugin_registrar_ = std::make_unique<FlutterDesktopPluginRegistrar>();
180  plugin_registrar_->engine = this;
181 
182  messenger_wrapper_ =
183  std::make_unique<BinaryMessengerImpl>(messenger_->ToRef());
184  message_dispatcher_ =
185  std::make_unique<IncomingMessageDispatcher>(messenger_->ToRef());
186 
187  texture_registrar_ =
188  std::make_unique<FlutterWindowsTextureRegistrar>(this, gl_);
189 
190  // Check for impeller support.
191  auto& switches = project_->GetSwitches();
192  enable_impeller_ = std::find(switches.begin(), switches.end(),
193  "--enable-impeller=true") != switches.end();
194 
195  egl_manager_ = egl::Manager::Create(enable_impeller_);
196  window_proc_delegate_manager_ = std::make_unique<WindowProcDelegateManager>();
197  window_proc_delegate_manager_->RegisterTopLevelWindowProcDelegate(
198  [](HWND hwnd, UINT msg, WPARAM wpar, LPARAM lpar, void* user_data,
199  LRESULT* result) {
200  BASE_DCHECK(user_data);
201  FlutterWindowsEngine* that =
202  static_cast<FlutterWindowsEngine*>(user_data);
203  BASE_DCHECK(that->lifecycle_manager_);
204  return that->lifecycle_manager_->WindowProc(hwnd, msg, wpar, lpar,
205  result);
206  },
207  static_cast<void*>(this));
208 
209  // Set up internal channels.
210  // TODO: Replace this with an embedder.h API. See
211  // https://github.com/flutter/flutter/issues/71099
212  internal_plugin_registrar_ =
213  std::make_unique<PluginRegistrar>(plugin_registrar_.get());
214 
215  accessibility_plugin_ = std::make_unique<AccessibilityPlugin>(this);
216  AccessibilityPlugin::SetUp(messenger_wrapper_.get(),
217  accessibility_plugin_.get());
218 
219  cursor_handler_ =
220  std::make_unique<CursorHandler>(messenger_wrapper_.get(), this);
221  platform_handler_ =
222  std::make_unique<PlatformHandler>(messenger_wrapper_.get(), this);
223  settings_plugin_ = std::make_unique<SettingsPlugin>(messenger_wrapper_.get(),
224  task_runner_.get());
225 }

References flutter::egl::ProcTable::Create().

◆ ~FlutterWindowsEngine()

flutter::FlutterWindowsEngine::~FlutterWindowsEngine ( )
virtual

Definition at line 227 of file flutter_windows_engine.cc.

227  {
228  messenger_->SetEngine(nullptr);
229  Stop();
230 }

Member Function Documentation

◆ AddPluginRegistrarDestructionCallback()

void flutter::FlutterWindowsEngine::AddPluginRegistrarDestructionCallback ( FlutterDesktopOnPluginRegistrarDestroyed  callback,
FlutterDesktopPluginRegistrarRef  registrar 
)

Definition at line 547 of file flutter_windows_engine.cc.

549  {
550  plugin_registrar_destruction_callbacks_[callback] = registrar;
551 }

References callback.

Referenced by FlutterDesktopPluginRegistrarSetDestructionHandler().

◆ CreateKeyboardKeyHandler()

std::unique_ptr< KeyboardHandlerBase > flutter::FlutterWindowsEngine::CreateKeyboardKeyHandler ( BinaryMessenger messenger,
KeyboardKeyEmbedderHandler::GetKeyStateHandler  get_key_state,
KeyboardKeyEmbedderHandler::MapVirtualKeyToScanCode  map_vk_to_scan 
)
protectedvirtual

Definition at line 696 of file flutter_windows_engine.cc.

699  {
700  auto keyboard_key_handler = std::make_unique<KeyboardKeyHandler>(messenger);
701  keyboard_key_handler->AddDelegate(
702  std::make_unique<KeyboardKeyEmbedderHandler>(
703  [this](const FlutterKeyEvent& event, FlutterKeyEventCallback callback,
704  void* user_data) {
705  return SendKeyEvent(event, callback, user_data);
706  },
707  get_key_state, map_vk_to_scan));
708  keyboard_key_handler->AddDelegate(
709  std::make_unique<KeyboardKeyChannelHandler>(messenger));
710  keyboard_key_handler->InitKeyboardChannel();
711  return keyboard_key_handler;
712 }

References callback, and user_data.

◆ CreateTextInputPlugin()

std::unique_ptr< TextInputPlugin > flutter::FlutterWindowsEngine::CreateTextInputPlugin ( BinaryMessenger messenger)
protectedvirtual

Definition at line 714 of file flutter_windows_engine.cc.

715  {
716  return std::make_unique<TextInputPlugin>(messenger, this);
717 }

◆ CreateView()

std::unique_ptr< FlutterWindowsView > flutter::FlutterWindowsEngine::CreateView ( std::unique_ptr< WindowBindingHandler window)

Definition at line 492 of file flutter_windows_engine.cc.

493  {
494  // TODO(loicsharma): Remove implicit view assumption.
495  // https://github.com/flutter/flutter/issues/142845
496  auto view = std::make_unique<FlutterWindowsView>(
497  kImplicitViewId, this, std::move(window), windows_proc_table_);
498 
499  views_[kImplicitViewId] = view.get();
500  InitializeKeyboard();
501 
502  return std::move(view);
503 }

References flutter::kImplicitViewId.

Referenced by CreateViewController().

◆ DispatchSemanticsAction()

bool flutter::FlutterWindowsEngine::DispatchSemanticsAction ( uint64_t  id,
FlutterSemanticsAction  action,
fml::MallocMapping  data 
)

Definition at line 755 of file flutter_windows_engine.cc.

758  {
759  return (embedder_api_.DispatchSemanticsAction(engine_, target, action,
760  data.GetMapping(),
761  data.GetSize()) == kSuccess);
762 }

Referenced by flutter::AccessibilityBridgeWindows::DispatchAccessibilityAction().

◆ egl_manager()

◆ GetExecutableName()

std::string flutter::FlutterWindowsEngine::GetExecutableName ( ) const

Definition at line 781 of file flutter_windows_engine.cc.

781  {
782  std::pair<bool, std::string> result = fml::paths::GetExecutablePath();
783  if (result.first) {
784  const std::string& executable_path = result.second;
785  size_t last_separator = executable_path.find_last_of("/\\");
786  if (last_separator == std::string::npos ||
787  last_separator == executable_path.size() - 1) {
788  return executable_path;
789  }
790  return executable_path.substr(last_separator + 1);
791  }
792  return "Flutter";
793 }

◆ GetRegistrar()

FlutterDesktopPluginRegistrarRef flutter::FlutterWindowsEngine::GetRegistrar ( )

Definition at line 543 of file flutter_windows_engine.cc.

543  {
544  return plugin_registrar_.get();
545 }

Referenced by FlutterDesktopEngineGetPluginRegistrar().

◆ HandlePlatformMessage()

void flutter::FlutterWindowsEngine::HandlePlatformMessage ( const FlutterPlatformMessage *  engine_message)

Definition at line 615 of file flutter_windows_engine.cc.

616  {
617  if (engine_message->struct_size != sizeof(FlutterPlatformMessage)) {
618  FML_LOG(ERROR) << "Invalid message size received. Expected: "
619  << sizeof(FlutterPlatformMessage) << " but received "
620  << engine_message->struct_size;
621  return;
622  }
623 
624  auto message = ConvertToDesktopMessage(*engine_message);
625 
626  message_dispatcher_->HandleMessage(message, [this] {}, [this] {});
627 }

References message.

Referenced by Run().

◆ high_contrast_enabled()

bool flutter::FlutterWindowsEngine::high_contrast_enabled ( ) const
inline

Definition at line 242 of file flutter_windows_engine.h.

242 { return high_contrast_enabled_; }

◆ keyboard_key_handler()

KeyboardHandlerBase* flutter::FlutterWindowsEngine::keyboard_key_handler ( )
inline

Definition at line 176 of file flutter_windows_engine.h.

176  {
177  return keyboard_key_handler_.get();
178  }

Referenced by flutter::FlutterWindowsView::OnPointerMove().

◆ lifecycle_manager()

WindowsLifecycleManager* flutter::FlutterWindowsEngine::lifecycle_manager ( )
inline

Definition at line 287 of file flutter_windows_engine.h.

287  {
288  return lifecycle_manager_.get();
289  }

◆ MarkExternalTextureFrameAvailable()

bool flutter::FlutterWindowsEngine::MarkExternalTextureFrameAvailable ( int64_t  texture_id)

Definition at line 729 of file flutter_windows_engine.cc.

730  {
731  return (embedder_api_.MarkExternalTextureFrameAvailable(
732  engine_, texture_id) == kSuccess);
733 }

◆ message_dispatcher()

IncomingMessageDispatcher* flutter::FlutterWindowsEngine::message_dispatcher ( )
inline

Definition at line 145 of file flutter_windows_engine.h.

145  {
146  return message_dispatcher_.get();
147  }

Referenced by FlutterDesktopMessengerSetCallback().

◆ messenger()

FlutterDesktopMessengerRef flutter::FlutterWindowsEngine::messenger ( )
inline

Definition at line 143 of file flutter_windows_engine.h.

143 { return messenger_->ToRef(); }

Referenced by FlutterDesktopEngineGetMessenger(), and FlutterDesktopPluginRegistrarGetMessenger().

◆ messenger_wrapper()

BinaryMessenger* flutter::FlutterWindowsEngine::messenger_wrapper ( )
inline

Definition at line 151 of file flutter_windows_engine.h.

151 { return messenger_wrapper_.get(); }

◆ OnChannelUpdate()

void flutter::FlutterWindowsEngine::OnChannelUpdate ( std::string  name,
bool  listening 
)
protectedvirtual

Definition at line 855 of file flutter_windows_engine.cc.

855  {
856  if (name == "flutter/platform" && listening) {
857  lifecycle_manager_->BeginProcessingExit();
858  } else if (name == "flutter/lifecycle" && listening) {
859  lifecycle_manager_->BeginProcessingLifecycle();
860  }
861 }

◆ OnDwmCompositionChanged()

void flutter::FlutterWindowsEngine::OnDwmCompositionChanged ( )

Definition at line 832 of file flutter_windows_engine.cc.

832  {
833  for (auto iterator = views_.begin(); iterator != views_.end(); iterator++) {
834  iterator->second->OnDwmCompositionChanged();
835  }
836 }

Referenced by flutter::WindowsLifecycleManager::WindowProc().

◆ OnPreEngineRestart()

void flutter::FlutterWindowsEngine::OnPreEngineRestart ( )
protected

Definition at line 774 of file flutter_windows_engine.cc.

774  {
775  // Reset the keyboard's state on hot restart.
776  if (!views_.empty()) {
777  InitializeKeyboard();
778  }
779 }

Referenced by Run().

◆ OnQuit()

void flutter::FlutterWindowsEngine::OnQuit ( std::optional< HWND >  hwnd,
std::optional< WPARAM >  wparam,
std::optional< LPARAM >  lparam,
UINT  exit_code 
)

Definition at line 825 of file flutter_windows_engine.cc.

828  {
829  lifecycle_manager_->Quit(hwnd, wparam, lparam, exit_code);
830 }

Referenced by flutter::PlatformHandler::QuitApplication().

◆ OnVsync()

void flutter::FlutterWindowsEngine::OnVsync ( intptr_t  baton)

Definition at line 505 of file flutter_windows_engine.cc.

505  {
506  std::chrono::nanoseconds current_time =
507  std::chrono::nanoseconds(embedder_api_.GetCurrentTime());
508  std::chrono::nanoseconds frame_interval = FrameInterval();
509  auto next = SnapToNextTick(current_time, start_time_, frame_interval);
510  embedder_api_.OnVsync(engine_, baton, next.count(),
511  (next + frame_interval).count());
512 }

Referenced by Run().

◆ OnWindowStateEvent()

void flutter::FlutterWindowsEngine::OnWindowStateEvent ( HWND  hwnd,
WindowStateEvent  event 
)

Definition at line 838 of file flutter_windows_engine.cc.

839  {
840  lifecycle_manager_->OnWindowStateEvent(hwnd, event);
841 }

Referenced by flutter::FlutterWindowsView::OnWindowStateEvent(), and flutter::FlutterWindowsView::~FlutterWindowsView().

◆ PostRasterThreadTask()

bool flutter::FlutterWindowsEngine::PostRasterThreadTask ( fml::closure  callback) const
virtual

Definition at line 735 of file flutter_windows_engine.cc.

735  {
736  struct Captures {
737  fml::closure callback;
738  };
739  auto captures = new Captures();
740  captures->callback = std::move(callback);
741  if (embedder_api_.PostRenderThreadTask(
742  engine_,
743  [](void* opaque) {
744  auto captures = reinterpret_cast<Captures*>(opaque);
745  captures->callback();
746  delete captures;
747  },
748  captures) == kSuccess) {
749  return true;
750  }
751  delete captures;
752  return false;
753 }

References callback.

Referenced by flutter::FlutterWindowsTextureRegistrar::UnregisterTexture().

◆ ProcessExternalWindowMessage()

std::optional< LRESULT > flutter::FlutterWindowsEngine::ProcessExternalWindowMessage ( HWND  hwnd,
UINT  message,
WPARAM  wparam,
LPARAM  lparam 
)

Definition at line 843 of file flutter_windows_engine.cc.

847  {
848  if (lifecycle_manager_) {
849  return lifecycle_manager_->ExternalWindowMessage(hwnd, message, wparam,
850  lparam);
851  }
852  return std::nullopt;
853 }

References message.

Referenced by FlutterDesktopEngineProcessExternalWindowMessage().

◆ RegisterExternalTexture()

bool flutter::FlutterWindowsEngine::RegisterExternalTexture ( int64_t  texture_id)

Definition at line 719 of file flutter_windows_engine.cc.

719  {
720  return (embedder_api_.RegisterExternalTexture(engine_, texture_id) ==
721  kSuccess);
722 }

◆ ReloadSystemFonts()

void flutter::FlutterWindowsEngine::ReloadSystemFonts ( )

Definition at line 629 of file flutter_windows_engine.cc.

629  {
630  embedder_api_.ReloadSystemFonts(engine_);
631 }

Referenced by FlutterDesktopEngineReloadSystemFonts().

◆ RequestApplicationQuit()

void flutter::FlutterWindowsEngine::RequestApplicationQuit ( HWND  hwnd,
WPARAM  wparam,
LPARAM  lparam,
AppExitType  exit_type 
)

Definition at line 818 of file flutter_windows_engine.cc.

821  {
822  platform_handler_->RequestAppExit(hwnd, wparam, lparam, exit_type, 0);
823 }

◆ Run() [1/2]

bool flutter::FlutterWindowsEngine::Run ( )

Definition at line 237 of file flutter_windows_engine.cc.

237  {
238  return Run("");
239 }

Referenced by FlutterDesktopEngineRun().

◆ Run() [2/2]

bool flutter::FlutterWindowsEngine::Run ( std::string_view  entrypoint)

Definition at line 241 of file flutter_windows_engine.cc.

241  {
242  if (!project_->HasValidPaths()) {
243  FML_LOG(ERROR) << "Missing or unresolvable paths to assets.";
244  return false;
245  }
246  std::string assets_path_string = project_->assets_path().u8string();
247  std::string icu_path_string = project_->icu_path().u8string();
248  if (embedder_api_.RunsAOTCompiledDartCode()) {
249  aot_data_ = project_->LoadAotData(embedder_api_);
250  if (!aot_data_) {
251  FML_LOG(ERROR) << "Unable to start engine without AOT data.";
252  return false;
253  }
254  }
255 
256  // FlutterProjectArgs is expecting a full argv, so when processing it for
257  // flags the first item is treated as the executable and ignored. Add a dummy
258  // value so that all provided arguments are used.
259  std::string executable_name = GetExecutableName();
260  std::vector<const char*> argv = {executable_name.c_str()};
261  std::vector<std::string> switches = project_->GetSwitches();
262  std::transform(
263  switches.begin(), switches.end(), std::back_inserter(argv),
264  [](const std::string& arg) -> const char* { return arg.c_str(); });
265 
266  const std::vector<std::string>& entrypoint_args =
267  project_->dart_entrypoint_arguments();
268  std::vector<const char*> entrypoint_argv;
269  std::transform(
270  entrypoint_args.begin(), entrypoint_args.end(),
271  std::back_inserter(entrypoint_argv),
272  [](const std::string& arg) -> const char* { return arg.c_str(); });
273 
274  // Configure task runners.
275  FlutterTaskRunnerDescription platform_task_runner = {};
276  platform_task_runner.struct_size = sizeof(FlutterTaskRunnerDescription);
277  platform_task_runner.user_data = task_runner_.get();
278  platform_task_runner.runs_task_on_current_thread_callback =
279  [](void* user_data) -> bool {
280  return static_cast<TaskRunner*>(user_data)->RunsTasksOnCurrentThread();
281  };
282  platform_task_runner.post_task_callback = [](FlutterTask task,
283  uint64_t target_time_nanos,
284  void* user_data) -> void {
285  static_cast<TaskRunner*>(user_data)->PostFlutterTask(task,
286  target_time_nanos);
287  };
288  FlutterCustomTaskRunners custom_task_runners = {};
289  custom_task_runners.struct_size = sizeof(FlutterCustomTaskRunners);
290  custom_task_runners.platform_task_runner = &platform_task_runner;
291  custom_task_runners.thread_priority_setter =
293 
294  FlutterProjectArgs args = {};
295  args.struct_size = sizeof(FlutterProjectArgs);
296  args.shutdown_dart_vm_when_done = true;
297  args.assets_path = assets_path_string.c_str();
298  args.icu_data_path = icu_path_string.c_str();
299  args.command_line_argc = static_cast<int>(argv.size());
300  args.command_line_argv = argv.empty() ? nullptr : argv.data();
301 
302  // Fail if conflicting non-default entrypoints are specified in the method
303  // argument and the project.
304  //
305  // TODO(cbracken): https://github.com/flutter/flutter/issues/109285
306  // The entrypoint method parameter should eventually be removed from this
307  // method and only the entrypoint specified in project_ should be used.
308  if (!project_->dart_entrypoint().empty() && !entrypoint.empty() &&
309  project_->dart_entrypoint() != entrypoint) {
310  FML_LOG(ERROR) << "Conflicting entrypoints were specified in "
311  "FlutterDesktopEngineProperties.dart_entrypoint and "
312  "FlutterDesktopEngineRun(engine, entry_point). ";
313  return false;
314  }
315  if (!entrypoint.empty()) {
316  args.custom_dart_entrypoint = entrypoint.data();
317  } else if (!project_->dart_entrypoint().empty()) {
318  args.custom_dart_entrypoint = project_->dart_entrypoint().c_str();
319  }
320  args.dart_entrypoint_argc = static_cast<int>(entrypoint_argv.size());
321  args.dart_entrypoint_argv =
322  entrypoint_argv.empty() ? nullptr : entrypoint_argv.data();
323  args.platform_message_callback =
324  [](const FlutterPlatformMessage* engine_message,
325  void* user_data) -> void {
326  auto host = static_cast<FlutterWindowsEngine*>(user_data);
327  return host->HandlePlatformMessage(engine_message);
328  };
329  args.vsync_callback = [](void* user_data, intptr_t baton) -> void {
330  auto host = static_cast<FlutterWindowsEngine*>(user_data);
331  host->OnVsync(baton);
332  };
333  args.on_pre_engine_restart_callback = [](void* user_data) {
334  auto host = static_cast<FlutterWindowsEngine*>(user_data);
335  host->OnPreEngineRestart();
336  };
337  args.update_semantics_callback2 = [](const FlutterSemanticsUpdate2* update,
338  void* user_data) {
339  auto host = static_cast<FlutterWindowsEngine*>(user_data);
340 
341  // TODO(loicsharma): Remove implicit view assumption.
342  // https://github.com/flutter/flutter/issues/142845
343  auto view = host->view(kImplicitViewId);
344  if (!view) {
345  return;
346  }
347 
348  auto accessibility_bridge = view->accessibility_bridge().lock();
349  if (!accessibility_bridge) {
350  return;
351  }
352 
353  for (size_t i = 0; i < update->node_count; i++) {
354  const FlutterSemanticsNode2* node = update->nodes[i];
355  accessibility_bridge->AddFlutterSemanticsNodeUpdate(*node);
356  }
357 
358  for (size_t i = 0; i < update->custom_action_count; i++) {
359  const FlutterSemanticsCustomAction2* action = update->custom_actions[i];
360  accessibility_bridge->AddFlutterSemanticsCustomActionUpdate(*action);
361  }
362 
363  accessibility_bridge->CommitUpdates();
364  };
365  args.root_isolate_create_callback = [](void* user_data) {
366  auto host = static_cast<FlutterWindowsEngine*>(user_data);
367  if (host->root_isolate_create_callback_) {
368  host->root_isolate_create_callback_();
369  }
370  };
371  args.channel_update_callback = [](const FlutterChannelUpdate* update,
372  void* user_data) {
373  auto host = static_cast<FlutterWindowsEngine*>(user_data);
374  if (SAFE_ACCESS(update, channel, nullptr) != nullptr) {
375  std::string channel_name(update->channel);
376  host->OnChannelUpdate(std::move(channel_name),
377  SAFE_ACCESS(update, listening, false));
378  }
379  };
380 
381  args.custom_task_runners = &custom_task_runners;
382 
383  if (!platform_view_plugin_) {
384  platform_view_plugin_ = std::make_unique<PlatformViewPlugin>(
385  messenger_wrapper_.get(), task_runner_.get());
386  }
387  if (egl_manager_) {
388  auto resolver = [](const char* name) -> void* {
389  return reinterpret_cast<void*>(::eglGetProcAddress(name));
390  };
391 
392  // TODO(schectman) Pass the platform view manager to the compositor
393  // constructors: https://github.com/flutter/flutter/issues/143375
394  compositor_ = std::make_unique<CompositorOpenGL>(this, resolver);
395  } else {
396  compositor_ = std::make_unique<CompositorSoftware>(this);
397  }
398 
399  FlutterCompositor compositor = {};
400  compositor.struct_size = sizeof(FlutterCompositor);
401  compositor.user_data = this;
402  compositor.create_backing_store_callback =
403  [](const FlutterBackingStoreConfig* config,
404  FlutterBackingStore* backing_store_out, void* user_data) -> bool {
405  auto host = static_cast<FlutterWindowsEngine*>(user_data);
406 
407  return host->compositor_->CreateBackingStore(*config, backing_store_out);
408  };
409 
410  compositor.collect_backing_store_callback =
411  [](const FlutterBackingStore* backing_store, void* user_data) -> bool {
412  auto host = static_cast<FlutterWindowsEngine*>(user_data);
413 
414  return host->compositor_->CollectBackingStore(backing_store);
415  };
416 
417  compositor.present_view_callback =
418  [](const FlutterPresentViewInfo* info) -> bool {
419  auto host = static_cast<FlutterWindowsEngine*>(info->user_data);
420 
421  return host->compositor_->Present(info->view_id, info->layers,
422  info->layers_count);
423  };
424  args.compositor = &compositor;
425 
426  if (aot_data_) {
427  args.aot_data = aot_data_.get();
428  }
429 
430  // The platform thread creates OpenGL contexts. These
431  // must be released to be used by the engine's threads.
432  FML_DCHECK(!egl_manager_ || !egl_manager_->HasContextCurrent());
433 
434  FlutterRendererConfig renderer_config;
435 
436  if (enable_impeller_) {
437  // Impeller does not support a Software backend. Avoid falling back and
438  // confusing the engine on which renderer is selected.
439  if (!egl_manager_) {
440  FML_LOG(ERROR) << "Could not create surface manager. Impeller backend "
441  "does not support software rendering.";
442  return false;
443  }
444  renderer_config = GetOpenGLRendererConfig();
445  } else {
446  renderer_config =
447  egl_manager_ ? GetOpenGLRendererConfig() : GetSoftwareRendererConfig();
448  }
449 
450  auto result = embedder_api_.Run(FLUTTER_ENGINE_VERSION, &renderer_config,
451  &args, this, &engine_);
452  if (result != kSuccess || engine_ == nullptr) {
453  FML_LOG(ERROR) << "Failed to start Flutter engine: error " << result;
454  return false;
455  }
456 
457  // Configure device frame rate displayed via devtools.
458  FlutterEngineDisplay display = {};
459  display.struct_size = sizeof(FlutterEngineDisplay);
460  display.display_id = 0;
461  display.single_display = true;
462  display.refresh_rate =
463  1.0 / (static_cast<double>(FrameInterval().count()) / 1000000000.0);
464 
465  std::vector<FlutterEngineDisplay> displays = {display};
466  embedder_api_.NotifyDisplayUpdate(engine_,
467  kFlutterEngineDisplaysUpdateTypeStartup,
468  displays.data(), displays.size());
469 
470  SendSystemLocales();
471  SetLifecycleState(flutter::AppLifecycleState::kResumed);
472 
473  settings_plugin_->StartWatching();
474  settings_plugin_->SendSettings();
475 
476  return true;
477 }

References flutter::FlutterWindowsView::accessibility_bridge(), action, HandlePlatformMessage(), flutter::kImplicitViewId, OnPreEngineRestart(), OnVsync(), user_data, view(), and flutter::WindowsPlatformThreadPrioritySetter().

◆ running()

virtual bool flutter::FlutterWindowsEngine::running ( ) const
inlinevirtual

Definition at line 116 of file flutter_windows_engine.h.

116 { return engine_ != nullptr; }

Referenced by FlutterDesktopEngineDestroy().

◆ ScheduleFrame()

void flutter::FlutterWindowsEngine::ScheduleFrame ( )

Definition at line 633 of file flutter_windows_engine.cc.

633  {
634  embedder_api_.ScheduleFrame(engine_);
635 }

Referenced by flutter::FlutterWindowsView::ForceRedraw().

◆ semantics_enabled()

bool flutter::FlutterWindowsEngine::semantics_enabled ( ) const
inline

Definition at line 233 of file flutter_windows_engine.h.

233 { return semantics_enabled_; }

Referenced by flutter::AccessibilityPlugin::Announce().

◆ SendKeyEvent()

void flutter::FlutterWindowsEngine::SendKeyEvent ( const FlutterKeyEvent &  event,
FlutterKeyEventCallback  callback,
void *  user_data 
)

Definition at line 566 of file flutter_windows_engine.cc.

568  {
569  if (engine_) {
570  embedder_api_.SendKeyEvent(engine_, &event, callback, user_data);
571  }
572 }

◆ SendPlatformMessage()

bool flutter::FlutterWindowsEngine::SendPlatformMessage ( const char *  channel,
const uint8_t *  message,
const size_t  message_size,
const FlutterDesktopBinaryReply  reply,
void *  user_data 
)

Definition at line 574 of file flutter_windows_engine.cc.

579  {
580  FlutterPlatformMessageResponseHandle* response_handle = nullptr;
581  if (reply != nullptr && user_data != nullptr) {
582  FlutterEngineResult result =
583  embedder_api_.PlatformMessageCreateResponseHandle(
584  engine_, reply, user_data, &response_handle);
585  if (result != kSuccess) {
586  FML_LOG(ERROR) << "Failed to create response handle";
587  return false;
588  }
589  }
590 
591  FlutterPlatformMessage platform_message = {
592  sizeof(FlutterPlatformMessage),
593  channel,
594  message,
595  message_size,
596  response_handle,
597  };
598 
599  FlutterEngineResult message_result =
600  embedder_api_.SendPlatformMessage(engine_, &platform_message);
601  if (response_handle != nullptr) {
602  embedder_api_.PlatformMessageReleaseResponseHandle(engine_,
603  response_handle);
604  }
605  return message_result == kSuccess;
606 }

References user_data.

Referenced by FlutterDesktopMessengerSendWithReply().

◆ SendPlatformMessageResponse()

void flutter::FlutterWindowsEngine::SendPlatformMessageResponse ( const FlutterDesktopMessageResponseHandle handle,
const uint8_t *  data,
size_t  data_length 
)

Definition at line 608 of file flutter_windows_engine.cc.

611  {
612  embedder_api_.SendPlatformMessageResponse(engine_, handle, data, data_length);
613 }

Referenced by FlutterDesktopMessengerSendResponse().

◆ SendPointerEvent()

void flutter::FlutterWindowsEngine::SendPointerEvent ( const FlutterPointerEvent &  event)

Definition at line 560 of file flutter_windows_engine.cc.

560  {
561  if (engine_) {
562  embedder_api_.SendPointerEvent(engine_, &event, 1);
563  }
564 }

◆ SendWindowMetricsEvent()

void flutter::FlutterWindowsEngine::SendWindowMetricsEvent ( const FlutterWindowMetricsEvent &  event)

Definition at line 553 of file flutter_windows_engine.cc.

554  {
555  if (engine_) {
556  embedder_api_.SendWindowMetricsEvent(engine_, &event);
557  }
558 }

◆ SetNextFrameCallback()

void flutter::FlutterWindowsEngine::SetNextFrameCallback ( fml::closure  callback)

Definition at line 637 of file flutter_windows_engine.cc.

637  {
638  next_frame_callback_ = std::move(callback);
639 
640  embedder_api_.SetNextFrameCallback(
641  engine_,
642  [](void* user_data) {
643  // Embedder callback runs on raster thread. Switch back to platform
644  // thread.
645  FlutterWindowsEngine* self =
646  static_cast<FlutterWindowsEngine*>(user_data);
647 
648  self->task_runner_->PostTask(std::move(self->next_frame_callback_));
649  },
650  this);
651 }

Referenced by FlutterDesktopEngineSetNextFrameCallback().

◆ SetRootIsolateCreateCallback()

void flutter::FlutterWindowsEngine::SetRootIsolateCreateCallback ( const fml::closure &  callback)
inline

Definition at line 253 of file flutter_windows_engine.h.

253  {
254  root_isolate_create_callback_ = callback;
255  }

References callback.

◆ SetSwitches()

void flutter::FlutterWindowsEngine::SetSwitches ( const std::vector< std::string > &  switches)

Definition at line 232 of file flutter_windows_engine.cc.

233  {
234  project_->SetSwitches(switches);
235 }

◆ Stop()

bool flutter::FlutterWindowsEngine::Stop ( )
virtual

Definition at line 479 of file flutter_windows_engine.cc.

479  {
480  if (engine_) {
481  for (const auto& [callback, registrar] :
482  plugin_registrar_destruction_callbacks_) {
483  callback(registrar);
484  }
485  FlutterEngineResult result = embedder_api_.Shutdown(engine_);
486  engine_ = nullptr;
487  return (result == kSuccess);
488  }
489  return false;
490 }

Referenced by FlutterDesktopEngineDestroy(), and flutter::FlutterWindowsView::~FlutterWindowsView().

◆ task_runner()

TaskRunner* flutter::FlutterWindowsEngine::task_runner ( )
inline

◆ text_input_plugin()

TextInputPlugin* flutter::FlutterWindowsEngine::text_input_plugin ( )
inline

Definition at line 179 of file flutter_windows_engine.h.

179 { return text_input_plugin_.get(); }

◆ texture_registrar()

FlutterWindowsTextureRegistrar* flutter::FlutterWindowsEngine::texture_registrar ( )
inline

Definition at line 153 of file flutter_windows_engine.h.

153  {
154  return texture_registrar_.get();
155  }

Referenced by FlutterDesktopRegistrarGetTextureRegistrar().

◆ UnregisterExternalTexture()

bool flutter::FlutterWindowsEngine::UnregisterExternalTexture ( int64_t  texture_id)

Definition at line 724 of file flutter_windows_engine.cc.

724  {
725  return (embedder_api_.UnregisterExternalTexture(engine_, texture_id) ==
726  kSuccess);
727 }

◆ UpdateAccessibilityFeatures()

void flutter::FlutterWindowsEngine::UpdateAccessibilityFeatures ( )

Definition at line 795 of file flutter_windows_engine.cc.

795  {
797 }

Referenced by flutter::testing::TEST_F().

◆ UpdateHighContrastMode()

void flutter::FlutterWindowsEngine::UpdateHighContrastMode ( )

Definition at line 799 of file flutter_windows_engine.cc.

799  {
800  high_contrast_enabled_ = windows_proc_table_->GetHighContrastEnabled();
801 
802  SendAccessibilityFeatures();
803  settings_plugin_->UpdateHighContrastMode(high_contrast_enabled_);
804 }

Referenced by flutter::FlutterWindowsView::OnHighContrastChanged().

◆ UpdateSemanticsEnabled()

void flutter::FlutterWindowsEngine::UpdateSemanticsEnabled ( bool  enabled)

Definition at line 764 of file flutter_windows_engine.cc.

764  {
765  if (engine_ && semantics_enabled_ != enabled) {
766  semantics_enabled_ = enabled;
767  embedder_api_.UpdateSemanticsEnabled(engine_, enabled);
768  for (auto iterator = views_.begin(); iterator != views_.end(); iterator++) {
769  iterator->second->UpdateSemanticsEnabled(enabled);
770  }
771  }
772 }

Referenced by flutter::FlutterWindowsView::OnUpdateSemanticsEnabled(), and flutter::testing::TEST_F().

◆ view()

FlutterWindowsView * flutter::FlutterWindowsEngine::view ( FlutterViewId  view_id) const

◆ window_proc_delegate_manager()

WindowProcDelegateManager* flutter::FlutterWindowsEngine::window_proc_delegate_manager ( )
inline

Definition at line 161 of file flutter_windows_engine.h.

161  {
162  return window_proc_delegate_manager_.get();
163  }

Referenced by FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(), and FlutterDesktopPluginRegistrarUnregisterTopLevelWindowProcDelegate().

◆ windows_proc_table()

std::shared_ptr<WindowsProcTable> flutter::FlutterWindowsEngine::windows_proc_table ( )
inline

Definition at line 291 of file flutter_windows_engine.h.

291  {
292  return windows_proc_table_;
293  }

Referenced by CreateViewController().

Friends And Related Function Documentation

◆ EngineModifier

friend class EngineModifier
friend

Definition at line 324 of file flutter_windows_engine.h.


The documentation for this class was generated from the following files:
flutter::kImplicitViewId
constexpr FlutterViewId kImplicitViewId
Definition: flutter_windows_engine.h:54
flutter::FlutterWindowsEngine::view
FlutterWindowsView * view(FlutterViewId view_id) const
Definition: flutter_windows_engine.cc:533
user_data
void * user_data
Definition: flutter_windows_view_unittests.cc:52
flutter::FlutterWindowsView::accessibility_bridge
std::weak_ptr< AccessibilityBridgeWindows > accessibility_bridge()
Definition: flutter_windows_view.h:220
flutter::FlutterWindowsEngine::FlutterWindowsEngine
FlutterWindowsEngine(const FlutterProjectBundle &project, std::shared_ptr< WindowsProcTable > windows_proc_table=nullptr)
Definition: flutter_windows_engine.cc:146
flutter::FlutterWindowsEngine::windows_proc_table
std::shared_ptr< WindowsProcTable > windows_proc_table()
Definition: flutter_windows_engine.h:291
flutter::FlutterWindowsEngine::Stop
virtual bool Stop()
Definition: flutter_windows_engine.cc:479
flutter::FlutterWindowsEngine::messenger
FlutterDesktopMessengerRef messenger()
Definition: flutter_windows_engine.h:143
flutter::FlutterWindowsEngine::keyboard_key_handler
KeyboardHandlerBase * keyboard_key_handler()
Definition: flutter_windows_engine.h:176
flutter::FlutterWindowsEngine::SendKeyEvent
void SendKeyEvent(const FlutterKeyEvent &event, FlutterKeyEventCallback callback, void *user_data)
Definition: flutter_windows_engine.cc:566
flutter::egl::ProcTable::Create
static std::shared_ptr< ProcTable > Create()
Definition: proc_table.cc:12
flutter::FlutterWindowsEngine::UpdateHighContrastMode
void UpdateHighContrastMode()
Definition: flutter_windows_engine.cc:799
flutter::AppLifecycleState::kResumed
@ kResumed
message
Win32Message message
Definition: keyboard_unittests.cc:137
action
int action
Definition: keyboard_key_handler_unittests.cc:116
flutter::egl::Manager::Create
static std::unique_ptr< Manager > Create(bool enable_impeller)
Definition: manager.cc:17
flutter::FlutterWindowsEngine::Run
bool Run()
Definition: flutter_windows_engine.cc:237
flutter::WindowsPlatformThreadPrioritySetter
static void WindowsPlatformThreadPrioritySetter(FlutterThreadPriority priority)
Definition: flutter_windows_engine.h:59
texture_id
uint32_t texture_id
Definition: compositor_opengl.cc:20
flutter::AccessibilityPlugin::SetUp
static void SetUp(BinaryMessenger *binary_messenger, AccessibilityPlugin *plugin)
Definition: accessibility_plugin.cc:76
flutter::FlutterWindowsEngine::GetExecutableName
std::string GetExecutableName() const
Definition: flutter_windows_engine.cc:781
callback
FlutterDesktopBinaryReply callback
Definition: flutter_windows_view_unittests.cc:51