Flutter Engine
fl_engine.cc File Reference
#include "flutter/shell/platform/linux/public/flutter_linux/fl_engine.h"
#include <gmodule.h>
#include <cstring>
#include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
#include "flutter/shell/platform/linux/fl_dart_project_private.h"
#include "flutter/shell/platform/linux/fl_engine_private.h"
#include "flutter/shell/platform/linux/fl_plugin_registrar_private.h"
#include "flutter/shell/platform/linux/fl_renderer.h"
#include "flutter/shell/platform/linux/fl_renderer_headless.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_plugin_registry.h"

Go to the source code of this file.

Classes

struct  _FlEngine
 

Functions

static void fl_engine_plugin_registry_iface_init (FlPluginRegistryInterface *iface)
 
 G_DEFINE_TYPE_WITH_CODE (FlEngine, fl_engine, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(fl_plugin_registry_get_type(), fl_engine_plugin_registry_iface_init)) typedef struct
 
static void parse_locale (const gchar *locale, gchar **language, gchar **territory, gchar **codeset, gchar **modifier)
 
static void setup_locales (FlEngine *self)
 
static gboolean flutter_source_dispatch (GSource *source, GSourceFunc callback, gpointer user_data)
 
static void engine_weak_notify_cb (gpointer user_data, GObject *object)
 
static void flutter_source_finalize (GSource *source)
 
static void * fl_engine_gl_proc_resolver (void *user_data, const char *name)
 
static bool fl_engine_gl_make_current (void *user_data)
 
static bool fl_engine_gl_clear_current (void *user_data)
 
static uint32_t fl_engine_gl_get_fbo (void *user_data)
 
static bool fl_engine_gl_present (void *user_data)
 
static bool fl_engine_gl_make_resource_current (void *user_data)
 
static bool fl_engine_runs_task_on_current_thread (void *user_data)
 
static void fl_engine_post_task (FlutterTask task, uint64_t target_time_nanos, void *user_data)
 
static void fl_engine_platform_message_cb (const FlutterPlatformMessage *message, void *user_data)
 
static void fl_engine_platform_message_response_cb (const uint8_t *data, size_t data_length, void *user_data)
 
static FlPluginRegistrar * fl_engine_get_registrar_for_plugin (FlPluginRegistry *registry, const gchar *name)
 
static void fl_engine_dispose (GObject *object)
 
static void fl_engine_class_init (FlEngineClass *klass)
 
static void fl_engine_init (FlEngine *self)
 
FlEngine * fl_engine_new (FlDartProject *project, FlRenderer *renderer)
 
G_MODULE_EXPORT FlEngine * fl_engine_new_headless (FlDartProject *project)
 
gboolean fl_engine_start (FlEngine *self, GError **error)
 
void fl_engine_set_platform_message_handler (FlEngine *self, FlEnginePlatformMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
 
gboolean fl_engine_send_platform_message_response (FlEngine *self, const FlutterPlatformMessageResponseHandle *handle, GBytes *response, GError **error)
 
void fl_engine_send_platform_message (FlEngine *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
 
GBytes * fl_engine_send_platform_message_finish (FlEngine *self, GAsyncResult *result, GError **error)
 
void fl_engine_send_window_metrics_event (FlEngine *self, size_t width, size_t height, double pixel_ratio)
 
void fl_engine_send_mouse_pointer_event (FlEngine *self, FlutterPointerPhase phase, size_t timestamp, double x, double y, double scroll_delta_x, double scroll_delta_y, int64_t buttons)
 
G_MODULE_EXPORT FlBinaryMessenger * fl_engine_get_binary_messenger (FlEngine *self)
 

Variables

static constexpr int kMicrosecondsPerNanosecond = 1000
 
static constexpr size_t kPlatformTaskRunnerIdentifier = 1
 
 FlutterSource
 
static GSourceFuncs flutter_source_funcs
 

Function Documentation

◆ engine_weak_notify_cb()

static void engine_weak_notify_cb ( gpointer  user_data,
GObject *  object 
)
static

Definition at line 155 of file fl_engine.cc.

References FlutterSource, and user_data.

Referenced by fl_engine_post_task(), and flutter_source_finalize().

155  {
156  FlutterSource* source = reinterpret_cast<FlutterSource*>(user_data);
157  source->engine = nullptr;
158  g_source_destroy(reinterpret_cast<GSource*>(source));
159 }
G_BEGIN_DECLS FlValue gpointer user_data
FlutterSource
Definition: fl_engine.cc:59

◆ fl_engine_class_init()

static void fl_engine_class_init ( FlEngineClass *  klass)
static

Definition at line 327 of file fl_engine.cc.

References fl_engine_dispose().

327  {
328  G_OBJECT_CLASS(klass)->dispose = fl_engine_dispose;
329 }
static void fl_engine_dispose(GObject *object)
Definition: fl_engine.cc:300

◆ fl_engine_dispose()

static void fl_engine_dispose ( GObject *  object)
static

Definition at line 300 of file fl_engine.cc.

References FlutterEngineCollectAOTData(), and FlutterEngineShutdown().

Referenced by fl_engine_class_init().

300  {
301  FlEngine* self = FL_ENGINE(object);
302 
303  if (self->engine != nullptr) {
304  FlutterEngineShutdown(self->engine);
305  self->engine = nullptr;
306  }
307 
308  if (self->aot_data != nullptr) {
309  FlutterEngineCollectAOTData(self->aot_data);
310  self->aot_data = nullptr;
311  }
312 
313  g_clear_object(&self->project);
314  g_clear_object(&self->renderer);
315  g_clear_object(&self->binary_messenger);
316 
317  if (self->platform_message_handler_destroy_notify) {
318  self->platform_message_handler_destroy_notify(
319  self->platform_message_handler_data);
320  }
321  self->platform_message_handler_data = nullptr;
322  self->platform_message_handler_destroy_notify = nullptr;
323 
324  G_OBJECT_CLASS(fl_engine_parent_class)->dispose(object);
325 }
FlutterEngineResult FlutterEngineShutdown(FLUTTER_API_SYMBOL(FlutterEngine) engine)
Shuts down a Flutter engine instance. The engine handle is no longer valid for any calls in the embed...
Definition: embedder.cc:1218
FlutterEngineResult FlutterEngineCollectAOTData(FlutterEngineAOTData data)
Collects the AOT data.
Definition: embedder.cc:651

◆ fl_engine_get_binary_messenger()

G_MODULE_EXPORT FlBinaryMessenger* fl_engine_get_binary_messenger ( FlEngine *  engine)

fl_engine_get_binary_messenger: : an #FlEngine.

Gets the messenger to communicate with this engine.

Returns: an #FlBinaryMessenger.

Definition at line 597 of file fl_engine.cc.

Referenced by fl_view_constructed(), and fl_view_get_registrar_for_plugin().

598  {
599  g_return_val_if_fail(FL_IS_ENGINE(self), nullptr);
600  return self->binary_messenger;
601 }

◆ fl_engine_get_registrar_for_plugin()

static FlPluginRegistrar* fl_engine_get_registrar_for_plugin ( FlPluginRegistry *  registry,
const gchar *  name 
)
static

Definition at line 287 of file fl_engine.cc.

References fl_plugin_registrar_new().

Referenced by fl_engine_plugin_registry_iface_init().

289  {
290  FlEngine* self = FL_ENGINE(registry);
291 
292  return fl_plugin_registrar_new(nullptr, self->binary_messenger);
293 }
FlPluginRegistrar * fl_plugin_registrar_new(FlView *view, FlBinaryMessenger *messenger)

◆ fl_engine_gl_clear_current()

static bool fl_engine_gl_clear_current ( void *  user_data)
static

Definition at line 198 of file fl_engine.cc.

References error, fl_renderer_clear_current(), and user_data.

Referenced by fl_engine_start().

198  {
199  FlEngine* self = static_cast<FlEngine*>(user_data);
200  g_autoptr(GError) error = nullptr;
201  gboolean result = fl_renderer_clear_current(self->renderer, &error);
202  if (!result) {
203  g_warning("%s", error->message);
204  }
205  return result;
206 }
gboolean fl_renderer_clear_current(FlRenderer *self, GError **error)
Definition: fl_renderer.cc:261
FlMethodResponse GError ** error
G_BEGIN_DECLS FlValue gpointer user_data

◆ fl_engine_gl_get_fbo()

static uint32_t fl_engine_gl_get_fbo ( void *  user_data)
static

Definition at line 208 of file fl_engine.cc.

References fl_renderer_get_fbo(), and user_data.

Referenced by fl_engine_start().

208  {
209  FlEngine* self = static_cast<FlEngine*>(user_data);
210  return fl_renderer_get_fbo(self->renderer);
211 }
G_BEGIN_DECLS FlValue gpointer user_data
guint32 fl_renderer_get_fbo(FlRenderer *self)
Definition: fl_renderer.cc:276

◆ fl_engine_gl_make_current()

static bool fl_engine_gl_make_current ( void *  user_data)
static

Definition at line 188 of file fl_engine.cc.

References error, fl_renderer_make_current(), and user_data.

Referenced by fl_engine_start().

188  {
189  FlEngine* self = static_cast<FlEngine*>(user_data);
190  g_autoptr(GError) error = nullptr;
191  gboolean result = fl_renderer_make_current(self->renderer, &error);
192  if (!result) {
193  g_warning("%s", error->message);
194  }
195  return result;
196 }
gboolean fl_renderer_make_current(FlRenderer *self, GError **error)
Definition: fl_renderer.cc:216
FlMethodResponse GError ** error
G_BEGIN_DECLS FlValue gpointer user_data

◆ fl_engine_gl_make_resource_current()

static bool fl_engine_gl_make_resource_current ( void *  user_data)
static

Definition at line 223 of file fl_engine.cc.

References error, fl_renderer_make_resource_current(), and user_data.

Referenced by fl_engine_start().

223  {
224  FlEngine* self = static_cast<FlEngine*>(user_data);
225  g_autoptr(GError) error = nullptr;
226  gboolean result = fl_renderer_make_resource_current(self->renderer, &error);
227  if (!result) {
228  g_warning("%s", error->message);
229  }
230  return result;
231 }
FlMethodResponse GError ** error
gboolean fl_renderer_make_resource_current(FlRenderer *self, GError **error)
Definition: fl_renderer.cc:238
G_BEGIN_DECLS FlValue gpointer user_data

◆ fl_engine_gl_present()

static bool fl_engine_gl_present ( void *  user_data)
static

Definition at line 213 of file fl_engine.cc.

References error, fl_renderer_present(), and user_data.

Referenced by fl_engine_start().

213  {
214  FlEngine* self = static_cast<FlEngine*>(user_data);
215  g_autoptr(GError) error = nullptr;
216  gboolean result = fl_renderer_present(self->renderer, &error);
217  if (!result) {
218  g_warning("%s", error->message);
219  }
220  return result;
221 }
FlMethodResponse GError ** error
G_BEGIN_DECLS FlValue gpointer user_data
gboolean fl_renderer_present(FlRenderer *self, GError **error)
Definition: fl_renderer.cc:281

◆ fl_engine_gl_proc_resolver()

static void* fl_engine_gl_proc_resolver ( void *  user_data,
const char *  name 
)
static

Definition at line 183 of file fl_engine.cc.

References fl_renderer_get_proc_address(), and user_data.

Referenced by fl_engine_start().

183  {
184  FlEngine* self = static_cast<FlEngine*>(user_data);
185  return fl_renderer_get_proc_address(self->renderer, name);
186 }
void * fl_renderer_get_proc_address(FlRenderer *self, const char *name)
Definition: fl_renderer.cc:212
G_BEGIN_DECLS FlValue gpointer user_data
const char * name
Definition: fuchsia.cc:50

◆ fl_engine_init()

static void fl_engine_init ( FlEngine *  self)
static

Definition at line 331 of file fl_engine.cc.

References fl_binary_messenger_new().

331  {
332  self->thread = g_thread_self();
333 
334  self->binary_messenger = fl_binary_messenger_new(self);
335 }
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)

◆ fl_engine_new()

FlEngine* fl_engine_new ( FlDartProject *  project,
FlRenderer *  renderer 
)

fl_engine_new: : an #FlDartProject. : an #FlRenderer.

Creates new Flutter engine.

Returns: a new #FlEngine.

Definition at line 337 of file fl_engine.cc.

Referenced by fl_engine_new_headless(), fl_view_constructed(), and make_mock_engine().

337  {
338  g_return_val_if_fail(FL_IS_DART_PROJECT(project), nullptr);
339  g_return_val_if_fail(FL_IS_RENDERER(renderer), nullptr);
340 
341  FlEngine* self = FL_ENGINE(g_object_new(fl_engine_get_type(), nullptr));
342  self->project = FL_DART_PROJECT(g_object_ref(project));
343  self->renderer = FL_RENDERER(g_object_ref(renderer));
344  return self;
345 }

◆ fl_engine_new_headless()

G_MODULE_EXPORT FlEngine* fl_engine_new_headless ( FlDartProject *  project)

FlEngine:

#FlEngine is an object that contains a running Flutter engine. fl_engine_new_headless: : an #FlDartProject.

Creates new Flutter engine running in headless mode.

Returns: a new #FlEngine.

Definition at line 347 of file fl_engine.cc.

References fl_engine_new(), fl_renderer_headless_new(), and _FlEngine::renderer.

347  {
348  g_autoptr(FlRendererHeadless) renderer = fl_renderer_headless_new();
349  return fl_engine_new(project, FL_RENDERER(renderer));
350 }
FlRendererHeadless * fl_renderer_headless_new()
FlEngine * fl_engine_new(FlDartProject *project, FlRenderer *renderer)
Definition: fl_engine.cc:337

◆ fl_engine_platform_message_cb()

static void fl_engine_platform_message_cb ( const FlutterPlatformMessage message,
void *  user_data 
)
static

Definition at line 257 of file fl_engine.cc.

References FlutterPlatformMessage::channel, fl_engine_send_platform_message_response(), FlutterPlatformMessage::message, FlutterPlatformMessage::message_size, and FlutterPlatformMessage::response_handle.

Referenced by fl_engine_start().

258  {
259  FlEngine* self = FL_ENGINE(user_data);
260 
261  gboolean handled = FALSE;
262  if (self->platform_message_handler != nullptr) {
263  g_autoptr(GBytes) data =
264  g_bytes_new(message->message, message->message_size);
265  handled = self->platform_message_handler(
266  self, message->channel, data, message->response_handle,
267  self->platform_message_handler_data);
268  }
269 
270  if (!handled) {
272  nullptr, nullptr);
273  }
274 }
const char * channel
Definition: embedder.h:567
const FlutterPlatformMessageResponseHandle * response_handle
Definition: embedder.h:576
G_BEGIN_DECLS FlValue gpointer user_data
const uint8_t * message
Definition: embedder.h:568
gboolean fl_engine_send_platform_message_response(FlEngine *self, const FlutterPlatformMessageResponseHandle *handle, GBytes *response, GError **error)
Definition: fl_engine.cc:451

◆ fl_engine_platform_message_response_cb()

static void fl_engine_platform_message_response_cb ( const uint8_t *  data,
size_t  data_length,
void *  user_data 
)
static

Definition at line 278 of file fl_engine.cc.

Referenced by fl_engine_send_platform_message().

280  {
281  g_autoptr(GTask) task = G_TASK(user_data);
282  g_task_return_pointer(task, g_bytes_new(data, data_length),
283  reinterpret_cast<GDestroyNotify>(g_bytes_unref));
284 }
G_BEGIN_DECLS FlValue gpointer user_data

◆ fl_engine_plugin_registry_iface_init()

static void fl_engine_plugin_registry_iface_init ( FlPluginRegistryInterface *  iface)
static

Definition at line 295 of file fl_engine.cc.

References fl_engine_get_registrar_for_plugin().

296  {
297  iface->get_registrar_for_plugin = fl_engine_get_registrar_for_plugin;
298 }
static FlPluginRegistrar * fl_engine_get_registrar_for_plugin(FlPluginRegistry *registry, const gchar *name)
Definition: fl_engine.cc:287

◆ fl_engine_post_task()

static void fl_engine_post_task ( FlutterTask  task,
uint64_t  target_time_nanos,
void *  user_data 
)
static

Definition at line 240 of file fl_engine.cc.

References engine_weak_notify_cb(), FlutterSource, kMicrosecondsPerNanosecond, and user_data.

Referenced by fl_engine_start().

242  {
243  FlEngine* self = static_cast<FlEngine*>(user_data);
244 
245  g_autoptr(GSource) source =
246  g_source_new(&flutter_source_funcs, sizeof(FlutterSource));
247  FlutterSource* fl_source = reinterpret_cast<FlutterSource*>(source);
248  fl_source->engine = self;
249  g_object_weak_ref(G_OBJECT(self), engine_weak_notify_cb, fl_source);
250  fl_source->task = task;
251  g_source_set_ready_time(source,
252  target_time_nanos / kMicrosecondsPerNanosecond);
253  g_source_attach(source, nullptr);
254 }
static void engine_weak_notify_cb(gpointer user_data, GObject *object)
Definition: fl_engine.cc:155
G_BEGIN_DECLS FlValue gpointer user_data
static constexpr int kMicrosecondsPerNanosecond
Definition: fl_engine.cc:19
FlutterSource
Definition: fl_engine.cc:59
static GSourceFuncs flutter_source_funcs
Definition: fl_engine.cc:172

◆ fl_engine_runs_task_on_current_thread()

static bool fl_engine_runs_task_on_current_thread ( void *  user_data)
static

Definition at line 234 of file fl_engine.cc.

References user_data.

Referenced by fl_engine_start().

234  {
235  FlEngine* self = static_cast<FlEngine*>(user_data);
236  return self->thread == g_thread_self();
237 }
G_BEGIN_DECLS FlValue gpointer user_data

◆ fl_engine_send_mouse_pointer_event()

void fl_engine_send_mouse_pointer_event ( FlEngine *  engine,
FlutterPointerPhase  phase,
size_t  timestamp,
double  x,
double  y,
double  scroll_delta_x,
double  scroll_delta_y,
int64_t  buttons 
)

fl_engine_send_mouse_pointer_event: : an #FlEngine. : mouse phase. : time when event occurred in microseconds. : x location of mouse cursor. : y location of mouse cursor. : x offset of scroll. : y offset of scroll. : buttons that are pressed.

Sends a mouse pointer event to the engine.

Definition at line 567 of file fl_engine.cc.

References FlutterPointerEvent::buttons, FlutterPointerEvent::device_kind, FlutterEngineSendPointerEvent(), kFlutterPointerDeviceKindMouse, kFlutterPointerSignalKindScroll, phase, FlutterPointerEvent::phase, FlutterPointerEvent::scroll_delta_x, FlutterPointerEvent::scroll_delta_y, FlutterPointerEvent::signal_kind, FlutterPointerEvent::struct_size, FlutterPointerEvent::timestamp, FlutterPointerEvent::x, and FlutterPointerEvent::y.

574  {
575  g_return_if_fail(FL_IS_ENGINE(self));
576 
577  if (self->engine == nullptr) {
578  return;
579  }
580 
581  FlutterPointerEvent fl_event = {};
582  fl_event.struct_size = sizeof(fl_event);
583  fl_event.phase = phase;
584  fl_event.timestamp = timestamp;
585  fl_event.x = x;
586  fl_event.y = y;
587  if (scroll_delta_x != 0 || scroll_delta_y != 0) {
589  }
590  fl_event.scroll_delta_x = scroll_delta_x;
591  fl_event.scroll_delta_y = scroll_delta_y;
593  fl_event.buttons = buttons;
594  FlutterEngineSendPointerEvent(self->engine, &fl_event, 1);
595 }
double scroll_delta_y
The y offset of the scroll in physical pixels.
Definition: embedder.h:549
double y
The y coordinate of the pointer event in physical pixels.
Definition: embedder.h:541
double scroll_delta_x
The x offset of the scroll in physical pixels.
Definition: embedder.h:547
size_t struct_size
The size of this struct. Must be sizeof(FlutterPointerEvent).
Definition: embedder.h:532
FlutterPointerPhase phase
Definition: fl_view.cc:78
double x
The x coordinate of the pointer event in physical pixels.
Definition: embedder.h:539
int64_t buttons
The buttons currently pressed, if any.
Definition: embedder.h:557
FlutterPointerSignalKind signal_kind
Definition: embedder.h:545
FlutterEngineResult FlutterEngineSendPointerEvent(FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterPointerEvent *pointers, size_t events_count)
Definition: embedder.cc:1323
FlutterPointerDeviceKind device_kind
Definition: embedder.h:555
FlutterPointerPhase phase
Definition: embedder.h:533

◆ fl_engine_send_platform_message()

void fl_engine_send_platform_message ( FlEngine *  engine,
const gchar *  channel,
GBytes *  message,
GCancellable *  cancellable,
GAsyncReadyCallback  callback,
gpointer  user_data 
)

fl_engine_send_platform_message: : an #FlEngine. : channel to send to. : (allow-none): message buffer to send or NULL for an empty message : (allow-none): a #GCancellable or NULL. : (scope async): a #GAsyncReadyCallback to call when the request is satisfied. : (closure): user data to pass to .

Asynchronously sends a platform message.

Definition at line 483 of file fl_engine.cc.

References FlutterPlatformMessage::channel, FL_ENGINE_ERROR_FAILED, fl_engine_error_quark(), fl_engine_platform_message_response_cb(), FlutterEngineSendPlatformMessage(), FlutterPlatformMessageCreateResponseHandle(), FlutterPlatformMessageReleaseResponseHandle(), kSuccess, FlutterPlatformMessage::message, FlutterPlatformMessage::message_size, FlutterPlatformMessage::response_handle, and FlutterPlatformMessage::struct_size.

Referenced by fl_binary_messenger_send_on_channel().

488  {
489  g_return_if_fail(FL_IS_ENGINE(self));
490 
491  GTask* task = nullptr;
492  FlutterPlatformMessageResponseHandle* response_handle = nullptr;
493  if (callback != nullptr) {
494  task = g_task_new(self, cancellable, callback, user_data);
495 
496  if (self->engine == nullptr) {
497  g_task_return_new_error(task, fl_engine_error_quark(),
498  FL_ENGINE_ERROR_FAILED, "No engine to send to");
499  return;
500  }
501 
503  self->engine, fl_engine_platform_message_response_cb, task,
504  &response_handle);
505  if (result != kSuccess) {
506  g_task_return_new_error(task, fl_engine_error_quark(),
508  "Failed to create response handle");
509  g_object_unref(task);
510  return;
511  }
512  } else if (self->engine == nullptr) {
513  return;
514  }
515 
516  FlutterPlatformMessage fl_message = {};
517  fl_message.struct_size = sizeof(fl_message);
518  fl_message.channel = channel;
519  fl_message.message =
520  message != nullptr
521  ? static_cast<const uint8_t*>(g_bytes_get_data(message, nullptr))
522  : nullptr;
523  fl_message.message_size = message != nullptr ? g_bytes_get_size(message) : 0;
524  fl_message.response_handle = response_handle;
525  FlutterEngineResult result =
526  FlutterEngineSendPlatformMessage(self->engine, &fl_message);
527 
528  if (result != kSuccess && task != nullptr) {
529  g_task_return_new_error(task, fl_engine_error_quark(),
531  "Failed to send platform messages");
532  g_object_unref(task);
533  }
534 
535  if (response_handle != nullptr) {
536  FlutterPlatformMessageReleaseResponseHandle(self->engine, response_handle);
537  }
538 }
const char * channel
Definition: embedder.h:567
FlutterEngineResult FlutterPlatformMessageReleaseResponseHandle(FLUTTER_API_SYMBOL(FlutterEngine) engine, FlutterPlatformMessageResponseHandle *response)
Collects the handle created using FlutterPlatformMessageCreateResponseHandle.
Definition: embedder.cc:1483
const FlutterPlatformMessageResponseHandle * response_handle
Definition: embedder.h:576
G_BEGIN_DECLS FlValue gpointer user_data
const uint8_t * message
Definition: embedder.h:568
FlutterEngineResult
Definition: embedder.h:65
static void fl_engine_platform_message_response_cb(const uint8_t *data, size_t data_length, void *user_data)
Definition: fl_engine.cc:278
size_t struct_size
The size of this struct. Must be sizeof(FlutterPlatformMessage).
Definition: embedder.h:566
GQuark fl_engine_error_quark(void) G_GNUC_CONST
FlutterEngineResult FlutterEngineSendPlatformMessage(FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterPlatformMessage *flutter_message)
Definition: embedder.cc:1396
FlutterEngineResult FlutterPlatformMessageCreateResponseHandle(FLUTTER_API_SYMBOL(FlutterEngine) engine, FlutterDataCallback data_callback, void *user_data, FlutterPlatformMessageResponseHandle **response_out)
Creates a platform message response handle that allows the embedder to set a native callback for a re...
Definition: embedder.cc:1448

◆ fl_engine_send_platform_message_finish()

GBytes* fl_engine_send_platform_message_finish ( FlEngine *  engine,
GAsyncResult *  result,
GError **  error 
)

fl_engine_send_platform_message_finish: : an #FlEngine.

Returns
: a #GAsyncResult. : (allow-none): #GError location to store the error occurring, or NULL to ignore.

Completes request started with fl_engine_send_platform_message().

Returns: message response on success or NULL on error.

Definition at line 540 of file fl_engine.cc.

Referenced by fl_binary_messenger_send_on_channel_finish().

542  {
543  g_return_val_if_fail(FL_IS_ENGINE(self), FALSE);
544  g_return_val_if_fail(g_task_is_valid(result, self), FALSE);
545 
546  return static_cast<GBytes*>(g_task_propagate_pointer(G_TASK(result), error));
547 }
FlMethodResponse GError ** error

◆ fl_engine_send_platform_message_response()

gboolean fl_engine_send_platform_message_response ( FlEngine *  engine,
const FlutterPlatformMessageResponseHandle handle,
GBytes *  response,
GError **  error 
)

fl_engine_send_platform_message_response: : an #FlEngine. : handle that was provided in FlEnginePlatformMessageHandler. : (allow-none): response to send or NULL for an empty response. : (allow-none): #GError location to store the error occurring, or NULL to ignore.

Responds to a platform message.

Returns: TRUE on success.

Definition at line 451 of file fl_engine.cc.

References FL_ENGINE_ERROR_FAILED, fl_engine_error_quark(), FlutterEngineSendPlatformMessageResponse(), kSuccess, and TRUE.

Referenced by fl_binary_messenger_send_response(), and fl_engine_platform_message_cb().

455  {
456  g_return_val_if_fail(FL_IS_ENGINE(self), FALSE);
457  g_return_val_if_fail(handle != nullptr, FALSE);
458 
459  if (self->engine == nullptr) {
461  "No engine to send response to");
462  return FALSE;
463  }
464 
465  gsize data_length = 0;
466  const uint8_t* data = nullptr;
467  if (response != nullptr) {
468  data =
469  static_cast<const uint8_t*>(g_bytes_get_data(response, &data_length));
470  }
472  self->engine, handle, data, data_length);
473 
474  if (result != kSuccess) {
476  "Failed to send platorm message response");
477  return FALSE;
478  }
479 
480  return TRUE;
481 }
FlMethodResponse GError ** error
FlutterEngineResult FlutterEngineSendPlatformMessageResponse(FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterPlatformMessageResponseHandle *handle, const uint8_t *data, size_t data_length)
Send a response from the native side to a platform message from the Dart Flutter application.
Definition: embedder.cc:1497
FlutterEngineResult
Definition: embedder.h:65
return TRUE
Definition: fl_view.cc:107
GQuark fl_engine_error_quark(void) G_GNUC_CONST

◆ fl_engine_send_window_metrics_event()

void fl_engine_send_window_metrics_event ( FlEngine *  engine,
size_t  width,
size_t  height,
double  pixel_ratio 
)

fl_engine_send_window_metrics_event: : an #FlEngine. : width of the window in pixels. : height of the window in pixels. : scale factor for window.

Sends a window metrics event to the engine.

Definition at line 549 of file fl_engine.cc.

References event, FlutterEngineSendWindowMetricsEvent(), height, FlutterWindowMetricsEvent::struct_size, and width.

Referenced by fl_view_geometry_changed().

552  {
553  g_return_if_fail(FL_IS_ENGINE(self));
554 
555  if (self->engine == nullptr) {
556  return;
557  }
558 
559  FlutterWindowMetricsEvent event = {};
560  event.struct_size = sizeof(FlutterWindowMetricsEvent);
561  event.width = width;
562  event.height = height;
563  event.pixel_ratio = pixel_ratio;
565 }
int32_t height
int32_t width
size_t struct_size
The size of this struct. Must be sizeof(FlutterWindowMetricsEvent).
Definition: embedder.h:457
GdkEventButton * event
Definition: fl_view.cc:62
FlutterEngineResult FlutterEngineSendWindowMetricsEvent(FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterWindowMetricsEvent *flutter_metrics)
Definition: embedder.cc:1229

◆ fl_engine_set_platform_message_handler()

void fl_engine_set_platform_message_handler ( FlEngine *  engine,
FlEnginePlatformMessageHandler  handler,
gpointer  user_data,
GDestroyNotify  destroy_notify 
)

fl_engine_set_platform_message_handler: : an #FlEngine. : function to call when a platform message is received. : (closure): user data to pass to . : (allow-none): a function which gets called to free , or NULL.

Registers the function called when a platform message is reveived. Call fl_engine_send_platform_message_response() with the response to this message. Ownership of FlutterPlatformMessageResponseHandle is transferred to the caller, and the message must be responded to to avoid memory leaks.

Definition at line 433 of file fl_engine.cc.

References user_data.

Referenced by fl_binary_messenger_new().

437  {
438  g_return_if_fail(FL_IS_ENGINE(self));
439  g_return_if_fail(handler != nullptr);
440 
441  if (self->platform_message_handler_destroy_notify) {
442  self->platform_message_handler_destroy_notify(
443  self->platform_message_handler_data);
444  }
445 
446  self->platform_message_handler = handler;
447  self->platform_message_handler_data = user_data;
448  self->platform_message_handler_destroy_notify = destroy_notify;
449 }
G_BEGIN_DECLS FlValue gpointer user_data

◆ fl_engine_start()

gboolean fl_engine_start ( FlEngine *  engine,
GError **  error 
)

fl_engine_start: : an #FlEngine. : (allow-none): #GError location to store the error occurring, or NULL to ignore.

Starts the Flutter engine.

Returns: TRUE on success.

Definition at line 352 of file fl_engine.cc.

References FlutterProjectArgs::aot_data, args, FlutterProjectArgs::assets_path, FlutterOpenGLRendererConfig::clear_current, FlutterProjectArgs::command_line_argc, FlutterProjectArgs::command_line_argv, FlutterProjectArgs::custom_task_runners, FlutterProjectArgs::dart_entrypoint_argc, FlutterProjectArgs::dart_entrypoint_argv, FlutterEngineAOTDataSource::elf_path, FlutterOpenGLRendererConfig::fbo_callback, fl_dart_project_get_aot_library_path(), fl_dart_project_get_assets_path(), fl_dart_project_get_dart_entrypoint_arguments(), fl_dart_project_get_icu_data_path(), fl_dart_project_get_switches(), FL_ENGINE_ERROR_FAILED, fl_engine_error_quark(), fl_engine_gl_clear_current(), fl_engine_gl_get_fbo(), fl_engine_gl_make_current(), fl_engine_gl_make_resource_current(), fl_engine_gl_present(), fl_engine_gl_proc_resolver(), fl_engine_platform_message_cb(), fl_engine_post_task(), fl_engine_runs_task_on_current_thread(), FLUTTER_ENGINE_VERSION, FlutterEngineCreateAOTData(), FlutterEngineInitialize(), FlutterEngineRunInitialized(), FlutterEngineRunsAOTCompiledDartCode(), FlutterOpenGLRendererConfig::gl_proc_resolver, FlutterProjectArgs::icu_data_path, FlutterTaskRunnerDescription::identifier, kFlutterEngineAOTDataSourceTypeElfPath, kOpenGL, kPlatformTaskRunnerIdentifier, kSuccess, FlutterOpenGLRendererConfig::make_current, FlutterOpenGLRendererConfig::make_resource_current, FlutterRendererConfig::open_gl, FlutterProjectArgs::platform_message_callback, FlutterCustomTaskRunners::platform_task_runner, FlutterTaskRunnerDescription::post_task_callback, FlutterOpenGLRendererConfig::present, FlutterTaskRunnerDescription::runs_task_on_current_thread_callback, setup_locales(), FlutterProjectArgs::shutdown_dart_vm_when_done, FlutterOpenGLRendererConfig::struct_size, FlutterTaskRunnerDescription::struct_size, FlutterCustomTaskRunners::struct_size, FlutterProjectArgs::struct_size, TRUE, FlutterRendererConfig::type, FlutterEngineAOTDataSource::type, and FlutterTaskRunnerDescription::user_data.

Referenced by fl_view_realize(), and make_mock_engine().

352  {
353  g_return_val_if_fail(FL_IS_ENGINE(self), FALSE);
354 
355  FlutterRendererConfig config = {};
356  config.type = kOpenGL;
364 
365  FlutterTaskRunnerDescription platform_task_runner = {};
366  platform_task_runner.struct_size = sizeof(FlutterTaskRunnerDescription);
367  platform_task_runner.user_data = self;
368  platform_task_runner.runs_task_on_current_thread_callback =
370  platform_task_runner.post_task_callback = fl_engine_post_task;
371  platform_task_runner.identifier = kPlatformTaskRunnerIdentifier;
372 
373  FlutterCustomTaskRunners custom_task_runners = {};
374  custom_task_runners.struct_size = sizeof(FlutterCustomTaskRunners);
375  custom_task_runners.platform_task_runner = &platform_task_runner;
376 
377  g_autoptr(GPtrArray) command_line_args =
378  fl_dart_project_get_switches(self->project);
379  // FlutterProjectArgs expects a full argv, so when processing it for flags
380  // the first item is treated as the executable and ignored. Add a dummy value
381  // so that all switches are used.
382  g_ptr_array_insert(command_line_args, 0, g_strdup("flutter"));
383 
384  gchar** dart_entrypoint_args =
386 
388  args.struct_size = sizeof(FlutterProjectArgs);
389  args.assets_path = fl_dart_project_get_assets_path(self->project);
390  args.icu_data_path = fl_dart_project_get_icu_data_path(self->project);
391  args.command_line_argc = command_line_args->len;
392  args.command_line_argv =
393  reinterpret_cast<const char* const*>(command_line_args->pdata);
395  args.custom_task_runners = &custom_task_runners;
396  args.shutdown_dart_vm_when_done = true;
397  args.dart_entrypoint_argc = g_strv_length(dart_entrypoint_args);
398  args.dart_entrypoint_argv =
399  reinterpret_cast<const char* const*>(dart_entrypoint_args);
400 
402  FlutterEngineAOTDataSource source = {};
404  source.elf_path = fl_dart_project_get_aot_library_path(self->project);
405  if (FlutterEngineCreateAOTData(&source, &self->aot_data) != kSuccess) {
407  "Failed to create AOT data");
408  return FALSE;
409  }
410  args.aot_data = self->aot_data;
411  }
412 
414  FLUTTER_ENGINE_VERSION, &config, &args, self, &self->engine);
415  if (result != kSuccess) {
417  "Failed to initialize Flutter engine");
418  return FALSE;
419  }
420 
421  result = FlutterEngineRunInitialized(self->engine);
422  if (result != kSuccess) {
424  "Failed to run Flutter engine");
425  return FALSE;
426  }
427 
428  setup_locales(self);
429 
430  return TRUE;
431 }
static void fl_engine_post_task(FlutterTask task, uint64_t target_time_nanos, void *user_data)
Definition: fl_engine.cc:240
G_BEGIN_DECLS FlValue * args
G_MODULE_EXPORT const gchar * fl_dart_project_get_icu_data_path(FlDartProject *self)
const char * icu_data_path
Definition: embedder.h:1199
FlMethodResponse GError ** error
size_t struct_size
The size of this struct. Must be sizeof(FlutterTaskRunnerDescription).
Definition: embedder.h:722
const FlutterCustomTaskRunners * custom_task_runners
Definition: embedder.h:1313
G_MODULE_EXPORT gchar ** fl_dart_project_get_dart_entrypoint_arguments(FlDartProject *self)
G_MODULE_EXPORT const gchar * fl_dart_project_get_aot_library_path(FlDartProject *self)
GPtrArray * fl_dart_project_get_switches(FlDartProject *self)
static bool fl_engine_gl_make_resource_current(void *user_data)
Definition: fl_engine.cc:223
FlutterPlatformMessageCallback platform_message_callback
Definition: embedder.h:1222
static void setup_locales(FlEngine *self)
Definition: fl_engine.cc:98
static void fl_engine_platform_message_cb(const FlutterPlatformMessage *message, void *user_data)
Definition: fl_engine.cc:257
const char *const * command_line_argv
Definition: embedder.h:1217
static bool fl_engine_gl_clear_current(void *user_data)
Definition: fl_engine.cc:198
bool shutdown_dart_vm_when_done
Definition: embedder.h:1332
const char *const * dart_entrypoint_argv
Definition: embedder.h:1386
static bool fl_engine_gl_present(void *user_data)
Definition: fl_engine.cc:213
#define FLUTTER_ENGINE_VERSION
Definition: embedder.h:63
FlutterRendererType type
Definition: embedder.h:448
size_t struct_size
The size of this struct. Must be sizeof(FlutterOpenGLRendererConfig).
Definition: embedder.h:379
BoolCallback make_resource_current
Definition: embedder.h:398
BoolCallback clear_current
Definition: embedder.h:381
static constexpr size_t kPlatformTaskRunnerIdentifier
Definition: fl_engine.cc:22
FlutterEngineResult FlutterEngineInitialize(size_t version, const FlutterRendererConfig *config, const FlutterProjectArgs *args, void *user_data, FLUTTER_API_SYMBOL(FlutterEngine) *engine_out)
Initialize a Flutter engine instance. This does not run the Flutter application code till the Flutter...
Definition: embedder.cc:734
static bool fl_engine_gl_make_current(void *user_data)
Definition: fl_engine.cc:188
const char * assets_path
Definition: embedder.h:1175
BoolCallback make_current
Definition: embedder.h:380
FlutterOpenGLRendererConfig open_gl
Definition: embedder.h:450
FlutterEngineResult
Definition: embedder.h:65
size_t struct_size
The size of this struct. Must be sizeof(FlutterProjectArgs).
Definition: embedder.h:1171
UIntCallback fbo_callback
Definition: embedder.h:391
return TRUE
Definition: fl_view.cc:107
const FlutterTaskRunnerDescription * platform_task_runner
Definition: embedder.h:752
static bool fl_engine_runs_task_on_current_thread(void *user_data)
Definition: fl_engine.cc:234
FlutterTaskRunnerPostTaskCallback post_task_callback
Definition: embedder.h:739
FlutterEngineAOTDataSourceType type
Definition: embedder.h:1126
FlutterEngineAOTData aot_data
Definition: embedder.h:1363
static uint32_t fl_engine_gl_get_fbo(void *user_data)
Definition: fl_engine.cc:208
ProcResolver gl_proc_resolver
Definition: embedder.h:415
size_t struct_size
The size of this struct. Must be sizeof(FlutterCustomTaskRunners).
Definition: embedder.h:747
G_MODULE_EXPORT const gchar * fl_dart_project_get_assets_path(FlDartProject *self)
FlutterEngineResult FlutterEngineRunInitialized(FLUTTER_API_SYMBOL(FlutterEngine) engine)
Runs an initialized engine instance. An engine can be initialized via FlutterEngineInitialize. An initialized instance can only be run once. During and after this call, custom task runners supplied by the embedder are expected to start servicing tasks.
Definition: embedder.cc:1167
bool FlutterEngineRunsAOTCompiledDartCode(void)
Returns if the Flutter engine instance will run AOT compiled Dart code. This call has no threading re...
Definition: embedder.cc:1821
BoolCallback runs_task_on_current_thread_callback
Definition: embedder.h:728
GQuark fl_engine_error_quark(void) G_GNUC_CONST
int command_line_argc
The command line argument count used to initialize the project.
Definition: embedder.h:1201
const char * elf_path
Absolute path to an ELF library file.
Definition: embedder.h:1129
static void * fl_engine_gl_proc_resolver(void *user_data, const char *name)
Definition: fl_engine.cc:183
FlutterEngineResult FlutterEngineCreateAOTData(const FlutterEngineAOTDataSource *source, FlutterEngineAOTData *data_out)
Creates the necessary data structures to launch a Flutter Dart application in AOT mode...
Definition: embedder.cc:597

◆ flutter_source_dispatch()

static gboolean flutter_source_dispatch ( GSource *  source,
GSourceFunc  callback,
gpointer  user_data 
)
static

Definition at line 139 of file fl_engine.cc.

References FlutterEngineRunTask(), FlutterSource, and kSuccess.

141  {
142  FlutterSource* fl_source = reinterpret_cast<FlutterSource*>(source);
143  FlEngine* self = fl_source->engine;
144 
145  FlutterEngineResult result =
146  FlutterEngineRunTask(self->engine, &fl_source->task);
147  if (result != kSuccess) {
148  g_warning("Failed to run Flutter task\n");
149  }
150 
151  return G_SOURCE_REMOVE;
152 }
FlutterEngineResult FlutterEngineRunTask(FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterTask *task)
Inform the engine to run the specified task. This task has been given to the engine via the FlutterTa...
Definition: embedder.cc:1717
FlutterSource
Definition: fl_engine.cc:59
FlutterEngineResult
Definition: embedder.h:65

◆ flutter_source_finalize()

static void flutter_source_finalize ( GSource *  source)
static

Definition at line 162 of file fl_engine.cc.

References engine_weak_notify_cb(), and FlutterSource.

162  {
163  FlutterSource* fl_source = reinterpret_cast<FlutterSource*>(source);
164  if (fl_source->engine != nullptr) {
165  g_object_weak_unref(G_OBJECT(fl_source->engine), engine_weak_notify_cb,
166  fl_source);
167  fl_source->engine = nullptr;
168  }
169 }
static void engine_weak_notify_cb(gpointer user_data, GObject *object)
Definition: fl_engine.cc:155
FlutterSource
Definition: fl_engine.cc:59

◆ G_DEFINE_TYPE_WITH_CODE()

G_DEFINE_TYPE_WITH_CODE ( FlEngine  ,
fl_engine  ,
G_TYPE_OBJECT  ,
G_IMPLEMENT_INTERFACE(fl_plugin_registry_get_type(), fl_engine_plugin_registry_iface_init  
)

Definition at line 47 of file fl_engine.cc.

References engine.

55  {
56  GSource parent;
57  FlEngine* engine;
58  FlutterTask task;
FlutterSource
Definition: fl_engine.cc:59

◆ parse_locale()

static void parse_locale ( const gchar *  locale,
gchar **  language,
gchar **  territory,
gchar **  codeset,
gchar **  modifier 
)
static

Definition at line 62 of file fl_engine.cc.

Referenced by setup_locales().

66  {
67  gchar* l = g_strdup(locale);
68 
69  // Locales are in the form "language[_territory][.codeset][@modifier]"
70  gchar* match = strrchr(l, '@');
71  if (match != nullptr) {
72  *modifier = g_strdup(match + 1);
73  *match = '\0';
74  } else {
75  *modifier = nullptr;
76  }
77 
78  match = strrchr(l, '.');
79  if (match != nullptr) {
80  *codeset = g_strdup(match + 1);
81  *match = '\0';
82  } else {
83  *codeset = nullptr;
84  }
85 
86  match = strrchr(l, '_');
87  if (match != nullptr) {
88  *territory = g_strdup(match + 1);
89  *match = '\0';
90  } else {
91  *territory = nullptr;
92  }
93 
94  *language = l;
95 }

◆ setup_locales()

static void setup_locales ( FlEngine *  self)
static

Definition at line 98 of file fl_engine.cc.

References FlutterLocale::country_code, FlutterEngineUpdateLocales(), kSuccess, FlutterLocale::language_code, parse_locale(), FlutterLocale::script_code, FlutterLocale::struct_size, and FlutterLocale::variant_code.

Referenced by fl_engine_start().

98  {
99  const gchar* const* languages = g_get_language_names();
100  g_autoptr(GPtrArray) locales_array = g_ptr_array_new_with_free_func(g_free);
101  // Helper array to take ownership of the strings passed to Flutter.
102  g_autoptr(GPtrArray) locale_strings = g_ptr_array_new_with_free_func(g_free);
103  for (int i = 0; languages[i] != nullptr; i++) {
104  gchar *language, *territory, *codeset, *modifier;
105  parse_locale(languages[i], &language, &territory, &codeset, &modifier);
106  if (language != nullptr) {
107  g_ptr_array_add(locale_strings, language);
108  }
109  if (territory != nullptr) {
110  g_ptr_array_add(locale_strings, territory);
111  }
112  if (codeset != nullptr) {
113  g_ptr_array_add(locale_strings, codeset);
114  }
115  if (modifier != nullptr) {
116  g_ptr_array_add(locale_strings, modifier);
117  }
118 
119  FlutterLocale* locale =
120  static_cast<FlutterLocale*>(g_malloc0(sizeof(FlutterLocale)));
121  g_ptr_array_add(locales_array, locale);
122  locale->struct_size = sizeof(FlutterLocale);
123  locale->language_code = language;
124  locale->country_code = territory;
125  locale->script_code = codeset;
126  locale->variant_code = modifier;
127  }
128  FlutterLocale** locales =
129  reinterpret_cast<FlutterLocale**>(locales_array->pdata);
131  self->engine, const_cast<const FlutterLocale**>(locales),
132  locales_array->len);
133  if (result != kSuccess) {
134  g_warning("Failed to set up Flutter locales");
135  }
136 }
size_t struct_size
This size of this struct. Must be sizeof(FlutterLocale).
Definition: embedder.h:944
const char * variant_code
Definition: embedder.h:963
const char * script_code
Definition: embedder.h:958
const char * country_code
Definition: embedder.h:953
static void parse_locale(const gchar *locale, gchar **language, gchar **territory, gchar **codeset, gchar **modifier)
Definition: fl_engine.cc:62
FlutterEngineResult FlutterEngineUpdateLocales(FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterLocale **locales, size_t locales_count)
Notify a running engine instance that the locale has been updated. The preferred locale must be the f...
Definition: embedder.cc:1761
FlutterEngineResult
Definition: embedder.h:65
const char * language_code
Definition: embedder.h:948

Variable Documentation

◆ flutter_source_funcs

GSourceFuncs flutter_source_funcs
static
Initial value:
= {
nullptr,
nullptr,
nullptr,
nullptr
}
static gboolean flutter_source_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
Definition: fl_engine.cc:139
static void flutter_source_finalize(GSource *source)
Definition: fl_engine.cc:162

Definition at line 172 of file fl_engine.cc.

◆ FlutterSource

◆ kMicrosecondsPerNanosecond

constexpr int kMicrosecondsPerNanosecond = 1000
static

Definition at line 19 of file fl_engine.cc.

Referenced by fl_engine_post_task().

◆ kPlatformTaskRunnerIdentifier

constexpr size_t kPlatformTaskRunnerIdentifier = 1
static

Definition at line 22 of file fl_engine.cc.

Referenced by fl_engine_start().