Flutter Engine
flutter_windows.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/shell/platform/windows/public/flutter_windows.h"
6 
7 #include <io.h>
8 
9 #include <algorithm>
10 #include <cassert>
11 #include <chrono>
12 #include <cstdlib>
13 #include <filesystem>
14 #include <iostream>
15 #include <memory>
16 #include <vector>
17 
18 #include "flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h"
19 #include "flutter/shell/platform/common/incoming_message_dispatcher.h"
20 #include "flutter/shell/platform/common/path_utils.h"
21 #include "flutter/shell/platform/embedder/embedder.h"
22 #include "flutter/shell/platform/windows/flutter_project_bundle.h"
23 #include "flutter/shell/platform/windows/flutter_windows_engine.h"
24 #include "flutter/shell/platform/windows/flutter_windows_view.h"
25 #include "flutter/shell/platform/windows/window_binding_handler.h"
26 #include "flutter/shell/platform/windows/window_state.h"
27 
28 static_assert(FLUTTER_ENGINE_VERSION == 1, "");
29 
30 // Returns the engine corresponding to the given opaque API handle.
33  return reinterpret_cast<flutter::FlutterWindowsEngine*>(ref);
34 }
35 
36 // Returns the opaque API handle for the given engine instance.
39  return reinterpret_cast<FlutterDesktopEngineRef>(engine);
40 }
41 
42 // Returns the view corresponding to the given opaque API handle.
44  return reinterpret_cast<flutter::FlutterWindowsView*>(ref);
45 }
46 
47 // Returns the opaque API handle for the given view instance.
49  return reinterpret_cast<FlutterDesktopViewRef>(view);
50 }
51 
52 // Returns the texture registrar corresponding to the given opaque API handle.
55  return reinterpret_cast<flutter::FlutterWindowsTextureRegistrar*>(ref);
56 }
57 
58 // Returns the opaque API handle for the given texture registrar instance.
61  return reinterpret_cast<FlutterDesktopTextureRegistrarRef>(registrar);
62 }
63 
66  delete controller;
67 }
68 
71  return HandleForEngine(controller->view->GetEngine());
72 }
73 
76  return HandleForView(controller->view.get());
77 }
78 
81  controller->view->ForceRedraw();
82 }
83 
85  const FlutterDesktopEngineProperties* engine_properties) {
86  flutter::FlutterProjectBundle project(*engine_properties);
87  auto engine = std::make_unique<flutter::FlutterWindowsEngine>(project);
88  return HandleForEngine(engine.release());
89 }
90 
93  bool result = true;
94  if (engine->running()) {
95  result = engine->Stop();
96  }
97  delete engine;
98  return result;
99 }
100 
102  const char* entry_point) {
103  return EngineFromHandle(engine)->RunWithEntrypoint(entry_point);
104 }
105 
108 }
109 
113 }
114 
117  const char* plugin_name) {
118  // Currently, one registrar acts as the registrar for all plugins, so the
119  // name is ignored. It is part of the API to reduce churn in the future when
120  // aligning more closely with the Flutter registrar system.
121 
122  return EngineFromHandle(engine)->GetRegistrar();
123 }
124 
127  return EngineFromHandle(engine)->messenger();
128 }
129 
133  EngineFromHandle(engine)->texture_registrar());
134 }
135 
136 #ifdef WINUWP
137 ABI::Windows::ApplicationModel::Core::CoreApplicationView*
138 FlutterDesktopViewGetCoreApplicationView(FlutterDesktopViewRef view) {
139  return static_cast<
140  ABI::Windows::ApplicationModel::Core::CoreApplicationView*>(
141  winrt::get_abi(ViewFromHandle(view)->GetPlatformWindow()));
142 }
143 #endif
144 
145 #ifndef WINUWP
147  return ViewFromHandle(view)->GetPlatformWindow();
148 }
149 #endif
150 
153  return HandleForView(registrar->engine->view());
154 }
155 
157  FILE* unused;
158  if (freopen_s(&unused, "CONOUT$", "w", stdout)) {
159  _dup2(_fileno(stdout), 1);
160  }
161  if (freopen_s(&unused, "CONOUT$", "w", stderr)) {
162  _dup2(_fileno(stdout), 2);
163  }
164  std::ios::sync_with_stdio();
165 }
166 
167 // Implementations of common/ API methods.
168 
171  return registrar->engine->messenger();
172 }
173 
177  registrar->engine->SetPluginRegistrarDestructionCallback(callback);
178 }
179 
181  const char* channel,
182  const uint8_t* message,
183  const size_t message_size,
184  const FlutterDesktopBinaryReply reply,
185  void* user_data) {
186  return messenger->engine->SendPlatformMessage(channel, message, message_size,
187  reply, user_data);
188 }
189 
191  const char* channel,
192  const uint8_t* message,
193  const size_t message_size) {
194  return FlutterDesktopMessengerSendWithReply(messenger, channel, message,
195  message_size, nullptr, nullptr);
196 }
197 
199  FlutterDesktopMessengerRef messenger,
201  const uint8_t* data,
202  size_t data_length) {
203  messenger->engine->SendPlatformMessageResponse(handle, data, data_length);
204 }
205 
207  const char* channel,
209  void* user_data) {
210  messenger->engine->message_dispatcher()->SetMessageCallback(channel, callback,
211  user_data);
212 }
213 
216  return HandleForTextureRegistrar(registrar->engine->texture_registrar());
217 }
218 
220  FlutterDesktopTextureRegistrarRef texture_registrar,
221  const FlutterDesktopTextureInfo* texture_info) {
222  return TextureRegistrarFromHandle(texture_registrar)
223  ->RegisterTexture(texture_info);
224 }
225 
227  FlutterDesktopTextureRegistrarRef texture_registrar,
228  int64_t texture_id) {
229  return TextureRegistrarFromHandle(texture_registrar)
230  ->UnregisterTexture(texture_id);
231 }
232 
234  FlutterDesktopTextureRegistrarRef texture_registrar,
235  int64_t texture_id) {
236  return TextureRegistrarFromHandle(texture_registrar)
237  ->MarkTextureFrameAvailable(texture_id);
238 }
int64_t FlutterDesktopTextureRegistrarRegisterExternalTexture(FlutterDesktopTextureRegistrarRef texture_registrar, const FlutterDesktopTextureInfo *texture_info)
std::unique_ptr< flutter::IncomingMessageDispatcher > message_dispatcher
static flutter::FlutterWindowsTextureRegistrar * TextureRegistrarFromHandle(FlutterDesktopTextureRegistrarRef ref)
bool FlutterDesktopTextureRegistrarUnregisterExternalTexture(FlutterDesktopTextureRegistrarRef texture_registrar, int64_t texture_id)
std::unique_ptr< flutter::FlutterWindowsView > view
Definition: window_state.h:26
void * user_data
int64_t texture_id
GAsyncResult * result
static FlutterDesktopTextureRegistrarRef HandleForTextureRegistrar(flutter::FlutterWindowsTextureRegistrar *registrar)
std::unique_ptr< FlutterDesktopMessenger > messenger
bool RunWithEntrypoint(const char *entrypoint)
static flutter::FlutterWindowsEngine * EngineFromHandle(FlutterDesktopEngineRef ref)
FlKeyEvent FlKeyResponderAsyncCallback callback
struct FlutterDesktopTextureRegistrar * FlutterDesktopTextureRegistrarRef
void FlutterDesktopViewControllerForceRedraw(FlutterDesktopViewControllerRef controller)
FlutterDesktopPluginRegistrarRef GetRegistrar()
struct FlutterDesktopView * FlutterDesktopViewRef
void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine)
HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view)
bool FlutterDesktopTextureRegistrarMarkExternalTextureFrameAvailable(FlutterDesktopTextureRegistrarRef texture_registrar, int64_t texture_id)
FlutterDesktopMessengerRef FlutterDesktopPluginRegistrarGetMessenger(FlutterDesktopPluginRegistrarRef registrar)
int64_t RegisterTexture(const FlutterDesktopTextureInfo *texture_info)
#define FLUTTER_ENGINE_VERSION
Definition: embedder.h:63
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition: switches.h:41
void FlutterDesktopMessengerSetCallback(FlutterDesktopMessengerRef messenger, const char *channel, FlutterDesktopMessageCallback callback, void *user_data)
FlutterDesktopEngineRef FlutterDesktopEngineCreate(const FlutterDesktopEngineProperties *engine_properties)
FlutterDesktopMessengerRef FlutterDesktopEngineGetMessenger(FlutterDesktopEngineRef engine)
bool FlutterDesktopMessengerSend(FlutterDesktopMessengerRef messenger, const char *channel, const uint8_t *message, const size_t message_size)
FlutterDesktopEngineState * engine
FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(FlutterDesktopEngineRef engine, const char *plugin_name)
bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger, const char *channel, const uint8_t *message, const size_t message_size, const FlutterDesktopBinaryReply reply, void *user_data)
FlutterDesktopViewRef FlutterDesktopViewControllerGetView(FlutterDesktopViewControllerRef controller)
FlutterDesktopTextureRegistrarRef FlutterDesktopRegistrarGetTextureRegistrar(FlutterDesktopPluginRegistrarRef registrar)
void FlutterDesktopResyncOutputStreams()
FlutterDesktopMessengerRef messenger()
void FlutterDesktopPluginRegistrarSetDestructionHandler(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopOnPluginRegistrarDestroyed callback)
bool FlutterDesktopEngineDestroy(FlutterDesktopEngineRef engine_ref)
FlView * view
FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine(FlutterDesktopViewControllerRef controller)
static FlutterDesktopEngineRef HandleForEngine(flutter::FlutterWindowsEngine *engine)
static FlutterDesktopViewRef HandleForView(flutter::FlutterWindowsView *view)
static flutter::FlutterWindowsView * ViewFromHandle(FlutterDesktopViewRef ref)
void(* FlutterDesktopBinaryReply)(const uint8_t *data, size_t data_size, void *user_data)
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(FlutterDesktopPluginRegistrarRef registrar)
void(* FlutterDesktopOnPluginRegistrarDestroyed)(FlutterDesktopPluginRegistrarRef)
PlatformWindow GetPlatformWindow() const
FlutterDesktopTextureRegistrarRef FlutterDesktopEngineGetTextureRegistrar(FlutterDesktopEngineRef engine)
void FlutterDesktopViewControllerDestroy(FlutterDesktopViewControllerRef controller)
void(* FlutterDesktopMessageCallback)(FlutterDesktopMessengerRef, const FlutterDesktopMessage *, void *)
FlutterDesktopEngineState * engine
void FlutterDesktopEngineReloadPlatformBrightness(FlutterDesktopEngineRef engine)
void FlutterDesktopMessengerSendResponse(FlutterDesktopMessengerRef messenger, const FlutterDesktopMessageResponseHandle *handle, const uint8_t *data, size_t data_length)
bool FlutterDesktopEngineRun(FlutterDesktopEngineRef engine, const char *entry_point)