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)
 
virtual void RemoveView (FlutterViewId view_id)
 
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)
 
void SendViewFocusEvent (const FlutterViewFocusEvent &event)
 
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 (FlutterViewId view_id, uint64_t node_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 ()
 
void UpdateFlutterCursor (const std::string &cursor_name) const
 
void SetFlutterCursor (HCURSOR cursor) const
 

Static Public Member Functions

static FlutterWindowsEngineGetEngineForId (int64_t engine_id)
 

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)
 
virtual void OnViewFocusChangeRequest (const FlutterViewFocusChangeRequest *request)
 

Friends

class EngineModifier
 

Detailed Description

Definition at line 90 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 149 of file flutter_windows_engine.cc.

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

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

◆ ~FlutterWindowsEngine()

flutter::FlutterWindowsEngine::~FlutterWindowsEngine ( )
virtual

Definition at line 231 of file flutter_windows_engine.cc.

231  {
232  messenger_->SetEngine(nullptr);
233  Stop();
234 }

Member Function Documentation

◆ AddPluginRegistrarDestructionCallback()

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

Definition at line 690 of file flutter_windows_engine.cc.

692  {
693  plugin_registrar_destruction_callbacks_[callback] = registrar;
694 }
FlutterDesktopBinaryReply callback

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 874 of file flutter_windows_engine.cc.

877  {
878  auto keyboard_key_handler = std::make_unique<KeyboardKeyHandler>(messenger);
879  keyboard_key_handler->AddDelegate(
880  std::make_unique<KeyboardKeyEmbedderHandler>(
881  [this](const FlutterKeyEvent& event, FlutterKeyEventCallback callback,
882  void* user_data) {
883  return SendKeyEvent(event, callback, user_data);
884  },
885  get_key_state, map_vk_to_scan));
886  keyboard_key_handler->AddDelegate(
887  std::make_unique<KeyboardKeyChannelHandler>(messenger));
888  keyboard_key_handler->InitKeyboardChannel();
889  return keyboard_key_handler;
890 }
FlutterDesktopMessengerRef messenger()
KeyboardHandlerBase * keyboard_key_handler()
void SendKeyEvent(const FlutterKeyEvent &event, FlutterKeyEventCallback callback, void *user_data)

References callback, and user_data.

◆ CreateTextInputPlugin()

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

Definition at line 892 of file flutter_windows_engine.cc.

893  {
894  return std::make_unique<TextInputPlugin>(messenger, this);
895 }

◆ CreateView()

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

Definition at line 512 of file flutter_windows_engine.cc.

513  {
514  auto view_id = next_view_id_;
515  auto view = std::make_unique<FlutterWindowsView>(
516  view_id, this, std::move(window), windows_proc_table_);
517 
519  view->UpdateSemanticsEnabled(semantics_enabled_);
520 
521  next_view_id_++;
522 
523  {
524  // Add the view to the embedder. This must happen before the engine
525  // is notified the view exists and starts presenting to it.
526  std::unique_lock write_lock(views_mutex_);
527  FML_DCHECK(views_.find(view_id) == views_.end());
528  views_[view_id] = view.get();
529  }
530 
531  if (!view->IsImplicitView()) {
532  FML_DCHECK(running());
533 
534  struct Captures {
535  fml::AutoResetWaitableEvent latch;
536  bool added;
537  };
538  Captures captures = {};
539 
540  FlutterWindowMetricsEvent metrics = view->CreateWindowMetricsEvent();
541 
542  FlutterAddViewInfo info = {};
543  info.struct_size = sizeof(FlutterAddViewInfo);
544  info.view_id = view_id;
545  info.view_metrics = &metrics;
546  info.user_data = &captures;
547  info.add_view_callback = [](const FlutterAddViewResult* result) {
548  Captures* captures = reinterpret_cast<Captures*>(result->user_data);
549  captures->added = result->added;
550  captures->latch.Signal();
551  };
552 
553  FlutterEngineResult result = embedder_api_.AddView(engine_, &info);
554  if (result != kSuccess) {
555  FML_LOG(ERROR)
556  << "Starting the add view operation failed. FlutterEngineAddView "
557  "returned an unexpected result: "
558  << result << ". This indicates a bug in the Windows embedder.";
559  FML_DCHECK(false);
560  return nullptr;
561  }
562 
563  // Block the platform thread until the engine has added the view.
564  // TODO(loicsharma): This blocks the platform thread eagerly and can
565  // cause unnecessary delay in input processing. Instead, this should block
566  // lazily only when the app does an operation which needs the view.
567  // https://github.com/flutter/flutter/issues/146248
568  captures.latch.Wait();
569 
570  if (!captures.added) {
571  // Adding the view failed. Update the embedder's state to match the
572  // engine's state. This is unexpected and indicates a bug in the Windows
573  // embedder.
574  FML_LOG(ERROR) << "FlutterEngineAddView failed to add view";
575  std::unique_lock write_lock(views_mutex_);
576  views_.erase(view_id);
577  return nullptr;
578  }
579  }
580 
581  return std::move(view);
582 }
FlutterWindowsView * view(FlutterViewId view_id) const
virtual void UpdateSemanticsEnabled(bool enabled)
FlutterWindowMetricsEvent CreateWindowMetricsEvent() const

Referenced by CreateViewController(), and flutter::testing::TEST_F().

◆ DispatchSemanticsAction()

bool flutter::FlutterWindowsEngine::DispatchSemanticsAction ( FlutterViewId  view_id,
uint64_t  node_id,
FlutterSemanticsAction  action,
fml::MallocMapping  data 
)

Definition at line 933 of file flutter_windows_engine.cc.

937  {
938  FlutterSendSemanticsActionInfo info{
939  .struct_size = sizeof(FlutterSendSemanticsActionInfo),
940  .view_id = view_id,
941  .node_id = target,
942  .action = action,
943  .data = data.GetMapping(),
944  .data_length = data.GetSize(),
945  };
946  return (embedder_api_.SendSemanticsAction(engine_, &info));
947 }

References action.

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

◆ egl_manager()

◆ GetEngineForId()

FlutterWindowsEngine * flutter::FlutterWindowsEngine::GetEngineForId ( int64_t  engine_id)
static

Definition at line 236 of file flutter_windows_engine.cc.

236  {
237  return reinterpret_cast<FlutterWindowsEngine*>(engine_id);
238 }

Referenced by FlutterDesktopEngineForId().

◆ GetExecutableName()

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

Definition at line 966 of file flutter_windows_engine.cc.

966  {
967  std::pair<bool, std::string> result = fml::paths::GetExecutablePath();
968  if (result.first) {
969  const std::string& executable_path = result.second;
970  size_t last_separator = executable_path.find_last_of("/\\");
971  if (last_separator == std::string::npos ||
972  last_separator == executable_path.size() - 1) {
973  return executable_path;
974  }
975  return executable_path.substr(last_separator + 1);
976  }
977  return "Flutter";
978 }

◆ GetRegistrar()

FlutterDesktopPluginRegistrarRef flutter::FlutterWindowsEngine::GetRegistrar ( )

Definition at line 686 of file flutter_windows_engine.cc.

686  {
687  return plugin_registrar_.get();
688 }

Referenced by FlutterDesktopEngineGetPluginRegistrar().

◆ HandlePlatformMessage()

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

Definition at line 765 of file flutter_windows_engine.cc.

766  {
767  if (engine_message->struct_size != sizeof(FlutterPlatformMessage)) {
768  FML_LOG(ERROR) << "Invalid message size received. Expected: "
769  << sizeof(FlutterPlatformMessage) << " but received "
770  << engine_message->struct_size;
771  return;
772  }
773 
774  auto message = ConvertToDesktopMessage(*engine_message);
775 
776  message_dispatcher_->HandleMessage(message, [this] {}, [this] {});
777 }
Win32Message message

References message.

Referenced by Run().

◆ high_contrast_enabled()

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

Definition at line 258 of file flutter_windows_engine.h.

258 { return high_contrast_enabled_; }

◆ keyboard_key_handler()

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

Definition at line 191 of file flutter_windows_engine.h.

191  {
192  return keyboard_key_handler_.get();
193  }

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

◆ lifecycle_manager()

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

Definition at line 303 of file flutter_windows_engine.h.

303  {
304  return lifecycle_manager_.get();
305  }

◆ MarkExternalTextureFrameAvailable()

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

Definition at line 907 of file flutter_windows_engine.cc.

908  {
909  return (embedder_api_.MarkExternalTextureFrameAvailable(
910  engine_, texture_id) == kSuccess);
911 }
uint32_t texture_id

◆ message_dispatcher()

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

Definition at line 157 of file flutter_windows_engine.h.

157  {
158  return message_dispatcher_.get();
159  }

Referenced by FlutterDesktopMessengerSetCallback().

◆ messenger()

FlutterDesktopMessengerRef flutter::FlutterWindowsEngine::messenger ( )
inline

Definition at line 155 of file flutter_windows_engine.h.

155 { return messenger_->ToRef(); }

Referenced by FlutterDesktopEngineGetMessenger(), and FlutterDesktopPluginRegistrarGetMessenger().

◆ messenger_wrapper()

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

Definition at line 163 of file flutter_windows_engine.h.

163 { return messenger_wrapper_.get(); }

◆ OnChannelUpdate()

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

Definition at line 1051 of file flutter_windows_engine.cc.

1051  {
1052  if (name == "flutter/platform" && listening) {
1053  lifecycle_manager_->BeginProcessingExit();
1054  } else if (name == "flutter/lifecycle" && listening) {
1055  lifecycle_manager_->BeginProcessingLifecycle();
1056  }
1057 }

◆ OnDwmCompositionChanged()

void flutter::FlutterWindowsEngine::OnDwmCompositionChanged ( )

Definition at line 1017 of file flutter_windows_engine.cc.

1017  {
1018  std::shared_lock read_lock(views_mutex_);
1019 
1020  for (auto iterator = views_.begin(); iterator != views_.end(); iterator++) {
1021  iterator->second->OnDwmCompositionChanged();
1022  }
1023 }

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

◆ OnPreEngineRestart()

void flutter::FlutterWindowsEngine::OnPreEngineRestart ( )
protected

Definition at line 961 of file flutter_windows_engine.cc.

961  {
962  // Reset the keyboard's state on hot restart.
963  InitializeKeyboard();
964 }

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 1010 of file flutter_windows_engine.cc.

1013  {
1014  lifecycle_manager_->Quit(hwnd, wparam, lparam, exit_code);
1015 }

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

◆ OnViewFocusChangeRequest()

void flutter::FlutterWindowsEngine::OnViewFocusChangeRequest ( const FlutterViewFocusChangeRequest *  request)
protectedvirtual

Definition at line 1059 of file flutter_windows_engine.cc.

1060  {
1061  std::shared_lock read_lock(views_mutex_);
1062 
1063  auto iterator = views_.find(request->view_id);
1064  if (iterator == views_.end()) {
1065  return;
1066  }
1067 
1068  FlutterWindowsView* view = iterator->second;
1069  view->Focus();
1070 }

References flutter::FlutterWindowsView::Focus().

Referenced by Run().

◆ OnVsync()

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

Definition at line 646 of file flutter_windows_engine.cc.

646  {
647  std::chrono::nanoseconds current_time =
648  std::chrono::nanoseconds(embedder_api_.GetCurrentTime());
649  std::chrono::nanoseconds frame_interval = FrameInterval();
650  auto next = SnapToNextTick(current_time, start_time_, frame_interval);
651  embedder_api_.OnVsync(engine_, baton, next.count(),
652  (next + frame_interval).count());
653 }

Referenced by Run().

◆ OnWindowStateEvent()

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

Definition at line 1025 of file flutter_windows_engine.cc.

1026  {
1027  lifecycle_manager_->OnWindowStateEvent(hwnd, event);
1028 }

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

◆ PostRasterThreadTask()

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

Definition at line 913 of file flutter_windows_engine.cc.

913  {
914  struct Captures {
915  fml::closure callback;
916  };
917  auto captures = new Captures();
918  captures->callback = std::move(callback);
919  if (embedder_api_.PostRenderThreadTask(
920  engine_,
921  [](void* opaque) {
922  auto captures = reinterpret_cast<Captures*>(opaque);
923  captures->callback();
924  delete captures;
925  },
926  captures) == kSuccess) {
927  return true;
928  }
929  delete captures;
930  return false;
931 }

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 1030 of file flutter_windows_engine.cc.

1034  {
1035  if (lifecycle_manager_) {
1036  return lifecycle_manager_->ExternalWindowMessage(hwnd, message, wparam,
1037  lparam);
1038  }
1039  return std::nullopt;
1040 }

References message.

Referenced by FlutterDesktopEngineProcessExternalWindowMessage().

◆ RegisterExternalTexture()

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

Definition at line 897 of file flutter_windows_engine.cc.

897  {
898  return (embedder_api_.RegisterExternalTexture(engine_, texture_id) ==
899  kSuccess);
900 }

◆ ReloadSystemFonts()

void flutter::FlutterWindowsEngine::ReloadSystemFonts ( )

Definition at line 779 of file flutter_windows_engine.cc.

779  {
780  embedder_api_.ReloadSystemFonts(engine_);
781 }

Referenced by FlutterDesktopEngineReloadSystemFonts().

◆ RemoveView()

void flutter::FlutterWindowsEngine::RemoveView ( FlutterViewId  view_id)
virtual

Definition at line 584 of file flutter_windows_engine.cc.

584  {
585  FML_DCHECK(running());
586 
587  // Notify the engine to stop rendering to the view if it isn't the implicit
588  // view. The engine and framework assume the implicit view always exists and
589  // can continue presenting.
590  if (view_id != kImplicitViewId) {
591  struct Captures {
592  fml::AutoResetWaitableEvent latch;
593  bool removed;
594  };
595  Captures captures = {};
596 
597  FlutterRemoveViewInfo info = {};
598  info.struct_size = sizeof(FlutterRemoveViewInfo);
599  info.view_id = view_id;
600  info.user_data = &captures;
601  info.remove_view_callback = [](const FlutterRemoveViewResult* result) {
602  // This is invoked on an engine thread. If
603  // |FlutterRemoveViewResult.removed| is `true`, the engine guarantees the
604  // view won't be presented.
605  Captures* captures = reinterpret_cast<Captures*>(result->user_data);
606  captures->removed = result->removed;
607  captures->latch.Signal();
608  };
609 
610  FlutterEngineResult result = embedder_api_.RemoveView(engine_, &info);
611  if (result != kSuccess) {
612  FML_LOG(ERROR) << "Starting the remove view operation failed. "
613  "FlutterEngineRemoveView "
614  "returned an unexpected result: "
615  << result
616  << ". This indicates a bug in the Windows embedder.";
617  FML_DCHECK(false);
618  return;
619  }
620 
621  // Block the platform thread until the engine has removed the view.
622  // TODO(loicsharma): This blocks the platform thread eagerly and can
623  // cause unnecessary delay in input processing. Instead, this should block
624  // lazily only when an operation needs the view.
625  // https://github.com/flutter/flutter/issues/146248
626  captures.latch.Wait();
627 
628  if (!captures.removed) {
629  // Removing the view failed. This is unexpected and indicates a bug in the
630  // Windows embedder.
631  FML_LOG(ERROR) << "FlutterEngineRemoveView failed to remove view";
632  return;
633  }
634  }
635 
636  {
637  // The engine no longer presents to the view. Remove the view from the
638  // embedder.
639  std::unique_lock write_lock(views_mutex_);
640 
641  FML_DCHECK(views_.find(view_id) != views_.end());
642  views_.erase(view_id);
643  }
644 }
constexpr FlutterViewId kImplicitViewId

References flutter::kImplicitViewId.

◆ RequestApplicationQuit()

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

Definition at line 1003 of file flutter_windows_engine.cc.

1006  {
1007  platform_handler_->RequestAppExit(hwnd, wparam, lparam, exit_type, 0);
1008 }

◆ Run() [1/2]

bool flutter::FlutterWindowsEngine::Run ( )

Definition at line 245 of file flutter_windows_engine.cc.

245  {
246  return Run("");
247 }

Referenced by FlutterDesktopEngineRun().

◆ Run() [2/2]

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

Definition at line 249 of file flutter_windows_engine.cc.

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

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

◆ running()

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

Definition at line 123 of file flutter_windows_engine.h.

123 { return engine_ != nullptr; }

Referenced by FlutterDesktopEngineDestroy().

◆ ScheduleFrame()

void flutter::FlutterWindowsEngine::ScheduleFrame ( )

Definition at line 783 of file flutter_windows_engine.cc.

783  {
784  embedder_api_.ScheduleFrame(engine_);
785 }

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

◆ semantics_enabled()

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

Definition at line 249 of file flutter_windows_engine.h.

249 { return semantics_enabled_; }

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

◆ SendKeyEvent()

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

Definition at line 709 of file flutter_windows_engine.cc.

711  {
712  if (engine_) {
713  embedder_api_.SendKeyEvent(engine_, &event, callback, user_data);
714  }
715 }

◆ 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 724 of file flutter_windows_engine.cc.

729  {
730  FlutterPlatformMessageResponseHandle* response_handle = nullptr;
731  if (reply != nullptr && user_data != nullptr) {
732  FlutterEngineResult result =
733  embedder_api_.PlatformMessageCreateResponseHandle(
734  engine_, reply, user_data, &response_handle);
735  if (result != kSuccess) {
736  FML_LOG(ERROR) << "Failed to create response handle";
737  return false;
738  }
739  }
740 
741  FlutterPlatformMessage platform_message = {
742  sizeof(FlutterPlatformMessage),
743  channel,
744  message,
745  message_size,
746  response_handle,
747  };
748 
749  FlutterEngineResult message_result =
750  embedder_api_.SendPlatformMessage(engine_, &platform_message);
751  if (response_handle != nullptr) {
752  embedder_api_.PlatformMessageReleaseResponseHandle(engine_,
753  response_handle);
754  }
755  return message_result == kSuccess;
756 }

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 758 of file flutter_windows_engine.cc.

761  {
762  embedder_api_.SendPlatformMessageResponse(engine_, handle, data, data_length);
763 }

Referenced by FlutterDesktopMessengerSendResponse().

◆ SendPointerEvent()

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

Definition at line 703 of file flutter_windows_engine.cc.

703  {
704  if (engine_) {
705  embedder_api_.SendPointerEvent(engine_, &event, 1);
706  }
707 }

◆ SendViewFocusEvent()

void flutter::FlutterWindowsEngine::SendViewFocusEvent ( const FlutterViewFocusEvent &  event)

Definition at line 717 of file flutter_windows_engine.cc.

718  {
719  if (engine_) {
720  embedder_api_.SendViewFocusEvent(engine_, &event);
721  }
722 }

◆ SendWindowMetricsEvent()

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

Definition at line 696 of file flutter_windows_engine.cc.

697  {
698  if (engine_) {
699  embedder_api_.SendWindowMetricsEvent(engine_, &event);
700  }
701 }

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

◆ SetFlutterCursor()

void flutter::FlutterWindowsEngine::SetFlutterCursor ( HCURSOR  cursor) const

Definition at line 1047 of file flutter_windows_engine.cc.

1047  {
1048  windows_proc_table_->SetCursor(cursor);
1049 }

◆ SetNextFrameCallback()

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

Definition at line 787 of file flutter_windows_engine.cc.

787  {
788  next_frame_callback_ = std::move(callback);
789 
790  embedder_api_.SetNextFrameCallback(
791  engine_,
792  [](void* user_data) {
793  // Embedder callback runs on raster thread. Switch back to platform
794  // thread.
795  FlutterWindowsEngine* self =
796  static_cast<FlutterWindowsEngine*>(user_data);
797 
798  self->task_runner_->PostTask(std::move(self->next_frame_callback_));
799  },
800  this);
801 }

Referenced by FlutterDesktopEngineSetNextFrameCallback().

◆ SetRootIsolateCreateCallback()

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

Definition at line 269 of file flutter_windows_engine.h.

269  {
270  root_isolate_create_callback_ = callback;
271  }

References callback.

◆ SetSwitches()

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

Definition at line 240 of file flutter_windows_engine.cc.

241  {
242  project_->SetSwitches(switches);
243 }

References SetSwitches().

Referenced by SetSwitches().

◆ Stop()

bool flutter::FlutterWindowsEngine::Stop ( )
virtual

Definition at line 499 of file flutter_windows_engine.cc.

499  {
500  if (engine_) {
501  for (const auto& [callback, registrar] :
502  plugin_registrar_destruction_callbacks_) {
503  callback(registrar);
504  }
505  FlutterEngineResult result = embedder_api_.Shutdown(engine_);
506  engine_ = nullptr;
507  return (result == kSuccess);
508  }
509  return false;
510 }

Referenced by FlutterDesktopEngineDestroy().

◆ task_runner()

◆ text_input_plugin()

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

Definition at line 194 of file flutter_windows_engine.h.

194 { return text_input_plugin_.get(); }

◆ texture_registrar()

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

Definition at line 165 of file flutter_windows_engine.h.

165  {
166  return texture_registrar_.get();
167  }

Referenced by FlutterDesktopRegistrarGetTextureRegistrar().

◆ UnregisterExternalTexture()

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

Definition at line 902 of file flutter_windows_engine.cc.

902  {
903  return (embedder_api_.UnregisterExternalTexture(engine_, texture_id) ==
904  kSuccess);
905 }

◆ UpdateAccessibilityFeatures()

void flutter::FlutterWindowsEngine::UpdateAccessibilityFeatures ( )

Definition at line 980 of file flutter_windows_engine.cc.

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

◆ UpdateFlutterCursor()

void flutter::FlutterWindowsEngine::UpdateFlutterCursor ( const std::string &  cursor_name) const

Definition at line 1042 of file flutter_windows_engine.cc.

1043  {
1044  SetFlutterCursor(GetCursorByName(cursor_name));
1045 }
void SetFlutterCursor(HCURSOR cursor) const

◆ UpdateHighContrastMode()

void flutter::FlutterWindowsEngine::UpdateHighContrastMode ( )

Definition at line 984 of file flutter_windows_engine.cc.

984  {
985  high_contrast_enabled_ = windows_proc_table_->GetHighContrastEnabled();
986 
987  SendAccessibilityFeatures();
988  settings_plugin_->UpdateHighContrastMode(high_contrast_enabled_);
989 }

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

◆ UpdateSemanticsEnabled()

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

Definition at line 949 of file flutter_windows_engine.cc.

949  {
950  if (engine_ && semantics_enabled_ != enabled) {
951  std::shared_lock read_lock(views_mutex_);
952 
953  semantics_enabled_ = enabled;
954  embedder_api_.UpdateSemanticsEnabled(engine_, enabled);
955  for (auto iterator = views_.begin(); iterator != views_.end(); iterator++) {
956  iterator->second->UpdateSemanticsEnabled(enabled);
957  }
958  }
959 }

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

◆ view()

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

Definition at line 674 of file flutter_windows_engine.cc.

674  {
675  std::shared_lock read_lock(views_mutex_);
676 
677  auto iterator = views_.find(view_id);
678  if (iterator == views_.end()) {
679  return nullptr;
680  }
681 
682  return iterator->second;
683 }

Referenced by flutter::AccessibilityPlugin::Announce(), FlutterDesktopPluginRegistrarGetView(), FlutterDesktopPluginRegistrarGetViewById(), flutter::PlatformHandler::GetHasStrings(), flutter::PlatformHandler::GetPlainText(), Run(), and flutter::PlatformHandler::SetPlainText().

◆ window_proc_delegate_manager()

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

Definition at line 173 of file flutter_windows_engine.h.

173  {
174  return window_proc_delegate_manager_.get();
175  }

Referenced by FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(), and FlutterDesktopPluginRegistrarUnregisterTopLevelWindowProcDelegate().

◆ windows_proc_table()

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

Definition at line 307 of file flutter_windows_engine.h.

307  {
308  return windows_proc_table_;
309  }

Referenced by CreateViewController().

Friends And Related Function Documentation

◆ EngineModifier

friend class EngineModifier
friend

Definition at line 350 of file flutter_windows_engine.h.


The documentation for this class was generated from the following files: