Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit c80e405

Browse files
committed
Refactor FlutterEngine usage in Linux shell
1 parent 1dba1ef commit c80e405

14 files changed

Lines changed: 733 additions & 182 deletions

ci/licenses_golden/licenses_flutter

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,8 +1159,17 @@ FILE: ../../../flutter/shell/platform/glfw/public/flutter_glfw.h
11591159
FILE: ../../../flutter/shell/platform/glfw/text_input_plugin.cc
11601160
FILE: ../../../flutter/shell/platform/glfw/text_input_plugin.h
11611161
FILE: ../../../flutter/shell/platform/linux/fl_dart_project.cc
1162+
FILE: ../../../flutter/shell/platform/linux/fl_engine.cc
1163+
FILE: ../../../flutter/shell/platform/linux/fl_engine_impl.cc
1164+
FILE: ../../../flutter/shell/platform/linux/fl_engine_impl.h
1165+
FILE: ../../../flutter/shell/platform/linux/fl_engine_impl_egl.cc
1166+
FILE: ../../../flutter/shell/platform/linux/fl_engine_impl_egl.h
1167+
FILE: ../../../flutter/shell/platform/linux/fl_engine_impl_x11.cc
1168+
FILE: ../../../flutter/shell/platform/linux/fl_engine_impl_x11.h
1169+
FILE: ../../../flutter/shell/platform/linux/fl_engine_private.cc
11621170
FILE: ../../../flutter/shell/platform/linux/fl_view.cc
11631171
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h
1172+
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_engine.h
11641173
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_view.h
11651174
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/flutter_linux.h
11661175
FILE: ../../../flutter/shell/platform/windows/angle_surface_manager.cc

shell/platform/linux/BUILD.gn

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ source_set("flutter_linux") {
5757

5858
sources = [
5959
"fl_dart_project.cc",
60+
"fl_engine.cc",
61+
"fl_engine_impl.cc",
62+
"fl_engine_impl_egl.cc",
63+
"fl_engine_impl_x11.cc",
6064
"fl_view.cc",
6165
]
6266

shell/platform/linux/fl_engine.cc

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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/linux/public/flutter_linux/fl_engine.h"
6+
#include "flutter/shell/platform/linux/fl_engine_private.h"
7+
8+
#include <gmodule.h>
9+
10+
struct _FlEngine {
11+
GObject parent_instance;
12+
13+
FlEngineImpl* impl;
14+
};
15+
16+
enum { PROP_FLUTTER_PROJECT = 1, PROP_LAST };
17+
18+
G_DEFINE_TYPE(FlEngine, fl_engine, G_TYPE_OBJECT)
19+
20+
static void fl_engine_get_property(GObject* object,
21+
guint prop_id,
22+
GValue* value,
23+
GParamSpec* pspec) {
24+
FlEngine* self = FL_ENGINE(object);
25+
26+
switch (prop_id) {
27+
case PROP_FLUTTER_PROJECT:
28+
g_value_set_object(value, fl_engine_impl_get_project(self->impl));
29+
break;
30+
default:
31+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
32+
break;
33+
}
34+
}
35+
36+
static void fl_engine_dispose(GObject* object) {
37+
FlEngine* self = FL_ENGINE(object);
38+
39+
g_clear_object(&self->impl);
40+
41+
G_OBJECT_CLASS(fl_engine_parent_class)->dispose(object);
42+
}
43+
44+
static void fl_engine_class_init(FlEngineClass* klass) {
45+
G_OBJECT_CLASS(klass)->get_property = fl_engine_get_property;
46+
G_OBJECT_CLASS(klass)->dispose = fl_engine_dispose;
47+
48+
g_object_class_install_property(
49+
G_OBJECT_CLASS(klass), PROP_FLUTTER_PROJECT,
50+
g_param_spec_object(
51+
"flutter-project", "flutter-project", "Flutter project in use",
52+
fl_dart_project_get_type(),
53+
static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
54+
}
55+
56+
static void fl_engine_init(FlEngine* self) {}
57+
58+
FlEngine* fl_engine_new(FlEngineImpl* impl) {
59+
FlEngine* self =
60+
static_cast<FlEngine*>(g_object_new(fl_engine_get_type(), NULL));
61+
self->impl = static_cast<FlEngineImpl*>(g_object_ref(impl));
62+
return self;
63+
}
64+
65+
FlEngineImpl* fl_engine_get_impl(FlEngine* self) {
66+
return self->impl;
67+
}
68+
69+
G_MODULE_EXPORT FlDartProject* fl_engine_get_project(FlEngine* self) {
70+
return fl_engine_impl_get_project(self->impl);
71+
}
72+
73+
gboolean fl_engine_start(FlEngine* self, GError** error) {
74+
return fl_engine_impl_start(self->impl, error);
75+
}
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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 "fl_engine_impl.h"
6+
7+
#include "flutter/shell/platform/embedder/embedder.h"
8+
9+
typedef struct {
10+
FlDartProject* project;
11+
FLUTTER_API_SYMBOL(FlutterEngine) engine;
12+
} FlEngineImplPrivate;
13+
14+
enum { PROP_FLUTTER_PROJECT = 1, PROP_LAST };
15+
16+
G_DEFINE_TYPE_WITH_PRIVATE(FlEngineImpl, fl_engine_impl, G_TYPE_OBJECT)
17+
18+
static void* fl_engine_impl_gl_proc_resolver(void* user_data, const char* name) {
19+
FlEngineImpl* self = static_cast<FlEngineImpl*>(user_data);
20+
21+
return FL_ENGINE_IMPL_GET_CLASS(self)->get_proc_address(self, name);
22+
}
23+
24+
static bool fl_engine_impl_gl_make_current(void* user_data) {
25+
FlEngineImpl* self = static_cast<FlEngineImpl*>(user_data);
26+
27+
FL_ENGINE_IMPL_GET_CLASS(self)->make_current(self);
28+
29+
return true;
30+
}
31+
32+
static bool fl_engine_impl_gl_clear_current(void* user_data) {
33+
FlEngineImpl* self = static_cast<FlEngineImpl*>(user_data);
34+
35+
FL_ENGINE_IMPL_GET_CLASS(self)->clear_current(self);
36+
37+
return true;
38+
}
39+
40+
static uint32_t fl_engine_impl_gl_fbo_callback(void* user_data) {
41+
FlEngineImpl* self = static_cast<FlEngineImpl*>(user_data);
42+
43+
return FL_ENGINE_IMPL_GET_CLASS(self)->get_fbo(self);
44+
}
45+
46+
static bool fl_engine_impl_gl_present(void* user_data) {
47+
FlEngineImpl* self = static_cast<FlEngineImpl*>(user_data);
48+
49+
FL_ENGINE_IMPL_GET_CLASS(self)->present(self);
50+
51+
return true;
52+
}
53+
54+
static void fl_engine_impl_set_property(GObject* object,
55+
guint prop_id,
56+
const GValue* value,
57+
GParamSpec* pspec) {
58+
FlEngineImpl* self = FL_ENGINE_IMPL(object);
59+
FlEngineImplPrivate* priv = static_cast<FlEngineImplPrivate*>(
60+
fl_engine_impl_get_instance_private(self));
61+
62+
switch (prop_id) {
63+
case PROP_FLUTTER_PROJECT:
64+
g_set_object(&priv->project,
65+
static_cast<FlDartProject*>(g_value_get_object(value)));
66+
break;
67+
default:
68+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
69+
break;
70+
}
71+
}
72+
73+
static void fl_engine_impl_get_property(GObject* object,
74+
guint prop_id,
75+
GValue* value,
76+
GParamSpec* pspec) {
77+
FlEngineImpl* self = FL_ENGINE_IMPL(object);
78+
FlEngineImplPrivate* priv = static_cast<FlEngineImplPrivate*>(
79+
fl_engine_impl_get_instance_private(self));
80+
81+
switch (prop_id) {
82+
case PROP_FLUTTER_PROJECT:
83+
g_value_set_object(value, priv->project);
84+
break;
85+
default:
86+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
87+
break;
88+
}
89+
}
90+
91+
static void fl_engine_impl_dispose(GObject* object) {
92+
FlEngineImpl* self = FL_ENGINE_IMPL(object);
93+
FlEngineImplPrivate* priv = static_cast<FlEngineImplPrivate*>(
94+
fl_engine_impl_get_instance_private(self));
95+
96+
FlutterEngineDeinitialize(priv->engine);
97+
FlutterEngineShutdown(priv->engine);
98+
99+
g_clear_object(&priv->project);
100+
101+
G_OBJECT_CLASS(fl_engine_impl_parent_class)->dispose(object);
102+
}
103+
104+
/* Default implementation of start method. Classes implementing FlEngine need to
105+
* have set up the environment required for the engine before calling this. */
106+
static gboolean fl_engine_impl_real_start(FlEngineImpl* self, GError** error) {
107+
FlEngineImplPrivate* priv = static_cast<FlEngineImplPrivate*>(
108+
fl_engine_impl_get_instance_private(self));
109+
110+
g_return_val_if_fail(FL_IS_ENGINE_IMPL(self), FALSE);
111+
112+
FlutterRendererConfig config = {};
113+
config.type = kOpenGL;
114+
config.open_gl.struct_size = sizeof(FlutterOpenGLRendererConfig);
115+
config.open_gl.gl_proc_resolver = fl_engine_impl_gl_proc_resolver;
116+
config.open_gl.make_current = fl_engine_impl_gl_make_current;
117+
config.open_gl.clear_current = fl_engine_impl_gl_clear_current;
118+
config.open_gl.fbo_callback = fl_engine_impl_gl_fbo_callback;
119+
config.open_gl.present = fl_engine_impl_gl_present;
120+
121+
FlutterProjectArgs args = {};
122+
args.struct_size = sizeof(FlutterProjectArgs);
123+
args.assets_path = fl_dart_project_get_assets_path(priv->project);
124+
args.icu_data_path = fl_dart_project_get_icu_data_path(priv->project);
125+
126+
FlutterEngineResult result = FlutterEngineInitialize(
127+
FLUTTER_ENGINE_VERSION, &config, &args, self, &priv->engine);
128+
if (result != kSuccess) {
129+
g_set_error(error, fl_engine_error_quark(), FL_ENGINE_ERROR_FAILED,
130+
"Failed to initialize Flutter engine");
131+
return FALSE;
132+
}
133+
134+
result = FlutterEngineRunInitialized(priv->engine);
135+
if (result != kSuccess) {
136+
g_set_error(error, fl_engine_error_quark(), FL_ENGINE_ERROR_FAILED,
137+
"Failed to run Flutter engine");
138+
return FALSE;
139+
}
140+
141+
return TRUE;
142+
}
143+
144+
static void fl_engine_impl_class_init(FlEngineImplClass* klass) {
145+
G_OBJECT_CLASS(klass)->set_property = fl_engine_impl_set_property;
146+
G_OBJECT_CLASS(klass)->get_property = fl_engine_impl_get_property;
147+
G_OBJECT_CLASS(klass)->dispose = fl_engine_impl_dispose;
148+
klass->start = fl_engine_impl_real_start;
149+
150+
g_object_class_install_property(
151+
G_OBJECT_CLASS(klass), PROP_FLUTTER_PROJECT,
152+
g_param_spec_object(
153+
"flutter-project", "flutter-project", "Flutter project in use",
154+
fl_dart_project_get_type(),
155+
static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
156+
G_PARAM_STATIC_STRINGS)));
157+
}
158+
159+
static void fl_engine_impl_init(FlEngineImpl* self) {}
160+
161+
G_DEFINE_QUARK(fl_engine_error_quark, fl_engine_error)
162+
163+
FlDartProject* fl_engine_impl_get_project(FlEngineImpl* self) {
164+
FlEngineImplPrivate* priv = static_cast<FlEngineImplPrivate*>(
165+
fl_engine_impl_get_instance_private(self));
166+
167+
g_return_val_if_fail(FL_IS_ENGINE_IMPL(self), NULL);
168+
169+
return priv->project;
170+
}
171+
172+
gboolean fl_engine_impl_start(FlEngineImpl* self, GError** error) {
173+
return FL_ENGINE_IMPL_GET_CLASS(self)->start(self, error);
174+
}
175+
176+
void fl_engine_impl_send_window_metrics_event(FlEngineImpl* self,
177+
size_t width,
178+
size_t height,
179+
double pixel_ratio) {
180+
FlEngineImplPrivate* priv = static_cast<FlEngineImplPrivate*>(
181+
fl_engine_impl_get_instance_private(self));
182+
183+
FlutterWindowMetricsEvent event = {};
184+
event.struct_size = sizeof(FlutterWindowMetricsEvent);
185+
event.width = width;
186+
event.height = height;
187+
event.pixel_ratio = pixel_ratio;
188+
FlutterEngineSendWindowMetricsEvent(priv->engine, &event);
189+
}
190+
191+
void
192+
fl_engine_impl_send_mouse_pointer_event(FlEngineImpl* self,
193+
FlutterPointerPhase phase,
194+
size_t timestamp,
195+
double x,
196+
double y,
197+
int64_t buttons) {
198+
FlEngineImplPrivate* priv = static_cast<FlEngineImplPrivate*>(
199+
fl_engine_impl_get_instance_private(self));
200+
201+
FlutterPointerEvent fl_event = {};
202+
fl_event.struct_size = sizeof(fl_event);
203+
fl_event.phase = phase;
204+
fl_event.timestamp = timestamp;
205+
fl_event.x = x;
206+
fl_event.y = y;
207+
fl_event.device_kind = kFlutterPointerDeviceKindMouse;
208+
fl_event.buttons = buttons;
209+
FlutterEngineSendPointerEvent(priv->engine, &fl_event, 1);
210+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
#ifndef FLUTTER_SHELL_PLATFORM_LINUX_FL_ENGINE_IMPL_H_
6+
#define FLUTTER_SHELL_PLATFORM_LINUX_FL_ENGINE_IMPL_H_
7+
8+
#include <glib-object.h>
9+
10+
#include "flutter/shell/platform/embedder/embedder.h"
11+
#include "flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h"
12+
13+
G_BEGIN_DECLS
14+
15+
G_DECLARE_DERIVABLE_TYPE(FlEngineImpl, fl_engine_impl, FL, ENGINE_IMPL, GObject)
16+
17+
struct _FlEngineImplClass {
18+
GObjectClass parent_class;
19+
20+
gboolean (*start)(FlEngineImpl* engine, GError** error);
21+
void* (*get_proc_address)(FlEngineImpl* engine, const char* name);
22+
void (*make_current)(FlEngineImpl* engine);
23+
void (*clear_current)(FlEngineImpl* engine);
24+
guint32 (*get_fbo)(FlEngineImpl* engine);
25+
void (*present)(FlEngineImpl* engine);
26+
};
27+
28+
typedef enum {
29+
FL_ENGINE_ERROR_FAILED,
30+
} FlEngineError;
31+
32+
GQuark fl_engine_error_quark(void) G_GNUC_CONST;
33+
34+
FlDartProject* fl_engine_impl_get_project(FlEngineImpl* engine);
35+
36+
gboolean fl_engine_impl_start(FlEngineImpl* engine, GError** error);
37+
38+
void fl_engine_impl_send_window_metrics_event(FlEngineImpl* engine,
39+
size_t width,
40+
size_t height,
41+
double pixel_ratio);
42+
43+
void fl_engine_impl_send_mouse_pointer_event(FlEngineImpl* engine,
44+
FlutterPointerPhase phase,
45+
size_t timestamp,
46+
double x,
47+
double y,
48+
int64_t buttons);
49+
50+
G_END_DECLS
51+
52+
#endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_ENGINE_IMPL_H_

0 commit comments

Comments
 (0)